dlmlock.c 19 KB

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