PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/ocfs2/dlm/dlmast.c

http://github.com/torvalds/linux
C | 487 lines | 353 code | 80 blank | 54 comment | 64 complexity | 455608991facfe89bc115fa1229fd9c2 MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * dlmast.c
  6. *
  7. * AST and BAST functionality for local and remote nodes
  8. *
  9. * Copyright (C) 2004 Oracle. All rights reserved.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/types.h>
  14. #include <linux/highmem.h>
  15. #include <linux/init.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/random.h>
  18. #include <linux/blkdev.h>
  19. #include <linux/socket.h>
  20. #include <linux/inet.h>
  21. #include <linux/spinlock.h>
  22. #include "../cluster/heartbeat.h"
  23. #include "../cluster/nodemanager.h"
  24. #include "../cluster/tcp.h"
  25. #include "dlmapi.h"
  26. #include "dlmcommon.h"
  27. #define MLOG_MASK_PREFIX ML_DLM
  28. #include "../cluster/masklog.h"
  29. static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  30. struct dlm_lock *lock);
  31. static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
  32. /* Should be called as an ast gets queued to see if the new
  33. * lock level will obsolete a pending bast.
  34. * For example, if dlm_thread queued a bast for an EX lock that
  35. * was blocking another EX, but before sending the bast the
  36. * lock owner downconverted to NL, the bast is now obsolete.
  37. * Only the ast should be sent.
  38. * This is needed because the lock and convert paths can queue
  39. * asts out-of-band (not waiting for dlm_thread) in order to
  40. * allow for LKM_NOQUEUE to get immediate responses. */
  41. static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  42. {
  43. assert_spin_locked(&dlm->ast_lock);
  44. assert_spin_locked(&lock->spinlock);
  45. if (lock->ml.highest_blocked == LKM_IVMODE)
  46. return 0;
  47. BUG_ON(lock->ml.highest_blocked == LKM_NLMODE);
  48. if (lock->bast_pending &&
  49. list_empty(&lock->bast_list))
  50. /* old bast already sent, ok */
  51. return 0;
  52. if (lock->ml.type == LKM_EXMODE)
  53. /* EX blocks anything left, any bast still valid */
  54. return 0;
  55. else if (lock->ml.type == LKM_NLMODE)
  56. /* NL blocks nothing, no reason to send any bast, cancel it */
  57. return 1;
  58. else if (lock->ml.highest_blocked != LKM_EXMODE)
  59. /* PR only blocks EX */
  60. return 1;
  61. return 0;
  62. }
  63. void __dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  64. {
  65. struct dlm_lock_resource *res;
  66. BUG_ON(!dlm);
  67. BUG_ON(!lock);
  68. res = lock->lockres;
  69. assert_spin_locked(&dlm->ast_lock);
  70. if (!list_empty(&lock->ast_list)) {
  71. mlog(ML_ERROR, "%s: res %.*s, lock %u:%llu, "
  72. "AST list not empty, pending %d, newlevel %d\n",
  73. dlm->name, res->lockname.len, res->lockname.name,
  74. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  75. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
  76. lock->ast_pending, lock->ml.type);
  77. BUG();
  78. }
  79. if (lock->ast_pending)
  80. mlog(0, "%s: res %.*s, lock %u:%llu, AST getting flushed\n",
  81. dlm->name, res->lockname.len, res->lockname.name,
  82. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  83. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
  84. /* putting lock on list, add a ref */
  85. dlm_lock_get(lock);
  86. spin_lock(&lock->spinlock);
  87. /* check to see if this ast obsoletes the bast */
  88. if (dlm_should_cancel_bast(dlm, lock)) {
  89. mlog(0, "%s: res %.*s, lock %u:%llu, Cancelling BAST\n",
  90. dlm->name, res->lockname.len, res->lockname.name,
  91. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  92. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
  93. lock->bast_pending = 0;
  94. list_del_init(&lock->bast_list);
  95. lock->ml.highest_blocked = LKM_IVMODE;
  96. /* removing lock from list, remove a ref. guaranteed
  97. * this won't be the last ref because of the get above,
  98. * so res->spinlock will not be taken here */
  99. dlm_lock_put(lock);
  100. /* free up the reserved bast that we are cancelling.
  101. * guaranteed that this will not be the last reserved
  102. * ast because *both* an ast and a bast were reserved
  103. * to get to this point. the res->spinlock will not be
  104. * taken here */
  105. dlm_lockres_release_ast(dlm, res);
  106. }
  107. list_add_tail(&lock->ast_list, &dlm->pending_asts);
  108. lock->ast_pending = 1;
  109. spin_unlock(&lock->spinlock);
  110. }
  111. void dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  112. {
  113. BUG_ON(!dlm);
  114. BUG_ON(!lock);
  115. spin_lock(&dlm->ast_lock);
  116. __dlm_queue_ast(dlm, lock);
  117. spin_unlock(&dlm->ast_lock);
  118. }
  119. void __dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  120. {
  121. struct dlm_lock_resource *res;
  122. BUG_ON(!dlm);
  123. BUG_ON(!lock);
  124. assert_spin_locked(&dlm->ast_lock);
  125. res = lock->lockres;
  126. BUG_ON(!list_empty(&lock->bast_list));
  127. if (lock->bast_pending)
  128. mlog(0, "%s: res %.*s, lock %u:%llu, BAST getting flushed\n",
  129. dlm->name, res->lockname.len, res->lockname.name,
  130. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  131. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
  132. /* putting lock on list, add a ref */
  133. dlm_lock_get(lock);
  134. spin_lock(&lock->spinlock);
  135. list_add_tail(&lock->bast_list, &dlm->pending_basts);
  136. lock->bast_pending = 1;
  137. spin_unlock(&lock->spinlock);
  138. }
  139. void dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  140. {
  141. BUG_ON(!dlm);
  142. BUG_ON(!lock);
  143. spin_lock(&dlm->ast_lock);
  144. __dlm_queue_bast(dlm, lock);
  145. spin_unlock(&dlm->ast_lock);
  146. }
  147. static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  148. struct dlm_lock *lock)
  149. {
  150. struct dlm_lockstatus *lksb = lock->lksb;
  151. BUG_ON(!lksb);
  152. /* only updates if this node masters the lockres */
  153. spin_lock(&res->spinlock);
  154. if (res->owner == dlm->node_num) {
  155. /* check the lksb flags for the direction */
  156. if (lksb->flags & DLM_LKSB_GET_LVB) {
  157. mlog(0, "getting lvb from lockres for %s node\n",
  158. lock->ml.node == dlm->node_num ? "master" :
  159. "remote");
  160. memcpy(lksb->lvb, res->lvb, DLM_LVB_LEN);
  161. }
  162. /* Do nothing for lvb put requests - they should be done in
  163. * place when the lock is downconverted - otherwise we risk
  164. * racing gets and puts which could result in old lvb data
  165. * being propagated. We leave the put flag set and clear it
  166. * here. In the future we might want to clear it at the time
  167. * the put is actually done.
  168. */
  169. }
  170. spin_unlock(&res->spinlock);
  171. /* reset any lvb flags on the lksb */
  172. lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
  173. }
  174. void dlm_do_local_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  175. struct dlm_lock *lock)
  176. {
  177. dlm_astlockfunc_t *fn;
  178. mlog(0, "%s: res %.*s, lock %u:%llu, Local AST\n", dlm->name,
  179. res->lockname.len, res->lockname.name,
  180. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  181. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
  182. fn = lock->ast;
  183. BUG_ON(lock->ml.node != dlm->node_num);
  184. dlm_update_lvb(dlm, res, lock);
  185. (*fn)(lock->astdata);
  186. }
  187. int dlm_do_remote_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  188. struct dlm_lock *lock)
  189. {
  190. int ret;
  191. struct dlm_lockstatus *lksb;
  192. int lksbflags;
  193. mlog(0, "%s: res %.*s, lock %u:%llu, Remote AST\n", dlm->name,
  194. res->lockname.len, res->lockname.name,
  195. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  196. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
  197. lksb = lock->lksb;
  198. BUG_ON(lock->ml.node == dlm->node_num);
  199. lksbflags = lksb->flags;
  200. dlm_update_lvb(dlm, res, lock);
  201. /* lock request came from another node
  202. * go do the ast over there */
  203. ret = dlm_send_proxy_ast(dlm, res, lock, lksbflags);
  204. return ret;
  205. }
  206. void dlm_do_local_bast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  207. struct dlm_lock *lock, int blocked_type)
  208. {
  209. dlm_bastlockfunc_t *fn = lock->bast;
  210. BUG_ON(lock->ml.node != dlm->node_num);
  211. mlog(0, "%s: res %.*s, lock %u:%llu, Local BAST, blocked %d\n",
  212. dlm->name, res->lockname.len, res->lockname.name,
  213. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  214. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
  215. blocked_type);
  216. (*fn)(lock->astdata, blocked_type);
  217. }
  218. int dlm_proxy_ast_handler(struct o2net_msg *msg, u32 len, void *data,
  219. void **ret_data)
  220. {
  221. int ret;
  222. unsigned int locklen;
  223. struct dlm_ctxt *dlm = data;
  224. struct dlm_lock_resource *res = NULL;
  225. struct dlm_lock *lock = NULL;
  226. struct dlm_proxy_ast *past = (struct dlm_proxy_ast *) msg->buf;
  227. char *name;
  228. struct list_head *head = NULL;
  229. __be64 cookie;
  230. u32 flags;
  231. u8 node;
  232. if (!dlm_grab(dlm)) {
  233. dlm_error(DLM_REJECTED);
  234. return DLM_REJECTED;
  235. }
  236. mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
  237. "Domain %s not fully joined!\n", dlm->name);
  238. name = past->name;
  239. locklen = past->namelen;
  240. cookie = past->cookie;
  241. flags = be32_to_cpu(past->flags);
  242. node = past->node_idx;
  243. if (locklen > DLM_LOCKID_NAME_MAX) {
  244. ret = DLM_IVBUFLEN;
  245. mlog(ML_ERROR, "Invalid name length (%d) in proxy ast "
  246. "handler!\n", locklen);
  247. goto leave;
  248. }
  249. if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
  250. (LKM_PUT_LVB|LKM_GET_LVB)) {
  251. mlog(ML_ERROR, "Both PUT and GET lvb specified, (0x%x)\n",
  252. flags);
  253. ret = DLM_BADARGS;
  254. goto leave;
  255. }
  256. mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
  257. (flags & LKM_GET_LVB ? "get lvb" : "none"));
  258. mlog(0, "type=%d, blocked_type=%d\n", past->type, past->blocked_type);
  259. if (past->type != DLM_AST &&
  260. past->type != DLM_BAST) {
  261. mlog(ML_ERROR, "Unknown ast type! %d, cookie=%u:%llu"
  262. "name=%.*s, node=%u\n", past->type,
  263. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  264. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  265. locklen, name, node);
  266. ret = DLM_IVLOCKID;
  267. goto leave;
  268. }
  269. res = dlm_lookup_lockres(dlm, name, locklen);
  270. if (!res) {
  271. mlog(0, "Got %sast for unknown lockres! cookie=%u:%llu, "
  272. "name=%.*s, node=%u\n", (past->type == DLM_AST ? "" : "b"),
  273. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  274. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  275. locklen, name, node);
  276. ret = DLM_IVLOCKID;
  277. goto leave;
  278. }
  279. /* cannot get a proxy ast message if this node owns it */
  280. BUG_ON(res->owner == dlm->node_num);
  281. mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
  282. res->lockname.name);
  283. spin_lock(&res->spinlock);
  284. if (res->state & DLM_LOCK_RES_RECOVERING) {
  285. mlog(0, "Responding with DLM_RECOVERING!\n");
  286. ret = DLM_RECOVERING;
  287. goto unlock_out;
  288. }
  289. if (res->state & DLM_LOCK_RES_MIGRATING) {
  290. mlog(0, "Responding with DLM_MIGRATING!\n");
  291. ret = DLM_MIGRATING;
  292. goto unlock_out;
  293. }
  294. /* try convert queue for both ast/bast */
  295. head = &res->converting;
  296. lock = NULL;
  297. list_for_each_entry(lock, head, list) {
  298. if (lock->ml.cookie == cookie)
  299. goto do_ast;
  300. }
  301. /* if not on convert, try blocked for ast, granted for bast */
  302. if (past->type == DLM_AST)
  303. head = &res->blocked;
  304. else
  305. head = &res->granted;
  306. list_for_each_entry(lock, head, list) {
  307. /* if lock is found but unlock is pending ignore the bast */
  308. if (lock->ml.cookie == cookie) {
  309. if (lock->unlock_pending)
  310. break;
  311. goto do_ast;
  312. }
  313. }
  314. mlog(0, "Got %sast for unknown lock! cookie=%u:%llu, name=%.*s, "
  315. "node=%u\n", past->type == DLM_AST ? "" : "b",
  316. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  317. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  318. locklen, name, node);
  319. ret = DLM_NORMAL;
  320. unlock_out:
  321. spin_unlock(&res->spinlock);
  322. goto leave;
  323. do_ast:
  324. ret = DLM_NORMAL;
  325. if (past->type == DLM_AST) {
  326. /* do not alter lock refcount. switching lists. */
  327. list_move_tail(&lock->list, &res->granted);
  328. mlog(0, "%s: res %.*s, lock %u:%llu, Granted type %d => %d\n",
  329. dlm->name, res->lockname.len, res->lockname.name,
  330. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  331. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  332. lock->ml.type, lock->ml.convert_type);
  333. if (lock->ml.convert_type != LKM_IVMODE) {
  334. lock->ml.type = lock->ml.convert_type;
  335. lock->ml.convert_type = LKM_IVMODE;
  336. } else {
  337. // should already be there....
  338. }
  339. lock->lksb->status = DLM_NORMAL;
  340. /* if we requested the lvb, fetch it into our lksb now */
  341. if (flags & LKM_GET_LVB) {
  342. BUG_ON(!(lock->lksb->flags & DLM_LKSB_GET_LVB));
  343. memcpy(lock->lksb->lvb, past->lvb, DLM_LVB_LEN);
  344. }
  345. }
  346. spin_unlock(&res->spinlock);
  347. if (past->type == DLM_AST)
  348. dlm_do_local_ast(dlm, res, lock);
  349. else
  350. dlm_do_local_bast(dlm, res, lock, past->blocked_type);
  351. leave:
  352. if (res)
  353. dlm_lockres_put(res);
  354. dlm_put(dlm);
  355. return ret;
  356. }
  357. int dlm_send_proxy_ast_msg(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  358. struct dlm_lock *lock, int msg_type,
  359. int blocked_type, int flags)
  360. {
  361. int ret = 0;
  362. struct dlm_proxy_ast past;
  363. struct kvec vec[2];
  364. size_t veclen = 1;
  365. int status;
  366. mlog(0, "%s: res %.*s, to %u, type %d, blocked_type %d\n", dlm->name,
  367. res->lockname.len, res->lockname.name, lock->ml.node, msg_type,
  368. blocked_type);
  369. memset(&past, 0, sizeof(struct dlm_proxy_ast));
  370. past.node_idx = dlm->node_num;
  371. past.type = msg_type;
  372. past.blocked_type = blocked_type;
  373. past.namelen = res->lockname.len;
  374. memcpy(past.name, res->lockname.name, past.namelen);
  375. past.cookie = lock->ml.cookie;
  376. vec[0].iov_len = sizeof(struct dlm_proxy_ast);
  377. vec[0].iov_base = &past;
  378. if (flags & DLM_LKSB_GET_LVB) {
  379. be32_add_cpu(&past.flags, LKM_GET_LVB);
  380. vec[1].iov_len = DLM_LVB_LEN;
  381. vec[1].iov_base = lock->lksb->lvb;
  382. veclen++;
  383. }
  384. ret = o2net_send_message_vec(DLM_PROXY_AST_MSG, dlm->key, vec, veclen,
  385. lock->ml.node, &status);
  386. if (ret < 0)
  387. mlog(ML_ERROR, "%s: res %.*s, error %d send AST to node %u\n",
  388. dlm->name, res->lockname.len, res->lockname.name, ret,
  389. lock->ml.node);
  390. else {
  391. if (status == DLM_RECOVERING) {
  392. mlog(ML_ERROR, "sent AST to node %u, it thinks this "
  393. "node is dead!\n", lock->ml.node);
  394. BUG();
  395. } else if (status == DLM_MIGRATING) {
  396. mlog(ML_ERROR, "sent AST to node %u, it returned "
  397. "DLM_MIGRATING!\n", lock->ml.node);
  398. BUG();
  399. } else if (status != DLM_NORMAL && status != DLM_IVLOCKID) {
  400. mlog(ML_ERROR, "AST to node %u returned %d!\n",
  401. lock->ml.node, status);
  402. /* ignore it */
  403. }
  404. ret = 0;
  405. }
  406. return ret;
  407. }