1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 4 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/sched.h> 10 #include <linux/slab.h> 11 #include <linux/spinlock.h> 12 #include <linux/buffer_head.h> 13 #include <linux/delay.h> 14 #include <linux/sort.h> 15 #include <linux/hash.h> 16 #include <linux/jhash.h> 17 #include <linux/kallsyms.h> 18 #include <linux/gfs2_ondisk.h> 19 #include <linux/list.h> 20 #include <linux/wait.h> 21 #include <linux/module.h> 22 #include <linux/uaccess.h> 23 #include <linux/seq_file.h> 24 #include <linux/debugfs.h> 25 #include <linux/kthread.h> 26 #include <linux/freezer.h> 27 #include <linux/workqueue.h> 28 #include <linux/jiffies.h> 29 #include <linux/rcupdate.h> 30 #include <linux/rculist_bl.h> 31 #include <linux/bit_spinlock.h> 32 #include <linux/percpu.h> 33 #include <linux/list_sort.h> 34 #include <linux/lockref.h> 35 #include <linux/rhashtable.h> 36 #include <linux/pid_namespace.h> 37 #include <linux/fdtable.h> 38 #include <linux/file.h> 39 40 #include "gfs2.h" 41 #include "incore.h" 42 #include "glock.h" 43 #include "glops.h" 44 #include "inode.h" 45 #include "lops.h" 46 #include "meta_io.h" 47 #include "quota.h" 48 #include "super.h" 49 #include "util.h" 50 #include "bmap.h" 51 #define CREATE_TRACE_POINTS 52 #include "trace_gfs2.h" 53 54 struct gfs2_glock_iter { 55 struct gfs2_sbd *sdp; /* incore superblock */ 56 struct rhashtable_iter hti; /* rhashtable iterator */ 57 struct gfs2_glock *gl; /* current glock struct */ 58 loff_t last_pos; /* last position */ 59 }; 60 61 typedef void (*glock_examiner) (struct gfs2_glock * gl); 62 63 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target); 64 static void __gfs2_glock_dq(struct gfs2_holder *gh); 65 static void handle_callback(struct gfs2_glock *gl, unsigned int state, 66 unsigned long delay, bool remote); 67 68 static struct dentry *gfs2_root; 69 static struct workqueue_struct *glock_workqueue; 70 static LIST_HEAD(lru_list); 71 static atomic_t lru_count = ATOMIC_INIT(0); 72 static DEFINE_SPINLOCK(lru_lock); 73 74 #define GFS2_GL_HASH_SHIFT 15 75 #define GFS2_GL_HASH_SIZE BIT(GFS2_GL_HASH_SHIFT) 76 77 static const struct rhashtable_params ht_parms = { 78 .nelem_hint = GFS2_GL_HASH_SIZE * 3 / 4, 79 .key_len = offsetofend(struct lm_lockname, ln_type), 80 .key_offset = offsetof(struct gfs2_glock, gl_name), 81 .head_offset = offsetof(struct gfs2_glock, gl_node), 82 }; 83 84 static struct rhashtable gl_hash_table; 85 86 #define GLOCK_WAIT_TABLE_BITS 12 87 #define GLOCK_WAIT_TABLE_SIZE (1 << GLOCK_WAIT_TABLE_BITS) 88 static wait_queue_head_t glock_wait_table[GLOCK_WAIT_TABLE_SIZE] __cacheline_aligned; 89 90 struct wait_glock_queue { 91 struct lm_lockname *name; 92 wait_queue_entry_t wait; 93 }; 94 95 static int glock_wake_function(wait_queue_entry_t *wait, unsigned int mode, 96 int sync, void *key) 97 { 98 struct wait_glock_queue *wait_glock = 99 container_of(wait, struct wait_glock_queue, wait); 100 struct lm_lockname *wait_name = wait_glock->name; 101 struct lm_lockname *wake_name = key; 102 103 if (wake_name->ln_sbd != wait_name->ln_sbd || 104 wake_name->ln_number != wait_name->ln_number || 105 wake_name->ln_type != wait_name->ln_type) 106 return 0; 107 return autoremove_wake_function(wait, mode, sync, key); 108 } 109 110 static wait_queue_head_t *glock_waitqueue(struct lm_lockname *name) 111 { 112 u32 hash = jhash2((u32 *)name, ht_parms.key_len / 4, 0); 113 114 return glock_wait_table + hash_32(hash, GLOCK_WAIT_TABLE_BITS); 115 } 116 117 /** 118 * wake_up_glock - Wake up waiters on a glock 119 * @gl: the glock 120 */ 121 static void wake_up_glock(struct gfs2_glock *gl) 122 { 123 wait_queue_head_t *wq = glock_waitqueue(&gl->gl_name); 124 125 if (waitqueue_active(wq)) 126 __wake_up(wq, TASK_NORMAL, 1, &gl->gl_name); 127 } 128 129 static void gfs2_glock_dealloc(struct rcu_head *rcu) 130 { 131 struct gfs2_glock *gl = container_of(rcu, struct gfs2_glock, gl_rcu); 132 133 kfree(gl->gl_lksb.sb_lvbptr); 134 if (gl->gl_ops->go_flags & GLOF_ASPACE) { 135 struct gfs2_glock_aspace *gla = 136 container_of(gl, struct gfs2_glock_aspace, glock); 137 kmem_cache_free(gfs2_glock_aspace_cachep, gla); 138 } else 139 kmem_cache_free(gfs2_glock_cachep, gl); 140 } 141 142 /** 143 * glock_blocked_by_withdraw - determine if we can still use a glock 144 * @gl: the glock 145 * 146 * We need to allow some glocks to be enqueued, dequeued, promoted, and demoted 147 * when we're withdrawn. For example, to maintain metadata integrity, we should 148 * disallow the use of inode and rgrp glocks when withdrawn. Other glocks like 149 * the iopen or freeze glock may be safely used because none of their 150 * metadata goes through the journal. So in general, we should disallow all 151 * glocks that are journaled, and allow all the others. One exception is: 152 * we need to allow our active journal to be promoted and demoted so others 153 * may recover it and we can reacquire it when they're done. 154 */ 155 static bool glock_blocked_by_withdraw(struct gfs2_glock *gl) 156 { 157 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 158 159 if (!gfs2_withdrawing_or_withdrawn(sdp)) 160 return false; 161 if (gl->gl_ops->go_flags & GLOF_NONDISK) 162 return false; 163 if (!sdp->sd_jdesc || 164 gl->gl_name.ln_number == sdp->sd_jdesc->jd_no_addr) 165 return false; 166 return true; 167 } 168 169 static void __gfs2_glock_free(struct gfs2_glock *gl) 170 { 171 rhashtable_remove_fast(&gl_hash_table, &gl->gl_node, ht_parms); 172 smp_mb(); 173 wake_up_glock(gl); 174 call_rcu(&gl->gl_rcu, gfs2_glock_dealloc); 175 } 176 177 void gfs2_glock_free(struct gfs2_glock *gl) { 178 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 179 180 __gfs2_glock_free(gl); 181 if (atomic_dec_and_test(&sdp->sd_glock_disposal)) 182 wake_up(&sdp->sd_kill_wait); 183 } 184 185 void gfs2_glock_free_later(struct gfs2_glock *gl) { 186 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 187 188 spin_lock(&lru_lock); 189 list_add(&gl->gl_lru, &sdp->sd_dead_glocks); 190 spin_unlock(&lru_lock); 191 if (atomic_dec_and_test(&sdp->sd_glock_disposal)) 192 wake_up(&sdp->sd_kill_wait); 193 } 194 195 static void gfs2_free_dead_glocks(struct gfs2_sbd *sdp) 196 { 197 struct list_head *list = &sdp->sd_dead_glocks; 198 199 while(!list_empty(list)) { 200 struct gfs2_glock *gl; 201 202 gl = list_first_entry(list, struct gfs2_glock, gl_lru); 203 list_del_init(&gl->gl_lru); 204 __gfs2_glock_free(gl); 205 } 206 } 207 208 /** 209 * gfs2_glock_hold() - increment reference count on glock 210 * @gl: The glock to hold 211 * 212 */ 213 214 struct gfs2_glock *gfs2_glock_hold(struct gfs2_glock *gl) 215 { 216 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref)); 217 lockref_get(&gl->gl_lockref); 218 return gl; 219 } 220 221 /** 222 * demote_ok - Check to see if it's ok to unlock a glock 223 * @gl: the glock 224 * 225 * Returns: 1 if it's ok 226 */ 227 228 static int demote_ok(const struct gfs2_glock *gl) 229 { 230 const struct gfs2_glock_operations *glops = gl->gl_ops; 231 232 if (gl->gl_state == LM_ST_UNLOCKED) 233 return 0; 234 if (!list_empty(&gl->gl_holders)) 235 return 0; 236 if (glops->go_demote_ok) 237 return glops->go_demote_ok(gl); 238 return 1; 239 } 240 241 242 void gfs2_glock_add_to_lru(struct gfs2_glock *gl) 243 { 244 if (!(gl->gl_ops->go_flags & GLOF_LRU)) 245 return; 246 247 spin_lock(&lru_lock); 248 249 list_move_tail(&gl->gl_lru, &lru_list); 250 251 if (!test_bit(GLF_LRU, &gl->gl_flags)) { 252 set_bit(GLF_LRU, &gl->gl_flags); 253 atomic_inc(&lru_count); 254 } 255 256 spin_unlock(&lru_lock); 257 } 258 259 static void gfs2_glock_remove_from_lru(struct gfs2_glock *gl) 260 { 261 if (!(gl->gl_ops->go_flags & GLOF_LRU)) 262 return; 263 264 spin_lock(&lru_lock); 265 if (test_bit(GLF_LRU, &gl->gl_flags)) { 266 list_del_init(&gl->gl_lru); 267 atomic_dec(&lru_count); 268 clear_bit(GLF_LRU, &gl->gl_flags); 269 } 270 spin_unlock(&lru_lock); 271 } 272 273 /* 274 * Enqueue the glock on the work queue. Passes one glock reference on to the 275 * work queue. 276 */ 277 static void __gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) { 278 if (!queue_delayed_work(glock_workqueue, &gl->gl_work, delay)) { 279 /* 280 * We are holding the lockref spinlock, and the work was still 281 * queued above. The queued work (glock_work_func) takes that 282 * spinlock before dropping its glock reference(s), so it 283 * cannot have dropped them in the meantime. 284 */ 285 GLOCK_BUG_ON(gl, gl->gl_lockref.count < 2); 286 gl->gl_lockref.count--; 287 } 288 } 289 290 static void gfs2_glock_queue_work(struct gfs2_glock *gl, unsigned long delay) { 291 spin_lock(&gl->gl_lockref.lock); 292 __gfs2_glock_queue_work(gl, delay); 293 spin_unlock(&gl->gl_lockref.lock); 294 } 295 296 static void __gfs2_glock_put(struct gfs2_glock *gl) 297 { 298 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 299 struct address_space *mapping = gfs2_glock2aspace(gl); 300 301 lockref_mark_dead(&gl->gl_lockref); 302 spin_unlock(&gl->gl_lockref.lock); 303 gfs2_glock_remove_from_lru(gl); 304 GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders)); 305 if (mapping) { 306 truncate_inode_pages_final(mapping); 307 if (!gfs2_withdrawing_or_withdrawn(sdp)) 308 GLOCK_BUG_ON(gl, !mapping_empty(mapping)); 309 } 310 trace_gfs2_glock_put(gl); 311 sdp->sd_lockstruct.ls_ops->lm_put_lock(gl); 312 } 313 314 /** 315 * gfs2_glock_put() - Decrement reference count on glock 316 * @gl: The glock to put 317 * 318 */ 319 320 void gfs2_glock_put(struct gfs2_glock *gl) 321 { 322 if (lockref_put_or_lock(&gl->gl_lockref)) 323 return; 324 325 __gfs2_glock_put(gl); 326 } 327 328 /* 329 * gfs2_glock_put_async - Decrement reference count without sleeping 330 * @gl: The glock to put 331 * 332 * Decrement the reference count on glock immediately unless it is the last 333 * reference. Defer putting the last reference to work queue context. 334 */ 335 void gfs2_glock_put_async(struct gfs2_glock *gl) 336 { 337 if (lockref_put_or_lock(&gl->gl_lockref)) 338 return; 339 340 __gfs2_glock_queue_work(gl, 0); 341 spin_unlock(&gl->gl_lockref.lock); 342 } 343 344 /** 345 * may_grant - check if it's ok to grant a new lock 346 * @gl: The glock 347 * @current_gh: One of the current holders of @gl 348 * @gh: The lock request which we wish to grant 349 * 350 * With our current compatibility rules, if a glock has one or more active 351 * holders (HIF_HOLDER flag set), any of those holders can be passed in as 352 * @current_gh; they are all the same as far as compatibility with the new @gh 353 * goes. 354 * 355 * Returns true if it's ok to grant the lock. 356 */ 357 358 static inline bool may_grant(struct gfs2_glock *gl, 359 struct gfs2_holder *current_gh, 360 struct gfs2_holder *gh) 361 { 362 if (current_gh) { 363 GLOCK_BUG_ON(gl, !test_bit(HIF_HOLDER, ¤t_gh->gh_iflags)); 364 365 switch(current_gh->gh_state) { 366 case LM_ST_EXCLUSIVE: 367 /* 368 * Here we make a special exception to grant holders 369 * who agree to share the EX lock with other holders 370 * who also have the bit set. If the original holder 371 * has the LM_FLAG_NODE_SCOPE bit set, we grant more 372 * holders with the bit set. 373 */ 374 return gh->gh_state == LM_ST_EXCLUSIVE && 375 (current_gh->gh_flags & LM_FLAG_NODE_SCOPE) && 376 (gh->gh_flags & LM_FLAG_NODE_SCOPE); 377 378 case LM_ST_SHARED: 379 case LM_ST_DEFERRED: 380 return gh->gh_state == current_gh->gh_state; 381 382 default: 383 return false; 384 } 385 } 386 387 if (gl->gl_state == gh->gh_state) 388 return true; 389 if (gh->gh_flags & GL_EXACT) 390 return false; 391 if (gl->gl_state == LM_ST_EXCLUSIVE) { 392 return gh->gh_state == LM_ST_SHARED || 393 gh->gh_state == LM_ST_DEFERRED; 394 } 395 if (gh->gh_flags & LM_FLAG_ANY) 396 return gl->gl_state != LM_ST_UNLOCKED; 397 return false; 398 } 399 400 static void gfs2_holder_wake(struct gfs2_holder *gh) 401 { 402 clear_bit(HIF_WAIT, &gh->gh_iflags); 403 smp_mb__after_atomic(); 404 wake_up_bit(&gh->gh_iflags, HIF_WAIT); 405 if (gh->gh_flags & GL_ASYNC) { 406 struct gfs2_sbd *sdp = gh->gh_gl->gl_name.ln_sbd; 407 408 wake_up(&sdp->sd_async_glock_wait); 409 } 410 } 411 412 /** 413 * do_error - Something unexpected has happened during a lock request 414 * @gl: The glock 415 * @ret: The status from the DLM 416 */ 417 418 static void do_error(struct gfs2_glock *gl, const int ret) 419 { 420 struct gfs2_holder *gh, *tmp; 421 422 list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) { 423 if (test_bit(HIF_HOLDER, &gh->gh_iflags)) 424 continue; 425 if (ret & LM_OUT_ERROR) 426 gh->gh_error = -EIO; 427 else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) 428 gh->gh_error = GLR_TRYFAILED; 429 else 430 continue; 431 list_del_init(&gh->gh_list); 432 trace_gfs2_glock_queue(gh, 0); 433 gfs2_holder_wake(gh); 434 } 435 } 436 437 /** 438 * find_first_holder - find the first "holder" gh 439 * @gl: the glock 440 */ 441 442 static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl) 443 { 444 struct gfs2_holder *gh; 445 446 if (!list_empty(&gl->gl_holders)) { 447 gh = list_first_entry(&gl->gl_holders, struct gfs2_holder, 448 gh_list); 449 if (test_bit(HIF_HOLDER, &gh->gh_iflags)) 450 return gh; 451 } 452 return NULL; 453 } 454 455 /* 456 * gfs2_instantiate - Call the glops instantiate function 457 * @gh: The glock holder 458 * 459 * Returns: 0 if instantiate was successful, or error. 460 */ 461 int gfs2_instantiate(struct gfs2_holder *gh) 462 { 463 struct gfs2_glock *gl = gh->gh_gl; 464 const struct gfs2_glock_operations *glops = gl->gl_ops; 465 int ret; 466 467 again: 468 if (!test_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags)) 469 goto done; 470 471 /* 472 * Since we unlock the lockref lock, we set a flag to indicate 473 * instantiate is in progress. 474 */ 475 if (test_and_set_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags)) { 476 wait_on_bit(&gl->gl_flags, GLF_INSTANTIATE_IN_PROG, 477 TASK_UNINTERRUPTIBLE); 478 /* 479 * Here we just waited for a different instantiate to finish. 480 * But that may not have been successful, as when a process 481 * locks an inode glock _before_ it has an actual inode to 482 * instantiate into. So we check again. This process might 483 * have an inode to instantiate, so might be successful. 484 */ 485 goto again; 486 } 487 488 ret = glops->go_instantiate(gl); 489 if (!ret) 490 clear_bit(GLF_INSTANTIATE_NEEDED, &gl->gl_flags); 491 clear_and_wake_up_bit(GLF_INSTANTIATE_IN_PROG, &gl->gl_flags); 492 if (ret) 493 return ret; 494 495 done: 496 if (glops->go_held) 497 return glops->go_held(gh); 498 return 0; 499 } 500 501 /** 502 * do_promote - promote as many requests as possible on the current queue 503 * @gl: The glock 504 * 505 * Returns true on success (i.e., progress was made or there are no waiters). 506 */ 507 508 static bool do_promote(struct gfs2_glock *gl) 509 { 510 struct gfs2_holder *gh, *current_gh; 511 512 current_gh = find_first_holder(gl); 513 list_for_each_entry(gh, &gl->gl_holders, gh_list) { 514 if (test_bit(HIF_HOLDER, &gh->gh_iflags)) 515 continue; 516 if (!may_grant(gl, current_gh, gh)) { 517 /* 518 * If we get here, it means we may not grant this 519 * holder for some reason. If this holder is at the 520 * head of the list, it means we have a blocked holder 521 * at the head, so return false. 522 */ 523 if (list_is_first(&gh->gh_list, &gl->gl_holders)) 524 return false; 525 do_error(gl, 0); 526 break; 527 } 528 set_bit(HIF_HOLDER, &gh->gh_iflags); 529 trace_gfs2_promote(gh); 530 gfs2_holder_wake(gh); 531 if (!current_gh) 532 current_gh = gh; 533 } 534 return true; 535 } 536 537 /** 538 * find_first_waiter - find the first gh that's waiting for the glock 539 * @gl: the glock 540 */ 541 542 static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl) 543 { 544 struct gfs2_holder *gh; 545 546 list_for_each_entry(gh, &gl->gl_holders, gh_list) { 547 if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) 548 return gh; 549 } 550 return NULL; 551 } 552 553 /** 554 * state_change - record that the glock is now in a different state 555 * @gl: the glock 556 * @new_state: the new state 557 */ 558 559 static void state_change(struct gfs2_glock *gl, unsigned int new_state) 560 { 561 int held1, held2; 562 563 held1 = (gl->gl_state != LM_ST_UNLOCKED); 564 held2 = (new_state != LM_ST_UNLOCKED); 565 566 if (held1 != held2) { 567 GLOCK_BUG_ON(gl, __lockref_is_dead(&gl->gl_lockref)); 568 if (held2) 569 gl->gl_lockref.count++; 570 else 571 gl->gl_lockref.count--; 572 } 573 if (new_state != gl->gl_target) 574 /* shorten our minimum hold time */ 575 gl->gl_hold_time = max(gl->gl_hold_time - GL_GLOCK_HOLD_DECR, 576 GL_GLOCK_MIN_HOLD); 577 gl->gl_state = new_state; 578 gl->gl_tchange = jiffies; 579 } 580 581 static void gfs2_set_demote(struct gfs2_glock *gl) 582 { 583 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 584 585 set_bit(GLF_DEMOTE, &gl->gl_flags); 586 smp_mb(); 587 wake_up(&sdp->sd_async_glock_wait); 588 } 589 590 static void gfs2_demote_wake(struct gfs2_glock *gl) 591 { 592 gl->gl_demote_state = LM_ST_EXCLUSIVE; 593 clear_bit(GLF_DEMOTE, &gl->gl_flags); 594 smp_mb__after_atomic(); 595 wake_up_bit(&gl->gl_flags, GLF_DEMOTE); 596 } 597 598 /** 599 * finish_xmote - The DLM has replied to one of our lock requests 600 * @gl: The glock 601 * @ret: The status from the DLM 602 * 603 */ 604 605 static void finish_xmote(struct gfs2_glock *gl, unsigned int ret) 606 { 607 const struct gfs2_glock_operations *glops = gl->gl_ops; 608 struct gfs2_holder *gh; 609 unsigned state = ret & LM_OUT_ST_MASK; 610 611 trace_gfs2_glock_state_change(gl, state); 612 state_change(gl, state); 613 gh = find_first_waiter(gl); 614 615 /* Demote to UN request arrived during demote to SH or DF */ 616 if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) && 617 state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED) 618 gl->gl_target = LM_ST_UNLOCKED; 619 620 /* Check for state != intended state */ 621 if (unlikely(state != gl->gl_target)) { 622 if (gh && (ret & LM_OUT_CANCELED)) 623 gfs2_holder_wake(gh); 624 if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) { 625 /* move to back of queue and try next entry */ 626 if (ret & LM_OUT_CANCELED) { 627 list_move_tail(&gh->gh_list, &gl->gl_holders); 628 gh = find_first_waiter(gl); 629 gl->gl_target = gh->gh_state; 630 if (do_promote(gl)) 631 goto out; 632 goto retry; 633 } 634 /* Some error or failed "try lock" - report it */ 635 if ((ret & LM_OUT_ERROR) || 636 (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) { 637 gl->gl_target = gl->gl_state; 638 do_error(gl, ret); 639 goto out; 640 } 641 } 642 switch(state) { 643 /* Unlocked due to conversion deadlock, try again */ 644 case LM_ST_UNLOCKED: 645 retry: 646 do_xmote(gl, gh, gl->gl_target); 647 break; 648 /* Conversion fails, unlock and try again */ 649 case LM_ST_SHARED: 650 case LM_ST_DEFERRED: 651 do_xmote(gl, gh, LM_ST_UNLOCKED); 652 break; 653 default: /* Everything else */ 654 fs_err(gl->gl_name.ln_sbd, "wanted %u got %u\n", 655 gl->gl_target, state); 656 GLOCK_BUG_ON(gl, 1); 657 } 658 return; 659 } 660 661 /* Fast path - we got what we asked for */ 662 if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) 663 gfs2_demote_wake(gl); 664 if (state != LM_ST_UNLOCKED) { 665 if (glops->go_xmote_bh) { 666 int rv; 667 668 spin_unlock(&gl->gl_lockref.lock); 669 rv = glops->go_xmote_bh(gl); 670 spin_lock(&gl->gl_lockref.lock); 671 if (rv) { 672 do_error(gl, rv); 673 goto out; 674 } 675 } 676 do_promote(gl); 677 } 678 out: 679 clear_bit(GLF_LOCK, &gl->gl_flags); 680 } 681 682 static bool is_system_glock(struct gfs2_glock *gl) 683 { 684 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 685 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode); 686 687 if (gl == m_ip->i_gl) 688 return true; 689 return false; 690 } 691 692 /** 693 * do_xmote - Calls the DLM to change the state of a lock 694 * @gl: The lock state 695 * @gh: The holder (only for promotes) 696 * @target: The target lock state 697 * 698 */ 699 700 static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, 701 unsigned int target) 702 __releases(&gl->gl_lockref.lock) 703 __acquires(&gl->gl_lockref.lock) 704 { 705 const struct gfs2_glock_operations *glops = gl->gl_ops; 706 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 707 struct lm_lockstruct *ls = &sdp->sd_lockstruct; 708 unsigned int lck_flags = (unsigned int)(gh ? gh->gh_flags : 0); 709 int ret; 710 711 if (target != LM_ST_UNLOCKED && glock_blocked_by_withdraw(gl) && 712 gh && !(gh->gh_flags & LM_FLAG_NOEXP)) 713 goto skip_inval; 714 715 lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP); 716 GLOCK_BUG_ON(gl, gl->gl_state == target); 717 GLOCK_BUG_ON(gl, gl->gl_state == gl->gl_target); 718 if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) && 719 glops->go_inval) { 720 /* 721 * If another process is already doing the invalidate, let that 722 * finish first. The glock state machine will get back to this 723 * holder again later. 724 */ 725 if (test_and_set_bit(GLF_INVALIDATE_IN_PROGRESS, 726 &gl->gl_flags)) 727 return; 728 do_error(gl, 0); /* Fail queued try locks */ 729 } 730 gl->gl_req = target; 731 set_bit(GLF_BLOCKING, &gl->gl_flags); 732 if ((gl->gl_req == LM_ST_UNLOCKED) || 733 (gl->gl_state == LM_ST_EXCLUSIVE) || 734 (lck_flags & (LM_FLAG_TRY|LM_FLAG_TRY_1CB))) 735 clear_bit(GLF_BLOCKING, &gl->gl_flags); 736 if (!glops->go_inval && !glops->go_sync) 737 goto skip_inval; 738 739 spin_unlock(&gl->gl_lockref.lock); 740 if (glops->go_sync) { 741 ret = glops->go_sync(gl); 742 /* If we had a problem syncing (due to io errors or whatever, 743 * we should not invalidate the metadata or tell dlm to 744 * release the glock to other nodes. 745 */ 746 if (ret) { 747 if (cmpxchg(&sdp->sd_log_error, 0, ret)) { 748 fs_err(sdp, "Error %d syncing glock \n", ret); 749 gfs2_dump_glock(NULL, gl, true); 750 } 751 spin_lock(&gl->gl_lockref.lock); 752 goto skip_inval; 753 } 754 } 755 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) { 756 /* 757 * The call to go_sync should have cleared out the ail list. 758 * If there are still items, we have a problem. We ought to 759 * withdraw, but we can't because the withdraw code also uses 760 * glocks. Warn about the error, dump the glock, then fall 761 * through and wait for logd to do the withdraw for us. 762 */ 763 if ((atomic_read(&gl->gl_ail_count) != 0) && 764 (!cmpxchg(&sdp->sd_log_error, 0, -EIO))) { 765 gfs2_glock_assert_warn(gl, 766 !atomic_read(&gl->gl_ail_count)); 767 gfs2_dump_glock(NULL, gl, true); 768 } 769 glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA); 770 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags); 771 } 772 spin_lock(&gl->gl_lockref.lock); 773 774 skip_inval: 775 gl->gl_lockref.count++; 776 /* 777 * Check for an error encountered since we called go_sync and go_inval. 778 * If so, we can't withdraw from the glock code because the withdraw 779 * code itself uses glocks (see function signal_our_withdraw) to 780 * change the mount to read-only. Most importantly, we must not call 781 * dlm to unlock the glock until the journal is in a known good state 782 * (after journal replay) otherwise other nodes may use the object 783 * (rgrp or dinode) and then later, journal replay will corrupt the 784 * file system. The best we can do here is wait for the logd daemon 785 * to see sd_log_error and withdraw, and in the meantime, requeue the 786 * work for later. 787 * 788 * We make a special exception for some system glocks, such as the 789 * system statfs inode glock, which needs to be granted before the 790 * gfs2_quotad daemon can exit, and that exit needs to finish before 791 * we can unmount the withdrawn file system. 792 * 793 * However, if we're just unlocking the lock (say, for unmount, when 794 * gfs2_gl_hash_clear calls clear_glock) and recovery is complete 795 * then it's okay to tell dlm to unlock it. 796 */ 797 if (unlikely(sdp->sd_log_error) && !gfs2_withdrawing_or_withdrawn(sdp)) 798 gfs2_withdraw_delayed(sdp); 799 if (glock_blocked_by_withdraw(gl) && 800 (target != LM_ST_UNLOCKED || 801 test_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags))) { 802 if (!is_system_glock(gl)) { 803 handle_callback(gl, LM_ST_UNLOCKED, 0, false); /* sets demote */ 804 /* 805 * Ordinarily, we would call dlm and its callback would call 806 * finish_xmote, which would call state_change() to the new state. 807 * Since we withdrew, we won't call dlm, so call state_change 808 * manually, but to the UNLOCKED state we desire. 809 */ 810 state_change(gl, LM_ST_UNLOCKED); 811 /* 812 * We skip telling dlm to do the locking, so we won't get a 813 * reply that would otherwise clear GLF_LOCK. So we clear it here. 814 */ 815 clear_bit(GLF_LOCK, &gl->gl_flags); 816 clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags); 817 __gfs2_glock_queue_work(gl, GL_GLOCK_DFT_HOLD); 818 return; 819 } else { 820 clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags); 821 } 822 } 823 824 if (ls->ls_ops->lm_lock) { 825 spin_unlock(&gl->gl_lockref.lock); 826 ret = ls->ls_ops->lm_lock(gl, target, lck_flags); 827 spin_lock(&gl->gl_lockref.lock); 828 829 if (ret == -EINVAL && gl->gl_target == LM_ST_UNLOCKED && 830 target == LM_ST_UNLOCKED && 831 test_bit(DFL_UNMOUNT, &ls->ls_recover_flags)) { 832 /* 833 * The lockspace has been released and the lock has 834 * been unlocked implicitly. 835 */ 836 } else if (ret) { 837 fs_err(sdp, "lm_lock ret %d\n", ret); 838 target = gl->gl_state | LM_OUT_ERROR; 839 } else { 840 /* The operation will be completed asynchronously. */ 841 return; 842 } 843 } 844 845 /* Complete the operation now. */ 846 finish_xmote(gl, target); 847 __gfs2_glock_queue_work(gl, 0); 848 } 849 850 /** 851 * run_queue - do all outstanding tasks related to a glock 852 * @gl: The glock in question 853 * @nonblock: True if we must not block in run_queue 854 * 855 */ 856 857 static void run_queue(struct gfs2_glock *gl, const int nonblock) 858 __releases(&gl->gl_lockref.lock) 859 __acquires(&gl->gl_lockref.lock) 860 { 861 struct gfs2_holder *gh = NULL; 862 863 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) 864 return; 865 866 GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)); 867 868 if (test_bit(GLF_DEMOTE, &gl->gl_flags) && 869 gl->gl_demote_state != gl->gl_state) { 870 if (find_first_holder(gl)) 871 goto out_unlock; 872 if (nonblock) 873 goto out_sched; 874 set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags); 875 GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE); 876 gl->gl_target = gl->gl_demote_state; 877 } else { 878 if (test_bit(GLF_DEMOTE, &gl->gl_flags)) 879 gfs2_demote_wake(gl); 880 if (do_promote(gl)) 881 goto out_unlock; 882 gh = find_first_waiter(gl); 883 gl->gl_target = gh->gh_state; 884 if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) 885 do_error(gl, 0); /* Fail queued try locks */ 886 } 887 do_xmote(gl, gh, gl->gl_target); 888 return; 889 890 out_sched: 891 clear_bit(GLF_LOCK, &gl->gl_flags); 892 smp_mb__after_atomic(); 893 gl->gl_lockref.count++; 894 __gfs2_glock_queue_work(gl, 0); 895 return; 896 897 out_unlock: 898 clear_bit(GLF_LOCK, &gl->gl_flags); 899 smp_mb__after_atomic(); 900 return; 901 } 902 903 /** 904 * glock_set_object - set the gl_object field of a glock 905 * @gl: the glock 906 * @object: the object 907 */ 908 void glock_set_object(struct gfs2_glock *gl, void *object) 909 { 910 void *prev_object; 911 912 spin_lock(&gl->gl_lockref.lock); 913 prev_object = gl->gl_object; 914 gl->gl_object = object; 915 spin_unlock(&gl->gl_lockref.lock); 916 if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == NULL)) { 917 pr_warn("glock=%u/%llx\n", 918 gl->gl_name.ln_type, 919 (unsigned long long)gl->gl_name.ln_number); 920 gfs2_dump_glock(NULL, gl, true); 921 } 922 } 923 924 /** 925 * glock_clear_object - clear the gl_object field of a glock 926 * @gl: the glock 927 * @object: object the glock currently points at 928 */ 929 void glock_clear_object(struct gfs2_glock *gl, void *object) 930 { 931 void *prev_object; 932 933 spin_lock(&gl->gl_lockref.lock); 934 prev_object = gl->gl_object; 935 gl->gl_object = NULL; 936 spin_unlock(&gl->gl_lockref.lock); 937 if (gfs2_assert_warn(gl->gl_name.ln_sbd, prev_object == object)) { 938 pr_warn("glock=%u/%llx\n", 939 gl->gl_name.ln_type, 940 (unsigned long long)gl->gl_name.ln_number); 941 gfs2_dump_glock(NULL, gl, true); 942 } 943 } 944 945 void gfs2_inode_remember_delete(struct gfs2_glock *gl, u64 generation) 946 { 947 struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr; 948 949 if (ri->ri_magic == 0) 950 ri->ri_magic = cpu_to_be32(GFS2_MAGIC); 951 if (ri->ri_magic == cpu_to_be32(GFS2_MAGIC)) 952 ri->ri_generation_deleted = cpu_to_be64(generation); 953 } 954 955 bool gfs2_inode_already_deleted(struct gfs2_glock *gl, u64 generation) 956 { 957 struct gfs2_inode_lvb *ri = (void *)gl->gl_lksb.sb_lvbptr; 958 959 if (ri->ri_magic != cpu_to_be32(GFS2_MAGIC)) 960 return false; 961 return generation <= be64_to_cpu(ri->ri_generation_deleted); 962 } 963 964 static void gfs2_glock_poke(struct gfs2_glock *gl) 965 { 966 int flags = LM_FLAG_TRY_1CB | LM_FLAG_ANY | GL_SKIP; 967 struct gfs2_holder gh; 968 int error; 969 970 __gfs2_holder_init(gl, LM_ST_SHARED, flags, &gh, _RET_IP_); 971 error = gfs2_glock_nq(&gh); 972 if (!error) 973 gfs2_glock_dq(&gh); 974 gfs2_holder_uninit(&gh); 975 } 976 977 static bool gfs2_try_evict(struct gfs2_glock *gl) 978 { 979 struct gfs2_inode *ip; 980 bool evicted = false; 981 982 /* 983 * If there is contention on the iopen glock and we have an inode, try 984 * to grab and release the inode so that it can be evicted. This will 985 * allow the remote node to go ahead and delete the inode without us 986 * having to do it, which will avoid rgrp glock thrashing. 987 * 988 * The remote node is likely still holding the corresponding inode 989 * glock, so it will run before we get to verify that the delete has 990 * happened below. 991 */ 992 spin_lock(&gl->gl_lockref.lock); 993 ip = gl->gl_object; 994 if (ip && !igrab(&ip->i_inode)) 995 ip = NULL; 996 spin_unlock(&gl->gl_lockref.lock); 997 if (ip) { 998 gl->gl_no_formal_ino = ip->i_no_formal_ino; 999 set_bit(GIF_DEFERRED_DELETE, &ip->i_flags); 1000 d_prune_aliases(&ip->i_inode); 1001 iput(&ip->i_inode); 1002 1003 /* If the inode was evicted, gl->gl_object will now be NULL. */ 1004 spin_lock(&gl->gl_lockref.lock); 1005 ip = gl->gl_object; 1006 if (ip) { 1007 clear_bit(GIF_DEFERRED_DELETE, &ip->i_flags); 1008 if (!igrab(&ip->i_inode)) 1009 ip = NULL; 1010 } 1011 spin_unlock(&gl->gl_lockref.lock); 1012 if (ip) { 1013 gfs2_glock_poke(ip->i_gl); 1014 iput(&ip->i_inode); 1015 } 1016 evicted = !ip; 1017 } 1018 return evicted; 1019 } 1020 1021 bool gfs2_queue_try_to_evict(struct gfs2_glock *gl) 1022 { 1023 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 1024 1025 if (test_and_set_bit(GLF_TRY_TO_EVICT, &gl->gl_flags)) 1026 return false; 1027 return queue_delayed_work(sdp->sd_delete_wq, 1028 &gl->gl_delete, 0); 1029 } 1030 1031 bool gfs2_queue_verify_delete(struct gfs2_glock *gl, bool later) 1032 { 1033 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 1034 unsigned long delay; 1035 1036 if (test_and_set_bit(GLF_VERIFY_DELETE, &gl->gl_flags)) 1037 return false; 1038 delay = later ? 5 * HZ : 0; 1039 return queue_delayed_work(sdp->sd_delete_wq, &gl->gl_delete, delay); 1040 } 1041 1042 static void delete_work_func(struct work_struct *work) 1043 { 1044 struct delayed_work *dwork = to_delayed_work(work); 1045 struct gfs2_glock *gl = container_of(dwork, struct gfs2_glock, gl_delete); 1046 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 1047 struct inode *inode; 1048 u64 no_addr = gl->gl_name.ln_number; 1049 1050 if (test_and_clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags)) { 1051 /* 1052 * If we can evict the inode, give the remote node trying to 1053 * delete the inode some time before verifying that the delete 1054 * has happened. Otherwise, if we cause contention on the inode glock 1055 * immediately, the remote node will think that we still have 1056 * the inode in use, and so it will give up waiting. 1057 * 1058 * If we can't evict the inode, signal to the remote node that 1059 * the inode is still in use. We'll later try to delete the 1060 * inode locally in gfs2_evict_inode. 1061 * 1062 * FIXME: We only need to verify that the remote node has 1063 * deleted the inode because nodes before this remote delete 1064 * rework won't cooperate. At a later time, when we no longer 1065 * care about compatibility with such nodes, we can skip this 1066 * step entirely. 1067 */ 1068 if (gfs2_try_evict(gl)) { 1069 if (test_bit(SDF_KILL, &sdp->sd_flags)) 1070 goto out; 1071 if (gfs2_queue_verify_delete(gl, true)) 1072 return; 1073 } 1074 goto out; 1075 } 1076 1077 if (test_and_clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags)) { 1078 inode = gfs2_lookup_by_inum(sdp, no_addr, gl->gl_no_formal_ino, 1079 GFS2_BLKST_UNLINKED); 1080 if (IS_ERR(inode)) { 1081 if (PTR_ERR(inode) == -EAGAIN && 1082 !test_bit(SDF_KILL, &sdp->sd_flags) && 1083 gfs2_queue_verify_delete(gl, true)) 1084 return; 1085 } else { 1086 d_prune_aliases(inode); 1087 iput(inode); 1088 } 1089 } 1090 1091 out: 1092 gfs2_glock_put(gl); 1093 } 1094 1095 static void glock_work_func(struct work_struct *work) 1096 { 1097 unsigned long delay = 0; 1098 struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work); 1099 unsigned int drop_refs = 1; 1100 1101 spin_lock(&gl->gl_lockref.lock); 1102 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags)) { 1103 clear_bit(GLF_REPLY_PENDING, &gl->gl_flags); 1104 finish_xmote(gl, gl->gl_reply); 1105 drop_refs++; 1106 } 1107 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) && 1108 gl->gl_state != LM_ST_UNLOCKED && 1109 gl->gl_demote_state != LM_ST_EXCLUSIVE) { 1110 unsigned long holdtime, now = jiffies; 1111 1112 holdtime = gl->gl_tchange + gl->gl_hold_time; 1113 if (time_before(now, holdtime)) 1114 delay = holdtime - now; 1115 1116 if (!delay) { 1117 clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags); 1118 gfs2_set_demote(gl); 1119 } 1120 } 1121 run_queue(gl, 0); 1122 if (delay) { 1123 /* Keep one glock reference for the work we requeue. */ 1124 drop_refs--; 1125 if (gl->gl_name.ln_type != LM_TYPE_INODE) 1126 delay = 0; 1127 __gfs2_glock_queue_work(gl, delay); 1128 } 1129 1130 /* 1131 * Drop the remaining glock references manually here. (Mind that 1132 * __gfs2_glock_queue_work depends on the lockref spinlock begin held 1133 * here as well.) 1134 */ 1135 gl->gl_lockref.count -= drop_refs; 1136 if (!gl->gl_lockref.count) { 1137 __gfs2_glock_put(gl); 1138 return; 1139 } 1140 spin_unlock(&gl->gl_lockref.lock); 1141 } 1142 1143 static struct gfs2_glock *find_insert_glock(struct lm_lockname *name, 1144 struct gfs2_glock *new) 1145 { 1146 struct wait_glock_queue wait; 1147 wait_queue_head_t *wq = glock_waitqueue(name); 1148 struct gfs2_glock *gl; 1149 1150 wait.name = name; 1151 init_wait(&wait.wait); 1152 wait.wait.func = glock_wake_function; 1153 1154 again: 1155 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); 1156 rcu_read_lock(); 1157 if (new) { 1158 gl = rhashtable_lookup_get_insert_fast(&gl_hash_table, 1159 &new->gl_node, ht_parms); 1160 if (IS_ERR(gl)) 1161 goto out; 1162 } else { 1163 gl = rhashtable_lookup_fast(&gl_hash_table, 1164 name, ht_parms); 1165 } 1166 if (gl && !lockref_get_not_dead(&gl->gl_lockref)) { 1167 rcu_read_unlock(); 1168 schedule(); 1169 goto again; 1170 } 1171 out: 1172 rcu_read_unlock(); 1173 finish_wait(wq, &wait.wait); 1174 return gl; 1175 } 1176 1177 /** 1178 * gfs2_glock_get() - Get a glock, or create one if one doesn't exist 1179 * @sdp: The GFS2 superblock 1180 * @number: the lock number 1181 * @glops: The glock_operations to use 1182 * @create: If 0, don't create the glock if it doesn't exist 1183 * @glp: the glock is returned here 1184 * 1185 * This does not lock a glock, just finds/creates structures for one. 1186 * 1187 * Returns: errno 1188 */ 1189 1190 int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number, 1191 const struct gfs2_glock_operations *glops, int create, 1192 struct gfs2_glock **glp) 1193 { 1194 struct super_block *s = sdp->sd_vfs; 1195 struct lm_lockname name = { .ln_number = number, 1196 .ln_type = glops->go_type, 1197 .ln_sbd = sdp }; 1198 struct gfs2_glock *gl, *tmp; 1199 struct address_space *mapping; 1200 int ret = 0; 1201 1202 gl = find_insert_glock(&name, NULL); 1203 if (gl) { 1204 *glp = gl; 1205 return 0; 1206 } 1207 if (!create) 1208 return -ENOENT; 1209 1210 if (glops->go_flags & GLOF_ASPACE) { 1211 struct gfs2_glock_aspace *gla = 1212 kmem_cache_alloc(gfs2_glock_aspace_cachep, GFP_NOFS); 1213 if (!gla) 1214 return -ENOMEM; 1215 gl = &gla->glock; 1216 } else { 1217 gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_NOFS); 1218 if (!gl) 1219 return -ENOMEM; 1220 } 1221 memset(&gl->gl_lksb, 0, sizeof(struct dlm_lksb)); 1222 gl->gl_ops = glops; 1223 1224 if (glops->go_flags & GLOF_LVB) { 1225 gl->gl_lksb.sb_lvbptr = kzalloc(GDLM_LVB_SIZE, GFP_NOFS); 1226 if (!gl->gl_lksb.sb_lvbptr) { 1227 gfs2_glock_dealloc(&gl->gl_rcu); 1228 return -ENOMEM; 1229 } 1230 } 1231 1232 atomic_inc(&sdp->sd_glock_disposal); 1233 gl->gl_node.next = NULL; 1234 gl->gl_flags = glops->go_instantiate ? BIT(GLF_INSTANTIATE_NEEDED) : 0; 1235 gl->gl_name = name; 1236 lockdep_set_subclass(&gl->gl_lockref.lock, glops->go_subclass); 1237 gl->gl_lockref.count = 1; 1238 gl->gl_state = LM_ST_UNLOCKED; 1239 gl->gl_target = LM_ST_UNLOCKED; 1240 gl->gl_demote_state = LM_ST_EXCLUSIVE; 1241 gl->gl_dstamp = 0; 1242 preempt_disable(); 1243 /* We use the global stats to estimate the initial per-glock stats */ 1244 gl->gl_stats = this_cpu_ptr(sdp->sd_lkstats)->lkstats[glops->go_type]; 1245 preempt_enable(); 1246 gl->gl_stats.stats[GFS2_LKS_DCOUNT] = 0; 1247 gl->gl_stats.stats[GFS2_LKS_QCOUNT] = 0; 1248 gl->gl_tchange = jiffies; 1249 gl->gl_object = NULL; 1250 gl->gl_hold_time = GL_GLOCK_DFT_HOLD; 1251 INIT_DELAYED_WORK(&gl->gl_work, glock_work_func); 1252 if (gl->gl_name.ln_type == LM_TYPE_IOPEN) 1253 INIT_DELAYED_WORK(&gl->gl_delete, delete_work_func); 1254 1255 mapping = gfs2_glock2aspace(gl); 1256 if (mapping) { 1257 mapping->a_ops = &gfs2_meta_aops; 1258 mapping->host = s->s_bdev->bd_inode; 1259 mapping->flags = 0; 1260 mapping_set_gfp_mask(mapping, GFP_NOFS); 1261 mapping->private_data = NULL; 1262 mapping->writeback_index = 0; 1263 } 1264 1265 tmp = find_insert_glock(&name, gl); 1266 if (!tmp) { 1267 *glp = gl; 1268 goto out; 1269 } 1270 if (IS_ERR(tmp)) { 1271 ret = PTR_ERR(tmp); 1272 goto out_free; 1273 } 1274 *glp = tmp; 1275 1276 out_free: 1277 gfs2_glock_dealloc(&gl->gl_rcu); 1278 if (atomic_dec_and_test(&sdp->sd_glock_disposal)) 1279 wake_up(&sdp->sd_kill_wait); 1280 1281 out: 1282 return ret; 1283 } 1284 1285 /** 1286 * __gfs2_holder_init - initialize a struct gfs2_holder in the default way 1287 * @gl: the glock 1288 * @state: the state we're requesting 1289 * @flags: the modifier flags 1290 * @gh: the holder structure 1291 * 1292 */ 1293 1294 void __gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, u16 flags, 1295 struct gfs2_holder *gh, unsigned long ip) 1296 { 1297 INIT_LIST_HEAD(&gh->gh_list); 1298 gh->gh_gl = gfs2_glock_hold(gl); 1299 gh->gh_ip = ip; 1300 gh->gh_owner_pid = get_pid(task_pid(current)); 1301 gh->gh_state = state; 1302 gh->gh_flags = flags; 1303 gh->gh_iflags = 0; 1304 } 1305 1306 /** 1307 * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it 1308 * @state: the state we're requesting 1309 * @flags: the modifier flags 1310 * @gh: the holder structure 1311 * 1312 * Don't mess with the glock. 1313 * 1314 */ 1315 1316 void gfs2_holder_reinit(unsigned int state, u16 flags, struct gfs2_holder *gh) 1317 { 1318 gh->gh_state = state; 1319 gh->gh_flags = flags; 1320 gh->gh_iflags = 0; 1321 gh->gh_ip = _RET_IP_; 1322 put_pid(gh->gh_owner_pid); 1323 gh->gh_owner_pid = get_pid(task_pid(current)); 1324 } 1325 1326 /** 1327 * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference) 1328 * @gh: the holder structure 1329 * 1330 */ 1331 1332 void gfs2_holder_uninit(struct gfs2_holder *gh) 1333 { 1334 put_pid(gh->gh_owner_pid); 1335 gfs2_glock_put(gh->gh_gl); 1336 gfs2_holder_mark_uninitialized(gh); 1337 gh->gh_ip = 0; 1338 } 1339 1340 static void gfs2_glock_update_hold_time(struct gfs2_glock *gl, 1341 unsigned long start_time) 1342 { 1343 /* Have we waited longer that a second? */ 1344 if (time_after(jiffies, start_time + HZ)) { 1345 /* Lengthen the minimum hold time. */ 1346 gl->gl_hold_time = min(gl->gl_hold_time + GL_GLOCK_HOLD_INCR, 1347 GL_GLOCK_MAX_HOLD); 1348 } 1349 } 1350 1351 /** 1352 * gfs2_glock_holder_ready - holder is ready and its error code can be collected 1353 * @gh: the glock holder 1354 * 1355 * Called when a glock holder no longer needs to be waited for because it is 1356 * now either held (HIF_HOLDER set; gh_error == 0), or acquiring the lock has 1357 * failed (gh_error != 0). 1358 */ 1359 1360 int gfs2_glock_holder_ready(struct gfs2_holder *gh) 1361 { 1362 if (gh->gh_error || (gh->gh_flags & GL_SKIP)) 1363 return gh->gh_error; 1364 gh->gh_error = gfs2_instantiate(gh); 1365 if (gh->gh_error) 1366 gfs2_glock_dq(gh); 1367 return gh->gh_error; 1368 } 1369 1370 /** 1371 * gfs2_glock_wait - wait on a glock acquisition 1372 * @gh: the glock holder 1373 * 1374 * Returns: 0 on success 1375 */ 1376 1377 int gfs2_glock_wait(struct gfs2_holder *gh) 1378 { 1379 unsigned long start_time = jiffies; 1380 1381 might_sleep(); 1382 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE); 1383 gfs2_glock_update_hold_time(gh->gh_gl, start_time); 1384 return gfs2_glock_holder_ready(gh); 1385 } 1386 1387 static int glocks_pending(unsigned int num_gh, struct gfs2_holder *ghs) 1388 { 1389 int i; 1390 1391 for (i = 0; i < num_gh; i++) 1392 if (test_bit(HIF_WAIT, &ghs[i].gh_iflags)) 1393 return 1; 1394 return 0; 1395 } 1396 1397 /** 1398 * gfs2_glock_async_wait - wait on multiple asynchronous glock acquisitions 1399 * @num_gh: the number of holders in the array 1400 * @ghs: the glock holder array 1401 * 1402 * Returns: 0 on success, meaning all glocks have been granted and are held. 1403 * -ESTALE if the request timed out, meaning all glocks were released, 1404 * and the caller should retry the operation. 1405 */ 1406 1407 int gfs2_glock_async_wait(unsigned int num_gh, struct gfs2_holder *ghs) 1408 { 1409 struct gfs2_sbd *sdp = ghs[0].gh_gl->gl_name.ln_sbd; 1410 int i, ret = 0, timeout = 0; 1411 unsigned long start_time = jiffies; 1412 1413 might_sleep(); 1414 /* 1415 * Total up the (minimum hold time * 2) of all glocks and use that to 1416 * determine the max amount of time we should wait. 1417 */ 1418 for (i = 0; i < num_gh; i++) 1419 timeout += ghs[i].gh_gl->gl_hold_time << 1; 1420 1421 if (!wait_event_timeout(sdp->sd_async_glock_wait, 1422 !glocks_pending(num_gh, ghs), timeout)) { 1423 ret = -ESTALE; /* request timed out. */ 1424 goto out; 1425 } 1426 1427 for (i = 0; i < num_gh; i++) { 1428 struct gfs2_holder *gh = &ghs[i]; 1429 int ret2; 1430 1431 if (test_bit(HIF_HOLDER, &gh->gh_iflags)) { 1432 gfs2_glock_update_hold_time(gh->gh_gl, 1433 start_time); 1434 } 1435 ret2 = gfs2_glock_holder_ready(gh); 1436 if (!ret) 1437 ret = ret2; 1438 } 1439 1440 out: 1441 if (ret) { 1442 for (i = 0; i < num_gh; i++) { 1443 struct gfs2_holder *gh = &ghs[i]; 1444 1445 gfs2_glock_dq(gh); 1446 } 1447 } 1448 return ret; 1449 } 1450 1451 /** 1452 * handle_callback - process a demote request 1453 * @gl: the glock 1454 * @state: the state the caller wants us to change to 1455 * @delay: zero to demote immediately; otherwise pending demote 1456 * @remote: true if this came from a different cluster node 1457 * 1458 * There are only two requests that we are going to see in actual 1459 * practise: LM_ST_SHARED and LM_ST_UNLOCKED 1460 */ 1461 1462 static void handle_callback(struct gfs2_glock *gl, unsigned int state, 1463 unsigned long delay, bool remote) 1464 { 1465 if (delay) 1466 set_bit(GLF_PENDING_DEMOTE, &gl->gl_flags); 1467 else 1468 gfs2_set_demote(gl); 1469 if (gl->gl_demote_state == LM_ST_EXCLUSIVE) { 1470 gl->gl_demote_state = state; 1471 gl->gl_demote_time = jiffies; 1472 } else if (gl->gl_demote_state != LM_ST_UNLOCKED && 1473 gl->gl_demote_state != state) { 1474 gl->gl_demote_state = LM_ST_UNLOCKED; 1475 } 1476 if (gl->gl_ops->go_callback) 1477 gl->gl_ops->go_callback(gl, remote); 1478 trace_gfs2_demote_rq(gl, remote); 1479 } 1480 1481 void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...) 1482 { 1483 struct va_format vaf; 1484 va_list args; 1485 1486 va_start(args, fmt); 1487 1488 if (seq) { 1489 seq_vprintf(seq, fmt, args); 1490 } else { 1491 vaf.fmt = fmt; 1492 vaf.va = &args; 1493 1494 pr_err("%pV", &vaf); 1495 } 1496 1497 va_end(args); 1498 } 1499 1500 static inline bool pid_is_meaningful(const struct gfs2_holder *gh) 1501 { 1502 if (!(gh->gh_flags & GL_NOPID)) 1503 return true; 1504 if (gh->gh_state == LM_ST_UNLOCKED) 1505 return true; 1506 return false; 1507 } 1508 1509 /** 1510 * add_to_queue - Add a holder to the wait queue (but look for recursion) 1511 * @gh: the holder structure to add 1512 * 1513 * Eventually we should move the recursive locking trap to a 1514 * debugging option or something like that. This is the fast 1515 * path and needs to have the minimum number of distractions. 1516 * 1517 */ 1518 1519 static inline void add_to_queue(struct gfs2_holder *gh) 1520 __releases(&gl->gl_lockref.lock) 1521 __acquires(&gl->gl_lockref.lock) 1522 { 1523 struct gfs2_glock *gl = gh->gh_gl; 1524 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 1525 struct list_head *insert_pt = NULL; 1526 struct gfs2_holder *gh2; 1527 int try_futile = 0; 1528 1529 GLOCK_BUG_ON(gl, gh->gh_owner_pid == NULL); 1530 if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags)) 1531 GLOCK_BUG_ON(gl, true); 1532 1533 if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) { 1534 if (test_bit(GLF_LOCK, &gl->gl_flags)) { 1535 struct gfs2_holder *current_gh; 1536 1537 current_gh = find_first_holder(gl); 1538 try_futile = !may_grant(gl, current_gh, gh); 1539 } 1540 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) 1541 goto fail; 1542 } 1543 1544 list_for_each_entry(gh2, &gl->gl_holders, gh_list) { 1545 if (likely(gh2->gh_owner_pid != gh->gh_owner_pid)) 1546 continue; 1547 if (gh->gh_gl->gl_ops->go_type == LM_TYPE_FLOCK) 1548 continue; 1549 if (!pid_is_meaningful(gh2)) 1550 continue; 1551 goto trap_recursive; 1552 } 1553 list_for_each_entry(gh2, &gl->gl_holders, gh_list) { 1554 if (try_futile && 1555 !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) { 1556 fail: 1557 gh->gh_error = GLR_TRYFAILED; 1558 gfs2_holder_wake(gh); 1559 return; 1560 } 1561 if (test_bit(HIF_HOLDER, &gh2->gh_iflags)) 1562 continue; 1563 } 1564 trace_gfs2_glock_queue(gh, 1); 1565 gfs2_glstats_inc(gl, GFS2_LKS_QCOUNT); 1566 gfs2_sbstats_inc(gl, GFS2_LKS_QCOUNT); 1567 if (likely(insert_pt == NULL)) { 1568 list_add_tail(&gh->gh_list, &gl->gl_holders); 1569 return; 1570 } 1571 list_add_tail(&gh->gh_list, insert_pt); 1572 gh = list_first_entry(&gl->gl_holders, struct gfs2_holder, gh_list); 1573 spin_unlock(&gl->gl_lockref.lock); 1574 if (sdp->sd_lockstruct.ls_ops->lm_cancel) 1575 sdp->sd_lockstruct.ls_ops->lm_cancel(gl); 1576 spin_lock(&gl->gl_lockref.lock); 1577 return; 1578 1579 trap_recursive: 1580 fs_err(sdp, "original: %pSR\n", (void *)gh2->gh_ip); 1581 fs_err(sdp, "pid: %d\n", pid_nr(gh2->gh_owner_pid)); 1582 fs_err(sdp, "lock type: %d req lock state : %d\n", 1583 gh2->gh_gl->gl_name.ln_type, gh2->gh_state); 1584 fs_err(sdp, "new: %pSR\n", (void *)gh->gh_ip); 1585 fs_err(sdp, "pid: %d\n", pid_nr(gh->gh_owner_pid)); 1586 fs_err(sdp, "lock type: %d req lock state : %d\n", 1587 gh->gh_gl->gl_name.ln_type, gh->gh_state); 1588 gfs2_dump_glock(NULL, gl, true); 1589 BUG(); 1590 } 1591 1592 /** 1593 * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock) 1594 * @gh: the holder structure 1595 * 1596 * if (gh->gh_flags & GL_ASYNC), this never returns an error 1597 * 1598 * Returns: 0, GLR_TRYFAILED, or errno on failure 1599 */ 1600 1601 int gfs2_glock_nq(struct gfs2_holder *gh) 1602 { 1603 struct gfs2_glock *gl = gh->gh_gl; 1604 int error = 0; 1605 1606 if (glock_blocked_by_withdraw(gl) && !(gh->gh_flags & LM_FLAG_NOEXP)) 1607 return -EIO; 1608 1609 if (test_bit(GLF_LRU, &gl->gl_flags)) 1610 gfs2_glock_remove_from_lru(gl); 1611 1612 gh->gh_error = 0; 1613 spin_lock(&gl->gl_lockref.lock); 1614 add_to_queue(gh); 1615 if (unlikely((LM_FLAG_NOEXP & gh->gh_flags) && 1616 test_and_clear_bit(GLF_FROZEN, &gl->gl_flags))) { 1617 set_bit(GLF_REPLY_PENDING, &gl->gl_flags); 1618 gl->gl_lockref.count++; 1619 __gfs2_glock_queue_work(gl, 0); 1620 } 1621 run_queue(gl, 1); 1622 spin_unlock(&gl->gl_lockref.lock); 1623 1624 if (!(gh->gh_flags & GL_ASYNC)) 1625 error = gfs2_glock_wait(gh); 1626 1627 return error; 1628 } 1629 1630 /** 1631 * gfs2_glock_poll - poll to see if an async request has been completed 1632 * @gh: the holder 1633 * 1634 * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on 1635 */ 1636 1637 int gfs2_glock_poll(struct gfs2_holder *gh) 1638 { 1639 return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1; 1640 } 1641 1642 static inline bool needs_demote(struct gfs2_glock *gl) 1643 { 1644 return (test_bit(GLF_DEMOTE, &gl->gl_flags) || 1645 test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags)); 1646 } 1647 1648 static void __gfs2_glock_dq(struct gfs2_holder *gh) 1649 { 1650 struct gfs2_glock *gl = gh->gh_gl; 1651 unsigned delay = 0; 1652 int fast_path = 0; 1653 1654 /* 1655 * This holder should not be cached, so mark it for demote. 1656 * Note: this should be done before the check for needs_demote 1657 * below. 1658 */ 1659 if (gh->gh_flags & GL_NOCACHE) 1660 handle_callback(gl, LM_ST_UNLOCKED, 0, false); 1661 1662 list_del_init(&gh->gh_list); 1663 clear_bit(HIF_HOLDER, &gh->gh_iflags); 1664 trace_gfs2_glock_queue(gh, 0); 1665 1666 /* 1667 * If there hasn't been a demote request we are done. 1668 * (Let the remaining holders, if any, keep holding it.) 1669 */ 1670 if (!needs_demote(gl)) { 1671 if (list_empty(&gl->gl_holders)) 1672 fast_path = 1; 1673 } 1674 1675 if (!test_bit(GLF_LFLUSH, &gl->gl_flags) && demote_ok(gl)) 1676 gfs2_glock_add_to_lru(gl); 1677 1678 if (unlikely(!fast_path)) { 1679 gl->gl_lockref.count++; 1680 if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) && 1681 !test_bit(GLF_DEMOTE, &gl->gl_flags) && 1682 gl->gl_name.ln_type == LM_TYPE_INODE) 1683 delay = gl->gl_hold_time; 1684 __gfs2_glock_queue_work(gl, delay); 1685 } 1686 } 1687 1688 /** 1689 * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock) 1690 * @gh: the glock holder 1691 * 1692 */ 1693 void gfs2_glock_dq(struct gfs2_holder *gh) 1694 { 1695 struct gfs2_glock *gl = gh->gh_gl; 1696 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 1697 1698 spin_lock(&gl->gl_lockref.lock); 1699 if (!gfs2_holder_queued(gh)) { 1700 /* 1701 * May have already been dequeued because the locking request 1702 * was GL_ASYNC and it has failed in the meantime. 1703 */ 1704 goto out; 1705 } 1706 1707 if (list_is_first(&gh->gh_list, &gl->gl_holders) && 1708 !test_bit(HIF_HOLDER, &gh->gh_iflags)) { 1709 spin_unlock(&gl->gl_lockref.lock); 1710 gl->gl_name.ln_sbd->sd_lockstruct.ls_ops->lm_cancel(gl); 1711 wait_on_bit(&gh->gh_iflags, HIF_WAIT, TASK_UNINTERRUPTIBLE); 1712 spin_lock(&gl->gl_lockref.lock); 1713 } 1714 1715 /* 1716 * If we're in the process of file system withdraw, we cannot just 1717 * dequeue any glocks until our journal is recovered, lest we introduce 1718 * file system corruption. We need two exceptions to this rule: We need 1719 * to allow unlocking of nondisk glocks and the glock for our own 1720 * journal that needs recovery. 1721 */ 1722 if (test_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags) && 1723 glock_blocked_by_withdraw(gl) && 1724 gh->gh_gl != sdp->sd_jinode_gl) { 1725 sdp->sd_glock_dqs_held++; 1726 spin_unlock(&gl->gl_lockref.lock); 1727 might_sleep(); 1728 wait_on_bit(&sdp->sd_flags, SDF_WITHDRAW_RECOVERY, 1729 TASK_UNINTERRUPTIBLE); 1730 spin_lock(&gl->gl_lockref.lock); 1731 } 1732 1733 __gfs2_glock_dq(gh); 1734 out: 1735 spin_unlock(&gl->gl_lockref.lock); 1736 } 1737 1738 void gfs2_glock_dq_wait(struct gfs2_holder *gh) 1739 { 1740 struct gfs2_glock *gl = gh->gh_gl; 1741 gfs2_glock_dq(gh); 1742 might_sleep(); 1743 wait_on_bit(&gl->gl_flags, GLF_DEMOTE, TASK_UNINTERRUPTIBLE); 1744 } 1745 1746 /** 1747 * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it 1748 * @gh: the holder structure 1749 * 1750 */ 1751 1752 void gfs2_glock_dq_uninit(struct gfs2_holder *gh) 1753 { 1754 gfs2_glock_dq(gh); 1755 gfs2_holder_uninit(gh); 1756 } 1757 1758 /** 1759 * gfs2_glock_nq_num - acquire a glock based on lock number 1760 * @sdp: the filesystem 1761 * @number: the lock number 1762 * @glops: the glock operations for the type of glock 1763 * @state: the state to acquire the glock in 1764 * @flags: modifier flags for the acquisition 1765 * @gh: the struct gfs2_holder 1766 * 1767 * Returns: errno 1768 */ 1769 1770 int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number, 1771 const struct gfs2_glock_operations *glops, 1772 unsigned int state, u16 flags, struct gfs2_holder *gh) 1773 { 1774 struct gfs2_glock *gl; 1775 int error; 1776 1777 error = gfs2_glock_get(sdp, number, glops, CREATE, &gl); 1778 if (!error) { 1779 error = gfs2_glock_nq_init(gl, state, flags, gh); 1780 gfs2_glock_put(gl); 1781 } 1782 1783 return error; 1784 } 1785 1786 /** 1787 * glock_compare - Compare two struct gfs2_glock structures for sorting 1788 * @arg_a: the first structure 1789 * @arg_b: the second structure 1790 * 1791 */ 1792 1793 static int glock_compare(const void *arg_a, const void *arg_b) 1794 { 1795 const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a; 1796 const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b; 1797 const struct lm_lockname *a = &gh_a->gh_gl->gl_name; 1798 const struct lm_lockname *b = &gh_b->gh_gl->gl_name; 1799 1800 if (a->ln_number > b->ln_number) 1801 return 1; 1802 if (a->ln_number < b->ln_number) 1803 return -1; 1804 BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type); 1805 return 0; 1806 } 1807 1808 /** 1809 * nq_m_sync - synchronously acquire more than one glock in deadlock free order 1810 * @num_gh: the number of structures 1811 * @ghs: an array of struct gfs2_holder structures 1812 * @p: placeholder for the holder structure to pass back 1813 * 1814 * Returns: 0 on success (all glocks acquired), 1815 * errno on failure (no glocks acquired) 1816 */ 1817 1818 static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs, 1819 struct gfs2_holder **p) 1820 { 1821 unsigned int x; 1822 int error = 0; 1823 1824 for (x = 0; x < num_gh; x++) 1825 p[x] = &ghs[x]; 1826 1827 sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL); 1828 1829 for (x = 0; x < num_gh; x++) { 1830 error = gfs2_glock_nq(p[x]); 1831 if (error) { 1832 while (x--) 1833 gfs2_glock_dq(p[x]); 1834 break; 1835 } 1836 } 1837 1838 return error; 1839 } 1840 1841 /** 1842 * gfs2_glock_nq_m - acquire multiple glocks 1843 * @num_gh: the number of structures 1844 * @ghs: an array of struct gfs2_holder structures 1845 * 1846 * Returns: 0 on success (all glocks acquired), 1847 * errno on failure (no glocks acquired) 1848 */ 1849 1850 int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs) 1851 { 1852 struct gfs2_holder *tmp[4]; 1853 struct gfs2_holder **pph = tmp; 1854 int error = 0; 1855 1856 switch(num_gh) { 1857 case 0: 1858 return 0; 1859 case 1: 1860 return gfs2_glock_nq(ghs); 1861 default: 1862 if (num_gh <= 4) 1863 break; 1864 pph = kmalloc_array(num_gh, sizeof(struct gfs2_holder *), 1865 GFP_NOFS); 1866 if (!pph) 1867 return -ENOMEM; 1868 } 1869 1870 error = nq_m_sync(num_gh, ghs, pph); 1871 1872 if (pph != tmp) 1873 kfree(pph); 1874 1875 return error; 1876 } 1877 1878 /** 1879 * gfs2_glock_dq_m - release multiple glocks 1880 * @num_gh: the number of structures 1881 * @ghs: an array of struct gfs2_holder structures 1882 * 1883 */ 1884 1885 void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs) 1886 { 1887 while (num_gh--) 1888 gfs2_glock_dq(&ghs[num_gh]); 1889 } 1890 1891 void gfs2_glock_cb(struct gfs2_glock *gl, unsigned int state) 1892 { 1893 unsigned long delay = 0; 1894 unsigned long holdtime; 1895 unsigned long now = jiffies; 1896 1897 gfs2_glock_hold(gl); 1898 spin_lock(&gl->gl_lockref.lock); 1899 holdtime = gl->gl_tchange + gl->gl_hold_time; 1900 if (!list_empty(&gl->gl_holders) && 1901 gl->gl_name.ln_type == LM_TYPE_INODE) { 1902 if (time_before(now, holdtime)) 1903 delay = holdtime - now; 1904 if (test_bit(GLF_REPLY_PENDING, &gl->gl_flags)) 1905 delay = gl->gl_hold_time; 1906 } 1907 handle_callback(gl, state, delay, true); 1908 __gfs2_glock_queue_work(gl, delay); 1909 spin_unlock(&gl->gl_lockref.lock); 1910 } 1911 1912 /** 1913 * gfs2_should_freeze - Figure out if glock should be frozen 1914 * @gl: The glock in question 1915 * 1916 * Glocks are not frozen if (a) the result of the dlm operation is 1917 * an error, (b) the locking operation was an unlock operation or 1918 * (c) if there is a "noexp" flagged request anywhere in the queue 1919 * 1920 * Returns: 1 if freezing should occur, 0 otherwise 1921 */ 1922 1923 static int gfs2_should_freeze(const struct gfs2_glock *gl) 1924 { 1925 const struct gfs2_holder *gh; 1926 1927 if (gl->gl_reply & ~LM_OUT_ST_MASK) 1928 return 0; 1929 if (gl->gl_target == LM_ST_UNLOCKED) 1930 return 0; 1931 1932 list_for_each_entry(gh, &gl->gl_holders, gh_list) { 1933 if (test_bit(HIF_HOLDER, &gh->gh_iflags)) 1934 continue; 1935 if (LM_FLAG_NOEXP & gh->gh_flags) 1936 return 0; 1937 } 1938 1939 return 1; 1940 } 1941 1942 /** 1943 * gfs2_glock_complete - Callback used by locking 1944 * @gl: Pointer to the glock 1945 * @ret: The return value from the dlm 1946 * 1947 * The gl_reply field is under the gl_lockref.lock lock so that it is ok 1948 * to use a bitfield shared with other glock state fields. 1949 */ 1950 1951 void gfs2_glock_complete(struct gfs2_glock *gl, int ret) 1952 { 1953 struct lm_lockstruct *ls = &gl->gl_name.ln_sbd->sd_lockstruct; 1954 1955 spin_lock(&gl->gl_lockref.lock); 1956 gl->gl_reply = ret; 1957 1958 if (unlikely(test_bit(DFL_BLOCK_LOCKS, &ls->ls_recover_flags))) { 1959 if (gfs2_should_freeze(gl)) { 1960 set_bit(GLF_FROZEN, &gl->gl_flags); 1961 spin_unlock(&gl->gl_lockref.lock); 1962 return; 1963 } 1964 } 1965 1966 gl->gl_lockref.count++; 1967 set_bit(GLF_REPLY_PENDING, &gl->gl_flags); 1968 __gfs2_glock_queue_work(gl, 0); 1969 spin_unlock(&gl->gl_lockref.lock); 1970 } 1971 1972 static int glock_cmp(void *priv, const struct list_head *a, 1973 const struct list_head *b) 1974 { 1975 struct gfs2_glock *gla, *glb; 1976 1977 gla = list_entry(a, struct gfs2_glock, gl_lru); 1978 glb = list_entry(b, struct gfs2_glock, gl_lru); 1979 1980 if (gla->gl_name.ln_number > glb->gl_name.ln_number) 1981 return 1; 1982 if (gla->gl_name.ln_number < glb->gl_name.ln_number) 1983 return -1; 1984 1985 return 0; 1986 } 1987 1988 /** 1989 * gfs2_dispose_glock_lru - Demote a list of glocks 1990 * @list: The list to dispose of 1991 * 1992 * Disposing of glocks may involve disk accesses, so that here we sort 1993 * the glocks by number (i.e. disk location of the inodes) so that if 1994 * there are any such accesses, they'll be sent in order (mostly). 1995 * 1996 * Must be called under the lru_lock, but may drop and retake this 1997 * lock. While the lru_lock is dropped, entries may vanish from the 1998 * list, but no new entries will appear on the list (since it is 1999 * private) 2000 */ 2001 2002 static void gfs2_dispose_glock_lru(struct list_head *list) 2003 __releases(&lru_lock) 2004 __acquires(&lru_lock) 2005 { 2006 struct gfs2_glock *gl; 2007 2008 list_sort(NULL, list, glock_cmp); 2009 2010 while(!list_empty(list)) { 2011 gl = list_first_entry(list, struct gfs2_glock, gl_lru); 2012 list_del_init(&gl->gl_lru); 2013 clear_bit(GLF_LRU, &gl->gl_flags); 2014 if (!spin_trylock(&gl->gl_lockref.lock)) { 2015 add_back_to_lru: 2016 list_add(&gl->gl_lru, &lru_list); 2017 set_bit(GLF_LRU, &gl->gl_flags); 2018 atomic_inc(&lru_count); 2019 continue; 2020 } 2021 if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) { 2022 spin_unlock(&gl->gl_lockref.lock); 2023 goto add_back_to_lru; 2024 } 2025 gl->gl_lockref.count++; 2026 if (demote_ok(gl)) 2027 handle_callback(gl, LM_ST_UNLOCKED, 0, false); 2028 WARN_ON(!test_and_clear_bit(GLF_LOCK, &gl->gl_flags)); 2029 __gfs2_glock_queue_work(gl, 0); 2030 spin_unlock(&gl->gl_lockref.lock); 2031 cond_resched_lock(&lru_lock); 2032 } 2033 } 2034 2035 /** 2036 * gfs2_scan_glock_lru - Scan the LRU looking for locks to demote 2037 * @nr: The number of entries to scan 2038 * 2039 * This function selects the entries on the LRU which are able to 2040 * be demoted, and then kicks off the process by calling 2041 * gfs2_dispose_glock_lru() above. 2042 */ 2043 2044 static long gfs2_scan_glock_lru(int nr) 2045 { 2046 struct gfs2_glock *gl, *next; 2047 LIST_HEAD(dispose); 2048 long freed = 0; 2049 2050 spin_lock(&lru_lock); 2051 list_for_each_entry_safe(gl, next, &lru_list, gl_lru) { 2052 if (nr-- <= 0) 2053 break; 2054 /* Test for being demotable */ 2055 if (!test_bit(GLF_LOCK, &gl->gl_flags)) { 2056 if (!spin_trylock(&gl->gl_lockref.lock)) 2057 continue; 2058 if (gl->gl_lockref.count <= 1 && 2059 (gl->gl_state == LM_ST_UNLOCKED || 2060 demote_ok(gl))) { 2061 list_move(&gl->gl_lru, &dispose); 2062 atomic_dec(&lru_count); 2063 freed++; 2064 } 2065 spin_unlock(&gl->gl_lockref.lock); 2066 } 2067 } 2068 if (!list_empty(&dispose)) 2069 gfs2_dispose_glock_lru(&dispose); 2070 spin_unlock(&lru_lock); 2071 2072 return freed; 2073 } 2074 2075 static unsigned long gfs2_glock_shrink_scan(struct shrinker *shrink, 2076 struct shrink_control *sc) 2077 { 2078 if (!(sc->gfp_mask & __GFP_FS)) 2079 return SHRINK_STOP; 2080 return gfs2_scan_glock_lru(sc->nr_to_scan); 2081 } 2082 2083 static unsigned long gfs2_glock_shrink_count(struct shrinker *shrink, 2084 struct shrink_control *sc) 2085 { 2086 return vfs_pressure_ratio(atomic_read(&lru_count)); 2087 } 2088 2089 static struct shrinker glock_shrinker = { 2090 .seeks = DEFAULT_SEEKS, 2091 .count_objects = gfs2_glock_shrink_count, 2092 .scan_objects = gfs2_glock_shrink_scan, 2093 }; 2094 2095 /** 2096 * glock_hash_walk - Call a function for glock in a hash bucket 2097 * @examiner: the function 2098 * @sdp: the filesystem 2099 * 2100 * Note that the function can be called multiple times on the same 2101 * object. So the user must ensure that the function can cope with 2102 * that. 2103 */ 2104 2105 static void glock_hash_walk(glock_examiner examiner, const struct gfs2_sbd *sdp) 2106 { 2107 struct gfs2_glock *gl; 2108 struct rhashtable_iter iter; 2109 2110 rhashtable_walk_enter(&gl_hash_table, &iter); 2111 2112 do { 2113 rhashtable_walk_start(&iter); 2114 2115 while ((gl = rhashtable_walk_next(&iter)) && !IS_ERR(gl)) { 2116 if (gl->gl_name.ln_sbd == sdp) 2117 examiner(gl); 2118 } 2119 2120 rhashtable_walk_stop(&iter); 2121 } while (cond_resched(), gl == ERR_PTR(-EAGAIN)); 2122 2123 rhashtable_walk_exit(&iter); 2124 } 2125 2126 void gfs2_cancel_delete_work(struct gfs2_glock *gl) 2127 { 2128 clear_bit(GLF_TRY_TO_EVICT, &gl->gl_flags); 2129 clear_bit(GLF_VERIFY_DELETE, &gl->gl_flags); 2130 if (cancel_delayed_work(&gl->gl_delete)) 2131 gfs2_glock_put(gl); 2132 } 2133 2134 static void flush_delete_work(struct gfs2_glock *gl) 2135 { 2136 if (gl->gl_name.ln_type == LM_TYPE_IOPEN) { 2137 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 2138 2139 if (cancel_delayed_work(&gl->gl_delete)) { 2140 queue_delayed_work(sdp->sd_delete_wq, 2141 &gl->gl_delete, 0); 2142 } 2143 } 2144 } 2145 2146 void gfs2_flush_delete_work(struct gfs2_sbd *sdp) 2147 { 2148 glock_hash_walk(flush_delete_work, sdp); 2149 flush_workqueue(sdp->sd_delete_wq); 2150 } 2151 2152 /** 2153 * thaw_glock - thaw out a glock which has an unprocessed reply waiting 2154 * @gl: The glock to thaw 2155 * 2156 */ 2157 2158 static void thaw_glock(struct gfs2_glock *gl) 2159 { 2160 if (!test_and_clear_bit(GLF_FROZEN, &gl->gl_flags)) 2161 return; 2162 if (!lockref_get_not_dead(&gl->gl_lockref)) 2163 return; 2164 2165 spin_lock(&gl->gl_lockref.lock); 2166 set_bit(GLF_REPLY_PENDING, &gl->gl_flags); 2167 __gfs2_glock_queue_work(gl, 0); 2168 spin_unlock(&gl->gl_lockref.lock); 2169 } 2170 2171 /** 2172 * clear_glock - look at a glock and see if we can free it from glock cache 2173 * @gl: the glock to look at 2174 * 2175 */ 2176 2177 static void clear_glock(struct gfs2_glock *gl) 2178 { 2179 gfs2_glock_remove_from_lru(gl); 2180 2181 spin_lock(&gl->gl_lockref.lock); 2182 if (!__lockref_is_dead(&gl->gl_lockref)) { 2183 gl->gl_lockref.count++; 2184 if (gl->gl_state != LM_ST_UNLOCKED) 2185 handle_callback(gl, LM_ST_UNLOCKED, 0, false); 2186 __gfs2_glock_queue_work(gl, 0); 2187 } 2188 spin_unlock(&gl->gl_lockref.lock); 2189 } 2190 2191 /** 2192 * gfs2_glock_thaw - Thaw any frozen glocks 2193 * @sdp: The super block 2194 * 2195 */ 2196 2197 void gfs2_glock_thaw(struct gfs2_sbd *sdp) 2198 { 2199 glock_hash_walk(thaw_glock, sdp); 2200 } 2201 2202 static void dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid) 2203 { 2204 spin_lock(&gl->gl_lockref.lock); 2205 gfs2_dump_glock(seq, gl, fsid); 2206 spin_unlock(&gl->gl_lockref.lock); 2207 } 2208 2209 static void dump_glock_func(struct gfs2_glock *gl) 2210 { 2211 dump_glock(NULL, gl, true); 2212 } 2213 2214 static void withdraw_dq(struct gfs2_glock *gl) 2215 { 2216 spin_lock(&gl->gl_lockref.lock); 2217 if (!__lockref_is_dead(&gl->gl_lockref) && 2218 glock_blocked_by_withdraw(gl)) 2219 do_error(gl, LM_OUT_ERROR); /* remove pending waiters */ 2220 spin_unlock(&gl->gl_lockref.lock); 2221 } 2222 2223 void gfs2_gl_dq_holders(struct gfs2_sbd *sdp) 2224 { 2225 glock_hash_walk(withdraw_dq, sdp); 2226 } 2227 2228 /** 2229 * gfs2_gl_hash_clear - Empty out the glock hash table 2230 * @sdp: the filesystem 2231 * 2232 * Called when unmounting the filesystem. 2233 */ 2234 2235 void gfs2_gl_hash_clear(struct gfs2_sbd *sdp) 2236 { 2237 set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags); 2238 flush_workqueue(glock_workqueue); 2239 glock_hash_walk(clear_glock, sdp); 2240 flush_workqueue(glock_workqueue); 2241 wait_event_timeout(sdp->sd_kill_wait, 2242 atomic_read(&sdp->sd_glock_disposal) == 0, 2243 HZ * 600); 2244 gfs2_lm_unmount(sdp); 2245 gfs2_free_dead_glocks(sdp); 2246 glock_hash_walk(dump_glock_func, sdp); 2247 } 2248 2249 static const char *state2str(unsigned state) 2250 { 2251 switch(state) { 2252 case LM_ST_UNLOCKED: 2253 return "UN"; 2254 case LM_ST_SHARED: 2255 return "SH"; 2256 case LM_ST_DEFERRED: 2257 return "DF"; 2258 case LM_ST_EXCLUSIVE: 2259 return "EX"; 2260 } 2261 return "??"; 2262 } 2263 2264 static const char *hflags2str(char *buf, u16 flags, unsigned long iflags) 2265 { 2266 char *p = buf; 2267 if (flags & LM_FLAG_TRY) 2268 *p++ = 't'; 2269 if (flags & LM_FLAG_TRY_1CB) 2270 *p++ = 'T'; 2271 if (flags & LM_FLAG_NOEXP) 2272 *p++ = 'e'; 2273 if (flags & LM_FLAG_ANY) 2274 *p++ = 'A'; 2275 if (flags & LM_FLAG_NODE_SCOPE) 2276 *p++ = 'n'; 2277 if (flags & GL_ASYNC) 2278 *p++ = 'a'; 2279 if (flags & GL_EXACT) 2280 *p++ = 'E'; 2281 if (flags & GL_NOCACHE) 2282 *p++ = 'c'; 2283 if (test_bit(HIF_HOLDER, &iflags)) 2284 *p++ = 'H'; 2285 if (test_bit(HIF_WAIT, &iflags)) 2286 *p++ = 'W'; 2287 if (flags & GL_SKIP) 2288 *p++ = 's'; 2289 *p = 0; 2290 return buf; 2291 } 2292 2293 /** 2294 * dump_holder - print information about a glock holder 2295 * @seq: the seq_file struct 2296 * @gh: the glock holder 2297 * @fs_id_buf: pointer to file system id (if requested) 2298 * 2299 */ 2300 2301 static void dump_holder(struct seq_file *seq, const struct gfs2_holder *gh, 2302 const char *fs_id_buf) 2303 { 2304 const char *comm = "(none)"; 2305 pid_t owner_pid = 0; 2306 char flags_buf[32]; 2307 2308 rcu_read_lock(); 2309 if (pid_is_meaningful(gh)) { 2310 struct task_struct *gh_owner; 2311 2312 comm = "(ended)"; 2313 owner_pid = pid_nr(gh->gh_owner_pid); 2314 gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID); 2315 if (gh_owner) 2316 comm = gh_owner->comm; 2317 } 2318 gfs2_print_dbg(seq, "%s H: s:%s f:%s e:%d p:%ld [%s] %pS\n", 2319 fs_id_buf, state2str(gh->gh_state), 2320 hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags), 2321 gh->gh_error, (long)owner_pid, comm, (void *)gh->gh_ip); 2322 rcu_read_unlock(); 2323 } 2324 2325 static const char *gflags2str(char *buf, const struct gfs2_glock *gl) 2326 { 2327 const unsigned long *gflags = &gl->gl_flags; 2328 char *p = buf; 2329 2330 if (test_bit(GLF_LOCK, gflags)) 2331 *p++ = 'l'; 2332 if (test_bit(GLF_DEMOTE, gflags)) 2333 *p++ = 'D'; 2334 if (test_bit(GLF_PENDING_DEMOTE, gflags)) 2335 *p++ = 'd'; 2336 if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags)) 2337 *p++ = 'p'; 2338 if (test_bit(GLF_DIRTY, gflags)) 2339 *p++ = 'y'; 2340 if (test_bit(GLF_LFLUSH, gflags)) 2341 *p++ = 'f'; 2342 if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags)) 2343 *p++ = 'i'; 2344 if (test_bit(GLF_REPLY_PENDING, gflags)) 2345 *p++ = 'r'; 2346 if (test_bit(GLF_INITIAL, gflags)) 2347 *p++ = 'I'; 2348 if (test_bit(GLF_FROZEN, gflags)) 2349 *p++ = 'F'; 2350 if (!list_empty(&gl->gl_holders)) 2351 *p++ = 'q'; 2352 if (test_bit(GLF_LRU, gflags)) 2353 *p++ = 'L'; 2354 if (gl->gl_object) 2355 *p++ = 'o'; 2356 if (test_bit(GLF_BLOCKING, gflags)) 2357 *p++ = 'b'; 2358 if (test_bit(GLF_FREEING, gflags)) 2359 *p++ = 'x'; 2360 if (test_bit(GLF_INSTANTIATE_NEEDED, gflags)) 2361 *p++ = 'n'; 2362 if (test_bit(GLF_INSTANTIATE_IN_PROG, gflags)) 2363 *p++ = 'N'; 2364 if (test_bit(GLF_TRY_TO_EVICT, gflags)) 2365 *p++ = 'e'; 2366 if (test_bit(GLF_VERIFY_DELETE, gflags)) 2367 *p++ = 'E'; 2368 *p = 0; 2369 return buf; 2370 } 2371 2372 /** 2373 * gfs2_dump_glock - print information about a glock 2374 * @seq: The seq_file struct 2375 * @gl: the glock 2376 * @fsid: If true, also dump the file system id 2377 * 2378 * The file format is as follows: 2379 * One line per object, capital letters are used to indicate objects 2380 * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented, 2381 * other objects are indented by a single space and follow the glock to 2382 * which they are related. Fields are indicated by lower case letters 2383 * followed by a colon and the field value, except for strings which are in 2384 * [] so that its possible to see if they are composed of spaces for 2385 * example. The field's are n = number (id of the object), f = flags, 2386 * t = type, s = state, r = refcount, e = error, p = pid. 2387 * 2388 */ 2389 2390 void gfs2_dump_glock(struct seq_file *seq, struct gfs2_glock *gl, bool fsid) 2391 { 2392 const struct gfs2_glock_operations *glops = gl->gl_ops; 2393 unsigned long long dtime; 2394 const struct gfs2_holder *gh; 2395 char gflags_buf[32]; 2396 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 2397 char fs_id_buf[sizeof(sdp->sd_fsname) + 7]; 2398 unsigned long nrpages = 0; 2399 2400 if (gl->gl_ops->go_flags & GLOF_ASPACE) { 2401 struct address_space *mapping = gfs2_glock2aspace(gl); 2402 2403 nrpages = mapping->nrpages; 2404 } 2405 memset(fs_id_buf, 0, sizeof(fs_id_buf)); 2406 if (fsid && sdp) /* safety precaution */ 2407 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname); 2408 dtime = jiffies - gl->gl_demote_time; 2409 dtime *= 1000000/HZ; /* demote time in uSec */ 2410 if (!test_bit(GLF_DEMOTE, &gl->gl_flags)) 2411 dtime = 0; 2412 gfs2_print_dbg(seq, "%sG: s:%s n:%u/%llx f:%s t:%s d:%s/%llu a:%d " 2413 "v:%d r:%d m:%ld p:%lu\n", 2414 fs_id_buf, state2str(gl->gl_state), 2415 gl->gl_name.ln_type, 2416 (unsigned long long)gl->gl_name.ln_number, 2417 gflags2str(gflags_buf, gl), 2418 state2str(gl->gl_target), 2419 state2str(gl->gl_demote_state), dtime, 2420 atomic_read(&gl->gl_ail_count), 2421 atomic_read(&gl->gl_revokes), 2422 (int)gl->gl_lockref.count, gl->gl_hold_time, nrpages); 2423 2424 list_for_each_entry(gh, &gl->gl_holders, gh_list) 2425 dump_holder(seq, gh, fs_id_buf); 2426 2427 if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump) 2428 glops->go_dump(seq, gl, fs_id_buf); 2429 } 2430 2431 static int gfs2_glstats_seq_show(struct seq_file *seq, void *iter_ptr) 2432 { 2433 struct gfs2_glock *gl = iter_ptr; 2434 2435 seq_printf(seq, "G: n:%u/%llx rtt:%llu/%llu rttb:%llu/%llu irt:%llu/%llu dcnt: %llu qcnt: %llu\n", 2436 gl->gl_name.ln_type, 2437 (unsigned long long)gl->gl_name.ln_number, 2438 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTT], 2439 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVAR], 2440 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTB], 2441 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SRTTVARB], 2442 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRT], 2443 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_SIRTVAR], 2444 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_DCOUNT], 2445 (unsigned long long)gl->gl_stats.stats[GFS2_LKS_QCOUNT]); 2446 return 0; 2447 } 2448 2449 static const char *gfs2_gltype[] = { 2450 "type", 2451 "reserved", 2452 "nondisk", 2453 "inode", 2454 "rgrp", 2455 "meta", 2456 "iopen", 2457 "flock", 2458 "plock", 2459 "quota", 2460 "journal", 2461 }; 2462 2463 static const char *gfs2_stype[] = { 2464 [GFS2_LKS_SRTT] = "srtt", 2465 [GFS2_LKS_SRTTVAR] = "srttvar", 2466 [GFS2_LKS_SRTTB] = "srttb", 2467 [GFS2_LKS_SRTTVARB] = "srttvarb", 2468 [GFS2_LKS_SIRT] = "sirt", 2469 [GFS2_LKS_SIRTVAR] = "sirtvar", 2470 [GFS2_LKS_DCOUNT] = "dlm", 2471 [GFS2_LKS_QCOUNT] = "queue", 2472 }; 2473 2474 #define GFS2_NR_SBSTATS (ARRAY_SIZE(gfs2_gltype) * ARRAY_SIZE(gfs2_stype)) 2475 2476 static int gfs2_sbstats_seq_show(struct seq_file *seq, void *iter_ptr) 2477 { 2478 struct gfs2_sbd *sdp = seq->private; 2479 loff_t pos = *(loff_t *)iter_ptr; 2480 unsigned index = pos >> 3; 2481 unsigned subindex = pos & 0x07; 2482 int i; 2483 2484 if (index == 0 && subindex != 0) 2485 return 0; 2486 2487 seq_printf(seq, "%-10s %8s:", gfs2_gltype[index], 2488 (index == 0) ? "cpu": gfs2_stype[subindex]); 2489 2490 for_each_possible_cpu(i) { 2491 const struct gfs2_pcpu_lkstats *lkstats = per_cpu_ptr(sdp->sd_lkstats, i); 2492 2493 if (index == 0) 2494 seq_printf(seq, " %15u", i); 2495 else 2496 seq_printf(seq, " %15llu", (unsigned long long)lkstats-> 2497 lkstats[index - 1].stats[subindex]); 2498 } 2499 seq_putc(seq, '\n'); 2500 return 0; 2501 } 2502 2503 int __init gfs2_glock_init(void) 2504 { 2505 int i, ret; 2506 2507 ret = rhashtable_init(&gl_hash_table, &ht_parms); 2508 if (ret < 0) 2509 return ret; 2510 2511 glock_workqueue = alloc_workqueue("glock_workqueue", WQ_MEM_RECLAIM | 2512 WQ_HIGHPRI | WQ_FREEZABLE, 0); 2513 if (!glock_workqueue) { 2514 rhashtable_destroy(&gl_hash_table); 2515 return -ENOMEM; 2516 } 2517 2518 ret = register_shrinker(&glock_shrinker, "gfs2-glock"); 2519 if (ret) { 2520 destroy_workqueue(glock_workqueue); 2521 rhashtable_destroy(&gl_hash_table); 2522 return ret; 2523 } 2524 2525 for (i = 0; i < GLOCK_WAIT_TABLE_SIZE; i++) 2526 init_waitqueue_head(glock_wait_table + i); 2527 2528 return 0; 2529 } 2530 2531 void gfs2_glock_exit(void) 2532 { 2533 unregister_shrinker(&glock_shrinker); 2534 rhashtable_destroy(&gl_hash_table); 2535 destroy_workqueue(glock_workqueue); 2536 } 2537 2538 static void gfs2_glock_iter_next(struct gfs2_glock_iter *gi, loff_t n) 2539 { 2540 struct gfs2_glock *gl = gi->gl; 2541 2542 if (gl) { 2543 if (n == 0) 2544 return; 2545 gfs2_glock_put_async(gl); 2546 } 2547 for (;;) { 2548 gl = rhashtable_walk_next(&gi->hti); 2549 if (IS_ERR_OR_NULL(gl)) { 2550 if (gl == ERR_PTR(-EAGAIN)) { 2551 n = 1; 2552 continue; 2553 } 2554 gl = NULL; 2555 break; 2556 } 2557 if (gl->gl_name.ln_sbd != gi->sdp) 2558 continue; 2559 if (n <= 1) { 2560 if (!lockref_get_not_dead(&gl->gl_lockref)) 2561 continue; 2562 break; 2563 } else { 2564 if (__lockref_is_dead(&gl->gl_lockref)) 2565 continue; 2566 n--; 2567 } 2568 } 2569 gi->gl = gl; 2570 } 2571 2572 static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos) 2573 __acquires(RCU) 2574 { 2575 struct gfs2_glock_iter *gi = seq->private; 2576 loff_t n; 2577 2578 /* 2579 * We can either stay where we are, skip to the next hash table 2580 * entry, or start from the beginning. 2581 */ 2582 if (*pos < gi->last_pos) { 2583 rhashtable_walk_exit(&gi->hti); 2584 rhashtable_walk_enter(&gl_hash_table, &gi->hti); 2585 n = *pos + 1; 2586 } else { 2587 n = *pos - gi->last_pos; 2588 } 2589 2590 rhashtable_walk_start(&gi->hti); 2591 2592 gfs2_glock_iter_next(gi, n); 2593 gi->last_pos = *pos; 2594 return gi->gl; 2595 } 2596 2597 static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr, 2598 loff_t *pos) 2599 { 2600 struct gfs2_glock_iter *gi = seq->private; 2601 2602 (*pos)++; 2603 gi->last_pos = *pos; 2604 gfs2_glock_iter_next(gi, 1); 2605 return gi->gl; 2606 } 2607 2608 static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr) 2609 __releases(RCU) 2610 { 2611 struct gfs2_glock_iter *gi = seq->private; 2612 2613 rhashtable_walk_stop(&gi->hti); 2614 } 2615 2616 static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr) 2617 { 2618 dump_glock(seq, iter_ptr, false); 2619 return 0; 2620 } 2621 2622 static void *gfs2_sbstats_seq_start(struct seq_file *seq, loff_t *pos) 2623 { 2624 preempt_disable(); 2625 if (*pos >= GFS2_NR_SBSTATS) 2626 return NULL; 2627 return pos; 2628 } 2629 2630 static void *gfs2_sbstats_seq_next(struct seq_file *seq, void *iter_ptr, 2631 loff_t *pos) 2632 { 2633 (*pos)++; 2634 if (*pos >= GFS2_NR_SBSTATS) 2635 return NULL; 2636 return pos; 2637 } 2638 2639 static void gfs2_sbstats_seq_stop(struct seq_file *seq, void *iter_ptr) 2640 { 2641 preempt_enable(); 2642 } 2643 2644 static const struct seq_operations gfs2_glock_seq_ops = { 2645 .start = gfs2_glock_seq_start, 2646 .next = gfs2_glock_seq_next, 2647 .stop = gfs2_glock_seq_stop, 2648 .show = gfs2_glock_seq_show, 2649 }; 2650 2651 static const struct seq_operations gfs2_glstats_seq_ops = { 2652 .start = gfs2_glock_seq_start, 2653 .next = gfs2_glock_seq_next, 2654 .stop = gfs2_glock_seq_stop, 2655 .show = gfs2_glstats_seq_show, 2656 }; 2657 2658 static const struct seq_operations gfs2_sbstats_sops = { 2659 .start = gfs2_sbstats_seq_start, 2660 .next = gfs2_sbstats_seq_next, 2661 .stop = gfs2_sbstats_seq_stop, 2662 .show = gfs2_sbstats_seq_show, 2663 }; 2664 2665 #define GFS2_SEQ_GOODSIZE min(PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER, 65536UL) 2666 2667 static int __gfs2_glocks_open(struct inode *inode, struct file *file, 2668 const struct seq_operations *ops) 2669 { 2670 int ret = seq_open_private(file, ops, sizeof(struct gfs2_glock_iter)); 2671 if (ret == 0) { 2672 struct seq_file *seq = file->private_data; 2673 struct gfs2_glock_iter *gi = seq->private; 2674 2675 gi->sdp = inode->i_private; 2676 seq->buf = kmalloc(GFS2_SEQ_GOODSIZE, GFP_KERNEL | __GFP_NOWARN); 2677 if (seq->buf) 2678 seq->size = GFS2_SEQ_GOODSIZE; 2679 /* 2680 * Initially, we are "before" the first hash table entry; the 2681 * first call to rhashtable_walk_next gets us the first entry. 2682 */ 2683 gi->last_pos = -1; 2684 gi->gl = NULL; 2685 rhashtable_walk_enter(&gl_hash_table, &gi->hti); 2686 } 2687 return ret; 2688 } 2689 2690 static int gfs2_glocks_open(struct inode *inode, struct file *file) 2691 { 2692 return __gfs2_glocks_open(inode, file, &gfs2_glock_seq_ops); 2693 } 2694 2695 static int gfs2_glocks_release(struct inode *inode, struct file *file) 2696 { 2697 struct seq_file *seq = file->private_data; 2698 struct gfs2_glock_iter *gi = seq->private; 2699 2700 if (gi->gl) 2701 gfs2_glock_put(gi->gl); 2702 rhashtable_walk_exit(&gi->hti); 2703 return seq_release_private(inode, file); 2704 } 2705 2706 static int gfs2_glstats_open(struct inode *inode, struct file *file) 2707 { 2708 return __gfs2_glocks_open(inode, file, &gfs2_glstats_seq_ops); 2709 } 2710 2711 static const struct file_operations gfs2_glocks_fops = { 2712 .owner = THIS_MODULE, 2713 .open = gfs2_glocks_open, 2714 .read = seq_read, 2715 .llseek = seq_lseek, 2716 .release = gfs2_glocks_release, 2717 }; 2718 2719 static const struct file_operations gfs2_glstats_fops = { 2720 .owner = THIS_MODULE, 2721 .open = gfs2_glstats_open, 2722 .read = seq_read, 2723 .llseek = seq_lseek, 2724 .release = gfs2_glocks_release, 2725 }; 2726 2727 struct gfs2_glockfd_iter { 2728 struct super_block *sb; 2729 unsigned int tgid; 2730 struct task_struct *task; 2731 unsigned int fd; 2732 struct file *file; 2733 }; 2734 2735 static struct task_struct *gfs2_glockfd_next_task(struct gfs2_glockfd_iter *i) 2736 { 2737 struct pid_namespace *ns = task_active_pid_ns(current); 2738 struct pid *pid; 2739 2740 if (i->task) 2741 put_task_struct(i->task); 2742 2743 rcu_read_lock(); 2744 retry: 2745 i->task = NULL; 2746 pid = find_ge_pid(i->tgid, ns); 2747 if (pid) { 2748 i->tgid = pid_nr_ns(pid, ns); 2749 i->task = pid_task(pid, PIDTYPE_TGID); 2750 if (!i->task) { 2751 i->tgid++; 2752 goto retry; 2753 } 2754 get_task_struct(i->task); 2755 } 2756 rcu_read_unlock(); 2757 return i->task; 2758 } 2759 2760 static struct file *gfs2_glockfd_next_file(struct gfs2_glockfd_iter *i) 2761 { 2762 if (i->file) { 2763 fput(i->file); 2764 i->file = NULL; 2765 } 2766 2767 rcu_read_lock(); 2768 for(;; i->fd++) { 2769 struct inode *inode; 2770 2771 i->file = task_lookup_next_fd_rcu(i->task, &i->fd); 2772 if (!i->file) { 2773 i->fd = 0; 2774 break; 2775 } 2776 inode = file_inode(i->file); 2777 if (inode->i_sb != i->sb) 2778 continue; 2779 if (get_file_rcu(i->file)) 2780 break; 2781 } 2782 rcu_read_unlock(); 2783 return i->file; 2784 } 2785 2786 static void *gfs2_glockfd_seq_start(struct seq_file *seq, loff_t *pos) 2787 { 2788 struct gfs2_glockfd_iter *i = seq->private; 2789 2790 if (*pos) 2791 return NULL; 2792 while (gfs2_glockfd_next_task(i)) { 2793 if (gfs2_glockfd_next_file(i)) 2794 return i; 2795 i->tgid++; 2796 } 2797 return NULL; 2798 } 2799 2800 static void *gfs2_glockfd_seq_next(struct seq_file *seq, void *iter_ptr, 2801 loff_t *pos) 2802 { 2803 struct gfs2_glockfd_iter *i = seq->private; 2804 2805 (*pos)++; 2806 i->fd++; 2807 do { 2808 if (gfs2_glockfd_next_file(i)) 2809 return i; 2810 i->tgid++; 2811 } while (gfs2_glockfd_next_task(i)); 2812 return NULL; 2813 } 2814 2815 static void gfs2_glockfd_seq_stop(struct seq_file *seq, void *iter_ptr) 2816 { 2817 struct gfs2_glockfd_iter *i = seq->private; 2818 2819 if (i->file) 2820 fput(i->file); 2821 if (i->task) 2822 put_task_struct(i->task); 2823 } 2824 2825 static void gfs2_glockfd_seq_show_flock(struct seq_file *seq, 2826 struct gfs2_glockfd_iter *i) 2827 { 2828 struct gfs2_file *fp = i->file->private_data; 2829 struct gfs2_holder *fl_gh = &fp->f_fl_gh; 2830 struct lm_lockname gl_name = { .ln_type = LM_TYPE_RESERVED }; 2831 2832 if (!READ_ONCE(fl_gh->gh_gl)) 2833 return; 2834 2835 spin_lock(&i->file->f_lock); 2836 if (gfs2_holder_initialized(fl_gh)) 2837 gl_name = fl_gh->gh_gl->gl_name; 2838 spin_unlock(&i->file->f_lock); 2839 2840 if (gl_name.ln_type != LM_TYPE_RESERVED) { 2841 seq_printf(seq, "%d %u %u/%llx\n", 2842 i->tgid, i->fd, gl_name.ln_type, 2843 (unsigned long long)gl_name.ln_number); 2844 } 2845 } 2846 2847 static int gfs2_glockfd_seq_show(struct seq_file *seq, void *iter_ptr) 2848 { 2849 struct gfs2_glockfd_iter *i = seq->private; 2850 struct inode *inode = file_inode(i->file); 2851 struct gfs2_glock *gl; 2852 2853 inode_lock_shared(inode); 2854 gl = GFS2_I(inode)->i_iopen_gh.gh_gl; 2855 if (gl) { 2856 seq_printf(seq, "%d %u %u/%llx\n", 2857 i->tgid, i->fd, gl->gl_name.ln_type, 2858 (unsigned long long)gl->gl_name.ln_number); 2859 } 2860 gfs2_glockfd_seq_show_flock(seq, i); 2861 inode_unlock_shared(inode); 2862 return 0; 2863 } 2864 2865 static const struct seq_operations gfs2_glockfd_seq_ops = { 2866 .start = gfs2_glockfd_seq_start, 2867 .next = gfs2_glockfd_seq_next, 2868 .stop = gfs2_glockfd_seq_stop, 2869 .show = gfs2_glockfd_seq_show, 2870 }; 2871 2872 static int gfs2_glockfd_open(struct inode *inode, struct file *file) 2873 { 2874 struct gfs2_glockfd_iter *i; 2875 struct gfs2_sbd *sdp = inode->i_private; 2876 2877 i = __seq_open_private(file, &gfs2_glockfd_seq_ops, 2878 sizeof(struct gfs2_glockfd_iter)); 2879 if (!i) 2880 return -ENOMEM; 2881 i->sb = sdp->sd_vfs; 2882 return 0; 2883 } 2884 2885 static const struct file_operations gfs2_glockfd_fops = { 2886 .owner = THIS_MODULE, 2887 .open = gfs2_glockfd_open, 2888 .read = seq_read, 2889 .llseek = seq_lseek, 2890 .release = seq_release_private, 2891 }; 2892 2893 DEFINE_SEQ_ATTRIBUTE(gfs2_sbstats); 2894 2895 void gfs2_create_debugfs_file(struct gfs2_sbd *sdp) 2896 { 2897 sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root); 2898 2899 debugfs_create_file("glocks", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp, 2900 &gfs2_glocks_fops); 2901 2902 debugfs_create_file("glockfd", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp, 2903 &gfs2_glockfd_fops); 2904 2905 debugfs_create_file("glstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp, 2906 &gfs2_glstats_fops); 2907 2908 debugfs_create_file("sbstats", S_IFREG | S_IRUGO, sdp->debugfs_dir, sdp, 2909 &gfs2_sbstats_fops); 2910 } 2911 2912 void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp) 2913 { 2914 debugfs_remove_recursive(sdp->debugfs_dir); 2915 sdp->debugfs_dir = NULL; 2916 } 2917 2918 void gfs2_register_debugfs(void) 2919 { 2920 gfs2_root = debugfs_create_dir("gfs2", NULL); 2921 } 2922 2923 void gfs2_unregister_debugfs(void) 2924 { 2925 debugfs_remove(gfs2_root); 2926 gfs2_root = NULL; 2927 } 2928