dlmlock.c 19 KB

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