dlmlock.c 18 KB

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