dlmast.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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. struct dlm_lockstatus *lksb;
  194. mlog(0, "%s: res %.*s, lock %u:%llu, Local AST\n", dlm->name,
  195. res->lockname.len, res->lockname.name,
  196. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  197. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
  198. lksb = lock->lksb;
  199. fn = lock->ast;
  200. BUG_ON(lock->ml.node != dlm->node_num);
  201. dlm_update_lvb(dlm, res, lock);
  202. (*fn)(lock->astdata);
  203. }
  204. int dlm_do_remote_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  205. struct dlm_lock *lock)
  206. {
  207. int ret;
  208. struct dlm_lockstatus *lksb;
  209. int lksbflags;
  210. mlog(0, "%s: res %.*s, lock %u:%llu, Remote AST\n", dlm->name,
  211. res->lockname.len, res->lockname.name,
  212. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  213. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)));
  214. lksb = lock->lksb;
  215. BUG_ON(lock->ml.node == dlm->node_num);
  216. lksbflags = lksb->flags;
  217. dlm_update_lvb(dlm, res, lock);
  218. /* lock request came from another node
  219. * go do the ast over there */
  220. ret = dlm_send_proxy_ast(dlm, res, lock, lksbflags);
  221. return ret;
  222. }
  223. void dlm_do_local_bast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  224. struct dlm_lock *lock, int blocked_type)
  225. {
  226. dlm_bastlockfunc_t *fn = lock->bast;
  227. BUG_ON(lock->ml.node != dlm->node_num);
  228. mlog(0, "%s: res %.*s, lock %u:%llu, Local BAST, blocked %d\n",
  229. dlm->name, res->lockname.len, res->lockname.name,
  230. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  231. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
  232. blocked_type);
  233. (*fn)(lock->astdata, blocked_type);
  234. }
  235. int dlm_proxy_ast_handler(struct o2net_msg *msg, u32 len, void *data,
  236. void **ret_data)
  237. {
  238. int ret;
  239. unsigned int locklen;
  240. struct dlm_ctxt *dlm = data;
  241. struct dlm_lock_resource *res = NULL;
  242. struct dlm_lock *lock = NULL;
  243. struct dlm_proxy_ast *past = (struct dlm_proxy_ast *) msg->buf;
  244. char *name;
  245. struct list_head *head = NULL;
  246. __be64 cookie;
  247. u32 flags;
  248. u8 node;
  249. if (!dlm_grab(dlm)) {
  250. dlm_error(DLM_REJECTED);
  251. return DLM_REJECTED;
  252. }
  253. mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
  254. "Domain %s not fully joined!\n", dlm->name);
  255. name = past->name;
  256. locklen = past->namelen;
  257. cookie = past->cookie;
  258. flags = be32_to_cpu(past->flags);
  259. node = past->node_idx;
  260. if (locklen > DLM_LOCKID_NAME_MAX) {
  261. ret = DLM_IVBUFLEN;
  262. mlog(ML_ERROR, "Invalid name length (%d) in proxy ast "
  263. "handler!\n", locklen);
  264. goto leave;
  265. }
  266. if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
  267. (LKM_PUT_LVB|LKM_GET_LVB)) {
  268. mlog(ML_ERROR, "Both PUT and GET lvb specified, (0x%x)\n",
  269. flags);
  270. ret = DLM_BADARGS;
  271. goto leave;
  272. }
  273. mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
  274. (flags & LKM_GET_LVB ? "get lvb" : "none"));
  275. mlog(0, "type=%d, blocked_type=%d\n", past->type, past->blocked_type);
  276. if (past->type != DLM_AST &&
  277. past->type != DLM_BAST) {
  278. mlog(ML_ERROR, "Unknown ast type! %d, cookie=%u:%llu"
  279. "name=%.*s, node=%u\n", past->type,
  280. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  281. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  282. locklen, name, node);
  283. ret = DLM_IVLOCKID;
  284. goto leave;
  285. }
  286. res = dlm_lookup_lockres(dlm, name, locklen);
  287. if (!res) {
  288. mlog(0, "Got %sast for unknown lockres! cookie=%u:%llu, "
  289. "name=%.*s, node=%u\n", (past->type == DLM_AST ? "" : "b"),
  290. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  291. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  292. locklen, name, node);
  293. ret = DLM_IVLOCKID;
  294. goto leave;
  295. }
  296. /* cannot get a proxy ast message if this node owns it */
  297. BUG_ON(res->owner == dlm->node_num);
  298. mlog(0, "%s: res %.*s\n", dlm->name, res->lockname.len,
  299. res->lockname.name);
  300. spin_lock(&res->spinlock);
  301. if (res->state & DLM_LOCK_RES_RECOVERING) {
  302. mlog(0, "Responding with DLM_RECOVERING!\n");
  303. ret = DLM_RECOVERING;
  304. goto unlock_out;
  305. }
  306. if (res->state & DLM_LOCK_RES_MIGRATING) {
  307. mlog(0, "Responding with DLM_MIGRATING!\n");
  308. ret = DLM_MIGRATING;
  309. goto unlock_out;
  310. }
  311. /* try convert queue for both ast/bast */
  312. head = &res->converting;
  313. lock = NULL;
  314. list_for_each_entry(lock, head, list) {
  315. if (lock->ml.cookie == cookie)
  316. goto do_ast;
  317. }
  318. /* if not on convert, try blocked for ast, granted for bast */
  319. if (past->type == DLM_AST)
  320. head = &res->blocked;
  321. else
  322. head = &res->granted;
  323. list_for_each_entry(lock, head, list) {
  324. /* if lock is found but unlock is pending ignore the bast */
  325. if (lock->ml.cookie == cookie) {
  326. if (lock->unlock_pending)
  327. break;
  328. goto do_ast;
  329. }
  330. }
  331. mlog(0, "Got %sast for unknown lock! cookie=%u:%llu, name=%.*s, "
  332. "node=%u\n", past->type == DLM_AST ? "" : "b",
  333. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  334. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  335. locklen, name, node);
  336. ret = DLM_NORMAL;
  337. unlock_out:
  338. spin_unlock(&res->spinlock);
  339. goto leave;
  340. do_ast:
  341. ret = DLM_NORMAL;
  342. if (past->type == DLM_AST) {
  343. /* do not alter lock refcount. switching lists. */
  344. list_move_tail(&lock->list, &res->granted);
  345. mlog(0, "%s: res %.*s, lock %u:%llu, Granted type %d => %d\n",
  346. dlm->name, res->lockname.len, res->lockname.name,
  347. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  348. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  349. lock->ml.type, lock->ml.convert_type);
  350. if (lock->ml.convert_type != LKM_IVMODE) {
  351. lock->ml.type = lock->ml.convert_type;
  352. lock->ml.convert_type = LKM_IVMODE;
  353. } else {
  354. // should already be there....
  355. }
  356. lock->lksb->status = DLM_NORMAL;
  357. /* if we requested the lvb, fetch it into our lksb now */
  358. if (flags & LKM_GET_LVB) {
  359. BUG_ON(!(lock->lksb->flags & DLM_LKSB_GET_LVB));
  360. memcpy(lock->lksb->lvb, past->lvb, DLM_LVB_LEN);
  361. }
  362. }
  363. spin_unlock(&res->spinlock);
  364. if (past->type == DLM_AST)
  365. dlm_do_local_ast(dlm, res, lock);
  366. else
  367. dlm_do_local_bast(dlm, res, lock, past->blocked_type);
  368. leave:
  369. if (res)
  370. dlm_lockres_put(res);
  371. dlm_put(dlm);
  372. return ret;
  373. }
  374. int dlm_send_proxy_ast_msg(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  375. struct dlm_lock *lock, int msg_type,
  376. int blocked_type, int flags)
  377. {
  378. int ret = 0;
  379. struct dlm_proxy_ast past;
  380. struct kvec vec[2];
  381. size_t veclen = 1;
  382. int status;
  383. mlog(0, "%s: res %.*s, to %u, type %d, blocked_type %d\n", dlm->name,
  384. res->lockname.len, res->lockname.name, lock->ml.node, msg_type,
  385. blocked_type);
  386. memset(&past, 0, sizeof(struct dlm_proxy_ast));
  387. past.node_idx = dlm->node_num;
  388. past.type = msg_type;
  389. past.blocked_type = blocked_type;
  390. past.namelen = res->lockname.len;
  391. memcpy(past.name, res->lockname.name, past.namelen);
  392. past.cookie = lock->ml.cookie;
  393. vec[0].iov_len = sizeof(struct dlm_proxy_ast);
  394. vec[0].iov_base = &past;
  395. if (flags & DLM_LKSB_GET_LVB) {
  396. be32_add_cpu(&past.flags, LKM_GET_LVB);
  397. vec[1].iov_len = DLM_LVB_LEN;
  398. vec[1].iov_base = lock->lksb->lvb;
  399. veclen++;
  400. }
  401. ret = o2net_send_message_vec(DLM_PROXY_AST_MSG, dlm->key, vec, veclen,
  402. lock->ml.node, &status);
  403. if (ret < 0)
  404. mlog(ML_ERROR, "%s: res %.*s, error %d send AST to node %u\n",
  405. dlm->name, res->lockname.len, res->lockname.name, ret,
  406. lock->ml.node);
  407. else {
  408. if (status == DLM_RECOVERING) {
  409. mlog(ML_ERROR, "sent AST to node %u, it thinks this "
  410. "node is dead!\n", lock->ml.node);
  411. BUG();
  412. } else if (status == DLM_MIGRATING) {
  413. mlog(ML_ERROR, "sent AST to node %u, it returned "
  414. "DLM_MIGRATING!\n", lock->ml.node);
  415. BUG();
  416. } else if (status != DLM_NORMAL && status != DLM_IVLOCKID) {
  417. mlog(ML_ERROR, "AST to node %u returned %d!\n",
  418. lock->ml.node, status);
  419. /* ignore it */
  420. }
  421. ret = 0;
  422. }
  423. return ret;
  424. }