1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5 ** Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
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.
11 *******************************************************************************
12 ******************************************************************************/
14 #include "dlm_internal.h"
15 #include "lockspace.h"
25 #include "requestqueue.h"
27 #ifdef CONFIG_DLM_DEBUG
28 int dlm_create_debug_file(struct dlm_ls *ls);
29 void dlm_delete_debug_file(struct dlm_ls *ls);
31 static inline int dlm_create_debug_file(struct dlm_ls *ls) { return 0; }
32 static inline void dlm_delete_debug_file(struct dlm_ls *ls) { }
36 static struct mutex ls_lock;
37 static struct list_head lslist;
38 static spinlock_t lslist_lock;
39 static struct task_struct * scand_task;
42 static ssize_t dlm_control_store(struct dlm_ls *ls, const char *buf, size_t len)
45 int n = simple_strtol(buf, NULL, 0);
47 ls = dlm_find_lockspace_local(ls->ls_local_handle);
61 dlm_put_lockspace(ls);
65 static ssize_t dlm_event_store(struct dlm_ls *ls, const char *buf, size_t len)
67 ls->ls_uevent_result = simple_strtol(buf, NULL, 0);
68 set_bit(LSFL_UEVENT_WAIT, &ls->ls_flags);
69 wake_up(&ls->ls_uevent_wait);
73 static ssize_t dlm_id_show(struct dlm_ls *ls, char *buf)
75 return snprintf(buf, PAGE_SIZE, "%u\n", ls->ls_global_id);
78 static ssize_t dlm_id_store(struct dlm_ls *ls, const char *buf, size_t len)
80 ls->ls_global_id = simple_strtoul(buf, NULL, 0);
84 static ssize_t dlm_recover_status_show(struct dlm_ls *ls, char *buf)
86 uint32_t status = dlm_recover_status(ls);
87 return snprintf(buf, PAGE_SIZE, "%x\n", status);
90 static ssize_t dlm_recover_nodeid_show(struct dlm_ls *ls, char *buf)
92 return snprintf(buf, PAGE_SIZE, "%d\n", ls->ls_recover_nodeid);
96 struct attribute attr;
97 ssize_t (*show)(struct dlm_ls *, char *);
98 ssize_t (*store)(struct dlm_ls *, const char *, size_t);
101 static struct dlm_attr dlm_attr_control = {
102 .attr = {.name = "control", .mode = S_IWUSR},
103 .store = dlm_control_store
106 static struct dlm_attr dlm_attr_event = {
107 .attr = {.name = "event_done", .mode = S_IWUSR},
108 .store = dlm_event_store
111 static struct dlm_attr dlm_attr_id = {
112 .attr = {.name = "id", .mode = S_IRUGO | S_IWUSR},
114 .store = dlm_id_store
117 static struct dlm_attr dlm_attr_recover_status = {
118 .attr = {.name = "recover_status", .mode = S_IRUGO},
119 .show = dlm_recover_status_show
122 static struct dlm_attr dlm_attr_recover_nodeid = {
123 .attr = {.name = "recover_nodeid", .mode = S_IRUGO},
124 .show = dlm_recover_nodeid_show
127 static struct attribute *dlm_attrs[] = {
128 &dlm_attr_control.attr,
129 &dlm_attr_event.attr,
131 &dlm_attr_recover_status.attr,
132 &dlm_attr_recover_nodeid.attr,
136 static ssize_t dlm_attr_show(struct kobject *kobj, struct attribute *attr,
139 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
140 struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
141 return a->show ? a->show(ls, buf) : 0;
144 static ssize_t dlm_attr_store(struct kobject *kobj, struct attribute *attr,
145 const char *buf, size_t len)
147 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
148 struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
149 return a->store ? a->store(ls, buf, len) : len;
152 static void lockspace_kobj_release(struct kobject *k)
154 struct dlm_ls *ls = container_of(k, struct dlm_ls, ls_kobj);
158 static struct sysfs_ops dlm_attr_ops = {
159 .show = dlm_attr_show,
160 .store = dlm_attr_store,
163 static struct kobj_type dlm_ktype = {
164 .default_attrs = dlm_attrs,
165 .sysfs_ops = &dlm_attr_ops,
166 .release = lockspace_kobj_release,
169 static struct kset dlm_kset = {
170 .kobj = {.name = "dlm",},
174 static int kobject_setup(struct dlm_ls *ls)
176 char lsname[DLM_LOCKSPACE_LEN];
179 memset(lsname, 0, DLM_LOCKSPACE_LEN);
180 snprintf(lsname, DLM_LOCKSPACE_LEN, "%s", ls->ls_name);
182 error = kobject_set_name(&ls->ls_kobj, "%s", lsname);
186 ls->ls_kobj.kset = &dlm_kset;
187 ls->ls_kobj.ktype = &dlm_ktype;
191 static int do_uevent(struct dlm_ls *ls, int in)
196 kobject_uevent(&ls->ls_kobj, KOBJ_ONLINE);
198 kobject_uevent(&ls->ls_kobj, KOBJ_OFFLINE);
200 error = wait_event_interruptible(ls->ls_uevent_wait,
201 test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags));
205 error = ls->ls_uevent_result;
211 int dlm_lockspace_init(void)
216 mutex_init(&ls_lock);
217 INIT_LIST_HEAD(&lslist);
218 spin_lock_init(&lslist_lock);
220 kobj_set_kset_s(&dlm_kset, kernel_subsys);
221 error = kset_register(&dlm_kset);
223 printk("dlm_lockspace_init: cannot register kset %d\n", error);
227 void dlm_lockspace_exit(void)
229 kset_unregister(&dlm_kset);
232 static int dlm_scand(void *data)
236 while (!kthread_should_stop()) {
237 list_for_each_entry(ls, &lslist, ls_list) {
238 if (dlm_lock_recovery_try(ls)) {
240 dlm_unlock_recovery(ls);
243 schedule_timeout_interruptible(dlm_config.ci_scan_secs * HZ);
248 static int dlm_scand_start(void)
250 struct task_struct *p;
253 p = kthread_run(dlm_scand, NULL, "dlm_scand");
261 static void dlm_scand_stop(void)
263 kthread_stop(scand_task);
266 static struct dlm_ls *dlm_find_lockspace_name(char *name, int namelen)
270 spin_lock(&lslist_lock);
272 list_for_each_entry(ls, &lslist, ls_list) {
273 if (ls->ls_namelen == namelen &&
274 memcmp(ls->ls_name, name, namelen) == 0)
279 spin_unlock(&lslist_lock);
283 struct dlm_ls *dlm_find_lockspace_global(uint32_t id)
287 spin_lock(&lslist_lock);
289 list_for_each_entry(ls, &lslist, ls_list) {
290 if (ls->ls_global_id == id) {
297 spin_unlock(&lslist_lock);
301 struct dlm_ls *dlm_find_lockspace_local(dlm_lockspace_t *lockspace)
305 spin_lock(&lslist_lock);
306 list_for_each_entry(ls, &lslist, ls_list) {
307 if (ls->ls_local_handle == lockspace) {
314 spin_unlock(&lslist_lock);
318 struct dlm_ls *dlm_find_lockspace_device(int minor)
322 spin_lock(&lslist_lock);
323 list_for_each_entry(ls, &lslist, ls_list) {
324 if (ls->ls_device.minor == minor) {
331 spin_unlock(&lslist_lock);
335 void dlm_put_lockspace(struct dlm_ls *ls)
337 spin_lock(&lslist_lock);
339 spin_unlock(&lslist_lock);
342 static void remove_lockspace(struct dlm_ls *ls)
345 spin_lock(&lslist_lock);
346 if (ls->ls_count == 0) {
347 list_del(&ls->ls_list);
348 spin_unlock(&lslist_lock);
351 spin_unlock(&lslist_lock);
356 static int threads_start(void)
360 /* Thread which process lock requests for all lockspace's */
361 error = dlm_astd_start();
363 log_print("cannot start dlm_astd thread %d", error);
367 error = dlm_scand_start();
369 log_print("cannot start dlm_scand thread %d", error);
373 /* Thread for sending/receiving messages for all lockspace's */
374 error = dlm_lowcomms_start();
376 log_print("cannot start dlm lowcomms %d", error);
390 static void threads_stop(void)
397 static int new_lockspace(char *name, int namelen, void **lockspace,
398 uint32_t flags, int lvblen)
401 int i, size, error = -ENOMEM;
403 if (namelen > DLM_LOCKSPACE_LEN)
406 if (!lvblen || (lvblen % 8))
409 if (!try_module_get(THIS_MODULE))
412 ls = dlm_find_lockspace_name(name, namelen);
415 module_put(THIS_MODULE);
419 ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_KERNEL);
422 memcpy(ls->ls_name, name, namelen);
423 ls->ls_namelen = namelen;
424 ls->ls_exflags = flags;
425 ls->ls_lvblen = lvblen;
429 size = dlm_config.ci_rsbtbl_size;
430 ls->ls_rsbtbl_size = size;
432 ls->ls_rsbtbl = kmalloc(sizeof(struct dlm_rsbtable) * size, GFP_KERNEL);
435 for (i = 0; i < size; i++) {
436 INIT_LIST_HEAD(&ls->ls_rsbtbl[i].list);
437 INIT_LIST_HEAD(&ls->ls_rsbtbl[i].toss);
438 rwlock_init(&ls->ls_rsbtbl[i].lock);
441 size = dlm_config.ci_lkbtbl_size;
442 ls->ls_lkbtbl_size = size;
444 ls->ls_lkbtbl = kmalloc(sizeof(struct dlm_lkbtable) * size, GFP_KERNEL);
447 for (i = 0; i < size; i++) {
448 INIT_LIST_HEAD(&ls->ls_lkbtbl[i].list);
449 rwlock_init(&ls->ls_lkbtbl[i].lock);
450 ls->ls_lkbtbl[i].counter = 1;
453 size = dlm_config.ci_dirtbl_size;
454 ls->ls_dirtbl_size = size;
456 ls->ls_dirtbl = kmalloc(sizeof(struct dlm_dirtable) * size, GFP_KERNEL);
459 for (i = 0; i < size; i++) {
460 INIT_LIST_HEAD(&ls->ls_dirtbl[i].list);
461 rwlock_init(&ls->ls_dirtbl[i].lock);
464 INIT_LIST_HEAD(&ls->ls_waiters);
465 mutex_init(&ls->ls_waiters_mutex);
466 INIT_LIST_HEAD(&ls->ls_orphans);
467 mutex_init(&ls->ls_orphans_mutex);
469 INIT_LIST_HEAD(&ls->ls_nodes);
470 INIT_LIST_HEAD(&ls->ls_nodes_gone);
471 ls->ls_num_nodes = 0;
472 ls->ls_low_nodeid = 0;
473 ls->ls_total_weight = 0;
474 ls->ls_node_array = NULL;
476 memset(&ls->ls_stub_rsb, 0, sizeof(struct dlm_rsb));
477 ls->ls_stub_rsb.res_ls = ls;
479 ls->ls_debug_rsb_dentry = NULL;
480 ls->ls_debug_waiters_dentry = NULL;
482 init_waitqueue_head(&ls->ls_uevent_wait);
483 ls->ls_uevent_result = 0;
485 ls->ls_recoverd_task = NULL;
486 mutex_init(&ls->ls_recoverd_active);
487 spin_lock_init(&ls->ls_recover_lock);
488 spin_lock_init(&ls->ls_rcom_spin);
489 get_random_bytes(&ls->ls_rcom_seq, sizeof(uint64_t));
490 ls->ls_recover_status = 0;
491 ls->ls_recover_seq = 0;
492 ls->ls_recover_args = NULL;
493 init_rwsem(&ls->ls_in_recovery);
494 INIT_LIST_HEAD(&ls->ls_requestqueue);
495 mutex_init(&ls->ls_requestqueue_mutex);
496 mutex_init(&ls->ls_clear_proc_locks);
498 ls->ls_recover_buf = kmalloc(dlm_config.ci_buffer_size, GFP_KERNEL);
499 if (!ls->ls_recover_buf)
502 INIT_LIST_HEAD(&ls->ls_recover_list);
503 spin_lock_init(&ls->ls_recover_list_lock);
504 ls->ls_recover_list_count = 0;
505 ls->ls_local_handle = ls;
506 init_waitqueue_head(&ls->ls_wait_general);
507 INIT_LIST_HEAD(&ls->ls_root_list);
508 init_rwsem(&ls->ls_root_sem);
510 down_write(&ls->ls_in_recovery);
512 spin_lock(&lslist_lock);
513 list_add(&ls->ls_list, &lslist);
514 spin_unlock(&lslist_lock);
516 /* needs to find ls in lslist */
517 error = dlm_recoverd_start(ls);
519 log_error(ls, "can't start dlm_recoverd %d", error);
523 dlm_create_debug_file(ls);
525 error = kobject_setup(ls);
529 error = kobject_register(&ls->ls_kobj);
533 error = do_uevent(ls, 1);
541 kobject_unregister(&ls->ls_kobj);
543 dlm_delete_debug_file(ls);
544 dlm_recoverd_stop(ls);
546 spin_lock(&lslist_lock);
547 list_del(&ls->ls_list);
548 spin_unlock(&lslist_lock);
549 kfree(ls->ls_recover_buf);
551 kfree(ls->ls_dirtbl);
553 kfree(ls->ls_lkbtbl);
555 kfree(ls->ls_rsbtbl);
559 module_put(THIS_MODULE);
563 int dlm_new_lockspace(char *name, int namelen, void **lockspace,
564 uint32_t flags, int lvblen)
568 mutex_lock(&ls_lock);
570 error = threads_start();
574 error = new_lockspace(name, namelen, lockspace, flags, lvblen);
578 mutex_unlock(&ls_lock);
582 /* Return 1 if the lockspace still has active remote locks,
583 * 2 if the lockspace still has active local locks.
585 static int lockspace_busy(struct dlm_ls *ls)
587 int i, lkb_found = 0;
590 /* NOTE: We check the lockidtbl here rather than the resource table.
591 This is because there may be LKBs queued as ASTs that have been
592 unlinked from their RSBs and are pending deletion once the AST has
595 for (i = 0; i < ls->ls_lkbtbl_size; i++) {
596 read_lock(&ls->ls_lkbtbl[i].lock);
597 if (!list_empty(&ls->ls_lkbtbl[i].list)) {
599 list_for_each_entry(lkb, &ls->ls_lkbtbl[i].list,
601 if (!lkb->lkb_nodeid) {
602 read_unlock(&ls->ls_lkbtbl[i].lock);
607 read_unlock(&ls->ls_lkbtbl[i].lock);
612 static int release_lockspace(struct dlm_ls *ls, int force)
616 struct list_head *head;
618 int busy = lockspace_busy(ls);
626 dlm_recoverd_stop(ls);
628 remove_lockspace(ls);
630 dlm_delete_debug_file(ls);
634 kfree(ls->ls_recover_buf);
637 * Free direntry structs.
641 kfree(ls->ls_dirtbl);
644 * Free all lkb's on lkbtbl[] lists.
647 for (i = 0; i < ls->ls_lkbtbl_size; i++) {
648 head = &ls->ls_lkbtbl[i].list;
649 while (!list_empty(head)) {
650 lkb = list_entry(head->next, struct dlm_lkb,
653 list_del(&lkb->lkb_idtbl_list);
657 if (lkb->lkb_lvbptr && lkb->lkb_flags & DLM_IFL_MSTCPY)
658 free_lvb(lkb->lkb_lvbptr);
665 kfree(ls->ls_lkbtbl);
668 * Free all rsb's on rsbtbl[] lists
671 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
672 head = &ls->ls_rsbtbl[i].list;
673 while (!list_empty(head)) {
674 rsb = list_entry(head->next, struct dlm_rsb,
677 list_del(&rsb->res_hashchain);
681 head = &ls->ls_rsbtbl[i].toss;
682 while (!list_empty(head)) {
683 rsb = list_entry(head->next, struct dlm_rsb,
685 list_del(&rsb->res_hashchain);
690 kfree(ls->ls_rsbtbl);
693 * Free structures on any other lists
696 dlm_purge_requestqueue(ls);
697 kfree(ls->ls_recover_args);
698 dlm_clear_free_entries(ls);
699 dlm_clear_members(ls);
700 dlm_clear_members_gone(ls);
701 kfree(ls->ls_node_array);
702 kobject_unregister(&ls->ls_kobj);
703 /* The ls structure will be freed when the kobject is done with */
705 mutex_lock(&ls_lock);
709 mutex_unlock(&ls_lock);
711 module_put(THIS_MODULE);
716 * Called when a system has released all its locks and is not going to use the
717 * lockspace any longer. We free everything we're managing for this lockspace.
718 * Remaining nodes will go through the recovery process as if we'd died. The
719 * lockspace must continue to function as usual, participating in recoveries,
720 * until this returns.
722 * Force has 4 possible values:
723 * 0 - don't destroy locksapce if it has any LKBs
724 * 1 - destroy lockspace if it has remote LKBs but not if it has local LKBs
725 * 2 - destroy lockspace regardless of LKBs
726 * 3 - destroy lockspace as part of a forced shutdown
729 int dlm_release_lockspace(void *lockspace, int force)
733 ls = dlm_find_lockspace_local(lockspace);
736 dlm_put_lockspace(ls);
737 return release_lockspace(ls, force);