dlmast.c 13 KB

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