dlmlock.c 20 KB

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