PageRenderTime 24ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ocfs2/dlm/dlmast.c

https://github.com/dmitriy103/bravo_kernel-2.6.35
C | 474 lines | 330 code | 76 blank | 68 comment | 63 complexity | 50d91fe551fd928177cb1b66e86e2274 MD5 | raw file
  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/highmem.h>
  30. #include <linux/init.h>
  31. #include <linux/sysctl.h>
  32. #include <linux/random.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/socket.h>
  35. #include <linux/inet.h>
  36. #include <linux/spinlock.h>
  37. #include "cluster/heartbeat.h"
  38. #include "cluster/nodemanager.h"
  39. #include "cluster/tcp.h"
  40. #include "dlmapi.h"
  41. #include "dlmcommon.h"
  42. #define MLOG_MASK_PREFIX ML_DLM
  43. #include "cluster/masklog.h"
  44. static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  45. struct dlm_lock *lock);
  46. static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock);
  47. /* Should be called as an ast gets queued to see if the new
  48. * lock level will obsolete a pending bast.
  49. * For example, if dlm_thread queued a bast for an EX lock that
  50. * was blocking another EX, but before sending the bast the
  51. * lock owner downconverted to NL, the bast is now obsolete.
  52. * Only the ast should be sent.
  53. * This is needed because the lock and convert paths can queue
  54. * asts out-of-band (not waiting for dlm_thread) in order to
  55. * allow for LKM_NOQUEUE to get immediate responses. */
  56. static int dlm_should_cancel_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  57. {
  58. assert_spin_locked(&dlm->ast_lock);
  59. assert_spin_locked(&lock->spinlock);
  60. if (lock->ml.highest_blocked == LKM_IVMODE)
  61. return 0;
  62. BUG_ON(lock->ml.highest_blocked == LKM_NLMODE);
  63. if (lock->bast_pending &&
  64. list_empty(&lock->bast_list))
  65. /* old bast already sent, ok */
  66. return 0;
  67. if (lock->ml.type == LKM_EXMODE)
  68. /* EX blocks anything left, any bast still valid */
  69. return 0;
  70. else if (lock->ml.type == LKM_NLMODE)
  71. /* NL blocks nothing, no reason to send any bast, cancel it */
  72. return 1;
  73. else if (lock->ml.highest_blocked != LKM_EXMODE)
  74. /* PR only blocks EX */
  75. return 1;
  76. return 0;
  77. }
  78. void __dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  79. {
  80. mlog_entry_void();
  81. BUG_ON(!dlm);
  82. BUG_ON(!lock);
  83. assert_spin_locked(&dlm->ast_lock);
  84. if (!list_empty(&lock->ast_list)) {
  85. mlog(ML_ERROR, "ast list not empty!! pending=%d, newlevel=%d\n",
  86. lock->ast_pending, lock->ml.type);
  87. BUG();
  88. }
  89. if (lock->ast_pending)
  90. mlog(0, "lock has an ast getting flushed right now\n");
  91. /* putting lock on list, add a ref */
  92. dlm_lock_get(lock);
  93. spin_lock(&lock->spinlock);
  94. /* check to see if this ast obsoletes the bast */
  95. if (dlm_should_cancel_bast(dlm, lock)) {
  96. struct dlm_lock_resource *res = lock->lockres;
  97. mlog(0, "%s: cancelling bast for %.*s\n",
  98. dlm->name, res->lockname.len, res->lockname.name);
  99. lock->bast_pending = 0;
  100. list_del_init(&lock->bast_list);
  101. lock->ml.highest_blocked = LKM_IVMODE;
  102. /* removing lock from list, remove a ref. guaranteed
  103. * this won't be the last ref because of the get above,
  104. * so res->spinlock will not be taken here */
  105. dlm_lock_put(lock);
  106. /* free up the reserved bast that we are cancelling.
  107. * guaranteed that this will not be the last reserved
  108. * ast because *both* an ast and a bast were reserved
  109. * to get to this point. the res->spinlock will not be
  110. * taken here */
  111. dlm_lockres_release_ast(dlm, res);
  112. }
  113. list_add_tail(&lock->ast_list, &dlm->pending_asts);
  114. lock->ast_pending = 1;
  115. spin_unlock(&lock->spinlock);
  116. }
  117. void dlm_queue_ast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  118. {
  119. mlog_entry_void();
  120. BUG_ON(!dlm);
  121. BUG_ON(!lock);
  122. spin_lock(&dlm->ast_lock);
  123. __dlm_queue_ast(dlm, lock);
  124. spin_unlock(&dlm->ast_lock);
  125. }
  126. void __dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  127. {
  128. mlog_entry_void();
  129. BUG_ON(!dlm);
  130. BUG_ON(!lock);
  131. assert_spin_locked(&dlm->ast_lock);
  132. BUG_ON(!list_empty(&lock->bast_list));
  133. if (lock->bast_pending)
  134. mlog(0, "lock has a bast getting flushed right now\n");
  135. /* putting lock on list, add a ref */
  136. dlm_lock_get(lock);
  137. spin_lock(&lock->spinlock);
  138. list_add_tail(&lock->bast_list, &dlm->pending_basts);
  139. lock->bast_pending = 1;
  140. spin_unlock(&lock->spinlock);
  141. }
  142. void dlm_queue_bast(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  143. {
  144. mlog_entry_void();
  145. BUG_ON(!dlm);
  146. BUG_ON(!lock);
  147. spin_lock(&dlm->ast_lock);
  148. __dlm_queue_bast(dlm, lock);
  149. spin_unlock(&dlm->ast_lock);
  150. }
  151. static void dlm_update_lvb(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  152. struct dlm_lock *lock)
  153. {
  154. struct dlm_lockstatus *lksb = lock->lksb;
  155. BUG_ON(!lksb);
  156. /* only updates if this node masters the lockres */
  157. spin_lock(&res->spinlock);
  158. if (res->owner == dlm->node_num) {
  159. /* check the lksb flags for the direction */
  160. if (lksb->flags & DLM_LKSB_GET_LVB) {
  161. mlog(0, "getting lvb from lockres for %s node\n",
  162. lock->ml.node == dlm->node_num ? "master" :
  163. "remote");
  164. memcpy(lksb->lvb, res->lvb, DLM_LVB_LEN);
  165. }
  166. /* Do nothing for lvb put requests - they should be done in
  167. * place when the lock is downconverted - otherwise we risk
  168. * racing gets and puts which could result in old lvb data
  169. * being propagated. We leave the put flag set and clear it
  170. * here. In the future we might want to clear it at the time
  171. * the put is actually done.
  172. */
  173. }
  174. spin_unlock(&res->spinlock);
  175. /* reset any lvb flags on the lksb */
  176. lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
  177. }
  178. void dlm_do_local_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  179. struct dlm_lock *lock)
  180. {
  181. dlm_astlockfunc_t *fn;
  182. struct dlm_lockstatus *lksb;
  183. mlog_entry_void();
  184. lksb = lock->lksb;
  185. fn = lock->ast;
  186. BUG_ON(lock->ml.node != dlm->node_num);
  187. dlm_update_lvb(dlm, res, lock);
  188. (*fn)(lock->astdata);
  189. }
  190. int dlm_do_remote_ast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  191. struct dlm_lock *lock)
  192. {
  193. int ret;
  194. struct dlm_lockstatus *lksb;
  195. int lksbflags;
  196. mlog_entry_void();
  197. lksb = lock->lksb;
  198. BUG_ON(lock->ml.node == dlm->node_num);
  199. lksbflags = lksb->flags;
  200. dlm_update_lvb(dlm, res, lock);
  201. /* lock request came from another node
  202. * go do the ast over there */
  203. ret = dlm_send_proxy_ast(dlm, res, lock, lksbflags);
  204. return ret;
  205. }
  206. void dlm_do_local_bast(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  207. struct dlm_lock *lock, int blocked_type)
  208. {
  209. dlm_bastlockfunc_t *fn = lock->bast;
  210. mlog_entry_void();
  211. BUG_ON(lock->ml.node != dlm->node_num);
  212. (*fn)(lock->astdata, blocked_type);
  213. }
  214. int dlm_proxy_ast_handler(struct o2net_msg *msg, u32 len, void *data,
  215. void **ret_data)
  216. {
  217. int ret;
  218. unsigned int locklen;
  219. struct dlm_ctxt *dlm = data;
  220. struct dlm_lock_resource *res = NULL;
  221. struct dlm_lock *lock = NULL;
  222. struct dlm_proxy_ast *past = (struct dlm_proxy_ast *) msg->buf;
  223. char *name;
  224. struct list_head *iter, *head=NULL;
  225. u64 cookie;
  226. u32 flags;
  227. u8 node;
  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 = past->cookie;
  237. flags = be32_to_cpu(past->flags);
  238. node = past->node_idx;
  239. if (locklen > DLM_LOCKID_NAME_MAX) {
  240. ret = DLM_IVBUFLEN;
  241. mlog(ML_ERROR, "Invalid name length (%d) in proxy ast "
  242. "handler!\n", locklen);
  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, (0x%x)\n",
  248. flags);
  249. ret = DLM_BADARGS;
  250. goto leave;
  251. }
  252. mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
  253. (flags & LKM_GET_LVB ? "get lvb" : "none"));
  254. mlog(0, "type=%d, blocked_type=%d\n", past->type, past->blocked_type);
  255. if (past->type != DLM_AST &&
  256. past->type != DLM_BAST) {
  257. mlog(ML_ERROR, "Unknown ast type! %d, cookie=%u:%llu"
  258. "name=%.*s, node=%u\n", past->type,
  259. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  260. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  261. locklen, name, node);
  262. ret = DLM_IVLOCKID;
  263. goto leave;
  264. }
  265. res = dlm_lookup_lockres(dlm, name, locklen);
  266. if (!res) {
  267. mlog(0, "Got %sast for unknown lockres! cookie=%u:%llu, "
  268. "name=%.*s, node=%u\n", (past->type == DLM_AST ? "" : "b"),
  269. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  270. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  271. locklen, name, node);
  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 (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 (lock->ml.cookie == cookie)
  305. goto do_ast;
  306. }
  307. mlog(0, "Got %sast for unknown lock! cookie=%u:%llu, name=%.*s, "
  308. "node=%u\n", past->type == DLM_AST ? "" : "b",
  309. dlm_get_lock_cookie_node(be64_to_cpu(cookie)),
  310. dlm_get_lock_cookie_seq(be64_to_cpu(cookie)),
  311. locklen, name, node);
  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(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  379. "node %u\n", ret, DLM_PROXY_AST_MSG, dlm->key,
  380. lock->ml.node);
  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. }