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