lock_dlm.c 38 KB

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