dlmunlock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmunlock.c
  5. *
  6. * underlying calls for unlocking locks
  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 <linux/delay.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. #define MLOG_MASK_PREFIX ML_DLM
  44. #include "cluster/masklog.h"
  45. #define DLM_UNLOCK_FREE_LOCK 0x00000001
  46. #define DLM_UNLOCK_CALL_AST 0x00000002
  47. #define DLM_UNLOCK_REMOVE_LOCK 0x00000004
  48. #define DLM_UNLOCK_REGRANT_LOCK 0x00000008
  49. #define DLM_UNLOCK_CLEAR_CONVERT_TYPE 0x00000010
  50. static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
  51. struct dlm_lock_resource *res,
  52. struct dlm_lock *lock,
  53. struct dlm_lockstatus *lksb,
  54. int *actions);
  55. static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
  56. struct dlm_lock_resource *res,
  57. struct dlm_lock *lock,
  58. struct dlm_lockstatus *lksb,
  59. int *actions);
  60. static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
  61. struct dlm_lock_resource *res,
  62. struct dlm_lock *lock,
  63. struct dlm_lockstatus *lksb,
  64. int flags,
  65. u8 owner);
  66. /*
  67. * according to the spec:
  68. * http://opendlm.sourceforge.net/cvsmirror/opendlm/docs/dlmbook_final.pdf
  69. *
  70. * flags & LKM_CANCEL != 0: must be converting or blocked
  71. * flags & LKM_CANCEL == 0: must be granted
  72. *
  73. * So to unlock a converting lock, you must first cancel the
  74. * convert (passing LKM_CANCEL in flags), then call the unlock
  75. * again (with no LKM_CANCEL in flags).
  76. */
  77. /*
  78. * locking:
  79. * caller needs: none
  80. * taken: res->spinlock and lock->spinlock taken and dropped
  81. * held on exit: none
  82. * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
  83. * all callers should have taken an extra ref on lock coming in
  84. */
  85. static enum dlm_status dlmunlock_common(struct dlm_ctxt *dlm,
  86. struct dlm_lock_resource *res,
  87. struct dlm_lock *lock,
  88. struct dlm_lockstatus *lksb,
  89. int flags, int *call_ast,
  90. int master_node)
  91. {
  92. enum dlm_status status;
  93. int actions = 0;
  94. int in_use;
  95. u8 owner;
  96. mlog(0, "master_node = %d, valblk = %d\n", master_node,
  97. flags & LKM_VALBLK);
  98. if (master_node)
  99. BUG_ON(res->owner != dlm->node_num);
  100. else
  101. BUG_ON(res->owner == dlm->node_num);
  102. spin_lock(&dlm->ast_lock);
  103. /* We want to be sure that we're not freeing a lock
  104. * that still has AST's pending... */
  105. in_use = !list_empty(&lock->ast_list);
  106. spin_unlock(&dlm->ast_lock);
  107. if (in_use && !(flags & LKM_CANCEL)) {
  108. mlog(ML_ERROR, "lockres %.*s: Someone is calling dlmunlock "
  109. "while waiting for an ast!", res->lockname.len,
  110. res->lockname.name);
  111. return DLM_BADPARAM;
  112. }
  113. spin_lock(&res->spinlock);
  114. if (res->state & DLM_LOCK_RES_IN_PROGRESS) {
  115. if (master_node && !(flags & LKM_CANCEL)) {
  116. mlog(ML_ERROR, "lockres in progress!\n");
  117. spin_unlock(&res->spinlock);
  118. return DLM_FORWARD;
  119. }
  120. /* ok for this to sleep if not in a network handler */
  121. __dlm_wait_on_lockres(res);
  122. res->state |= DLM_LOCK_RES_IN_PROGRESS;
  123. }
  124. spin_lock(&lock->spinlock);
  125. if (res->state & DLM_LOCK_RES_RECOVERING) {
  126. status = DLM_RECOVERING;
  127. goto leave;
  128. }
  129. if (res->state & DLM_LOCK_RES_MIGRATING) {
  130. status = DLM_MIGRATING;
  131. goto leave;
  132. }
  133. /* see above for what the spec says about
  134. * LKM_CANCEL and the lock queue state */
  135. if (flags & LKM_CANCEL)
  136. status = dlm_get_cancel_actions(dlm, res, lock, lksb, &actions);
  137. else
  138. status = dlm_get_unlock_actions(dlm, res, lock, lksb, &actions);
  139. if (status != DLM_NORMAL && (status != DLM_CANCELGRANT || !master_node))
  140. goto leave;
  141. /* By now this has been masked out of cancel requests. */
  142. if (flags & LKM_VALBLK) {
  143. /* make the final update to the lvb */
  144. if (master_node)
  145. memcpy(res->lvb, lksb->lvb, DLM_LVB_LEN);
  146. else
  147. flags |= LKM_PUT_LVB; /* let the send function
  148. * handle it. */
  149. }
  150. if (!master_node) {
  151. owner = res->owner;
  152. /* drop locks and send message */
  153. if (flags & LKM_CANCEL)
  154. lock->cancel_pending = 1;
  155. else
  156. lock->unlock_pending = 1;
  157. spin_unlock(&lock->spinlock);
  158. spin_unlock(&res->spinlock);
  159. status = dlm_send_remote_unlock_request(dlm, res, lock, lksb,
  160. flags, owner);
  161. spin_lock(&res->spinlock);
  162. spin_lock(&lock->spinlock);
  163. /* if the master told us the lock was already granted,
  164. * let the ast handle all of these actions */
  165. if (status == DLM_CANCELGRANT) {
  166. actions &= ~(DLM_UNLOCK_REMOVE_LOCK|
  167. DLM_UNLOCK_REGRANT_LOCK|
  168. DLM_UNLOCK_CLEAR_CONVERT_TYPE);
  169. } else if (status == DLM_RECOVERING ||
  170. status == DLM_MIGRATING ||
  171. status == DLM_FORWARD ||
  172. status == DLM_NOLOCKMGR
  173. ) {
  174. /* must clear the actions because this unlock
  175. * is about to be retried. cannot free or do
  176. * any list manipulation. */
  177. mlog(0, "%s:%.*s: clearing actions, %s\n",
  178. dlm->name, res->lockname.len,
  179. res->lockname.name,
  180. status==DLM_RECOVERING?"recovering":
  181. (status==DLM_MIGRATING?"migrating":
  182. (status == DLM_FORWARD ? "forward" :
  183. "nolockmanager")));
  184. actions = 0;
  185. }
  186. if (flags & LKM_CANCEL)
  187. lock->cancel_pending = 0;
  188. else
  189. lock->unlock_pending = 0;
  190. }
  191. /* get an extra ref on lock. if we are just switching
  192. * lists here, we dont want the lock to go away. */
  193. dlm_lock_get(lock);
  194. if (actions & DLM_UNLOCK_REMOVE_LOCK) {
  195. list_del_init(&lock->list);
  196. dlm_lock_put(lock);
  197. }
  198. if (actions & DLM_UNLOCK_REGRANT_LOCK) {
  199. dlm_lock_get(lock);
  200. list_add_tail(&lock->list, &res->granted);
  201. }
  202. if (actions & DLM_UNLOCK_CLEAR_CONVERT_TYPE) {
  203. mlog(0, "clearing convert_type at %smaster node\n",
  204. master_node ? "" : "non-");
  205. lock->ml.convert_type = LKM_IVMODE;
  206. }
  207. /* remove the extra ref on lock */
  208. dlm_lock_put(lock);
  209. leave:
  210. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  211. if (!dlm_lock_on_list(&res->converting, lock))
  212. BUG_ON(lock->ml.convert_type != LKM_IVMODE);
  213. else
  214. BUG_ON(lock->ml.convert_type == LKM_IVMODE);
  215. spin_unlock(&lock->spinlock);
  216. spin_unlock(&res->spinlock);
  217. wake_up(&res->wq);
  218. /* let the caller's final dlm_lock_put handle the actual kfree */
  219. if (actions & DLM_UNLOCK_FREE_LOCK) {
  220. /* this should always be coupled with list removal */
  221. BUG_ON(!(actions & DLM_UNLOCK_REMOVE_LOCK));
  222. mlog(0, "lock %u:%llu should be gone now! refs=%d\n",
  223. dlm_get_lock_cookie_node(be64_to_cpu(lock->ml.cookie)),
  224. dlm_get_lock_cookie_seq(be64_to_cpu(lock->ml.cookie)),
  225. atomic_read(&lock->lock_refs.refcount)-1);
  226. dlm_lock_put(lock);
  227. }
  228. if (actions & DLM_UNLOCK_CALL_AST)
  229. *call_ast = 1;
  230. /* if cancel or unlock succeeded, lvb work is done */
  231. if (status == DLM_NORMAL)
  232. lksb->flags &= ~(DLM_LKSB_PUT_LVB|DLM_LKSB_GET_LVB);
  233. return status;
  234. }
  235. void dlm_commit_pending_unlock(struct dlm_lock_resource *res,
  236. struct dlm_lock *lock)
  237. {
  238. /* leave DLM_LKSB_PUT_LVB on the lksb so any final
  239. * update of the lvb will be sent to the new master */
  240. list_del_init(&lock->list);
  241. }
  242. void dlm_commit_pending_cancel(struct dlm_lock_resource *res,
  243. struct dlm_lock *lock)
  244. {
  245. list_move_tail(&lock->list, &res->granted);
  246. lock->ml.convert_type = LKM_IVMODE;
  247. }
  248. static inline enum dlm_status dlmunlock_master(struct dlm_ctxt *dlm,
  249. struct dlm_lock_resource *res,
  250. struct dlm_lock *lock,
  251. struct dlm_lockstatus *lksb,
  252. int flags,
  253. int *call_ast)
  254. {
  255. return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 1);
  256. }
  257. static inline enum dlm_status dlmunlock_remote(struct dlm_ctxt *dlm,
  258. struct dlm_lock_resource *res,
  259. struct dlm_lock *lock,
  260. struct dlm_lockstatus *lksb,
  261. int flags, int *call_ast)
  262. {
  263. return dlmunlock_common(dlm, res, lock, lksb, flags, call_ast, 0);
  264. }
  265. /*
  266. * locking:
  267. * caller needs: none
  268. * taken: none
  269. * held on exit: none
  270. * returns: DLM_NORMAL, DLM_NOLOCKMGR, status from network
  271. */
  272. static enum dlm_status dlm_send_remote_unlock_request(struct dlm_ctxt *dlm,
  273. struct dlm_lock_resource *res,
  274. struct dlm_lock *lock,
  275. struct dlm_lockstatus *lksb,
  276. int flags,
  277. u8 owner)
  278. {
  279. struct dlm_unlock_lock unlock;
  280. int tmpret;
  281. enum dlm_status ret;
  282. int status = 0;
  283. struct kvec vec[2];
  284. size_t veclen = 1;
  285. mlog(0, "%.*s\n", res->lockname.len, res->lockname.name);
  286. if (owner == dlm->node_num) {
  287. /* ended up trying to contact ourself. this means
  288. * that the lockres had been remote but became local
  289. * via a migration. just retry it, now as local */
  290. mlog(0, "%s:%.*s: this node became the master due to a "
  291. "migration, re-evaluate now\n", dlm->name,
  292. res->lockname.len, res->lockname.name);
  293. return DLM_FORWARD;
  294. }
  295. memset(&unlock, 0, sizeof(unlock));
  296. unlock.node_idx = dlm->node_num;
  297. unlock.flags = cpu_to_be32(flags);
  298. unlock.cookie = lock->ml.cookie;
  299. unlock.namelen = res->lockname.len;
  300. memcpy(unlock.name, res->lockname.name, unlock.namelen);
  301. vec[0].iov_len = sizeof(struct dlm_unlock_lock);
  302. vec[0].iov_base = &unlock;
  303. if (flags & LKM_PUT_LVB) {
  304. /* extra data to send if we are updating lvb */
  305. vec[1].iov_len = DLM_LVB_LEN;
  306. vec[1].iov_base = lock->lksb->lvb;
  307. veclen++;
  308. }
  309. tmpret = o2net_send_message_vec(DLM_UNLOCK_LOCK_MSG, dlm->key,
  310. vec, veclen, owner, &status);
  311. if (tmpret >= 0) {
  312. // successfully sent and received
  313. if (status == DLM_FORWARD)
  314. mlog(0, "master was in-progress. retry\n");
  315. ret = status;
  316. } else {
  317. mlog(ML_ERROR, "Error %d when sending message %u (key 0x%x) to "
  318. "node %u\n", tmpret, DLM_UNLOCK_LOCK_MSG, dlm->key, owner);
  319. if (dlm_is_host_down(tmpret)) {
  320. /* NOTE: this seems strange, but it is what we want.
  321. * when the master goes down during a cancel or
  322. * unlock, the recovery code completes the operation
  323. * as if the master had not died, then passes the
  324. * updated state to the recovery master. this thread
  325. * just needs to finish out the operation and call
  326. * the unlockast. */
  327. if (dlm_is_node_dead(dlm, owner))
  328. ret = DLM_NORMAL;
  329. else
  330. ret = DLM_NOLOCKMGR;
  331. } else {
  332. /* something bad. this will BUG in ocfs2 */
  333. ret = dlm_err_to_dlm_status(tmpret);
  334. }
  335. }
  336. return ret;
  337. }
  338. /*
  339. * locking:
  340. * caller needs: none
  341. * taken: takes and drops res->spinlock
  342. * held on exit: none
  343. * returns: DLM_NORMAL, DLM_BADARGS, DLM_IVLOCKID,
  344. * return value from dlmunlock_master
  345. */
  346. int dlm_unlock_lock_handler(struct o2net_msg *msg, u32 len, void *data,
  347. void **ret_data)
  348. {
  349. struct dlm_ctxt *dlm = data;
  350. struct dlm_unlock_lock *unlock = (struct dlm_unlock_lock *)msg->buf;
  351. struct dlm_lock_resource *res = NULL;
  352. struct dlm_lock *lock = NULL;
  353. enum dlm_status status = DLM_NORMAL;
  354. int found = 0, i;
  355. struct dlm_lockstatus *lksb = NULL;
  356. int ignore;
  357. u32 flags;
  358. struct list_head *queue;
  359. flags = be32_to_cpu(unlock->flags);
  360. if (flags & LKM_GET_LVB) {
  361. mlog(ML_ERROR, "bad args! GET_LVB specified on unlock!\n");
  362. return DLM_BADARGS;
  363. }
  364. if ((flags & (LKM_PUT_LVB|LKM_CANCEL)) == (LKM_PUT_LVB|LKM_CANCEL)) {
  365. mlog(ML_ERROR, "bad args! cannot modify lvb on a CANCEL "
  366. "request!\n");
  367. return DLM_BADARGS;
  368. }
  369. if (unlock->namelen > DLM_LOCKID_NAME_MAX) {
  370. mlog(ML_ERROR, "Invalid name length in unlock handler!\n");
  371. return DLM_IVBUFLEN;
  372. }
  373. if (!dlm_grab(dlm))
  374. return DLM_REJECTED;
  375. mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
  376. "Domain %s not fully joined!\n", dlm->name);
  377. mlog(0, "lvb: %s\n", flags & LKM_PUT_LVB ? "put lvb" : "none");
  378. res = dlm_lookup_lockres(dlm, unlock->name, unlock->namelen);
  379. if (!res) {
  380. /* We assume here that a no lock resource simply means
  381. * it was migrated away and destroyed before the other
  382. * node could detect it. */
  383. mlog(0, "returning DLM_FORWARD -- res no longer exists\n");
  384. status = DLM_FORWARD;
  385. goto not_found;
  386. }
  387. queue=&res->granted;
  388. found = 0;
  389. spin_lock(&res->spinlock);
  390. if (res->state & DLM_LOCK_RES_RECOVERING) {
  391. spin_unlock(&res->spinlock);
  392. mlog(0, "returning DLM_RECOVERING\n");
  393. status = DLM_RECOVERING;
  394. goto leave;
  395. }
  396. if (res->state & DLM_LOCK_RES_MIGRATING) {
  397. spin_unlock(&res->spinlock);
  398. mlog(0, "returning DLM_MIGRATING\n");
  399. status = DLM_MIGRATING;
  400. goto leave;
  401. }
  402. if (res->owner != dlm->node_num) {
  403. spin_unlock(&res->spinlock);
  404. mlog(0, "returning DLM_FORWARD -- not master\n");
  405. status = DLM_FORWARD;
  406. goto leave;
  407. }
  408. for (i=0; i<3; i++) {
  409. list_for_each_entry(lock, queue, list) {
  410. if (lock->ml.cookie == unlock->cookie &&
  411. lock->ml.node == unlock->node_idx) {
  412. dlm_lock_get(lock);
  413. found = 1;
  414. break;
  415. }
  416. }
  417. if (found)
  418. break;
  419. /* scan granted -> converting -> blocked queues */
  420. queue++;
  421. }
  422. spin_unlock(&res->spinlock);
  423. if (!found) {
  424. status = DLM_IVLOCKID;
  425. goto not_found;
  426. }
  427. /* lock was found on queue */
  428. lksb = lock->lksb;
  429. if (flags & (LKM_VALBLK|LKM_PUT_LVB) &&
  430. lock->ml.type != LKM_EXMODE)
  431. flags &= ~(LKM_VALBLK|LKM_PUT_LVB);
  432. /* unlockast only called on originating node */
  433. if (flags & LKM_PUT_LVB) {
  434. lksb->flags |= DLM_LKSB_PUT_LVB;
  435. memcpy(&lksb->lvb[0], &unlock->lvb[0], DLM_LVB_LEN);
  436. }
  437. /* if this is in-progress, propagate the DLM_FORWARD
  438. * all the way back out */
  439. status = dlmunlock_master(dlm, res, lock, lksb, flags, &ignore);
  440. if (status == DLM_FORWARD)
  441. mlog(0, "lockres is in progress\n");
  442. if (flags & LKM_PUT_LVB)
  443. lksb->flags &= ~DLM_LKSB_PUT_LVB;
  444. dlm_lockres_calc_usage(dlm, res);
  445. dlm_kick_thread(dlm, res);
  446. not_found:
  447. if (!found)
  448. mlog(ML_ERROR, "failed to find lock to unlock! "
  449. "cookie=%u:%llu\n",
  450. dlm_get_lock_cookie_node(be64_to_cpu(unlock->cookie)),
  451. dlm_get_lock_cookie_seq(be64_to_cpu(unlock->cookie)));
  452. else
  453. dlm_lock_put(lock);
  454. leave:
  455. if (res)
  456. dlm_lockres_put(res);
  457. dlm_put(dlm);
  458. return status;
  459. }
  460. static enum dlm_status dlm_get_cancel_actions(struct dlm_ctxt *dlm,
  461. struct dlm_lock_resource *res,
  462. struct dlm_lock *lock,
  463. struct dlm_lockstatus *lksb,
  464. int *actions)
  465. {
  466. enum dlm_status status;
  467. if (dlm_lock_on_list(&res->blocked, lock)) {
  468. /* cancel this outright */
  469. status = DLM_NORMAL;
  470. *actions = (DLM_UNLOCK_CALL_AST |
  471. DLM_UNLOCK_REMOVE_LOCK);
  472. } else if (dlm_lock_on_list(&res->converting, lock)) {
  473. /* cancel the request, put back on granted */
  474. status = DLM_NORMAL;
  475. *actions = (DLM_UNLOCK_CALL_AST |
  476. DLM_UNLOCK_REMOVE_LOCK |
  477. DLM_UNLOCK_REGRANT_LOCK |
  478. DLM_UNLOCK_CLEAR_CONVERT_TYPE);
  479. } else if (dlm_lock_on_list(&res->granted, lock)) {
  480. /* too late, already granted. */
  481. status = DLM_CANCELGRANT;
  482. *actions = DLM_UNLOCK_CALL_AST;
  483. } else {
  484. mlog(ML_ERROR, "lock to cancel is not on any list!\n");
  485. status = DLM_IVLOCKID;
  486. *actions = 0;
  487. }
  488. return status;
  489. }
  490. static enum dlm_status dlm_get_unlock_actions(struct dlm_ctxt *dlm,
  491. struct dlm_lock_resource *res,
  492. struct dlm_lock *lock,
  493. struct dlm_lockstatus *lksb,
  494. int *actions)
  495. {
  496. enum dlm_status status;
  497. /* unlock request */
  498. if (!dlm_lock_on_list(&res->granted, lock)) {
  499. status = DLM_DENIED;
  500. dlm_error(status);
  501. *actions = 0;
  502. } else {
  503. /* unlock granted lock */
  504. status = DLM_NORMAL;
  505. *actions = (DLM_UNLOCK_FREE_LOCK |
  506. DLM_UNLOCK_CALL_AST |
  507. DLM_UNLOCK_REMOVE_LOCK);
  508. }
  509. return status;
  510. }
  511. /* there seems to be no point in doing this async
  512. * since (even for the remote case) there is really
  513. * no work to queue up... so just do it and fire the
  514. * unlockast by hand when done... */
  515. enum dlm_status dlmunlock(struct dlm_ctxt *dlm, struct dlm_lockstatus *lksb,
  516. int flags, dlm_astunlockfunc_t *unlockast, void *data)
  517. {
  518. enum dlm_status status;
  519. struct dlm_lock_resource *res;
  520. struct dlm_lock *lock = NULL;
  521. int call_ast, is_master;
  522. if (!lksb) {
  523. dlm_error(DLM_BADARGS);
  524. return DLM_BADARGS;
  525. }
  526. if (flags & ~(LKM_CANCEL | LKM_VALBLK | LKM_INVVALBLK)) {
  527. dlm_error(DLM_BADPARAM);
  528. return DLM_BADPARAM;
  529. }
  530. if ((flags & (LKM_VALBLK | LKM_CANCEL)) == (LKM_VALBLK | LKM_CANCEL)) {
  531. mlog(0, "VALBLK given with CANCEL: ignoring VALBLK\n");
  532. flags &= ~LKM_VALBLK;
  533. }
  534. if (!lksb->lockid || !lksb->lockid->lockres) {
  535. dlm_error(DLM_BADPARAM);
  536. return DLM_BADPARAM;
  537. }
  538. lock = lksb->lockid;
  539. BUG_ON(!lock);
  540. dlm_lock_get(lock);
  541. res = lock->lockres;
  542. BUG_ON(!res);
  543. dlm_lockres_get(res);
  544. retry:
  545. call_ast = 0;
  546. /* need to retry up here because owner may have changed */
  547. mlog(0, "lock=%p res=%p\n", lock, res);
  548. spin_lock(&res->spinlock);
  549. is_master = (res->owner == dlm->node_num);
  550. if (flags & LKM_VALBLK && lock->ml.type != LKM_EXMODE)
  551. flags &= ~LKM_VALBLK;
  552. spin_unlock(&res->spinlock);
  553. if (is_master) {
  554. status = dlmunlock_master(dlm, res, lock, lksb, flags,
  555. &call_ast);
  556. mlog(0, "done calling dlmunlock_master: returned %d, "
  557. "call_ast is %d\n", status, call_ast);
  558. } else {
  559. status = dlmunlock_remote(dlm, res, lock, lksb, flags,
  560. &call_ast);
  561. mlog(0, "done calling dlmunlock_remote: returned %d, "
  562. "call_ast is %d\n", status, call_ast);
  563. }
  564. if (status == DLM_RECOVERING ||
  565. status == DLM_MIGRATING ||
  566. status == DLM_FORWARD ||
  567. status == DLM_NOLOCKMGR) {
  568. /* We want to go away for a tiny bit to allow recovery
  569. * / migration to complete on this resource. I don't
  570. * know of any wait queue we could sleep on as this
  571. * may be happening on another node. Perhaps the
  572. * proper solution is to queue up requests on the
  573. * other end? */
  574. /* do we want to yield(); ?? */
  575. msleep(50);
  576. mlog(0, "retrying unlock due to pending recovery/"
  577. "migration/in-progress/reconnect\n");
  578. goto retry;
  579. }
  580. if (call_ast) {
  581. mlog(0, "calling unlockast(%p, %d)\n", data, status);
  582. if (is_master) {
  583. /* it is possible that there is one last bast
  584. * pending. make sure it is flushed, then
  585. * call the unlockast.
  586. * not an issue if this is a mastered remotely,
  587. * since this lock has been removed from the
  588. * lockres queues and cannot be found. */
  589. dlm_kick_thread(dlm, NULL);
  590. wait_event(dlm->ast_wq,
  591. dlm_lock_basts_flushed(dlm, lock));
  592. }
  593. (*unlockast)(data, status);
  594. }
  595. if (status == DLM_CANCELGRANT)
  596. status = DLM_NORMAL;
  597. if (status == DLM_NORMAL) {
  598. mlog(0, "kicking the thread\n");
  599. dlm_kick_thread(dlm, res);
  600. } else
  601. dlm_error(status);
  602. dlm_lockres_calc_usage(dlm, res);
  603. dlm_lockres_put(res);
  604. dlm_lock_put(lock);
  605. mlog(0, "returning status=%d!\n", status);
  606. return status;
  607. }
  608. EXPORT_SYMBOL_GPL(dlmunlock);