dlmlock.c 20 KB

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