1 /******************************************************************************
2 *******************************************************************************
4 ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
5 ** Copyright (C) 2004-2005 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"
26 #ifdef CONFIG_DLM_DEBUG
27 int dlm_create_debug_file(struct dlm_ls *ls);
28 void dlm_delete_debug_file(struct dlm_ls *ls);
30 static inline int dlm_create_debug_file(struct dlm_ls *ls) { return 0; }
31 static inline void dlm_delete_debug_file(struct dlm_ls *ls) { }
35 static struct mutex ls_lock;
36 static struct list_head lslist;
37 static spinlock_t lslist_lock;
38 static struct task_struct * scand_task;
41 static ssize_t dlm_control_store(struct dlm_ls *ls, const char *buf, size_t len)
44 int n = simple_strtol(buf, NULL, 0);
59 static ssize_t dlm_event_store(struct dlm_ls *ls, const char *buf, size_t len)
61 ls->ls_uevent_result = simple_strtol(buf, NULL, 0);
62 set_bit(LSFL_UEVENT_WAIT, &ls->ls_flags);
63 wake_up(&ls->ls_uevent_wait);
67 static ssize_t dlm_id_show(struct dlm_ls *ls, char *buf)
69 return sprintf(buf, "%u\n", ls->ls_global_id);
72 static ssize_t dlm_id_store(struct dlm_ls *ls, const char *buf, size_t len)
74 ls->ls_global_id = simple_strtoul(buf, NULL, 0);
78 static ssize_t dlm_recover_status_show(struct dlm_ls *ls, char *buf)
80 uint32_t status = dlm_recover_status(ls);
81 return sprintf(buf, "%x\n", status);
85 struct attribute attr;
86 ssize_t (*show)(struct dlm_ls *, char *);
87 ssize_t (*store)(struct dlm_ls *, const char *, size_t);
90 static struct dlm_attr dlm_attr_control = {
91 .attr = {.name = "control", .mode = S_IWUSR},
92 .store = dlm_control_store
95 static struct dlm_attr dlm_attr_event = {
96 .attr = {.name = "event_done", .mode = S_IWUSR},
97 .store = dlm_event_store
100 static struct dlm_attr dlm_attr_id = {
101 .attr = {.name = "id", .mode = S_IRUGO | S_IWUSR},
103 .store = dlm_id_store
106 static struct dlm_attr dlm_attr_recover_status = {
107 .attr = {.name = "recover_status", .mode = S_IRUGO},
108 .show = dlm_recover_status_show
111 static struct attribute *dlm_attrs[] = {
112 &dlm_attr_control.attr,
113 &dlm_attr_event.attr,
115 &dlm_attr_recover_status.attr,
119 static ssize_t dlm_attr_show(struct kobject *kobj, struct attribute *attr,
122 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
123 struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
124 return a->show ? a->show(ls, buf) : 0;
127 static ssize_t dlm_attr_store(struct kobject *kobj, struct attribute *attr,
128 const char *buf, size_t len)
130 struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
131 struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
132 return a->store ? a->store(ls, buf, len) : len;
135 static struct sysfs_ops dlm_attr_ops = {
136 .show = dlm_attr_show,
137 .store = dlm_attr_store,
140 static struct kobj_type dlm_ktype = {
141 .default_attrs = dlm_attrs,
142 .sysfs_ops = &dlm_attr_ops,
145 static struct kset dlm_kset = {
146 .subsys = &kernel_subsys,
147 .kobj = {.name = "dlm",},
151 static int kobject_setup(struct dlm_ls *ls)
153 char lsname[DLM_LOCKSPACE_LEN];
156 memset(lsname, 0, DLM_LOCKSPACE_LEN);
157 snprintf(lsname, DLM_LOCKSPACE_LEN, "%s", ls->ls_name);
159 error = kobject_set_name(&ls->ls_kobj, "%s", lsname);
163 ls->ls_kobj.kset = &dlm_kset;
164 ls->ls_kobj.ktype = &dlm_ktype;
168 static int do_uevent(struct dlm_ls *ls, int in)
173 kobject_uevent(&ls->ls_kobj, KOBJ_ONLINE);
175 kobject_uevent(&ls->ls_kobj, KOBJ_OFFLINE);
177 error = wait_event_interruptible(ls->ls_uevent_wait,
178 test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags));
182 error = ls->ls_uevent_result;
188 int dlm_lockspace_init(void)
193 mutex_init(&ls_lock);
194 INIT_LIST_HEAD(&lslist);
195 spin_lock_init(&lslist_lock);
197 error = kset_register(&dlm_kset);
199 printk("dlm_lockspace_init: cannot register kset %d\n", error);
203 void dlm_lockspace_exit(void)
205 kset_unregister(&dlm_kset);
208 static int dlm_scand(void *data)
212 while (!kthread_should_stop()) {
213 list_for_each_entry(ls, &lslist, ls_list)
215 schedule_timeout_interruptible(dlm_config.scan_secs * HZ);
220 static int dlm_scand_start(void)
222 struct task_struct *p;
225 p = kthread_run(dlm_scand, NULL, "dlm_scand");
233 static void dlm_scand_stop(void)
235 kthread_stop(scand_task);
238 static struct dlm_ls *dlm_find_lockspace_name(char *name, int namelen)
242 spin_lock(&lslist_lock);
244 list_for_each_entry(ls, &lslist, ls_list) {
245 if (ls->ls_namelen == namelen &&
246 memcmp(ls->ls_name, name, namelen) == 0)
251 spin_unlock(&lslist_lock);
255 struct dlm_ls *dlm_find_lockspace_global(uint32_t id)
259 spin_lock(&lslist_lock);
261 list_for_each_entry(ls, &lslist, ls_list) {
262 if (ls->ls_global_id == id) {
269 spin_unlock(&lslist_lock);
273 struct dlm_ls *dlm_find_lockspace_local(void *id)
275 struct dlm_ls *ls = id;
277 spin_lock(&lslist_lock);
279 spin_unlock(&lslist_lock);
283 void dlm_put_lockspace(struct dlm_ls *ls)
285 spin_lock(&lslist_lock);
287 spin_unlock(&lslist_lock);
290 static void remove_lockspace(struct dlm_ls *ls)
293 spin_lock(&lslist_lock);
294 if (ls->ls_count == 0) {
295 list_del(&ls->ls_list);
296 spin_unlock(&lslist_lock);
299 spin_unlock(&lslist_lock);
304 static int threads_start(void)
308 /* Thread which process lock requests for all lockspace's */
309 error = dlm_astd_start();
311 log_print("cannot start dlm_astd thread %d", error);
315 error = dlm_scand_start();
317 log_print("cannot start dlm_scand thread %d", error);
321 /* Thread for sending/receiving messages for all lockspace's */
322 error = dlm_lowcomms_start();
324 log_print("cannot start dlm lowcomms %d", error);
338 static void threads_stop(void)
345 static int new_lockspace(char *name, int namelen, void **lockspace,
346 uint32_t flags, int lvblen)
349 int i, size, error = -ENOMEM;
351 if (namelen > DLM_LOCKSPACE_LEN)
354 if (!lvblen || (lvblen % 8))
357 if (!try_module_get(THIS_MODULE))
360 ls = dlm_find_lockspace_name(name, namelen);
363 module_put(THIS_MODULE);
367 ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_KERNEL);
370 memcpy(ls->ls_name, name, namelen);
371 ls->ls_namelen = namelen;
372 ls->ls_exflags = flags;
373 ls->ls_lvblen = lvblen;
377 size = dlm_config.rsbtbl_size;
378 ls->ls_rsbtbl_size = size;
380 ls->ls_rsbtbl = kmalloc(sizeof(struct dlm_rsbtable) * size, GFP_KERNEL);
383 for (i = 0; i < size; i++) {
384 INIT_LIST_HEAD(&ls->ls_rsbtbl[i].list);
385 INIT_LIST_HEAD(&ls->ls_rsbtbl[i].toss);
386 rwlock_init(&ls->ls_rsbtbl[i].lock);
389 size = dlm_config.lkbtbl_size;
390 ls->ls_lkbtbl_size = size;
392 ls->ls_lkbtbl = kmalloc(sizeof(struct dlm_lkbtable) * size, GFP_KERNEL);
395 for (i = 0; i < size; i++) {
396 INIT_LIST_HEAD(&ls->ls_lkbtbl[i].list);
397 rwlock_init(&ls->ls_lkbtbl[i].lock);
398 ls->ls_lkbtbl[i].counter = 1;
401 size = dlm_config.dirtbl_size;
402 ls->ls_dirtbl_size = size;
404 ls->ls_dirtbl = kmalloc(sizeof(struct dlm_dirtable) * size, GFP_KERNEL);
407 for (i = 0; i < size; i++) {
408 INIT_LIST_HEAD(&ls->ls_dirtbl[i].list);
409 rwlock_init(&ls->ls_dirtbl[i].lock);
412 INIT_LIST_HEAD(&ls->ls_waiters);
413 mutex_init(&ls->ls_waiters_mutex);
415 INIT_LIST_HEAD(&ls->ls_nodes);
416 INIT_LIST_HEAD(&ls->ls_nodes_gone);
417 ls->ls_num_nodes = 0;
418 ls->ls_low_nodeid = 0;
419 ls->ls_total_weight = 0;
420 ls->ls_node_array = NULL;
422 memset(&ls->ls_stub_rsb, 0, sizeof(struct dlm_rsb));
423 ls->ls_stub_rsb.res_ls = ls;
425 ls->ls_debug_dentry = NULL;
427 init_waitqueue_head(&ls->ls_uevent_wait);
428 ls->ls_uevent_result = 0;
430 ls->ls_recoverd_task = NULL;
431 mutex_init(&ls->ls_recoverd_active);
432 spin_lock_init(&ls->ls_recover_lock);
433 ls->ls_recover_status = 0;
434 ls->ls_recover_seq = 0;
435 ls->ls_recover_args = NULL;
436 init_rwsem(&ls->ls_in_recovery);
437 INIT_LIST_HEAD(&ls->ls_requestqueue);
438 mutex_init(&ls->ls_requestqueue_mutex);
440 ls->ls_recover_buf = kmalloc(dlm_config.buffer_size, GFP_KERNEL);
441 if (!ls->ls_recover_buf)
444 INIT_LIST_HEAD(&ls->ls_recover_list);
445 spin_lock_init(&ls->ls_recover_list_lock);
446 ls->ls_recover_list_count = 0;
447 init_waitqueue_head(&ls->ls_wait_general);
448 INIT_LIST_HEAD(&ls->ls_root_list);
449 init_rwsem(&ls->ls_root_sem);
451 down_write(&ls->ls_in_recovery);
453 error = dlm_recoverd_start(ls);
455 log_error(ls, "can't start dlm_recoverd %d", error);
459 spin_lock(&lslist_lock);
460 list_add(&ls->ls_list, &lslist);
461 spin_unlock(&lslist_lock);
463 dlm_create_debug_file(ls);
465 error = kobject_setup(ls);
469 error = kobject_register(&ls->ls_kobj);
473 error = do_uevent(ls, 1);
481 kobject_unregister(&ls->ls_kobj);
483 dlm_delete_debug_file(ls);
484 spin_lock(&lslist_lock);
485 list_del(&ls->ls_list);
486 spin_unlock(&lslist_lock);
487 dlm_recoverd_stop(ls);
489 kfree(ls->ls_recover_buf);
491 kfree(ls->ls_dirtbl);
493 kfree(ls->ls_lkbtbl);
495 kfree(ls->ls_rsbtbl);
499 module_put(THIS_MODULE);
503 int dlm_new_lockspace(char *name, int namelen, void **lockspace,
504 uint32_t flags, int lvblen)
508 mutex_lock(&ls_lock);
510 error = threads_start();
514 error = new_lockspace(name, namelen, lockspace, flags, lvblen);
518 mutex_unlock(&ls_lock);
522 /* Return 1 if the lockspace still has active remote locks,
523 * 2 if the lockspace still has active local locks.
525 static int lockspace_busy(struct dlm_ls *ls)
527 int i, lkb_found = 0;
530 /* NOTE: We check the lockidtbl here rather than the resource table.
531 This is because there may be LKBs queued as ASTs that have been
532 unlinked from their RSBs and are pending deletion once the AST has
535 for (i = 0; i < ls->ls_lkbtbl_size; i++) {
536 read_lock(&ls->ls_lkbtbl[i].lock);
537 if (!list_empty(&ls->ls_lkbtbl[i].list)) {
539 list_for_each_entry(lkb, &ls->ls_lkbtbl[i].list,
541 if (!lkb->lkb_nodeid) {
542 read_unlock(&ls->ls_lkbtbl[i].lock);
547 read_unlock(&ls->ls_lkbtbl[i].lock);
552 static int release_lockspace(struct dlm_ls *ls, int force)
556 struct list_head *head;
558 int busy = lockspace_busy(ls);
566 dlm_recoverd_stop(ls);
568 remove_lockspace(ls);
570 dlm_delete_debug_file(ls);
574 kfree(ls->ls_recover_buf);
577 * Free direntry structs.
581 kfree(ls->ls_dirtbl);
584 * Free all lkb's on lkbtbl[] lists.
587 for (i = 0; i < ls->ls_lkbtbl_size; i++) {
588 head = &ls->ls_lkbtbl[i].list;
589 while (!list_empty(head)) {
590 lkb = list_entry(head->next, struct dlm_lkb,
593 list_del(&lkb->lkb_idtbl_list);
597 if (lkb->lkb_lvbptr && lkb->lkb_flags & DLM_IFL_MSTCPY)
598 free_lvb(lkb->lkb_lvbptr);
605 kfree(ls->ls_lkbtbl);
608 * Free all rsb's on rsbtbl[] lists
611 for (i = 0; i < ls->ls_rsbtbl_size; i++) {
612 head = &ls->ls_rsbtbl[i].list;
613 while (!list_empty(head)) {
614 rsb = list_entry(head->next, struct dlm_rsb,
617 list_del(&rsb->res_hashchain);
621 head = &ls->ls_rsbtbl[i].toss;
622 while (!list_empty(head)) {
623 rsb = list_entry(head->next, struct dlm_rsb,
625 list_del(&rsb->res_hashchain);
630 kfree(ls->ls_rsbtbl);
633 * Free structures on any other lists
636 kfree(ls->ls_recover_args);
637 dlm_clear_free_entries(ls);
638 dlm_clear_members(ls);
639 dlm_clear_members_gone(ls);
640 kfree(ls->ls_node_array);
641 kobject_unregister(&ls->ls_kobj);
644 mutex_lock(&ls_lock);
648 mutex_unlock(&ls_lock);
650 module_put(THIS_MODULE);
655 * Called when a system has released all its locks and is not going to use the
656 * lockspace any longer. We free everything we're managing for this lockspace.
657 * Remaining nodes will go through the recovery process as if we'd died. The
658 * lockspace must continue to function as usual, participating in recoveries,
659 * until this returns.
661 * Force has 4 possible values:
662 * 0 - don't destroy locksapce if it has any LKBs
663 * 1 - destroy lockspace if it has remote LKBs but not if it has local LKBs
664 * 2 - destroy lockspace regardless of LKBs
665 * 3 - destroy lockspace as part of a forced shutdown
668 int dlm_release_lockspace(void *lockspace, int force)
672 ls = dlm_find_lockspace_local(lockspace);
675 dlm_put_lockspace(ls);
676 return release_lockspace(ls, force);