dlmconvert.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmconvert.c
  5. *
  6. * underlying calls for lock conversion
  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. #include "dlmconvert.h"
  43. #define MLOG_MASK_PREFIX ML_DLM
  44. #include "cluster/masklog.h"
  45. /* NOTE: __dlmconvert_master is the only function in here that
  46. * needs a spinlock held on entry (res->spinlock) and it is the
  47. * only one that holds a lock on exit (res->spinlock).
  48. * All other functions in here need no locks and drop all of
  49. * the locks that they acquire. */
  50. static enum dlm_status __dlmconvert_master(struct dlm_ctxt *dlm,
  51. struct dlm_lock_resource *res,
  52. struct dlm_lock *lock, int flags,
  53. int type, int *call_ast,
  54. int *kick_thread);
  55. static enum dlm_status dlm_send_remote_convert_request(struct dlm_ctxt *dlm,
  56. struct dlm_lock_resource *res,
  57. struct dlm_lock *lock, int flags, int type);
  58. /*
  59. * this is only called directly by dlmlock(), and only when the
  60. * local node is the owner of the lockres
  61. * locking:
  62. * caller needs: none
  63. * taken: takes and drops res->spinlock
  64. * held on exit: none
  65. * returns: see __dlmconvert_master
  66. */
  67. enum dlm_status dlmconvert_master(struct dlm_ctxt *dlm,
  68. struct dlm_lock_resource *res,
  69. struct dlm_lock *lock, int flags, int type)
  70. {
  71. int call_ast = 0, kick_thread = 0;
  72. enum dlm_status status;
  73. spin_lock(&res->spinlock);
  74. /* we are not in a network handler, this is fine */
  75. __dlm_wait_on_lockres(res);
  76. __dlm_lockres_reserve_ast(res);
  77. res->state |= DLM_LOCK_RES_IN_PROGRESS;
  78. status = __dlmconvert_master(dlm, res, lock, flags, type,
  79. &call_ast, &kick_thread);
  80. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  81. spin_unlock(&res->spinlock);
  82. wake_up(&res->wq);
  83. if (status != DLM_NORMAL && status != DLM_NOTQUEUED)
  84. dlm_error(status);
  85. /* either queue the ast or release it */
  86. if (call_ast)
  87. dlm_queue_ast(dlm, lock);
  88. else
  89. dlm_lockres_release_ast(dlm, res);
  90. if (kick_thread)
  91. dlm_kick_thread(dlm, res);
  92. return status;
  93. }
  94. /* performs lock conversion at the lockres master site
  95. * locking:
  96. * caller needs: res->spinlock
  97. * taken: takes and drops lock->spinlock
  98. * held on exit: res->spinlock
  99. * returns: DLM_NORMAL, DLM_NOTQUEUED, DLM_DENIED
  100. * call_ast: whether ast should be called for this lock
  101. * kick_thread: whether dlm_kick_thread should be called
  102. */
  103. static enum dlm_status __dlmconvert_master(struct dlm_ctxt *dlm,
  104. struct dlm_lock_resource *res,
  105. struct dlm_lock *lock, int flags,
  106. int type, int *call_ast,
  107. int *kick_thread)
  108. {
  109. enum dlm_status status = DLM_NORMAL;
  110. struct dlm_lock *tmplock=NULL;
  111. assert_spin_locked(&res->spinlock);
  112. mlog(0, "type=%d, convert_type=%d, new convert_type=%d\n",
  113. lock->ml.type, lock->ml.convert_type, type);
  114. spin_lock(&lock->spinlock);
  115. /* already converting? */
  116. if (lock->ml.convert_type != LKM_IVMODE) {
  117. mlog(ML_ERROR, "attempted to convert a lock with a lock "
  118. "conversion pending\n");
  119. status = DLM_DENIED;
  120. goto unlock_exit;
  121. }
  122. /* must be on grant queue to convert */
  123. if (!dlm_lock_on_list(&res->granted, lock)) {
  124. mlog(ML_ERROR, "attempted to convert a lock not on grant "
  125. "queue\n");
  126. status = DLM_DENIED;
  127. goto unlock_exit;
  128. }
  129. if (flags & LKM_VALBLK) {
  130. switch (lock->ml.type) {
  131. case LKM_EXMODE:
  132. /* EX + LKM_VALBLK + convert == set lvb */
  133. mlog(0, "will set lvb: converting %s->%s\n",
  134. dlm_lock_mode_name(lock->ml.type),
  135. dlm_lock_mode_name(type));
  136. lock->lksb->flags |= DLM_LKSB_PUT_LVB;
  137. break;
  138. case LKM_PRMODE:
  139. case LKM_NLMODE:
  140. /* refetch if new level is not NL */
  141. if (type > LKM_NLMODE) {
  142. mlog(0, "will fetch new value into "
  143. "lvb: converting %s->%s\n",
  144. dlm_lock_mode_name(lock->ml.type),
  145. dlm_lock_mode_name(type));
  146. lock->lksb->flags |= DLM_LKSB_GET_LVB;
  147. } else {
  148. mlog(0, "will NOT fetch new value "
  149. "into lvb: converting %s->%s\n",
  150. dlm_lock_mode_name(lock->ml.type),
  151. dlm_lock_mode_name(type));
  152. flags &= ~(LKM_VALBLK);
  153. }
  154. break;
  155. }
  156. }
  157. /* in-place downconvert? */
  158. if (type <= lock->ml.type)
  159. goto grant;
  160. /* upconvert from here on */
  161. status = DLM_NORMAL;
  162. list_for_each_entry(tmplock, &res->granted, list) {
  163. if (tmplock == lock)
  164. continue;
  165. if (!dlm_lock_compatible(tmplock->ml.type, type))
  166. goto switch_queues;
  167. }
  168. list_for_each_entry(tmplock, &res->converting, list) {
  169. if (!dlm_lock_compatible(tmplock->ml.type, type))
  170. goto switch_queues;
  171. /* existing conversion requests take precedence */
  172. if (!dlm_lock_compatible(tmplock->ml.convert_type, type))
  173. goto switch_queues;
  174. }
  175. /* fall thru to grant */
  176. grant:
  177. mlog(0, "res %.*s, granting %s lock\n", res->lockname.len,
  178. res->lockname.name, dlm_lock_mode_name(type));
  179. /* immediately grant the new lock type */
  180. lock->lksb->status = DLM_NORMAL;
  181. if (lock->ml.node == dlm->node_num)
  182. mlog(0, "doing in-place convert for nonlocal lock\n");
  183. lock->ml.type = type;
  184. if (lock->lksb->flags & DLM_LKSB_PUT_LVB)
  185. memcpy(res->lvb, lock->lksb->lvb, DLM_LVB_LEN);
  186. status = DLM_NORMAL;
  187. *call_ast = 1;
  188. goto unlock_exit;
  189. switch_queues:
  190. if (flags & LKM_NOQUEUE) {
  191. mlog(0, "failed to convert NOQUEUE lock %.*s from "
  192. "%d to %d...\n", res->lockname.len, res->lockname.name,
  193. lock->ml.type, type);
  194. status = DLM_NOTQUEUED;
  195. goto unlock_exit;
  196. }
  197. mlog(0, "res %.*s, queueing...\n", res->lockname.len,
  198. res->lockname.name);
  199. lock->ml.convert_type = type;
  200. /* do not alter lock refcount. switching lists. */
  201. list_move_tail(&lock->list, &res->converting);
  202. unlock_exit:
  203. spin_unlock(&lock->spinlock);
  204. if (status == DLM_DENIED) {
  205. __dlm_print_one_lock_resource(res);
  206. }
  207. if (status == DLM_NORMAL)
  208. *kick_thread = 1;
  209. return status;
  210. }
  211. void dlm_revert_pending_convert(struct dlm_lock_resource *res,
  212. struct dlm_lock *lock)
  213. {
  214. /* do not alter lock refcount. switching lists. */
  215. list_move_tail(&lock->list, &res->granted);
  216. lock->ml.convert_type = LKM_IVMODE;
  217. lock->lksb->flags &= ~(DLM_LKSB_GET_LVB|DLM_LKSB_PUT_LVB);
  218. }
  219. /* messages the master site to do lock conversion
  220. * locking:
  221. * caller needs: none
  222. * taken: takes and drops res->spinlock, uses DLM_LOCK_RES_IN_PROGRESS
  223. * held on exit: none
  224. * returns: DLM_NORMAL, DLM_RECOVERING, status from remote node
  225. */
  226. enum dlm_status dlmconvert_remote(struct dlm_ctxt *dlm,
  227. struct dlm_lock_resource *res,
  228. struct dlm_lock *lock, int flags, int type)
  229. {
  230. enum dlm_status status;
  231. mlog(0, "type=%d, convert_type=%d, busy=%d\n", lock->ml.type,
  232. lock->ml.convert_type, res->state & DLM_LOCK_RES_IN_PROGRESS);
  233. spin_lock(&res->spinlock);
  234. if (res->state & DLM_LOCK_RES_RECOVERING) {
  235. mlog(0, "bailing out early since res is RECOVERING "
  236. "on secondary queue\n");
  237. /* __dlm_print_one_lock_resource(res); */
  238. status = DLM_RECOVERING;
  239. goto bail;
  240. }
  241. /* will exit this call with spinlock held */
  242. __dlm_wait_on_lockres(res);
  243. if (lock->ml.convert_type != LKM_IVMODE) {
  244. __dlm_print_one_lock_resource(res);
  245. mlog(ML_ERROR, "converting a remote lock that is already "
  246. "converting! (cookie=%u:%llu, conv=%d)\n",
  247. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  248. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
  249. lock->ml.convert_type);
  250. status = DLM_DENIED;
  251. goto bail;
  252. }
  253. res->state |= DLM_LOCK_RES_IN_PROGRESS;
  254. /* move lock to local convert queue */
  255. /* do not alter lock refcount. switching lists. */
  256. list_move_tail(&lock->list, &res->converting);
  257. lock->convert_pending = 1;
  258. lock->ml.convert_type = type;
  259. if (flags & LKM_VALBLK) {
  260. if (lock->ml.type == LKM_EXMODE) {
  261. flags |= LKM_PUT_LVB;
  262. lock->lksb->flags |= DLM_LKSB_PUT_LVB;
  263. } else {
  264. if (lock->ml.convert_type == LKM_NLMODE)
  265. flags &= ~LKM_VALBLK;
  266. else {
  267. flags |= LKM_GET_LVB;
  268. lock->lksb->flags |= DLM_LKSB_GET_LVB;
  269. }
  270. }
  271. }
  272. spin_unlock(&res->spinlock);
  273. /* no locks held here.
  274. * need to wait for a reply as to whether it got queued or not. */
  275. status = dlm_send_remote_convert_request(dlm, res, lock, flags, type);
  276. spin_lock(&res->spinlock);
  277. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  278. lock->convert_pending = 0;
  279. /* if it failed, move it back to granted queue */
  280. if (status != DLM_NORMAL) {
  281. if (status != DLM_NOTQUEUED)
  282. dlm_error(status);
  283. dlm_revert_pending_convert(res, lock);
  284. }
  285. bail:
  286. spin_unlock(&res->spinlock);
  287. /* TODO: should this be a wake_one? */
  288. /* wake up any IN_PROGRESS waiters */
  289. wake_up(&res->wq);
  290. return status;
  291. }
  292. /* sends DLM_CONVERT_LOCK_MSG to master site
  293. * locking:
  294. * caller needs: none
  295. * taken: none
  296. * held on exit: none
  297. * returns: DLM_NOLOCKMGR, status from remote node
  298. */
  299. static enum dlm_status dlm_send_remote_convert_request(struct dlm_ctxt *dlm,
  300. struct dlm_lock_resource *res,
  301. struct dlm_lock *lock, int flags, int type)
  302. {
  303. struct dlm_convert_lock convert;
  304. int tmpret;
  305. enum dlm_status ret;
  306. int status = 0;
  307. struct kvec vec[2];
  308. size_t veclen = 1;
  309. mlog(0, "%.*s\n", res->lockname.len, res->lockname.name);
  310. memset(&convert, 0, sizeof(struct dlm_convert_lock));
  311. convert.node_idx = dlm->node_num;
  312. convert.requested_type = type;
  313. convert.cookie = lock->ml.cookie;
  314. convert.namelen = res->lockname.len;
  315. convert.flags = cpu_to_be32(flags);
  316. memcpy(convert.name, res->lockname.name, convert.namelen);
  317. vec[0].iov_len = sizeof(struct dlm_convert_lock);
  318. vec[0].iov_base = &convert;
  319. if (flags & LKM_PUT_LVB) {
  320. /* extra data to send if we are updating lvb */
  321. vec[1].iov_len = DLM_LVB_LEN;
  322. vec[1].iov_base = lock->lksb->lvb;
  323. veclen++;
  324. }
  325. tmpret = o2net_send_message_vec(DLM_CONVERT_LOCK_MSG, dlm->key,
  326. vec, veclen, res->owner, &status);
  327. if (tmpret >= 0) {
  328. // successfully sent and received
  329. ret = status; // this is already a dlm_status
  330. if (ret == DLM_RECOVERING) {
  331. mlog(0, "node %u returned DLM_RECOVERING from convert "
  332. "message!\n", res->owner);
  333. } else if (ret == DLM_MIGRATING) {
  334. mlog(0, "node %u returned DLM_MIGRATING from convert "
  335. "message!\n", res->owner);
  336. } else if (ret == DLM_FORWARD) {
  337. mlog(0, "node %u returned DLM_FORWARD from convert "
  338. "message!\n", res->owner);
  339. } else if (ret != DLM_NORMAL && ret != DLM_NOTQUEUED)
  340. dlm_error(ret);
  341. } else {
  342. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  343. "node %u\n", tmpret, DLM_CONVERT_LOCK_MSG, dlm->key,
  344. res->owner);
  345. if (dlm_is_host_down(tmpret)) {
  346. /* instead of logging the same network error over
  347. * and over, sleep here and wait for the heartbeat
  348. * to notice the node is dead. times out after 5s. */
  349. dlm_wait_for_node_death(dlm, res->owner,
  350. DLM_NODE_DEATH_WAIT_MAX);
  351. ret = DLM_RECOVERING;
  352. mlog(0, "node %u died so returning DLM_RECOVERING "
  353. "from convert message!\n", res->owner);
  354. } else {
  355. ret = dlm_err_to_dlm_status(tmpret);
  356. }
  357. }
  358. return ret;
  359. }
  360. /* handler for DLM_CONVERT_LOCK_MSG on master site
  361. * locking:
  362. * caller needs: none
  363. * taken: takes and drop res->spinlock
  364. * held on exit: none
  365. * returns: DLM_NORMAL, DLM_IVLOCKID, DLM_BADARGS,
  366. * status from __dlmconvert_master
  367. */
  368. int dlm_convert_lock_handler(struct o2net_msg *msg, u32 len, void *data,
  369. void **ret_data)
  370. {
  371. struct dlm_ctxt *dlm = data;
  372. struct dlm_convert_lock *cnv = (struct dlm_convert_lock *)msg->buf;
  373. struct dlm_lock_resource *res = NULL;
  374. struct dlm_lock *lock = NULL;
  375. struct dlm_lock *tmp_lock;
  376. struct dlm_lockstatus *lksb;
  377. enum dlm_status status = DLM_NORMAL;
  378. u32 flags;
  379. int call_ast = 0, kick_thread = 0, ast_reserved = 0, wake = 0;
  380. if (!dlm_grab(dlm)) {
  381. dlm_error(DLM_REJECTED);
  382. return DLM_REJECTED;
  383. }
  384. mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
  385. "Domain %s not fully joined!\n", dlm->name);
  386. if (cnv->namelen > DLM_LOCKID_NAME_MAX) {
  387. status = DLM_IVBUFLEN;
  388. dlm_error(status);
  389. goto leave;
  390. }
  391. flags = be32_to_cpu(cnv->flags);
  392. if ((flags & (LKM_PUT_LVB|LKM_GET_LVB)) ==
  393. (LKM_PUT_LVB|LKM_GET_LVB)) {
  394. mlog(ML_ERROR, "both PUT and GET lvb specified\n");
  395. status = DLM_BADARGS;
  396. goto leave;
  397. }
  398. mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" :
  399. (flags & LKM_GET_LVB ? "get lvb" : "none"));
  400. status = DLM_IVLOCKID;
  401. res = dlm_lookup_lockres(dlm, cnv->name, cnv->namelen);
  402. if (!res) {
  403. dlm_error(status);
  404. goto leave;
  405. }
  406. spin_lock(&res->spinlock);
  407. status = __dlm_lockres_state_to_status(res);
  408. if (status != DLM_NORMAL) {
  409. spin_unlock(&res->spinlock);
  410. dlm_error(status);
  411. goto leave;
  412. }
  413. list_for_each_entry(tmp_lock, &res->granted, list) {
  414. if (tmp_lock->ml.cookie == cnv->cookie &&
  415. tmp_lock->ml.node == cnv->node_idx) {
  416. lock = tmp_lock;
  417. dlm_lock_get(lock);
  418. break;
  419. }
  420. }
  421. spin_unlock(&res->spinlock);
  422. if (!lock) {
  423. status = DLM_IVLOCKID;
  424. mlog(ML_ERROR, "did not find lock to convert on grant queue! "
  425. "cookie=%u:%llu\n",
  426. dlm_get_lock_cookie_node(be64_to_cpu(cnv->cookie)),
  427. dlm_get_lock_cookie_seq(be64_to_cpu(cnv->cookie)));
  428. dlm_print_one_lock_resource(res);
  429. goto leave;
  430. }
  431. /* found the lock */
  432. lksb = lock->lksb;
  433. /* see if caller needed to get/put lvb */
  434. if (flags & LKM_PUT_LVB) {
  435. BUG_ON(lksb->flags & (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
  436. lksb->flags |= DLM_LKSB_PUT_LVB;
  437. memcpy(&lksb->lvb[0], &cnv->lvb[0], DLM_LVB_LEN);
  438. } else if (flags & LKM_GET_LVB) {
  439. BUG_ON(lksb->flags & (DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB));
  440. lksb->flags |= DLM_LKSB_GET_LVB;
  441. }
  442. spin_lock(&res->spinlock);
  443. status = __dlm_lockres_state_to_status(res);
  444. if (status == DLM_NORMAL) {
  445. __dlm_lockres_reserve_ast(res);
  446. ast_reserved = 1;
  447. res->state |= DLM_LOCK_RES_IN_PROGRESS;
  448. status = __dlmconvert_master(dlm, res, lock, flags,
  449. cnv->requested_type,
  450. &call_ast, &kick_thread);
  451. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  452. wake = 1;
  453. }
  454. spin_unlock(&res->spinlock);
  455. if (wake)
  456. wake_up(&res->wq);
  457. if (status != DLM_NORMAL) {
  458. if (status != DLM_NOTQUEUED)
  459. dlm_error(status);
  460. lksb->flags &= ~(DLM_LKSB_GET_LVB|DLM_LKSB_PUT_LVB);
  461. }
  462. leave:
  463. if (lock)
  464. dlm_lock_put(lock);
  465. /* either queue the ast or release it, if reserved */
  466. if (call_ast)
  467. dlm_queue_ast(dlm, lock);
  468. else if (ast_reserved)
  469. dlm_lockres_release_ast(dlm, res);
  470. if (kick_thread)
  471. dlm_kick_thread(dlm, res);
  472. if (res)
  473. dlm_lockres_put(res);
  474. dlm_put(dlm);
  475. return status;
  476. }