dlmconvert.c 15 KB

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