lockspace.c 20.2 KB
Newer Older
1 2 3 4
/******************************************************************************
*******************************************************************************
**
**  Copyright (C) Sistina Software, Inc.  1997-2003  All rights reserved.
D
David Teigland 已提交
5
**  Copyright (C) 2004-2011 Red Hat, Inc.  All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
**
**  This copyrighted material is made available to anyone wishing to use,
**  modify, copy, or redistribute it subject to the terms and conditions
**  of the GNU General Public License v.2.
**
*******************************************************************************
******************************************************************************/

#include "dlm_internal.h"
#include "lockspace.h"
#include "member.h"
#include "recoverd.h"
#include "dir.h"
#include "lowcomms.h"
#include "config.h"
#include "memory.h"
#include "lock.h"
23
#include "recover.h"
24
#include "requestqueue.h"
25
#include "user.h"
26
#include "ast.h"
27 28

static int			ls_count;
29
static struct mutex		ls_lock;
30 31 32 33 34 35 36 37 38 39
static struct list_head		lslist;
static spinlock_t		lslist_lock;
static struct task_struct *	scand_task;


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);

40 41 42 43
	ls = dlm_find_lockspace_local(ls->ls_local_handle);
	if (!ls)
		return -EINVAL;

44 45 46 47 48 49 50 51 52 53
	switch (n) {
	case 0:
		dlm_ls_stop(ls);
		break;
	case 1:
		dlm_ls_start(ls);
		break;
	default:
		ret = -EINVAL;
	}
54
	dlm_put_lockspace(ls);
55 56 57 58 59 60 61 62 63 64 65 66 67
	return ret;
}

static ssize_t dlm_event_store(struct dlm_ls *ls, const char *buf, size_t len)
{
	ls->ls_uevent_result = simple_strtol(buf, NULL, 0);
	set_bit(LSFL_UEVENT_WAIT, &ls->ls_flags);
	wake_up(&ls->ls_uevent_wait);
	return len;
}

static ssize_t dlm_id_show(struct dlm_ls *ls, char *buf)
{
68
	return snprintf(buf, PAGE_SIZE, "%u\n", ls->ls_global_id);
69 70 71 72 73 74 75 76
}

static ssize_t dlm_id_store(struct dlm_ls *ls, const char *buf, size_t len)
{
	ls->ls_global_id = simple_strtoul(buf, NULL, 0);
	return len;
}

D
David Teigland 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89
static ssize_t dlm_nodir_show(struct dlm_ls *ls, char *buf)
{
	return snprintf(buf, PAGE_SIZE, "%u\n", dlm_no_directory(ls));
}

static ssize_t dlm_nodir_store(struct dlm_ls *ls, const char *buf, size_t len)
{
	int val = simple_strtoul(buf, NULL, 0);
	if (val == 1)
		set_bit(LSFL_NODIR, &ls->ls_flags);
	return len;
}

90 91 92
static ssize_t dlm_recover_status_show(struct dlm_ls *ls, char *buf)
{
	uint32_t status = dlm_recover_status(ls);
93
	return snprintf(buf, PAGE_SIZE, "%x\n", status);
94 95
}

96 97
static ssize_t dlm_recover_nodeid_show(struct dlm_ls *ls, char *buf)
{
98
	return snprintf(buf, PAGE_SIZE, "%d\n", ls->ls_recover_nodeid);
99 100
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
struct dlm_attr {
	struct attribute attr;
	ssize_t (*show)(struct dlm_ls *, char *);
	ssize_t (*store)(struct dlm_ls *, const char *, size_t);
};

static struct dlm_attr dlm_attr_control = {
	.attr  = {.name = "control", .mode = S_IWUSR},
	.store = dlm_control_store
};

static struct dlm_attr dlm_attr_event = {
	.attr  = {.name = "event_done", .mode = S_IWUSR},
	.store = dlm_event_store
};

static struct dlm_attr dlm_attr_id = {
	.attr  = {.name = "id", .mode = S_IRUGO | S_IWUSR},
	.show  = dlm_id_show,
	.store = dlm_id_store
};

D
David Teigland 已提交
123 124 125 126 127 128
static struct dlm_attr dlm_attr_nodir = {
	.attr  = {.name = "nodir", .mode = S_IRUGO | S_IWUSR},
	.show  = dlm_nodir_show,
	.store = dlm_nodir_store
};

129 130 131 132 133
static struct dlm_attr dlm_attr_recover_status = {
	.attr  = {.name = "recover_status", .mode = S_IRUGO},
	.show  = dlm_recover_status_show
};

134 135 136 137 138
static struct dlm_attr dlm_attr_recover_nodeid = {
	.attr  = {.name = "recover_nodeid", .mode = S_IRUGO},
	.show  = dlm_recover_nodeid_show
};

139 140 141 142
static struct attribute *dlm_attrs[] = {
	&dlm_attr_control.attr,
	&dlm_attr_event.attr,
	&dlm_attr_id.attr,
D
David Teigland 已提交
143
	&dlm_attr_nodir.attr,
144
	&dlm_attr_recover_status.attr,
145
	&dlm_attr_recover_nodeid.attr,
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
	NULL,
};

static ssize_t dlm_attr_show(struct kobject *kobj, struct attribute *attr,
			     char *buf)
{
	struct dlm_ls *ls  = container_of(kobj, struct dlm_ls, ls_kobj);
	struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
	return a->show ? a->show(ls, buf) : 0;
}

static ssize_t dlm_attr_store(struct kobject *kobj, struct attribute *attr,
			      const char *buf, size_t len)
{
	struct dlm_ls *ls  = container_of(kobj, struct dlm_ls, ls_kobj);
	struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
	return a->store ? a->store(ls, buf, len) : len;
}

P
Patrick Caulfield 已提交
165 166 167 168 169 170
static void lockspace_kobj_release(struct kobject *k)
{
	struct dlm_ls *ls  = container_of(k, struct dlm_ls, ls_kobj);
	kfree(ls);
}

171
static const struct sysfs_ops dlm_attr_ops = {
172 173 174 175 176 177 178
	.show  = dlm_attr_show,
	.store = dlm_attr_store,
};

static struct kobj_type dlm_ktype = {
	.default_attrs = dlm_attrs,
	.sysfs_ops     = &dlm_attr_ops,
P
Patrick Caulfield 已提交
179
	.release       = lockspace_kobj_release,
180 181
};

182
static struct kset *dlm_kset;
183 184 185 186 187 188 189 190 191 192

static int do_uevent(struct dlm_ls *ls, int in)
{
	int error;

	if (in)
		kobject_uevent(&ls->ls_kobj, KOBJ_ONLINE);
	else
		kobject_uevent(&ls->ls_kobj, KOBJ_OFFLINE);

193 194 195 196 197
	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 */

198 199
	error = wait_event_interruptible(ls->ls_uevent_wait,
			test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags));
200 201 202

	log_debug(ls, "group event done %d %d", error, ls->ls_uevent_result);

203 204 205 206 207
	if (error)
		goto out;

	error = ls->ls_uevent_result;
 out:
208 209 210
	if (error)
		log_error(ls, "group %s failed %d %d", in ? "join" : "leave",
			  error, ls->ls_uevent_result);
211 212 213
	return error;
}

214 215 216 217 218 219 220 221 222 223 224 225
static int dlm_uevent(struct kset *kset, struct kobject *kobj,
		      struct kobj_uevent_env *env)
{
	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,
};
226

227
int __init dlm_lockspace_init(void)
228 229
{
	ls_count = 0;
230
	mutex_init(&ls_lock);
231 232 233
	INIT_LIST_HEAD(&lslist);
	spin_lock_init(&lslist_lock);

234
	dlm_kset = kset_create_and_add("dlm", &dlm_uevent_ops, kernel_kobj);
235
	if (!dlm_kset) {
236
		printk(KERN_WARNING "%s: can not create kset\n", __func__);
237 238 239
		return -ENOMEM;
	}
	return 0;
240 241 242 243
}

void dlm_lockspace_exit(void)
{
244
	kset_unregister(dlm_kset);
245 246
}

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
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;
}

263 264 265 266 267
static int dlm_scand(void *data)
{
	struct dlm_ls *ls;

	while (!kthread_should_stop()) {
268 269
		ls = find_ls_to_scan();
		if (ls) {
270
			if (dlm_lock_recovery_try(ls)) {
271
				ls->ls_scan_time = jiffies;
272
				dlm_scan_rsbs(ls);
273
				dlm_scan_timeout(ls);
274
				dlm_scan_waiters(ls);
275
				dlm_unlock_recovery(ls);
276 277
			} else {
				ls->ls_scan_time += HZ;
278
			}
279
			continue;
280
		}
281
		schedule_timeout_interruptible(dlm_config.ci_scan_secs * HZ);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
	}
	return 0;
}

static int dlm_scand_start(void)
{
	struct task_struct *p;
	int error = 0;

	p = kthread_run(dlm_scand, NULL, "dlm_scand");
	if (IS_ERR(p))
		error = PTR_ERR(p);
	else
		scand_task = p;
	return error;
}

static void dlm_scand_stop(void)
{
	kthread_stop(scand_task);
}

struct dlm_ls *dlm_find_lockspace_global(uint32_t id)
{
	struct dlm_ls *ls;

	spin_lock(&lslist_lock);

	list_for_each_entry(ls, &lslist, ls_list) {
		if (ls->ls_global_id == id) {
			ls->ls_count++;
			goto out;
		}
	}
	ls = NULL;
 out:
	spin_unlock(&lslist_lock);
	return ls;
}

D
David Teigland 已提交
322
struct dlm_ls *dlm_find_lockspace_local(dlm_lockspace_t *lockspace)
323
{
D
David Teigland 已提交
324
	struct dlm_ls *ls;
325 326

	spin_lock(&lslist_lock);
D
David Teigland 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
	list_for_each_entry(ls, &lslist, ls_list) {
		if (ls->ls_local_handle == lockspace) {
			ls->ls_count++;
			goto out;
		}
	}
	ls = NULL;
 out:
	spin_unlock(&lslist_lock);
	return ls;
}

struct dlm_ls *dlm_find_lockspace_device(int minor)
{
	struct dlm_ls *ls;

	spin_lock(&lslist_lock);
	list_for_each_entry(ls, &lslist, ls_list) {
		if (ls->ls_device.minor == minor) {
			ls->ls_count++;
			goto out;
		}
	}
	ls = NULL;
 out:
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	spin_unlock(&lslist_lock);
	return ls;
}

void dlm_put_lockspace(struct dlm_ls *ls)
{
	spin_lock(&lslist_lock);
	ls->ls_count--;
	spin_unlock(&lslist_lock);
}

static void remove_lockspace(struct dlm_ls *ls)
{
	for (;;) {
		spin_lock(&lslist_lock);
		if (ls->ls_count == 0) {
368
			WARN_ON(ls->ls_create_count != 0);
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
			list_del(&ls->ls_list);
			spin_unlock(&lslist_lock);
			return;
		}
		spin_unlock(&lslist_lock);
		ssleep(1);
	}
}

static int threads_start(void)
{
	int error;

	error = dlm_scand_start();
	if (error) {
		log_print("cannot start dlm_scand thread %d", error);
385
		goto fail;
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
	}

	/* Thread for sending/receiving messages for all lockspace's */
	error = dlm_lowcomms_start();
	if (error) {
		log_print("cannot start dlm lowcomms %d", error);
		goto scand_fail;
	}

	return 0;

 scand_fail:
	dlm_scand_stop();
 fail:
	return error;
}

static void threads_stop(void)
{
	dlm_scand_stop();
	dlm_lowcomms_stop();
}

D
David Teigland 已提交
409 410 411 412
static int new_lockspace(const char *name, const char *cluster,
			 uint32_t flags, int lvblen,
			 const struct dlm_lockspace_ops *ops, void *ops_arg,
			 int *ops_result, dlm_lockspace_t **lockspace)
413 414
{
	struct dlm_ls *ls;
415
	int i, size, error;
416
	int do_unreg = 0;
D
David Teigland 已提交
417
	int namelen = strlen(name);
418 419 420 421 422 423 424 425 426 427

	if (namelen > DLM_LOCKSPACE_LEN)
		return -EINVAL;

	if (!lvblen || (lvblen % 8))
		return -EINVAL;

	if (!try_module_get(THIS_MODULE))
		return -EINVAL;

428
	if (!dlm_user_daemon_available()) {
D
David Teigland 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
		log_print("dlm user daemon not available");
		error = -EUNATCH;
		goto out;
	}

	if (ops && ops_result) {
	       	if (!dlm_config.ci_recover_callbacks)
			*ops_result = -EOPNOTSUPP;
		else
			*ops_result = 0;
	}

	if (dlm_config.ci_recover_callbacks && cluster &&
	    strncmp(cluster, dlm_config.ci_cluster_name, DLM_LOCKSPACE_LEN)) {
		log_print("dlm cluster name %s mismatch %s",
			  dlm_config.ci_cluster_name, cluster);
		error = -EBADR;
		goto out;
447 448
	}

449 450 451 452 453 454 455 456 457 458 459 460 461 462
	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++;
463 464
		*lockspace = ls;
		error = 1;
465
		break;
466
	}
467 468 469
	spin_unlock(&lslist_lock);

	if (error)
470
		goto out;
471 472

	error = -ENOMEM;
473

D
David Teigland 已提交
474
	ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_NOFS);
475 476 477 478 479 480 481
	if (!ls)
		goto out;
	memcpy(ls->ls_name, name, namelen);
	ls->ls_namelen = namelen;
	ls->ls_lvblen = lvblen;
	ls->ls_count = 0;
	ls->ls_flags = 0;
482
	ls->ls_scan_time = jiffies;
483

D
David Teigland 已提交
484 485 486 487 488
	if (ops && dlm_config.ci_recover_callbacks) {
		ls->ls_ops = ops;
		ls->ls_ops_arg = ops_arg;
	}

489 490 491
	if (flags & DLM_LSFL_TIMEWARN)
		set_bit(LSFL_TIMEWARN, &ls->ls_flags);

492
	/* ls_exflags are forced to match among nodes, and we don't
493 494 495
	   need to require all nodes to have some flags set */
	ls->ls_exflags = (flags & ~(DLM_LSFL_TIMEWARN | DLM_LSFL_FS |
				    DLM_LSFL_NEWEXCL));
496

497
	size = dlm_config.ci_rsbtbl_size;
498 499
	ls->ls_rsbtbl_size = size;

500
	ls->ls_rsbtbl = vmalloc(sizeof(struct dlm_rsbtable) * size);
501 502 503
	if (!ls->ls_rsbtbl)
		goto out_lsfree;
	for (i = 0; i < size; i++) {
B
Bob Peterson 已提交
504 505
		ls->ls_rsbtbl[i].keep.rb_node = NULL;
		ls->ls_rsbtbl[i].toss.rb_node = NULL;
506
		spin_lock_init(&ls->ls_rsbtbl[i].lock);
507 508
	}

509 510 511 512 513 514 515 516 517
	spin_lock_init(&ls->ls_remove_spin);

	for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++) {
		ls->ls_remove_names[i] = kzalloc(DLM_RESNAME_MAXLEN+1,
						 GFP_KERNEL);
		if (!ls->ls_remove_names[i])
			goto out_rsbtbl;
	}

D
David Teigland 已提交
518 519
	idr_init(&ls->ls_lkbidr);
	spin_lock_init(&ls->ls_lkbidr_spin);
520 521

	INIT_LIST_HEAD(&ls->ls_waiters);
522
	mutex_init(&ls->ls_waiters_mutex);
523 524
	INIT_LIST_HEAD(&ls->ls_orphans);
	mutex_init(&ls->ls_orphans_mutex);
525 526
	INIT_LIST_HEAD(&ls->ls_timeout);
	mutex_init(&ls->ls_timeout_mutex);
527

D
David Teigland 已提交
528 529 530
	INIT_LIST_HEAD(&ls->ls_new_rsb);
	spin_lock_init(&ls->ls_new_rsb_spin);

531 532 533 534 535 536 537 538 539 540
	INIT_LIST_HEAD(&ls->ls_nodes);
	INIT_LIST_HEAD(&ls->ls_nodes_gone);
	ls->ls_num_nodes = 0;
	ls->ls_low_nodeid = 0;
	ls->ls_total_weight = 0;
	ls->ls_node_array = NULL;

	memset(&ls->ls_stub_rsb, 0, sizeof(struct dlm_rsb));
	ls->ls_stub_rsb.res_ls = ls;

D
David Teigland 已提交
541 542
	ls->ls_debug_rsb_dentry = NULL;
	ls->ls_debug_waiters_dentry = NULL;
543 544 545

	init_waitqueue_head(&ls->ls_uevent_wait);
	ls->ls_uevent_result = 0;
546 547
	init_completion(&ls->ls_members_done);
	ls->ls_members_result = -1;
548

549 550 551
	mutex_init(&ls->ls_cb_mutex);
	INIT_LIST_HEAD(&ls->ls_cb_delay);

552
	ls->ls_recoverd_task = NULL;
553
	mutex_init(&ls->ls_recoverd_active);
554
	spin_lock_init(&ls->ls_recover_lock);
555 556
	spin_lock_init(&ls->ls_rcom_spin);
	get_random_bytes(&ls->ls_rcom_seq, sizeof(uint64_t));
557 558 559 560
	ls->ls_recover_status = 0;
	ls->ls_recover_seq = 0;
	ls->ls_recover_args = NULL;
	init_rwsem(&ls->ls_in_recovery);
561
	init_rwsem(&ls->ls_recv_active);
562
	INIT_LIST_HEAD(&ls->ls_requestqueue);
563
	mutex_init(&ls->ls_requestqueue_mutex);
D
David Teigland 已提交
564
	mutex_init(&ls->ls_clear_proc_locks);
565

D
David Teigland 已提交
566
	ls->ls_recover_buf = kmalloc(dlm_config.ci_buffer_size, GFP_NOFS);
567
	if (!ls->ls_recover_buf)
568
		goto out_lkbidr;
569

570 571 572 573 574
	ls->ls_slot = 0;
	ls->ls_num_slots = 0;
	ls->ls_slots_size = 0;
	ls->ls_slots = NULL;

575 576
	INIT_LIST_HEAD(&ls->ls_recover_list);
	spin_lock_init(&ls->ls_recover_list_lock);
577 578
	idr_init(&ls->ls_recover_idr);
	spin_lock_init(&ls->ls_recover_idr_lock);
579
	ls->ls_recover_list_count = 0;
D
David Teigland 已提交
580
	ls->ls_local_handle = ls;
581 582 583 584
	init_waitqueue_head(&ls->ls_wait_general);
	INIT_LIST_HEAD(&ls->ls_root_list);
	init_rwsem(&ls->ls_root_sem);

585
	spin_lock(&lslist_lock);
586
	ls->ls_create_count = 1;
587 588 589
	list_add(&ls->ls_list, &lslist);
	spin_unlock(&lslist_lock);

590 591 592 593 594 595 596 597
	if (flags & DLM_LSFL_FS) {
		error = dlm_callback_start(ls);
		if (error) {
			log_error(ls, "can't start dlm_callback %d", error);
			goto out_delist;
		}
	}

598 599 600 601 602 603 604 605 606
	init_waitqueue_head(&ls->ls_recover_lock_wait);

	/*
	 * Once started, dlm_recoverd first looks for ls in lslist, then
	 * initializes ls_in_recovery as locked in "down" mode.  We need
	 * to wait for the wakeup from dlm_recoverd because in_recovery
	 * has to start out in down mode.
	 */

607 608 609
	error = dlm_recoverd_start(ls);
	if (error) {
		log_error(ls, "can't start dlm_recoverd %d", error);
610
		goto out_callback;
611 612
	}

613 614 615
	wait_event(ls->ls_recover_lock_wait,
		   test_bit(LSFL_RECOVER_LOCK, &ls->ls_flags));

616 617 618
	ls->ls_kobj.kset = dlm_kset;
	error = kobject_init_and_add(&ls->ls_kobj, &dlm_ktype, NULL,
				     "%s", ls->ls_name);
619
	if (error)
620
		goto out_recoverd;
621
	kobject_uevent(&ls->ls_kobj, KOBJ_ADD);
622 623 624

	/* let kobject handle freeing of ls if there's an error */
	do_unreg = 1;
625

626 627 628 629 630 631
	/* 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(). */

632 633
	error = do_uevent(ls, 1);
	if (error)
634
		goto out_recoverd;
635

636 637 638 639 640
	wait_for_completion(&ls->ls_members_done);
	error = ls->ls_members_result;
	if (error)
		goto out_members;

641 642 643
	dlm_create_debug_file(ls);

	log_debug(ls, "join complete");
644 645 646
	*lockspace = ls;
	return 0;

647 648 649 650
 out_members:
	do_uevent(ls, 0);
	dlm_clear_members(ls);
	kfree(ls->ls_node_array);
651
 out_recoverd:
652
	dlm_recoverd_stop(ls);
653 654
 out_callback:
	dlm_callback_stop(ls);
655
 out_delist:
656 657 658
	spin_lock(&lslist_lock);
	list_del(&ls->ls_list);
	spin_unlock(&lslist_lock);
659
	idr_destroy(&ls->ls_recover_idr);
660
	kfree(ls->ls_recover_buf);
661
 out_lkbidr:
D
David Teigland 已提交
662
	idr_destroy(&ls->ls_lkbidr);
663 664 665 666 667
	for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++) {
		if (ls->ls_remove_names[i])
			kfree(ls->ls_remove_names[i]);
	}
 out_rsbtbl:
668
	vfree(ls->ls_rsbtbl);
669
 out_lsfree:
670
	if (do_unreg)
671
		kobject_put(&ls->ls_kobj);
672 673
	else
		kfree(ls);
674 675 676 677 678
 out:
	module_put(THIS_MODULE);
	return error;
}

D
David Teigland 已提交
679 680 681 682
int dlm_new_lockspace(const char *name, const char *cluster,
		      uint32_t flags, int lvblen,
		      const struct dlm_lockspace_ops *ops, void *ops_arg,
		      int *ops_result, dlm_lockspace_t **lockspace)
683 684 685
{
	int error = 0;

686
	mutex_lock(&ls_lock);
687 688 689 690 691
	if (!ls_count)
		error = threads_start();
	if (error)
		goto out;

D
David Teigland 已提交
692 693
	error = new_lockspace(name, cluster, flags, lvblen, ops, ops_arg,
			      ops_result, lockspace);
694 695
	if (!error)
		ls_count++;
696 697 698
	if (error > 0)
		error = 0;
	if (!ls_count)
699
		threads_stop();
700
 out:
701
	mutex_unlock(&ls_lock);
702 703 704
	return error;
}

D
David Teigland 已提交
705
static int lkb_idr_is_local(int id, void *p, void *data)
706
{
D
David Teigland 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
	struct dlm_lkb *lkb = p;

	if (!lkb->lkb_nodeid)
		return 1;
	return 0;
}

static int lkb_idr_is_any(int id, void *p, void *data)
{
	return 1;
}

static int lkb_idr_free(int id, void *p, void *data)
{
	struct dlm_lkb *lkb = p;

	if (lkb->lkb_lvbptr && lkb->lkb_flags & DLM_IFL_MSTCPY)
		dlm_free_lvb(lkb->lkb_lvbptr);

	dlm_free_lkb(lkb);
	return 0;
}

/* NOTE: We check the lkbidr here rather than the resource table.
   This is because there may be LKBs queued as ASTs that have been unlinked
   from their RSBs and are pending deletion once the AST has been delivered */

static int lockspace_busy(struct dlm_ls *ls, int force)
{
	int rv;

	spin_lock(&ls->ls_lkbidr_spin);
	if (force == 0) {
		rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_any, ls);
	} else if (force == 1) {
		rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_local, ls);
	} else {
		rv = 0;
745
	}
D
David Teigland 已提交
746 747
	spin_unlock(&ls->ls_lkbidr_spin);
	return rv;
748 749 750 751 752
}

static int release_lockspace(struct dlm_ls *ls, int force)
{
	struct dlm_rsb *rsb;
B
Bob Peterson 已提交
753
	struct rb_node *n;
754 755
	int i, busy, rv;

D
David Teigland 已提交
756
	busy = lockspace_busy(ls, force);
757 758 759

	spin_lock(&lslist_lock);
	if (ls->ls_create_count == 1) {
D
David Teigland 已提交
760
		if (busy) {
761
			rv = -EBUSY;
D
David Teigland 已提交
762
		} else {
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
			/* 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;
	}
778

779
	dlm_device_deregister(ls);
780

781
	if (force < 3 && dlm_user_daemon_available())
782 783 784 785
		do_uevent(ls, 0);

	dlm_recoverd_stop(ls);

786 787
	dlm_callback_stop(ls);

788 789 790 791 792 793 794
	remove_lockspace(ls);

	dlm_delete_debug_file(ls);

	kfree(ls->ls_recover_buf);

	/*
D
David Teigland 已提交
795
	 * Free all lkb's in idr
796 797
	 */

D
David Teigland 已提交
798 799
	idr_for_each(&ls->ls_lkbidr, lkb_idr_free, ls);
	idr_destroy(&ls->ls_lkbidr);
800 801 802 803 804 805

	/*
	 * Free all rsb's on rsbtbl[] lists
	 */

	for (i = 0; i < ls->ls_rsbtbl_size; i++) {
B
Bob Peterson 已提交
806 807 808
		while ((n = rb_first(&ls->ls_rsbtbl[i].keep))) {
			rsb = rb_entry(n, struct dlm_rsb, res_hashnode);
			rb_erase(n, &ls->ls_rsbtbl[i].keep);
809
			dlm_free_rsb(rsb);
810 811
		}

B
Bob Peterson 已提交
812 813 814
		while ((n = rb_first(&ls->ls_rsbtbl[i].toss))) {
			rsb = rb_entry(n, struct dlm_rsb, res_hashnode);
			rb_erase(n, &ls->ls_rsbtbl[i].toss);
815
			dlm_free_rsb(rsb);
816 817 818
		}
	}

819
	vfree(ls->ls_rsbtbl);
820

821 822 823
	for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++)
		kfree(ls->ls_remove_names[i]);

D
David Teigland 已提交
824 825 826 827 828 829 830
	while (!list_empty(&ls->ls_new_rsb)) {
		rsb = list_first_entry(&ls->ls_new_rsb, struct dlm_rsb,
				       res_hashchain);
		list_del(&rsb->res_hashchain);
		dlm_free_rsb(rsb);
	}

831 832 833 834
	/*
	 * Free structures on any other lists
	 */

835
	dlm_purge_requestqueue(ls);
836 837 838 839
	kfree(ls->ls_recover_args);
	dlm_clear_members(ls);
	dlm_clear_members_gone(ls);
	kfree(ls->ls_node_array);
840
	log_debug(ls, "release_lockspace final free");
841
	kobject_put(&ls->ls_kobj);
842
	/* The ls structure will be freed when the kobject is done with */
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864

	module_put(THIS_MODULE);
	return 0;
}

/*
 * Called when a system has released all its locks and is not going to use the
 * lockspace any longer.  We free everything we're managing for this lockspace.
 * Remaining nodes will go through the recovery process as if we'd died.  The
 * lockspace must continue to function as usual, participating in recoveries,
 * until this returns.
 *
 * Force has 4 possible values:
 * 0 - don't destroy locksapce if it has any LKBs
 * 1 - destroy lockspace if it has remote LKBs but not if it has local LKBs
 * 2 - destroy lockspace regardless of LKBs
 * 3 - destroy lockspace as part of a forced shutdown
 */

int dlm_release_lockspace(void *lockspace, int force)
{
	struct dlm_ls *ls;
865
	int error;
866 867 868 869 870

	ls = dlm_find_lockspace_local(lockspace);
	if (!ls)
		return -EINVAL;
	dlm_put_lockspace(ls);
871 872 873 874 875

	mutex_lock(&ls_lock);
	error = release_lockspace(ls, force);
	if (!error)
		ls_count--;
D
David Teigland 已提交
876
	if (!ls_count)
877 878 879 880
		threads_stop();
	mutex_unlock(&ls_lock);

	return error;
881 882
}

883 884 885
void dlm_stop_lockspaces(void)
{
	struct dlm_ls *ls;
886
	int count;
887 888

 restart:
889
	count = 0;
890 891
	spin_lock(&lslist_lock);
	list_for_each_entry(ls, &lslist, ls_list) {
892 893
		if (!test_bit(LSFL_RUNNING, &ls->ls_flags)) {
			count++;
894
			continue;
895
		}
896 897 898 899 900 901
		spin_unlock(&lslist_lock);
		log_error(ls, "no userland control daemon, stopping lockspace");
		dlm_ls_stop(ls);
		goto restart;
	}
	spin_unlock(&lslist_lock);
902 903 904

	if (count)
		log_print("dlm user daemon left %d lockspaces", count);
905 906
}