userdlm.c 17 KB

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