dlmlock.c 20 KB

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