dlmast.c 13 KB

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