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);
46 ls
= dlm_find_lockspace_local(ls
->ls_local_handle
);
60 dlm_put_lockspace(ls
);
64 static ssize_t
dlm_event_store(struct dlm_ls
*ls
, const char *buf
, size_t len
)
66 ls
->ls_uevent_result
= simple_strtol(buf
, NULL
, 0);
67 set_bit(LSFL_UEVENT_WAIT
, &ls
->ls_flags
);
68 wake_up(&ls
->ls_uevent_wait
);
72 static ssize_t
dlm_id_show(struct dlm_ls
*ls
, char *buf
)
74 return snprintf(buf
, PAGE_SIZE
, "%u\n", ls
->ls_global_id
);
77 static ssize_t
dlm_id_store(struct dlm_ls
*ls
, const char *buf
, size_t len
)
79 ls
->ls_global_id
= simple_strtoul(buf
, NULL
, 0);
83 static ssize_t
dlm_recover_status_show(struct dlm_ls
*ls
, char *buf
)
85 uint32_t status
= dlm_recover_status(ls
);
86 return snprintf(buf
, PAGE_SIZE
, "%x\n", status
);
89 static ssize_t
dlm_recover_nodeid_show(struct dlm_ls
*ls
, char *buf
)
91 return snprintf(buf
, PAGE_SIZE
, "%d\n", ls
->ls_recover_nodeid
);
95 struct attribute attr
;
96 ssize_t (*show
)(struct dlm_ls
*, char *);
97 ssize_t (*store
)(struct dlm_ls
*, const char *, size_t);
100 static struct dlm_attr dlm_attr_control
= {
101 .attr
= {.name
= "control", .mode
= S_IWUSR
},
102 .store
= dlm_control_store
105 static struct dlm_attr dlm_attr_event
= {
106 .attr
= {.name
= "event_done", .mode
= S_IWUSR
},
107 .store
= dlm_event_store
110 static struct dlm_attr dlm_attr_id
= {
111 .attr
= {.name
= "id", .mode
= S_IRUGO
| S_IWUSR
},
113 .store
= dlm_id_store
116 static struct dlm_attr dlm_attr_recover_status
= {
117 .attr
= {.name
= "recover_status", .mode
= S_IRUGO
},
118 .show
= dlm_recover_status_show
121 static struct dlm_attr dlm_attr_recover_nodeid
= {
122 .attr
= {.name
= "recover_nodeid", .mode
= S_IRUGO
},
123 .show
= dlm_recover_nodeid_show
126 static struct attribute
*dlm_attrs
[] = {
127 &dlm_attr_control
.attr
,
128 &dlm_attr_event
.attr
,
130 &dlm_attr_recover_status
.attr
,
131 &dlm_attr_recover_nodeid
.attr
,
135 static ssize_t
dlm_attr_show(struct kobject
*kobj
, struct attribute
*attr
,
138 struct dlm_ls
*ls
= container_of(kobj
, struct dlm_ls
, ls_kobj
);
139 struct dlm_attr
*a
= container_of(attr
, struct dlm_attr
, attr
);
140 return a
->show
? a
->show(ls
, buf
) : 0;
143 static ssize_t
dlm_attr_store(struct kobject
*kobj
, struct attribute
*attr
,
144 const char *buf
, size_t len
)
146 struct dlm_ls
*ls
= container_of(kobj
, struct dlm_ls
, ls_kobj
);
147 struct dlm_attr
*a
= container_of(attr
, struct dlm_attr
, attr
);
148 return a
->store
? a
->store(ls
, buf
, len
) : len
;
151 static void lockspace_kobj_release(struct kobject
*k
)
153 struct dlm_ls
*ls
= container_of(k
, struct dlm_ls
, ls_kobj
);
157 static struct sysfs_ops dlm_attr_ops
= {
158 .show
= dlm_attr_show
,
159 .store
= dlm_attr_store
,
162 static struct kobj_type dlm_ktype
= {
163 .default_attrs
= dlm_attrs
,
164 .sysfs_ops
= &dlm_attr_ops
,
165 .release
= lockspace_kobj_release
,
168 static struct kset dlm_kset
= {
169 .subsys
= &kernel_subsys
,
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 error
= kset_register(&dlm_kset
);
222 printk("dlm_lockspace_init: cannot register kset %d\n", error
);
226 void dlm_lockspace_exit(void)
228 kset_unregister(&dlm_kset
);
231 static int dlm_scand(void *data
)
235 while (!kthread_should_stop()) {
236 list_for_each_entry(ls
, &lslist
, ls_list
)
238 schedule_timeout_interruptible(dlm_config
.scan_secs
* HZ
);
243 static int dlm_scand_start(void)
245 struct task_struct
*p
;
248 p
= kthread_run(dlm_scand
, NULL
, "dlm_scand");
256 static void dlm_scand_stop(void)
258 kthread_stop(scand_task
);
261 static struct dlm_ls
*dlm_find_lockspace_name(char *name
, int namelen
)
265 spin_lock(&lslist_lock
);
267 list_for_each_entry(ls
, &lslist
, ls_list
) {
268 if (ls
->ls_namelen
== namelen
&&
269 memcmp(ls
->ls_name
, name
, namelen
) == 0)
274 spin_unlock(&lslist_lock
);
278 struct dlm_ls
*dlm_find_lockspace_global(uint32_t id
)
282 spin_lock(&lslist_lock
);
284 list_for_each_entry(ls
, &lslist
, ls_list
) {
285 if (ls
->ls_global_id
== id
) {
292 spin_unlock(&lslist_lock
);
296 struct dlm_ls
*dlm_find_lockspace_local(dlm_lockspace_t
*lockspace
)
300 spin_lock(&lslist_lock
);
301 list_for_each_entry(ls
, &lslist
, ls_list
) {
302 if (ls
->ls_local_handle
== lockspace
) {
309 spin_unlock(&lslist_lock
);
313 struct dlm_ls
*dlm_find_lockspace_device(int minor
)
317 spin_lock(&lslist_lock
);
318 list_for_each_entry(ls
, &lslist
, ls_list
) {
319 if (ls
->ls_device
.minor
== minor
) {
326 spin_unlock(&lslist_lock
);
330 void dlm_put_lockspace(struct dlm_ls
*ls
)
332 spin_lock(&lslist_lock
);
334 spin_unlock(&lslist_lock
);
337 static void remove_lockspace(struct dlm_ls
*ls
)
340 spin_lock(&lslist_lock
);
341 if (ls
->ls_count
== 0) {
342 list_del(&ls
->ls_list
);
343 spin_unlock(&lslist_lock
);
346 spin_unlock(&lslist_lock
);
351 static int threads_start(void)
355 /* Thread which process lock requests for all lockspace's */
356 error
= dlm_astd_start();
358 log_print("cannot start dlm_astd thread %d", error
);
362 error
= dlm_scand_start();
364 log_print("cannot start dlm_scand thread %d", error
);
368 /* Thread for sending/receiving messages for all lockspace's */
369 error
= dlm_lowcomms_start();
371 log_print("cannot start dlm lowcomms %d", error
);
385 static void threads_stop(void)
392 static int new_lockspace(char *name
, int namelen
, void **lockspace
,
393 uint32_t flags
, int lvblen
)
396 int i
, size
, error
= -ENOMEM
;
398 if (namelen
> DLM_LOCKSPACE_LEN
)
401 if (!lvblen
|| (lvblen
% 8))
404 if (!try_module_get(THIS_MODULE
))
407 ls
= dlm_find_lockspace_name(name
, namelen
);
410 module_put(THIS_MODULE
);
414 ls
= kzalloc(sizeof(struct dlm_ls
) + namelen
, GFP_KERNEL
);
417 memcpy(ls
->ls_name
, name
, namelen
);
418 ls
->ls_namelen
= namelen
;
419 ls
->ls_exflags
= flags
;
420 ls
->ls_lvblen
= lvblen
;
424 size
= dlm_config
.rsbtbl_size
;
425 ls
->ls_rsbtbl_size
= size
;
427 ls
->ls_rsbtbl
= kmalloc(sizeof(struct dlm_rsbtable
) * size
, GFP_KERNEL
);
430 for (i
= 0; i
< size
; i
++) {
431 INIT_LIST_HEAD(&ls
->ls_rsbtbl
[i
].list
);
432 INIT_LIST_HEAD(&ls
->ls_rsbtbl
[i
].toss
);
433 rwlock_init(&ls
->ls_rsbtbl
[i
].lock
);
436 size
= dlm_config
.lkbtbl_size
;
437 ls
->ls_lkbtbl_size
= size
;
439 ls
->ls_lkbtbl
= kmalloc(sizeof(struct dlm_lkbtable
) * size
, GFP_KERNEL
);
442 for (i
= 0; i
< size
; i
++) {
443 INIT_LIST_HEAD(&ls
->ls_lkbtbl
[i
].list
);
444 rwlock_init(&ls
->ls_lkbtbl
[i
].lock
);
445 ls
->ls_lkbtbl
[i
].counter
= 1;
448 size
= dlm_config
.dirtbl_size
;
449 ls
->ls_dirtbl_size
= size
;
451 ls
->ls_dirtbl
= kmalloc(sizeof(struct dlm_dirtable
) * size
, GFP_KERNEL
);
454 for (i
= 0; i
< size
; i
++) {
455 INIT_LIST_HEAD(&ls
->ls_dirtbl
[i
].list
);
456 rwlock_init(&ls
->ls_dirtbl
[i
].lock
);
459 INIT_LIST_HEAD(&ls
->ls_waiters
);
460 mutex_init(&ls
->ls_waiters_mutex
);
462 INIT_LIST_HEAD(&ls
->ls_nodes
);
463 INIT_LIST_HEAD(&ls
->ls_nodes_gone
);
464 ls
->ls_num_nodes
= 0;
465 ls
->ls_low_nodeid
= 0;
466 ls
->ls_total_weight
= 0;
467 ls
->ls_node_array
= NULL
;
469 memset(&ls
->ls_stub_rsb
, 0, sizeof(struct dlm_rsb
));
470 ls
->ls_stub_rsb
.res_ls
= ls
;
472 ls
->ls_debug_rsb_dentry
= NULL
;
473 ls
->ls_debug_waiters_dentry
= NULL
;
475 init_waitqueue_head(&ls
->ls_uevent_wait
);
476 ls
->ls_uevent_result
= 0;
478 ls
->ls_recoverd_task
= NULL
;
479 mutex_init(&ls
->ls_recoverd_active
);
480 spin_lock_init(&ls
->ls_recover_lock
);
481 ls
->ls_recover_status
= 0;
482 ls
->ls_recover_seq
= 0;
483 ls
->ls_recover_args
= NULL
;
484 init_rwsem(&ls
->ls_in_recovery
);
485 INIT_LIST_HEAD(&ls
->ls_requestqueue
);
486 mutex_init(&ls
->ls_requestqueue_mutex
);
487 mutex_init(&ls
->ls_clear_proc_locks
);
489 ls
->ls_recover_buf
= kmalloc(dlm_config
.buffer_size
, GFP_KERNEL
);
490 if (!ls
->ls_recover_buf
)
493 INIT_LIST_HEAD(&ls
->ls_recover_list
);
494 spin_lock_init(&ls
->ls_recover_list_lock
);
495 ls
->ls_recover_list_count
= 0;
496 ls
->ls_local_handle
= ls
;
497 init_waitqueue_head(&ls
->ls_wait_general
);
498 INIT_LIST_HEAD(&ls
->ls_root_list
);
499 init_rwsem(&ls
->ls_root_sem
);
501 down_write(&ls
->ls_in_recovery
);
503 spin_lock(&lslist_lock
);
504 list_add(&ls
->ls_list
, &lslist
);
505 spin_unlock(&lslist_lock
);
507 /* needs to find ls in lslist */
508 error
= dlm_recoverd_start(ls
);
510 log_error(ls
, "can't start dlm_recoverd %d", error
);
514 dlm_create_debug_file(ls
);
516 error
= kobject_setup(ls
);
520 error
= kobject_register(&ls
->ls_kobj
);
524 error
= do_uevent(ls
, 1);
532 kobject_unregister(&ls
->ls_kobj
);
534 dlm_delete_debug_file(ls
);
535 dlm_recoverd_stop(ls
);
537 spin_lock(&lslist_lock
);
538 list_del(&ls
->ls_list
);
539 spin_unlock(&lslist_lock
);
540 kfree(ls
->ls_recover_buf
);
542 kfree(ls
->ls_dirtbl
);
544 kfree(ls
->ls_lkbtbl
);
546 kfree(ls
->ls_rsbtbl
);
550 module_put(THIS_MODULE
);
554 int dlm_new_lockspace(char *name
, int namelen
, void **lockspace
,
555 uint32_t flags
, int lvblen
)
559 mutex_lock(&ls_lock
);
561 error
= threads_start();
565 error
= new_lockspace(name
, namelen
, lockspace
, flags
, lvblen
);
569 mutex_unlock(&ls_lock
);
573 /* Return 1 if the lockspace still has active remote locks,
574 * 2 if the lockspace still has active local locks.
576 static int lockspace_busy(struct dlm_ls
*ls
)
578 int i
, lkb_found
= 0;
581 /* NOTE: We check the lockidtbl here rather than the resource table.
582 This is because there may be LKBs queued as ASTs that have been
583 unlinked from their RSBs and are pending deletion once the AST has
586 for (i
= 0; i
< ls
->ls_lkbtbl_size
; i
++) {
587 read_lock(&ls
->ls_lkbtbl
[i
].lock
);
588 if (!list_empty(&ls
->ls_lkbtbl
[i
].list
)) {
590 list_for_each_entry(lkb
, &ls
->ls_lkbtbl
[i
].list
,
592 if (!lkb
->lkb_nodeid
) {
593 read_unlock(&ls
->ls_lkbtbl
[i
].lock
);
598 read_unlock(&ls
->ls_lkbtbl
[i
].lock
);
603 static int release_lockspace(struct dlm_ls
*ls
, int force
)
607 struct list_head
*head
;
609 int busy
= lockspace_busy(ls
);
617 dlm_recoverd_stop(ls
);
619 remove_lockspace(ls
);
621 dlm_delete_debug_file(ls
);
625 kfree(ls
->ls_recover_buf
);
628 * Free direntry structs.
632 kfree(ls
->ls_dirtbl
);
635 * Free all lkb's on lkbtbl[] lists.
638 for (i
= 0; i
< ls
->ls_lkbtbl_size
; i
++) {
639 head
= &ls
->ls_lkbtbl
[i
].list
;
640 while (!list_empty(head
)) {
641 lkb
= list_entry(head
->next
, struct dlm_lkb
,
644 list_del(&lkb
->lkb_idtbl_list
);
648 if (lkb
->lkb_lvbptr
&& lkb
->lkb_flags
& DLM_IFL_MSTCPY
)
649 free_lvb(lkb
->lkb_lvbptr
);
656 kfree(ls
->ls_lkbtbl
);
659 * Free all rsb's on rsbtbl[] lists
662 for (i
= 0; i
< ls
->ls_rsbtbl_size
; i
++) {
663 head
= &ls
->ls_rsbtbl
[i
].list
;
664 while (!list_empty(head
)) {
665 rsb
= list_entry(head
->next
, struct dlm_rsb
,
668 list_del(&rsb
->res_hashchain
);
672 head
= &ls
->ls_rsbtbl
[i
].toss
;
673 while (!list_empty(head
)) {
674 rsb
= list_entry(head
->next
, struct dlm_rsb
,
676 list_del(&rsb
->res_hashchain
);
681 kfree(ls
->ls_rsbtbl
);
684 * Free structures on any other lists
687 kfree(ls
->ls_recover_args
);
688 dlm_clear_free_entries(ls
);
689 dlm_clear_members(ls
);
690 dlm_clear_members_gone(ls
);
691 kfree(ls
->ls_node_array
);
692 kobject_unregister(&ls
->ls_kobj
);
693 /* The ls structure will be freed when the kobject is done with */
695 mutex_lock(&ls_lock
);
699 mutex_unlock(&ls_lock
);
701 module_put(THIS_MODULE
);
706 * Called when a system has released all its locks and is not going to use the
707 * lockspace any longer. We free everything we're managing for this lockspace.
708 * Remaining nodes will go through the recovery process as if we'd died. The
709 * lockspace must continue to function as usual, participating in recoveries,
710 * until this returns.
712 * Force has 4 possible values:
713 * 0 - don't destroy locksapce if it has any LKBs
714 * 1 - destroy lockspace if it has remote LKBs but not if it has local LKBs
715 * 2 - destroy lockspace regardless of LKBs
716 * 3 - destroy lockspace as part of a forced shutdown
719 int dlm_release_lockspace(void *lockspace
, int force
)
723 ls
= dlm_find_lockspace_local(lockspace
);
726 dlm_put_lockspace(ls
);
727 return release_lockspace(ls
, force
);