lock_dlm.c 39 KB

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