userdlm.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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(ML_BASTS, "AST fired for lockres %.*s, level %d => %d\n",
  109. lockres->l_namelen, lockres->l_name, lockres->l_level,
  110. lockres->l_requested);
  111. spin_lock(&lockres->l_lock);
  112. status = ocfs2_dlm_lock_status(&lockres->l_lksb);
  113. if (status) {
  114. mlog(ML_ERROR, "lksb status value of %u on lockres %.*s\n",
  115. status, lockres->l_namelen, lockres->l_name);
  116. spin_unlock(&lockres->l_lock);
  117. return;
  118. }
  119. mlog_bug_on_msg(lockres->l_requested == DLM_LOCK_IV,
  120. "Lockres %.*s, requested ivmode. flags 0x%x\n",
  121. lockres->l_namelen, lockres->l_name, lockres->l_flags);
  122. /* we're downconverting. */
  123. if (lockres->l_requested < lockres->l_level) {
  124. if (lockres->l_requested <=
  125. user_highest_compat_lock_level(lockres->l_blocking)) {
  126. lockres->l_blocking = DLM_LOCK_NL;
  127. lockres->l_flags &= ~USER_LOCK_BLOCKED;
  128. }
  129. }
  130. lockres->l_level = lockres->l_requested;
  131. lockres->l_requested = DLM_LOCK_IV;
  132. lockres->l_flags |= USER_LOCK_ATTACHED;
  133. lockres->l_flags &= ~USER_LOCK_BUSY;
  134. spin_unlock(&lockres->l_lock);
  135. wake_up(&lockres->l_event);
  136. }
  137. static inline void user_dlm_grab_inode_ref(struct user_lock_res *lockres)
  138. {
  139. struct inode *inode;
  140. inode = user_dlm_inode_from_user_lockres(lockres);
  141. if (!igrab(inode))
  142. BUG();
  143. }
  144. static void user_dlm_unblock_lock(struct work_struct *work);
  145. static void __user_dlm_queue_lockres(struct user_lock_res *lockres)
  146. {
  147. if (!(lockres->l_flags & USER_LOCK_QUEUED)) {
  148. user_dlm_grab_inode_ref(lockres);
  149. INIT_WORK(&lockres->l_work, user_dlm_unblock_lock);
  150. queue_work(user_dlm_worker, &lockres->l_work);
  151. lockres->l_flags |= USER_LOCK_QUEUED;
  152. }
  153. }
  154. static void __user_dlm_cond_queue_lockres(struct user_lock_res *lockres)
  155. {
  156. int queue = 0;
  157. if (!(lockres->l_flags & USER_LOCK_BLOCKED))
  158. return;
  159. switch (lockres->l_blocking) {
  160. case DLM_LOCK_EX:
  161. if (!lockres->l_ex_holders && !lockres->l_ro_holders)
  162. queue = 1;
  163. break;
  164. case DLM_LOCK_PR:
  165. if (!lockres->l_ex_holders)
  166. queue = 1;
  167. break;
  168. default:
  169. BUG();
  170. }
  171. if (queue)
  172. __user_dlm_queue_lockres(lockres);
  173. }
  174. static void user_bast(struct ocfs2_dlm_lksb *lksb, int level)
  175. {
  176. struct user_lock_res *lockres = user_lksb_to_lock_res(lksb);
  177. mlog(ML_BASTS, "BAST fired for lockres %.*s, blocking %d, level %d\n",
  178. lockres->l_namelen, lockres->l_name, level, lockres->l_level);
  179. spin_lock(&lockres->l_lock);
  180. lockres->l_flags |= USER_LOCK_BLOCKED;
  181. if (level > lockres->l_blocking)
  182. lockres->l_blocking = level;
  183. __user_dlm_queue_lockres(lockres);
  184. spin_unlock(&lockres->l_lock);
  185. wake_up(&lockres->l_event);
  186. }
  187. static void user_unlock_ast(struct ocfs2_dlm_lksb *lksb, int status)
  188. {
  189. struct user_lock_res *lockres = user_lksb_to_lock_res(lksb);
  190. mlog(ML_BASTS, "UNLOCK AST fired for lockres %.*s, flags 0x%x\n",
  191. lockres->l_namelen, lockres->l_name, lockres->l_flags);
  192. if (status)
  193. mlog(ML_ERROR, "dlm returns status %d\n", status);
  194. spin_lock(&lockres->l_lock);
  195. /* The teardown flag gets set early during the unlock process,
  196. * so test the cancel flag to make sure that this ast isn't
  197. * for a concurrent cancel. */
  198. if (lockres->l_flags & USER_LOCK_IN_TEARDOWN
  199. && !(lockres->l_flags & USER_LOCK_IN_CANCEL)) {
  200. lockres->l_level = DLM_LOCK_IV;
  201. } else if (status == DLM_CANCELGRANT) {
  202. /* We tried to cancel a convert request, but it was
  203. * already granted. Don't clear the busy flag - the
  204. * ast should've done this already. */
  205. BUG_ON(!(lockres->l_flags & USER_LOCK_IN_CANCEL));
  206. lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
  207. goto out_noclear;
  208. } else {
  209. BUG_ON(!(lockres->l_flags & USER_LOCK_IN_CANCEL));
  210. /* Cancel succeeded, we want to re-queue */
  211. lockres->l_requested = DLM_LOCK_IV; /* cancel an
  212. * upconvert
  213. * request. */
  214. lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
  215. /* we want the unblock thread to look at it again
  216. * now. */
  217. if (lockres->l_flags & USER_LOCK_BLOCKED)
  218. __user_dlm_queue_lockres(lockres);
  219. }
  220. lockres->l_flags &= ~USER_LOCK_BUSY;
  221. out_noclear:
  222. spin_unlock(&lockres->l_lock);
  223. wake_up(&lockres->l_event);
  224. }
  225. /*
  226. * This is the userdlmfs locking protocol version.
  227. *
  228. * See fs/ocfs2/dlmglue.c for more details on locking versions.
  229. */
  230. static struct ocfs2_locking_protocol user_dlm_lproto = {
  231. .lp_max_version = {
  232. .pv_major = OCFS2_LOCKING_PROTOCOL_MAJOR,
  233. .pv_minor = OCFS2_LOCKING_PROTOCOL_MINOR,
  234. },
  235. .lp_lock_ast = user_ast,
  236. .lp_blocking_ast = user_bast,
  237. .lp_unlock_ast = user_unlock_ast,
  238. };
  239. static inline void user_dlm_drop_inode_ref(struct user_lock_res *lockres)
  240. {
  241. struct inode *inode;
  242. inode = user_dlm_inode_from_user_lockres(lockres);
  243. iput(inode);
  244. }
  245. static void user_dlm_unblock_lock(struct work_struct *work)
  246. {
  247. int new_level, status;
  248. struct user_lock_res *lockres =
  249. container_of(work, struct user_lock_res, l_work);
  250. struct ocfs2_cluster_connection *conn =
  251. cluster_connection_from_user_lockres(lockres);
  252. mlog(0, "lockres %.*s\n", lockres->l_namelen, 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. mlog(ML_BASTS, "lockres %.*s USER_LOCK_BLOCKED\n",
  267. lockres->l_namelen, lockres->l_name);
  268. spin_unlock(&lockres->l_lock);
  269. goto drop_ref;
  270. }
  271. if (lockres->l_flags & USER_LOCK_IN_TEARDOWN) {
  272. mlog(ML_BASTS, "lockres %.*s USER_LOCK_IN_TEARDOWN\n",
  273. lockres->l_namelen, lockres->l_name);
  274. spin_unlock(&lockres->l_lock);
  275. goto drop_ref;
  276. }
  277. if (lockres->l_flags & USER_LOCK_BUSY) {
  278. if (lockres->l_flags & USER_LOCK_IN_CANCEL) {
  279. mlog(ML_BASTS, "lockres %.*s USER_LOCK_IN_CANCEL\n",
  280. lockres->l_namelen, lockres->l_name);
  281. spin_unlock(&lockres->l_lock);
  282. goto drop_ref;
  283. }
  284. lockres->l_flags |= USER_LOCK_IN_CANCEL;
  285. spin_unlock(&lockres->l_lock);
  286. status = ocfs2_dlm_unlock(conn, &lockres->l_lksb,
  287. DLM_LKF_CANCEL);
  288. if (status)
  289. user_log_dlm_error("ocfs2_dlm_unlock", status, lockres);
  290. goto drop_ref;
  291. }
  292. /* If there are still incompat holders, we can exit safely
  293. * without worrying about re-queueing this lock as that will
  294. * happen on the last call to user_cluster_unlock. */
  295. if ((lockres->l_blocking == DLM_LOCK_EX)
  296. && (lockres->l_ex_holders || lockres->l_ro_holders)) {
  297. spin_unlock(&lockres->l_lock);
  298. mlog(ML_BASTS, "lockres %.*s, EX/PR Holders %u,%u\n",
  299. lockres->l_namelen, lockres->l_name,
  300. lockres->l_ex_holders, lockres->l_ro_holders);
  301. goto drop_ref;
  302. }
  303. if ((lockres->l_blocking == DLM_LOCK_PR)
  304. && lockres->l_ex_holders) {
  305. spin_unlock(&lockres->l_lock);
  306. mlog(ML_BASTS, "lockres %.*s, EX Holders %u\n",
  307. lockres->l_namelen, lockres->l_name,
  308. lockres->l_ex_holders);
  309. goto drop_ref;
  310. }
  311. /* yay, we can downconvert now. */
  312. new_level = user_highest_compat_lock_level(lockres->l_blocking);
  313. lockres->l_requested = new_level;
  314. lockres->l_flags |= USER_LOCK_BUSY;
  315. mlog(ML_BASTS, "lockres %.*s, downconvert %d => %d\n",
  316. lockres->l_namelen, lockres->l_name, lockres->l_level, new_level);
  317. spin_unlock(&lockres->l_lock);
  318. /* need lock downconvert request now... */
  319. status = ocfs2_dlm_lock(conn, new_level, &lockres->l_lksb,
  320. DLM_LKF_CONVERT|DLM_LKF_VALBLK,
  321. lockres->l_name,
  322. lockres->l_namelen);
  323. if (status) {
  324. user_log_dlm_error("ocfs2_dlm_lock", status, lockres);
  325. user_recover_from_dlm_error(lockres);
  326. }
  327. drop_ref:
  328. user_dlm_drop_inode_ref(lockres);
  329. }
  330. static inline void user_dlm_inc_holders(struct user_lock_res *lockres,
  331. int level)
  332. {
  333. switch(level) {
  334. case DLM_LOCK_EX:
  335. lockres->l_ex_holders++;
  336. break;
  337. case DLM_LOCK_PR:
  338. lockres->l_ro_holders++;
  339. break;
  340. default:
  341. BUG();
  342. }
  343. }
  344. /* predict what lock level we'll be dropping down to on behalf
  345. * of another node, and return true if the currently wanted
  346. * level will be compatible with it. */
  347. static inline int
  348. user_may_continue_on_blocked_lock(struct user_lock_res *lockres,
  349. int wanted)
  350. {
  351. BUG_ON(!(lockres->l_flags & USER_LOCK_BLOCKED));
  352. return wanted <= user_highest_compat_lock_level(lockres->l_blocking);
  353. }
  354. int user_dlm_cluster_lock(struct user_lock_res *lockres,
  355. int level,
  356. int lkm_flags)
  357. {
  358. int status, local_flags;
  359. struct ocfs2_cluster_connection *conn =
  360. cluster_connection_from_user_lockres(lockres);
  361. if (level != DLM_LOCK_EX &&
  362. level != DLM_LOCK_PR) {
  363. mlog(ML_ERROR, "lockres %.*s: invalid request!\n",
  364. lockres->l_namelen, lockres->l_name);
  365. status = -EINVAL;
  366. goto bail;
  367. }
  368. mlog(ML_BASTS, "lockres %.*s, level %d, flags = 0x%x\n",
  369. lockres->l_namelen, lockres->l_name, level, lkm_flags);
  370. again:
  371. if (signal_pending(current)) {
  372. status = -ERESTARTSYS;
  373. goto bail;
  374. }
  375. spin_lock(&lockres->l_lock);
  376. /* We only compare against the currently granted level
  377. * here. If the lock is blocked waiting on a downconvert,
  378. * we'll get caught below. */
  379. if ((lockres->l_flags & USER_LOCK_BUSY) &&
  380. (level > lockres->l_level)) {
  381. /* is someone sitting in dlm_lock? If so, wait on
  382. * them. */
  383. spin_unlock(&lockres->l_lock);
  384. user_wait_on_busy_lock(lockres);
  385. goto again;
  386. }
  387. if ((lockres->l_flags & USER_LOCK_BLOCKED) &&
  388. (!user_may_continue_on_blocked_lock(lockres, level))) {
  389. /* is the lock is currently blocked on behalf of
  390. * another node */
  391. spin_unlock(&lockres->l_lock);
  392. user_wait_on_blocked_lock(lockres);
  393. goto again;
  394. }
  395. if (level > lockres->l_level) {
  396. local_flags = lkm_flags | DLM_LKF_VALBLK;
  397. if (lockres->l_level != DLM_LOCK_IV)
  398. local_flags |= DLM_LKF_CONVERT;
  399. lockres->l_requested = level;
  400. lockres->l_flags |= USER_LOCK_BUSY;
  401. spin_unlock(&lockres->l_lock);
  402. BUG_ON(level == DLM_LOCK_IV);
  403. BUG_ON(level == DLM_LOCK_NL);
  404. /* call dlm_lock to upgrade lock now */
  405. status = ocfs2_dlm_lock(conn, level, &lockres->l_lksb,
  406. local_flags, lockres->l_name,
  407. lockres->l_namelen);
  408. if (status) {
  409. if ((lkm_flags & DLM_LKF_NOQUEUE) &&
  410. (status != -EAGAIN))
  411. user_log_dlm_error("ocfs2_dlm_lock",
  412. status, lockres);
  413. user_recover_from_dlm_error(lockres);
  414. goto bail;
  415. }
  416. user_wait_on_busy_lock(lockres);
  417. goto again;
  418. }
  419. user_dlm_inc_holders(lockres, level);
  420. spin_unlock(&lockres->l_lock);
  421. status = 0;
  422. bail:
  423. return status;
  424. }
  425. static inline void user_dlm_dec_holders(struct user_lock_res *lockres,
  426. int level)
  427. {
  428. switch(level) {
  429. case DLM_LOCK_EX:
  430. BUG_ON(!lockres->l_ex_holders);
  431. lockres->l_ex_holders--;
  432. break;
  433. case DLM_LOCK_PR:
  434. BUG_ON(!lockres->l_ro_holders);
  435. lockres->l_ro_holders--;
  436. break;
  437. default:
  438. BUG();
  439. }
  440. }
  441. void user_dlm_cluster_unlock(struct user_lock_res *lockres,
  442. int level)
  443. {
  444. if (level != DLM_LOCK_EX &&
  445. level != DLM_LOCK_PR) {
  446. mlog(ML_ERROR, "lockres %.*s: invalid request!\n",
  447. lockres->l_namelen, lockres->l_name);
  448. return;
  449. }
  450. spin_lock(&lockres->l_lock);
  451. user_dlm_dec_holders(lockres, level);
  452. __user_dlm_cond_queue_lockres(lockres);
  453. spin_unlock(&lockres->l_lock);
  454. }
  455. void user_dlm_write_lvb(struct inode *inode,
  456. const char *val,
  457. unsigned int len)
  458. {
  459. struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
  460. char *lvb;
  461. BUG_ON(len > DLM_LVB_LEN);
  462. spin_lock(&lockres->l_lock);
  463. BUG_ON(lockres->l_level < DLM_LOCK_EX);
  464. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  465. memcpy(lvb, val, len);
  466. spin_unlock(&lockres->l_lock);
  467. }
  468. ssize_t user_dlm_read_lvb(struct inode *inode,
  469. char *val,
  470. unsigned int len)
  471. {
  472. struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
  473. char *lvb;
  474. ssize_t ret = len;
  475. BUG_ON(len > DLM_LVB_LEN);
  476. spin_lock(&lockres->l_lock);
  477. BUG_ON(lockres->l_level < DLM_LOCK_PR);
  478. if (ocfs2_dlm_lvb_valid(&lockres->l_lksb)) {
  479. lvb = ocfs2_dlm_lvb(&lockres->l_lksb);
  480. memcpy(val, lvb, len);
  481. } else
  482. ret = 0;
  483. spin_unlock(&lockres->l_lock);
  484. return ret;
  485. }
  486. void user_dlm_lock_res_init(struct user_lock_res *lockres,
  487. struct dentry *dentry)
  488. {
  489. memset(lockres, 0, sizeof(*lockres));
  490. spin_lock_init(&lockres->l_lock);
  491. init_waitqueue_head(&lockres->l_event);
  492. lockres->l_level = DLM_LOCK_IV;
  493. lockres->l_requested = DLM_LOCK_IV;
  494. lockres->l_blocking = DLM_LOCK_IV;
  495. /* should have been checked before getting here. */
  496. BUG_ON(dentry->d_name.len >= USER_DLM_LOCK_ID_MAX_LEN);
  497. memcpy(lockres->l_name,
  498. dentry->d_name.name,
  499. dentry->d_name.len);
  500. lockres->l_namelen = dentry->d_name.len;
  501. }
  502. int user_dlm_destroy_lock(struct user_lock_res *lockres)
  503. {
  504. int status = -EBUSY;
  505. struct ocfs2_cluster_connection *conn =
  506. cluster_connection_from_user_lockres(lockres);
  507. mlog(ML_BASTS, "lockres %.*s\n", lockres->l_namelen, lockres->l_name);
  508. spin_lock(&lockres->l_lock);
  509. if (lockres->l_flags & USER_LOCK_IN_TEARDOWN) {
  510. spin_unlock(&lockres->l_lock);
  511. return 0;
  512. }
  513. lockres->l_flags |= USER_LOCK_IN_TEARDOWN;
  514. while (lockres->l_flags & USER_LOCK_BUSY) {
  515. spin_unlock(&lockres->l_lock);
  516. user_wait_on_busy_lock(lockres);
  517. spin_lock(&lockres->l_lock);
  518. }
  519. if (lockres->l_ro_holders || lockres->l_ex_holders) {
  520. spin_unlock(&lockres->l_lock);
  521. goto bail;
  522. }
  523. status = 0;
  524. if (!(lockres->l_flags & USER_LOCK_ATTACHED)) {
  525. spin_unlock(&lockres->l_lock);
  526. goto bail;
  527. }
  528. lockres->l_flags &= ~USER_LOCK_ATTACHED;
  529. lockres->l_flags |= USER_LOCK_BUSY;
  530. spin_unlock(&lockres->l_lock);
  531. status = ocfs2_dlm_unlock(conn, &lockres->l_lksb, DLM_LKF_VALBLK);
  532. if (status) {
  533. user_log_dlm_error("ocfs2_dlm_unlock", status, lockres);
  534. goto bail;
  535. }
  536. user_wait_on_busy_lock(lockres);
  537. status = 0;
  538. bail:
  539. return status;
  540. }
  541. static void user_dlm_recovery_handler_noop(int node_num,
  542. void *recovery_data)
  543. {
  544. /* We ignore recovery events */
  545. return;
  546. }
  547. void user_dlm_set_locking_protocol(void)
  548. {
  549. ocfs2_stack_glue_set_max_proto_version(&user_dlm_lproto.lp_max_version);
  550. }
  551. struct ocfs2_cluster_connection *user_dlm_register(struct qstr *name)
  552. {
  553. int rc;
  554. struct ocfs2_cluster_connection *conn;
  555. rc = ocfs2_cluster_connect_agnostic(name->name, name->len,
  556. &user_dlm_lproto,
  557. user_dlm_recovery_handler_noop,
  558. NULL, &conn);
  559. if (rc)
  560. mlog_errno(rc);
  561. return rc ? ERR_PTR(rc) : conn;
  562. }
  563. void user_dlm_unregister(struct ocfs2_cluster_connection *conn)
  564. {
  565. ocfs2_cluster_disconnect(conn, 0);
  566. }