lock_dlm.c 39 KB

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