userdlm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. /* we're downconverting. */
  116. if (lockres->l_requested < lockres->l_level) {
  117. if (lockres->l_requested <=
  118. user_highest_compat_lock_level(lockres->l_blocking)) {
  119. lockres->l_blocking = LKM_NLMODE;
  120. lockres->l_flags &= ~USER_LOCK_BLOCKED;
  121. }
  122. }
  123. lockres->l_level = lockres->l_requested;
  124. lockres->l_requested = LKM_IVMODE;
  125. lockres->l_flags |= USER_LOCK_ATTACHED;
  126. lockres->l_flags &= ~USER_LOCK_BUSY;
  127. spin_unlock(&lockres->l_lock);
  128. wake_up(&lockres->l_event);
  129. }
  130. static inline void user_dlm_grab_inode_ref(struct user_lock_res *lockres)
  131. {
  132. struct inode *inode;
  133. inode = user_dlm_inode_from_user_lockres(lockres);
  134. if (!igrab(inode))
  135. BUG();
  136. }
  137. static void user_dlm_unblock_lock(void *opaque);
  138. static void __user_dlm_queue_lockres(struct user_lock_res *lockres)
  139. {
  140. if (!(lockres->l_flags & USER_LOCK_QUEUED)) {
  141. user_dlm_grab_inode_ref(lockres);
  142. INIT_WORK(&lockres->l_work, user_dlm_unblock_lock,
  143. lockres);
  144. queue_work(user_dlm_worker, &lockres->l_work);
  145. lockres->l_flags |= USER_LOCK_QUEUED;
  146. }
  147. }
  148. static void __user_dlm_cond_queue_lockres(struct user_lock_res *lockres)
  149. {
  150. int queue = 0;
  151. if (!(lockres->l_flags & USER_LOCK_BLOCKED))
  152. return;
  153. switch (lockres->l_blocking) {
  154. case LKM_EXMODE:
  155. if (!lockres->l_ex_holders && !lockres->l_ro_holders)
  156. queue = 1;
  157. break;
  158. case LKM_PRMODE:
  159. if (!lockres->l_ex_holders)
  160. queue = 1;
  161. break;
  162. default:
  163. BUG();
  164. }
  165. if (queue)
  166. __user_dlm_queue_lockres(lockres);
  167. }
  168. static void user_bast(void *opaque, int level)
  169. {
  170. struct user_lock_res *lockres = opaque;
  171. mlog(0, "Blocking AST fired for lockres %s. Blocking level %d\n",
  172. lockres->l_name, level);
  173. spin_lock(&lockres->l_lock);
  174. lockres->l_flags |= USER_LOCK_BLOCKED;
  175. if (level > lockres->l_blocking)
  176. lockres->l_blocking = level;
  177. __user_dlm_queue_lockres(lockres);
  178. spin_unlock(&lockres->l_lock);
  179. wake_up(&lockres->l_event);
  180. }
  181. static void user_unlock_ast(void *opaque, enum dlm_status status)
  182. {
  183. struct user_lock_res *lockres = opaque;
  184. mlog(0, "UNLOCK AST called on lock %s\n", lockres->l_name);
  185. if (status != DLM_NORMAL)
  186. mlog(ML_ERROR, "Dlm returns status %d\n", status);
  187. spin_lock(&lockres->l_lock);
  188. if (lockres->l_flags & USER_LOCK_IN_TEARDOWN)
  189. lockres->l_level = LKM_IVMODE;
  190. else {
  191. lockres->l_requested = LKM_IVMODE; /* cancel an
  192. * upconvert
  193. * request. */
  194. lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
  195. /* we want the unblock thread to look at it again
  196. * now. */
  197. __user_dlm_queue_lockres(lockres);
  198. }
  199. lockres->l_flags &= ~USER_LOCK_BUSY;
  200. spin_unlock(&lockres->l_lock);
  201. wake_up(&lockres->l_event);
  202. }
  203. static inline void user_dlm_drop_inode_ref(struct user_lock_res *lockres)
  204. {
  205. struct inode *inode;
  206. inode = user_dlm_inode_from_user_lockres(lockres);
  207. iput(inode);
  208. }
  209. static void user_dlm_unblock_lock(void *opaque)
  210. {
  211. int new_level, status;
  212. struct user_lock_res *lockres = (struct user_lock_res *) opaque;
  213. struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres);
  214. mlog(0, "processing lockres %s\n", lockres->l_name);
  215. spin_lock(&lockres->l_lock);
  216. BUG_ON(!(lockres->l_flags & USER_LOCK_BLOCKED));
  217. BUG_ON(!(lockres->l_flags & USER_LOCK_QUEUED));
  218. /* notice that we don't clear USER_LOCK_BLOCKED here. That's
  219. * for user_ast to do. */
  220. lockres->l_flags &= ~USER_LOCK_QUEUED;
  221. if (lockres->l_flags & USER_LOCK_IN_TEARDOWN) {
  222. mlog(0, "lock is in teardown so we do nothing\n");
  223. spin_unlock(&lockres->l_lock);
  224. goto drop_ref;
  225. }
  226. if (lockres->l_flags & USER_LOCK_BUSY) {
  227. mlog(0, "BUSY flag detected...\n");
  228. if (lockres->l_flags & USER_LOCK_IN_CANCEL) {
  229. spin_unlock(&lockres->l_lock);
  230. goto drop_ref;
  231. }
  232. lockres->l_flags |= USER_LOCK_IN_CANCEL;
  233. spin_unlock(&lockres->l_lock);
  234. status = dlmunlock(dlm,
  235. &lockres->l_lksb,
  236. LKM_CANCEL,
  237. user_unlock_ast,
  238. lockres);
  239. if (status == DLM_CANCELGRANT) {
  240. /* If we got this, then the ast was fired
  241. * before we could cancel. We cleanup our
  242. * state, and restart the function. */
  243. spin_lock(&lockres->l_lock);
  244. lockres->l_flags &= ~USER_LOCK_IN_CANCEL;
  245. spin_unlock(&lockres->l_lock);
  246. } else if (status != DLM_NORMAL)
  247. user_log_dlm_error("dlmunlock", status, lockres);
  248. goto drop_ref;
  249. }
  250. /* If there are still incompat holders, we can exit safely
  251. * without worrying about re-queueing this lock as that will
  252. * happen on the last call to user_cluster_unlock. */
  253. if ((lockres->l_blocking == LKM_EXMODE)
  254. && (lockres->l_ex_holders || lockres->l_ro_holders)) {
  255. spin_unlock(&lockres->l_lock);
  256. mlog(0, "can't downconvert for ex: ro = %u, ex = %u\n",
  257. lockres->l_ro_holders, lockres->l_ex_holders);
  258. goto drop_ref;
  259. }
  260. if ((lockres->l_blocking == LKM_PRMODE)
  261. && lockres->l_ex_holders) {
  262. spin_unlock(&lockres->l_lock);
  263. mlog(0, "can't downconvert for pr: ex = %u\n",
  264. lockres->l_ex_holders);
  265. goto drop_ref;
  266. }
  267. /* yay, we can downconvert now. */
  268. new_level = user_highest_compat_lock_level(lockres->l_blocking);
  269. lockres->l_requested = new_level;
  270. lockres->l_flags |= USER_LOCK_BUSY;
  271. mlog(0, "Downconvert lock from %d to %d\n",
  272. lockres->l_level, new_level);
  273. spin_unlock(&lockres->l_lock);
  274. /* need lock downconvert request now... */
  275. status = dlmlock(dlm,
  276. new_level,
  277. &lockres->l_lksb,
  278. LKM_CONVERT|LKM_VALBLK,
  279. lockres->l_name,
  280. user_ast,
  281. lockres,
  282. user_bast);
  283. if (status != DLM_NORMAL) {
  284. user_log_dlm_error("dlmlock", status, lockres);
  285. user_recover_from_dlm_error(lockres);
  286. }
  287. drop_ref:
  288. user_dlm_drop_inode_ref(lockres);
  289. }
  290. static inline void user_dlm_inc_holders(struct user_lock_res *lockres,
  291. int level)
  292. {
  293. switch(level) {
  294. case LKM_EXMODE:
  295. lockres->l_ex_holders++;
  296. break;
  297. case LKM_PRMODE:
  298. lockres->l_ro_holders++;
  299. break;
  300. default:
  301. BUG();
  302. }
  303. }
  304. /* predict what lock level we'll be dropping down to on behalf
  305. * of another node, and return true if the currently wanted
  306. * level will be compatible with it. */
  307. static inline int
  308. user_may_continue_on_blocked_lock(struct user_lock_res *lockres,
  309. int wanted)
  310. {
  311. BUG_ON(!(lockres->l_flags & USER_LOCK_BLOCKED));
  312. return wanted <= user_highest_compat_lock_level(lockres->l_blocking);
  313. }
  314. int user_dlm_cluster_lock(struct user_lock_res *lockres,
  315. int level,
  316. int lkm_flags)
  317. {
  318. int status, local_flags;
  319. struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres);
  320. if (level != LKM_EXMODE &&
  321. level != LKM_PRMODE) {
  322. mlog(ML_ERROR, "lockres %s: invalid request!\n",
  323. lockres->l_name);
  324. status = -EINVAL;
  325. goto bail;
  326. }
  327. mlog(0, "lockres %s: asking for %s lock, passed flags = 0x%x\n",
  328. lockres->l_name,
  329. (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE",
  330. lkm_flags);
  331. again:
  332. if (signal_pending(current)) {
  333. status = -ERESTARTSYS;
  334. goto bail;
  335. }
  336. spin_lock(&lockres->l_lock);
  337. /* We only compare against the currently granted level
  338. * here. If the lock is blocked waiting on a downconvert,
  339. * we'll get caught below. */
  340. if ((lockres->l_flags & USER_LOCK_BUSY) &&
  341. (level > lockres->l_level)) {
  342. /* is someone sitting in dlm_lock? If so, wait on
  343. * them. */
  344. spin_unlock(&lockres->l_lock);
  345. user_wait_on_busy_lock(lockres);
  346. goto again;
  347. }
  348. if ((lockres->l_flags & USER_LOCK_BLOCKED) &&
  349. (!user_may_continue_on_blocked_lock(lockres, level))) {
  350. /* is the lock is currently blocked on behalf of
  351. * another node */
  352. spin_unlock(&lockres->l_lock);
  353. user_wait_on_blocked_lock(lockres);
  354. goto again;
  355. }
  356. if (level > lockres->l_level) {
  357. local_flags = lkm_flags | LKM_VALBLK;
  358. if (lockres->l_level != LKM_IVMODE)
  359. local_flags |= LKM_CONVERT;
  360. lockres->l_requested = level;
  361. lockres->l_flags |= USER_LOCK_BUSY;
  362. spin_unlock(&lockres->l_lock);
  363. BUG_ON(level == LKM_IVMODE);
  364. BUG_ON(level == LKM_NLMODE);
  365. mlog(0, "lock %s, get lock from %d to level = %d\n",
  366. lockres->l_name, lockres->l_level, level);
  367. /* call dlm_lock to upgrade lock now */
  368. status = dlmlock(dlm,
  369. level,
  370. &lockres->l_lksb,
  371. local_flags,
  372. lockres->l_name,
  373. user_ast,
  374. lockres,
  375. user_bast);
  376. if (status != DLM_NORMAL) {
  377. if ((lkm_flags & LKM_NOQUEUE) &&
  378. (status == DLM_NOTQUEUED))
  379. status = -EAGAIN;
  380. else {
  381. user_log_dlm_error("dlmlock", status, lockres);
  382. status = -EINVAL;
  383. }
  384. user_recover_from_dlm_error(lockres);
  385. goto bail;
  386. }
  387. mlog(0, "lock %s, successfull return from dlmlock\n",
  388. lockres->l_name);
  389. user_wait_on_busy_lock(lockres);
  390. goto again;
  391. }
  392. user_dlm_inc_holders(lockres, level);
  393. spin_unlock(&lockres->l_lock);
  394. mlog(0, "lockres %s: Got %s lock!\n", lockres->l_name,
  395. (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE");
  396. status = 0;
  397. bail:
  398. return status;
  399. }
  400. static inline void user_dlm_dec_holders(struct user_lock_res *lockres,
  401. int level)
  402. {
  403. switch(level) {
  404. case LKM_EXMODE:
  405. BUG_ON(!lockres->l_ex_holders);
  406. lockres->l_ex_holders--;
  407. break;
  408. case LKM_PRMODE:
  409. BUG_ON(!lockres->l_ro_holders);
  410. lockres->l_ro_holders--;
  411. break;
  412. default:
  413. BUG();
  414. }
  415. }
  416. void user_dlm_cluster_unlock(struct user_lock_res *lockres,
  417. int level)
  418. {
  419. if (level != LKM_EXMODE &&
  420. level != LKM_PRMODE) {
  421. mlog(ML_ERROR, "lockres %s: invalid request!\n", lockres->l_name);
  422. return;
  423. }
  424. mlog(0, "lockres %s: dropping %s lock\n", lockres->l_name,
  425. (level == LKM_EXMODE) ? "LKM_EXMODE" : "LKM_PRMODE");
  426. spin_lock(&lockres->l_lock);
  427. user_dlm_dec_holders(lockres, level);
  428. __user_dlm_cond_queue_lockres(lockres);
  429. spin_unlock(&lockres->l_lock);
  430. }
  431. void user_dlm_write_lvb(struct inode *inode,
  432. const char *val,
  433. unsigned int len)
  434. {
  435. struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
  436. char *lvb = lockres->l_lksb.lvb;
  437. BUG_ON(len > DLM_LVB_LEN);
  438. spin_lock(&lockres->l_lock);
  439. BUG_ON(lockres->l_level < LKM_EXMODE);
  440. memcpy(lvb, val, len);
  441. spin_unlock(&lockres->l_lock);
  442. }
  443. void user_dlm_read_lvb(struct inode *inode,
  444. char *val,
  445. unsigned int len)
  446. {
  447. struct user_lock_res *lockres = &DLMFS_I(inode)->ip_lockres;
  448. char *lvb = lockres->l_lksb.lvb;
  449. BUG_ON(len > DLM_LVB_LEN);
  450. spin_lock(&lockres->l_lock);
  451. BUG_ON(lockres->l_level < LKM_PRMODE);
  452. memcpy(val, lvb, len);
  453. spin_unlock(&lockres->l_lock);
  454. }
  455. void user_dlm_lock_res_init(struct user_lock_res *lockres,
  456. struct dentry *dentry)
  457. {
  458. memset(lockres, 0, sizeof(*lockres));
  459. spin_lock_init(&lockres->l_lock);
  460. init_waitqueue_head(&lockres->l_event);
  461. lockres->l_level = LKM_IVMODE;
  462. lockres->l_requested = LKM_IVMODE;
  463. lockres->l_blocking = LKM_IVMODE;
  464. /* should have been checked before getting here. */
  465. BUG_ON(dentry->d_name.len >= USER_DLM_LOCK_ID_MAX_LEN);
  466. memcpy(lockres->l_name,
  467. dentry->d_name.name,
  468. dentry->d_name.len);
  469. }
  470. int user_dlm_destroy_lock(struct user_lock_res *lockres)
  471. {
  472. int status = -EBUSY;
  473. struct dlm_ctxt *dlm = dlm_ctxt_from_user_lockres(lockres);
  474. mlog(0, "asked to destroy %s\n", lockres->l_name);
  475. spin_lock(&lockres->l_lock);
  476. while (lockres->l_flags & USER_LOCK_BUSY) {
  477. spin_unlock(&lockres->l_lock);
  478. mlog(0, "lock %s is busy\n", lockres->l_name);
  479. user_wait_on_busy_lock(lockres);
  480. spin_lock(&lockres->l_lock);
  481. }
  482. if (lockres->l_ro_holders || lockres->l_ex_holders) {
  483. spin_unlock(&lockres->l_lock);
  484. mlog(0, "lock %s has holders\n", lockres->l_name);
  485. goto bail;
  486. }
  487. status = 0;
  488. if (!(lockres->l_flags & USER_LOCK_ATTACHED)) {
  489. spin_unlock(&lockres->l_lock);
  490. mlog(0, "lock %s is not attached\n", lockres->l_name);
  491. goto bail;
  492. }
  493. lockres->l_flags &= ~USER_LOCK_ATTACHED;
  494. lockres->l_flags |= USER_LOCK_BUSY;
  495. lockres->l_flags |= USER_LOCK_IN_TEARDOWN;
  496. spin_unlock(&lockres->l_lock);
  497. mlog(0, "unlocking lockres %s\n", lockres->l_name);
  498. status = dlmunlock(dlm,
  499. &lockres->l_lksb,
  500. LKM_VALBLK,
  501. user_unlock_ast,
  502. lockres);
  503. if (status != DLM_NORMAL) {
  504. user_log_dlm_error("dlmunlock", status, lockres);
  505. status = -EINVAL;
  506. goto bail;
  507. }
  508. user_wait_on_busy_lock(lockres);
  509. status = 0;
  510. bail:
  511. return status;
  512. }
  513. struct dlm_ctxt *user_dlm_register_context(struct qstr *name)
  514. {
  515. struct dlm_ctxt *dlm;
  516. u32 dlm_key;
  517. char *domain;
  518. domain = kmalloc(name->len + 1, GFP_KERNEL);
  519. if (!domain) {
  520. mlog_errno(-ENOMEM);
  521. return ERR_PTR(-ENOMEM);
  522. }
  523. dlm_key = crc32_le(0, name->name, name->len);
  524. snprintf(domain, name->len + 1, "%.*s", name->len, name->name);
  525. dlm = dlm_register_domain(domain, dlm_key);
  526. if (IS_ERR(dlm))
  527. mlog_errno(PTR_ERR(dlm));
  528. kfree(domain);
  529. return dlm;
  530. }
  531. void user_dlm_unregister_context(struct dlm_ctxt *dlm)
  532. {
  533. dlm_unregister_domain(dlm);
  534. }