dlmunlock.c 19 KB

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