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/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. 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 = be64_to_cpu(past->cookie);
  239. flags = be32_to_cpu(past->flags);
  240. if (locklen > DLM_LOCKID_NAME_MAX) {
  241. ret = DLM_IVBUFLEN;
  242. mlog(ML_ERROR, "Invalid name length in proxy ast handler!\n");
  243. goto leave;
  244. }
  245. if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
  246. (LKM_PUT_LVB|LKM_GET_LVB)) {
  247. mlog(ML_ERROR, "both PUT and GET lvb specified\n");
  248. ret = DLM_BADARGS;
  249. goto leave;
  250. }
  251. mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
  252. (flags & LKM_GET_LVB ? "get lvb" : "none"));
  253. mlog(0, "type=%d, blocked_type=%d\n", past->type, past->blocked_type);
  254. if (past->type != DLM_AST &&
  255. past->type != DLM_BAST) {
  256. mlog(ML_ERROR, "Unknown ast type! %d, cookie=%u:%llu"
  257. "name=%.*s\n", past->type,
  258. dlm_get_lock_cookie_node(cookie),
  259. dlm_get_lock_cookie_seq(cookie),
  260. locklen, name);
  261. ret = DLM_IVLOCKID;
  262. goto leave;
  263. }
  264. res = dlm_lookup_lockres(dlm, name, locklen);
  265. if (!res) {
  266. mlog(0, "got %sast for unknown lockres! "
  267. "cookie=%u:%llu, name=%.*s, namelen=%u\n",
  268. past->type == DLM_AST ? "" : "b",
  269. dlm_get_lock_cookie_node(cookie),
  270. dlm_get_lock_cookie_seq(cookie),
  271. locklen, name, locklen);
  272. ret = DLM_IVLOCKID;
  273. goto leave;
  274. }
  275. /* cannot get a proxy ast message if this node owns it */
  276. BUG_ON(res->owner == dlm->node_num);
  277. mlog(0, "lockres %.*s\n", res->lockname.len, res->lockname.name);
  278. spin_lock(&res->spinlock);
  279. if (res->state & DLM_LOCK_RES_RECOVERING) {
  280. mlog(0, "responding with DLM_RECOVERING!\n");
  281. ret = DLM_RECOVERING;
  282. goto unlock_out;
  283. }
  284. if (res->state & DLM_LOCK_RES_MIGRATING) {
  285. mlog(0, "responding with DLM_MIGRATING!\n");
  286. ret = DLM_MIGRATING;
  287. goto unlock_out;
  288. }
  289. /* try convert queue for both ast/bast */
  290. head = &res->converting;
  291. lock = NULL;
  292. list_for_each(iter, head) {
  293. lock = list_entry (iter, struct dlm_lock, list);
  294. if (be64_to_cpu(lock->ml.cookie) == cookie)
  295. goto do_ast;
  296. }
  297. /* if not on convert, try blocked for ast, granted for bast */
  298. if (past->type == DLM_AST)
  299. head = &res->blocked;
  300. else
  301. head = &res->granted;
  302. list_for_each(iter, head) {
  303. lock = list_entry (iter, struct dlm_lock, list);
  304. if (be64_to_cpu(lock->ml.cookie) == cookie)
  305. goto do_ast;
  306. }
  307. mlog(0, "got %sast for unknown lock! cookie=%u:%llu, "
  308. "name=%.*s, namelen=%u\n", past->type == DLM_AST ? "" : "b",
  309. dlm_get_lock_cookie_node(cookie),
  310. dlm_get_lock_cookie_seq(cookie),
  311. locklen, name, locklen);
  312. ret = DLM_NORMAL;
  313. unlock_out:
  314. spin_unlock(&res->spinlock);
  315. goto leave;
  316. do_ast:
  317. ret = DLM_NORMAL;
  318. if (past->type == DLM_AST) {
  319. /* do not alter lock refcount. switching lists. */
  320. list_move_tail(&lock->list, &res->granted);
  321. mlog(0, "ast: adding to granted list... type=%d, "
  322. "convert_type=%d\n", lock->ml.type, lock->ml.convert_type);
  323. if (lock->ml.convert_type != LKM_IVMODE) {
  324. lock->ml.type = lock->ml.convert_type;
  325. lock->ml.convert_type = LKM_IVMODE;
  326. } else {
  327. // should already be there....
  328. }
  329. lock->lksb->status = DLM_NORMAL;
  330. /* if we requested the lvb, fetch it into our lksb now */
  331. if (flags & LKM_GET_LVB) {
  332. BUG_ON(!(lock->lksb->flags & DLM_LKSB_GET_LVB));
  333. memcpy(lock->lksb->lvb, past->lvb, DLM_LVB_LEN);
  334. }
  335. }
  336. spin_unlock(&res->spinlock);
  337. if (past->type == DLM_AST)
  338. dlm_do_local_ast(dlm, res, lock);
  339. else
  340. dlm_do_local_bast(dlm, res, lock, past->blocked_type);
  341. leave:
  342. if (res)
  343. dlm_lockres_put(res);
  344. dlm_put(dlm);
  345. return ret;
  346. }
  347. int dlm_send_proxy_ast_msg(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  348. struct dlm_lock *lock, int msg_type,
  349. int blocked_type, int flags)
  350. {
  351. int ret = 0;
  352. struct dlm_proxy_ast past;
  353. struct kvec vec[2];
  354. size_t veclen = 1;
  355. int status;
  356. mlog_entry("res %.*s, to=%u, type=%d, blocked_type=%d\n",
  357. res->lockname.len, res->lockname.name, lock->ml.node,
  358. msg_type, blocked_type);
  359. memset(&past, 0, sizeof(struct dlm_proxy_ast));
  360. past.node_idx = dlm->node_num;
  361. past.type = msg_type;
  362. past.blocked_type = blocked_type;
  363. past.namelen = res->lockname.len;
  364. memcpy(past.name, res->lockname.name, past.namelen);
  365. past.cookie = lock->ml.cookie;
  366. vec[0].iov_len = sizeof(struct dlm_proxy_ast);
  367. vec[0].iov_base = &past;
  368. if (flags & DLM_LKSB_GET_LVB) {
  369. mlog(0, "returning requested LVB data\n");
  370. be32_add_cpu(&past.flags, LKM_GET_LVB);
  371. vec[1].iov_len = DLM_LVB_LEN;
  372. vec[1].iov_base = lock->lksb->lvb;
  373. veclen++;
  374. }
  375. ret = o2net_send_message_vec(DLM_PROXY_AST_MSG, dlm->key, vec, veclen,
  376. lock->ml.node, &status);
  377. if (ret < 0)
  378. mlog_errno(ret);
  379. else {
  380. if (status == DLM_RECOVERING) {
  381. mlog(ML_ERROR, "sent AST to node %u, it thinks this "
  382. "node is dead!\n", lock->ml.node);
  383. BUG();
  384. } else if (status == DLM_MIGRATING) {
  385. mlog(ML_ERROR, "sent AST to node %u, it returned "
  386. "DLM_MIGRATING!\n", lock->ml.node);
  387. BUG();
  388. } else if (status != DLM_NORMAL && status != DLM_IVLOCKID) {
  389. mlog(ML_ERROR, "AST to node %u returned %d!\n",
  390. lock->ml.node, status);
  391. /* ignore it */
  392. }
  393. ret = 0;
  394. }
  395. return ret;
  396. }