lock_dlm.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350
  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 <linux/sched/signal.h>
  17. #include "incore.h"
  18. #include "glock.h"
  19. #include "util.h"
  20. #include "sys.h"
  21. #include "trace_gfs2.h"
  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. fs_err(gl->gl_name.ln_sbd, "unknown bast mode %d\n", mode);
  162. BUG();
  163. }
  164. }
  165. /* convert gfs lock-state to dlm lock-mode */
  166. static int make_mode(struct gfs2_sbd *sdp, 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. fs_err(sdp, "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(gl->gl_name.ln_sbd, 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. fs_err(sdp, "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. /**
  730. * If we're a spectator, we don't want to take the lock in EX because
  731. * we cannot do the first-mount responsibility it implies: recovery.
  732. */
  733. if (sdp->sd_args.ar_spectator)
  734. goto locks_done;
  735. error = mounted_lock(sdp, DLM_LOCK_EX, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE);
  736. if (!error) {
  737. mounted_mode = DLM_LOCK_EX;
  738. goto locks_done;
  739. } else if (error != -EAGAIN) {
  740. fs_err(sdp, "control_mount mounted_lock EX error %d\n", error);
  741. goto fail;
  742. }
  743. error = mounted_lock(sdp, DLM_LOCK_PR, DLM_LKF_CONVERT|DLM_LKF_NOQUEUE);
  744. if (!error) {
  745. mounted_mode = DLM_LOCK_PR;
  746. goto locks_done;
  747. } else {
  748. /* not even -EAGAIN should happen here */
  749. fs_err(sdp, "control_mount mounted_lock PR error %d\n", error);
  750. goto fail;
  751. }
  752. locks_done:
  753. /*
  754. * If we got both locks above in EX, then we're the first mounter.
  755. * If not, then we need to wait for the control_lock lvb to be
  756. * updated by other mounted nodes to reflect our mount generation.
  757. *
  758. * In simple first mounter cases, first mounter will see zero lvb_gen,
  759. * but in cases where all existing nodes leave/fail before mounting
  760. * nodes finish control_mount, then all nodes will be mounting and
  761. * lvb_gen will be non-zero.
  762. */
  763. control_lvb_read(ls, &lvb_gen, ls->ls_lvb_bits);
  764. if (lvb_gen == 0xFFFFFFFF) {
  765. /* special value to force mount attempts to fail */
  766. fs_err(sdp, "control_mount control_lock disabled\n");
  767. error = -EINVAL;
  768. goto fail;
  769. }
  770. if (mounted_mode == DLM_LOCK_EX) {
  771. /* first mounter, keep both EX while doing first recovery */
  772. spin_lock(&ls->ls_recover_spin);
  773. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  774. set_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags);
  775. set_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  776. spin_unlock(&ls->ls_recover_spin);
  777. fs_info(sdp, "first mounter control generation %u\n", lvb_gen);
  778. return 0;
  779. }
  780. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT);
  781. if (error)
  782. goto fail;
  783. /*
  784. * We are not first mounter, now we need to wait for the control_lock
  785. * lvb generation to be >= the generation from our first recover_done
  786. * and all lvb bits to be clear (no pending journal recoveries.)
  787. */
  788. if (!all_jid_bits_clear(ls->ls_lvb_bits)) {
  789. /* journals need recovery, wait until all are clear */
  790. fs_info(sdp, "control_mount wait for journal recovery\n");
  791. goto restart;
  792. }
  793. spin_lock(&ls->ls_recover_spin);
  794. block_gen = ls->ls_recover_block;
  795. start_gen = ls->ls_recover_start;
  796. mount_gen = ls->ls_recover_mount;
  797. if (lvb_gen < mount_gen) {
  798. /* wait for mounted nodes to update control_lock lvb to our
  799. generation, which might include new recovery bits set */
  800. if (sdp->sd_args.ar_spectator) {
  801. fs_info(sdp, "Recovery is required. Waiting for a "
  802. "non-spectator to mount.\n");
  803. msleep_interruptible(1000);
  804. } else {
  805. fs_info(sdp, "control_mount wait1 block %u start %u "
  806. "mount %u lvb %u flags %lx\n", block_gen,
  807. start_gen, mount_gen, lvb_gen,
  808. ls->ls_recover_flags);
  809. }
  810. spin_unlock(&ls->ls_recover_spin);
  811. goto restart;
  812. }
  813. if (lvb_gen != start_gen) {
  814. /* wait for mounted nodes to update control_lock lvb to the
  815. latest recovery generation */
  816. fs_info(sdp, "control_mount wait2 block %u start %u mount %u "
  817. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  818. lvb_gen, ls->ls_recover_flags);
  819. spin_unlock(&ls->ls_recover_spin);
  820. goto restart;
  821. }
  822. if (block_gen == start_gen) {
  823. /* dlm recovery in progress, wait for it to finish */
  824. fs_info(sdp, "control_mount wait3 block %u start %u mount %u "
  825. "lvb %u flags %lx\n", block_gen, start_gen, mount_gen,
  826. lvb_gen, ls->ls_recover_flags);
  827. spin_unlock(&ls->ls_recover_spin);
  828. goto restart;
  829. }
  830. clear_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  831. set_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags);
  832. memset(ls->ls_recover_submit, 0, ls->ls_recover_size*sizeof(uint32_t));
  833. memset(ls->ls_recover_result, 0, ls->ls_recover_size*sizeof(uint32_t));
  834. spin_unlock(&ls->ls_recover_spin);
  835. return 0;
  836. fail:
  837. mounted_unlock(sdp);
  838. control_unlock(sdp);
  839. return error;
  840. }
  841. static int control_first_done(struct gfs2_sbd *sdp)
  842. {
  843. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  844. uint32_t start_gen, block_gen;
  845. int error;
  846. restart:
  847. spin_lock(&ls->ls_recover_spin);
  848. start_gen = ls->ls_recover_start;
  849. block_gen = ls->ls_recover_block;
  850. if (test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags) ||
  851. !test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  852. !test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  853. /* sanity check, should not happen */
  854. fs_err(sdp, "control_first_done start %u block %u flags %lx\n",
  855. start_gen, block_gen, ls->ls_recover_flags);
  856. spin_unlock(&ls->ls_recover_spin);
  857. control_unlock(sdp);
  858. return -1;
  859. }
  860. if (start_gen == block_gen) {
  861. /*
  862. * Wait for the end of a dlm recovery cycle to switch from
  863. * first mounter recovery. We can ignore any recover_slot
  864. * callbacks between the recover_prep and next recover_done
  865. * because we are still the first mounter and any failed nodes
  866. * have not fully mounted, so they don't need recovery.
  867. */
  868. spin_unlock(&ls->ls_recover_spin);
  869. fs_info(sdp, "control_first_done wait gen %u\n", start_gen);
  870. wait_on_bit(&ls->ls_recover_flags, DFL_DLM_RECOVERY,
  871. TASK_UNINTERRUPTIBLE);
  872. goto restart;
  873. }
  874. clear_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  875. set_bit(DFL_FIRST_MOUNT_DONE, &ls->ls_recover_flags);
  876. memset(ls->ls_recover_submit, 0, ls->ls_recover_size*sizeof(uint32_t));
  877. memset(ls->ls_recover_result, 0, ls->ls_recover_size*sizeof(uint32_t));
  878. spin_unlock(&ls->ls_recover_spin);
  879. memset(ls->ls_lvb_bits, 0, GDLM_LVB_SIZE);
  880. control_lvb_write(ls, start_gen, ls->ls_lvb_bits);
  881. error = mounted_lock(sdp, DLM_LOCK_PR, DLM_LKF_CONVERT);
  882. if (error)
  883. fs_err(sdp, "control_first_done mounted PR error %d\n", error);
  884. error = control_lock(sdp, DLM_LOCK_NL, DLM_LKF_CONVERT|DLM_LKF_VALBLK);
  885. if (error)
  886. fs_err(sdp, "control_first_done control NL error %d\n", error);
  887. return error;
  888. }
  889. /*
  890. * Expand static jid arrays if necessary (by increments of RECOVER_SIZE_INC)
  891. * to accomodate the largest slot number. (NB dlm slot numbers start at 1,
  892. * gfs2 jids start at 0, so jid = slot - 1)
  893. */
  894. #define RECOVER_SIZE_INC 16
  895. static int set_recover_size(struct gfs2_sbd *sdp, struct dlm_slot *slots,
  896. int num_slots)
  897. {
  898. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  899. uint32_t *submit = NULL;
  900. uint32_t *result = NULL;
  901. uint32_t old_size, new_size;
  902. int i, max_jid;
  903. if (!ls->ls_lvb_bits) {
  904. ls->ls_lvb_bits = kzalloc(GDLM_LVB_SIZE, GFP_NOFS);
  905. if (!ls->ls_lvb_bits)
  906. return -ENOMEM;
  907. }
  908. max_jid = 0;
  909. for (i = 0; i < num_slots; i++) {
  910. if (max_jid < slots[i].slot - 1)
  911. max_jid = slots[i].slot - 1;
  912. }
  913. old_size = ls->ls_recover_size;
  914. if (old_size >= max_jid + 1)
  915. return 0;
  916. new_size = old_size + RECOVER_SIZE_INC;
  917. submit = kcalloc(new_size, sizeof(uint32_t), GFP_NOFS);
  918. result = kcalloc(new_size, sizeof(uint32_t), GFP_NOFS);
  919. if (!submit || !result) {
  920. kfree(submit);
  921. kfree(result);
  922. return -ENOMEM;
  923. }
  924. spin_lock(&ls->ls_recover_spin);
  925. memcpy(submit, ls->ls_recover_submit, old_size * sizeof(uint32_t));
  926. memcpy(result, ls->ls_recover_result, old_size * sizeof(uint32_t));
  927. kfree(ls->ls_recover_submit);
  928. kfree(ls->ls_recover_result);
  929. ls->ls_recover_submit = submit;
  930. ls->ls_recover_result = result;
  931. ls->ls_recover_size = new_size;
  932. spin_unlock(&ls->ls_recover_spin);
  933. return 0;
  934. }
  935. static void free_recover_size(struct lm_lockstruct *ls)
  936. {
  937. kfree(ls->ls_lvb_bits);
  938. kfree(ls->ls_recover_submit);
  939. kfree(ls->ls_recover_result);
  940. ls->ls_recover_submit = NULL;
  941. ls->ls_recover_result = NULL;
  942. ls->ls_recover_size = 0;
  943. ls->ls_lvb_bits = NULL;
  944. }
  945. /* dlm calls before it does lock recovery */
  946. static void gdlm_recover_prep(void *arg)
  947. {
  948. struct gfs2_sbd *sdp = arg;
  949. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  950. spin_lock(&ls->ls_recover_spin);
  951. ls->ls_recover_block = ls->ls_recover_start;
  952. set_bit(DFL_DLM_RECOVERY, &ls->ls_recover_flags);
  953. if (!test_bit(DFL_MOUNT_DONE, &ls->ls_recover_flags) ||
  954. test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  955. spin_unlock(&ls->ls_recover_spin);
  956. return;
  957. }
  958. set_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags);
  959. spin_unlock(&ls->ls_recover_spin);
  960. }
  961. /* dlm calls after recover_prep has been completed on all lockspace members;
  962. identifies slot/jid of failed member */
  963. static void gdlm_recover_slot(void *arg, struct dlm_slot *slot)
  964. {
  965. struct gfs2_sbd *sdp = arg;
  966. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  967. int jid = slot->slot - 1;
  968. spin_lock(&ls->ls_recover_spin);
  969. if (ls->ls_recover_size < jid + 1) {
  970. fs_err(sdp, "recover_slot jid %d gen %u short size %d\n",
  971. jid, ls->ls_recover_block, ls->ls_recover_size);
  972. spin_unlock(&ls->ls_recover_spin);
  973. return;
  974. }
  975. if (ls->ls_recover_submit[jid]) {
  976. fs_info(sdp, "recover_slot jid %d gen %u prev %u\n",
  977. jid, ls->ls_recover_block, ls->ls_recover_submit[jid]);
  978. }
  979. ls->ls_recover_submit[jid] = ls->ls_recover_block;
  980. spin_unlock(&ls->ls_recover_spin);
  981. }
  982. /* dlm calls after recover_slot and after it completes lock recovery */
  983. static void gdlm_recover_done(void *arg, struct dlm_slot *slots, int num_slots,
  984. int our_slot, uint32_t generation)
  985. {
  986. struct gfs2_sbd *sdp = arg;
  987. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  988. /* ensure the ls jid arrays are large enough */
  989. set_recover_size(sdp, slots, num_slots);
  990. spin_lock(&ls->ls_recover_spin);
  991. ls->ls_recover_start = generation;
  992. if (!ls->ls_recover_mount) {
  993. ls->ls_recover_mount = generation;
  994. ls->ls_jid = our_slot - 1;
  995. }
  996. if (!test_bit(DFL_UNMOUNT, &ls->ls_recover_flags))
  997. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work, 0);
  998. clear_bit(DFL_DLM_RECOVERY, &ls->ls_recover_flags);
  999. smp_mb__after_atomic();
  1000. wake_up_bit(&ls->ls_recover_flags, DFL_DLM_RECOVERY);
  1001. spin_unlock(&ls->ls_recover_spin);
  1002. }
  1003. /* gfs2_recover thread has a journal recovery result */
  1004. static void gdlm_recovery_result(struct gfs2_sbd *sdp, unsigned int jid,
  1005. unsigned int result)
  1006. {
  1007. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1008. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  1009. return;
  1010. /* don't care about the recovery of own journal during mount */
  1011. if (jid == ls->ls_jid)
  1012. return;
  1013. spin_lock(&ls->ls_recover_spin);
  1014. if (test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags)) {
  1015. spin_unlock(&ls->ls_recover_spin);
  1016. return;
  1017. }
  1018. if (ls->ls_recover_size < jid + 1) {
  1019. fs_err(sdp, "recovery_result jid %d short size %d\n",
  1020. jid, ls->ls_recover_size);
  1021. spin_unlock(&ls->ls_recover_spin);
  1022. return;
  1023. }
  1024. fs_info(sdp, "recover jid %d result %s\n", jid,
  1025. result == LM_RD_GAVEUP ? "busy" : "success");
  1026. ls->ls_recover_result[jid] = result;
  1027. /* GAVEUP means another node is recovering the journal; delay our
  1028. next attempt to recover it, to give the other node a chance to
  1029. finish before trying again */
  1030. if (!test_bit(DFL_UNMOUNT, &ls->ls_recover_flags))
  1031. queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work,
  1032. result == LM_RD_GAVEUP ? HZ : 0);
  1033. spin_unlock(&ls->ls_recover_spin);
  1034. }
  1035. static const struct dlm_lockspace_ops gdlm_lockspace_ops = {
  1036. .recover_prep = gdlm_recover_prep,
  1037. .recover_slot = gdlm_recover_slot,
  1038. .recover_done = gdlm_recover_done,
  1039. };
  1040. static int gdlm_mount(struct gfs2_sbd *sdp, const char *table)
  1041. {
  1042. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1043. char cluster[GFS2_LOCKNAME_LEN];
  1044. const char *fsname;
  1045. uint32_t flags;
  1046. int error, ops_result;
  1047. /*
  1048. * initialize everything
  1049. */
  1050. INIT_DELAYED_WORK(&sdp->sd_control_work, gfs2_control_func);
  1051. spin_lock_init(&ls->ls_recover_spin);
  1052. ls->ls_recover_flags = 0;
  1053. ls->ls_recover_mount = 0;
  1054. ls->ls_recover_start = 0;
  1055. ls->ls_recover_block = 0;
  1056. ls->ls_recover_size = 0;
  1057. ls->ls_recover_submit = NULL;
  1058. ls->ls_recover_result = NULL;
  1059. ls->ls_lvb_bits = NULL;
  1060. error = set_recover_size(sdp, NULL, 0);
  1061. if (error)
  1062. goto fail;
  1063. /*
  1064. * prepare dlm_new_lockspace args
  1065. */
  1066. fsname = strchr(table, ':');
  1067. if (!fsname) {
  1068. fs_info(sdp, "no fsname found\n");
  1069. error = -EINVAL;
  1070. goto fail_free;
  1071. }
  1072. memset(cluster, 0, sizeof(cluster));
  1073. memcpy(cluster, table, strlen(table) - strlen(fsname));
  1074. fsname++;
  1075. flags = DLM_LSFL_FS | DLM_LSFL_NEWEXCL;
  1076. /*
  1077. * create/join lockspace
  1078. */
  1079. error = dlm_new_lockspace(fsname, cluster, flags, GDLM_LVB_SIZE,
  1080. &gdlm_lockspace_ops, sdp, &ops_result,
  1081. &ls->ls_dlm);
  1082. if (error) {
  1083. fs_err(sdp, "dlm_new_lockspace error %d\n", error);
  1084. goto fail_free;
  1085. }
  1086. if (ops_result < 0) {
  1087. /*
  1088. * dlm does not support ops callbacks,
  1089. * old dlm_controld/gfs_controld are used, try without ops.
  1090. */
  1091. fs_info(sdp, "dlm lockspace ops not used\n");
  1092. free_recover_size(ls);
  1093. set_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags);
  1094. return 0;
  1095. }
  1096. if (!test_bit(SDF_NOJOURNALID, &sdp->sd_flags)) {
  1097. fs_err(sdp, "dlm lockspace ops disallow jid preset\n");
  1098. error = -EINVAL;
  1099. goto fail_release;
  1100. }
  1101. /*
  1102. * control_mount() uses control_lock to determine first mounter,
  1103. * and for later mounts, waits for any recoveries to be cleared.
  1104. */
  1105. error = control_mount(sdp);
  1106. if (error) {
  1107. fs_err(sdp, "mount control error %d\n", error);
  1108. goto fail_release;
  1109. }
  1110. ls->ls_first = !!test_bit(DFL_FIRST_MOUNT, &ls->ls_recover_flags);
  1111. clear_bit(SDF_NOJOURNALID, &sdp->sd_flags);
  1112. smp_mb__after_atomic();
  1113. wake_up_bit(&sdp->sd_flags, SDF_NOJOURNALID);
  1114. return 0;
  1115. fail_release:
  1116. dlm_release_lockspace(ls->ls_dlm, 2);
  1117. fail_free:
  1118. free_recover_size(ls);
  1119. fail:
  1120. return error;
  1121. }
  1122. static void gdlm_first_done(struct gfs2_sbd *sdp)
  1123. {
  1124. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1125. int error;
  1126. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  1127. return;
  1128. error = control_first_done(sdp);
  1129. if (error)
  1130. fs_err(sdp, "mount first_done error %d\n", error);
  1131. }
  1132. static void gdlm_unmount(struct gfs2_sbd *sdp)
  1133. {
  1134. struct lm_lockstruct *ls = &sdp->sd_lockstruct;
  1135. if (test_bit(DFL_NO_DLM_OPS, &ls->ls_recover_flags))
  1136. goto release;
  1137. /* wait for gfs2_control_wq to be done with this mount */
  1138. spin_lock(&ls->ls_recover_spin);
  1139. set_bit(DFL_UNMOUNT, &ls->ls_recover_flags);
  1140. spin_unlock(&ls->ls_recover_spin);
  1141. flush_delayed_work(&sdp->sd_control_work);
  1142. /* mounted_lock and control_lock will be purged in dlm recovery */
  1143. release:
  1144. if (ls->ls_dlm) {
  1145. dlm_release_lockspace(ls->ls_dlm, 2);
  1146. ls->ls_dlm = NULL;
  1147. }
  1148. free_recover_size(ls);
  1149. }
  1150. static const match_table_t dlm_tokens = {
  1151. { Opt_jid, "jid=%d"},
  1152. { Opt_id, "id=%d"},
  1153. { Opt_first, "first=%d"},
  1154. { Opt_nodir, "nodir=%d"},
  1155. { Opt_err, NULL },
  1156. };
  1157. const struct lm_lockops gfs2_dlm_ops = {
  1158. .lm_proto_name = "lock_dlm",
  1159. .lm_mount = gdlm_mount,
  1160. .lm_first_done = gdlm_first_done,
  1161. .lm_recovery_result = gdlm_recovery_result,
  1162. .lm_unmount = gdlm_unmount,
  1163. .lm_put_lock = gdlm_put_lock,
  1164. .lm_lock = gdlm_lock,
  1165. .lm_cancel = gdlm_cancel,
  1166. .lm_tokens = &dlm_tokens,
  1167. };