lock_dlm.c 38 KB

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