lock_dlm.c 39 KB

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