lockspace.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *******************************************************************************
  4. **
  5. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  6. ** Copyright (C) 2004-2011 Red Hat, Inc. All rights reserved.
  7. **
  8. **
  9. *******************************************************************************
  10. ******************************************************************************/
  11. #include <linux/module.h>
  12. #include "dlm_internal.h"
  13. #include "lockspace.h"
  14. #include "member.h"
  15. #include "recoverd.h"
  16. #include "dir.h"
  17. #include "lowcomms.h"
  18. #include "config.h"
  19. #include "memory.h"
  20. #include "lock.h"
  21. #include "recover.h"
  22. #include "requestqueue.h"
  23. #include "user.h"
  24. #include "ast.h"
  25. static int ls_count;
  26. static struct mutex ls_lock;
  27. static struct list_head lslist;
  28. static spinlock_t lslist_lock;
  29. static struct task_struct * scand_task;
  30. static ssize_t dlm_control_store(struct dlm_ls *ls, const char *buf, size_t len)
  31. {
  32. ssize_t ret = len;
  33. int n;
  34. int rc = kstrtoint(buf, 0, &n);
  35. if (rc)
  36. return rc;
  37. ls = dlm_find_lockspace_local(ls->ls_local_handle);
  38. if (!ls)
  39. return -EINVAL;
  40. switch (n) {
  41. case 0:
  42. dlm_ls_stop(ls);
  43. break;
  44. case 1:
  45. dlm_ls_start(ls);
  46. break;
  47. default:
  48. ret = -EINVAL;
  49. }
  50. dlm_put_lockspace(ls);
  51. return ret;
  52. }
  53. static ssize_t dlm_event_store(struct dlm_ls *ls, const char *buf, size_t len)
  54. {
  55. int rc = kstrtoint(buf, 0, &ls->ls_uevent_result);
  56. if (rc)
  57. return rc;
  58. set_bit(LSFL_UEVENT_WAIT, &ls->ls_flags);
  59. wake_up(&ls->ls_uevent_wait);
  60. return len;
  61. }
  62. static ssize_t dlm_id_show(struct dlm_ls *ls, char *buf)
  63. {
  64. return snprintf(buf, PAGE_SIZE, "%u\n", ls->ls_global_id);
  65. }
  66. static ssize_t dlm_id_store(struct dlm_ls *ls, const char *buf, size_t len)
  67. {
  68. int rc = kstrtouint(buf, 0, &ls->ls_global_id);
  69. if (rc)
  70. return rc;
  71. return len;
  72. }
  73. static ssize_t dlm_nodir_show(struct dlm_ls *ls, char *buf)
  74. {
  75. return snprintf(buf, PAGE_SIZE, "%u\n", dlm_no_directory(ls));
  76. }
  77. static ssize_t dlm_nodir_store(struct dlm_ls *ls, const char *buf, size_t len)
  78. {
  79. int val;
  80. int rc = kstrtoint(buf, 0, &val);
  81. if (rc)
  82. return rc;
  83. if (val == 1)
  84. set_bit(LSFL_NODIR, &ls->ls_flags);
  85. return len;
  86. }
  87. static ssize_t dlm_recover_status_show(struct dlm_ls *ls, char *buf)
  88. {
  89. uint32_t status = dlm_recover_status(ls);
  90. return snprintf(buf, PAGE_SIZE, "%x\n", status);
  91. }
  92. static ssize_t dlm_recover_nodeid_show(struct dlm_ls *ls, char *buf)
  93. {
  94. return snprintf(buf, PAGE_SIZE, "%d\n", ls->ls_recover_nodeid);
  95. }
  96. struct dlm_attr {
  97. struct attribute attr;
  98. ssize_t (*show)(struct dlm_ls *, char *);
  99. ssize_t (*store)(struct dlm_ls *, const char *, size_t);
  100. };
  101. static struct dlm_attr dlm_attr_control = {
  102. .attr = {.name = "control", .mode = S_IWUSR},
  103. .store = dlm_control_store
  104. };
  105. static struct dlm_attr dlm_attr_event = {
  106. .attr = {.name = "event_done", .mode = S_IWUSR},
  107. .store = dlm_event_store
  108. };
  109. static struct dlm_attr dlm_attr_id = {
  110. .attr = {.name = "id", .mode = S_IRUGO | S_IWUSR},
  111. .show = dlm_id_show,
  112. .store = dlm_id_store
  113. };
  114. static struct dlm_attr dlm_attr_nodir = {
  115. .attr = {.name = "nodir", .mode = S_IRUGO | S_IWUSR},
  116. .show = dlm_nodir_show,
  117. .store = dlm_nodir_store
  118. };
  119. static struct dlm_attr dlm_attr_recover_status = {
  120. .attr = {.name = "recover_status", .mode = S_IRUGO},
  121. .show = dlm_recover_status_show
  122. };
  123. static struct dlm_attr dlm_attr_recover_nodeid = {
  124. .attr = {.name = "recover_nodeid", .mode = S_IRUGO},
  125. .show = dlm_recover_nodeid_show
  126. };
  127. static struct attribute *dlm_attrs[] = {
  128. &dlm_attr_control.attr,
  129. &dlm_attr_event.attr,
  130. &dlm_attr_id.attr,
  131. &dlm_attr_nodir.attr,
  132. &dlm_attr_recover_status.attr,
  133. &dlm_attr_recover_nodeid.attr,
  134. NULL,
  135. };
  136. ATTRIBUTE_GROUPS(dlm);
  137. static ssize_t dlm_attr_show(struct kobject *kobj, struct attribute *attr,
  138. char *buf)
  139. {
  140. struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
  141. struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
  142. return a->show ? a->show(ls, buf) : 0;
  143. }
  144. static ssize_t dlm_attr_store(struct kobject *kobj, struct attribute *attr,
  145. const char *buf, size_t len)
  146. {
  147. struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
  148. struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
  149. return a->store ? a->store(ls, buf, len) : len;
  150. }
  151. static void lockspace_kobj_release(struct kobject *k)
  152. {
  153. struct dlm_ls *ls = container_of(k, struct dlm_ls, ls_kobj);
  154. kfree(ls);
  155. }
  156. static const struct sysfs_ops dlm_attr_ops = {
  157. .show = dlm_attr_show,
  158. .store = dlm_attr_store,
  159. };
  160. static struct kobj_type dlm_ktype = {
  161. .default_groups = dlm_groups,
  162. .sysfs_ops = &dlm_attr_ops,
  163. .release = lockspace_kobj_release,
  164. };
  165. static struct kset *dlm_kset;
  166. static int do_uevent(struct dlm_ls *ls, int in)
  167. {
  168. if (in)
  169. kobject_uevent(&ls->ls_kobj, KOBJ_ONLINE);
  170. else
  171. kobject_uevent(&ls->ls_kobj, KOBJ_OFFLINE);
  172. log_rinfo(ls, "%s the lockspace group...", in ? "joining" : "leaving");
  173. /* dlm_controld will see the uevent, do the necessary group management
  174. and then write to sysfs to wake us */
  175. wait_event(ls->ls_uevent_wait,
  176. test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags));
  177. log_rinfo(ls, "group event done %d", ls->ls_uevent_result);
  178. return ls->ls_uevent_result;
  179. }
  180. static int dlm_uevent(struct kset *kset, struct kobject *kobj,
  181. struct kobj_uevent_env *env)
  182. {
  183. struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
  184. add_uevent_var(env, "LOCKSPACE=%s", ls->ls_name);
  185. return 0;
  186. }
  187. static const struct kset_uevent_ops dlm_uevent_ops = {
  188. .uevent = dlm_uevent,
  189. };
  190. int __init dlm_lockspace_init(void)
  191. {
  192. ls_count = 0;
  193. mutex_init(&ls_lock);
  194. INIT_LIST_HEAD(&lslist);
  195. spin_lock_init(&lslist_lock);
  196. dlm_kset = kset_create_and_add("dlm", &dlm_uevent_ops, kernel_kobj);
  197. if (!dlm_kset) {
  198. printk(KERN_WARNING "%s: can not create kset\n", __func__);
  199. return -ENOMEM;
  200. }
  201. return 0;
  202. }
  203. void dlm_lockspace_exit(void)
  204. {
  205. kset_unregister(dlm_kset);
  206. }
  207. static struct dlm_ls *find_ls_to_scan(void)
  208. {
  209. struct dlm_ls *ls;
  210. spin_lock(&lslist_lock);
  211. list_for_each_entry(ls, &lslist, ls_list) {
  212. if (time_after_eq(jiffies, ls->ls_scan_time +
  213. dlm_config.ci_scan_secs * HZ)) {
  214. spin_unlock(&lslist_lock);
  215. return ls;
  216. }
  217. }
  218. spin_unlock(&lslist_lock);
  219. return NULL;
  220. }
  221. static int dlm_scand(void *data)
  222. {
  223. struct dlm_ls *ls;
  224. while (!kthread_should_stop()) {
  225. ls = find_ls_to_scan();
  226. if (ls) {
  227. if (dlm_lock_recovery_try(ls)) {
  228. ls->ls_scan_time = jiffies;
  229. dlm_scan_rsbs(ls);
  230. dlm_scan_timeout(ls);
  231. dlm_scan_waiters(ls);
  232. dlm_unlock_recovery(ls);
  233. } else {
  234. ls->ls_scan_time += HZ;
  235. }
  236. continue;
  237. }
  238. schedule_timeout_interruptible(dlm_config.ci_scan_secs * HZ);
  239. }
  240. return 0;
  241. }
  242. static int dlm_scand_start(void)
  243. {
  244. struct task_struct *p;
  245. int error = 0;
  246. p = kthread_run(dlm_scand, NULL, "dlm_scand");
  247. if (IS_ERR(p))
  248. error = PTR_ERR(p);
  249. else
  250. scand_task = p;
  251. return error;
  252. }
  253. static void dlm_scand_stop(void)
  254. {
  255. kthread_stop(scand_task);
  256. }
  257. struct dlm_ls *dlm_find_lockspace_global(uint32_t id)
  258. {
  259. struct dlm_ls *ls;
  260. spin_lock(&lslist_lock);
  261. list_for_each_entry(ls, &lslist, ls_list) {
  262. if (ls->ls_global_id == id) {
  263. ls->ls_count++;
  264. goto out;
  265. }
  266. }
  267. ls = NULL;
  268. out:
  269. spin_unlock(&lslist_lock);
  270. return ls;
  271. }
  272. struct dlm_ls *dlm_find_lockspace_local(dlm_lockspace_t *lockspace)
  273. {
  274. struct dlm_ls *ls;
  275. spin_lock(&lslist_lock);
  276. list_for_each_entry(ls, &lslist, ls_list) {
  277. if (ls->ls_local_handle == lockspace) {
  278. ls->ls_count++;
  279. goto out;
  280. }
  281. }
  282. ls = NULL;
  283. out:
  284. spin_unlock(&lslist_lock);
  285. return ls;
  286. }
  287. struct dlm_ls *dlm_find_lockspace_device(int minor)
  288. {
  289. struct dlm_ls *ls;
  290. spin_lock(&lslist_lock);
  291. list_for_each_entry(ls, &lslist, ls_list) {
  292. if (ls->ls_device.minor == minor) {
  293. ls->ls_count++;
  294. goto out;
  295. }
  296. }
  297. ls = NULL;
  298. out:
  299. spin_unlock(&lslist_lock);
  300. return ls;
  301. }
  302. void dlm_put_lockspace(struct dlm_ls *ls)
  303. {
  304. spin_lock(&lslist_lock);
  305. ls->ls_count--;
  306. spin_unlock(&lslist_lock);
  307. }
  308. static void remove_lockspace(struct dlm_ls *ls)
  309. {
  310. for (;;) {
  311. spin_lock(&lslist_lock);
  312. if (ls->ls_count == 0) {
  313. WARN_ON(ls->ls_create_count != 0);
  314. list_del(&ls->ls_list);
  315. spin_unlock(&lslist_lock);
  316. return;
  317. }
  318. spin_unlock(&lslist_lock);
  319. ssleep(1);
  320. }
  321. }
  322. static int threads_start(void)
  323. {
  324. int error;
  325. error = dlm_scand_start();
  326. if (error) {
  327. log_print("cannot start dlm_scand thread %d", error);
  328. goto fail;
  329. }
  330. /* Thread for sending/receiving messages for all lockspace's */
  331. error = dlm_lowcomms_start();
  332. if (error) {
  333. log_print("cannot start dlm lowcomms %d", error);
  334. goto scand_fail;
  335. }
  336. return 0;
  337. scand_fail:
  338. dlm_scand_stop();
  339. fail:
  340. return error;
  341. }
  342. static void threads_stop(void)
  343. {
  344. dlm_scand_stop();
  345. dlm_lowcomms_stop();
  346. }
  347. static int new_lockspace(const char *name, const char *cluster,
  348. uint32_t flags, int lvblen,
  349. const struct dlm_lockspace_ops *ops, void *ops_arg,
  350. int *ops_result, dlm_lockspace_t **lockspace)
  351. {
  352. struct dlm_ls *ls;
  353. int i, size, error;
  354. int do_unreg = 0;
  355. int namelen = strlen(name);
  356. if (namelen > DLM_LOCKSPACE_LEN || namelen == 0)
  357. return -EINVAL;
  358. if (!lvblen || (lvblen % 8))
  359. return -EINVAL;
  360. if (!try_module_get(THIS_MODULE))
  361. return -EINVAL;
  362. if (!dlm_user_daemon_available()) {
  363. log_print("dlm user daemon not available");
  364. error = -EUNATCH;
  365. goto out;
  366. }
  367. if (ops && ops_result) {
  368. if (!dlm_config.ci_recover_callbacks)
  369. *ops_result = -EOPNOTSUPP;
  370. else
  371. *ops_result = 0;
  372. }
  373. if (!cluster)
  374. log_print("dlm cluster name '%s' is being used without an application provided cluster name",
  375. dlm_config.ci_cluster_name);
  376. if (dlm_config.ci_recover_callbacks && cluster &&
  377. strncmp(cluster, dlm_config.ci_cluster_name, DLM_LOCKSPACE_LEN)) {
  378. log_print("dlm cluster name '%s' does not match "
  379. "the application cluster name '%s'",
  380. dlm_config.ci_cluster_name, cluster);
  381. error = -EBADR;
  382. goto out;
  383. }
  384. error = 0;
  385. spin_lock(&lslist_lock);
  386. list_for_each_entry(ls, &lslist, ls_list) {
  387. WARN_ON(ls->ls_create_count <= 0);
  388. if (ls->ls_namelen != namelen)
  389. continue;
  390. if (memcmp(ls->ls_name, name, namelen))
  391. continue;
  392. if (flags & DLM_LSFL_NEWEXCL) {
  393. error = -EEXIST;
  394. break;
  395. }
  396. ls->ls_create_count++;
  397. *lockspace = ls;
  398. error = 1;
  399. break;
  400. }
  401. spin_unlock(&lslist_lock);
  402. if (error)
  403. goto out;
  404. error = -ENOMEM;
  405. ls = kzalloc(sizeof(struct dlm_ls) + namelen, GFP_NOFS);
  406. if (!ls)
  407. goto out;
  408. memcpy(ls->ls_name, name, namelen);
  409. ls->ls_namelen = namelen;
  410. ls->ls_lvblen = lvblen;
  411. ls->ls_count = 0;
  412. ls->ls_flags = 0;
  413. ls->ls_scan_time = jiffies;
  414. if (ops && dlm_config.ci_recover_callbacks) {
  415. ls->ls_ops = ops;
  416. ls->ls_ops_arg = ops_arg;
  417. }
  418. if (flags & DLM_LSFL_TIMEWARN)
  419. set_bit(LSFL_TIMEWARN, &ls->ls_flags);
  420. /* ls_exflags are forced to match among nodes, and we don't
  421. need to require all nodes to have some flags set */
  422. ls->ls_exflags = (flags & ~(DLM_LSFL_TIMEWARN | DLM_LSFL_FS |
  423. DLM_LSFL_NEWEXCL));
  424. size = dlm_config.ci_rsbtbl_size;
  425. ls->ls_rsbtbl_size = size;
  426. ls->ls_rsbtbl = vmalloc(array_size(size, sizeof(struct dlm_rsbtable)));
  427. if (!ls->ls_rsbtbl)
  428. goto out_lsfree;
  429. for (i = 0; i < size; i++) {
  430. ls->ls_rsbtbl[i].keep.rb_node = NULL;
  431. ls->ls_rsbtbl[i].toss.rb_node = NULL;
  432. spin_lock_init(&ls->ls_rsbtbl[i].lock);
  433. }
  434. spin_lock_init(&ls->ls_remove_spin);
  435. for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++) {
  436. ls->ls_remove_names[i] = kzalloc(DLM_RESNAME_MAXLEN+1,
  437. GFP_KERNEL);
  438. if (!ls->ls_remove_names[i])
  439. goto out_rsbtbl;
  440. }
  441. idr_init(&ls->ls_lkbidr);
  442. spin_lock_init(&ls->ls_lkbidr_spin);
  443. INIT_LIST_HEAD(&ls->ls_waiters);
  444. mutex_init(&ls->ls_waiters_mutex);
  445. INIT_LIST_HEAD(&ls->ls_orphans);
  446. mutex_init(&ls->ls_orphans_mutex);
  447. INIT_LIST_HEAD(&ls->ls_timeout);
  448. mutex_init(&ls->ls_timeout_mutex);
  449. INIT_LIST_HEAD(&ls->ls_new_rsb);
  450. spin_lock_init(&ls->ls_new_rsb_spin);
  451. INIT_LIST_HEAD(&ls->ls_nodes);
  452. INIT_LIST_HEAD(&ls->ls_nodes_gone);
  453. ls->ls_num_nodes = 0;
  454. ls->ls_low_nodeid = 0;
  455. ls->ls_total_weight = 0;
  456. ls->ls_node_array = NULL;
  457. memset(&ls->ls_stub_rsb, 0, sizeof(struct dlm_rsb));
  458. ls->ls_stub_rsb.res_ls = ls;
  459. ls->ls_debug_rsb_dentry = NULL;
  460. ls->ls_debug_waiters_dentry = NULL;
  461. init_waitqueue_head(&ls->ls_uevent_wait);
  462. ls->ls_uevent_result = 0;
  463. init_completion(&ls->ls_members_done);
  464. ls->ls_members_result = -1;
  465. mutex_init(&ls->ls_cb_mutex);
  466. INIT_LIST_HEAD(&ls->ls_cb_delay);
  467. ls->ls_recoverd_task = NULL;
  468. mutex_init(&ls->ls_recoverd_active);
  469. spin_lock_init(&ls->ls_recover_lock);
  470. spin_lock_init(&ls->ls_rcom_spin);
  471. get_random_bytes(&ls->ls_rcom_seq, sizeof(uint64_t));
  472. ls->ls_recover_status = 0;
  473. ls->ls_recover_seq = 0;
  474. ls->ls_recover_args = NULL;
  475. init_rwsem(&ls->ls_in_recovery);
  476. init_rwsem(&ls->ls_recv_active);
  477. INIT_LIST_HEAD(&ls->ls_requestqueue);
  478. mutex_init(&ls->ls_requestqueue_mutex);
  479. mutex_init(&ls->ls_clear_proc_locks);
  480. ls->ls_recover_buf = kmalloc(dlm_config.ci_buffer_size, GFP_NOFS);
  481. if (!ls->ls_recover_buf)
  482. goto out_lkbidr;
  483. ls->ls_slot = 0;
  484. ls->ls_num_slots = 0;
  485. ls->ls_slots_size = 0;
  486. ls->ls_slots = NULL;
  487. INIT_LIST_HEAD(&ls->ls_recover_list);
  488. spin_lock_init(&ls->ls_recover_list_lock);
  489. idr_init(&ls->ls_recover_idr);
  490. spin_lock_init(&ls->ls_recover_idr_lock);
  491. ls->ls_recover_list_count = 0;
  492. ls->ls_local_handle = ls;
  493. init_waitqueue_head(&ls->ls_wait_general);
  494. INIT_LIST_HEAD(&ls->ls_root_list);
  495. init_rwsem(&ls->ls_root_sem);
  496. spin_lock(&lslist_lock);
  497. ls->ls_create_count = 1;
  498. list_add(&ls->ls_list, &lslist);
  499. spin_unlock(&lslist_lock);
  500. if (flags & DLM_LSFL_FS) {
  501. error = dlm_callback_start(ls);
  502. if (error) {
  503. log_error(ls, "can't start dlm_callback %d", error);
  504. goto out_delist;
  505. }
  506. }
  507. init_waitqueue_head(&ls->ls_recover_lock_wait);
  508. /*
  509. * Once started, dlm_recoverd first looks for ls in lslist, then
  510. * initializes ls_in_recovery as locked in "down" mode. We need
  511. * to wait for the wakeup from dlm_recoverd because in_recovery
  512. * has to start out in down mode.
  513. */
  514. error = dlm_recoverd_start(ls);
  515. if (error) {
  516. log_error(ls, "can't start dlm_recoverd %d", error);
  517. goto out_callback;
  518. }
  519. wait_event(ls->ls_recover_lock_wait,
  520. test_bit(LSFL_RECOVER_LOCK, &ls->ls_flags));
  521. /* let kobject handle freeing of ls if there's an error */
  522. do_unreg = 1;
  523. ls->ls_kobj.kset = dlm_kset;
  524. error = kobject_init_and_add(&ls->ls_kobj, &dlm_ktype, NULL,
  525. "%s", ls->ls_name);
  526. if (error)
  527. goto out_recoverd;
  528. kobject_uevent(&ls->ls_kobj, KOBJ_ADD);
  529. /* This uevent triggers dlm_controld in userspace to add us to the
  530. group of nodes that are members of this lockspace (managed by the
  531. cluster infrastructure.) Once it's done that, it tells us who the
  532. current lockspace members are (via configfs) and then tells the
  533. lockspace to start running (via sysfs) in dlm_ls_start(). */
  534. error = do_uevent(ls, 1);
  535. if (error)
  536. goto out_recoverd;
  537. wait_for_completion(&ls->ls_members_done);
  538. error = ls->ls_members_result;
  539. if (error)
  540. goto out_members;
  541. dlm_create_debug_file(ls);
  542. log_rinfo(ls, "join complete");
  543. *lockspace = ls;
  544. return 0;
  545. out_members:
  546. do_uevent(ls, 0);
  547. dlm_clear_members(ls);
  548. kfree(ls->ls_node_array);
  549. out_recoverd:
  550. dlm_recoverd_stop(ls);
  551. out_callback:
  552. dlm_callback_stop(ls);
  553. out_delist:
  554. spin_lock(&lslist_lock);
  555. list_del(&ls->ls_list);
  556. spin_unlock(&lslist_lock);
  557. idr_destroy(&ls->ls_recover_idr);
  558. kfree(ls->ls_recover_buf);
  559. out_lkbidr:
  560. idr_destroy(&ls->ls_lkbidr);
  561. out_rsbtbl:
  562. for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++)
  563. kfree(ls->ls_remove_names[i]);
  564. vfree(ls->ls_rsbtbl);
  565. out_lsfree:
  566. if (do_unreg)
  567. kobject_put(&ls->ls_kobj);
  568. else
  569. kfree(ls);
  570. out:
  571. module_put(THIS_MODULE);
  572. return error;
  573. }
  574. int dlm_new_lockspace(const char *name, const char *cluster,
  575. uint32_t flags, int lvblen,
  576. const struct dlm_lockspace_ops *ops, void *ops_arg,
  577. int *ops_result, dlm_lockspace_t **lockspace)
  578. {
  579. int error = 0;
  580. mutex_lock(&ls_lock);
  581. if (!ls_count)
  582. error = threads_start();
  583. if (error)
  584. goto out;
  585. error = new_lockspace(name, cluster, flags, lvblen, ops, ops_arg,
  586. ops_result, lockspace);
  587. if (!error)
  588. ls_count++;
  589. if (error > 0)
  590. error = 0;
  591. if (!ls_count)
  592. threads_stop();
  593. out:
  594. mutex_unlock(&ls_lock);
  595. return error;
  596. }
  597. static int lkb_idr_is_local(int id, void *p, void *data)
  598. {
  599. struct dlm_lkb *lkb = p;
  600. return lkb->lkb_nodeid == 0 && lkb->lkb_grmode != DLM_LOCK_IV;
  601. }
  602. static int lkb_idr_is_any(int id, void *p, void *data)
  603. {
  604. return 1;
  605. }
  606. static int lkb_idr_free(int id, void *p, void *data)
  607. {
  608. struct dlm_lkb *lkb = p;
  609. if (lkb->lkb_lvbptr && lkb->lkb_flags & DLM_IFL_MSTCPY)
  610. dlm_free_lvb(lkb->lkb_lvbptr);
  611. dlm_free_lkb(lkb);
  612. return 0;
  613. }
  614. /* NOTE: We check the lkbidr here rather than the resource table.
  615. This is because there may be LKBs queued as ASTs that have been unlinked
  616. from their RSBs and are pending deletion once the AST has been delivered */
  617. static int lockspace_busy(struct dlm_ls *ls, int force)
  618. {
  619. int rv;
  620. spin_lock(&ls->ls_lkbidr_spin);
  621. if (force == 0) {
  622. rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_any, ls);
  623. } else if (force == 1) {
  624. rv = idr_for_each(&ls->ls_lkbidr, lkb_idr_is_local, ls);
  625. } else {
  626. rv = 0;
  627. }
  628. spin_unlock(&ls->ls_lkbidr_spin);
  629. return rv;
  630. }
  631. static int release_lockspace(struct dlm_ls *ls, int force)
  632. {
  633. struct dlm_rsb *rsb;
  634. struct rb_node *n;
  635. int i, busy, rv;
  636. busy = lockspace_busy(ls, force);
  637. spin_lock(&lslist_lock);
  638. if (ls->ls_create_count == 1) {
  639. if (busy) {
  640. rv = -EBUSY;
  641. } else {
  642. /* remove_lockspace takes ls off lslist */
  643. ls->ls_create_count = 0;
  644. rv = 0;
  645. }
  646. } else if (ls->ls_create_count > 1) {
  647. rv = --ls->ls_create_count;
  648. } else {
  649. rv = -EINVAL;
  650. }
  651. spin_unlock(&lslist_lock);
  652. if (rv) {
  653. log_debug(ls, "release_lockspace no remove %d", rv);
  654. return rv;
  655. }
  656. dlm_device_deregister(ls);
  657. if (force < 3 && dlm_user_daemon_available())
  658. do_uevent(ls, 0);
  659. dlm_recoverd_stop(ls);
  660. dlm_callback_stop(ls);
  661. remove_lockspace(ls);
  662. dlm_delete_debug_file(ls);
  663. idr_destroy(&ls->ls_recover_idr);
  664. kfree(ls->ls_recover_buf);
  665. /*
  666. * Free all lkb's in idr
  667. */
  668. idr_for_each(&ls->ls_lkbidr, lkb_idr_free, ls);
  669. idr_destroy(&ls->ls_lkbidr);
  670. /*
  671. * Free all rsb's on rsbtbl[] lists
  672. */
  673. for (i = 0; i < ls->ls_rsbtbl_size; i++) {
  674. while ((n = rb_first(&ls->ls_rsbtbl[i].keep))) {
  675. rsb = rb_entry(n, struct dlm_rsb, res_hashnode);
  676. rb_erase(n, &ls->ls_rsbtbl[i].keep);
  677. dlm_free_rsb(rsb);
  678. }
  679. while ((n = rb_first(&ls->ls_rsbtbl[i].toss))) {
  680. rsb = rb_entry(n, struct dlm_rsb, res_hashnode);
  681. rb_erase(n, &ls->ls_rsbtbl[i].toss);
  682. dlm_free_rsb(rsb);
  683. }
  684. }
  685. vfree(ls->ls_rsbtbl);
  686. for (i = 0; i < DLM_REMOVE_NAMES_MAX; i++)
  687. kfree(ls->ls_remove_names[i]);
  688. while (!list_empty(&ls->ls_new_rsb)) {
  689. rsb = list_first_entry(&ls->ls_new_rsb, struct dlm_rsb,
  690. res_hashchain);
  691. list_del(&rsb->res_hashchain);
  692. dlm_free_rsb(rsb);
  693. }
  694. /*
  695. * Free structures on any other lists
  696. */
  697. dlm_purge_requestqueue(ls);
  698. kfree(ls->ls_recover_args);
  699. dlm_clear_members(ls);
  700. dlm_clear_members_gone(ls);
  701. kfree(ls->ls_node_array);
  702. log_rinfo(ls, "release_lockspace final free");
  703. kobject_put(&ls->ls_kobj);
  704. /* The ls structure will be freed when the kobject is done with */
  705. module_put(THIS_MODULE);
  706. return 0;
  707. }
  708. /*
  709. * Called when a system has released all its locks and is not going to use the
  710. * lockspace any longer. We free everything we're managing for this lockspace.
  711. * Remaining nodes will go through the recovery process as if we'd died. The
  712. * lockspace must continue to function as usual, participating in recoveries,
  713. * until this returns.
  714. *
  715. * Force has 4 possible values:
  716. * 0 - don't destroy locksapce if it has any LKBs
  717. * 1 - destroy lockspace if it has remote LKBs but not if it has local LKBs
  718. * 2 - destroy lockspace regardless of LKBs
  719. * 3 - destroy lockspace as part of a forced shutdown
  720. */
  721. int dlm_release_lockspace(void *lockspace, int force)
  722. {
  723. struct dlm_ls *ls;
  724. int error;
  725. ls = dlm_find_lockspace_local(lockspace);
  726. if (!ls)
  727. return -EINVAL;
  728. dlm_put_lockspace(ls);
  729. mutex_lock(&ls_lock);
  730. error = release_lockspace(ls, force);
  731. if (!error)
  732. ls_count--;
  733. if (!ls_count)
  734. threads_stop();
  735. mutex_unlock(&ls_lock);
  736. return error;
  737. }
  738. void dlm_stop_lockspaces(void)
  739. {
  740. struct dlm_ls *ls;
  741. int count;
  742. restart:
  743. count = 0;
  744. spin_lock(&lslist_lock);
  745. list_for_each_entry(ls, &lslist, ls_list) {
  746. if (!test_bit(LSFL_RUNNING, &ls->ls_flags)) {
  747. count++;
  748. continue;
  749. }
  750. spin_unlock(&lslist_lock);
  751. log_error(ls, "no userland control daemon, stopping lockspace");
  752. dlm_ls_stop(ls);
  753. goto restart;
  754. }
  755. spin_unlock(&lslist_lock);
  756. if (count)
  757. log_print("dlm user daemon left %d lockspaces", count);
  758. }