lockspace.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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 "midcomms.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 ssize_t dlm_control_store(struct dlm_ls *ls, const char *buf, size_t len)
  30. {
  31. ssize_t ret = len;
  32. int n;
  33. int rc = kstrtoint(buf, 0, &n);
  34. if (rc)
  35. return rc;
  36. ls = dlm_find_lockspace_local(ls);
  37. if (!ls)
  38. return -EINVAL;
  39. switch (n) {
  40. case 0:
  41. dlm_ls_stop(ls);
  42. break;
  43. case 1:
  44. dlm_ls_start(ls);
  45. break;
  46. default:
  47. ret = -EINVAL;
  48. }
  49. dlm_put_lockspace(ls);
  50. return ret;
  51. }
  52. static ssize_t dlm_event_store(struct dlm_ls *ls, const char *buf, size_t len)
  53. {
  54. int rc = kstrtoint(buf, 0, &ls->ls_uevent_result);
  55. if (rc)
  56. return rc;
  57. set_bit(LSFL_UEVENT_WAIT, &ls->ls_flags);
  58. wake_up(&ls->ls_uevent_wait);
  59. return len;
  60. }
  61. static ssize_t dlm_id_show(struct dlm_ls *ls, char *buf)
  62. {
  63. return snprintf(buf, PAGE_SIZE, "%u\n", ls->ls_global_id);
  64. }
  65. static ssize_t dlm_id_store(struct dlm_ls *ls, const char *buf, size_t len)
  66. {
  67. int rc = kstrtouint(buf, 0, &ls->ls_global_id);
  68. if (rc)
  69. return rc;
  70. return len;
  71. }
  72. static ssize_t dlm_nodir_show(struct dlm_ls *ls, char *buf)
  73. {
  74. return snprintf(buf, PAGE_SIZE, "%u\n", dlm_no_directory(ls));
  75. }
  76. static ssize_t dlm_nodir_store(struct dlm_ls *ls, const char *buf, size_t len)
  77. {
  78. int val;
  79. int rc = kstrtoint(buf, 0, &val);
  80. if (rc)
  81. return rc;
  82. if (val == 1)
  83. set_bit(LSFL_NODIR, &ls->ls_flags);
  84. return len;
  85. }
  86. static ssize_t dlm_recover_status_show(struct dlm_ls *ls, char *buf)
  87. {
  88. uint32_t status = dlm_recover_status(ls);
  89. return snprintf(buf, PAGE_SIZE, "%x\n", status);
  90. }
  91. static ssize_t dlm_recover_nodeid_show(struct dlm_ls *ls, char *buf)
  92. {
  93. return snprintf(buf, PAGE_SIZE, "%d\n", ls->ls_recover_nodeid);
  94. }
  95. struct dlm_attr {
  96. struct attribute attr;
  97. ssize_t (*show)(struct dlm_ls *, char *);
  98. ssize_t (*store)(struct dlm_ls *, const char *, size_t);
  99. };
  100. static struct dlm_attr dlm_attr_control = {
  101. .attr = {.name = "control", .mode = S_IWUSR},
  102. .store = dlm_control_store
  103. };
  104. static struct dlm_attr dlm_attr_event = {
  105. .attr = {.name = "event_done", .mode = S_IWUSR},
  106. .store = dlm_event_store
  107. };
  108. static struct dlm_attr dlm_attr_id = {
  109. .attr = {.name = "id", .mode = S_IRUGO | S_IWUSR},
  110. .show = dlm_id_show,
  111. .store = dlm_id_store
  112. };
  113. static struct dlm_attr dlm_attr_nodir = {
  114. .attr = {.name = "nodir", .mode = S_IRUGO | S_IWUSR},
  115. .show = dlm_nodir_show,
  116. .store = dlm_nodir_store
  117. };
  118. static struct dlm_attr dlm_attr_recover_status = {
  119. .attr = {.name = "recover_status", .mode = S_IRUGO},
  120. .show = dlm_recover_status_show
  121. };
  122. static struct dlm_attr dlm_attr_recover_nodeid = {
  123. .attr = {.name = "recover_nodeid", .mode = S_IRUGO},
  124. .show = dlm_recover_nodeid_show
  125. };
  126. static struct attribute *dlm_attrs[] = {
  127. &dlm_attr_control.attr,
  128. &dlm_attr_event.attr,
  129. &dlm_attr_id.attr,
  130. &dlm_attr_nodir.attr,
  131. &dlm_attr_recover_status.attr,
  132. &dlm_attr_recover_nodeid.attr,
  133. NULL,
  134. };
  135. ATTRIBUTE_GROUPS(dlm);
  136. static ssize_t dlm_attr_show(struct kobject *kobj, struct attribute *attr,
  137. char *buf)
  138. {
  139. struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
  140. struct dlm_attr *a = container_of(attr, struct dlm_attr, attr);
  141. return a->show ? a->show(ls, buf) : 0;
  142. }
  143. static ssize_t dlm_attr_store(struct kobject *kobj, struct attribute *attr,
  144. const char *buf, size_t len)
  145. {
  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;
  149. }
  150. static const struct sysfs_ops dlm_attr_ops = {
  151. .show = dlm_attr_show,
  152. .store = dlm_attr_store,
  153. };
  154. static struct kobj_type dlm_ktype = {
  155. .default_groups = dlm_groups,
  156. .sysfs_ops = &dlm_attr_ops,
  157. };
  158. static struct kset *dlm_kset;
  159. static int do_uevent(struct dlm_ls *ls, int in, unsigned int release_recover)
  160. {
  161. char message[512] = {};
  162. char *envp[] = { message, NULL };
  163. if (in) {
  164. kobject_uevent(&ls->ls_kobj, KOBJ_ONLINE);
  165. } else {
  166. snprintf(message, 511, "RELEASE_RECOVER=%u", release_recover);
  167. kobject_uevent_env(&ls->ls_kobj, KOBJ_OFFLINE, envp);
  168. }
  169. log_rinfo(ls, "%s the lockspace group...", in ? "joining" : "leaving");
  170. /* dlm_controld will see the uevent, do the necessary group management
  171. and then write to sysfs to wake us */
  172. wait_event(ls->ls_uevent_wait,
  173. test_and_clear_bit(LSFL_UEVENT_WAIT, &ls->ls_flags));
  174. log_rinfo(ls, "group event done %d", ls->ls_uevent_result);
  175. return ls->ls_uevent_result;
  176. }
  177. static int dlm_uevent(const struct kobject *kobj, struct kobj_uevent_env *env)
  178. {
  179. const struct dlm_ls *ls = container_of(kobj, struct dlm_ls, ls_kobj);
  180. add_uevent_var(env, "LOCKSPACE=%s", ls->ls_name);
  181. return 0;
  182. }
  183. static const struct kset_uevent_ops dlm_uevent_ops = {
  184. .uevent = dlm_uevent,
  185. };
  186. int __init dlm_lockspace_init(void)
  187. {
  188. ls_count = 0;
  189. mutex_init(&ls_lock);
  190. INIT_LIST_HEAD(&lslist);
  191. spin_lock_init(&lslist_lock);
  192. dlm_kset = kset_create_and_add("dlm", &dlm_uevent_ops, kernel_kobj);
  193. if (!dlm_kset) {
  194. printk(KERN_WARNING "%s: can not create kset\n", __func__);
  195. return -ENOMEM;
  196. }
  197. return 0;
  198. }
  199. void dlm_lockspace_exit(void)
  200. {
  201. kset_unregister(dlm_kset);
  202. }
  203. struct dlm_ls *dlm_find_lockspace_global(uint32_t id)
  204. {
  205. struct dlm_ls *ls;
  206. spin_lock_bh(&lslist_lock);
  207. list_for_each_entry(ls, &lslist, ls_list) {
  208. if (ls->ls_global_id == id) {
  209. atomic_inc(&ls->ls_count);
  210. goto out;
  211. }
  212. }
  213. ls = NULL;
  214. out:
  215. spin_unlock_bh(&lslist_lock);
  216. return ls;
  217. }
  218. struct dlm_ls *dlm_find_lockspace_local(dlm_lockspace_t *lockspace)
  219. {
  220. struct dlm_ls *ls = lockspace;
  221. atomic_inc(&ls->ls_count);
  222. return ls;
  223. }
  224. struct dlm_ls *dlm_find_lockspace_device(int minor)
  225. {
  226. struct dlm_ls *ls;
  227. spin_lock_bh(&lslist_lock);
  228. list_for_each_entry(ls, &lslist, ls_list) {
  229. if (ls->ls_device.minor == minor) {
  230. atomic_inc(&ls->ls_count);
  231. goto out;
  232. }
  233. }
  234. ls = NULL;
  235. out:
  236. spin_unlock_bh(&lslist_lock);
  237. return ls;
  238. }
  239. void dlm_put_lockspace(struct dlm_ls *ls)
  240. {
  241. if (atomic_dec_and_test(&ls->ls_count))
  242. wake_up(&ls->ls_count_wait);
  243. }
  244. static void remove_lockspace(struct dlm_ls *ls)
  245. {
  246. retry:
  247. wait_event(ls->ls_count_wait, atomic_read(&ls->ls_count) == 0);
  248. spin_lock_bh(&lslist_lock);
  249. if (atomic_read(&ls->ls_count) != 0) {
  250. spin_unlock_bh(&lslist_lock);
  251. goto retry;
  252. }
  253. WARN_ON(ls->ls_create_count != 0);
  254. list_del(&ls->ls_list);
  255. spin_unlock_bh(&lslist_lock);
  256. }
  257. static int threads_start(void)
  258. {
  259. int error;
  260. /* Thread for sending/receiving messages for all lockspace's */
  261. error = dlm_midcomms_start();
  262. if (error)
  263. log_print("cannot start dlm midcomms %d", error);
  264. return error;
  265. }
  266. static int lkb_idr_free(struct dlm_lkb *lkb)
  267. {
  268. if (lkb->lkb_lvbptr && test_bit(DLM_IFL_MSTCPY_BIT, &lkb->lkb_iflags))
  269. dlm_free_lvb(lkb->lkb_lvbptr);
  270. dlm_free_lkb(lkb);
  271. return 0;
  272. }
  273. static void rhash_free_rsb(void *ptr, void *arg)
  274. {
  275. struct dlm_rsb *rsb = ptr;
  276. dlm_free_rsb(rsb);
  277. }
  278. static void free_lockspace(struct work_struct *work)
  279. {
  280. struct dlm_ls *ls = container_of(work, struct dlm_ls, ls_free_work);
  281. struct dlm_lkb *lkb;
  282. unsigned long id;
  283. /*
  284. * Free all lkb's in xa
  285. */
  286. xa_for_each(&ls->ls_lkbxa, id, lkb) {
  287. lkb_idr_free(lkb);
  288. }
  289. xa_destroy(&ls->ls_lkbxa);
  290. /*
  291. * Free all rsb's on rsbtbl
  292. */
  293. rhashtable_free_and_destroy(&ls->ls_rsbtbl, rhash_free_rsb, NULL);
  294. kfree(ls);
  295. }
  296. static int new_lockspace(const char *name, const char *cluster,
  297. uint32_t flags, int lvblen,
  298. const struct dlm_lockspace_ops *ops, void *ops_arg,
  299. int *ops_result, dlm_lockspace_t **lockspace)
  300. {
  301. struct dlm_ls *ls;
  302. int namelen = strlen(name);
  303. int error;
  304. if (namelen > DLM_LOCKSPACE_LEN || namelen == 0)
  305. return -EINVAL;
  306. if (lvblen % 8)
  307. return -EINVAL;
  308. if (!try_module_get(THIS_MODULE))
  309. return -EINVAL;
  310. if (!dlm_user_daemon_available()) {
  311. log_print("dlm user daemon not available");
  312. error = -EUNATCH;
  313. goto out;
  314. }
  315. if (ops && ops_result) {
  316. if (!dlm_config.ci_recover_callbacks)
  317. *ops_result = -EOPNOTSUPP;
  318. else
  319. *ops_result = 0;
  320. }
  321. if (!cluster)
  322. log_print("dlm cluster name '%s' is being used without an application provided cluster name",
  323. dlm_config.ci_cluster_name);
  324. if (dlm_config.ci_recover_callbacks && cluster &&
  325. strncmp(cluster, dlm_config.ci_cluster_name, DLM_LOCKSPACE_LEN)) {
  326. log_print("dlm cluster name '%s' does not match "
  327. "the application cluster name '%s'",
  328. dlm_config.ci_cluster_name, cluster);
  329. error = -EBADR;
  330. goto out;
  331. }
  332. error = 0;
  333. spin_lock_bh(&lslist_lock);
  334. list_for_each_entry(ls, &lslist, ls_list) {
  335. WARN_ON(ls->ls_create_count <= 0);
  336. if (ls->ls_namelen != namelen)
  337. continue;
  338. if (memcmp(ls->ls_name, name, namelen))
  339. continue;
  340. if (flags & DLM_LSFL_NEWEXCL) {
  341. error = -EEXIST;
  342. break;
  343. }
  344. ls->ls_create_count++;
  345. *lockspace = ls;
  346. error = 1;
  347. break;
  348. }
  349. spin_unlock_bh(&lslist_lock);
  350. if (error)
  351. goto out;
  352. error = -ENOMEM;
  353. ls = kzalloc_obj(*ls, GFP_NOFS);
  354. if (!ls)
  355. goto out;
  356. memcpy(ls->ls_name, name, namelen);
  357. ls->ls_namelen = namelen;
  358. ls->ls_lvblen = lvblen;
  359. atomic_set(&ls->ls_count, 0);
  360. init_waitqueue_head(&ls->ls_count_wait);
  361. ls->ls_flags = 0;
  362. if (ops && dlm_config.ci_recover_callbacks) {
  363. ls->ls_ops = ops;
  364. ls->ls_ops_arg = ops_arg;
  365. }
  366. if (flags & DLM_LSFL_SOFTIRQ)
  367. set_bit(LSFL_SOFTIRQ, &ls->ls_flags);
  368. /* ls_exflags are forced to match among nodes, and we don't
  369. * need to require all nodes to have some flags set
  370. */
  371. ls->ls_exflags = (flags & ~(DLM_LSFL_FS | DLM_LSFL_NEWEXCL |
  372. DLM_LSFL_SOFTIRQ));
  373. INIT_LIST_HEAD(&ls->ls_slow_inactive);
  374. INIT_LIST_HEAD(&ls->ls_slow_active);
  375. rwlock_init(&ls->ls_rsbtbl_lock);
  376. error = rhashtable_init(&ls->ls_rsbtbl, &dlm_rhash_rsb_params);
  377. if (error)
  378. goto out_lsfree;
  379. xa_init_flags(&ls->ls_lkbxa, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_BH);
  380. rwlock_init(&ls->ls_lkbxa_lock);
  381. INIT_LIST_HEAD(&ls->ls_waiters);
  382. spin_lock_init(&ls->ls_waiters_lock);
  383. INIT_LIST_HEAD(&ls->ls_orphans);
  384. spin_lock_init(&ls->ls_orphans_lock);
  385. INIT_LIST_HEAD(&ls->ls_nodes);
  386. INIT_LIST_HEAD(&ls->ls_nodes_gone);
  387. ls->ls_num_nodes = 0;
  388. ls->ls_low_nodeid = 0;
  389. ls->ls_total_weight = 0;
  390. ls->ls_node_array = NULL;
  391. memset(&ls->ls_local_rsb, 0, sizeof(struct dlm_rsb));
  392. ls->ls_local_rsb.res_ls = ls;
  393. ls->ls_debug_rsb_dentry = NULL;
  394. ls->ls_debug_waiters_dentry = NULL;
  395. init_waitqueue_head(&ls->ls_uevent_wait);
  396. ls->ls_uevent_result = 0;
  397. init_completion(&ls->ls_recovery_done);
  398. ls->ls_recovery_result = -1;
  399. spin_lock_init(&ls->ls_cb_lock);
  400. INIT_LIST_HEAD(&ls->ls_cb_delay);
  401. INIT_WORK(&ls->ls_free_work, free_lockspace);
  402. ls->ls_recoverd_task = NULL;
  403. mutex_init(&ls->ls_recoverd_active);
  404. spin_lock_init(&ls->ls_recover_lock);
  405. spin_lock_init(&ls->ls_rcom_spin);
  406. get_random_bytes(&ls->ls_rcom_seq, sizeof(uint64_t));
  407. ls->ls_recover_status = 0;
  408. ls->ls_recover_seq = get_random_u64();
  409. ls->ls_recover_args = NULL;
  410. init_rwsem(&ls->ls_in_recovery);
  411. rwlock_init(&ls->ls_recv_active);
  412. INIT_LIST_HEAD(&ls->ls_requestqueue);
  413. rwlock_init(&ls->ls_requestqueue_lock);
  414. spin_lock_init(&ls->ls_clear_proc_locks);
  415. /* Due backwards compatibility with 3.1 we need to use maximum
  416. * possible dlm message size to be sure the message will fit and
  417. * not having out of bounds issues. However on sending side 3.2
  418. * might send less.
  419. */
  420. ls->ls_recover_buf = kmalloc(DLM_MAX_SOCKET_BUFSIZE, GFP_NOFS);
  421. if (!ls->ls_recover_buf) {
  422. error = -ENOMEM;
  423. goto out_lkbxa;
  424. }
  425. ls->ls_slot = 0;
  426. ls->ls_num_slots = 0;
  427. ls->ls_slots_size = 0;
  428. ls->ls_slots = NULL;
  429. INIT_LIST_HEAD(&ls->ls_recover_list);
  430. spin_lock_init(&ls->ls_recover_list_lock);
  431. xa_init_flags(&ls->ls_recover_xa, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_BH);
  432. spin_lock_init(&ls->ls_recover_xa_lock);
  433. ls->ls_recover_list_count = 0;
  434. init_waitqueue_head(&ls->ls_wait_general);
  435. INIT_LIST_HEAD(&ls->ls_masters_list);
  436. rwlock_init(&ls->ls_masters_lock);
  437. INIT_LIST_HEAD(&ls->ls_dir_dump_list);
  438. rwlock_init(&ls->ls_dir_dump_lock);
  439. INIT_LIST_HEAD(&ls->ls_scan_list);
  440. spin_lock_init(&ls->ls_scan_lock);
  441. timer_setup(&ls->ls_scan_timer, dlm_rsb_scan, TIMER_DEFERRABLE);
  442. spin_lock_bh(&lslist_lock);
  443. ls->ls_create_count = 1;
  444. list_add(&ls->ls_list, &lslist);
  445. spin_unlock_bh(&lslist_lock);
  446. if (flags & DLM_LSFL_FS)
  447. set_bit(LSFL_FS, &ls->ls_flags);
  448. error = dlm_callback_start(ls);
  449. if (error) {
  450. log_error(ls, "can't start dlm_callback %d", error);
  451. goto out_delist;
  452. }
  453. init_waitqueue_head(&ls->ls_recover_lock_wait);
  454. /*
  455. * Once started, dlm_recoverd first looks for ls in lslist, then
  456. * initializes ls_in_recovery as locked in "down" mode. We need
  457. * to wait for the wakeup from dlm_recoverd because in_recovery
  458. * has to start out in down mode.
  459. */
  460. error = dlm_recoverd_start(ls);
  461. if (error) {
  462. log_error(ls, "can't start dlm_recoverd %d", error);
  463. goto out_callback;
  464. }
  465. wait_event(ls->ls_recover_lock_wait,
  466. test_bit(LSFL_RECOVER_LOCK, &ls->ls_flags));
  467. ls->ls_kobj.kset = dlm_kset;
  468. error = kobject_init_and_add(&ls->ls_kobj, &dlm_ktype, NULL,
  469. "%s", ls->ls_name);
  470. if (error)
  471. goto out_recoverd;
  472. kobject_uevent(&ls->ls_kobj, KOBJ_ADD);
  473. /* This uevent triggers dlm_controld in userspace to add us to the
  474. group of nodes that are members of this lockspace (managed by the
  475. cluster infrastructure.) Once it's done that, it tells us who the
  476. current lockspace members are (via configfs) and then tells the
  477. lockspace to start running (via sysfs) in dlm_ls_start(). */
  478. error = do_uevent(ls, 1, 0);
  479. if (error < 0)
  480. goto out_recoverd;
  481. /* wait until recovery is successful or failed */
  482. wait_for_completion(&ls->ls_recovery_done);
  483. error = ls->ls_recovery_result;
  484. if (error)
  485. goto out_members;
  486. dlm_create_debug_file(ls);
  487. log_rinfo(ls, "join complete");
  488. *lockspace = ls;
  489. return 0;
  490. out_members:
  491. do_uevent(ls, 0, 0);
  492. dlm_clear_members(ls);
  493. kfree(ls->ls_node_array);
  494. out_recoverd:
  495. dlm_recoverd_stop(ls);
  496. out_callback:
  497. dlm_callback_stop(ls);
  498. out_delist:
  499. spin_lock_bh(&lslist_lock);
  500. list_del(&ls->ls_list);
  501. spin_unlock_bh(&lslist_lock);
  502. xa_destroy(&ls->ls_recover_xa);
  503. kfree(ls->ls_recover_buf);
  504. out_lkbxa:
  505. xa_destroy(&ls->ls_lkbxa);
  506. rhashtable_destroy(&ls->ls_rsbtbl);
  507. out_lsfree:
  508. kobject_put(&ls->ls_kobj);
  509. kfree(ls);
  510. out:
  511. module_put(THIS_MODULE);
  512. return error;
  513. }
  514. static int __dlm_new_lockspace(const char *name, const char *cluster,
  515. uint32_t flags, int lvblen,
  516. const struct dlm_lockspace_ops *ops,
  517. void *ops_arg, int *ops_result,
  518. dlm_lockspace_t **lockspace)
  519. {
  520. int error = 0;
  521. mutex_lock(&ls_lock);
  522. if (!ls_count)
  523. error = threads_start();
  524. if (error)
  525. goto out;
  526. error = new_lockspace(name, cluster, flags, lvblen, ops, ops_arg,
  527. ops_result, lockspace);
  528. if (!error)
  529. ls_count++;
  530. if (error > 0)
  531. error = 0;
  532. if (!ls_count) {
  533. dlm_midcomms_shutdown();
  534. dlm_midcomms_stop();
  535. }
  536. out:
  537. mutex_unlock(&ls_lock);
  538. return error;
  539. }
  540. int dlm_new_lockspace(const char *name, const char *cluster, uint32_t flags,
  541. int lvblen, const struct dlm_lockspace_ops *ops,
  542. void *ops_arg, int *ops_result,
  543. dlm_lockspace_t **lockspace)
  544. {
  545. return __dlm_new_lockspace(name, cluster, flags | DLM_LSFL_FS, lvblen,
  546. ops, ops_arg, ops_result, lockspace);
  547. }
  548. int dlm_new_user_lockspace(const char *name, const char *cluster,
  549. uint32_t flags, int lvblen,
  550. const struct dlm_lockspace_ops *ops,
  551. void *ops_arg, int *ops_result,
  552. dlm_lockspace_t **lockspace)
  553. {
  554. if (flags & DLM_LSFL_SOFTIRQ)
  555. return -EINVAL;
  556. return __dlm_new_lockspace(name, cluster, flags, lvblen, ops,
  557. ops_arg, ops_result, lockspace);
  558. }
  559. /* NOTE: We check the lkbxa here rather than the resource table.
  560. This is because there may be LKBs queued as ASTs that have been unlinked
  561. from their RSBs and are pending deletion once the AST has been delivered */
  562. static int lockspace_busy(struct dlm_ls *ls, unsigned int release_option)
  563. {
  564. struct dlm_lkb *lkb;
  565. unsigned long id;
  566. int rv = 0;
  567. read_lock_bh(&ls->ls_lkbxa_lock);
  568. if (release_option == DLM_RELEASE_NO_LOCKS) {
  569. xa_for_each(&ls->ls_lkbxa, id, lkb) {
  570. rv = 1;
  571. break;
  572. }
  573. } else if (release_option == DLM_RELEASE_UNUSED) {
  574. /* TODO: handle this UNUSED option as NO_LOCKS in later patch */
  575. xa_for_each(&ls->ls_lkbxa, id, lkb) {
  576. if (lkb->lkb_nodeid == 0 &&
  577. lkb->lkb_grmode != DLM_LOCK_IV) {
  578. rv = 1;
  579. break;
  580. }
  581. }
  582. } else {
  583. rv = 0;
  584. }
  585. read_unlock_bh(&ls->ls_lkbxa_lock);
  586. return rv;
  587. }
  588. static int release_lockspace(struct dlm_ls *ls, unsigned int release_option)
  589. {
  590. int busy, rv;
  591. busy = lockspace_busy(ls, release_option);
  592. spin_lock_bh(&lslist_lock);
  593. if (ls->ls_create_count == 1) {
  594. if (busy) {
  595. rv = -EBUSY;
  596. } else {
  597. /* remove_lockspace takes ls off lslist */
  598. ls->ls_create_count = 0;
  599. rv = 0;
  600. }
  601. } else if (ls->ls_create_count > 1) {
  602. rv = --ls->ls_create_count;
  603. } else {
  604. rv = -EINVAL;
  605. }
  606. spin_unlock_bh(&lslist_lock);
  607. if (rv) {
  608. log_debug(ls, "release_lockspace no remove %d", rv);
  609. return rv;
  610. }
  611. if (ls_count == 1)
  612. dlm_midcomms_version_wait();
  613. dlm_device_deregister(ls);
  614. if (release_option != DLM_RELEASE_NO_EVENT &&
  615. dlm_user_daemon_available())
  616. do_uevent(ls, 0, (release_option == DLM_RELEASE_RECOVER));
  617. dlm_recoverd_stop(ls);
  618. /* clear the LSFL_RUNNING flag to fast up
  619. * time_shutdown_sync(), we don't care anymore
  620. */
  621. clear_bit(LSFL_RUNNING, &ls->ls_flags);
  622. timer_shutdown_sync(&ls->ls_scan_timer);
  623. if (ls_count == 1) {
  624. dlm_clear_members(ls);
  625. dlm_midcomms_shutdown();
  626. }
  627. dlm_callback_stop(ls);
  628. remove_lockspace(ls);
  629. dlm_delete_debug_file(ls);
  630. kobject_put(&ls->ls_kobj);
  631. xa_destroy(&ls->ls_recover_xa);
  632. kfree(ls->ls_recover_buf);
  633. /*
  634. * Free structures on any other lists
  635. */
  636. dlm_purge_requestqueue(ls);
  637. kfree(ls->ls_recover_args);
  638. dlm_clear_members(ls);
  639. dlm_clear_members_gone(ls);
  640. kfree(ls->ls_node_array);
  641. log_rinfo(ls, "%s final free", __func__);
  642. /* delayed free of data structures see free_lockspace() */
  643. queue_work(dlm_wq, &ls->ls_free_work);
  644. module_put(THIS_MODULE);
  645. return 0;
  646. }
  647. /*
  648. * Called when a system has released all its locks and is not going to use the
  649. * lockspace any longer. We free everything we're managing for this lockspace.
  650. * Remaining nodes will go through the recovery process as if we'd died. The
  651. * lockspace must continue to function as usual, participating in recoveries,
  652. * until this returns.
  653. *
  654. * See DLM_RELEASE defines for release_option values and their meaning.
  655. */
  656. int dlm_release_lockspace(void *lockspace, unsigned int release_option)
  657. {
  658. struct dlm_ls *ls;
  659. int error;
  660. if (release_option > __DLM_RELEASE_MAX)
  661. return -EINVAL;
  662. ls = dlm_find_lockspace_local(lockspace);
  663. if (!ls)
  664. return -EINVAL;
  665. dlm_put_lockspace(ls);
  666. mutex_lock(&ls_lock);
  667. error = release_lockspace(ls, release_option);
  668. if (!error)
  669. ls_count--;
  670. if (!ls_count)
  671. dlm_midcomms_stop();
  672. mutex_unlock(&ls_lock);
  673. return error;
  674. }
  675. void dlm_stop_lockspaces(void)
  676. {
  677. struct dlm_ls *ls;
  678. int count;
  679. restart:
  680. count = 0;
  681. spin_lock_bh(&lslist_lock);
  682. list_for_each_entry(ls, &lslist, ls_list) {
  683. if (!test_bit(LSFL_RUNNING, &ls->ls_flags)) {
  684. count++;
  685. continue;
  686. }
  687. spin_unlock_bh(&lslist_lock);
  688. log_error(ls, "no userland control daemon, stopping lockspace");
  689. dlm_ls_stop(ls);
  690. goto restart;
  691. }
  692. spin_unlock_bh(&lslist_lock);
  693. if (count)
  694. log_print("dlm user daemon left %d lockspaces", count);
  695. }