lock_dlm.c 39 KB

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