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