userdlm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * userdlm.c
  5. *
  6. * Code which implements the kernel side of a minimal userspace
  7. * interface to our DLM.
  8. *
  9. * Many of the functions here are pared down versions of dlmglue.c
  10. * functions.
  11. *
  12. * Copyright (C) 2003, 2004 Oracle. All rights reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2 of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public
  25. * License along with this program; if not, write to the
  26. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  27. * Boston, MA 021110-1307, USA.
  28. */
  29. #include <linux/signal.h>
  30. #include <linux/module.h>
  31. #include <linux/fs.h>
  32. #include <linux/types.h>
  33. #include <linux/crc32.h>
  34. #include "ocfs2_lockingver.h"
  35. #include "stackglue.h"
  36. #include "userdlm.h"
  37. #define MLOG_MASK_PREFIX ML_DLMFS
  38. #include "cluster/masklog.h"
  39. static inline struct user_lock_res *user_lksb_to_lock_res(struct ocfs2_dlm_lksb *lksb)
  40. {
  41. return container_of(lksb, struct user_lock_res, l_lksb);
  42. }
  43. static inline int user_check_wait_flag(struct user_lock_res *lockres,
  44. int flag)
  45. {
  46. int ret;
  47. spin_lock(&lockres->l_lock);
  48. ret = lockres->l_flags & flag;
  49. spin_unlock(&lockres->l_lock);
  50. return ret;
  51. }
  52. static inline void user_wait_on_busy_lock(struct user_lock_res *lockres)
  53. {
  54. wait_event(lockres->l_event,
  55. !user_check_wait_flag(lockres, USER_LOCK_BUSY));
  56. }
  57. static inline void user_wait_on_blocked_lock(struct user_lock_res *lockres)
  58. {
  59. wait_event(lockres->l_event,
  60. !user_check_wait_flag(lockres, USER_LOCK_BLOCKED));
  61. }
  62. /* I heart container_of... */
  63. static inline struct ocfs2_cluster_connection *
  64. cluster_connection_from_user_lockres(struct user_lock_res *lockres)
  65. {
  66. struct dlmfs_inode_private *ip;
  67. ip = container_of(lockres,
  68. struct dlmfs_inode_private,
  69. ip_lockres);
  70. return ip->ip_conn;
  71. }
  72. static struct inode *
  73. user_dlm_inode_from_user_lockres(struct user_lock_res *lockres)
  74. {
  75. struct dlmfs_inode_private *ip;
  76. ip = container_of(lockres,
  77. struct dlmfs_inode_private,
  78. ip_lockres);
  79. return &ip->ip_vfs_inode;
  80. }
  81. static inline void user_recover_from_dlm_error(struct user_lock_res *lockres)
  82. {
  83. spin_lock(&lockres->l_lock);
  84. lockres->l_flags &= ~USER_LOCK_BUSY;
  85. spin_unlock(&lockres->l_lock);
  86. }
  87. #define user_log_dlm_error(_func, _stat, _lockres) do { \
  88. mlog(ML_ERROR, "Dlm error %d while calling %s on " \
  89. "resource %.*s\n", _stat, _func, \
  90. _lockres->l_namelen, _lockres->l_name); \
  91. } while (0)
  92. /* WARNING: This function lives in a world where the only three lock
  93. * levels are EX, PR, and NL. It *will* have to be adjusted when more
  94. * lock types are added. */
  95. static inline int user_highest_compat_lock_level(int level)
  96. {
  97. int new_level = DLM_LOCK_EX;
  98. if (level == DLM_LOCK_EX)
  99. new_level = DLM_LOCK_NL;
  100. else if (level == DLM_LOCK_PR)
  101. new_level = DLM_LOCK_PR;
  102. return new_level;
  103. }
  104. static void user_ast(struct ocfs2_dlm_lksb *lksb)
  105. {
  106. struct user_lock_res *lockres = user_lksb_to_lock_res(lksb);
  107. int status;
  108. mlog(0, "AST fired for lockres %.*s\n", lockres->l_namelen,
  109. lockres->l_name);
  110. spin_lock(&lockres->l_lock);
  111. status = ocfs2_dlm_lock_status(&lockres->l_lksb);
  112. if (status) {
  113. mlog(ML_ERROR, "lksb status value of %u on lockres %.*s\n",
  114. status, lockres->l_namelen, lockres->l_name);
  115. spin_unlock(&lockres->l_lock);
  116. return;
  117. }
  118. mlog_bug_on_msg(lockres->l_requested == DLM_LOCK_IV,
  119. "Lockres %.*s, requested ivmode. flags 0x%x\n",
  120. lockres->l_namelen, lockres->l_name, lockres->l_flags);
  121. /* we're downconverting. */
  122. if (lockres->l_requested < lockres->l_level) {
  123. if (lockres->l_requested <=
  124. user_highest_compat_lock_level(lockres->l_blocking)) {
  125. lockres->l_blocking = DLM_LOCK_NL;
  126. lockres->l_flags &= ~USER_LOCK_BLOCKED;
  127. }
  128. }
  129. lockres->l_level = lockres->l_requested;
  130. lockres->l_requested = DLM_LOCK_IV;
  131. lockres->l_flags |= USER_LOCK_ATTACHED;
  132. lockres->l_flags &= ~USER_LOCK_BUSY;
  133. spin_unlock(&lockres->l_lock);
  134. wake_up(&lockres->l_event);
  135. }
  136. static inline void user_dlm_grab_inode_ref(struct user_lock_res *lockres)
  137. {
  138. struct inode *inode;
  139. inode = user_dlm_inode_from_user_lockres(lockres);
  140. if (!igrab(inode))
  141. BUG();
  142. }
  143. static void user_dlm_unblock_lock(struct work_struct *work);
  144. static void __user_dlm_queue_lockres(struct user_lock_res *lockres)
  145. {
  146. if (!(lockres->l_flags & USER_LOCK_QUEUED)) {
  147. user_dlm_grab_inode_ref(lockres);
  148. INIT_WORK(&lockres->l_work, user_dlm_unblock_lock);
  149. queue_work(user_dlm_worker, &lockres->l_work);
  150. lockres->l_flags |= USER_LOCK_QUEUED;
  151. }
  152. }
  153. static void __user_dlm_cond_queue_lockres(struct user_lock_res *lockres)
  154. {
  155. int queue = 0;
  156. if (!(lockres->l_flags & USER_LOCK_BLOCKED))
  157. return;
  158. switch (lockres->l_blocking) {
  159. case DLM_LOCK_EX:
  160. if (!lockres->l_ex_holders && !lockres->l_ro_holders)
  161. queue = 1;
  162. break;
  163. case DLM_LOCK_PR:
  164. if (!lockres->l_ex_holders)
  165. queue = 1;
  166. break;
  167. default:
  168. BUG();
  169. }
  170. if (queue)
  171. __user_dlm_queue_lockres(lockres);
  172. }
  173. static void user_bast(struct ocfs2_dlm_lksb *lksb, int level)
  174. {
  175. struct user_lock_res *lockres = user_lksb_to_lock_res(lksb);
  176. mlog(0, "Blocking AST fired for lockres %.*s. Blocking level %d\n",
  177. lockres->l_namelen, lockres->l_name, level);
  178. spin_lock(&lockres->l_lock);
  179. lockres->l_flags |= USER_LOCK_BLOCKED;
  180. if (level > lockres->l_blocking)
  181. lockres->l_blocking = level;
  182. __user_dlm_queue_lockres(lockres);
  183. spin_unlock(&lockres->l_lock);
  184. wake_up(&lockres->l_event);
  185. }
  186. static void user_unlock_ast(struct ocfs2_dlm_lksb *lksb, int status)
  187. {
  188. struct user_lock_res *lockres = user_lksb_to_lock_res(lksb);
  189. mlog(0, "UNLOCK AST called on lock %.*s\n", lockres->l_namelen,
  190. lockres->l_name);
  191. if (status)
  192. mlog(ML_ERROR, "dlm returns status %d\n", status);
  193. spin_lock(&lockres->l_lock);
  194. /* The teardown flag gets set early during the unlock process,
  195. * so test the cancel flag to make sure that this ast isn't
  196. * for a concurrent cancel. */
  197. if (lockres->l_flags & USER_LOCK_IN_TEARDOWN
  198. && !(lockres->l_flags & USER_LOCK_IN_CANCEL)) {
  199. lockres->l_level = DLM_LOCK_IV;
  200. } else if (status == DLM_CANCELGRANT) {
  201. /* We tried to cancel a convert request, but it was
  202. * already granted. Don't clear the busy flag - the
  203. * ast should've done this already. */
  204. BUG_ON(!(lockres->l_flags & USER_LOCK_IN_CANCEL));
  205. lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
  206. goto out_noclear;
  207. } else {
  208. BUG_ON(!(lockres->l_flags & USER_LOCK_IN_CANCEL));
  209. /* Cancel succeeded, we want to re-queue */
  210. lockres->l_requested = DLM_LOCK_IV; /* cancel an
  211. * upconvert
  212. * request. */
  213. lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
  214. /* we want the unblock thread to look at it again
  215. * now. */
  216. if (lockres->l_flags & USER_LOCK_BLOCKED)
  217. __user_dlm_queue_lockres(lockres);
  218. }
  219. lockres->l_flags &= ~USER_LOCK_BUSY;
  220. out_noclear:
  221. spin_unlock(&lockres->l_lock);
  222. wake_up(&lockres->l_event);
  223. }
  224. /*
  225. * This is the userdlmfs locking protocol version.
  226. *
  227. * See fs/ocfs2/dlmglue.c for more details on locking versions.
  228. */
  229. static struct ocfs2_locking_protocol user_dlm_lproto = {
  230. .lp_max_version = {
  231. .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
  232. .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
  233. },
  234. .lp_lock_ast = user_ast,
  235. .lp_blocking_ast = user_bast,
  236. .lp_unlock_ast = user_unlock_ast,
  237. };
  238. static inline void user_dlm_drop_inode_ref(struct user_lock_res *lockres)
  239. {
  240. struct inode *inode;
  241. inode = user_dlm_inode_from_user_lockres(lockres);
  242. iput(inode);
  243. }
  244. static void user_dlm_unblock_lock(struct work_struct *work)
  245. {
  246. int new_level, status;
  247. struct user_lock_res *lockres =
  248. container_of(work, struct user_lock_res, l_work);
  249. struct ocfs2_cluster_connection *conn =
  250. cluster_connection_from_user_lockres(lockres);
  251. mlog(0, "processing lockres %.*s\n", lockres->l_namelen,
  252. lockres->l_name);
  253. spin_lock(&lockres->l_lock);
  254. mlog_bug_on_msg(!(lockres->l_flags & USER_LOCK_QUEUED),
  255. "Lockres %.*s, flags 0x%x\n",
  256. lockres->l_namelen, lockres->l_name, lockres->l_flags);
  257. /* notice that we don't clear USER_LOCK_BLOCKED here. If it's
  258. * set, we want user_ast clear it. */
  259. lockres->l_flags &= ~USER_LOCK_QUEUED;
  260. /* It's valid to get here and no longer be blocked - if we get
  261. * several basts in a row, we might be queued by the first
  262. * one, the unblock thread might run and clear the queued
  263. * flag, and finally we might get another bast which re-queues
  264. * us before our ast for the downconvert is called. */
  265. if (!(lockres->l_flags & USER_LOCK_BLOCKED)) {
  266. spin_unlock(&lockres->l_lock);
  267. goto drop_ref;
  268. }
  269. if (lockres->l_flags & USER_LOCK_IN_TEARDOWN) {
  270. spin_unlock(&lockres->l_lock);
  271. goto drop_ref;
  272. }
  273. if (lockres->l_flags & USER_LOCK_BUSY) {
  274. if (lockres->l_flags & USER_LOCK_IN_CANCEL) {
  275. spin_unlock(&lockres->l_lock);
  276. goto drop_ref;
  277. }
  278. lockres->l_flags |= USER_LOCK_IN_CANCEL;
  279. spin_unlock(&lockres->l_lock);
  280. status = ocfs2_dlm_unlock(conn, &lockres->l_lksb,
  281. DLM_LKF_CANCEL);
  282. if (status)
  283. user_log_dlm_error("ocfs2_dlm_unlock", status, lockres);
  284. goto drop_ref;
  285. }
  286. /* If there are still incompat holders, we can exit safely
  287. * without worrying about re-queueing this lock as that will
  288. * happen on the last call to user_cluster_unlock. */
  289. if ((lockres->l_blocking == DLM_LOCK_EX)
  290. && (lockres->l_ex_holders || lockres->l_ro_holders)) {
  291. spin_unlock(&lockres->l_lock);
  292. mlog(0, "can't downconvert for ex: ro = %u, ex = %u\n",
  293. lockres->l_ro_holders, lockres->l_ex_holders);
  294. goto drop_ref;
  295. }
  296. if ((lockres->l_blocking == DLM_LOCK_PR)
  297. && lockres->l_ex_holders) {
  298. spin_unlock(&lockres->l_lock);
  299. mlog(0, "can't downconvert for pr: ex = %u\n",
  300. lockres->l_ex_holders);
  301. goto drop_ref;
  302. }
  303. /* yay, we can downconvert now. */
  304. new_level = user_highest_compat_lock_level(lockres->l_blocking);
  305. lockres->l_requested = new_level;
  306. lockres->l_flags |= USER_LOCK_BUSY;
  307. mlog(0, "Downconvert lock from %d to %d\n",
  308. lockres->l_level, new_level);
  309. spin_unlock(&lockres->l_lock);
  310. /* need lock downconvert request now... */
  311. status = ocfs2_dlm_lock(conn, new_level, &lockres->l_lksb,
  312. DLM_LKF_CONVERT|DLM_LKF_VALBLK,
  313. lockres->l_name,
  314. lockres->l_namelen);
  315. if (status) {
  316. user_log_dlm_error("ocfs2_dlm_lock", status, lockres);
  317. user_recover_from_dlm_error(lockres);
  318. }
  319. drop_ref:
  320. user_dlm_drop_inode_ref(lockres);
  321. }
  322. static inline void user_dlm_inc_holders(struct user_lock_res *lockres,
  323. int level)
  324. {
  325. switch(level) {
  326. case DLM_LOCK_EX:
  327. lockres->l_ex_holders++;
  328. break;
  329. case DLM_LOCK_PR:
  330. lockres->l_ro_holders++;
  331. break;
  332. default:
  333. BUG();
  334. }
  335. }
  336. /* predict what lock level we'll be dropping down to on behalf
  337. * of another node, and return true if the currently wanted
  338. * level will be compatible with it. */
  339. static inline int
  340. user_may_continue_on_blocked_lock(struct user_lock_res *lockres,
  341. int wanted)
  342. {
  343. BUG_ON(!(lockres->l_flags & USER_LOCK_BLOCKED));
  344. return wanted <= user_highest_compat_lock_level(lockres->l_blocking);
  345. }
  346. int user_dlm_cluster_lock(struct user_lock_res *lockres,
  347. int level,
  348. int lkm_flags)
  349. {
  350. int status, local_flags;
  351. struct ocfs2_cluster_connection *conn =
  352. cluster_connection_from_user_lockres(lockres);
  353. if (level != DLM_LOCK_EX &&
  354. level != DLM_LOCK_PR) {
  355. mlog(ML_ERROR, "lockres %.*s: invalid request!\n",
  356. lockres->l_namelen, lockres->l_name);
  357. status = -EINVAL;
  358. goto bail;
  359. }
  360. mlog(0, "lockres %.*s: asking for %s lock, passed flags = 0x%x\n",
  361. lockres->l_namelen, lockres->l_name,
  362. (level == DLM_LOCK_EX) ? "DLM_LOCK_EX" : "DLM_LOCK_PR",
  363. lkm_flags);
  364. again:
  365. if (signal_pending(current)) {
  366. status = -ERESTARTSYS;
  367. goto bail;
  368. }
  369. spin_lock(&lockres->l_lock);
  370. /* We only compare against the currently granted level
  371. * here. If the lock is blocked waiting on a downconvert,
  372. * we'll get caught below. */
  373. if ((lockres->l_flags & USER_LOCK_BUSY) &&
  374. (level > lockres->l_level)) {
  375. /* is someone sitting in dlm_lock? If so, wait on
  376. * them. */
  377. spin_unlock(&lockres->l_lock);
  378. user_wait_on_busy_lock(lockres);
  379. goto again;
  380. }
  381. if ((lockres->l_flags & USER_LOCK_BLOCKED) &&
  382. (!user_may_continue_on_blocked_lock(lockres, level))) {
  383. /* is the lock is currently blocked on behalf of
  384. * another node */
  385. spin_unlock(&lockres->l_lock);
  386. user_wait_on_blocked_lock(lockres);
  387. goto again;
  388. }
  389. if (level > lockres->l_level) {
  390. local_flags = lkm_flags | DLM_LKF_VALBLK;
  391. if (lockres->l_level != DLM_LOCK_IV)
  392. local_flags |= DLM_LKF_CONVERT;
  393. lockres->l_requested = level;
  394. lockres->l_flags |= USER_LOCK_BUSY;
  395. spin_unlock(&lockres->l_lock);
  396. BUG_ON(level == DLM_LOCK_IV);
  397. BUG_ON(level == DLM_LOCK_NL);
  398. /* call dlm_lock to upgrade lock now */
  399. status = ocfs2_dlm_lock(conn, level, &lockres->l_lksb,
  400. local_flags, lockres->l_name,
  401. lockres->l_namelen);
  402. if (status) {
  403. if ((lkm_flags & DLM_LKF_NOQUEUE) &&
  404. (status != -EAGAIN))
  405. user_log_dlm_error("ocfs2_dlm_lock",
  406. status, lockres);
  407. user_recover_from_dlm_error(lockres);
  408. goto bail;
  409. }
  410. user_wait_on_busy_lock(lockres);
  411. goto again;
  412. }
  413. user_dlm_inc_holders(lockres, level);
  414. spin_unlock(&lockres->l_lock);
  415. status = 0;
  416. bail:
  417. return status;
  418. }
  419. static inline void user_dlm_dec_holders(struct user_lock_res *lockres,
  420. int level)
  421. {
  422. switch(level) {
  423. case DLM_LOCK_EX:
  424. BUG_ON(!lockres->l_ex_holders);
  425. lockres->l_ex_holders--;
  426. break;
  427. case DLM_LOCK_PR:
  428. BUG_ON(!lockres->l_ro_holders);
  429. lockres->l_ro_holders--;
  430. break;
  431. default:
  432. BUG();
  433. }
  434. }
  435. void user_dlm_cluster_unlock(struct user_lock_res *lockres,
  436. int level)
  437. {
  438. if (level != DLM_LOCK_EX &&
  439. level != DLM_LOCK_PR) {
  440. mlog(ML_ERROR, "lockres %.*s: invalid request!\n",
  441. lockres->l_namelen, lockres->l_name);
  442. return;
  443. }
  444. spin_lock(&lockres->l_lock);
  445. user_dlm_dec_holders(lockres, level);
  446. __user_dlm_cond_queue_lockres(lockres);
  447. spin_unlock(&lockres->l_lock);
  448. }
  449. void user_dlm_write_lvb(struct inode *inode,
  450. const char *val,
  451. unsigned int len)
  452. {
  453. struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
  454. char *lvb;
  455. BUG_ON(len > DLM_LVB_LEN);
  456. spin_lock(&lockres->l_lock);
  457. BUG_ON(lockres->l_level < DLM_LOCK_EX);
  458. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  459. memcpy(lvb, val, len);
  460. spin_unlock(&lockres->l_lock);
  461. }
  462. ssize_t user_dlm_read_lvb(struct inode *inode,
  463. char *val,
  464. unsigned int len)
  465. {
  466. struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
  467. char *lvb;
  468. ssize_t ret = len;
  469. BUG_ON(len > DLM_LVB_LEN);
  470. spin_lock(&lockres->l_lock);
  471. BUG_ON(lockres->l_level < DLM_LOCK_PR);
  472. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)) {
  473. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  474. memcpy(val, lvb, len);
  475. } else
  476. ret = 0;
  477. spin_unlock(&lockres->l_lock);
  478. return ret;
  479. }
  480. void user_dlm_lock_res_init(struct user_lock_res *lockres,
  481. struct dentry *dentry)
  482. {
  483. memset(lockres, 0, sizeof(*lockres));
  484. spin_lock_init(&lockres->l_lock);
  485. init_waitqueue_head(&lockres->l_event);
  486. lockres->l_level = DLM_LOCK_IV;
  487. lockres->l_requested = DLM_LOCK_IV;
  488. lockres->l_blocking = DLM_LOCK_IV;
  489. /* should have been checked before getting here. */
  490. BUG_ON(dentry->d_name.len >= USER_DLM_LOCK_ID_MAX_LEN);
  491. memcpy(lockres->l_name,
  492. dentry->d_name.name,
  493. dentry->d_name.len);
  494. lockres->l_namelen = dentry->d_name.len;
  495. }
  496. int user_dlm_destroy_lock(struct user_lock_res *lockres)
  497. {
  498. int status = -EBUSY;
  499. struct ocfs2_cluster_connection *conn =
  500. cluster_connection_from_user_lockres(lockres);
  501. mlog(0, "asked to destroy %.*s\n", lockres->l_namelen, lockres->l_name);
  502. spin_lock(&lockres->l_lock);
  503. if (lockres->l_flags & USER_LOCK_IN_TEARDOWN) {
  504. spin_unlock(&lockres->l_lock);
  505. return 0;
  506. }
  507. lockres->l_flags |= USER_LOCK_IN_TEARDOWN;
  508. while (lockres->l_flags & USER_LOCK_BUSY) {
  509. spin_unlock(&lockres->l_lock);
  510. user_wait_on_busy_lock(lockres);
  511. spin_lock(&lockres->l_lock);
  512. }
  513. if (lockres->l_ro_holders || lockres->l_ex_holders) {
  514. spin_unlock(&lockres->l_lock);
  515. goto bail;
  516. }
  517. status = 0;
  518. if (!(lockres->l_flags & USER_LOCK_ATTACHED)) {
  519. spin_unlock(&lockres->l_lock);
  520. goto bail;
  521. }
  522. lockres->l_flags &= ~USER_LOCK_ATTACHED;
  523. lockres->l_flags |= USER_LOCK_BUSY;
  524. spin_unlock(&lockres->l_lock);
  525. status = ocfs2_dlm_unlock(conn, &lockres->l_lksb, DLM_LKF_VALBLK);
  526. if (status) {
  527. user_log_dlm_error("ocfs2_dlm_unlock", status, lockres);
  528. goto bail;
  529. }
  530. user_wait_on_busy_lock(lockres);
  531. status = 0;
  532. bail:
  533. return status;
  534. }
  535. static void user_dlm_recovery_handler_noop(int node_num,
  536. void *recovery_data)
  537. {
  538. /* We ignore recovery events */
  539. return;
  540. }
  541. void user_dlm_set_locking_protocol(void)
  542. {
  543. ocfs2_stack_glue_set_max_proto_version(&user_dlm_lproto.lp_max_version);
  544. }
  545. struct ocfs2_cluster_connection *user_dlm_register(struct qstr *name)
  546. {
  547. int rc;
  548. struct ocfs2_cluster_connection *conn;
  549. rc = ocfs2_cluster_connect("o2cb", name->name, name->len,
  550. &user_dlm_lproto,
  551. user_dlm_recovery_handler_noop,
  552. NULL, &conn);
  553. if (rc)
  554. mlog_errno(rc);
  555. return rc ? ERR_PTR(rc) : conn;
  556. }
  557. void user_dlm_unregister(struct ocfs2_cluster_connection *conn)
  558. {
  559. ocfs2_cluster_disconnect(conn, 0);
  560. }