dlmunlock.c 19 KB

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