PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ocfs2/dlm/dlmast.c

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