lock_dlm.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. int i;
  441. for (i = JID_BITMAP_OFFSET; i < GDLM_LVB_SIZE; i++) {
  442. if (lvb[i])
  443. return 0;
  444. }
  445. return 1;
  446. }
  447. static void sync_wait_cb(void *arg)
  448. {
  449. struct lm_lockstruct *ls = arg;
  450. complete(&ls->ls_sync_wait);
  451. }
  452. static int sync_unlock(struct gfs2_sbd *sdp, struct dlm_lksb *lksb, char *name)
  453. {
  454. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  455. int error;
  456. error = dlm_unlock(ls->ls_dlm, lksb->sb_lkid, 0, lksb, ls);
  457. if (error) {
  458. fs_err(sdp, "%s lkid %x error %d\n",
  459. name, lksb->sb_lkid, error);
  460. return error;
  461. }
  462. wait_for_completion(&ls->ls_sync_wait);
  463. if (lksb->sb_status != -DLM_EUNLOCK) {
  464. fs_err(sdp, "%s lkid %x status %d\n",
  465. name, lksb->sb_lkid, lksb->sb_status);
  466. return -1;
  467. }
  468. return 0;
  469. }
  470. static int sync_lock(struct gfs2_sbd *sdp, int mode, uint32_t flags,
  471. unsigned int num, struct dlm_lksb *lksb, char *name)
  472. {
  473. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  474. char strname[GDLM_STRNAME_BYTES];
  475. int error, status;
  476. memset(strname, 0, GDLM_STRNAME_BYTES);
  477. snprintf(strname, GDLM_STRNAME_BYTES, "%8x%16x", LM_TYPE_NONDISK, num);
  478. error = dlm_lock(ls->ls_dlm, mode, lksb, flags,
  479. strname, GDLM_STRNAME_BYTES - 1,
  480. 0, sync_wait_cb, ls, NULL);
  481. if (error) {
  482. fs_err(sdp, "%s lkid %x flags %x mode %d error %d\n",
  483. name, lksb->sb_lkid, flags, mode, error);
  484. return error;
  485. }
  486. wait_for_completion(&ls->ls_sync_wait);
  487. status = lksb->sb_status;
  488. if (status && status != -EAGAIN) {
  489. fs_err(sdp, "%s lkid %x flags %x mode %d status %d\n",
  490. name, lksb->sb_lkid, flags, mode, status);
  491. }
  492. return status;
  493. }
  494. static int mounted_unlock(struct gfs2_sbd *sdp)
  495. {
  496. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  497. return sync_unlock(sdp, &ls->ls_mounted_lksb, "mounted_lock");
  498. }
  499. static int mounted_lock(struct gfs2_sbd *sdp, int mode, uint32_t flags)
  500. {
  501. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  502. return sync_lock(sdp, mode, flags, GFS2_MOUNTED_LOCK,
  503. &ls->ls_mounted_lksb, "mounted_lock");
  504. }
  505. static int control_unlock(struct gfs2_sbd *sdp)
  506. {
  507. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  508. return sync_unlock(sdp, &ls->ls_control_lksb, "control_lock");
  509. }
  510. static int control_lock(struct gfs2_sbd *sdp, int mode, uint32_t flags)
  511. {
  512. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  513. return sync_lock(sdp, mode, flags, GFS2_CONTROL_LOCK,
  514. &ls->ls_control_lksb, "control_lock");
  515. }
  516. static void gfs2_control_func(struct work_struct *work)
  517. {
  518. struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_control_work.work);
  519. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  520. char lvb_bits[GDLM_LVB_SIZE];
  521. uint32_t block_gen, start_gen, lvb_gen, flags;
  522. int recover_set = 0;
  523. int write_lvb = 0;
  524. int recover_size;
  525. int i, error;
  526. spin_lock(&ls->ls_recover_spin);
  527. /*
  528. * No MOUNT_DONE means we're still mounting; control_mount()
  529. * will set this flag, after which this thread will take over
  530. * all further clearing of BLOCK_LOCKS.
  531. *
  532. * FIRST_MOUNT means this node is doing first mounter recovery,
  533. * for which recovery control is handled by
  534. * control_mount()/control_first_done(), not this thread.
  535. */
  536. if (!test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  537. test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  538. spin_unlock(&ls->ls_recover_spin);
  539. return;
  540. }
  541. block_gen = ls->ls_recover_block;
  542. start_gen = ls->ls_recover_start;
  543. spin_unlock(&ls->ls_recover_spin);
  544. /*
  545. * Equal block_gen and start_gen implies we are between
  546. * recover_prep and recover_done callbacks, which means
  547. * dlm recovery is in progress and dlm locking is blocked.
  548. * There's no point trying to do any work until recover_done.
  549. */
  550. if (block_gen == start_gen)
  551. return;
  552. /*
  553. * Propagate recover_submit[] and recover_result[] to lvb:
  554. * dlm_recoverd adds to recover_submit[] jids needing recovery
  555. * gfs2_recover adds to recover_result[] journal recovery results
  556. *
  557. * set lvb bit for jids in recover_submit[] if the lvb has not
  558. * yet been updated for the generation of the failure
  559. *
  560. * clear lvb bit for jids in recover_result[] if the result of
  561. * the journal recovery is SUCCESS
  562. */
  563. error = control_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  564. if (error) {
  565. fs_err(sdp, "control lock EX error %d\n", error);
  566. return;
  567. }
  568. control_lvb_read(ls, &lvb_gen, lvb_bits);
  569. spin_lock(&ls->ls_recover_spin);
  570. if (block_gen != ls->ls_recover_block ||
  571. start_gen != ls->ls_recover_start) {
  572. fs_info(sdp, "recover generation %u block1 %u %u\n",
  573. start_gen, block_gen, ls->ls_recover_block);
  574. spin_unlock(&ls->ls_recover_spin);
  575. control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  576. return;
  577. }
  578. recover_size = ls->ls_recover_size;
  579. if (lvb_gen <= start_gen) {
  580. /*
  581. * Clear lvb bits for jids we've successfully recovered.
  582. * Because all nodes attempt to recover failed journals,
  583. * a journal can be recovered multiple times successfully
  584. * in succession. Only the first will really do recovery,
  585. * the others find it clean, but still report a successful
  586. * recovery. So, another node may have already recovered
  587. * the jid and cleared the lvb bit for it.
  588. */
  589. for (i = 0; i < recover_size; i++) {
  590. if (ls->ls_recover_result[i] != LM_RD_SUCCESS)
  591. continue;
  592. ls->ls_recover_result[i] = 0;
  593. if (!test_bit_le(i, lvb_bits + JID_BITMAP_OFFSET))
  594. continue;
  595. __clear_bit_le(i, lvb_bits + JID_BITMAP_OFFSET);
  596. write_lvb = 1;
  597. }
  598. }
  599. if (lvb_gen == start_gen) {
  600. /*
  601. * Failed slots before start_gen are already set in lvb.
  602. */
  603. for (i = 0; i < recover_size; i++) {
  604. if (!ls->ls_recover_submit[i])
  605. continue;
  606. if (ls->ls_recover_submit[i] < lvb_gen)
  607. ls->ls_recover_submit[i] = 0;
  608. }
  609. } else if (lvb_gen < start_gen) {
  610. /*
  611. * Failed slots before start_gen are not yet set in lvb.
  612. */
  613. for (i = 0; i < recover_size; i++) {
  614. if (!ls->ls_recover_submit[i])
  615. continue;
  616. if (ls->ls_recover_submit[i] < start_gen) {
  617. ls->ls_recover_submit[i] = 0;
  618. __set_bit_le(i, lvb_bits + JID_BITMAP_OFFSET);
  619. }
  620. }
  621. /* even if there are no bits to set, we need to write the
  622. latest generation to the lvb */
  623. write_lvb = 1;
  624. } else {
  625. /*
  626. * we should be getting a recover_done() for lvb_gen soon
  627. */
  628. }
  629. spin_unlock(&ls->ls_recover_spin);
  630. if (write_lvb) {
  631. control_lvb_write(ls, start_gen, lvb_bits);
  632. flags = DLM_LKF_CONVERT | DLM_LKF_VALBLK;
  633. } else {
  634. flags = DLM_LKF_CONVERT;
  635. }
  636. error = control_lock(sdp, DLM_LOCK_NL, flags);
  637. if (error) {
  638. fs_err(sdp, "control lock NL error %d\n", error);
  639. return;
  640. }
  641. /*
  642. * Everyone will see jid bits set in the lvb, run gfs2_recover_set(),
  643. * and clear a jid bit in the lvb if the recovery is a success.
  644. * Eventually all journals will be recovered, all jid bits will
  645. * be cleared in the lvb, and everyone will clear BLOCK_LOCKS.
  646. */
  647. for (i = 0; i < recover_size; i++) {
  648. if (test_bit_le(i, lvb_bits + JID_BITMAP_OFFSET)) {
  649. fs_info(sdp, "recover generation %u jid %d\n",
  650. start_gen, i);
  651. gfs2_recover_set(sdp, i);
  652. recover_set++;
  653. }
  654. }
  655. if (recover_set)
  656. return;
  657. /*
  658. * No more jid bits set in lvb, all recovery is done, unblock locks
  659. * (unless a new recover_prep callback has occured blocking locks
  660. * again while working above)
  661. */
  662. spin_lock(&ls->ls_recover_spin);
  663. if (ls->ls_recover_block == block_gen &&
  664. ls->ls_recover_start == start_gen) {
  665. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  666. spin_unlock(&ls->ls_recover_spin);
  667. fs_info(sdp, "recover generation %u done\n", start_gen);
  668. gfs2_glock_thaw(sdp);
  669. } else {
  670. fs_info(sdp, "recover generation %u block2 %u %u\n",
  671. start_gen, block_gen, ls->ls_recover_block);
  672. spin_unlock(&ls->ls_recover_spin);
  673. }
  674. }
  675. static int control_mount(struct gfs2_sbd *sdp)
  676. {
  677. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  678. char lvb_bits[GDLM_LVB_SIZE];
  679. uint32_t start_gen, block_gen, mount_gen, lvb_gen;
  680. int mounted_mode;
  681. int retries = 0;
  682. int error;
  683. memset(&ls->ls_mounted_lksb, 0, sizeof(struct dlm_lksb));
  684. memset(&ls->ls_control_lksb, 0, sizeof(struct dlm_lksb));
  685. memset(&ls->ls_control_lvb, 0, GDLM_LVB_SIZE);
  686. ls->ls_control_lksb.sb_lvbptr = ls->ls_control_lvb;
  687. init_completion(&ls->ls_sync_wait);
  688. set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  689. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_VALBLK);
  690. if (error) {
  691. fs_err(sdp, "control_mount control_lock NL error %d\n", error);
  692. return error;
  693. }
  694. error = mounted_lock(sdp, DLM_LOCK_NL, 0);
  695. if (error) {
  696. fs_err(sdp, "control_mount mounted_lock NL error %d\n", error);
  697. control_unlock(sdp);
  698. return error;
  699. }
  700. mounted_mode = DLM_LOCK_NL;
  701. restart:
  702. if (retries++ && signal_pending(current)) {
  703. error = -EINTR;
  704. goto fail;
  705. }
  706. /*
  707. * We always start with both locks in NL. control_lock is
  708. * demoted to NL below so we don't need to do it here.
  709. */
  710. if (mounted_mode != DLM_LOCK_NL) {
  711. error = mounted_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  712. if (error)
  713. goto fail;
  714. mounted_mode = DLM_LOCK_NL;
  715. }
  716. /*
  717. * Other nodes need to do some work in dlm recovery and gfs2_control
  718. * before the recover_done and control_lock will be ready for us below.
  719. * A delay here is not required but often avoids having to retry.
  720. */
  721. msleep_interruptible(500);
  722. /*
  723. * Acquire control_lock in EX and mounted_lock in either EX or PR.
  724. * control_lock lvb keeps track of any pending journal recoveries.
  725. * mounted_lock indicates if any other nodes have the fs mounted.
  726. */
  727. error = control_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE|DLM_LKF_VALBLK);
  728. if (error == -EAGAIN) {
  729. goto restart;
  730. } else if (error) {
  731. fs_err(sdp, "control_mount control_lock EX error %d\n", error);
  732. goto fail;
  733. }
  734. error = mounted_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE);
  735. if (!error) {
  736. mounted_mode = DLM_LOCK_EX;
  737. goto locks_done;
  738. } else if (error != -EAGAIN) {
  739. fs_err(sdp, "control_mount mounted_lock EX error %d\n", error);
  740. goto fail;
  741. }
  742. error = mounted_lock(sdp, DLM_LOCK_PR, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE);
  743. if (!error) {
  744. mounted_mode = DLM_LOCK_PR;
  745. goto locks_done;
  746. } else {
  747. /* not even -EAGAIN should happen here */
  748. fs_err(sdp, "control_mount mounted_lock PR error %d\n", error);
  749. goto fail;
  750. }
  751. locks_done:
  752. /*
  753. * If we got both locks above in EX, then we're the first mounter.
  754. * If not, then we need to wait for the control_lock lvb to be
  755. * updated by other mounted nodes to reflect our mount generation.
  756. *
  757. * In simple first mounter cases, first mounter will see zero lvb_gen,
  758. * but in cases where all existing nodes leave/fail before mounting
  759. * nodes finish control_mount, then all nodes will be mounting and
  760. * lvb_gen will be non-zero.
  761. */
  762. control_lvb_read(ls, &lvb_gen, lvb_bits);
  763. if (lvb_gen == 0xFFFFFFFF) {
  764. /* special value to force mount attempts to fail */
  765. fs_err(sdp, "control_mount control_lock disabled\n");
  766. error = -EINVAL;
  767. goto fail;
  768. }
  769. if (mounted_mode == DLM_LOCK_EX) {
  770. /* first mounter, keep both EX while doing first recovery */
  771. spin_lock(&ls->ls_recover_spin);
  772. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  773. set_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags);
  774. set_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  775. spin_unlock(&ls->ls_recover_spin);
  776. fs_info(sdp, "first mounter control generation %u\n", lvb_gen);
  777. return 0;
  778. }
  779. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  780. if (error)
  781. goto fail;
  782. /*
  783. * We are not first mounter, now we need to wait for the control_lock
  784. * lvb generation to be >= the generation from our first recover_done
  785. * and all lvb bits to be clear (no pending journal recoveries.)
  786. */
  787. if (!all_jid_bits_clear(lvb_bits)) {
  788. /* journals need recovery, wait until all are clear */
  789. fs_info(sdp, "control_mount wait for journal recovery\n");
  790. goto restart;
  791. }
  792. spin_lock(&ls->ls_recover_spin);
  793. block_gen = ls->ls_recover_block;
  794. start_gen = ls->ls_recover_start;
  795. mount_gen = ls->ls_recover_mount;
  796. if (lvb_gen < mount_gen) {
  797. /* wait for mounted nodes to update control_lock lvb to our
  798. generation, which might include new recovery bits set */
  799. fs_info(sdp, "control_mount wait1 block %u start %u mount %u "
  800. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  801. lvb_gen, ls->ls_recover_flags);
  802. spin_unlock(&ls->ls_recover_spin);
  803. goto restart;
  804. }
  805. if (lvb_gen != start_gen) {
  806. /* wait for mounted nodes to update control_lock lvb to the
  807. latest recovery generation */
  808. fs_info(sdp, "control_mount wait2 block %u start %u mount %u "
  809. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  810. lvb_gen, ls->ls_recover_flags);
  811. spin_unlock(&ls->ls_recover_spin);
  812. goto restart;
  813. }
  814. if (block_gen == start_gen) {
  815. /* dlm recovery in progress, wait for it to finish */
  816. fs_info(sdp, "control_mount wait3 block %u start %u mount %u "
  817. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  818. lvb_gen, ls->ls_recover_flags);
  819. spin_unlock(&ls->ls_recover_spin);
  820. goto restart;
  821. }
  822. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  823. set_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags);
  824. memset(ls->ls_recover_submit, 0, ls->ls_recover_size*sizeof(uint32_t));
  825. memset(ls->ls_recover_result, 0, ls->ls_recover_size*sizeof(uint32_t));
  826. spin_unlock(&ls->ls_recover_spin);
  827. return 0;
  828. fail:
  829. mounted_unlock(sdp);
  830. control_unlock(sdp);
  831. return error;
  832. }
  833. static int dlm_recovery_wait(void *word)
  834. {
  835. schedule();
  836. return 0;
  837. }
  838. static int control_first_done(struct gfs2_sbd *sdp)
  839. {
  840. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  841. char lvb_bits[GDLM_LVB_SIZE];
  842. uint32_t start_gen, block_gen;
  843. int error;
  844. restart:
  845. spin_lock(&ls->ls_recover_spin);
  846. start_gen = ls->ls_recover_start;
  847. block_gen = ls->ls_recover_block;
  848. if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags) ||
  849. !test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  850. !test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  851. /* sanity check, should not happen */
  852. fs_err(sdp, "control_first_done start %u block %u flags %lx\n",
  853. start_gen, block_gen, ls->ls_recover_flags);
  854. spin_unlock(&ls->ls_recover_spin);
  855. control_unlock(sdp);
  856. return -1;
  857. }
  858. if (start_gen == block_gen) {
  859. /*
  860. * Wait for the end of a dlm recovery cycle to switch from
  861. * first mounter recovery. We can ignore any recover_slot
  862. * callbacks between the recover_prep and next recover_done
  863. * because we are still the first mounter and any failed nodes
  864. * have not fully mounted, so they don't need recovery.
  865. */
  866. spin_unlock(&ls->ls_recover_spin);
  867. fs_info(sdp, "control_first_done wait gen %u\n", start_gen);
  868. wait_on_bit(&ls->ls_recover_flags, DFL_DLM_RECOVERY,
  869. dlm_recovery_wait, TASK_UNINTERRUPTIBLE);
  870. goto restart;
  871. }
  872. clear_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  873. set_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags);
  874. memset(ls->ls_recover_submit, 0, ls->ls_recover_size*sizeof(uint32_t));
  875. memset(ls->ls_recover_result, 0, ls->ls_recover_size*sizeof(uint32_t));
  876. spin_unlock(&ls->ls_recover_spin);
  877. memset(lvb_bits, 0, sizeof(lvb_bits));
  878. control_lvb_write(ls, start_gen, lvb_bits);
  879. error = mounted_lock(sdp, DLM_LOCK_PR, DLM_LKF_CONVERT);
  880. if (error)
  881. fs_err(sdp, "control_first_done mounted PR error %d\n", error);
  882. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  883. if (error)
  884. fs_err(sdp, "control_first_done control NL error %d\n", error);
  885. return error;
  886. }
  887. /*
  888. * Expand static jid arrays if necessary (by increments of RECOVER_SIZE_INC)
  889. * to accomodate the largest slot number. (NB dlm slot numbers start at 1,
  890. * gfs2 jids start at 0, so jid = slot - 1)
  891. */
  892. #define RECOVER_SIZE_INC 16
  893. static int set_recover_size(struct gfs2_sbd *sdp, struct dlm_slot *slots,
  894. int num_slots)
  895. {
  896. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  897. uint32_t *submit = NULL;
  898. uint32_t *result = NULL;
  899. uint32_t old_size, new_size;
  900. int i, max_jid;
  901. max_jid = 0;
  902. for (i = 0; i < num_slots; i++) {
  903. if (max_jid < slots[i].slot - 1)
  904. max_jid = slots[i].slot - 1;
  905. }
  906. old_size = ls->ls_recover_size;
  907. if (old_size >= max_jid + 1)
  908. return 0;
  909. new_size = old_size + RECOVER_SIZE_INC;
  910. submit = kzalloc(new_size * sizeof(uint32_t), GFP_NOFS);
  911. result = kzalloc(new_size * sizeof(uint32_t), GFP_NOFS);
  912. if (!submit || !result) {
  913. kfree(submit);
  914. kfree(result);
  915. return -ENOMEM;
  916. }
  917. spin_lock(&ls->ls_recover_spin);
  918. memcpy(submit, ls->ls_recover_submit, old_size * sizeof(uint32_t));
  919. memcpy(result, ls->ls_recover_result, old_size * sizeof(uint32_t));
  920. kfree(ls->ls_recover_submit);
  921. kfree(ls->ls_recover_result);
  922. ls->ls_recover_submit = submit;
  923. ls->ls_recover_result = result;
  924. ls->ls_recover_size = new_size;
  925. spin_unlock(&ls->ls_recover_spin);
  926. return 0;
  927. }
  928. static void free_recover_size(struct lm_lockstruct *ls)
  929. {
  930. kfree(ls->ls_recover_submit);
  931. kfree(ls->ls_recover_result);
  932. ls->ls_recover_submit = NULL;
  933. ls->ls_recover_result = NULL;
  934. ls->ls_recover_size = 0;
  935. }
  936. /* dlm calls before it does lock recovery */
  937. static void gdlm_recover_prep(void *arg)
  938. {
  939. struct gfs2_sbd *sdp = arg;
  940. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  941. spin_lock(&ls->ls_recover_spin);
  942. ls->ls_recover_block = ls->ls_recover_start;
  943. set_bit(DFL_DLM_RECOVERY, &ls->ls_recover_flags);
  944. if (!test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  945. test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  946. spin_unlock(&ls->ls_recover_spin);
  947. return;
  948. }
  949. set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  950. spin_unlock(&ls->ls_recover_spin);
  951. }
  952. /* dlm calls after recover_prep has been completed on all lockspace members;
  953. identifies slot/jid of failed member */
  954. static void gdlm_recover_slot(void *arg, struct dlm_slot *slot)
  955. {
  956. struct gfs2_sbd *sdp = arg;
  957. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  958. int jid = slot->slot - 1;
  959. spin_lock(&ls->ls_recover_spin);
  960. if (ls->ls_recover_size < jid + 1) {
  961. fs_err(sdp, "recover_slot jid %d gen %u short size %d",
  962. jid, ls->ls_recover_block, ls->ls_recover_size);
  963. spin_unlock(&ls->ls_recover_spin);
  964. return;
  965. }
  966. if (ls->ls_recover_submit[jid]) {
  967. fs_info(sdp, "recover_slot jid %d gen %u prev %u",
  968. jid, ls->ls_recover_block, ls->ls_recover_submit[jid]);
  969. }
  970. ls->ls_recover_submit[jid] = ls->ls_recover_block;
  971. spin_unlock(&ls->ls_recover_spin);
  972. }
  973. /* dlm calls after recover_slot and after it completes lock recovery */
  974. static void gdlm_recover_done(void *arg, struct dlm_slot *slots, int num_slots,
  975. int our_slot, uint32_t generation)
  976. {
  977. struct gfs2_sbd *sdp = arg;
  978. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  979. /* ensure the ls jid arrays are large enough */
  980. set_recover_size(sdp, slots, num_slots);
  981. spin_lock(&ls->ls_recover_spin);
  982. ls->ls_recover_start = generation;
  983. if (!ls->ls_recover_mount) {
  984. ls->ls_recover_mount = generation;
  985. ls->ls_jid = our_slot - 1;
  986. }
  987. if (!test_bit(DFL_UNMOUNT, &ls->ls_recover_flags))
  988. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work, 0);
  989. clear_bit(DFL_DLM_RECOVERY, &ls->ls_recover_flags);
  990. smp_mb__after_clear_bit();
  991. wake_up_bit(&ls->ls_recover_flags, DFL_DLM_RECOVERY);
  992. spin_unlock(&ls->ls_recover_spin);
  993. }
  994. /* gfs2_recover thread has a journal recovery result */
  995. static void gdlm_recovery_result(struct gfs2_sbd *sdp, unsigned int jid,
  996. unsigned int result)
  997. {
  998. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  999. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  1000. return;
  1001. /* don't care about the recovery of own journal during mount */
  1002. if (jid == ls->ls_jid)
  1003. return;
  1004. spin_lock(&ls->ls_recover_spin);
  1005. if (test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  1006. spin_unlock(&ls->ls_recover_spin);
  1007. return;
  1008. }
  1009. if (ls->ls_recover_size < jid + 1) {
  1010. fs_err(sdp, "recovery_result jid %d short size %d",
  1011. jid, ls->ls_recover_size);
  1012. spin_unlock(&ls->ls_recover_spin);
  1013. return;
  1014. }
  1015. fs_info(sdp, "recover jid %d result %s\n", jid,
  1016. result == LM_RD_GAVEUP ? "busy" : "success");
  1017. ls->ls_recover_result[jid] = result;
  1018. /* GAVEUP means another node is recovering the journal; delay our
  1019. next attempt to recover it, to give the other node a chance to
  1020. finish before trying again */
  1021. if (!test_bit(DFL_UNMOUNT, &ls->ls_recover_flags))
  1022. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work,
  1023. result == LM_RD_GAVEUP ? HZ : 0);
  1024. spin_unlock(&ls->ls_recover_spin);
  1025. }
  1026. const struct dlm_lockspace_ops gdlm_lockspace_ops = {
  1027. .recover_prep = gdlm_recover_prep,
  1028. .recover_slot = gdlm_recover_slot,
  1029. .recover_done = gdlm_recover_done,
  1030. };
  1031. static int gdlm_mount(struct gfs2_sbd *sdp, const char *table)
  1032. {
  1033. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1034. char cluster[GFS2_LOCKNAME_LEN];
  1035. const char *fsname;
  1036. uint32_t flags;
  1037. int error, ops_result;
  1038. /*
  1039. * initialize everything
  1040. */
  1041. INIT_DELAYED_WORK(&sdp->sd_control_work, gfs2_control_func);
  1042. spin_lock_init(&ls->ls_recover_spin);
  1043. ls->ls_recover_flags = 0;
  1044. ls->ls_recover_mount = 0;
  1045. ls->ls_recover_start = 0;
  1046. ls->ls_recover_block = 0;
  1047. ls->ls_recover_size = 0;
  1048. ls->ls_recover_submit = NULL;
  1049. ls->ls_recover_result = 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. };