fix setattr error handling in sysfs, configfs
[safe/jmp/linux-2.6] / fs / dlm / lockspace.c
index 109333c..f994a7d 100644 (file)
@@ -2,7 +2,7 @@
 *******************************************************************************
 **
 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
-**  Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
+**  Copyright (C) 2004-2008 Red Hat, Inc.  All rights reserved.
 **
 **  This copyrighted material is made available to anyone wishing to use,
 **  modify, copy, or redistribute it subject to the terms and conditions
 #include "memory.h"
 #include "lock.h"
 #include "recover.h"
-
-#ifdef CONFIG_DLM_DEBUG
-int dlm_create_debug_file(struct dlm_ls *ls);
-void dlm_delete_debug_file(struct dlm_ls *ls);
-#else
-static inline int dlm_create_debug_file(struct dlm_ls *ls) { return 0; }
-static inline void dlm_delete_debug_file(struct dlm_ls *ls) { }
-#endif
+#include "requestqueue.h"
+#include "user.h"
 
 static int                     ls_count;
 static struct mutex            ls_lock;
@@ -43,6 +37,10 @@ static ssize_t dlm_control_store(struct dlm_ls *ls, const char *buf, size_t len)
        ssize_t ret = len;
        int n = simple_strtol(buf, NULL, 0);
 
+       ls = dlm_find_lockspace_local(ls->ls_local_handle);
+       if (!ls)
+               return -EINVAL;
+
        switch (n) {
        case 0:
                dlm_ls_stop(ls);
@@ -53,6 +51,7 @@ static ssize_t dlm_control_store(struct dlm_ls *ls, const char *buf, size_t len)
        default:
                ret = -EINVAL;
        }
+       dlm_put_lockspace(ls);
        return ret;
 }
 
@@ -143,7 +142,13 @@ static ssize_t dlm_attr_store(struct kobject *kobj, struct attribute *attr,
        return a->store ? a->store(ls, buf, len) : len;
 }
 
-static struct sysfs_ops dlm_attr_ops = {
+static void lockspace_kobj_release(struct kobject *k)
+{
+       struct dlm_ls *ls  = container_of(k, struct dlm_ls, ls_kobj);
+       kfree(ls);
+}
+
+static const struct sysfs_ops dlm_attr_ops = {
        .show  = dlm_attr_show,
        .store = dlm_attr_store,
 };
@@ -151,30 +156,10 @@ static struct sysfs_ops dlm_attr_ops = {
 static struct kobj_type dlm_ktype = {
        .default_attrs = dlm_attrs,
        .sysfs_ops     = &dlm_attr_ops,
+       .release       = lockspace_kobj_release,
 };
 
-static struct kset dlm_kset = {
-       .subsys = &kernel_subsys,
-       .kobj   = {.name = "dlm",},
-       .ktype  = &dlm_ktype,
-};
-
-static int kobject_setup(struct dlm_ls *ls)
-{
-       char lsname[DLM_LOCKSPACE_LEN];
-       int error;
-
-       memset(lsname, 0, DLM_LOCKSPACE_LEN);
-       snprintf(lsname, DLM_LOCKSPACE_LEN, "%s", ls->ls_name);
-
-       error = kobject_set_name(&ls->ls_kobj, "%s", lsname);
-       if (error)
-               return error;
-
-       ls->ls_kobj.kset = &dlm_kset;
-       ls->ls_kobj.ktype = &dlm_ktype;
-       return 0;
-}
+static struct kset *dlm_kset;
 
 static int do_uevent(struct dlm_ls *ls, int in)
 {
@@ -185,45 +170,95 @@ static int do_uevent(struct dlm_ls *ls, int in)
        else
                kobject_uevent(&ls->ls_kobj, KOBJ_OFFLINE);
 
+       log_debug(ls, "%s the lockspace group...", in ? "joining" : "leaving");
+
+       /* dlm_controld will see the uevent, do the necessary group management
+          and then write to sysfs to wake us */
+
        error = wait_event_interruptible(ls->ls_uevent_wait,
                        test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags));
+
+       log_debug(ls, "group event done %d %d", error, ls->ls_uevent_result);
+
        if (error)
                goto out;
 
        error = ls->ls_uevent_result;
  out:
+       if (error)
+               log_error(ls, "group %s failed %d %d", in ? "join" : "leave",
+                         error, ls->ls_uevent_result);
        return error;
 }
 
-
-int dlm_lockspace_init(void)
+static int dlm_uevent(struct kset *kset, struct kobject *kobj,
+                     struct kobj_uevent_env *env)
 {
-       int error;
+       struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
+
+       add_uevent_var(env, "LOCKSPACE=%s", ls->ls_name);
+       return 0;
+}
+
+static struct kset_uevent_ops dlm_uevent_ops = {
+       .uevent = dlm_uevent,
+};
 
+int __init dlm_lockspace_init(void)
+{
        ls_count = 0;
        mutex_init(&ls_lock);
        INIT_LIST_HEAD(&lslist);
        spin_lock_init(&lslist_lock);
 
-       error = kset_register(&dlm_kset);
-       if (error)
-               printk("dlm_lockspace_init: cannot register kset %d\n", error);
-       return error;
+       dlm_kset = kset_create_and_add("dlm", &dlm_uevent_ops, kernel_kobj);
+       if (!dlm_kset) {
+               printk(KERN_WARNING "%s: can not create kset\n", __func__);
+               return -ENOMEM;
+       }
+       return 0;
 }
 
 void dlm_lockspace_exit(void)
 {
-       kset_unregister(&dlm_kset);
+       kset_unregister(dlm_kset);
+}
+
+static struct dlm_ls *find_ls_to_scan(void)
+{
+       struct dlm_ls *ls;
+
+       spin_lock(&lslist_lock);
+       list_for_each_entry(ls, &lslist, ls_list) {
+               if (time_after_eq(jiffies, ls->ls_scan_time +
+                                           dlm_config.ci_scan_secs * HZ)) {
+                       spin_unlock(&lslist_lock);
+                       return ls;
+               }
+       }
+       spin_unlock(&lslist_lock);
+       return NULL;
 }
 
 static int dlm_scand(void *data)
 {
        struct dlm_ls *ls;
+       int timeout_jiffies = dlm_config.ci_scan_secs * HZ;
 
        while (!kthread_should_stop()) {
-               list_for_each_entry(ls, &lslist, ls_list)
-                       dlm_scan_rsbs(ls);
-               schedule_timeout_interruptible(dlm_config.scan_secs * HZ);
+               ls = find_ls_to_scan();
+               if (ls) {
+                       if (dlm_lock_recovery_try(ls)) {
+                               ls->ls_scan_time = jiffies;
+                               dlm_scan_rsbs(ls);
+                               dlm_scan_timeout(ls);
+                               dlm_unlock_recovery(ls);
+                       } else {
+                               ls->ls_scan_time += HZ;
+                       }
+               } else {
+                       schedule_timeout_interruptible(timeout_jiffies);
+               }
        }
        return 0;
 }
@@ -246,23 +281,6 @@ static void dlm_scand_stop(void)
        kthread_stop(scand_task);
 }
 
-static struct dlm_ls *dlm_find_lockspace_name(char *name, int namelen)
-{
-       struct dlm_ls *ls;
-
-       spin_lock(&lslist_lock);
-
-       list_for_each_entry(ls, &lslist, ls_list) {
-               if (ls->ls_namelen == namelen &&
-                   memcmp(ls->ls_name, name, namelen) == 0)
-                       goto out;
-       }
-       ls = NULL;
- out:
-       spin_unlock(&lslist_lock);
-       return ls;
-}
-
 struct dlm_ls *dlm_find_lockspace_global(uint32_t id)
 {
        struct dlm_ls *ls;
@@ -327,6 +345,7 @@ static void remove_lockspace(struct dlm_ls *ls)
        for (;;) {
                spin_lock(&lslist_lock);
                if (ls->ls_count == 0) {
+                       WARN_ON(ls->ls_create_count != 0);
                        list_del(&ls->ls_list);
                        spin_unlock(&lslist_lock);
                        return;
@@ -377,11 +396,12 @@ static void threads_stop(void)
        dlm_astd_stop();
 }
 
-static int new_lockspace(char *name, int namelen, void **lockspace,
+static int new_lockspace(const char *name, int namelen, void **lockspace,
                         uint32_t flags, int lvblen)
 {
        struct dlm_ls *ls;
-       int i, size, error = -ENOMEM;
+       int i, size, error;
+       int do_unreg = 0;
 
        if (namelen > DLM_LOCKSPACE_LEN)
                return -EINVAL;
@@ -392,39 +412,70 @@ static int new_lockspace(char *name, int namelen, void **lockspace,
        if (!try_module_get(THIS_MODULE))
                return -EINVAL;
 
-       ls = dlm_find_lockspace_name(name, namelen);
-       if (ls) {
-               *lockspace = ls;
+       if (!dlm_user_daemon_available()) {
                module_put(THIS_MODULE);
-               return -EEXIST;
+               return -EUNATCH;
        }
 
-       ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_KERNEL);
+       error = 0;
+
+       spin_lock(&lslist_lock);
+       list_for_each_entry(ls, &lslist, ls_list) {
+               WARN_ON(ls->ls_create_count <= 0);
+               if (ls->ls_namelen != namelen)
+                       continue;
+               if (memcmp(ls->ls_name, name, namelen))
+                       continue;
+               if (flags & DLM_LSFL_NEWEXCL) {
+                       error = -EEXIST;
+                       break;
+               }
+               ls->ls_create_count++;
+               *lockspace = ls;
+               error = 1;
+               break;
+       }
+       spin_unlock(&lslist_lock);
+
+       if (error)
+               goto out;
+
+       error = -ENOMEM;
+
+       ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_NOFS);
        if (!ls)
                goto out;
        memcpy(ls->ls_name, name, namelen);
        ls->ls_namelen = namelen;
-       ls->ls_exflags = flags;
        ls->ls_lvblen = lvblen;
        ls->ls_count = 0;
        ls->ls_flags = 0;
+       ls->ls_scan_time = jiffies;
+
+       if (flags & DLM_LSFL_TIMEWARN)
+               set_bit(LSFL_TIMEWARN, &ls->ls_flags);
 
-       size = dlm_config.rsbtbl_size;
+       /* ls_exflags are forced to match among nodes, and we don't
+          need to require all nodes to have some flags set */
+       ls->ls_exflags = (flags & ~(DLM_LSFL_TIMEWARN | DLM_LSFL_FS |
+                                   DLM_LSFL_NEWEXCL));
+
+       size = dlm_config.ci_rsbtbl_size;
        ls->ls_rsbtbl_size = size;
 
-       ls->ls_rsbtbl = kmalloc(sizeof(struct dlm_rsbtable) * size, GFP_KERNEL);
+       ls->ls_rsbtbl = kmalloc(sizeof(struct dlm_rsbtable) * size, GFP_NOFS);
        if (!ls->ls_rsbtbl)
                goto out_lsfree;
        for (i = 0; i < size; i++) {
                INIT_LIST_HEAD(&ls->ls_rsbtbl[i].list);
                INIT_LIST_HEAD(&ls->ls_rsbtbl[i].toss);
-               rwlock_init(&ls->ls_rsbtbl[i].lock);
+               spin_lock_init(&ls->ls_rsbtbl[i].lock);
        }
 
-       size = dlm_config.lkbtbl_size;
+       size = dlm_config.ci_lkbtbl_size;
        ls->ls_lkbtbl_size = size;
 
-       ls->ls_lkbtbl = kmalloc(sizeof(struct dlm_lkbtable) * size, GFP_KERNEL);
+       ls->ls_lkbtbl = kmalloc(sizeof(struct dlm_lkbtable) * size, GFP_NOFS);
        if (!ls->ls_lkbtbl)
                goto out_rsbfree;
        for (i = 0; i < size; i++) {
@@ -433,19 +484,23 @@ static int new_lockspace(char *name, int namelen, void **lockspace,
                ls->ls_lkbtbl[i].counter = 1;
        }
 
-       size = dlm_config.dirtbl_size;
+       size = dlm_config.ci_dirtbl_size;
        ls->ls_dirtbl_size = size;
 
-       ls->ls_dirtbl = kmalloc(sizeof(struct dlm_dirtable) * size, GFP_KERNEL);
+       ls->ls_dirtbl = kmalloc(sizeof(struct dlm_dirtable) * size, GFP_NOFS);
        if (!ls->ls_dirtbl)
                goto out_lkbfree;
        for (i = 0; i < size; i++) {
                INIT_LIST_HEAD(&ls->ls_dirtbl[i].list);
-               rwlock_init(&ls->ls_dirtbl[i].lock);
+               spin_lock_init(&ls->ls_dirtbl[i].lock);
        }
 
        INIT_LIST_HEAD(&ls->ls_waiters);
        mutex_init(&ls->ls_waiters_mutex);
+       INIT_LIST_HEAD(&ls->ls_orphans);
+       mutex_init(&ls->ls_orphans_mutex);
+       INIT_LIST_HEAD(&ls->ls_timeout);
+       mutex_init(&ls->ls_timeout_mutex);
 
        INIT_LIST_HEAD(&ls->ls_nodes);
        INIT_LIST_HEAD(&ls->ls_nodes_gone);
@@ -462,19 +517,24 @@ static int new_lockspace(char *name, int namelen, void **lockspace,
 
        init_waitqueue_head(&ls->ls_uevent_wait);
        ls->ls_uevent_result = 0;
+       init_completion(&ls->ls_members_done);
+       ls->ls_members_result = -1;
 
        ls->ls_recoverd_task = NULL;
        mutex_init(&ls->ls_recoverd_active);
        spin_lock_init(&ls->ls_recover_lock);
+       spin_lock_init(&ls->ls_rcom_spin);
+       get_random_bytes(&ls->ls_rcom_seq, sizeof(uint64_t));
        ls->ls_recover_status = 0;
        ls->ls_recover_seq = 0;
        ls->ls_recover_args = NULL;
        init_rwsem(&ls->ls_in_recovery);
+       init_rwsem(&ls->ls_recv_active);
        INIT_LIST_HEAD(&ls->ls_requestqueue);
        mutex_init(&ls->ls_requestqueue_mutex);
        mutex_init(&ls->ls_clear_proc_locks);
 
-       ls->ls_recover_buf = kmalloc(dlm_config.buffer_size, GFP_KERNEL);
+       ls->ls_recover_buf = kmalloc(dlm_config.ci_buffer_size, GFP_NOFS);
        if (!ls->ls_recover_buf)
                goto out_dirfree;
 
@@ -489,6 +549,7 @@ static int new_lockspace(char *name, int namelen, void **lockspace,
        down_write(&ls->ls_in_recovery);
 
        spin_lock(&lslist_lock);
+       ls->ls_create_count = 1;
        list_add(&ls->ls_list, &lslist);
        spin_unlock(&lslist_lock);
 
@@ -496,32 +557,47 @@ static int new_lockspace(char *name, int namelen, void **lockspace,
        error = dlm_recoverd_start(ls);
        if (error) {
                log_error(ls, "can't start dlm_recoverd %d", error);
-               goto out_rcomfree;
+               goto out_delist;
        }
 
-       dlm_create_debug_file(ls);
-
-       error = kobject_setup(ls);
+       ls->ls_kobj.kset = dlm_kset;
+       error = kobject_init_and_add(&ls->ls_kobj, &dlm_ktype, NULL,
+                                    "%s", ls->ls_name);
        if (error)
-               goto out_del;
+               goto out_stop;
+       kobject_uevent(&ls->ls_kobj, KOBJ_ADD);
 
-       error = kobject_register(&ls->ls_kobj);
-       if (error)
-               goto out_del;
+       /* let kobject handle freeing of ls if there's an error */
+       do_unreg = 1;
+
+       /* This uevent triggers dlm_controld in userspace to add us to the
+          group of nodes that are members of this lockspace (managed by the
+          cluster infrastructure.)  Once it's done that, it tells us who the
+          current lockspace members are (via configfs) and then tells the
+          lockspace to start running (via sysfs) in dlm_ls_start(). */
 
        error = do_uevent(ls, 1);
        if (error)
-               goto out_unreg;
+               goto out_stop;
+
+       wait_for_completion(&ls->ls_members_done);
+       error = ls->ls_members_result;
+       if (error)
+               goto out_members;
+
+       dlm_create_debug_file(ls);
 
+       log_debug(ls, "join complete");
        *lockspace = ls;
        return 0;
 
- out_unreg:
-       kobject_unregister(&ls->ls_kobj);
- out_del:
-       dlm_delete_debug_file(ls);
+ out_members:
+       do_uevent(ls, 0);
+       dlm_clear_members(ls);
+       kfree(ls->ls_node_array);
+ out_stop:
        dlm_recoverd_stop(ls);
- out_rcomfree:
+ out_delist:
        spin_lock(&lslist_lock);
        list_del(&ls->ls_list);
        spin_unlock(&lslist_lock);
@@ -533,13 +609,16 @@ static int new_lockspace(char *name, int namelen, void **lockspace,
  out_rsbfree:
        kfree(ls->ls_rsbtbl);
  out_lsfree:
-       kfree(ls);
+       if (do_unreg)
+               kobject_put(&ls->ls_kobj);
+       else
+               kfree(ls);
  out:
        module_put(THIS_MODULE);
        return error;
 }
 
-int dlm_new_lockspace(char *name, int namelen, void **lockspace,
+int dlm_new_lockspace(const char *name, int namelen, void **lockspace,
                      uint32_t flags, int lvblen)
 {
        int error = 0;
@@ -553,6 +632,10 @@ int dlm_new_lockspace(char *name, int namelen, void **lockspace,
        error = new_lockspace(name, namelen, lockspace, flags, lvblen);
        if (!error)
                ls_count++;
+       if (error > 0)
+               error = 0;
+       if (!ls_count)
+               threads_stop();
  out:
        mutex_unlock(&ls_lock);
        return error;
@@ -593,13 +676,34 @@ static int release_lockspace(struct dlm_ls *ls, int force)
        struct dlm_lkb *lkb;
        struct dlm_rsb *rsb;
        struct list_head *head;
-       int i;
-       int busy = lockspace_busy(ls);
+       int i, busy, rv;
+
+       busy = lockspace_busy(ls);
+
+       spin_lock(&lslist_lock);
+       if (ls->ls_create_count == 1) {
+               if (busy > force)
+                       rv = -EBUSY;
+               else {
+                       /* remove_lockspace takes ls off lslist */
+                       ls->ls_create_count = 0;
+                       rv = 0;
+               }
+       } else if (ls->ls_create_count > 1) {
+               rv = --ls->ls_create_count;
+       } else {
+               rv = -EINVAL;
+       }
+       spin_unlock(&lslist_lock);
 
-       if (busy > force)
-               return -EBUSY;
+       if (rv) {
+               log_debug(ls, "release_lockspace no remove %d", rv);
+               return rv;
+       }
+
+       dlm_device_deregister(ls);
 
-       if (force < 3)
+       if (force < 3 && dlm_user_daemon_available())
                do_uevent(ls, 0);
 
        dlm_recoverd_stop(ls);
@@ -634,9 +738,9 @@ static int release_lockspace(struct dlm_ls *ls, int force)
                        dlm_del_ast(lkb);
 
                        if (lkb->lkb_lvbptr && lkb->lkb_flags & DLM_IFL_MSTCPY)
-                               free_lvb(lkb->lkb_lvbptr);
+                               dlm_free_lvb(lkb->lkb_lvbptr);
 
-                       free_lkb(lkb);
+                       dlm_free_lkb(lkb);
                }
        }
        dlm_astd_resume();
@@ -654,7 +758,7 @@ static int release_lockspace(struct dlm_ls *ls, int force)
                                         res_hashchain);
 
                        list_del(&rsb->res_hashchain);
-                       free_rsb(rsb);
+                       dlm_free_rsb(rsb);
                }
 
                head = &ls->ls_rsbtbl[i].toss;
@@ -662,7 +766,7 @@ static int release_lockspace(struct dlm_ls *ls, int force)
                        rsb = list_entry(head->next, struct dlm_rsb,
                                         res_hashchain);
                        list_del(&rsb->res_hashchain);
-                       free_rsb(rsb);
+                       dlm_free_rsb(rsb);
                }
        }
 
@@ -672,19 +776,15 @@ static int release_lockspace(struct dlm_ls *ls, int force)
         * Free structures on any other lists
         */
 
+       dlm_purge_requestqueue(ls);
        kfree(ls->ls_recover_args);
        dlm_clear_free_entries(ls);
        dlm_clear_members(ls);
        dlm_clear_members_gone(ls);
        kfree(ls->ls_node_array);
-       kobject_unregister(&ls->ls_kobj);
-       kfree(ls);
-
-       mutex_lock(&ls_lock);
-       ls_count--;
-       if (!ls_count)
-               threads_stop();
-       mutex_unlock(&ls_lock);
+       log_debug(ls, "release_lockspace final free");
+       kobject_put(&ls->ls_kobj);
+       /* The ls structure will be freed when the kobject is done with */
 
        module_put(THIS_MODULE);
        return 0;
@@ -707,11 +807,38 @@ static int release_lockspace(struct dlm_ls *ls, int force)
 int dlm_release_lockspace(void *lockspace, int force)
 {
        struct dlm_ls *ls;
+       int error;
 
        ls = dlm_find_lockspace_local(lockspace);
        if (!ls)
                return -EINVAL;
        dlm_put_lockspace(ls);
-       return release_lockspace(ls, force);
+
+       mutex_lock(&ls_lock);
+       error = release_lockspace(ls, force);
+       if (!error)
+               ls_count--;
+       if (!ls_count)
+               threads_stop();
+       mutex_unlock(&ls_lock);
+
+       return error;
+}
+
+void dlm_stop_lockspaces(void)
+{
+       struct dlm_ls *ls;
+
+ restart:
+       spin_lock(&lslist_lock);
+       list_for_each_entry(ls, &lslist, ls_list) {
+               if (!test_bit(LSFL_RUNNING, &ls->ls_flags))
+                       continue;
+               spin_unlock(&lslist_lock);
+               log_error(ls, "no userland control daemon, stopping lockspace");
+               dlm_ls_stop(ls);
+               goto restart;
+       }
+       spin_unlock(&lslist_lock);
 }