lock_dlm.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334
  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 error;
  251. if (gl->gl_lksb.sb_lkid == 0) {
  252. gfs2_glock_free(gl);
  253. return;
  254. }
  255. clear_bit(GLF_BLOCKING, &gl->gl_flags);
  256. gfs2_glstats_inc(gl, GFS2_LKS_DCOUNT);
  257. gfs2_sbstats_inc(gl, GFS2_LKS_DCOUNT);
  258. gfs2_update_request_times(gl);
  259. /* don't want to skip dlm_unlock writing the lvb when lock is ex */
  260. if (test_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags) &&
  261. gl->gl_lksb.sb_lvbptr && (gl->gl_state != LM_ST_EXCLUSIVE)) {
  262. gfs2_glock_free(gl);
  263. return;
  264. }
  265. error = dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_VALBLK,
  266. NULL, gl);
  267. if (error) {
  268. printk(KERN_ERR "gdlm_unlock %x,%llx err=%d\n",
  269. gl->gl_name.ln_type,
  270. (unsigned long long)gl->gl_name.ln_number, error);
  271. return;
  272. }
  273. }
  274. static void gdlm_cancel(struct gfs2_glock *gl)
  275. {
  276. struct lm_lockstruct *ls = &gl->gl_sbd->sd_lockstruct;
  277. dlm_unlock(ls->ls_dlm, gl->gl_lksb.sb_lkid, DLM_LKF_CANCEL, NULL, gl);
  278. }
  279. /*
  280. * dlm/gfs2 recovery coordination using dlm_recover callbacks
  281. *
  282. * 1. dlm_controld sees lockspace members change
  283. * 2. dlm_controld blocks dlm-kernel locking activity
  284. * 3. dlm_controld within dlm-kernel notifies gfs2 (recover_prep)
  285. * 4. dlm_controld starts and finishes its own user level recovery
  286. * 5. dlm_controld starts dlm-kernel dlm_recoverd to do kernel recovery
  287. * 6. dlm_recoverd notifies gfs2 of failed nodes (recover_slot)
  288. * 7. dlm_recoverd does its own lock recovery
  289. * 8. dlm_recoverd unblocks dlm-kernel locking activity
  290. * 9. dlm_recoverd notifies gfs2 when done (recover_done with new generation)
  291. * 10. gfs2_control updates control_lock lvb with new generation and jid bits
  292. * 11. gfs2_control enqueues journals for gfs2_recover to recover (maybe none)
  293. * 12. gfs2_recover dequeues and recovers journals of failed nodes
  294. * 13. gfs2_recover provides recovery results to gfs2_control (recovery_result)
  295. * 14. gfs2_control updates control_lock lvb jid bits for recovered journals
  296. * 15. gfs2_control unblocks normal locking when all journals are recovered
  297. *
  298. * - failures during recovery
  299. *
  300. * recover_prep() may set BLOCK_LOCKS (step 3) again before gfs2_control
  301. * clears BLOCK_LOCKS (step 15), e.g. another node fails while still
  302. * recovering for a prior failure. gfs2_control needs a way to detect
  303. * this so it can leave BLOCK_LOCKS set in step 15. This is managed using
  304. * the recover_block and recover_start values.
  305. *
  306. * recover_done() provides a new lockspace generation number each time it
  307. * is called (step 9). This generation number is saved as recover_start.
  308. * When recover_prep() is called, it sets BLOCK_LOCKS and sets
  309. * recover_block = recover_start. So, while recover_block is equal to
  310. * recover_start, BLOCK_LOCKS should remain set. (recover_spin must
  311. * be held around the BLOCK_LOCKS/recover_block/recover_start logic.)
  312. *
  313. * - more specific gfs2 steps in sequence above
  314. *
  315. * 3. recover_prep sets BLOCK_LOCKS and sets recover_block = recover_start
  316. * 6. recover_slot records any failed jids (maybe none)
  317. * 9. recover_done sets recover_start = new generation number
  318. * 10. gfs2_control sets control_lock lvb = new gen + bits for failed jids
  319. * 12. gfs2_recover does journal recoveries for failed jids identified above
  320. * 14. gfs2_control clears control_lock lvb bits for recovered jids
  321. * 15. gfs2_control checks if recover_block == recover_start (step 3 occured
  322. * again) then do nothing, otherwise if recover_start > recover_block
  323. * then clear BLOCK_LOCKS.
  324. *
  325. * - parallel recovery steps across all nodes
  326. *
  327. * All nodes attempt to update the control_lock lvb with the new generation
  328. * number and jid bits, but only the first to get the control_lock EX will
  329. * do so; others will see that it's already done (lvb already contains new
  330. * generation number.)
  331. *
  332. * . All nodes get the same recover_prep/recover_slot/recover_done callbacks
  333. * . All nodes attempt to set control_lock lvb gen + bits for the new gen
  334. * . One node gets control_lock first and writes the lvb, others see it's done
  335. * . All nodes attempt to recover jids for which they see control_lock bits set
  336. * . One node succeeds for a jid, and that one clears the jid bit in the lvb
  337. * . All nodes will eventually see all lvb bits clear and unblock locks
  338. *
  339. * - is there a problem with clearing an lvb bit that should be set
  340. * and missing a journal recovery?
  341. *
  342. * 1. jid fails
  343. * 2. lvb bit set for step 1
  344. * 3. jid recovered for step 1
  345. * 4. jid taken again (new mount)
  346. * 5. jid fails (for step 4)
  347. * 6. lvb bit set for step 5 (will already be set)
  348. * 7. lvb bit cleared for step 3
  349. *
  350. * This is not a problem because the failure in step 5 does not
  351. * require recovery, because the mount in step 4 could not have
  352. * progressed far enough to unblock locks and access the fs. The
  353. * control_mount() function waits for all recoveries to be complete
  354. * for the latest lockspace generation before ever unblocking locks
  355. * and returning. The mount in step 4 waits until the recovery in
  356. * step 1 is done.
  357. *
  358. * - special case of first mounter: first node to mount the fs
  359. *
  360. * The first node to mount a gfs2 fs needs to check all the journals
  361. * and recover any that need recovery before other nodes are allowed
  362. * to mount the fs. (Others may begin mounting, but they must wait
  363. * for the first mounter to be done before taking locks on the fs
  364. * or accessing the fs.) This has two parts:
  365. *
  366. * 1. The mounted_lock tells a node it's the first to mount the fs.
  367. * Each node holds the mounted_lock in PR while it's mounted.
  368. * Each node tries to acquire the mounted_lock in EX when it mounts.
  369. * If a node is granted the mounted_lock EX it means there are no
  370. * other mounted nodes (no PR locks exist), and it is the first mounter.
  371. * The mounted_lock is demoted to PR when first recovery is done, so
  372. * others will fail to get an EX lock, but will get a PR lock.
  373. *
  374. * 2. The control_lock blocks others in control_mount() while the first
  375. * mounter is doing first mount recovery of all journals.
  376. * A mounting node needs to acquire control_lock in EX mode before
  377. * it can proceed. The first mounter holds control_lock in EX while doing
  378. * the first mount recovery, blocking mounts from other nodes, then demotes
  379. * control_lock to NL when it's done (others_may_mount/first_done),
  380. * allowing other nodes to continue mounting.
  381. *
  382. * first mounter:
  383. * control_lock EX/NOQUEUE success
  384. * mounted_lock EX/NOQUEUE success (no other PR, so no other mounters)
  385. * set first=1
  386. * do first mounter recovery
  387. * mounted_lock EX->PR
  388. * control_lock EX->NL, write lvb generation
  389. *
  390. * other mounter:
  391. * control_lock EX/NOQUEUE success (if fail -EAGAIN, retry)
  392. * mounted_lock EX/NOQUEUE fail -EAGAIN (expected due to other mounters PR)
  393. * mounted_lock PR/NOQUEUE success
  394. * read lvb generation
  395. * control_lock EX->NL
  396. * set first=0
  397. *
  398. * - mount during recovery
  399. *
  400. * If a node mounts while others are doing recovery (not first mounter),
  401. * the mounting node will get its initial recover_done() callback without
  402. * having seen any previous failures/callbacks.
  403. *
  404. * It must wait for all recoveries preceding its mount to be finished
  405. * before it unblocks locks. It does this by repeating the "other mounter"
  406. * steps above until the lvb generation number is >= its mount generation
  407. * number (from initial recover_done) and all lvb bits are clear.
  408. *
  409. * - control_lock lvb format
  410. *
  411. * 4 bytes generation number: the latest dlm lockspace generation number
  412. * from recover_done callback. Indicates the jid bitmap has been updated
  413. * to reflect all slot failures through that generation.
  414. * 4 bytes unused.
  415. * GDLM_LVB_SIZE-8 bytes of jid bit map. If bit N is set, it indicates
  416. * that jid N needs recovery.
  417. */
  418. #define JID_BITMAP_OFFSET 8 /* 4 byte generation number + 4 byte unused */
  419. static void control_lvb_read(struct lm_lockstruct *ls, uint32_t *lvb_gen,
  420. char *lvb_bits)
  421. {
  422. uint32_t gen;
  423. memcpy(lvb_bits, ls->ls_control_lvb, GDLM_LVB_SIZE);
  424. memcpy(&gen, lvb_bits, sizeof(uint32_t));
  425. *lvb_gen = le32_to_cpu(gen);
  426. }
  427. static void control_lvb_write(struct lm_lockstruct *ls, uint32_t lvb_gen,
  428. char *lvb_bits)
  429. {
  430. uint32_t gen;
  431. memcpy(ls->ls_control_lvb, lvb_bits, GDLM_LVB_SIZE);
  432. gen = cpu_to_le32(lvb_gen);
  433. memcpy(ls->ls_control_lvb, &gen, sizeof(uint32_t));
  434. }
  435. static int all_jid_bits_clear(char *lvb)
  436. {
  437. int i;
  438. for (i = JID_BITMAP_OFFSET; i < GDLM_LVB_SIZE; i++) {
  439. if (lvb[i])
  440. return 0;
  441. }
  442. return 1;
  443. }
  444. static void sync_wait_cb(void *arg)
  445. {
  446. struct lm_lockstruct *ls = arg;
  447. complete(&ls->ls_sync_wait);
  448. }
  449. static int sync_unlock(struct gfs2_sbd *sdp, struct dlm_lksb *lksb, char *name)
  450. {
  451. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  452. int error;
  453. error = dlm_unlock(ls->ls_dlm, lksb->sb_lkid, 0, lksb, ls);
  454. if (error) {
  455. fs_err(sdp, "%s lkid %x error %d\n",
  456. name, lksb->sb_lkid, error);
  457. return error;
  458. }
  459. wait_for_completion(&ls->ls_sync_wait);
  460. if (lksb->sb_status != -DLM_EUNLOCK) {
  461. fs_err(sdp, "%s lkid %x status %d\n",
  462. name, lksb->sb_lkid, lksb->sb_status);
  463. return -1;
  464. }
  465. return 0;
  466. }
  467. static int sync_lock(struct gfs2_sbd *sdp, int mode, uint32_t flags,
  468. unsigned int num, struct dlm_lksb *lksb, char *name)
  469. {
  470. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  471. char strname[GDLM_STRNAME_BYTES];
  472. int error, status;
  473. memset(strname, 0, GDLM_STRNAME_BYTES);
  474. snprintf(strname, GDLM_STRNAME_BYTES, "%8x%16x", LM_TYPE_NONDISK, num);
  475. error = dlm_lock(ls->ls_dlm, mode, lksb, flags,
  476. strname, GDLM_STRNAME_BYTES - 1,
  477. 0, sync_wait_cb, ls, NULL);
  478. if (error) {
  479. fs_err(sdp, "%s lkid %x flags %x mode %d error %d\n",
  480. name, lksb->sb_lkid, flags, mode, error);
  481. return error;
  482. }
  483. wait_for_completion(&ls->ls_sync_wait);
  484. status = lksb->sb_status;
  485. if (status && status != -EAGAIN) {
  486. fs_err(sdp, "%s lkid %x flags %x mode %d status %d\n",
  487. name, lksb->sb_lkid, flags, mode, status);
  488. }
  489. return status;
  490. }
  491. static int mounted_unlock(struct gfs2_sbd *sdp)
  492. {
  493. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  494. return sync_unlock(sdp, &ls->ls_mounted_lksb, "mounted_lock");
  495. }
  496. static int mounted_lock(struct gfs2_sbd *sdp, int mode, uint32_t flags)
  497. {
  498. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  499. return sync_lock(sdp, mode, flags, GFS2_MOUNTED_LOCK,
  500. &ls->ls_mounted_lksb, "mounted_lock");
  501. }
  502. static int control_unlock(struct gfs2_sbd *sdp)
  503. {
  504. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  505. return sync_unlock(sdp, &ls->ls_control_lksb, "control_lock");
  506. }
  507. static int control_lock(struct gfs2_sbd *sdp, int mode, uint32_t flags)
  508. {
  509. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  510. return sync_lock(sdp, mode, flags, GFS2_CONTROL_LOCK,
  511. &ls->ls_control_lksb, "control_lock");
  512. }
  513. static void gfs2_control_func(struct work_struct *work)
  514. {
  515. struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_control_work.work);
  516. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  517. char lvb_bits[GDLM_LVB_SIZE];
  518. uint32_t block_gen, start_gen, lvb_gen, flags;
  519. int recover_set = 0;
  520. int write_lvb = 0;
  521. int recover_size;
  522. int i, error;
  523. spin_lock(&ls->ls_recover_spin);
  524. /*
  525. * No MOUNT_DONE means we're still mounting; control_mount()
  526. * will set this flag, after which this thread will take over
  527. * all further clearing of BLOCK_LOCKS.
  528. *
  529. * FIRST_MOUNT means this node is doing first mounter recovery,
  530. * for which recovery control is handled by
  531. * control_mount()/control_first_done(), not this thread.
  532. */
  533. if (!test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  534. test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  535. spin_unlock(&ls->ls_recover_spin);
  536. return;
  537. }
  538. block_gen = ls->ls_recover_block;
  539. start_gen = ls->ls_recover_start;
  540. spin_unlock(&ls->ls_recover_spin);
  541. /*
  542. * Equal block_gen and start_gen implies we are between
  543. * recover_prep and recover_done callbacks, which means
  544. * dlm recovery is in progress and dlm locking is blocked.
  545. * There's no point trying to do any work until recover_done.
  546. */
  547. if (block_gen == start_gen)
  548. return;
  549. /*
  550. * Propagate recover_submit[] and recover_result[] to lvb:
  551. * dlm_recoverd adds to recover_submit[] jids needing recovery
  552. * gfs2_recover adds to recover_result[] journal recovery results
  553. *
  554. * set lvb bit for jids in recover_submit[] if the lvb has not
  555. * yet been updated for the generation of the failure
  556. *
  557. * clear lvb bit for jids in recover_result[] if the result of
  558. * the journal recovery is SUCCESS
  559. */
  560. error = control_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  561. if (error) {
  562. fs_err(sdp, "control lock EX error %d\n", error);
  563. return;
  564. }
  565. control_lvb_read(ls, &lvb_gen, lvb_bits);
  566. spin_lock(&ls->ls_recover_spin);
  567. if (block_gen != ls->ls_recover_block ||
  568. start_gen != ls->ls_recover_start) {
  569. fs_info(sdp, "recover generation %u block1 %u %u\n",
  570. start_gen, block_gen, ls->ls_recover_block);
  571. spin_unlock(&ls->ls_recover_spin);
  572. control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  573. return;
  574. }
  575. recover_size = ls->ls_recover_size;
  576. if (lvb_gen <= start_gen) {
  577. /*
  578. * Clear lvb bits for jids we've successfully recovered.
  579. * Because all nodes attempt to recover failed journals,
  580. * a journal can be recovered multiple times successfully
  581. * in succession. Only the first will really do recovery,
  582. * the others find it clean, but still report a successful
  583. * recovery. So, another node may have already recovered
  584. * the jid and cleared the lvb bit for it.
  585. */
  586. for (i = 0; i < recover_size; i++) {
  587. if (ls->ls_recover_result[i] != LM_RD_SUCCESS)
  588. continue;
  589. ls->ls_recover_result[i] = 0;
  590. if (!test_bit_le(i, lvb_bits + JID_BITMAP_OFFSET))
  591. continue;
  592. __clear_bit_le(i, lvb_bits + JID_BITMAP_OFFSET);
  593. write_lvb = 1;
  594. }
  595. }
  596. if (lvb_gen == start_gen) {
  597. /*
  598. * Failed slots before start_gen are already set in lvb.
  599. */
  600. for (i = 0; i < recover_size; i++) {
  601. if (!ls->ls_recover_submit[i])
  602. continue;
  603. if (ls->ls_recover_submit[i] < lvb_gen)
  604. ls->ls_recover_submit[i] = 0;
  605. }
  606. } else if (lvb_gen < start_gen) {
  607. /*
  608. * Failed slots before start_gen are not yet set in lvb.
  609. */
  610. for (i = 0; i < recover_size; i++) {
  611. if (!ls->ls_recover_submit[i])
  612. continue;
  613. if (ls->ls_recover_submit[i] < start_gen) {
  614. ls->ls_recover_submit[i] = 0;
  615. __set_bit_le(i, lvb_bits + JID_BITMAP_OFFSET);
  616. }
  617. }
  618. /* even if there are no bits to set, we need to write the
  619. latest generation to the lvb */
  620. write_lvb = 1;
  621. } else {
  622. /*
  623. * we should be getting a recover_done() for lvb_gen soon
  624. */
  625. }
  626. spin_unlock(&ls->ls_recover_spin);
  627. if (write_lvb) {
  628. control_lvb_write(ls, start_gen, lvb_bits);
  629. flags = DLM_LKF_CONVERT | DLM_LKF_VALBLK;
  630. } else {
  631. flags = DLM_LKF_CONVERT;
  632. }
  633. error = control_lock(sdp, DLM_LOCK_NL, flags);
  634. if (error) {
  635. fs_err(sdp, "control lock NL error %d\n", error);
  636. return;
  637. }
  638. /*
  639. * Everyone will see jid bits set in the lvb, run gfs2_recover_set(),
  640. * and clear a jid bit in the lvb if the recovery is a success.
  641. * Eventually all journals will be recovered, all jid bits will
  642. * be cleared in the lvb, and everyone will clear BLOCK_LOCKS.
  643. */
  644. for (i = 0; i < recover_size; i++) {
  645. if (test_bit_le(i, lvb_bits + JID_BITMAP_OFFSET)) {
  646. fs_info(sdp, "recover generation %u jid %d\n",
  647. start_gen, i);
  648. gfs2_recover_set(sdp, i);
  649. recover_set++;
  650. }
  651. }
  652. if (recover_set)
  653. return;
  654. /*
  655. * No more jid bits set in lvb, all recovery is done, unblock locks
  656. * (unless a new recover_prep callback has occured blocking locks
  657. * again while working above)
  658. */
  659. spin_lock(&ls->ls_recover_spin);
  660. if (ls->ls_recover_block == block_gen &&
  661. ls->ls_recover_start == start_gen) {
  662. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  663. spin_unlock(&ls->ls_recover_spin);
  664. fs_info(sdp, "recover generation %u done\n", start_gen);
  665. gfs2_glock_thaw(sdp);
  666. } else {
  667. fs_info(sdp, "recover generation %u block2 %u %u\n",
  668. start_gen, block_gen, ls->ls_recover_block);
  669. spin_unlock(&ls->ls_recover_spin);
  670. }
  671. }
  672. static int control_mount(struct gfs2_sbd *sdp)
  673. {
  674. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  675. char lvb_bits[GDLM_LVB_SIZE];
  676. uint32_t start_gen, block_gen, mount_gen, lvb_gen;
  677. int mounted_mode;
  678. int retries = 0;
  679. int error;
  680. memset(&ls->ls_mounted_lksb, 0, sizeof(struct dlm_lksb));
  681. memset(&ls->ls_control_lksb, 0, sizeof(struct dlm_lksb));
  682. memset(&ls->ls_control_lvb, 0, GDLM_LVB_SIZE);
  683. ls->ls_control_lksb.sb_lvbptr = ls->ls_control_lvb;
  684. init_completion(&ls->ls_sync_wait);
  685. set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  686. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_VALBLK);
  687. if (error) {
  688. fs_err(sdp, "control_mount control_lock NL error %d\n", error);
  689. return error;
  690. }
  691. error = mounted_lock(sdp, DLM_LOCK_NL, 0);
  692. if (error) {
  693. fs_err(sdp, "control_mount mounted_lock NL error %d\n", error);
  694. control_unlock(sdp);
  695. return error;
  696. }
  697. mounted_mode = DLM_LOCK_NL;
  698. restart:
  699. if (retries++ && signal_pending(current)) {
  700. error = -EINTR;
  701. goto fail;
  702. }
  703. /*
  704. * We always start with both locks in NL. control_lock is
  705. * demoted to NL below so we don't need to do it here.
  706. */
  707. if (mounted_mode != DLM_LOCK_NL) {
  708. error = mounted_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  709. if (error)
  710. goto fail;
  711. mounted_mode = DLM_LOCK_NL;
  712. }
  713. /*
  714. * Other nodes need to do some work in dlm recovery and gfs2_control
  715. * before the recover_done and control_lock will be ready for us below.
  716. * A delay here is not required but often avoids having to retry.
  717. */
  718. msleep_interruptible(500);
  719. /*
  720. * Acquire control_lock in EX and mounted_lock in either EX or PR.
  721. * control_lock lvb keeps track of any pending journal recoveries.
  722. * mounted_lock indicates if any other nodes have the fs mounted.
  723. */
  724. error = control_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE|DLM_LKF_VALBLK);
  725. if (error == -EAGAIN) {
  726. goto restart;
  727. } else if (error) {
  728. fs_err(sdp, "control_mount control_lock EX error %d\n", error);
  729. goto fail;
  730. }
  731. error = mounted_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE);
  732. if (!error) {
  733. mounted_mode = DLM_LOCK_EX;
  734. goto locks_done;
  735. } else if (error != -EAGAIN) {
  736. fs_err(sdp, "control_mount mounted_lock EX error %d\n", error);
  737. goto fail;
  738. }
  739. error = mounted_lock(sdp, DLM_LOCK_PR, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE);
  740. if (!error) {
  741. mounted_mode = DLM_LOCK_PR;
  742. goto locks_done;
  743. } else {
  744. /* not even -EAGAIN should happen here */
  745. fs_err(sdp, "control_mount mounted_lock PR error %d\n", error);
  746. goto fail;
  747. }
  748. locks_done:
  749. /*
  750. * If we got both locks above in EX, then we're the first mounter.
  751. * If not, then we need to wait for the control_lock lvb to be
  752. * updated by other mounted nodes to reflect our mount generation.
  753. *
  754. * In simple first mounter cases, first mounter will see zero lvb_gen,
  755. * but in cases where all existing nodes leave/fail before mounting
  756. * nodes finish control_mount, then all nodes will be mounting and
  757. * lvb_gen will be non-zero.
  758. */
  759. control_lvb_read(ls, &lvb_gen, lvb_bits);
  760. if (lvb_gen == 0xFFFFFFFF) {
  761. /* special value to force mount attempts to fail */
  762. fs_err(sdp, "control_mount control_lock disabled\n");
  763. error = -EINVAL;
  764. goto fail;
  765. }
  766. if (mounted_mode == DLM_LOCK_EX) {
  767. /* first mounter, keep both EX while doing first recovery */
  768. spin_lock(&ls->ls_recover_spin);
  769. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  770. set_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags);
  771. set_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  772. spin_unlock(&ls->ls_recover_spin);
  773. fs_info(sdp, "first mounter control generation %u\n", lvb_gen);
  774. return 0;
  775. }
  776. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  777. if (error)
  778. goto fail;
  779. /*
  780. * We are not first mounter, now we need to wait for the control_lock
  781. * lvb generation to be >= the generation from our first recover_done
  782. * and all lvb bits to be clear (no pending journal recoveries.)
  783. */
  784. if (!all_jid_bits_clear(lvb_bits)) {
  785. /* journals need recovery, wait until all are clear */
  786. fs_info(sdp, "control_mount wait for journal recovery\n");
  787. goto restart;
  788. }
  789. spin_lock(&ls->ls_recover_spin);
  790. block_gen = ls->ls_recover_block;
  791. start_gen = ls->ls_recover_start;
  792. mount_gen = ls->ls_recover_mount;
  793. if (lvb_gen < mount_gen) {
  794. /* wait for mounted nodes to update control_lock lvb to our
  795. generation, which might include new recovery bits set */
  796. fs_info(sdp, "control_mount wait1 block %u start %u mount %u "
  797. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  798. lvb_gen, ls->ls_recover_flags);
  799. spin_unlock(&ls->ls_recover_spin);
  800. goto restart;
  801. }
  802. if (lvb_gen != start_gen) {
  803. /* wait for mounted nodes to update control_lock lvb to the
  804. latest recovery generation */
  805. fs_info(sdp, "control_mount wait2 block %u start %u mount %u "
  806. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  807. lvb_gen, ls->ls_recover_flags);
  808. spin_unlock(&ls->ls_recover_spin);
  809. goto restart;
  810. }
  811. if (block_gen == start_gen) {
  812. /* dlm recovery in progress, wait for it to finish */
  813. fs_info(sdp, "control_mount wait3 block %u start %u mount %u "
  814. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  815. lvb_gen, ls->ls_recover_flags);
  816. spin_unlock(&ls->ls_recover_spin);
  817. goto restart;
  818. }
  819. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  820. set_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags);
  821. memset(ls->ls_recover_submit, 0, ls->ls_recover_size*sizeof(uint32_t));
  822. memset(ls->ls_recover_result, 0, ls->ls_recover_size*sizeof(uint32_t));
  823. spin_unlock(&ls->ls_recover_spin);
  824. return 0;
  825. fail:
  826. mounted_unlock(sdp);
  827. control_unlock(sdp);
  828. return error;
  829. }
  830. static int dlm_recovery_wait(void *word)
  831. {
  832. schedule();
  833. return 0;
  834. }
  835. static int control_first_done(struct gfs2_sbd *sdp)
  836. {
  837. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  838. char lvb_bits[GDLM_LVB_SIZE];
  839. uint32_t start_gen, block_gen;
  840. int error;
  841. restart:
  842. spin_lock(&ls->ls_recover_spin);
  843. start_gen = ls->ls_recover_start;
  844. block_gen = ls->ls_recover_block;
  845. if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags) ||
  846. !test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  847. !test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  848. /* sanity check, should not happen */
  849. fs_err(sdp, "control_first_done start %u block %u flags %lx\n",
  850. start_gen, block_gen, ls->ls_recover_flags);
  851. spin_unlock(&ls->ls_recover_spin);
  852. control_unlock(sdp);
  853. return -1;
  854. }
  855. if (start_gen == block_gen) {
  856. /*
  857. * Wait for the end of a dlm recovery cycle to switch from
  858. * first mounter recovery. We can ignore any recover_slot
  859. * callbacks between the recover_prep and next recover_done
  860. * because we are still the first mounter and any failed nodes
  861. * have not fully mounted, so they don't need recovery.
  862. */
  863. spin_unlock(&ls->ls_recover_spin);
  864. fs_info(sdp, "control_first_done wait gen %u\n", start_gen);
  865. wait_on_bit(&ls->ls_recover_flags, DFL_DLM_RECOVERY,
  866. dlm_recovery_wait, TASK_UNINTERRUPTIBLE);
  867. goto restart;
  868. }
  869. clear_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  870. set_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags);
  871. memset(ls->ls_recover_submit, 0, ls->ls_recover_size*sizeof(uint32_t));
  872. memset(ls->ls_recover_result, 0, ls->ls_recover_size*sizeof(uint32_t));
  873. spin_unlock(&ls->ls_recover_spin);
  874. memset(lvb_bits, 0, sizeof(lvb_bits));
  875. control_lvb_write(ls, start_gen, lvb_bits);
  876. error = mounted_lock(sdp, DLM_LOCK_PR, DLM_LKF_CONVERT);
  877. if (error)
  878. fs_err(sdp, "control_first_done mounted PR error %d\n", error);
  879. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  880. if (error)
  881. fs_err(sdp, "control_first_done control NL error %d\n", error);
  882. return error;
  883. }
  884. /*
  885. * Expand static jid arrays if necessary (by increments of RECOVER_SIZE_INC)
  886. * to accomodate the largest slot number. (NB dlm slot numbers start at 1,
  887. * gfs2 jids start at 0, so jid = slot - 1)
  888. */
  889. #define RECOVER_SIZE_INC 16
  890. static int set_recover_size(struct gfs2_sbd *sdp, struct dlm_slot *slots,
  891. int num_slots)
  892. {
  893. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  894. uint32_t *submit = NULL;
  895. uint32_t *result = NULL;
  896. uint32_t old_size, new_size;
  897. int i, max_jid;
  898. max_jid = 0;
  899. for (i = 0; i < num_slots; i++) {
  900. if (max_jid < slots[i].slot - 1)
  901. max_jid = slots[i].slot - 1;
  902. }
  903. old_size = ls->ls_recover_size;
  904. if (old_size >= max_jid + 1)
  905. return 0;
  906. new_size = old_size + RECOVER_SIZE_INC;
  907. submit = kzalloc(new_size * sizeof(uint32_t), GFP_NOFS);
  908. result = kzalloc(new_size * sizeof(uint32_t), GFP_NOFS);
  909. if (!submit || !result) {
  910. kfree(submit);
  911. kfree(result);
  912. return -ENOMEM;
  913. }
  914. spin_lock(&ls->ls_recover_spin);
  915. memcpy(submit, ls->ls_recover_submit, old_size * sizeof(uint32_t));
  916. memcpy(result, ls->ls_recover_result, old_size * sizeof(uint32_t));
  917. kfree(ls->ls_recover_submit);
  918. kfree(ls->ls_recover_result);
  919. ls->ls_recover_submit = submit;
  920. ls->ls_recover_result = result;
  921. ls->ls_recover_size = new_size;
  922. spin_unlock(&ls->ls_recover_spin);
  923. return 0;
  924. }
  925. static void free_recover_size(struct lm_lockstruct *ls)
  926. {
  927. kfree(ls->ls_recover_submit);
  928. kfree(ls->ls_recover_result);
  929. ls->ls_recover_submit = NULL;
  930. ls->ls_recover_result = NULL;
  931. ls->ls_recover_size = 0;
  932. }
  933. /* dlm calls before it does lock recovery */
  934. static void gdlm_recover_prep(void *arg)
  935. {
  936. struct gfs2_sbd *sdp = arg;
  937. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  938. spin_lock(&ls->ls_recover_spin);
  939. ls->ls_recover_block = ls->ls_recover_start;
  940. set_bit(DFL_DLM_RECOVERY, &ls->ls_recover_flags);
  941. if (!test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  942. test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  943. spin_unlock(&ls->ls_recover_spin);
  944. return;
  945. }
  946. set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  947. spin_unlock(&ls->ls_recover_spin);
  948. }
  949. /* dlm calls after recover_prep has been completed on all lockspace members;
  950. identifies slot/jid of failed member */
  951. static void gdlm_recover_slot(void *arg, struct dlm_slot *slot)
  952. {
  953. struct gfs2_sbd *sdp = arg;
  954. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  955. int jid = slot->slot - 1;
  956. spin_lock(&ls->ls_recover_spin);
  957. if (ls->ls_recover_size < jid + 1) {
  958. fs_err(sdp, "recover_slot jid %d gen %u short size %d",
  959. jid, ls->ls_recover_block, ls->ls_recover_size);
  960. spin_unlock(&ls->ls_recover_spin);
  961. return;
  962. }
  963. if (ls->ls_recover_submit[jid]) {
  964. fs_info(sdp, "recover_slot jid %d gen %u prev %u",
  965. jid, ls->ls_recover_block, ls->ls_recover_submit[jid]);
  966. }
  967. ls->ls_recover_submit[jid] = ls->ls_recover_block;
  968. spin_unlock(&ls->ls_recover_spin);
  969. }
  970. /* dlm calls after recover_slot and after it completes lock recovery */
  971. static void gdlm_recover_done(void *arg, struct dlm_slot *slots, int num_slots,
  972. int our_slot, uint32_t generation)
  973. {
  974. struct gfs2_sbd *sdp = arg;
  975. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  976. /* ensure the ls jid arrays are large enough */
  977. set_recover_size(sdp, slots, num_slots);
  978. spin_lock(&ls->ls_recover_spin);
  979. ls->ls_recover_start = generation;
  980. if (!ls->ls_recover_mount) {
  981. ls->ls_recover_mount = generation;
  982. ls->ls_jid = our_slot - 1;
  983. }
  984. if (!test_bit(DFL_UNMOUNT, &ls->ls_recover_flags))
  985. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work, 0);
  986. clear_bit(DFL_DLM_RECOVERY, &ls->ls_recover_flags);
  987. smp_mb__after_clear_bit();
  988. wake_up_bit(&ls->ls_recover_flags, DFL_DLM_RECOVERY);
  989. spin_unlock(&ls->ls_recover_spin);
  990. }
  991. /* gfs2_recover thread has a journal recovery result */
  992. static void gdlm_recovery_result(struct gfs2_sbd *sdp, unsigned int jid,
  993. unsigned int result)
  994. {
  995. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  996. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  997. return;
  998. /* don't care about the recovery of own journal during mount */
  999. if (jid == ls->ls_jid)
  1000. return;
  1001. spin_lock(&ls->ls_recover_spin);
  1002. if (test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  1003. spin_unlock(&ls->ls_recover_spin);
  1004. return;
  1005. }
  1006. if (ls->ls_recover_size < jid + 1) {
  1007. fs_err(sdp, "recovery_result jid %d short size %d",
  1008. jid, ls->ls_recover_size);
  1009. spin_unlock(&ls->ls_recover_spin);
  1010. return;
  1011. }
  1012. fs_info(sdp, "recover jid %d result %s\n", jid,
  1013. result == LM_RD_GAVEUP ? "busy" : "success");
  1014. ls->ls_recover_result[jid] = result;
  1015. /* GAVEUP means another node is recovering the journal; delay our
  1016. next attempt to recover it, to give the other node a chance to
  1017. finish before trying again */
  1018. if (!test_bit(DFL_UNMOUNT, &ls->ls_recover_flags))
  1019. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work,
  1020. result == LM_RD_GAVEUP ? HZ : 0);
  1021. spin_unlock(&ls->ls_recover_spin);
  1022. }
  1023. const struct dlm_lockspace_ops gdlm_lockspace_ops = {
  1024. .recover_prep = gdlm_recover_prep,
  1025. .recover_slot = gdlm_recover_slot,
  1026. .recover_done = gdlm_recover_done,
  1027. };
  1028. static int gdlm_mount(struct gfs2_sbd *sdp, const char *table)
  1029. {
  1030. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1031. char cluster[GFS2_LOCKNAME_LEN];
  1032. const char *fsname;
  1033. uint32_t flags;
  1034. int error, ops_result;
  1035. /*
  1036. * initialize everything
  1037. */
  1038. INIT_DELAYED_WORK(&sdp->sd_control_work, gfs2_control_func);
  1039. spin_lock_init(&ls->ls_recover_spin);
  1040. ls->ls_recover_flags = 0;
  1041. ls->ls_recover_mount = 0;
  1042. ls->ls_recover_start = 0;
  1043. ls->ls_recover_block = 0;
  1044. ls->ls_recover_size = 0;
  1045. ls->ls_recover_submit = NULL;
  1046. ls->ls_recover_result = NULL;
  1047. error = set_recover_size(sdp, NULL, 0);
  1048. if (error)
  1049. goto fail;
  1050. /*
  1051. * prepare dlm_new_lockspace args
  1052. */
  1053. fsname = strchr(table, ':');
  1054. if (!fsname) {
  1055. fs_info(sdp, "no fsname found\n");
  1056. error = -EINVAL;
  1057. goto fail_free;
  1058. }
  1059. memset(cluster, 0, sizeof(cluster));
  1060. memcpy(cluster, table, strlen(table) - strlen(fsname));
  1061. fsname++;
  1062. flags = DLM_LSFL_FS | DLM_LSFL_NEWEXCL;
  1063. /*
  1064. * create/join lockspace
  1065. */
  1066. error = dlm_new_lockspace(fsname, cluster, flags, GDLM_LVB_SIZE,
  1067. &gdlm_lockspace_ops, sdp, &ops_result,
  1068. &ls->ls_dlm);
  1069. if (error) {
  1070. fs_err(sdp, "dlm_new_lockspace error %d\n", error);
  1071. goto fail_free;
  1072. }
  1073. if (ops_result < 0) {
  1074. /*
  1075. * dlm does not support ops callbacks,
  1076. * old dlm_controld/gfs_controld are used, try without ops.
  1077. */
  1078. fs_info(sdp, "dlm lockspace ops not used\n");
  1079. free_recover_size(ls);
  1080. set_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags);
  1081. return 0;
  1082. }
  1083. if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags)) {
  1084. fs_err(sdp, "dlm lockspace ops disallow jid preset\n");
  1085. error = -EINVAL;
  1086. goto fail_release;
  1087. }
  1088. /*
  1089. * control_mount() uses control_lock to determine first mounter,
  1090. * and for later mounts, waits for any recoveries to be cleared.
  1091. */
  1092. error = control_mount(sdp);
  1093. if (error) {
  1094. fs_err(sdp, "mount control error %d\n", error);
  1095. goto fail_release;
  1096. }
  1097. ls->ls_first = !!test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  1098. clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
  1099. smp_mb__after_clear_bit();
  1100. wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
  1101. return 0;
  1102. fail_release:
  1103. dlm_release_lockspace(ls->ls_dlm, 2);
  1104. fail_free:
  1105. free_recover_size(ls);
  1106. fail:
  1107. return error;
  1108. }
  1109. static void gdlm_first_done(struct gfs2_sbd *sdp)
  1110. {
  1111. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1112. int error;
  1113. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  1114. return;
  1115. error = control_first_done(sdp);
  1116. if (error)
  1117. fs_err(sdp, "mount first_done error %d\n", error);
  1118. }
  1119. static void gdlm_unmount(struct gfs2_sbd *sdp)
  1120. {
  1121. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1122. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  1123. goto release;
  1124. /* wait for gfs2_control_wq to be done with this mount */
  1125. spin_lock(&ls->ls_recover_spin);
  1126. set_bit(DFL_UNMOUNT, &ls->ls_recover_flags);
  1127. spin_unlock(&ls->ls_recover_spin);
  1128. flush_delayed_work(&sdp->sd_control_work);
  1129. /* mounted_lock and control_lock will be purged in dlm recovery */
  1130. release:
  1131. if (ls->ls_dlm) {
  1132. dlm_release_lockspace(ls->ls_dlm, 2);
  1133. ls->ls_dlm = NULL;
  1134. }
  1135. free_recover_size(ls);
  1136. }
  1137. static const match_table_t dlm_tokens = {
  1138. { Opt_jid, "jid=%d"},
  1139. { Opt_id, "id=%d"},
  1140. { Opt_first, "first=%d"},
  1141. { Opt_nodir, "nodir=%d"},
  1142. { Opt_err, NULL },
  1143. };
  1144. const struct lm_lockops gfs2_dlm_ops = {
  1145. .lm_proto_name = "lock_dlm",
  1146. .lm_mount = gdlm_mount,
  1147. .lm_first_done = gdlm_first_done,
  1148. .lm_recovery_result = gdlm_recovery_result,
  1149. .lm_unmount = gdlm_unmount,
  1150. .lm_put_lock = gdlm_put_lock,
  1151. .lm_lock = gdlm_lock,
  1152. .lm_cancel = gdlm_cancel,
  1153. .lm_tokens = &dlm_tokens,
  1154. };