fs: dlm: use list_first_entry marco
[linux-block.git] / fs / dlm / lockspace.c
CommitLineData
2522fe45 1// SPDX-License-Identifier: GPL-2.0-only
e7fd4179
DT
2/******************************************************************************
3*******************************************************************************
4**
5** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
60f98d18 6** Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
e7fd4179 7**
e7fd4179
DT
8**
9*******************************************************************************
10******************************************************************************/
11
7963b8a5
PG
12#include <linux/module.h>
13
e7fd4179
DT
14#include "dlm_internal.h"
15#include "lockspace.h"
16#include "member.h"
17#include "recoverd.h"
e7fd4179 18#include "dir.h"
a070a91c 19#include "midcomms.h"
e7fd4179
DT
20#include "lowcomms.h"
21#include "config.h"
22#include "memory.h"
23#include "lock.h"
c56b39cd 24#include "recover.h"
2896ee37 25#include "requestqueue.h"
0f8e0d9a 26#include "user.h"
23e8e1aa 27#include "ast.h"
e7fd4179 28
e7fd4179 29static int ls_count;
90135925 30static struct mutex ls_lock;
e7fd4179
DT
31static struct list_head lslist;
32static spinlock_t lslist_lock;
33static struct task_struct * scand_task;
34
35
36static ssize_t dlm_control_store(struct dlm_ls *ls, const char *buf, size_t len)
37{
38 ssize_t ret = len;
6edb5687
FF
39 int n;
40 int rc = kstrtoint(buf, 0, &n);
e7fd4179 41
6edb5687
FF
42 if (rc)
43 return rc;
e2de7f56
PC
44 ls = dlm_find_lockspace_local(ls->ls_local_handle);
45 if (!ls)
46 return -EINVAL;
47
e7fd4179
DT
48 switch (n) {
49 case 0:
50 dlm_ls_stop(ls);
51 break;
52 case 1:
53 dlm_ls_start(ls);
54 break;
55 default:
56 ret = -EINVAL;
57 }
e2de7f56 58 dlm_put_lockspace(ls);
e7fd4179
DT
59 return ret;
60}
61
62static ssize_t dlm_event_store(struct dlm_ls *ls, const char *buf, size_t len)
63{
6edb5687
FF
64 int rc = kstrtoint(buf, 0, &ls->ls_uevent_result);
65
66 if (rc)
67 return rc;
e7fd4179
DT
68 set_bit(LSFL_UEVENT_WAIT, &ls->ls_flags);
69 wake_up(&ls->ls_uevent_wait);
70 return len;
71}
72
73static ssize_t dlm_id_show(struct dlm_ls *ls, char *buf)
74{
a1d144c7 75 return snprintf(buf, PAGE_SIZE, "%u\n", ls->ls_global_id);
e7fd4179
DT
76}
77
78static ssize_t dlm_id_store(struct dlm_ls *ls, const char *buf, size_t len)
79{
6edb5687
FF
80 int rc = kstrtouint(buf, 0, &ls->ls_global_id);
81
82 if (rc)
83 return rc;
e7fd4179
DT
84 return len;
85}
86
4875647a
DT
87static ssize_t dlm_nodir_show(struct dlm_ls *ls, char *buf)
88{
89 return snprintf(buf, PAGE_SIZE, "%u\n", dlm_no_directory(ls));
90}
91
92static ssize_t dlm_nodir_store(struct dlm_ls *ls, const char *buf, size_t len)
93{
6edb5687
FF
94 int val;
95 int rc = kstrtoint(buf, 0, &val);
96
97 if (rc)
98 return rc;
4875647a
DT
99 if (val == 1)
100 set_bit(LSFL_NODIR, &ls->ls_flags);
101 return len;
102}
103
c56b39cd
DT
104static ssize_t dlm_recover_status_show(struct dlm_ls *ls, char *buf)
105{
106 uint32_t status = dlm_recover_status(ls);
a1d144c7 107 return snprintf(buf, PAGE_SIZE, "%x\n", status);
c56b39cd
DT
108}
109
faa0f267
DT
110static ssize_t dlm_recover_nodeid_show(struct dlm_ls *ls, char *buf)
111{
a1d144c7 112 return snprintf(buf, PAGE_SIZE, "%d\n", ls->ls_recover_nodeid);
faa0f267
DT
113}
114
e7fd4179
DT
115struct dlm_attr {
116 struct attribute attr;
117 ssize_t (*show)(struct dlm_ls *, char *);
118 ssize_t (*store)(struct dlm_ls *, const char *, size_t);
119};
120
121static struct dlm_attr dlm_attr_control = {
122 .attr = {.name = "control", .mode = S_IWUSR},
123 .store = dlm_control_store
124};
125
126static struct dlm_attr dlm_attr_event = {
127 .attr = {.name = "event_done", .mode = S_IWUSR},
128 .store = dlm_event_store
129};
130
131static struct dlm_attr dlm_attr_id = {
132 .attr = {.name = "id", .mode = S_IRUGO | S_IWUSR},
133 .show = dlm_id_show,
134 .store = dlm_id_store
135};
136
4875647a
DT
137static struct dlm_attr dlm_attr_nodir = {
138 .attr = {.name = "nodir", .mode = S_IRUGO | S_IWUSR},
139 .show = dlm_nodir_show,
140 .store = dlm_nodir_store
141};
142
c56b39cd
DT
143static struct dlm_attr dlm_attr_recover_status = {
144 .attr = {.name = "recover_status", .mode = S_IRUGO},
145 .show = dlm_recover_status_show
146};
147
faa0f267
DT
148static struct dlm_attr dlm_attr_recover_nodeid = {
149 .attr = {.name = "recover_nodeid", .mode = S_IRUGO},
150 .show = dlm_recover_nodeid_show
151};
152
e7fd4179
DT
153static struct attribute *dlm_attrs[] = {
154 &dlm_attr_control.attr,
155 &dlm_attr_event.attr,
156 &dlm_attr_id.attr,
4875647a 157 &dlm_attr_nodir.attr,
c56b39cd 158 &dlm_attr_recover_status.attr,
faa0f267 159 &dlm_attr_recover_nodeid.attr,
e7fd4179
DT
160 NULL,
161};
c9c5b5e1 162ATTRIBUTE_GROUPS(dlm);
e7fd4179
DT
163
164static ssize_t dlm_attr_show(struct kobject *kobj, struct attribute *attr,
165 char *buf)
166{
167 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
168 struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
169 return a->show ? a->show(ls, buf) : 0;
170}
171
172static ssize_t dlm_attr_store(struct kobject *kobj, struct attribute *attr,
173 const char *buf, size_t len)
174{
175 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
176 struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
177 return a->store ? a->store(ls, buf, len) : len;
178}
179
ba542e3b
PC
180static void lockspace_kobj_release(struct kobject *k)
181{
182 struct dlm_ls *ls = container_of(k, struct dlm_ls, ls_kobj);
183 kfree(ls);
184}
185
52cf25d0 186static const struct sysfs_ops dlm_attr_ops = {
e7fd4179
DT
187 .show = dlm_attr_show,
188 .store = dlm_attr_store,
189};
190
191static struct kobj_type dlm_ktype = {
c9c5b5e1 192 .default_groups = dlm_groups,
e7fd4179 193 .sysfs_ops = &dlm_attr_ops,
ba542e3b 194 .release = lockspace_kobj_release,
e7fd4179
DT
195};
196
d405936b 197static struct kset *dlm_kset;
e7fd4179 198
e7fd4179
DT
199static int do_uevent(struct dlm_ls *ls, int in)
200{
e7fd4179
DT
201 if (in)
202 kobject_uevent(&ls->ls_kobj, KOBJ_ONLINE);
203 else
204 kobject_uevent(&ls->ls_kobj, KOBJ_OFFLINE);
205
075f0177 206 log_rinfo(ls, "%s the lockspace group...", in ? "joining" : "leaving");
8b0e7b2c
DT
207
208 /* dlm_controld will see the uevent, do the necessary group management
209 and then write to sysfs to wake us */
210
f084a4f4
RL
211 wait_event(ls->ls_uevent_wait,
212 test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags));
8b0e7b2c 213
f084a4f4 214 log_rinfo(ls, "group event done %d", ls->ls_uevent_result);
e7fd4179 215
f084a4f4 216 return ls->ls_uevent_result;
e7fd4179
DT
217}
218
cf6299b6 219static int dlm_uevent(struct kobject *kobj, struct kobj_uevent_env *env)
b4a5d4bc
SW
220{
221 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
222
223 add_uevent_var(env, "LOCKSPACE=%s", ls->ls_name);
224 return 0;
225}
226
417f7c59 227static const struct kset_uevent_ops dlm_uevent_ops = {
b4a5d4bc
SW
228 .uevent = dlm_uevent,
229};
e7fd4179 230
30727174 231int __init dlm_lockspace_init(void)
e7fd4179 232{
e7fd4179 233 ls_count = 0;
90135925 234 mutex_init(&ls_lock);
e7fd4179
DT
235 INIT_LIST_HEAD(&lslist);
236 spin_lock_init(&lslist_lock);
237
b4a5d4bc 238 dlm_kset = kset_create_and_add("dlm", &dlm_uevent_ops, kernel_kobj);
d405936b 239 if (!dlm_kset) {
8e24eea7 240 printk(KERN_WARNING "%s: can not create kset\n", __func__);
d405936b
GKH
241 return -ENOMEM;
242 }
243 return 0;
e7fd4179
DT
244}
245
246void dlm_lockspace_exit(void)
247{
d405936b 248 kset_unregister(dlm_kset);
e7fd4179
DT
249}
250
c1dcf65f
DT
251static struct dlm_ls *find_ls_to_scan(void)
252{
253 struct dlm_ls *ls;
254
255 spin_lock(&lslist_lock);
256 list_for_each_entry(ls, &lslist, ls_list) {
257 if (time_after_eq(jiffies, ls->ls_scan_time +
258 dlm_config.ci_scan_secs * HZ)) {
259 spin_unlock(&lslist_lock);
260 return ls;
261 }
262 }
263 spin_unlock(&lslist_lock);
264 return NULL;
265}
266
e7fd4179
DT
267static int dlm_scand(void *data)
268{
269 struct dlm_ls *ls;
270
271 while (!kthread_should_stop()) {
c1dcf65f
DT
272 ls = find_ls_to_scan();
273 if (ls) {
85e86edf 274 if (dlm_lock_recovery_try(ls)) {
c1dcf65f 275 ls->ls_scan_time = jiffies;
85e86edf 276 dlm_scan_rsbs(ls);
3ae1acf9 277 dlm_scan_timeout(ls);
85e86edf 278 dlm_unlock_recovery(ls);
c1dcf65f
DT
279 } else {
280 ls->ls_scan_time += HZ;
85e86edf 281 }
c6ff669b 282 continue;
85e86edf 283 }
c6ff669b 284 schedule_timeout_interruptible(dlm_config.ci_scan_secs * HZ);
e7fd4179
DT
285 }
286 return 0;
287}
288
289static int dlm_scand_start(void)
290{
291 struct task_struct *p;
292 int error = 0;
293
294 p = kthread_run(dlm_scand, NULL, "dlm_scand");
295 if (IS_ERR(p))
296 error = PTR_ERR(p);
297 else
298 scand_task = p;
299 return error;
300}
301
302static void dlm_scand_stop(void)
303{
304 kthread_stop(scand_task);
305}
306
e7fd4179
DT
307struct dlm_ls *dlm_find_lockspace_global(uint32_t id)
308{
309 struct dlm_ls *ls;
310
311 spin_lock(&lslist_lock);
312
313 list_for_each_entry(ls, &lslist, ls_list) {
314 if (ls->ls_global_id == id) {
3cb5977c 315 atomic_inc(&ls->ls_count);
e7fd4179
DT
316 goto out;
317 }
318 }
319 ls = NULL;
320 out:
321 spin_unlock(&lslist_lock);
322 return ls;
323}
324
597d0cae 325struct dlm_ls *dlm_find_lockspace_local(dlm_lockspace_t *lockspace)
e7fd4179 326{
597d0cae 327 struct dlm_ls *ls;
e7fd4179
DT
328
329 spin_lock(&lslist_lock);
597d0cae
DT
330 list_for_each_entry(ls, &lslist, ls_list) {
331 if (ls->ls_local_handle == lockspace) {
3cb5977c 332 atomic_inc(&ls->ls_count);
597d0cae
DT
333 goto out;
334 }
335 }
336 ls = NULL;
337 out:
338 spin_unlock(&lslist_lock);
339 return ls;
340}
341
342struct dlm_ls *dlm_find_lockspace_device(int minor)
343{
344 struct dlm_ls *ls;
345
346 spin_lock(&lslist_lock);
347 list_for_each_entry(ls, &lslist, ls_list) {
348 if (ls->ls_device.minor == minor) {
3cb5977c 349 atomic_inc(&ls->ls_count);
597d0cae
DT
350 goto out;
351 }
352 }
353 ls = NULL;
354 out:
e7fd4179
DT
355 spin_unlock(&lslist_lock);
356 return ls;
357}
358
359void dlm_put_lockspace(struct dlm_ls *ls)
360{
3cb5977c
AA
361 if (atomic_dec_and_test(&ls->ls_count))
362 wake_up(&ls->ls_count_wait);
e7fd4179
DT
363}
364
365static void remove_lockspace(struct dlm_ls *ls)
366{
3cb5977c
AA
367retry:
368 wait_event(ls->ls_count_wait, atomic_read(&ls->ls_count) == 0);
369
370 spin_lock(&lslist_lock);
371 if (atomic_read(&ls->ls_count) != 0) {
e7fd4179 372 spin_unlock(&lslist_lock);
3cb5977c 373 goto retry;
e7fd4179 374 }
3cb5977c
AA
375
376 WARN_ON(ls->ls_create_count != 0);
377 list_del(&ls->ls_list);
378 spin_unlock(&lslist_lock);
e7fd4179
DT
379}
380
381static int threads_start(void)
382{
383 int error;
384
e7fd4179
DT
385 error = dlm_scand_start();
386 if (error) {
387 log_print("cannot start dlm_scand thread %d", error);
23e8e1aa 388 goto fail;
e7fd4179
DT
389 }
390
391 /* Thread for sending/receiving messages for all lockspace's */
a070a91c 392 error = dlm_midcomms_start();
e7fd4179
DT
393 if (error) {
394 log_print("cannot start dlm lowcomms %d", error);
395 goto scand_fail;
396 }
397
398 return 0;
399
400 scand_fail:
401 dlm_scand_stop();
e7fd4179
DT
402 fail:
403 return error;
404}
405
60f98d18
DT
406static int new_lockspace(const char *name, const char *cluster,
407 uint32_t flags, int lvblen,
408 const struct dlm_lockspace_ops *ops, void *ops_arg,
409 int *ops_result, dlm_lockspace_t **lockspace)
e7fd4179
DT
410{
411 struct dlm_ls *ls;
0f8e0d9a 412 int i, size, error;
79d72b54 413 int do_unreg = 0;
60f98d18 414 int namelen = strlen(name);
e7fd4179 415
3f0806d2 416 if (namelen > DLM_LOCKSPACE_LEN || namelen == 0)
e7fd4179
DT
417 return -EINVAL;
418
b5c9d37c 419 if (lvblen % 8)
e7fd4179
DT
420 return -EINVAL;
421
422 if (!try_module_get(THIS_MODULE))
423 return -EINVAL;
424
dc68c7ed 425 if (!dlm_user_daemon_available()) {
60f98d18
DT
426 log_print("dlm user daemon not available");
427 error = -EUNATCH;
428 goto out;
429 }
430
431 if (ops && ops_result) {
432 if (!dlm_config.ci_recover_callbacks)
433 *ops_result = -EOPNOTSUPP;
434 else
435 *ops_result = 0;
436 }
437
3b0e761b
ZL
438 if (!cluster)
439 log_print("dlm cluster name '%s' is being used without an application provided cluster name",
440 dlm_config.ci_cluster_name);
441
60f98d18
DT
442 if (dlm_config.ci_recover_callbacks && cluster &&
443 strncmp(cluster, dlm_config.ci_cluster_name, DLM_LOCKSPACE_LEN)) {
8e174374
GH
444 log_print("dlm cluster name '%s' does not match "
445 "the application cluster name '%s'",
60f98d18
DT
446 dlm_config.ci_cluster_name, cluster);
447 error = -EBADR;
448 goto out;
dc68c7ed
DT
449 }
450
0f8e0d9a
DT
451 error = 0;
452
453 spin_lock(&lslist_lock);
454 list_for_each_entry(ls, &lslist, ls_list) {
455 WARN_ON(ls->ls_create_count <= 0);
456 if (ls->ls_namelen != namelen)
457 continue;
458 if (memcmp(ls->ls_name, name, namelen))
459 continue;
460 if (flags & DLM_LSFL_NEWEXCL) {
461 error = -EEXIST;
462 break;
463 }
464 ls->ls_create_count++;
8511a272
DT
465 *lockspace = ls;
466 error = 1;
0f8e0d9a 467 break;
e7fd4179 468 }
0f8e0d9a
DT
469 spin_unlock(&lslist_lock);
470
0f8e0d9a 471 if (error)
8511a272 472 goto out;
0f8e0d9a
DT
473
474 error = -ENOMEM;
e7fd4179 475
d96d0f96 476 ls = kzalloc(sizeof(*ls), GFP_NOFS);
e7fd4179
DT
477 if (!ls)
478 goto out;
e7fd4179
DT
479 memcpy(ls->ls_name, name, namelen);
480 ls->ls_namelen = namelen;
e7fd4179 481 ls->ls_lvblen = lvblen;
3cb5977c
AA
482 atomic_set(&ls->ls_count, 0);
483 init_waitqueue_head(&ls->ls_count_wait);
e7fd4179 484 ls->ls_flags = 0;
c1dcf65f 485 ls->ls_scan_time = jiffies;
e7fd4179 486
60f98d18
DT
487 if (ops && dlm_config.ci_recover_callbacks) {
488 ls->ls_ops = ops;
489 ls->ls_ops_arg = ops_arg;
490 }
491
81eeb82f 492#ifdef CONFIG_DLM_DEPRECATED_API
6b0afc0c 493 if (flags & DLM_LSFL_TIMEWARN) {
81eeb82f
AA
494 pr_warn_once("===============================================================\n"
495 "WARNING: the dlm DLM_LSFL_TIMEWARN flag is being deprecated and\n"
496 " will be removed in v6.2!\n"
497 " Inclusive DLM_LSFL_TIMEWARN define in UAPI header!\n"
498 "===============================================================\n");
81eeb82f 499
3ae1acf9 500 set_bit(LSFL_TIMEWARN, &ls->ls_flags);
81eeb82f 501 }
3ae1acf9 502
fad59c13 503 /* ls_exflags are forced to match among nodes, and we don't
6b0afc0c
AA
504 * need to require all nodes to have some flags set
505 */
0f8e0d9a
DT
506 ls->ls_exflags = (flags & ~(DLM_LSFL_TIMEWARN | DLM_LSFL_FS |
507 DLM_LSFL_NEWEXCL));
6b0afc0c
AA
508#else
509 /* ls_exflags are forced to match among nodes, and we don't
510 * need to require all nodes to have some flags set
511 */
512 ls->ls_exflags = (flags & ~(DLM_LSFL_FS | DLM_LSFL_NEWEXCL));
513#endif
fad59c13 514
d921a23f 515 size = READ_ONCE(dlm_config.ci_rsbtbl_size);
e7fd4179
DT
516 ls->ls_rsbtbl_size = size;
517
42bc47b3 518 ls->ls_rsbtbl = vmalloc(array_size(size, sizeof(struct dlm_rsbtable)));
e7fd4179
DT
519 if (!ls->ls_rsbtbl)
520 goto out_lsfree;
521 for (i = 0; i < size; i++) {
9beb3bf5
BP
522 ls->ls_rsbtbl[i].keep.rb_node = NULL;
523 ls->ls_rsbtbl[i].toss.rb_node = NULL;
c7be761a 524 spin_lock_init(&ls->ls_rsbtbl[i].lock);
e7fd4179
DT
525 }
526
05c32f47 527 spin_lock_init(&ls->ls_remove_spin);
21d9ac1a 528 init_waitqueue_head(&ls->ls_remove_wait);
05c32f47
DT
529
530 for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++) {
531 ls->ls_remove_names[i] = kzalloc(DLM_RESNAME_MAXLEN+1,
532 GFP_KERNEL);
533 if (!ls->ls_remove_names[i])
534 goto out_rsbtbl;
535 }
536
3d6aa675
DT
537 idr_init(&ls->ls_lkbidr);
538 spin_lock_init(&ls->ls_lkbidr_spin);
e7fd4179 539
e7fd4179 540 INIT_LIST_HEAD(&ls->ls_waiters);
90135925 541 mutex_init(&ls->ls_waiters_mutex);
ef0c2bb0
DT
542 INIT_LIST_HEAD(&ls->ls_orphans);
543 mutex_init(&ls->ls_orphans_mutex);
6b0afc0c 544#ifdef CONFIG_DLM_DEPRECATED_API
3ae1acf9
DT
545 INIT_LIST_HEAD(&ls->ls_timeout);
546 mutex_init(&ls->ls_timeout_mutex);
6b0afc0c 547#endif
e7fd4179 548
3881ac04
DT
549 INIT_LIST_HEAD(&ls->ls_new_rsb);
550 spin_lock_init(&ls->ls_new_rsb_spin);
551
e7fd4179
DT
552 INIT_LIST_HEAD(&ls->ls_nodes);
553 INIT_LIST_HEAD(&ls->ls_nodes_gone);
554 ls->ls_num_nodes = 0;
555 ls->ls_low_nodeid = 0;
556 ls->ls_total_weight = 0;
557 ls->ls_node_array = NULL;
558
559 memset(&ls->ls_stub_rsb, 0, sizeof(struct dlm_rsb));
560 ls->ls_stub_rsb.res_ls = ls;
561
5de6319b
DT
562 ls->ls_debug_rsb_dentry = NULL;
563 ls->ls_debug_waiters_dentry = NULL;
e7fd4179
DT
564
565 init_waitqueue_head(&ls->ls_uevent_wait);
566 ls->ls_uevent_result = 0;
682bb91b
AA
567 init_completion(&ls->ls_recovery_done);
568 ls->ls_recovery_result = -1;
e7fd4179 569
23e8e1aa
DT
570 mutex_init(&ls->ls_cb_mutex);
571 INIT_LIST_HEAD(&ls->ls_cb_delay);
572
e7fd4179 573 ls->ls_recoverd_task = NULL;
90135925 574 mutex_init(&ls->ls_recoverd_active);
e7fd4179 575 spin_lock_init(&ls->ls_recover_lock);
98f176fb
DT
576 spin_lock_init(&ls->ls_rcom_spin);
577 get_random_bytes(&ls->ls_rcom_seq, sizeof(uint64_t));
e7fd4179
DT
578 ls->ls_recover_status = 0;
579 ls->ls_recover_seq = 0;
580 ls->ls_recover_args = NULL;
581 init_rwsem(&ls->ls_in_recovery);
c36258b5 582 init_rwsem(&ls->ls_recv_active);
e7fd4179 583 INIT_LIST_HEAD(&ls->ls_requestqueue);
164d88ab
AA
584 atomic_set(&ls->ls_requestqueue_cnt, 0);
585 init_waitqueue_head(&ls->ls_requestqueue_wait);
90135925 586 mutex_init(&ls->ls_requestqueue_mutex);
296d9d1e 587 spin_lock_init(&ls->ls_clear_proc_locks);
e7fd4179 588
489d8e55
AA
589 /* Due backwards compatibility with 3.1 we need to use maximum
590 * possible dlm message size to be sure the message will fit and
591 * not having out of bounds issues. However on sending side 3.2
592 * might send less.
593 */
d10a0b88 594 ls->ls_recover_buf = kmalloc(DLM_MAX_SOCKET_BUFSIZE, GFP_NOFS);
e7fd4179 595 if (!ls->ls_recover_buf)
05c32f47 596 goto out_lkbidr;
e7fd4179 597
757a4271
DT
598 ls->ls_slot = 0;
599 ls->ls_num_slots = 0;
600 ls->ls_slots_size = 0;
601 ls->ls_slots = NULL;
602
e7fd4179
DT
603 INIT_LIST_HEAD(&ls->ls_recover_list);
604 spin_lock_init(&ls->ls_recover_list_lock);
1d7c484e
DT
605 idr_init(&ls->ls_recover_idr);
606 spin_lock_init(&ls->ls_recover_idr_lock);
e7fd4179 607 ls->ls_recover_list_count = 0;
597d0cae 608 ls->ls_local_handle = ls;
e7fd4179
DT
609 init_waitqueue_head(&ls->ls_wait_general);
610 INIT_LIST_HEAD(&ls->ls_root_list);
611 init_rwsem(&ls->ls_root_sem);
612
5f88f1ea 613 spin_lock(&lslist_lock);
0f8e0d9a 614 ls->ls_create_count = 1;
5f88f1ea
DT
615 list_add(&ls->ls_list, &lslist);
616 spin_unlock(&lslist_lock);
617
23e8e1aa
DT
618 if (flags & DLM_LSFL_FS) {
619 error = dlm_callback_start(ls);
620 if (error) {
621 log_error(ls, "can't start dlm_callback %d", error);
622 goto out_delist;
623 }
624 }
625
475f230c
DT
626 init_waitqueue_head(&ls->ls_recover_lock_wait);
627
628 /*
629 * Once started, dlm_recoverd first looks for ls in lslist, then
630 * initializes ls_in_recovery as locked in "down" mode. We need
631 * to wait for the wakeup from dlm_recoverd because in_recovery
632 * has to start out in down mode.
633 */
634
e7fd4179
DT
635 error = dlm_recoverd_start(ls);
636 if (error) {
637 log_error(ls, "can't start dlm_recoverd %d", error);
23e8e1aa 638 goto out_callback;
e7fd4179
DT
639 }
640
475f230c
DT
641 wait_event(ls->ls_recover_lock_wait,
642 test_bit(LSFL_RECOVER_LOCK, &ls->ls_flags));
643
0ffddafc
WH
644 /* let kobject handle freeing of ls if there's an error */
645 do_unreg = 1;
646
901195ed
GKH
647 ls->ls_kobj.kset = dlm_kset;
648 error = kobject_init_and_add(&ls->ls_kobj, &dlm_ktype, NULL,
649 "%s", ls->ls_name);
e7fd4179 650 if (error)
23e8e1aa 651 goto out_recoverd;
901195ed 652 kobject_uevent(&ls->ls_kobj, KOBJ_ADD);
79d72b54 653
8b0e7b2c
DT
654 /* This uevent triggers dlm_controld in userspace to add us to the
655 group of nodes that are members of this lockspace (managed by the
656 cluster infrastructure.) Once it's done that, it tells us who the
657 current lockspace members are (via configfs) and then tells the
658 lockspace to start running (via sysfs) in dlm_ls_start(). */
659
e7fd4179
DT
660 error = do_uevent(ls, 1);
661 if (error)
23e8e1aa 662 goto out_recoverd;
79d72b54 663
682bb91b
AA
664 /* wait until recovery is successful or failed */
665 wait_for_completion(&ls->ls_recovery_done);
666 error = ls->ls_recovery_result;
8b0e7b2c
DT
667 if (error)
668 goto out_members;
669
79d72b54
DT
670 dlm_create_debug_file(ls);
671
075f0177 672 log_rinfo(ls, "join complete");
e7fd4179
DT
673 *lockspace = ls;
674 return 0;
675
8b0e7b2c
DT
676 out_members:
677 do_uevent(ls, 0);
678 dlm_clear_members(ls);
679 kfree(ls->ls_node_array);
23e8e1aa 680 out_recoverd:
5f88f1ea 681 dlm_recoverd_stop(ls);
23e8e1aa
DT
682 out_callback:
683 dlm_callback_stop(ls);
79d72b54 684 out_delist:
e7fd4179
DT
685 spin_lock(&lslist_lock);
686 list_del(&ls->ls_list);
687 spin_unlock(&lslist_lock);
1d7c484e 688 idr_destroy(&ls->ls_recover_idr);
e7fd4179 689 kfree(ls->ls_recover_buf);
05c32f47 690 out_lkbidr:
3d6aa675 691 idr_destroy(&ls->ls_lkbidr);
b982896c 692 out_rsbtbl:
3456880f
TM
693 for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++)
694 kfree(ls->ls_remove_names[i]);
c282af49 695 vfree(ls->ls_rsbtbl);
e7fd4179 696 out_lsfree:
79d72b54 697 if (do_unreg)
197b12d6 698 kobject_put(&ls->ls_kobj);
79d72b54
DT
699 else
700 kfree(ls);
e7fd4179
DT
701 out:
702 module_put(THIS_MODULE);
703 return error;
704}
705
12cda13c
AA
706static int __dlm_new_lockspace(const char *name, const char *cluster,
707 uint32_t flags, int lvblen,
708 const struct dlm_lockspace_ops *ops,
709 void *ops_arg, int *ops_result,
710 dlm_lockspace_t **lockspace)
e7fd4179
DT
711{
712 int error = 0;
713
90135925 714 mutex_lock(&ls_lock);
e7fd4179
DT
715 if (!ls_count)
716 error = threads_start();
717 if (error)
718 goto out;
719
60f98d18
DT
720 error = new_lockspace(name, cluster, flags, lvblen, ops, ops_arg,
721 ops_result, lockspace);
e7fd4179
DT
722 if (!error)
723 ls_count++;
8511a272
DT
724 if (error > 0)
725 error = 0;
9d232469
AA
726 if (!ls_count) {
727 dlm_scand_stop();
a070a91c 728 dlm_midcomms_shutdown();
9d232469
AA
729 dlm_lowcomms_stop();
730 }
e7fd4179 731 out:
90135925 732 mutex_unlock(&ls_lock);
e7fd4179
DT
733 return error;
734}
735
12cda13c
AA
736int dlm_new_lockspace(const char *name, const char *cluster, uint32_t flags,
737 int lvblen, const struct dlm_lockspace_ops *ops,
738 void *ops_arg, int *ops_result,
739 dlm_lockspace_t **lockspace)
740{
741 return __dlm_new_lockspace(name, cluster, flags | DLM_LSFL_FS, lvblen,
742 ops, ops_arg, ops_result, lockspace);
743}
744
745int dlm_new_user_lockspace(const char *name, const char *cluster,
746 uint32_t flags, int lvblen,
747 const struct dlm_lockspace_ops *ops,
748 void *ops_arg, int *ops_result,
749 dlm_lockspace_t **lockspace)
750{
751 return __dlm_new_lockspace(name, cluster, flags, lvblen, ops,
752 ops_arg, ops_result, lockspace);
753}
754
3d6aa675 755static int lkb_idr_is_local(int id, void *p, void *data)
e7fd4179 756{
3d6aa675
DT
757 struct dlm_lkb *lkb = p;
758
a97f4a66 759 return lkb->lkb_nodeid == 0 && lkb->lkb_grmode != DLM_LOCK_IV;
3d6aa675
DT
760}
761
762static int lkb_idr_is_any(int id, void *p, void *data)
763{
764 return 1;
765}
766
767static int lkb_idr_free(int id, void *p, void *data)
768{
769 struct dlm_lkb *lkb = p;
770
3d6aa675
DT
771 if (lkb->lkb_lvbptr && lkb->lkb_flags & DLM_IFL_MSTCPY)
772 dlm_free_lvb(lkb->lkb_lvbptr);
773
774 dlm_free_lkb(lkb);
775 return 0;
776}
777
778/* NOTE: We check the lkbidr here rather than the resource table.
779 This is because there may be LKBs queued as ASTs that have been unlinked
780 from their RSBs and are pending deletion once the AST has been delivered */
781
782static int lockspace_busy(struct dlm_ls *ls, int force)
783{
784 int rv;
785
786 spin_lock(&ls->ls_lkbidr_spin);
787 if (force == 0) {
788 rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_any, ls);
789 } else if (force == 1) {
790 rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_local, ls);
791 } else {
792 rv = 0;
e7fd4179 793 }
3d6aa675
DT
794 spin_unlock(&ls->ls_lkbidr_spin);
795 return rv;
e7fd4179
DT
796}
797
798static int release_lockspace(struct dlm_ls *ls, int force)
799{
e7fd4179 800 struct dlm_rsb *rsb;
9beb3bf5 801 struct rb_node *n;
0f8e0d9a
DT
802 int i, busy, rv;
803
3d6aa675 804 busy = lockspace_busy(ls, force);
0f8e0d9a
DT
805
806 spin_lock(&lslist_lock);
807 if (ls->ls_create_count == 1) {
3d6aa675 808 if (busy) {
0f8e0d9a 809 rv = -EBUSY;
3d6aa675 810 } else {
0f8e0d9a
DT
811 /* remove_lockspace takes ls off lslist */
812 ls->ls_create_count = 0;
813 rv = 0;
814 }
815 } else if (ls->ls_create_count > 1) {
816 rv = --ls->ls_create_count;
817 } else {
818 rv = -EINVAL;
819 }
820 spin_unlock(&lslist_lock);
821
822 if (rv) {
823 log_debug(ls, "release_lockspace no remove %d", rv);
824 return rv;
825 }
e7fd4179 826
0f8e0d9a 827 dlm_device_deregister(ls);
e7fd4179 828
dc68c7ed 829 if (force < 3 && dlm_user_daemon_available())
e7fd4179
DT
830 do_uevent(ls, 0);
831
832 dlm_recoverd_stop(ls);
833
9d232469
AA
834 if (ls_count == 1) {
835 dlm_scand_stop();
ecd95673 836 dlm_clear_members(ls);
a070a91c 837 dlm_midcomms_shutdown();
9d232469
AA
838 }
839
23e8e1aa
DT
840 dlm_callback_stop(ls);
841
e7fd4179
DT
842 remove_lockspace(ls);
843
844 dlm_delete_debug_file(ls);
845
8fc6ed9a 846 idr_destroy(&ls->ls_recover_idr);
e7fd4179
DT
847 kfree(ls->ls_recover_buf);
848
e7fd4179 849 /*
3d6aa675 850 * Free all lkb's in idr
e7fd4179
DT
851 */
852
3d6aa675 853 idr_for_each(&ls->ls_lkbidr, lkb_idr_free, ls);
3d6aa675 854 idr_destroy(&ls->ls_lkbidr);
e7fd4179 855
e7fd4179
DT
856 /*
857 * Free all rsb's on rsbtbl[] lists
858 */
859
860 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
9beb3bf5
BP
861 while ((n = rb_first(&ls->ls_rsbtbl[i].keep))) {
862 rsb = rb_entry(n, struct dlm_rsb, res_hashnode);
863 rb_erase(n, &ls->ls_rsbtbl[i].keep);
52bda2b5 864 dlm_free_rsb(rsb);
e7fd4179
DT
865 }
866
9beb3bf5
BP
867 while ((n = rb_first(&ls->ls_rsbtbl[i].toss))) {
868 rsb = rb_entry(n, struct dlm_rsb, res_hashnode);
869 rb_erase(n, &ls->ls_rsbtbl[i].toss);
52bda2b5 870 dlm_free_rsb(rsb);
e7fd4179
DT
871 }
872 }
873
c282af49 874 vfree(ls->ls_rsbtbl);
e7fd4179 875
05c32f47
DT
876 for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++)
877 kfree(ls->ls_remove_names[i]);
878
3881ac04
DT
879 while (!list_empty(&ls->ls_new_rsb)) {
880 rsb = list_first_entry(&ls->ls_new_rsb, struct dlm_rsb,
881 res_hashchain);
882 list_del(&rsb->res_hashchain);
883 dlm_free_rsb(rsb);
884 }
885
e7fd4179
DT
886 /*
887 * Free structures on any other lists
888 */
889
2896ee37 890 dlm_purge_requestqueue(ls);
e7fd4179 891 kfree(ls->ls_recover_args);
e7fd4179
DT
892 dlm_clear_members(ls);
893 dlm_clear_members_gone(ls);
894 kfree(ls->ls_node_array);
075f0177 895 log_rinfo(ls, "release_lockspace final free");
197b12d6 896 kobject_put(&ls->ls_kobj);
79d72b54 897 /* The ls structure will be freed when the kobject is done with */
e7fd4179 898
e7fd4179
DT
899 module_put(THIS_MODULE);
900 return 0;
901}
902
903/*
904 * Called when a system has released all its locks and is not going to use the
905 * lockspace any longer. We free everything we're managing for this lockspace.
906 * Remaining nodes will go through the recovery process as if we'd died. The
907 * lockspace must continue to function as usual, participating in recoveries,
908 * until this returns.
909 *
910 * Force has 4 possible values:
bb6866a5 911 * 0 - don't destroy lockspace if it has any LKBs
e7fd4179
DT
912 * 1 - destroy lockspace if it has remote LKBs but not if it has local LKBs
913 * 2 - destroy lockspace regardless of LKBs
914 * 3 - destroy lockspace as part of a forced shutdown
915 */
916
917int dlm_release_lockspace(void *lockspace, int force)
918{
919 struct dlm_ls *ls;
0f8e0d9a 920 int error;
e7fd4179
DT
921
922 ls = dlm_find_lockspace_local(lockspace);
923 if (!ls)
924 return -EINVAL;
925 dlm_put_lockspace(ls);
0f8e0d9a
DT
926
927 mutex_lock(&ls_lock);
928 error = release_lockspace(ls, force);
929 if (!error)
930 ls_count--;
278afcbf 931 if (!ls_count)
9d232469 932 dlm_lowcomms_stop();
0f8e0d9a
DT
933 mutex_unlock(&ls_lock);
934
935 return error;
e7fd4179
DT
936}
937
dc68c7ed
DT
938void dlm_stop_lockspaces(void)
939{
940 struct dlm_ls *ls;
696b3d84 941 int count;
dc68c7ed
DT
942
943 restart:
696b3d84 944 count = 0;
dc68c7ed
DT
945 spin_lock(&lslist_lock);
946 list_for_each_entry(ls, &lslist, ls_list) {
696b3d84
DT
947 if (!test_bit(LSFL_RUNNING, &ls->ls_flags)) {
948 count++;
dc68c7ed 949 continue;
696b3d84 950 }
dc68c7ed
DT
951 spin_unlock(&lslist_lock);
952 log_error(ls, "no userland control daemon, stopping lockspace");
953 dlm_ls_stop(ls);
954 goto restart;
955 }
956 spin_unlock(&lslist_lock);
696b3d84
DT
957
958 if (count)
959 log_print("dlm user daemon left %d lockspaces", count);
dc68c7ed
DT
960}
961
2c3fa6ae
AA
962void dlm_stop_lockspaces_check(void)
963{
964 struct dlm_ls *ls;
965
966 spin_lock(&lslist_lock);
967 list_for_each_entry(ls, &lslist, ls_list) {
968 if (WARN_ON(!rwsem_is_locked(&ls->ls_in_recovery) ||
969 !dlm_locking_stopped(ls)))
970 break;
971 }
972 spin_unlock(&lslist_lock);
973}