dlmlock.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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. void **ret_data)
  388. {
  389. struct dlm_ctxt *dlm = data;
  390. struct dlm_create_lock *create = (struct dlm_create_lock *)msg->buf;
  391. struct dlm_lock_resource *res = NULL;
  392. struct dlm_lock *newlock = NULL;
  393. struct dlm_lockstatus *lksb = NULL;
  394. enum dlm_status status = DLM_NORMAL;
  395. char *name;
  396. unsigned int namelen;
  397. BUG_ON(!dlm);
  398. mlog_entry_void();
  399. if (!dlm_grab(dlm))
  400. return DLM_REJECTED;
  401. name = create->name;
  402. namelen = create->namelen;
  403. status = DLM_REJECTED;
  404. if (!dlm_domain_fully_joined(dlm)) {
  405. mlog(ML_ERROR, "Domain %s not fully joined, but node %u is "
  406. "sending a create_lock message for lock %.*s!\n",
  407. dlm->name, create->node_idx, namelen, name);
  408. dlm_error(status);
  409. goto leave;
  410. }
  411. status = DLM_IVBUFLEN;
  412. if (namelen > DLM_LOCKID_NAME_MAX) {
  413. dlm_error(status);
  414. goto leave;
  415. }
  416. status = DLM_SYSERR;
  417. newlock = dlm_new_lock(create->requested_type,
  418. create->node_idx,
  419. be64_to_cpu(create->cookie), NULL);
  420. if (!newlock) {
  421. dlm_error(status);
  422. goto leave;
  423. }
  424. lksb = newlock->lksb;
  425. if (be32_to_cpu(create->flags) & LKM_GET_LVB) {
  426. lksb->flags |= DLM_LKSB_GET_LVB;
  427. mlog(0, "set DLM_LKSB_GET_LVB flag\n");
  428. }
  429. status = DLM_IVLOCKID;
  430. res = dlm_lookup_lockres(dlm, name, namelen);
  431. if (!res) {
  432. dlm_error(status);
  433. goto leave;
  434. }
  435. spin_lock(&res->spinlock);
  436. status = __dlm_lockres_state_to_status(res);
  437. spin_unlock(&res->spinlock);
  438. if (status != DLM_NORMAL) {
  439. mlog(0, "lockres recovering/migrating/in-progress\n");
  440. goto leave;
  441. }
  442. dlm_lock_attach_lockres(newlock, res);
  443. status = dlmlock_master(dlm, res, newlock, be32_to_cpu(create->flags));
  444. leave:
  445. if (status != DLM_NORMAL)
  446. if (newlock)
  447. dlm_lock_put(newlock);
  448. if (res)
  449. dlm_lockres_put(res);
  450. dlm_put(dlm);
  451. return status;
  452. }
  453. /* fetch next node-local (u8 nodenum + u56 cookie) into u64 */
  454. static inline void dlm_get_next_cookie(u8 node_num, u64 *cookie)
  455. {
  456. u64 tmpnode = node_num;
  457. /* shift single byte of node num into top 8 bits */
  458. tmpnode <<= 56;
  459. spin_lock(&dlm_cookie_lock);
  460. *cookie = (dlm_next_cookie | tmpnode);
  461. if (++dlm_next_cookie & 0xff00000000000000ull) {
  462. mlog(0, "This node's cookie will now wrap!\n");
  463. dlm_next_cookie = 1;
  464. }
  465. spin_unlock(&dlm_cookie_lock);
  466. }
  467. enum dlm_status dlmlock(struct dlm_ctxt *dlm, int mode,
  468. struct dlm_lockstatus *lksb, int flags,
  469. const char *name, int namelen, dlm_astlockfunc_t *ast,
  470. void *data, dlm_bastlockfunc_t *bast)
  471. {
  472. enum dlm_status status;
  473. struct dlm_lock_resource *res = NULL;
  474. struct dlm_lock *lock = NULL;
  475. int convert = 0, recovery = 0;
  476. /* yes this function is a mess.
  477. * TODO: clean this up. lots of common code in the
  478. * lock and convert paths, especially in the retry blocks */
  479. if (!lksb) {
  480. dlm_error(DLM_BADARGS);
  481. return DLM_BADARGS;
  482. }
  483. status = DLM_BADPARAM;
  484. if (mode != LKM_EXMODE && mode != LKM_PRMODE && mode != LKM_NLMODE) {
  485. dlm_error(status);
  486. goto error;
  487. }
  488. if (flags & ~LKM_VALID_FLAGS) {
  489. dlm_error(status);
  490. goto error;
  491. }
  492. convert = (flags & LKM_CONVERT);
  493. recovery = (flags & LKM_RECOVERY);
  494. if (recovery &&
  495. (!dlm_is_recovery_lock(name, namelen) || convert) ) {
  496. dlm_error(status);
  497. goto error;
  498. }
  499. if (convert && (flags & LKM_LOCAL)) {
  500. mlog(ML_ERROR, "strange LOCAL convert request!\n");
  501. goto error;
  502. }
  503. if (convert) {
  504. /* CONVERT request */
  505. /* if converting, must pass in a valid dlm_lock */
  506. lock = lksb->lockid;
  507. if (!lock) {
  508. mlog(ML_ERROR, "NULL lock pointer in convert "
  509. "request\n");
  510. goto error;
  511. }
  512. res = lock->lockres;
  513. if (!res) {
  514. mlog(ML_ERROR, "NULL lockres pointer in convert "
  515. "request\n");
  516. goto error;
  517. }
  518. dlm_lockres_get(res);
  519. /* XXX: for ocfs2 purposes, the ast/bast/astdata/lksb are
  520. * static after the original lock call. convert requests will
  521. * ensure that everything is the same, or return DLM_BADARGS.
  522. * this means that DLM_DENIED_NOASTS will never be returned.
  523. */
  524. if (lock->lksb != lksb || lock->ast != ast ||
  525. lock->bast != bast || lock->astdata != data) {
  526. status = DLM_BADARGS;
  527. mlog(ML_ERROR, "new args: lksb=%p, ast=%p, bast=%p, "
  528. "astdata=%p\n", lksb, ast, bast, data);
  529. mlog(ML_ERROR, "orig args: lksb=%p, ast=%p, bast=%p, "
  530. "astdata=%p\n", lock->lksb, lock->ast,
  531. lock->bast, lock->astdata);
  532. goto error;
  533. }
  534. retry_convert:
  535. dlm_wait_for_recovery(dlm);
  536. if (res->owner == dlm->node_num)
  537. status = dlmconvert_master(dlm, res, lock, flags, mode);
  538. else
  539. status = dlmconvert_remote(dlm, res, lock, flags, mode);
  540. if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
  541. status == DLM_FORWARD) {
  542. /* for now, see how this works without sleeping
  543. * and just retry right away. I suspect the reco
  544. * or migration will complete fast enough that
  545. * no waiting will be necessary */
  546. mlog(0, "retrying convert with migration/recovery/"
  547. "in-progress\n");
  548. msleep(100);
  549. goto retry_convert;
  550. }
  551. } else {
  552. u64 tmpcookie;
  553. /* LOCK request */
  554. status = DLM_BADARGS;
  555. if (!name) {
  556. dlm_error(status);
  557. goto error;
  558. }
  559. status = DLM_IVBUFLEN;
  560. if (namelen > DLM_LOCKID_NAME_MAX || namelen < 1) {
  561. dlm_error(status);
  562. goto error;
  563. }
  564. dlm_get_next_cookie(dlm->node_num, &tmpcookie);
  565. lock = dlm_new_lock(mode, dlm->node_num, tmpcookie, lksb);
  566. if (!lock) {
  567. dlm_error(status);
  568. goto error;
  569. }
  570. if (!recovery)
  571. dlm_wait_for_recovery(dlm);
  572. /* find or create the lock resource */
  573. res = dlm_get_lock_resource(dlm, name, namelen, flags);
  574. if (!res) {
  575. status = DLM_IVLOCKID;
  576. dlm_error(status);
  577. goto error;
  578. }
  579. mlog(0, "type=%d, flags = 0x%x\n", mode, flags);
  580. mlog(0, "creating lock: lock=%p res=%p\n", lock, res);
  581. dlm_lock_attach_lockres(lock, res);
  582. lock->ast = ast;
  583. lock->bast = bast;
  584. lock->astdata = data;
  585. retry_lock:
  586. if (flags & LKM_VALBLK) {
  587. mlog(0, "LKM_VALBLK passed by caller\n");
  588. /* LVB requests for non PR, PW or EX locks are
  589. * ignored. */
  590. if (mode < LKM_PRMODE)
  591. flags &= ~LKM_VALBLK;
  592. else {
  593. flags |= LKM_GET_LVB;
  594. lock->lksb->flags |= DLM_LKSB_GET_LVB;
  595. }
  596. }
  597. if (res->owner == dlm->node_num)
  598. status = dlmlock_master(dlm, res, lock, flags);
  599. else
  600. status = dlmlock_remote(dlm, res, lock, flags);
  601. if (status == DLM_RECOVERING || status == DLM_MIGRATING ||
  602. status == DLM_FORWARD) {
  603. mlog(0, "retrying lock with migration/"
  604. "recovery/in progress\n");
  605. msleep(100);
  606. /* no waiting for dlm_reco_thread */
  607. if (recovery) {
  608. if (status != DLM_RECOVERING)
  609. goto retry_lock;
  610. mlog(0, "%s: got RECOVERING "
  611. "for $RECOVERY lock, master "
  612. "was %u\n", dlm->name,
  613. res->owner);
  614. /* wait to see the node go down, then
  615. * drop down and allow the lockres to
  616. * get cleaned up. need to remaster. */
  617. dlm_wait_for_node_death(dlm, res->owner,
  618. DLM_NODE_DEATH_WAIT_MAX);
  619. } else {
  620. dlm_wait_for_recovery(dlm);
  621. goto retry_lock;
  622. }
  623. }
  624. if (status != DLM_NORMAL) {
  625. lock->lksb->flags &= ~DLM_LKSB_GET_LVB;
  626. if (status != DLM_NOTQUEUED)
  627. dlm_error(status);
  628. goto error;
  629. }
  630. }
  631. error:
  632. if (status != DLM_NORMAL) {
  633. if (lock && !convert)
  634. dlm_lock_put(lock);
  635. // this is kind of unnecessary
  636. lksb->status = status;
  637. }
  638. /* put lockres ref from the convert path
  639. * or from dlm_get_lock_resource */
  640. if (res)
  641. dlm_lockres_put(res);
  642. return status;
  643. }
  644. EXPORT_SYMBOL_GPL(dlmlock);