dlmast.c 13 KB

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