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