userdlm.c 18 KB

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