lock_dlm.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright 2004-2011 Red Hat, Inc.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/dlm.h>
  11. #include <linux/slab.h>
  12. #include <linux/types.h>
  13. #include <linux/delay.h>
  14. #include <linux/gfs2_ondisk.h>
  15. #include "incore.h"
  16. #include "glock.h"
  17. #include "util.h"
  18. #include "sys.h"
  19. extern struct workqueue_struct *gfs2_control_wq;
  20. static void gdlm_ast(void *arg)
  21. {
  22. struct gfs2_glock *gl = arg;
  23. unsigned ret = gl->gl_state;
  24. BUG_ON(gl->gl_lksb.sb_flags & DLM_SBF_DEMOTED);
  25. if (gl->gl_lksb.sb_flags & DLM_SBF_VALNOTVALID)
  26. memset(gl->gl_lvb, 0, GDLM_LVB_SIZE);
  27. switch (gl->gl_lksb.sb_status) {
  28. case -DLM_EUNLOCK: /* Unlocked, so glock can be freed */
  29. gfs2_glock_free(gl);
  30. return;
  31. case -DLM_ECANCEL: /* Cancel while getting lock */
  32. ret |= LM_OUT_CANCELED;
  33. goto out;
  34. case -EAGAIN: /* Try lock fails */
  35. case -EDEADLK: /* Deadlock detected */
  36. goto out;
  37. case -ETIMEDOUT: /* Canceled due to timeout */
  38. ret |= LM_OUT_ERROR;
  39. goto out;
  40. case 0: /* Success */
  41. break;
  42. default: /* Something unexpected */
  43. BUG();
  44. }
  45. ret = gl->gl_req;
  46. if (gl->gl_lksb.sb_flags & DLM_SBF_ALTMODE) {
  47. if (gl->gl_req == LM_ST_SHARED)
  48. ret = LM_ST_DEFERRED;
  49. else if (gl->gl_req == LM_ST_DEFERRED)
  50. ret = LM_ST_SHARED;
  51. else
  52. BUG();
  53. }
  54. set_bit(GLF_INITIAL, &gl->gl_flags);
  55. gfs2_glock_complete(gl, ret);
  56. return;
  57. out:
  58. if (!test_bit(GLF_INITIAL, &gl->gl_flags))
  59. gl->gl_lksb.sb_lkid = 0;
  60. gfs2_glock_complete(gl, ret);
  61. }
  62. static void gdlm_bast(void *arg, int mode)
  63. {
  64. struct gfs2_glock *gl = arg;
  65. switch (mode) {
  66. case DLM_LOCK_EX:
  67. gfs2_glock_cb(gl, LM_ST_UNLOCKED);
  68. break;
  69. case DLM_LOCK_CW:
  70. gfs2_glock_cb(gl, LM_ST_DEFERRED);
  71. break;
  72. case DLM_LOCK_PR:
  73. gfs2_glock_cb(gl, LM_ST_SHARED);
  74. break;
  75. default:
  76. printk(KERN_ERR "unknown bast mode %d", mode);
  77. BUG();
  78. }
  79. }
  80. /* convert gfs lock-state to dlm lock-mode */
  81. static int make_mode(const unsigned int lmstate)
  82. {
  83. switch (lmstate) {
  84. case LM_ST_UNLOCKED:
  85. return DLM_LOCK_NL;
  86. case LM_ST_EXCLUSIVE:
  87. return DLM_LOCK_EX;
  88. case LM_ST_DEFERRED:
  89. return DLM_LOCK_CW;
  90. case LM_ST_SHARED:
  91. return DLM_LOCK_PR;
  92. }
  93. printk(KERN_ERR "unknown LM state %d", lmstate);
  94. BUG();
  95. return -1;
  96. }
  97. static u32 make_flags(const u32 lkid, const unsigned int gfs_flags,
  98. const int req)
  99. {
  100. u32 lkf = 0;
  101. if (gfs_flags & LM_FLAG_TRY)
  102. lkf |= DLM_LKF_NOQUEUE;
  103. if (gfs_flags & LM_FLAG_TRY_1CB) {
  104. lkf |= DLM_LKF_NOQUEUE;
  105. lkf |= DLM_LKF_NOQUEUEBAST;
  106. }
  107. if (gfs_flags & LM_FLAG_PRIORITY) {
  108. lkf |= DLM_LKF_NOORDER;
  109. lkf |= DLM_LKF_HEADQUE;
  110. }
  111. if (gfs_flags & LM_FLAG_ANY) {
  112. if (req == DLM_LOCK_PR)
  113. lkf |= DLM_LKF_ALTCW;
  114. else if (req == DLM_LOCK_CW)
  115. lkf |= DLM_LKF_ALTPR;
  116. else
  117. BUG();
  118. }
  119. if (lkid != 0)
  120. lkf |= DLM_LKF_CONVERT;
  121. lkf |= DLM_LKF_VALBLK;
  122. return lkf;
  123. }
  124. static int gdlm_lock(struct gfs2_glock *gl, unsigned int req_state,
  125. unsigned int flags)
  126. {
  127. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  128. int req;
  129. u32 lkf;
  130. req = make_mode(req_state);
  131. lkf = make_flags(gl->gl_lksb.sb_lkid, flags, req);
  132. /*
  133. * Submit the actual lock request.
  134. */
  135. return dlm_lock(ls->ls_dlm, req, &gl->gl_lksb, lkf, gl->gl_strname,
  136. GDLM_STRNAME_BYTES - 1, 0, gdlm_ast, gl, gdlm_bast);
  137. }
  138. static void gdlm_put_lock(struct gfs2_glock *gl)
  139. {
  140. struct gfs2_sbd *sdp = gl->gl_sbd;
  141. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  142. int error;
  143. if (gl->gl_lksb.sb_lkid == 0) {
  144. gfs2_glock_free(gl);
  145. return;
  146. }
  147. error = dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_VALBLK,
  148. NULL, gl);
  149. if (error) {
  150. printk(KERN_ERR "gdlm_unlock %x,%llx err=%d\n",
  151. gl->gl_name.ln_type,
  152. (unsigned long long)gl->gl_name.ln_number, error);
  153. return;
  154. }
  155. }
  156. static void gdlm_cancel(struct gfs2_glock *gl)
  157. {
  158. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  159. dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_CANCEL, NULL, gl);
  160. }
  161. /*
  162. * dlm/gfs2 recovery coordination using dlm_recover callbacks
  163. *
  164. * 1. dlm_controld sees lockspace members change
  165. * 2. dlm_controld blocks dlm-kernel locking activity
  166. * 3. dlm_controld within dlm-kernel notifies gfs2 (recover_prep)
  167. * 4. dlm_controld starts and finishes its own user level recovery
  168. * 5. dlm_controld starts dlm-kernel dlm_recoverd to do kernel recovery
  169. * 6. dlm_recoverd notifies gfs2 of failed nodes (recover_slot)
  170. * 7. dlm_recoverd does its own lock recovery
  171. * 8. dlm_recoverd unblocks dlm-kernel locking activity
  172. * 9. dlm_recoverd notifies gfs2 when done (recover_done with new generation)
  173. * 10. gfs2_control updates control_lock lvb with new generation and jid bits
  174. * 11. gfs2_control enqueues journals for gfs2_recover to recover (maybe none)
  175. * 12. gfs2_recover dequeues and recovers journals of failed nodes
  176. * 13. gfs2_recover provides recovery results to gfs2_control (recovery_result)
  177. * 14. gfs2_control updates control_lock lvb jid bits for recovered journals
  178. * 15. gfs2_control unblocks normal locking when all journals are recovered
  179. *
  180. * - failures during recovery
  181. *
  182. * recover_prep() may set BLOCK_LOCKS (step 3) again before gfs2_control
  183. * clears BLOCK_LOCKS (step 15), e.g. another node fails while still
  184. * recovering for a prior failure. gfs2_control needs a way to detect
  185. * this so it can leave BLOCK_LOCKS set in step 15. This is managed using
  186. * the recover_block and recover_start values.
  187. *
  188. * recover_done() provides a new lockspace generation number each time it
  189. * is called (step 9). This generation number is saved as recover_start.
  190. * When recover_prep() is called, it sets BLOCK_LOCKS and sets
  191. * recover_block = recover_start. So, while recover_block is equal to
  192. * recover_start, BLOCK_LOCKS should remain set. (recover_spin must
  193. * be held around the BLOCK_LOCKS/recover_block/recover_start logic.)
  194. *
  195. * - more specific gfs2 steps in sequence above
  196. *
  197. * 3. recover_prep sets BLOCK_LOCKS and sets recover_block = recover_start
  198. * 6. recover_slot records any failed jids (maybe none)
  199. * 9. recover_done sets recover_start = new generation number
  200. * 10. gfs2_control sets control_lock lvb = new gen + bits for failed jids
  201. * 12. gfs2_recover does journal recoveries for failed jids identified above
  202. * 14. gfs2_control clears control_lock lvb bits for recovered jids
  203. * 15. gfs2_control checks if recover_block == recover_start (step 3 occured
  204. * again) then do nothing, otherwise if recover_start > recover_block
  205. * then clear BLOCK_LOCKS.
  206. *
  207. * - parallel recovery steps across all nodes
  208. *
  209. * All nodes attempt to update the control_lock lvb with the new generation
  210. * number and jid bits, but only the first to get the control_lock EX will
  211. * do so; others will see that it's already done (lvb already contains new
  212. * generation number.)
  213. *
  214. * . All nodes get the same recover_prep/recover_slot/recover_done callbacks
  215. * . All nodes attempt to set control_lock lvb gen + bits for the new gen
  216. * . One node gets control_lock first and writes the lvb, others see it's done
  217. * . All nodes attempt to recover jids for which they see control_lock bits set
  218. * . One node succeeds for a jid, and that one clears the jid bit in the lvb
  219. * . All nodes will eventually see all lvb bits clear and unblock locks
  220. *
  221. * - is there a problem with clearing an lvb bit that should be set
  222. * and missing a journal recovery?
  223. *
  224. * 1. jid fails
  225. * 2. lvb bit set for step 1
  226. * 3. jid recovered for step 1
  227. * 4. jid taken again (new mount)
  228. * 5. jid fails (for step 4)
  229. * 6. lvb bit set for step 5 (will already be set)
  230. * 7. lvb bit cleared for step 3
  231. *
  232. * This is not a problem because the failure in step 5 does not
  233. * require recovery, because the mount in step 4 could not have
  234. * progressed far enough to unblock locks and access the fs. The
  235. * control_mount() function waits for all recoveries to be complete
  236. * for the latest lockspace generation before ever unblocking locks
  237. * and returning. The mount in step 4 waits until the recovery in
  238. * step 1 is done.
  239. *
  240. * - special case of first mounter: first node to mount the fs
  241. *
  242. * The first node to mount a gfs2 fs needs to check all the journals
  243. * and recover any that need recovery before other nodes are allowed
  244. * to mount the fs. (Others may begin mounting, but they must wait
  245. * for the first mounter to be done before taking locks on the fs
  246. * or accessing the fs.) This has two parts:
  247. *
  248. * 1. The mounted_lock tells a node it's the first to mount the fs.
  249. * Each node holds the mounted_lock in PR while it's mounted.
  250. * Each node tries to acquire the mounted_lock in EX when it mounts.
  251. * If a node is granted the mounted_lock EX it means there are no
  252. * other mounted nodes (no PR locks exist), and it is the first mounter.
  253. * The mounted_lock is demoted to PR when first recovery is done, so
  254. * others will fail to get an EX lock, but will get a PR lock.
  255. *
  256. * 2. The control_lock blocks others in control_mount() while the first
  257. * mounter is doing first mount recovery of all journals.
  258. * A mounting node needs to acquire control_lock in EX mode before
  259. * it can proceed. The first mounter holds control_lock in EX while doing
  260. * the first mount recovery, blocking mounts from other nodes, then demotes
  261. * control_lock to NL when it's done (others_may_mount/first_done),
  262. * allowing other nodes to continue mounting.
  263. *
  264. * first mounter:
  265. * control_lock EX/NOQUEUE success
  266. * mounted_lock EX/NOQUEUE success (no other PR, so no other mounters)
  267. * set first=1
  268. * do first mounter recovery
  269. * mounted_lock EX->PR
  270. * control_lock EX->NL, write lvb generation
  271. *
  272. * other mounter:
  273. * control_lock EX/NOQUEUE success (if fail -EAGAIN, retry)
  274. * mounted_lock EX/NOQUEUE fail -EAGAIN (expected due to other mounters PR)
  275. * mounted_lock PR/NOQUEUE success
  276. * read lvb generation
  277. * control_lock EX->NL
  278. * set first=0
  279. *
  280. * - mount during recovery
  281. *
  282. * If a node mounts while others are doing recovery (not first mounter),
  283. * the mounting node will get its initial recover_done() callback without
  284. * having seen any previous failures/callbacks.
  285. *
  286. * It must wait for all recoveries preceding its mount to be finished
  287. * before it unblocks locks. It does this by repeating the "other mounter"
  288. * steps above until the lvb generation number is >= its mount generation
  289. * number (from initial recover_done) and all lvb bits are clear.
  290. *
  291. * - control_lock lvb format
  292. *
  293. * 4 bytes generation number: the latest dlm lockspace generation number
  294. * from recover_done callback. Indicates the jid bitmap has been updated
  295. * to reflect all slot failures through that generation.
  296. * 4 bytes unused.
  297. * GDLM_LVB_SIZE-8 bytes of jid bit map. If bit N is set, it indicates
  298. * that jid N needs recovery.
  299. */
  300. #define JID_BITMAP_OFFSET 8 /* 4 byte generation number + 4 byte unused */
  301. static void control_lvb_read(struct lm_lockstruct *ls, uint32_t *lvb_gen,
  302. char *lvb_bits)
  303. {
  304. uint32_t gen;
  305. memcpy(lvb_bits, ls->ls_control_lvb, GDLM_LVB_SIZE);
  306. memcpy(&gen, lvb_bits, sizeof(uint32_t));
  307. *lvb_gen = le32_to_cpu(gen);
  308. }
  309. static void control_lvb_write(struct lm_lockstruct *ls, uint32_t lvb_gen,
  310. char *lvb_bits)
  311. {
  312. uint32_t gen;
  313. memcpy(ls->ls_control_lvb, lvb_bits, GDLM_LVB_SIZE);
  314. gen = cpu_to_le32(lvb_gen);
  315. memcpy(ls->ls_control_lvb, &gen, sizeof(uint32_t));
  316. }
  317. static int all_jid_bits_clear(char *lvb)
  318. {
  319. int i;
  320. for (i = JID_BITMAP_OFFSET; i < GDLM_LVB_SIZE; i++) {
  321. if (lvb[i])
  322. return 0;
  323. }
  324. return 1;
  325. }
  326. static void sync_wait_cb(void *arg)
  327. {
  328. struct lm_lockstruct *ls = arg;
  329. complete(&ls->ls_sync_wait);
  330. }
  331. static int sync_unlock(struct gfs2_sbd *sdp, struct dlm_lksb *lksb, char *name)
  332. {
  333. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  334. int error;
  335. error = dlm_unlock(ls->ls_dlm, lksb->sb_lkid, 0, lksb, ls);
  336. if (error) {
  337. fs_err(sdp, "%s lkid %x error %d\n",
  338. name, lksb->sb_lkid, error);
  339. return error;
  340. }
  341. wait_for_completion(&ls->ls_sync_wait);
  342. if (lksb->sb_status != -DLM_EUNLOCK) {
  343. fs_err(sdp, "%s lkid %x status %d\n",
  344. name, lksb->sb_lkid, lksb->sb_status);
  345. return -1;
  346. }
  347. return 0;
  348. }
  349. static int sync_lock(struct gfs2_sbd *sdp, int mode, uint32_t flags,
  350. unsigned int num, struct dlm_lksb *lksb, char *name)
  351. {
  352. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  353. char strname[GDLM_STRNAME_BYTES];
  354. int error, status;
  355. memset(strname, 0, GDLM_STRNAME_BYTES);
  356. snprintf(strname, GDLM_STRNAME_BYTES, "%8x%16x", LM_TYPE_NONDISK, num);
  357. error = dlm_lock(ls->ls_dlm, mode, lksb, flags,
  358. strname, GDLM_STRNAME_BYTES - 1,
  359. 0, sync_wait_cb, ls, NULL);
  360. if (error) {
  361. fs_err(sdp, "%s lkid %x flags %x mode %d error %d\n",
  362. name, lksb->sb_lkid, flags, mode, error);
  363. return error;
  364. }
  365. wait_for_completion(&ls->ls_sync_wait);
  366. status = lksb->sb_status;
  367. if (status && status != -EAGAIN) {
  368. fs_err(sdp, "%s lkid %x flags %x mode %d status %d\n",
  369. name, lksb->sb_lkid, flags, mode, status);
  370. }
  371. return status;
  372. }
  373. static int mounted_unlock(struct gfs2_sbd *sdp)
  374. {
  375. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  376. return sync_unlock(sdp, &ls->ls_mounted_lksb, "mounted_lock");
  377. }
  378. static int mounted_lock(struct gfs2_sbd *sdp, int mode, uint32_t flags)
  379. {
  380. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  381. return sync_lock(sdp, mode, flags, GFS2_MOUNTED_LOCK,
  382. &ls->ls_mounted_lksb, "mounted_lock");
  383. }
  384. static int control_unlock(struct gfs2_sbd *sdp)
  385. {
  386. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  387. return sync_unlock(sdp, &ls->ls_control_lksb, "control_lock");
  388. }
  389. static int control_lock(struct gfs2_sbd *sdp, int mode, uint32_t flags)
  390. {
  391. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  392. return sync_lock(sdp, mode, flags, GFS2_CONTROL_LOCK,
  393. &ls->ls_control_lksb, "control_lock");
  394. }
  395. static void gfs2_control_func(struct work_struct *work)
  396. {
  397. struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_control_work.work);
  398. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  399. char lvb_bits[GDLM_LVB_SIZE];
  400. uint32_t block_gen, start_gen, lvb_gen, flags;
  401. int recover_set = 0;
  402. int write_lvb = 0;
  403. int recover_size;
  404. int i, error;
  405. spin_lock(&ls->ls_recover_spin);
  406. /*
  407. * No MOUNT_DONE means we're still mounting; control_mount()
  408. * will set this flag, after which this thread will take over
  409. * all further clearing of BLOCK_LOCKS.
  410. *
  411. * FIRST_MOUNT means this node is doing first mounter recovery,
  412. * for which recovery control is handled by
  413. * control_mount()/control_first_done(), not this thread.
  414. */
  415. if (!test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  416. test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  417. spin_unlock(&ls->ls_recover_spin);
  418. return;
  419. }
  420. block_gen = ls->ls_recover_block;
  421. start_gen = ls->ls_recover_start;
  422. spin_unlock(&ls->ls_recover_spin);
  423. /*
  424. * Equal block_gen and start_gen implies we are between
  425. * recover_prep and recover_done callbacks, which means
  426. * dlm recovery is in progress and dlm locking is blocked.
  427. * There's no point trying to do any work until recover_done.
  428. */
  429. if (block_gen == start_gen)
  430. return;
  431. /*
  432. * Propagate recover_submit[] and recover_result[] to lvb:
  433. * dlm_recoverd adds to recover_submit[] jids needing recovery
  434. * gfs2_recover adds to recover_result[] journal recovery results
  435. *
  436. * set lvb bit for jids in recover_submit[] if the lvb has not
  437. * yet been updated for the generation of the failure
  438. *
  439. * clear lvb bit for jids in recover_result[] if the result of
  440. * the journal recovery is SUCCESS
  441. */
  442. error = control_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  443. if (error) {
  444. fs_err(sdp, "control lock EX error %d\n", error);
  445. return;
  446. }
  447. control_lvb_read(ls, &lvb_gen, lvb_bits);
  448. spin_lock(&ls->ls_recover_spin);
  449. if (block_gen != ls->ls_recover_block ||
  450. start_gen != ls->ls_recover_start) {
  451. fs_info(sdp, "recover generation %u block1 %u %u\n",
  452. start_gen, block_gen, ls->ls_recover_block);
  453. spin_unlock(&ls->ls_recover_spin);
  454. control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  455. return;
  456. }
  457. recover_size = ls->ls_recover_size;
  458. if (lvb_gen <= start_gen) {
  459. /*
  460. * Clear lvb bits for jids we've successfully recovered.
  461. * Because all nodes attempt to recover failed journals,
  462. * a journal can be recovered multiple times successfully
  463. * in succession. Only the first will really do recovery,
  464. * the others find it clean, but still report a successful
  465. * recovery. So, another node may have already recovered
  466. * the jid and cleared the lvb bit for it.
  467. */
  468. for (i = 0; i < recover_size; i++) {
  469. if (ls->ls_recover_result[i] != LM_RD_SUCCESS)
  470. continue;
  471. ls->ls_recover_result[i] = 0;
  472. if (!test_bit_le(i, lvb_bits + JID_BITMAP_OFFSET))
  473. continue;
  474. __clear_bit_le(i, lvb_bits + JID_BITMAP_OFFSET);
  475. write_lvb = 1;
  476. }
  477. }
  478. if (lvb_gen == start_gen) {
  479. /*
  480. * Failed slots before start_gen are already set in lvb.
  481. */
  482. for (i = 0; i < recover_size; i++) {
  483. if (!ls->ls_recover_submit[i])
  484. continue;
  485. if (ls->ls_recover_submit[i] < lvb_gen)
  486. ls->ls_recover_submit[i] = 0;
  487. }
  488. } else if (lvb_gen < start_gen) {
  489. /*
  490. * Failed slots before start_gen are not yet set in lvb.
  491. */
  492. for (i = 0; i < recover_size; i++) {
  493. if (!ls->ls_recover_submit[i])
  494. continue;
  495. if (ls->ls_recover_submit[i] < start_gen) {
  496. ls->ls_recover_submit[i] = 0;
  497. __set_bit_le(i, lvb_bits + JID_BITMAP_OFFSET);
  498. }
  499. }
  500. /* even if there are no bits to set, we need to write the
  501. latest generation to the lvb */
  502. write_lvb = 1;
  503. } else {
  504. /*
  505. * we should be getting a recover_done() for lvb_gen soon
  506. */
  507. }
  508. spin_unlock(&ls->ls_recover_spin);
  509. if (write_lvb) {
  510. control_lvb_write(ls, start_gen, lvb_bits);
  511. flags = DLM_LKF_CONVERT | DLM_LKF_VALBLK;
  512. } else {
  513. flags = DLM_LKF_CONVERT;
  514. }
  515. error = control_lock(sdp, DLM_LOCK_NL, flags);
  516. if (error) {
  517. fs_err(sdp, "control lock NL error %d\n", error);
  518. return;
  519. }
  520. /*
  521. * Everyone will see jid bits set in the lvb, run gfs2_recover_set(),
  522. * and clear a jid bit in the lvb if the recovery is a success.
  523. * Eventually all journals will be recovered, all jid bits will
  524. * be cleared in the lvb, and everyone will clear BLOCK_LOCKS.
  525. */
  526. for (i = 0; i < recover_size; i++) {
  527. if (test_bit_le(i, lvb_bits + JID_BITMAP_OFFSET)) {
  528. fs_info(sdp, "recover generation %u jid %d\n",
  529. start_gen, i);
  530. gfs2_recover_set(sdp, i);
  531. recover_set++;
  532. }
  533. }
  534. if (recover_set)
  535. return;
  536. /*
  537. * No more jid bits set in lvb, all recovery is done, unblock locks
  538. * (unless a new recover_prep callback has occured blocking locks
  539. * again while working above)
  540. */
  541. spin_lock(&ls->ls_recover_spin);
  542. if (ls->ls_recover_block == block_gen &&
  543. ls->ls_recover_start == start_gen) {
  544. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  545. spin_unlock(&ls->ls_recover_spin);
  546. fs_info(sdp, "recover generation %u done\n", start_gen);
  547. gfs2_glock_thaw(sdp);
  548. } else {
  549. fs_info(sdp, "recover generation %u block2 %u %u\n",
  550. start_gen, block_gen, ls->ls_recover_block);
  551. spin_unlock(&ls->ls_recover_spin);
  552. }
  553. }
  554. static int control_mount(struct gfs2_sbd *sdp)
  555. {
  556. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  557. char lvb_bits[GDLM_LVB_SIZE];
  558. uint32_t start_gen, block_gen, mount_gen, lvb_gen;
  559. int mounted_mode;
  560. int retries = 0;
  561. int error;
  562. memset(&ls->ls_mounted_lksb, 0, sizeof(struct dlm_lksb));
  563. memset(&ls->ls_control_lksb, 0, sizeof(struct dlm_lksb));
  564. memset(&ls->ls_control_lvb, 0, GDLM_LVB_SIZE);
  565. ls->ls_control_lksb.sb_lvbptr = ls->ls_control_lvb;
  566. init_completion(&ls->ls_sync_wait);
  567. set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  568. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_VALBLK);
  569. if (error) {
  570. fs_err(sdp, "control_mount control_lock NL error %d\n", error);
  571. return error;
  572. }
  573. error = mounted_lock(sdp, DLM_LOCK_NL, 0);
  574. if (error) {
  575. fs_err(sdp, "control_mount mounted_lock NL error %d\n", error);
  576. control_unlock(sdp);
  577. return error;
  578. }
  579. mounted_mode = DLM_LOCK_NL;
  580. restart:
  581. if (retries++ && signal_pending(current)) {
  582. error = -EINTR;
  583. goto fail;
  584. }
  585. /*
  586. * We always start with both locks in NL. control_lock is
  587. * demoted to NL below so we don't need to do it here.
  588. */
  589. if (mounted_mode != DLM_LOCK_NL) {
  590. error = mounted_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  591. if (error)
  592. goto fail;
  593. mounted_mode = DLM_LOCK_NL;
  594. }
  595. /*
  596. * Other nodes need to do some work in dlm recovery and gfs2_control
  597. * before the recover_done and control_lock will be ready for us below.
  598. * A delay here is not required but often avoids having to retry.
  599. */
  600. msleep_interruptible(500);
  601. /*
  602. * Acquire control_lock in EX and mounted_lock in either EX or PR.
  603. * control_lock lvb keeps track of any pending journal recoveries.
  604. * mounted_lock indicates if any other nodes have the fs mounted.
  605. */
  606. error = control_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE|DLM_LKF_VALBLK);
  607. if (error == -EAGAIN) {
  608. goto restart;
  609. } else if (error) {
  610. fs_err(sdp, "control_mount control_lock EX error %d\n", error);
  611. goto fail;
  612. }
  613. error = mounted_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE);
  614. if (!error) {
  615. mounted_mode = DLM_LOCK_EX;
  616. goto locks_done;
  617. } else if (error != -EAGAIN) {
  618. fs_err(sdp, "control_mount mounted_lock EX error %d\n", error);
  619. goto fail;
  620. }
  621. error = mounted_lock(sdp, DLM_LOCK_PR, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE);
  622. if (!error) {
  623. mounted_mode = DLM_LOCK_PR;
  624. goto locks_done;
  625. } else {
  626. /* not even -EAGAIN should happen here */
  627. fs_err(sdp, "control_mount mounted_lock PR error %d\n", error);
  628. goto fail;
  629. }
  630. locks_done:
  631. /*
  632. * If we got both locks above in EX, then we're the first mounter.
  633. * If not, then we need to wait for the control_lock lvb to be
  634. * updated by other mounted nodes to reflect our mount generation.
  635. *
  636. * In simple first mounter cases, first mounter will see zero lvb_gen,
  637. * but in cases where all existing nodes leave/fail before mounting
  638. * nodes finish control_mount, then all nodes will be mounting and
  639. * lvb_gen will be non-zero.
  640. */
  641. control_lvb_read(ls, &lvb_gen, lvb_bits);
  642. if (lvb_gen == 0xFFFFFFFF) {
  643. /* special value to force mount attempts to fail */
  644. fs_err(sdp, "control_mount control_lock disabled\n");
  645. error = -EINVAL;
  646. goto fail;
  647. }
  648. if (mounted_mode == DLM_LOCK_EX) {
  649. /* first mounter, keep both EX while doing first recovery */
  650. spin_lock(&ls->ls_recover_spin);
  651. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  652. set_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags);
  653. set_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  654. spin_unlock(&ls->ls_recover_spin);
  655. fs_info(sdp, "first mounter control generation %u\n", lvb_gen);
  656. return 0;
  657. }
  658. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  659. if (error)
  660. goto fail;
  661. /*
  662. * We are not first mounter, now we need to wait for the control_lock
  663. * lvb generation to be >= the generation from our first recover_done
  664. * and all lvb bits to be clear (no pending journal recoveries.)
  665. */
  666. if (!all_jid_bits_clear(lvb_bits)) {
  667. /* journals need recovery, wait until all are clear */
  668. fs_info(sdp, "control_mount wait for journal recovery\n");
  669. goto restart;
  670. }
  671. spin_lock(&ls->ls_recover_spin);
  672. block_gen = ls->ls_recover_block;
  673. start_gen = ls->ls_recover_start;
  674. mount_gen = ls->ls_recover_mount;
  675. if (lvb_gen < mount_gen) {
  676. /* wait for mounted nodes to update control_lock lvb to our
  677. generation, which might include new recovery bits set */
  678. fs_info(sdp, "control_mount wait1 block %u start %u mount %u "
  679. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  680. lvb_gen, ls->ls_recover_flags);
  681. spin_unlock(&ls->ls_recover_spin);
  682. goto restart;
  683. }
  684. if (lvb_gen != start_gen) {
  685. /* wait for mounted nodes to update control_lock lvb to the
  686. latest recovery generation */
  687. fs_info(sdp, "control_mount wait2 block %u start %u mount %u "
  688. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  689. lvb_gen, ls->ls_recover_flags);
  690. spin_unlock(&ls->ls_recover_spin);
  691. goto restart;
  692. }
  693. if (block_gen == start_gen) {
  694. /* dlm recovery in progress, wait for it to finish */
  695. fs_info(sdp, "control_mount wait3 block %u start %u mount %u "
  696. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  697. lvb_gen, ls->ls_recover_flags);
  698. spin_unlock(&ls->ls_recover_spin);
  699. goto restart;
  700. }
  701. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  702. set_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags);
  703. memset(ls->ls_recover_submit, 0, ls->ls_recover_size*sizeof(uint32_t));
  704. memset(ls->ls_recover_result, 0, ls->ls_recover_size*sizeof(uint32_t));
  705. spin_unlock(&ls->ls_recover_spin);
  706. return 0;
  707. fail:
  708. mounted_unlock(sdp);
  709. control_unlock(sdp);
  710. return error;
  711. }
  712. static int dlm_recovery_wait(void *word)
  713. {
  714. schedule();
  715. return 0;
  716. }
  717. static int control_first_done(struct gfs2_sbd *sdp)
  718. {
  719. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  720. char lvb_bits[GDLM_LVB_SIZE];
  721. uint32_t start_gen, block_gen;
  722. int error;
  723. restart:
  724. spin_lock(&ls->ls_recover_spin);
  725. start_gen = ls->ls_recover_start;
  726. block_gen = ls->ls_recover_block;
  727. if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags) ||
  728. !test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  729. !test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  730. /* sanity check, should not happen */
  731. fs_err(sdp, "control_first_done start %u block %u flags %lx\n",
  732. start_gen, block_gen, ls->ls_recover_flags);
  733. spin_unlock(&ls->ls_recover_spin);
  734. control_unlock(sdp);
  735. return -1;
  736. }
  737. if (start_gen == block_gen) {
  738. /*
  739. * Wait for the end of a dlm recovery cycle to switch from
  740. * first mounter recovery. We can ignore any recover_slot
  741. * callbacks between the recover_prep and next recover_done
  742. * because we are still the first mounter and any failed nodes
  743. * have not fully mounted, so they don't need recovery.
  744. */
  745. spin_unlock(&ls->ls_recover_spin);
  746. fs_info(sdp, "control_first_done wait gen %u\n", start_gen);
  747. wait_on_bit(&ls->ls_recover_flags, DFL_DLM_RECOVERY,
  748. dlm_recovery_wait, TASK_UNINTERRUPTIBLE);
  749. goto restart;
  750. }
  751. clear_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  752. set_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags);
  753. memset(ls->ls_recover_submit, 0, ls->ls_recover_size*sizeof(uint32_t));
  754. memset(ls->ls_recover_result, 0, ls->ls_recover_size*sizeof(uint32_t));
  755. spin_unlock(&ls->ls_recover_spin);
  756. memset(lvb_bits, 0, sizeof(lvb_bits));
  757. control_lvb_write(ls, start_gen, lvb_bits);
  758. error = mounted_lock(sdp, DLM_LOCK_PR, DLM_LKF_CONVERT);
  759. if (error)
  760. fs_err(sdp, "control_first_done mounted PR error %d\n", error);
  761. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  762. if (error)
  763. fs_err(sdp, "control_first_done control NL error %d\n", error);
  764. return error;
  765. }
  766. /*
  767. * Expand static jid arrays if necessary (by increments of RECOVER_SIZE_INC)
  768. * to accomodate the largest slot number. (NB dlm slot numbers start at 1,
  769. * gfs2 jids start at 0, so jid = slot - 1)
  770. */
  771. #define RECOVER_SIZE_INC 16
  772. static int set_recover_size(struct gfs2_sbd *sdp, struct dlm_slot *slots,
  773. int num_slots)
  774. {
  775. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  776. uint32_t *submit = NULL;
  777. uint32_t *result = NULL;
  778. uint32_t old_size, new_size;
  779. int i, max_jid;
  780. max_jid = 0;
  781. for (i = 0; i < num_slots; i++) {
  782. if (max_jid < slots[i].slot - 1)
  783. max_jid = slots[i].slot - 1;
  784. }
  785. old_size = ls->ls_recover_size;
  786. if (old_size >= max_jid + 1)
  787. return 0;
  788. new_size = old_size + RECOVER_SIZE_INC;
  789. submit = kzalloc(new_size * sizeof(uint32_t), GFP_NOFS);
  790. result = kzalloc(new_size * sizeof(uint32_t), GFP_NOFS);
  791. if (!submit || !result) {
  792. kfree(submit);
  793. kfree(result);
  794. return -ENOMEM;
  795. }
  796. spin_lock(&ls->ls_recover_spin);
  797. memcpy(submit, ls->ls_recover_submit, old_size * sizeof(uint32_t));
  798. memcpy(result, ls->ls_recover_result, old_size * sizeof(uint32_t));
  799. kfree(ls->ls_recover_submit);
  800. kfree(ls->ls_recover_result);
  801. ls->ls_recover_submit = submit;
  802. ls->ls_recover_result = result;
  803. ls->ls_recover_size = new_size;
  804. spin_unlock(&ls->ls_recover_spin);
  805. return 0;
  806. }
  807. static void free_recover_size(struct lm_lockstruct *ls)
  808. {
  809. kfree(ls->ls_recover_submit);
  810. kfree(ls->ls_recover_result);
  811. ls->ls_recover_submit = NULL;
  812. ls->ls_recover_result = NULL;
  813. ls->ls_recover_size = 0;
  814. }
  815. /* dlm calls before it does lock recovery */
  816. static void gdlm_recover_prep(void *arg)
  817. {
  818. struct gfs2_sbd *sdp = arg;
  819. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  820. spin_lock(&ls->ls_recover_spin);
  821. ls->ls_recover_block = ls->ls_recover_start;
  822. set_bit(DFL_DLM_RECOVERY, &ls->ls_recover_flags);
  823. if (!test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  824. test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  825. spin_unlock(&ls->ls_recover_spin);
  826. return;
  827. }
  828. set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  829. spin_unlock(&ls->ls_recover_spin);
  830. }
  831. /* dlm calls after recover_prep has been completed on all lockspace members;
  832. identifies slot/jid of failed member */
  833. static void gdlm_recover_slot(void *arg, struct dlm_slot *slot)
  834. {
  835. struct gfs2_sbd *sdp = arg;
  836. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  837. int jid = slot->slot - 1;
  838. spin_lock(&ls->ls_recover_spin);
  839. if (ls->ls_recover_size < jid + 1) {
  840. fs_err(sdp, "recover_slot jid %d gen %u short size %d",
  841. jid, ls->ls_recover_block, ls->ls_recover_size);
  842. spin_unlock(&ls->ls_recover_spin);
  843. return;
  844. }
  845. if (ls->ls_recover_submit[jid]) {
  846. fs_info(sdp, "recover_slot jid %d gen %u prev %u",
  847. jid, ls->ls_recover_block, ls->ls_recover_submit[jid]);
  848. }
  849. ls->ls_recover_submit[jid] = ls->ls_recover_block;
  850. spin_unlock(&ls->ls_recover_spin);
  851. }
  852. /* dlm calls after recover_slot and after it completes lock recovery */
  853. static void gdlm_recover_done(void *arg, struct dlm_slot *slots, int num_slots,
  854. int our_slot, uint32_t generation)
  855. {
  856. struct gfs2_sbd *sdp = arg;
  857. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  858. /* ensure the ls jid arrays are large enough */
  859. set_recover_size(sdp, slots, num_slots);
  860. spin_lock(&ls->ls_recover_spin);
  861. ls->ls_recover_start = generation;
  862. if (!ls->ls_recover_mount) {
  863. ls->ls_recover_mount = generation;
  864. ls->ls_jid = our_slot - 1;
  865. }
  866. if (!test_bit(DFL_UNMOUNT, &ls->ls_recover_flags))
  867. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work, 0);
  868. clear_bit(DFL_DLM_RECOVERY, &ls->ls_recover_flags);
  869. smp_mb__after_clear_bit();
  870. wake_up_bit(&ls->ls_recover_flags, DFL_DLM_RECOVERY);
  871. spin_unlock(&ls->ls_recover_spin);
  872. }
  873. /* gfs2_recover thread has a journal recovery result */
  874. static void gdlm_recovery_result(struct gfs2_sbd *sdp, unsigned int jid,
  875. unsigned int result)
  876. {
  877. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  878. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  879. return;
  880. /* don't care about the recovery of own journal during mount */
  881. if (jid == ls->ls_jid)
  882. return;
  883. spin_lock(&ls->ls_recover_spin);
  884. if (test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  885. spin_unlock(&ls->ls_recover_spin);
  886. return;
  887. }
  888. if (ls->ls_recover_size < jid + 1) {
  889. fs_err(sdp, "recovery_result jid %d short size %d",
  890. jid, ls->ls_recover_size);
  891. spin_unlock(&ls->ls_recover_spin);
  892. return;
  893. }
  894. fs_info(sdp, "recover jid %d result %s\n", jid,
  895. result == LM_RD_GAVEUP ? "busy" : "success");
  896. ls->ls_recover_result[jid] = result;
  897. /* GAVEUP means another node is recovering the journal; delay our
  898. next attempt to recover it, to give the other node a chance to
  899. finish before trying again */
  900. if (!test_bit(DFL_UNMOUNT, &ls->ls_recover_flags))
  901. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work,
  902. result == LM_RD_GAVEUP ? HZ : 0);
  903. spin_unlock(&ls->ls_recover_spin);
  904. }
  905. const struct dlm_lockspace_ops gdlm_lockspace_ops = {
  906. .recover_prep = gdlm_recover_prep,
  907. .recover_slot = gdlm_recover_slot,
  908. .recover_done = gdlm_recover_done,
  909. };
  910. static int gdlm_mount(struct gfs2_sbd *sdp, const char *table)
  911. {
  912. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  913. char cluster[GFS2_LOCKNAME_LEN];
  914. const char *fsname;
  915. uint32_t flags;
  916. int error, ops_result;
  917. /*
  918. * initialize everything
  919. */
  920. INIT_DELAYED_WORK(&sdp->sd_control_work, gfs2_control_func);
  921. spin_lock_init(&ls->ls_recover_spin);
  922. ls->ls_recover_flags = 0;
  923. ls->ls_recover_mount = 0;
  924. ls->ls_recover_start = 0;
  925. ls->ls_recover_block = 0;
  926. ls->ls_recover_size = 0;
  927. ls->ls_recover_submit = NULL;
  928. ls->ls_recover_result = NULL;
  929. error = set_recover_size(sdp, NULL, 0);
  930. if (error)
  931. goto fail;
  932. /*
  933. * prepare dlm_new_lockspace args
  934. */
  935. fsname = strchr(table, ':');
  936. if (!fsname) {
  937. fs_info(sdp, "no fsname found\n");
  938. error = -EINVAL;
  939. goto fail_free;
  940. }
  941. memset(cluster, 0, sizeof(cluster));
  942. memcpy(cluster, table, strlen(table) - strlen(fsname));
  943. fsname++;
  944. flags = DLM_LSFL_FS | DLM_LSFL_NEWEXCL;
  945. if (ls->ls_nodir)
  946. flags |= DLM_LSFL_NODIR;
  947. /*
  948. * create/join lockspace
  949. */
  950. error = dlm_new_lockspace(fsname, cluster, flags, GDLM_LVB_SIZE,
  951. &gdlm_lockspace_ops, sdp, &ops_result,
  952. &ls->ls_dlm);
  953. if (error) {
  954. fs_err(sdp, "dlm_new_lockspace error %d\n", error);
  955. goto fail_free;
  956. }
  957. if (ops_result < 0) {
  958. /*
  959. * dlm does not support ops callbacks,
  960. * old dlm_controld/gfs_controld are used, try without ops.
  961. */
  962. fs_info(sdp, "dlm lockspace ops not used\n");
  963. free_recover_size(ls);
  964. set_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags);
  965. return 0;
  966. }
  967. if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags)) {
  968. fs_err(sdp, "dlm lockspace ops disallow jid preset\n");
  969. error = -EINVAL;
  970. goto fail_release;
  971. }
  972. /*
  973. * control_mount() uses control_lock to determine first mounter,
  974. * and for later mounts, waits for any recoveries to be cleared.
  975. */
  976. error = control_mount(sdp);
  977. if (error) {
  978. fs_err(sdp, "mount control error %d\n", error);
  979. goto fail_release;
  980. }
  981. ls->ls_first = !!test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  982. clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
  983. smp_mb__after_clear_bit();
  984. wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
  985. return 0;
  986. fail_release:
  987. dlm_release_lockspace(ls->ls_dlm, 2);
  988. fail_free:
  989. free_recover_size(ls);
  990. fail:
  991. return error;
  992. }
  993. static void gdlm_first_done(struct gfs2_sbd *sdp)
  994. {
  995. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  996. int error;
  997. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  998. return;
  999. error = control_first_done(sdp);
  1000. if (error)
  1001. fs_err(sdp, "mount first_done error %d\n", error);
  1002. }
  1003. static void gdlm_unmount(struct gfs2_sbd *sdp)
  1004. {
  1005. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1006. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  1007. goto release;
  1008. /* wait for gfs2_control_wq to be done with this mount */
  1009. spin_lock(&ls->ls_recover_spin);
  1010. set_bit(DFL_UNMOUNT, &ls->ls_recover_flags);
  1011. spin_unlock(&ls->ls_recover_spin);
  1012. flush_delayed_work_sync(&sdp->sd_control_work);
  1013. /* mounted_lock and control_lock will be purged in dlm recovery */
  1014. release:
  1015. if (ls->ls_dlm) {
  1016. dlm_release_lockspace(ls->ls_dlm, 2);
  1017. ls->ls_dlm = NULL;
  1018. }
  1019. free_recover_size(ls);
  1020. }
  1021. static const match_table_t dlm_tokens = {
  1022. { Opt_jid, "jid=%d"},
  1023. { Opt_id, "id=%d"},
  1024. { Opt_first, "first=%d"},
  1025. { Opt_nodir, "nodir=%d"},
  1026. { Opt_err, NULL },
  1027. };
  1028. const struct lm_lockops gfs2_dlm_ops = {
  1029. .lm_proto_name = "lock_dlm",
  1030. .lm_mount = gdlm_mount,
  1031. .lm_first_done = gdlm_first_done,
  1032. .lm_recovery_result = gdlm_recovery_result,
  1033. .lm_unmount = gdlm_unmount,
  1034. .lm_put_lock = gdlm_put_lock,
  1035. .lm_lock = gdlm_lock,
  1036. .lm_cancel = gdlm_cancel,
  1037. .lm_tokens = &dlm_tokens,
  1038. };