dlmlock.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmlock.c
  5. *
  6. * underlying calls for lock creation
  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. #include "dlmconvert.h"
  46. #define MLOG_MASK_PREFIX ML_DLM
  47. #include "cluster/masklog.h"
  48. static spinlock_t dlm_cookie_lock = SPIN_LOCK_UNLOCKED;
  49. static u64 dlm_next_cookie = 1;
  50. static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
  51. struct dlm_lock_resource *res,
  52. struct dlm_lock *lock, int flags);
  53. static void dlm_init_lock(struct dlm_lock *newlock, int type,
  54. u8 node, u64 cookie);
  55. static void dlm_lock_release(struct kref *kref);
  56. static void dlm_lock_detach_lockres(struct dlm_lock *lock);
  57. /* Tell us whether we can grant a new lock request.
  58. * locking:
  59. * caller needs: res->spinlock
  60. * taken: none
  61. * held on exit: none
  62. * returns: 1 if the lock can be granted, 0 otherwise.
  63. */
  64. static int dlm_can_grant_new_lock(struct dlm_lock_resource *res,
  65. struct dlm_lock *lock)
  66. {
  67. struct list_head *iter;
  68. struct dlm_lock *tmplock;
  69. list_for_each(iter, &res->granted) {
  70. tmplock = list_entry(iter, struct dlm_lock, list);
  71. if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
  72. return 0;
  73. }
  74. list_for_each(iter, &res->converting) {
  75. tmplock = list_entry(iter, struct dlm_lock, list);
  76. if (!dlm_lock_compatible(tmplock->ml.type, lock->ml.type))
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. /* performs lock creation at the lockres master site
  82. * locking:
  83. * caller needs: none
  84. * taken: takes and drops res->spinlock
  85. * held on exit: none
  86. * returns: DLM_NORMAL, DLM_NOTQUEUED
  87. */
  88. static enum dlm_status dlmlock_master(struct dlm_ctxt *dlm,
  89. struct dlm_lock_resource *res,
  90. struct dlm_lock *lock, int flags)
  91. {
  92. int call_ast = 0, kick_thread = 0;
  93. enum dlm_status status = DLM_NORMAL;
  94. mlog_entry("type=%d\n", lock->ml.type);
  95. spin_lock(&res->spinlock);
  96. /* if called from dlm_create_lock_handler, need to
  97. * ensure it will not sleep in dlm_wait_on_lockres */
  98. status = __dlm_lockres_state_to_status(res);
  99. if (status != DLM_NORMAL &&
  100. lock->ml.node != dlm->node_num) {
  101. /* erf. state changed after lock was dropped. */
  102. spin_unlock(&res->spinlock);
  103. dlm_error(status);
  104. return status;
  105. }
  106. __dlm_wait_on_lockres(res);
  107. __dlm_lockres_reserve_ast(res);
  108. if (dlm_can_grant_new_lock(res, lock)) {
  109. mlog(0, "I can grant this lock right away\n");
  110. /* got it right away */
  111. lock->lksb->status = DLM_NORMAL;
  112. status = DLM_NORMAL;
  113. dlm_lock_get(lock);
  114. list_add_tail(&lock->list, &res->granted);
  115. /* for the recovery lock, we can't allow the ast
  116. * to be queued since the dlmthread is already
  117. * frozen. but the recovery lock is always locked
  118. * with LKM_NOQUEUE so we do not need the ast in
  119. * this special case */
  120. if (!dlm_is_recovery_lock(res->lockname.name,
  121. res->lockname.len)) {
  122. kick_thread = 1;
  123. call_ast = 1;
  124. }
  125. } else {
  126. /* for NOQUEUE request, unless we get the
  127. * lock right away, return DLM_NOTQUEUED */
  128. if (flags & LKM_NOQUEUE)
  129. status = DLM_NOTQUEUED;
  130. else {
  131. dlm_lock_get(lock);
  132. list_add_tail(&lock->list, &res->blocked);
  133. kick_thread = 1;
  134. }
  135. }
  136. spin_unlock(&res->spinlock);
  137. wake_up(&res->wq);
  138. /* either queue the ast or release it */
  139. if (call_ast)
  140. dlm_queue_ast(dlm, lock);
  141. else
  142. dlm_lockres_release_ast(dlm, res);
  143. dlm_lockres_calc_usage(dlm, res);
  144. if (kick_thread)
  145. dlm_kick_thread(dlm, res);
  146. return status;
  147. }
  148. void dlm_revert_pending_lock(struct dlm_lock_resource *res,
  149. struct dlm_lock *lock)
  150. {
  151. /* remove from local queue if it failed */
  152. list_del_init(&lock->list);
  153. lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
  154. }
  155. /*
  156. * locking:
  157. * caller needs: none
  158. * taken: takes and drops res->spinlock
  159. * held on exit: none
  160. * returns: DLM_DENIED, DLM_RECOVERING, or net status
  161. */
  162. static enum dlm_status dlmlock_remote(struct dlm_ctxt *dlm,
  163. struct dlm_lock_resource *res,
  164. struct dlm_lock *lock, int flags)
  165. {
  166. enum dlm_status status = DLM_DENIED;
  167. mlog_entry("type=%d\n", lock->ml.type);
  168. mlog(0, "lockres %.*s, flags = 0x%x\n", res->lockname.len,
  169. res->lockname.name, flags);
  170. spin_lock(&res->spinlock);
  171. /* will exit this call with spinlock held */
  172. __dlm_wait_on_lockres(res);
  173. res->state |= DLM_LOCK_RES_IN_PROGRESS;
  174. /* add lock to local (secondary) queue */
  175. dlm_lock_get(lock);
  176. list_add_tail(&lock->list, &res->blocked);
  177. lock->lock_pending = 1;
  178. spin_unlock(&res->spinlock);
  179. /* spec seems to say that you will get DLM_NORMAL when the lock
  180. * has been queued, meaning we need to wait for a reply here. */
  181. status = dlm_send_remote_lock_request(dlm, res, lock, flags);
  182. spin_lock(&res->spinlock);
  183. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  184. lock->lock_pending = 0;
  185. if (status != DLM_NORMAL) {
  186. if (status != DLM_NOTQUEUED)
  187. dlm_error(status);
  188. dlm_revert_pending_lock(res, lock);
  189. dlm_lock_put(lock);
  190. } else if (dlm_is_recovery_lock(res->lockname.name,
  191. res->lockname.len)) {
  192. /* special case for the $RECOVERY lock.
  193. * there will never be an AST delivered to put
  194. * this lock on the proper secondary queue
  195. * (granted), so do it manually. */
  196. mlog(0, "%s: $RECOVERY lock for this node (%u) is "
  197. "mastered by %u; got lock, manually granting (no ast)\n",
  198. dlm->name, dlm->node_num, res->owner);
  199. list_del_init(&lock->list);
  200. list_add_tail(&lock->list, &res->granted);
  201. }
  202. spin_unlock(&res->spinlock);
  203. dlm_lockres_calc_usage(dlm, res);
  204. wake_up(&res->wq);
  205. return status;
  206. }
  207. /* for remote lock creation.
  208. * locking:
  209. * caller needs: none, but need res->state & DLM_LOCK_RES_IN_PROGRESS
  210. * taken: none
  211. * held on exit: none
  212. * returns: DLM_NOLOCKMGR, or net status
  213. */
  214. static enum dlm_status dlm_send_remote_lock_request(struct dlm_ctxt *dlm,
  215. struct dlm_lock_resource *res,
  216. struct dlm_lock *lock, int flags)
  217. {
  218. struct dlm_create_lock create;
  219. int tmpret, status = 0;
  220. enum dlm_status ret;
  221. mlog_entry_void();
  222. memset(&create, 0, sizeof(create));
  223. create.node_idx = dlm->node_num;
  224. create.requested_type = lock->ml.type;
  225. create.cookie = lock->ml.cookie;
  226. create.namelen = res->lockname.len;
  227. create.flags = cpu_to_be32(flags);
  228. memcpy(create.name, res->lockname.name, create.namelen);
  229. tmpret = o2net_send_message(DLM_CREATE_LOCK_MSG, dlm->key, &create,
  230. sizeof(create), res->owner, &status);
  231. if (tmpret >= 0) {
  232. // successfully sent and received
  233. ret = status; // this is already a dlm_status
  234. } else {
  235. mlog_errno(tmpret);
  236. if (dlm_is_host_down(tmpret)) {
  237. ret = DLM_RECOVERING;
  238. mlog(0, "node %u died so returning DLM_RECOVERING "
  239. "from lock message!\n", res->owner);
  240. } else {
  241. ret = dlm_err_to_dlm_status(tmpret);
  242. }
  243. }
  244. return ret;
  245. }
  246. void dlm_lock_get(struct dlm_lock *lock)
  247. {
  248. kref_get(&lock->lock_refs);
  249. }
  250. void dlm_lock_put(struct dlm_lock *lock)
  251. {
  252. kref_put(&lock->lock_refs, dlm_lock_release);
  253. }
  254. static void dlm_lock_release(struct kref *kref)
  255. {
  256. struct dlm_lock *lock;
  257. lock = container_of(kref, struct dlm_lock, lock_refs);
  258. BUG_ON(!list_empty(&lock->list));
  259. BUG_ON(!list_empty(&lock->ast_list));
  260. BUG_ON(!list_empty(&lock->bast_list));
  261. BUG_ON(lock->ast_pending);
  262. BUG_ON(lock->bast_pending);
  263. dlm_lock_detach_lockres(lock);
  264. if (lock->lksb_kernel_allocated) {
  265. mlog(0, "freeing kernel-allocated lksb\n");
  266. kfree(lock->lksb);
  267. }
  268. kfree(lock);
  269. }
  270. /* associate a lock with it's lockres, getting a ref on the lockres */
  271. void dlm_lock_attach_lockres(struct dlm_lock *lock,
  272. struct dlm_lock_resource *res)
  273. {
  274. dlm_lockres_get(res);
  275. lock->lockres = res;
  276. }
  277. /* drop ref on lockres, if there is still one associated with lock */
  278. static void dlm_lock_detach_lockres(struct dlm_lock *lock)
  279. {
  280. struct dlm_lock_resource *res;
  281. res = lock->lockres;
  282. if (res) {
  283. lock->lockres = NULL;
  284. mlog(0, "removing lock's lockres reference\n");
  285. dlm_lockres_put(res);
  286. }
  287. }
  288. static void dlm_init_lock(struct dlm_lock *newlock, int type,
  289. u8 node, u64 cookie)
  290. {
  291. INIT_LIST_HEAD(&newlock->list);
  292. INIT_LIST_HEAD(&newlock->ast_list);
  293. INIT_LIST_HEAD(&newlock->bast_list);
  294. spin_lock_init(&newlock->spinlock);
  295. newlock->ml.type = type;
  296. newlock->ml.convert_type = LKM_IVMODE;
  297. newlock->ml.highest_blocked = LKM_IVMODE;
  298. newlock->ml.node = node;
  299. newlock->ml.pad1 = 0;
  300. newlock->ml.list = 0;
  301. newlock->ml.flags = 0;
  302. newlock->ast = NULL;
  303. newlock->bast = NULL;
  304. newlock->astdata = NULL;
  305. newlock->ml.cookie = cpu_to_be64(cookie);
  306. newlock->ast_pending = 0;
  307. newlock->bast_pending = 0;
  308. newlock->convert_pending = 0;
  309. newlock->lock_pending = 0;
  310. newlock->unlock_pending = 0;
  311. newlock->cancel_pending = 0;
  312. newlock->lksb_kernel_allocated = 0;
  313. kref_init(&newlock->lock_refs);
  314. }
  315. struct dlm_lock * dlm_new_lock(int type, u8 node, u64 cookie,
  316. struct dlm_lockstatus *lksb)
  317. {
  318. struct dlm_lock *lock;
  319. int kernel_allocated = 0;
  320. lock = kcalloc(1, sizeof(*lock), GFP_KERNEL);
  321. if (!lock)
  322. return NULL;
  323. if (!lksb) {
  324. /* zero memory only if kernel-allocated */
  325. lksb = kcalloc(1, sizeof(*lksb), GFP_KERNEL);
  326. if (!lksb) {
  327. kfree(lock);
  328. return NULL;
  329. }
  330. kernel_allocated = 1;
  331. }
  332. dlm_init_lock(lock, type, node, cookie);
  333. if (kernel_allocated)
  334. lock->lksb_kernel_allocated = 1;
  335. lock->lksb = lksb;
  336. lksb->lockid = lock;
  337. return lock;
  338. }
  339. /* handler for lock creation net message
  340. * locking:
  341. * caller needs: none
  342. * taken: takes and drops res->spinlock
  343. * held on exit: none
  344. * returns: DLM_NORMAL, DLM_SYSERR, DLM_IVLOCKID, DLM_NOTQUEUED
  345. */
  346. int dlm_create_lock_handler(struct o2net_msg *msg, u32 len, void *data)
  347. {
  348. struct dlm_ctxt *dlm = data;
  349. struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf;
  350. struct dlm_lock_resource *res = NULL;
  351. struct dlm_lock *newlock = NULL;
  352. struct dlm_lockstatus *lksb = NULL;
  353. enum dlm_status status = DLM_NORMAL;
  354. char *name;
  355. unsigned int namelen;
  356. BUG_ON(!dlm);
  357. mlog_entry_void();
  358. if (!dlm_grab(dlm))
  359. return DLM_REJECTED;
  360. mlog_bug_on_msg(!dlm_domain_fully_joined(dlm),
  361. "Domain %s not fully joined!\n", dlm->name);
  362. name = create->name;
  363. namelen = create->namelen;
  364. status = DLM_IVBUFLEN;
  365. if (namelen > DLM_LOCKID_NAME_MAX) {
  366. dlm_error(status);
  367. goto leave;
  368. }
  369. status = DLM_SYSERR;
  370. newlock = dlm_new_lock(create->requested_type,
  371. create->node_idx,
  372. be64_to_cpu(create->cookie), NULL);
  373. if (!newlock) {
  374. dlm_error(status);
  375. goto leave;
  376. }
  377. lksb = newlock->lksb;
  378. if (be32_to_cpu(create->flags) & LKM_GET_LVB) {
  379. lksb->flags |= DLM_LKSB_GET_LVB;
  380. mlog(0, "set DLM_LKSB_GET_LVB flag\n");
  381. }
  382. status = DLM_IVLOCKID;
  383. res = dlm_lookup_lockres(dlm, name, namelen);
  384. if (!res) {
  385. dlm_error(status);
  386. goto leave;
  387. }
  388. spin_lock(&res->spinlock);
  389. status = __dlm_lockres_state_to_status(res);
  390. spin_unlock(&res->spinlock);
  391. if (status != DLM_NORMAL) {
  392. mlog(0, "lockres recovering/migrating/in-progress\n");
  393. goto leave;
  394. }
  395. dlm_lock_attach_lockres(newlock, res);
  396. status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags));
  397. leave:
  398. if (status != DLM_NORMAL)
  399. if (newlock)
  400. dlm_lock_put(newlock);
  401. if (res)
  402. dlm_lockres_put(res);
  403. dlm_put(dlm);
  404. return status;
  405. }
  406. /* fetch next node-local (u8 nodenum + u56 cookie) into u64 */
  407. static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie)
  408. {
  409. u64 tmpnode = node_num;
  410. /* shift single byte of node num into top 8 bits */
  411. tmpnode <<= 56;
  412. spin_lock(&dlm_cookie_lock);
  413. *cookie = (dlm_next_cookie | tmpnode);
  414. if (++dlm_next_cookie & 0xff00000000000000ull) {
  415. mlog(0, "This node's cookie will now wrap!\n");
  416. dlm_next_cookie = 1;
  417. }
  418. spin_unlock(&dlm_cookie_lock);
  419. }
  420. enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode,
  421. struct dlm_lockstatus *lksb, int flags,
  422. const char *name, dlm_astlockfunc_t *ast, void *data,
  423. dlm_bastlockfunc_t *bast)
  424. {
  425. enum dlm_status status;
  426. struct dlm_lock_resource *res = NULL;
  427. struct dlm_lock *lock = NULL;
  428. int convert = 0, recovery = 0;
  429. /* yes this function is a mess.
  430. * TODO: clean this up. lots of common code in the
  431. * lock and convert paths, especially in the retry blocks */
  432. if (!lksb) {
  433. dlm_error(DLM_BADARGS);
  434. return DLM_BADARGS;
  435. }
  436. status = DLM_BADPARAM;
  437. if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) {
  438. dlm_error(status);
  439. goto error;
  440. }
  441. if (flags & ~LKM_VALID_FLAGS) {
  442. dlm_error(status);
  443. goto error;
  444. }
  445. convert = (flags & LKM_CONVERT);
  446. recovery = (flags & LKM_RECOVERY);
  447. if (recovery &&
  448. (!dlm_is_recovery_lock(name, strlen(name)) || convert) ) {
  449. dlm_error(status);
  450. goto error;
  451. }
  452. if (convert && (flags & LKM_LOCAL)) {
  453. mlog(ML_ERROR, "strange LOCAL convert request!\n");
  454. goto error;
  455. }
  456. if (convert) {
  457. /* CONVERT request */
  458. /* if converting, must pass in a valid dlm_lock */
  459. lock = lksb->lockid;
  460. if (!lock) {
  461. mlog(ML_ERROR, "NULL lock pointer in convert "
  462. "request\n");
  463. goto error;
  464. }
  465. res = lock->lockres;
  466. if (!res) {
  467. mlog(ML_ERROR, "NULL lockres pointer in convert "
  468. "request\n");
  469. goto error;
  470. }
  471. dlm_lockres_get(res);
  472. /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are
  473. * static after the original lock call. convert requests will
  474. * ensure that everything is the same, or return DLM_BADARGS.
  475. * this means that DLM_DENIED_NOASTS will never be returned.
  476. */
  477. if (lock->lksb != lksb || lock->ast != ast ||
  478. lock->bast != bast || lock->astdata != data) {
  479. status = DLM_BADARGS;
  480. mlog(ML_ERROR, "new args: lksb=%p, ast=%p, bast=%p, "
  481. "astdata=%p\n", lksb, ast, bast, data);
  482. mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, "
  483. "astdata=%p\n", lock->lksb, lock->ast,
  484. lock->bast, lock->astdata);
  485. goto error;
  486. }
  487. retry_convert:
  488. dlm_wait_for_recovery(dlm);
  489. if (res->owner == dlm->node_num)
  490. status = dlmconvert_master(dlm, res, lock, flags, mode);
  491. else
  492. status = dlmconvert_remote(dlm, res, lock, flags, mode);
  493. if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
  494. status == DLM_FORWARD) {
  495. /* for now, see how this works without sleeping
  496. * and just retry right away. I suspect the reco
  497. * or migration will complete fast enough that
  498. * no waiting will be necessary */
  499. mlog(0, "retrying convert with migration/recovery/"
  500. "in-progress\n");
  501. msleep(100);
  502. goto retry_convert;
  503. }
  504. } else {
  505. u64 tmpcookie;
  506. /* LOCK request */
  507. status = DLM_BADARGS;
  508. if (!name) {
  509. dlm_error(status);
  510. goto error;
  511. }
  512. status = DLM_IVBUFLEN;
  513. if (strlen(name) > DLM_LOCKID_NAME_MAX || strlen(name) < 1) {
  514. dlm_error(status);
  515. goto error;
  516. }
  517. dlm_get_next_cookie(dlm->node_num, &tmpcookie);
  518. lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb);
  519. if (!lock) {
  520. dlm_error(status);
  521. goto error;
  522. }
  523. if (!recovery)
  524. dlm_wait_for_recovery(dlm);
  525. /* find or create the lock resource */
  526. res = dlm_get_lock_resource(dlm, name, flags);
  527. if (!res) {
  528. status = DLM_IVLOCKID;
  529. dlm_error(status);
  530. goto error;
  531. }
  532. mlog(0, "type=%d, flags = 0x%x\n", mode, flags);
  533. mlog(0, "creating lock: lock=%p res=%p\n", lock, res);
  534. dlm_lock_attach_lockres(lock, res);
  535. lock->ast = ast;
  536. lock->bast = bast;
  537. lock->astdata = data;
  538. retry_lock:
  539. if (flags & LKM_VALBLK) {
  540. mlog(0, "LKM_VALBLK passed by caller\n");
  541. /* LVB requests for non PR, PW or EX locks are
  542. * ignored. */
  543. if (mode < LKM_PRMODE)
  544. flags &= ~LKM_VALBLK;
  545. else {
  546. flags |= LKM_GET_LVB;
  547. lock->lksb->flags |= DLM_LKSB_GET_LVB;
  548. }
  549. }
  550. if (res->owner == dlm->node_num)
  551. status = dlmlock_master(dlm, res, lock, flags);
  552. else
  553. status = dlmlock_remote(dlm, res, lock, flags);
  554. if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
  555. status == DLM_FORWARD) {
  556. mlog(0, "retrying lock with migration/"
  557. "recovery/in progress\n");
  558. msleep(100);
  559. /* no waiting for dlm_reco_thread */
  560. if (recovery) {
  561. if (status == DLM_RECOVERING) {
  562. mlog(0, "%s: got RECOVERING "
  563. "for $REOCVERY lock, master "
  564. "was %u\n", dlm->name,
  565. res->owner);
  566. dlm_wait_for_node_death(dlm, res->owner,
  567. DLM_NODE_DEATH_WAIT_MAX);
  568. }
  569. } else {
  570. dlm_wait_for_recovery(dlm);
  571. }
  572. goto retry_lock;
  573. }
  574. if (status != DLM_NORMAL) {
  575. lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
  576. if (status != DLM_NOTQUEUED)
  577. dlm_error(status);
  578. goto error;
  579. }
  580. }
  581. error:
  582. if (status != DLM_NORMAL) {
  583. if (lock && !convert)
  584. dlm_lock_put(lock);
  585. // this is kind of unnecessary
  586. lksb->status = status;
  587. }
  588. /* put lockres ref from the convert path
  589. * or from dlm_get_lock_resource */
  590. if (res)
  591. dlm_lockres_put(res);
  592. return status;
  593. }
  594. EXPORT_SYMBOL_GPL(dlmlock);