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