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 "cluster/endian.h"
  43. #include "dlmapi.h"
  44. #include "dlmcommon.h"
  45. #define MLOG_MASK_PREFIX ML_DLM
  46. #include "cluster/masklog.h"
  47. static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  48. struct dlm_lock *lock);
  49. static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
  50. /* Should be called as an ast gets queued to see if the new
  51. * lock level will obsolete a pending bast.
  52. * For example, if dlm_thread queued a bast for an EX lock that
  53. * was blocking another EX, but before sending the bast the
  54. * lock owner downconverted to NL, the bast is now obsolete.
  55. * Only the ast should be sent.
  56. * This is needed because the lock and convert paths can queue
  57. * asts out-of-band (not waiting for dlm_thread) in order to
  58. * allow for LKM_NOQUEUE to get immediate responses. */
  59. static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  60. {
  61. assert_spin_locked(&dlm->ast_lock);
  62. assert_spin_locked(&lock->spinlock);
  63. if (lock->ml.highest_blocked == LKM_IVMODE)
  64. return 0;
  65. BUG_ON(lock->ml.highest_blocked == LKM_NLMODE);
  66. if (lock->bast_pending &&
  67. list_empty(&lock->bast_list))
  68. /* old bast already sent, ok */
  69. return 0;
  70. if (lock->ml.type == LKM_EXMODE)
  71. /* EX blocks anything left, any bast still valid */
  72. return 0;
  73. else if (lock->ml.type == LKM_NLMODE)
  74. /* NL blocks nothing, no reason to send any bast, cancel it */
  75. return 1;
  76. else if (lock->ml.highest_blocked != LKM_EXMODE)
  77. /* PR only blocks EX */
  78. return 1;
  79. return 0;
  80. }
  81. static void __dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  82. {
  83. mlog_entry_void();
  84. BUG_ON(!dlm);
  85. BUG_ON(!lock);
  86. assert_spin_locked(&dlm->ast_lock);
  87. if (!list_empty(&lock->ast_list)) {
  88. mlog(ML_ERROR, "ast list not empty!! pending=%d, newlevel=%d\n",
  89. lock->ast_pending, lock->ml.type);
  90. BUG();
  91. }
  92. BUG_ON(!list_empty(&lock->ast_list));
  93. if (lock->ast_pending)
  94. mlog(0, "lock has an ast getting flushed right now\n");
  95. /* putting lock on list, add a ref */
  96. dlm_lock_get(lock);
  97. spin_lock(&lock->spinlock);
  98. /* check to see if this ast obsoletes the bast */
  99. if (dlm_should_cancel_bast(dlm, lock)) {
  100. struct dlm_lock_resource *res = lock->lockres;
  101. mlog(0, "%s: cancelling bast for %.*s\n",
  102. dlm->name, res->lockname.len, res->lockname.name);
  103. lock->bast_pending = 0;
  104. list_del_init(&lock->bast_list);
  105. lock->ml.highest_blocked = LKM_IVMODE;
  106. /* removing lock from list, remove a ref. guaranteed
  107. * this won't be the last ref because of the get above,
  108. * so res->spinlock will not be taken here */
  109. dlm_lock_put(lock);
  110. /* free up the reserved bast that we are cancelling.
  111. * guaranteed that this will not be the last reserved
  112. * ast because *both* an ast and a bast were reserved
  113. * to get to this point. the res->spinlock will not be
  114. * taken here */
  115. dlm_lockres_release_ast(dlm, res);
  116. }
  117. list_add_tail(&lock->ast_list, &dlm->pending_asts);
  118. lock->ast_pending = 1;
  119. spin_unlock(&lock->spinlock);
  120. }
  121. void dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  122. {
  123. mlog_entry_void();
  124. BUG_ON(!dlm);
  125. BUG_ON(!lock);
  126. spin_lock(&dlm->ast_lock);
  127. __dlm_queue_ast(dlm, lock);
  128. spin_unlock(&dlm->ast_lock);
  129. }
  130. static void __dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  131. {
  132. mlog_entry_void();
  133. BUG_ON(!dlm);
  134. BUG_ON(!lock);
  135. assert_spin_locked(&dlm->ast_lock);
  136. BUG_ON(!list_empty(&lock->bast_list));
  137. if (lock->bast_pending)
  138. mlog(0, "lock has a bast getting flushed right now\n");
  139. /* putting lock on list, add a ref */
  140. dlm_lock_get(lock);
  141. spin_lock(&lock->spinlock);
  142. list_add_tail(&lock->bast_list, &dlm->pending_basts);
  143. lock->bast_pending = 1;
  144. spin_unlock(&lock->spinlock);
  145. }
  146. void dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  147. {
  148. mlog_entry_void();
  149. BUG_ON(!dlm);
  150. BUG_ON(!lock);
  151. spin_lock(&dlm->ast_lock);
  152. __dlm_queue_bast(dlm, lock);
  153. spin_unlock(&dlm->ast_lock);
  154. }
  155. static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  156. struct dlm_lock *lock)
  157. {
  158. struct dlm_lockstatus *lksb = lock->lksb;
  159. BUG_ON(!lksb);
  160. /* only updates if this node masters the lockres */
  161. if (res->owner == dlm->node_num) {
  162. spin_lock(&res->spinlock);
  163. /* check the lksb flags for the direction */
  164. if (lksb->flags & DLM_LKSB_GET_LVB) {
  165. mlog(0, "getting lvb from lockres for %s node\n",
  166. lock->ml.node == dlm->node_num ? "master" :
  167. "remote");
  168. memcpy(lksb->lvb, res->lvb, DLM_LVB_LEN);
  169. } else if (lksb->flags & DLM_LKSB_PUT_LVB) {
  170. mlog(0, "setting lvb from lockres for %s node\n",
  171. lock->ml.node == dlm->node_num ? "master" :
  172. "remote");
  173. memcpy(res->lvb, lksb->lvb, DLM_LVB_LEN);
  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. {
  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. if (!dlm_grab(dlm)) {
  229. dlm_error(DLM_REJECTED);
  230. return DLM_REJECTED;
  231. }
  232. mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
  233. "Domain %s not fully joined!\n", dlm->name);
  234. name = past->name;
  235. locklen = past->namelen;
  236. cookie = be64_to_cpu(past->cookie);
  237. flags = be32_to_cpu(past->flags);
  238. if (locklen > DLM_LOCKID_NAME_MAX) {
  239. ret = DLM_IVBUFLEN;
  240. mlog(ML_ERROR, "Invalid name length in proxy ast handler!\n");
  241. goto leave;
  242. }
  243. if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
  244. (LKM_PUT_LVB|LKM_GET_LVB)) {
  245. mlog(ML_ERROR, "both PUT and GET lvb specified\n");
  246. ret = DLM_BADARGS;
  247. goto leave;
  248. }
  249. mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
  250. (flags & LKM_GET_LVB ? "get lvb" : "none"));
  251. mlog(0, "type=%d, blocked_type=%d\n", past->type, past->blocked_type);
  252. if (past->type != DLM_AST &&
  253. past->type != DLM_BAST) {
  254. mlog(ML_ERROR, "Unknown ast type! %d, cookie=%u:%llu"
  255. "name=%.*s\n", past->type,
  256. dlm_get_lock_cookie_node(cookie),
  257. dlm_get_lock_cookie_seq(cookie),
  258. locklen, name);
  259. ret = DLM_IVLOCKID;
  260. goto leave;
  261. }
  262. res = dlm_lookup_lockres(dlm, name, locklen);
  263. if (!res) {
  264. mlog(ML_ERROR, "got %sast for unknown lockres! "
  265. "cookie=%u:%llu, name=%.*s, namelen=%u\n",
  266. past->type == DLM_AST ? "" : "b",
  267. dlm_get_lock_cookie_node(cookie),
  268. dlm_get_lock_cookie_seq(cookie),
  269. locklen, name, locklen);
  270. ret = DLM_IVLOCKID;
  271. goto leave;
  272. }
  273. /* cannot get a proxy ast message if this node owns it */
  274. BUG_ON(res->owner == dlm->node_num);
  275. mlog(0, "lockres %.*s\n", res->lockname.len, res->lockname.name);
  276. spin_lock(&res->spinlock);
  277. if (res->state & DLM_LOCK_RES_RECOVERING) {
  278. mlog(0, "responding with DLM_RECOVERING!\n");
  279. ret = DLM_RECOVERING;
  280. goto unlock_out;
  281. }
  282. if (res->state & DLM_LOCK_RES_MIGRATING) {
  283. mlog(0, "responding with DLM_MIGRATING!\n");
  284. ret = DLM_MIGRATING;
  285. goto unlock_out;
  286. }
  287. /* try convert queue for both ast/bast */
  288. head = &res->converting;
  289. lock = NULL;
  290. list_for_each(iter, head) {
  291. lock = list_entry (iter, struct dlm_lock, list);
  292. if (be64_to_cpu(lock->ml.cookie) == cookie)
  293. goto do_ast;
  294. }
  295. /* if not on convert, try blocked for ast, granted for bast */
  296. if (past->type == DLM_AST)
  297. head = &res->blocked;
  298. else
  299. head = &res->granted;
  300. list_for_each(iter, head) {
  301. lock = list_entry (iter, struct dlm_lock, list);
  302. if (be64_to_cpu(lock->ml.cookie) == cookie)
  303. goto do_ast;
  304. }
  305. mlog(ML_ERROR, "got %sast for unknown lock! cookie=%u:%llu, "
  306. "name=%.*s, namelen=%u\n",
  307. past->type == DLM_AST ? "" : "b",
  308. dlm_get_lock_cookie_node(cookie),
  309. dlm_get_lock_cookie_seq(cookie),
  310. locklen, name, locklen);
  311. ret = DLM_NORMAL;
  312. unlock_out:
  313. spin_unlock(&res->spinlock);
  314. goto leave;
  315. do_ast:
  316. ret = DLM_NORMAL;
  317. if (past->type == DLM_AST) {
  318. /* do not alter lock refcount. switching lists. */
  319. list_del_init(&lock->list);
  320. list_add_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) {
  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. }