*******************************************************************************
 **
 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
-**  Copyright (C) 2004-2007 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
        uint32_t                ls_global_id;   /* global unique lockspace ID */
        uint32_t                ls_exflags;
        int                     ls_lvblen;
-       int                     ls_count;       /* reference count */
+       int                     ls_count;       /* refcount of processes in
+                                                  the dlm using this ls */
+       int                     ls_create_count; /* create/release refcount */
        unsigned long           ls_flags;       /* LSFL_ */
        struct kobject          ls_kobj;
 
 
 *******************************************************************************
 **
 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
-**  Copyright (C) 2004-2007 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 "lock.h"
 #include "recover.h"
 #include "requestqueue.h"
+#include "user.h"
 
 static int                     ls_count;
 static struct mutex            ls_lock;
        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;
        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;
                         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)
        if (!try_module_get(THIS_MODULE))
                return -EINVAL;
 
-       ls = dlm_find_lockspace_name(name, namelen);
-       if (ls) {
-               *lockspace = ls;
+       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++;
                module_put(THIS_MODULE);
-               return -EEXIST;
+               error = 1; /* not an error, return 0 */
+               break;
        }
+       spin_unlock(&lslist_lock);
+
+       if (error < 0)
+               goto out;
+       if (error)
+               goto ret_zero;
+
+       error = -ENOMEM;
 
        ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_KERNEL);
        if (!ls)
                ls->ls_allocation = GFP_KERNEL;
 
        /* ls_exflags are forced to match among nodes, and we don't
-          need to require all nodes to have TIMEWARN or FS set */
-       ls->ls_exflags = (flags & ~(DLM_LSFL_TIMEWARN | DLM_LSFL_FS));
+          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;
        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);
 
        dlm_create_debug_file(ls);
 
        log_debug(ls, "join complete");
-
+ ret_zero:
        *lockspace = ls;
        return 0;
 
        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 (rv) {
+               log_debug(ls, "release_lockspace no remove %d", rv);
+               return rv;
+       }
 
-       if (busy > force)
-               return -EBUSY;
+       dlm_device_deregister(ls);
 
        if (force < 3)
                do_uevent(ls, 0);
        dlm_clear_members(ls);
        dlm_clear_members_gone(ls);
        kfree(ls->ls_node_array);
+       log_debug(ls, "release_lockspace final free");
        kobject_put(&ls->ls_kobj);
        /* The ls structure will be freed when the kobject is done with */
 
-       mutex_lock(&ls_lock);
-       ls_count--;
-       if (!ls_count)
-               threads_stop();
-       mutex_unlock(&ls_lock);
-
        module_put(THIS_MODULE);
        return 0;
 }
 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--;
+       else if (!ls_count)
+               threads_stop();
+       mutex_unlock(&ls_lock);
+
+       return error;
 }
 
 
 /*
- * Copyright (C) 2006-2007 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2006-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
        return error;
 }
 
-static int create_misc_device(struct dlm_ls *ls, char *name)
+static int dlm_device_register(struct dlm_ls *ls, char *name)
 {
        int error, len;
 
+       /* The device is already registered.  This happens when the
+          lockspace is created multiple times from userspace. */
+       if (ls->ls_device.name)
+               return 0;
+
        error = -ENOMEM;
        len = strlen(name) + strlen(name_prefix) + 2;
        ls->ls_device.name = kzalloc(len, GFP_KERNEL);
        return error;
 }
 
+int dlm_device_deregister(struct dlm_ls *ls)
+{
+       int error;
+
+       /* The device is not registered.  This happens when the lockspace
+          was never used from userspace, or when device_create_lockspace()
+          calls dlm_release_lockspace() after the register fails. */
+       if (!ls->ls_device.name)
+               return 0;
+
+       error = misc_deregister(&ls->ls_device);
+       if (!error)
+               kfree(ls->ls_device.name);
+       return error;
+}
+
 static int device_user_purge(struct dlm_user_proc *proc,
                             struct dlm_purge_params *params)
 {
        if (!ls)
                return -ENOENT;
 
-       error = create_misc_device(ls, params->name);
+       error = dlm_device_register(ls, params->name);
        dlm_put_lockspace(ls);
 
        if (error)
        if (!ls)
                return -ENOENT;
 
-       /* Deregister the misc device first, so we don't have
-        * a device that's not attached to a lockspace. If
-        * dlm_release_lockspace fails then we can recreate it
-        */
-       error = misc_deregister(&ls->ls_device);
-       if (error) {
-               dlm_put_lockspace(ls);
-               goto out;
-       }
-       kfree(ls->ls_device.name);
-
        if (params->flags & DLM_USER_LSFLG_FORCEFREE)
                force = 2;
 
        lockspace = ls->ls_local_handle;
+       dlm_put_lockspace(ls);
 
-       /* dlm_release_lockspace waits for references to go to zero,
-          so all processes will need to close their device for the ls
-          before the release will procede */
+       /* The final dlm_release_lockspace waits for references to go to
+          zero, so all processes will need to close their device for the
+          ls before the release will proceed.  release also calls the
+          device_deregister above.  Converting a positive return value
+          from release to zero means that userspace won't know when its
+          release was the final one, but it shouldn't need to know. */
 
-       dlm_put_lockspace(ls);
        error = dlm_release_lockspace(lockspace, force);
-       if (error)
-               create_misc_device(ls, ls->ls_name);
- out:
+       if (error > 0)
+               error = 0;
        return error;
 }
 
 
 /*
- * Copyright (C) 2006 Red Hat, Inc.  All rights reserved.
+ * Copyright (C) 2006-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
 void dlm_user_add_ast(struct dlm_lkb *lkb, int type);
 int dlm_user_init(void);
 void dlm_user_exit(void);
+int dlm_device_deregister(struct dlm_ls *ls);
 
 #endif
 
 
        error = dlm_new_lockspace(ls->fsname, strlen(ls->fsname),
                                  &ls->dlm_lockspace,
-                                 DLM_LSFL_FS | (nodir ? DLM_LSFL_NODIR : 0),
+                                 DLM_LSFL_FS | DLM_LSFL_NEWEXCL |
+                                 (nodir ? DLM_LSFL_NODIR : 0),
                                  GDLM_LVB_SIZE);
        if (error) {
                log_error("dlm_new_lockspace error %d", error);
 
 *******************************************************************************
 **
 **  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
-**  Copyright (C) 2004-2007 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
        char *   sb_lvbptr;
 };
 
+/* dlm_new_lockspace() flags */
+
 #define DLM_LSFL_NODIR         0x00000001
 #define DLM_LSFL_TIMEWARN      0x00000002
 #define DLM_LSFL_FS            0x00000004
+#define DLM_LSFL_NEWEXCL       0x00000008
 
 #ifdef __KERNEL__
 
 
 /* Version of the device interface */
 #define DLM_DEVICE_VERSION_MAJOR 6
 #define DLM_DEVICE_VERSION_MINOR 0
-#define DLM_DEVICE_VERSION_PATCH 0
+#define DLM_DEVICE_VERSION_PATCH 1
 
 /* struct passed to the lock write */
 struct dlm_lock_params {