dlmlock.c 19 KB

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