David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 3 | * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. |
| 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
| 7 | * of the GNU General Public License v.2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/sort.h> |
| 17 | #include <linux/jhash.h> |
| 18 | #include <linux/kref.h> |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 19 | #include <linux/kallsyms.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 20 | #include <linux/gfs2_ondisk.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 21 | #include <asm/semaphore.h> |
| 22 | #include <asm/uaccess.h> |
| 23 | |
| 24 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 25 | #include "lm_interface.h" |
| 26 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 27 | #include "glock.h" |
| 28 | #include "glops.h" |
| 29 | #include "inode.h" |
| 30 | #include "lm.h" |
| 31 | #include "lops.h" |
| 32 | #include "meta_io.h" |
| 33 | #include "quota.h" |
| 34 | #include "super.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 35 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | |
| 37 | /* Must be kept in sync with the beginning of struct gfs2_glock */ |
| 38 | struct glock_plug { |
| 39 | struct list_head gl_list; |
| 40 | unsigned long gl_flags; |
| 41 | }; |
| 42 | |
| 43 | struct greedy { |
| 44 | struct gfs2_holder gr_gh; |
| 45 | struct work_struct gr_work; |
| 46 | }; |
| 47 | |
| 48 | typedef void (*glock_examiner) (struct gfs2_glock * gl); |
| 49 | |
| 50 | /** |
| 51 | * relaxed_state_ok - is a requested lock compatible with the current lock mode? |
| 52 | * @actual: the current state of the lock |
| 53 | * @requested: the lock state that was requested by the caller |
| 54 | * @flags: the modifier flags passed in by the caller |
| 55 | * |
| 56 | * Returns: 1 if the locks are compatible, 0 otherwise |
| 57 | */ |
| 58 | |
| 59 | static inline int relaxed_state_ok(unsigned int actual, unsigned requested, |
| 60 | int flags) |
| 61 | { |
| 62 | if (actual == requested) |
| 63 | return 1; |
| 64 | |
| 65 | if (flags & GL_EXACT) |
| 66 | return 0; |
| 67 | |
| 68 | if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED) |
| 69 | return 1; |
| 70 | |
| 71 | if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY)) |
| 72 | return 1; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * gl_hash() - Turn glock number into hash bucket number |
| 79 | * @lock: The glock number |
| 80 | * |
| 81 | * Returns: The number of the corresponding hash bucket |
| 82 | */ |
| 83 | |
| 84 | static unsigned int gl_hash(struct lm_lockname *name) |
| 85 | { |
| 86 | unsigned int h; |
| 87 | |
| 88 | h = jhash(&name->ln_number, sizeof(uint64_t), 0); |
| 89 | h = jhash(&name->ln_type, sizeof(unsigned int), h); |
| 90 | h &= GFS2_GL_HASH_MASK; |
| 91 | |
| 92 | return h; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * glock_free() - Perform a few checks and then release struct gfs2_glock |
| 97 | * @gl: The glock to release |
| 98 | * |
| 99 | * Also calls lock module to release its internal structure for this glock. |
| 100 | * |
| 101 | */ |
| 102 | |
| 103 | static void glock_free(struct gfs2_glock *gl) |
| 104 | { |
| 105 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 106 | struct inode *aspace = gl->gl_aspace; |
| 107 | |
| 108 | gfs2_lm_put_lock(sdp, gl->gl_lock); |
| 109 | |
| 110 | if (aspace) |
| 111 | gfs2_aspace_put(aspace); |
| 112 | |
| 113 | kmem_cache_free(gfs2_glock_cachep, gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
| 117 | * gfs2_glock_hold() - increment reference count on glock |
| 118 | * @gl: The glock to hold |
| 119 | * |
| 120 | */ |
| 121 | |
| 122 | void gfs2_glock_hold(struct gfs2_glock *gl) |
| 123 | { |
| 124 | kref_get(&gl->gl_ref); |
| 125 | } |
| 126 | |
| 127 | /* All work is done after the return from kref_put() so we |
| 128 | can release the write_lock before the free. */ |
| 129 | |
| 130 | static void kill_glock(struct kref *kref) |
| 131 | { |
| 132 | struct gfs2_glock *gl = container_of(kref, struct gfs2_glock, gl_ref); |
| 133 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 134 | |
| 135 | gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED); |
| 136 | gfs2_assert(sdp, list_empty(&gl->gl_reclaim)); |
| 137 | gfs2_assert(sdp, list_empty(&gl->gl_holders)); |
| 138 | gfs2_assert(sdp, list_empty(&gl->gl_waiters1)); |
| 139 | gfs2_assert(sdp, list_empty(&gl->gl_waiters2)); |
| 140 | gfs2_assert(sdp, list_empty(&gl->gl_waiters3)); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * gfs2_glock_put() - Decrement reference count on glock |
| 145 | * @gl: The glock to put |
| 146 | * |
| 147 | */ |
| 148 | |
| 149 | int gfs2_glock_put(struct gfs2_glock *gl) |
| 150 | { |
| 151 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 152 | struct gfs2_gl_hash_bucket *bucket = gl->gl_bucket; |
| 153 | int rv = 0; |
| 154 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 155 | mutex_lock(&sdp->sd_invalidate_inodes_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 156 | |
| 157 | write_lock(&bucket->hb_lock); |
| 158 | if (kref_put(&gl->gl_ref, kill_glock)) { |
| 159 | list_del_init(&gl->gl_list); |
| 160 | write_unlock(&bucket->hb_lock); |
| 161 | glock_free(gl); |
| 162 | rv = 1; |
| 163 | goto out; |
| 164 | } |
| 165 | write_unlock(&bucket->hb_lock); |
| 166 | out: |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 167 | mutex_unlock(&sdp->sd_invalidate_inodes_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 168 | return rv; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * queue_empty - check to see if a glock's queue is empty |
| 173 | * @gl: the glock |
| 174 | * @head: the head of the queue to check |
| 175 | * |
| 176 | * This function protects the list in the event that a process already |
| 177 | * has a holder on the list and is adding a second holder for itself. |
| 178 | * The glmutex lock is what generally prevents processes from working |
| 179 | * on the same glock at once, but the special case of adding a second |
| 180 | * holder for yourself ("recursive" locking) doesn't involve locking |
| 181 | * glmutex, making the spin lock necessary. |
| 182 | * |
| 183 | * Returns: 1 if the queue is empty |
| 184 | */ |
| 185 | |
| 186 | static inline int queue_empty(struct gfs2_glock *gl, struct list_head *head) |
| 187 | { |
| 188 | int empty; |
| 189 | spin_lock(&gl->gl_spin); |
| 190 | empty = list_empty(head); |
| 191 | spin_unlock(&gl->gl_spin); |
| 192 | return empty; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * search_bucket() - Find struct gfs2_glock by lock number |
| 197 | * @bucket: the bucket to search |
| 198 | * @name: The lock name |
| 199 | * |
| 200 | * Returns: NULL, or the struct gfs2_glock with the requested number |
| 201 | */ |
| 202 | |
| 203 | static struct gfs2_glock *search_bucket(struct gfs2_gl_hash_bucket *bucket, |
| 204 | struct lm_lockname *name) |
| 205 | { |
| 206 | struct gfs2_glock *gl; |
| 207 | |
| 208 | list_for_each_entry(gl, &bucket->hb_list, gl_list) { |
| 209 | if (test_bit(GLF_PLUG, &gl->gl_flags)) |
| 210 | continue; |
| 211 | if (!lm_name_equal(&gl->gl_name, name)) |
| 212 | continue; |
| 213 | |
| 214 | kref_get(&gl->gl_ref); |
| 215 | |
| 216 | return gl; |
| 217 | } |
| 218 | |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * gfs2_glock_find() - Find glock by lock number |
| 224 | * @sdp: The GFS2 superblock |
| 225 | * @name: The lock name |
| 226 | * |
| 227 | * Returns: NULL, or the struct gfs2_glock with the requested number |
| 228 | */ |
| 229 | |
| 230 | struct gfs2_glock *gfs2_glock_find(struct gfs2_sbd *sdp, |
| 231 | struct lm_lockname *name) |
| 232 | { |
| 233 | struct gfs2_gl_hash_bucket *bucket = &sdp->sd_gl_hash[gl_hash(name)]; |
| 234 | struct gfs2_glock *gl; |
| 235 | |
| 236 | read_lock(&bucket->hb_lock); |
| 237 | gl = search_bucket(bucket, name); |
| 238 | read_unlock(&bucket->hb_lock); |
| 239 | |
| 240 | return gl; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * gfs2_glock_get() - Get a glock, or create one if one doesn't exist |
| 245 | * @sdp: The GFS2 superblock |
| 246 | * @number: the lock number |
| 247 | * @glops: The glock_operations to use |
| 248 | * @create: If 0, don't create the glock if it doesn't exist |
| 249 | * @glp: the glock is returned here |
| 250 | * |
| 251 | * This does not lock a glock, just finds/creates structures for one. |
| 252 | * |
| 253 | * Returns: errno |
| 254 | */ |
| 255 | |
| 256 | int gfs2_glock_get(struct gfs2_sbd *sdp, uint64_t number, |
| 257 | struct gfs2_glock_operations *glops, int create, |
| 258 | struct gfs2_glock **glp) |
| 259 | { |
| 260 | struct lm_lockname name; |
| 261 | struct gfs2_glock *gl, *tmp; |
| 262 | struct gfs2_gl_hash_bucket *bucket; |
| 263 | int error; |
| 264 | |
| 265 | name.ln_number = number; |
| 266 | name.ln_type = glops->go_type; |
| 267 | bucket = &sdp->sd_gl_hash[gl_hash(&name)]; |
| 268 | |
| 269 | read_lock(&bucket->hb_lock); |
| 270 | gl = search_bucket(bucket, &name); |
| 271 | read_unlock(&bucket->hb_lock); |
| 272 | |
| 273 | if (gl || !create) { |
| 274 | *glp = gl; |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL); |
| 279 | if (!gl) |
| 280 | return -ENOMEM; |
| 281 | |
| 282 | memset(gl, 0, sizeof(struct gfs2_glock)); |
| 283 | |
| 284 | INIT_LIST_HEAD(&gl->gl_list); |
| 285 | gl->gl_name = name; |
| 286 | kref_init(&gl->gl_ref); |
| 287 | |
| 288 | spin_lock_init(&gl->gl_spin); |
| 289 | |
| 290 | gl->gl_state = LM_ST_UNLOCKED; |
| 291 | INIT_LIST_HEAD(&gl->gl_holders); |
| 292 | INIT_LIST_HEAD(&gl->gl_waiters1); |
| 293 | INIT_LIST_HEAD(&gl->gl_waiters2); |
| 294 | INIT_LIST_HEAD(&gl->gl_waiters3); |
| 295 | |
| 296 | gl->gl_ops = glops; |
| 297 | |
| 298 | gl->gl_bucket = bucket; |
| 299 | INIT_LIST_HEAD(&gl->gl_reclaim); |
| 300 | |
| 301 | gl->gl_sbd = sdp; |
| 302 | |
| 303 | lops_init_le(&gl->gl_le, &gfs2_glock_lops); |
| 304 | INIT_LIST_HEAD(&gl->gl_ail_list); |
| 305 | |
| 306 | /* If this glock protects actual on-disk data or metadata blocks, |
| 307 | create a VFS inode to manage the pages/buffers holding them. */ |
| 308 | if (glops == &gfs2_inode_glops || |
| 309 | glops == &gfs2_rgrp_glops || |
| 310 | glops == &gfs2_meta_glops) { |
| 311 | gl->gl_aspace = gfs2_aspace_get(sdp); |
| 312 | if (!gl->gl_aspace) { |
| 313 | error = -ENOMEM; |
| 314 | goto fail; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock); |
| 319 | if (error) |
| 320 | goto fail_aspace; |
| 321 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 322 | write_lock(&bucket->hb_lock); |
| 323 | tmp = search_bucket(bucket, &name); |
| 324 | if (tmp) { |
| 325 | write_unlock(&bucket->hb_lock); |
| 326 | glock_free(gl); |
| 327 | gl = tmp; |
| 328 | } else { |
| 329 | list_add_tail(&gl->gl_list, &bucket->hb_list); |
| 330 | write_unlock(&bucket->hb_lock); |
| 331 | } |
| 332 | |
| 333 | *glp = gl; |
| 334 | |
| 335 | return 0; |
| 336 | |
| 337 | fail_aspace: |
| 338 | if (gl->gl_aspace) |
| 339 | gfs2_aspace_put(gl->gl_aspace); |
| 340 | |
| 341 | fail: |
| 342 | kmem_cache_free(gfs2_glock_cachep, gl); |
| 343 | |
| 344 | return error; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * gfs2_holder_init - initialize a struct gfs2_holder in the default way |
| 349 | * @gl: the glock |
| 350 | * @state: the state we're requesting |
| 351 | * @flags: the modifier flags |
| 352 | * @gh: the holder structure |
| 353 | * |
| 354 | */ |
| 355 | |
| 356 | void gfs2_holder_init(struct gfs2_glock *gl, unsigned int state, int flags, |
| 357 | struct gfs2_holder *gh) |
| 358 | { |
| 359 | INIT_LIST_HEAD(&gh->gh_list); |
| 360 | gh->gh_gl = gl; |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 361 | gh->gh_ip = (unsigned long)__builtin_return_address(0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 362 | gh->gh_owner = (flags & GL_NEVER_RECURSE) ? NULL : current; |
| 363 | gh->gh_state = state; |
| 364 | gh->gh_flags = flags; |
| 365 | gh->gh_error = 0; |
| 366 | gh->gh_iflags = 0; |
| 367 | init_completion(&gh->gh_wait); |
| 368 | |
| 369 | if (gh->gh_state == LM_ST_EXCLUSIVE) |
| 370 | gh->gh_flags |= GL_LOCAL_EXCL; |
| 371 | |
| 372 | gfs2_glock_hold(gl); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it |
| 377 | * @state: the state we're requesting |
| 378 | * @flags: the modifier flags |
| 379 | * @gh: the holder structure |
| 380 | * |
| 381 | * Don't mess with the glock. |
| 382 | * |
| 383 | */ |
| 384 | |
| 385 | void gfs2_holder_reinit(unsigned int state, int flags, struct gfs2_holder *gh) |
| 386 | { |
| 387 | gh->gh_state = state; |
| 388 | gh->gh_flags = flags; |
| 389 | if (gh->gh_state == LM_ST_EXCLUSIVE) |
| 390 | gh->gh_flags |= GL_LOCAL_EXCL; |
| 391 | |
| 392 | gh->gh_iflags &= 1 << HIF_ALLOCED; |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 393 | gh->gh_ip = (unsigned long)__builtin_return_address(0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** |
| 397 | * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference) |
| 398 | * @gh: the holder structure |
| 399 | * |
| 400 | */ |
| 401 | |
| 402 | void gfs2_holder_uninit(struct gfs2_holder *gh) |
| 403 | { |
| 404 | gfs2_glock_put(gh->gh_gl); |
| 405 | gh->gh_gl = NULL; |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 406 | gh->gh_ip = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | /** |
| 410 | * gfs2_holder_get - get a struct gfs2_holder structure |
| 411 | * @gl: the glock |
| 412 | * @state: the state we're requesting |
| 413 | * @flags: the modifier flags |
| 414 | * @gfp_flags: __GFP_NOFAIL |
| 415 | * |
| 416 | * Figure out how big an impact this function has. Either: |
| 417 | * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd |
| 418 | * 2) Leave it like it is |
| 419 | * |
| 420 | * Returns: the holder structure, NULL on ENOMEM |
| 421 | */ |
| 422 | |
| 423 | struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl, unsigned int state, |
| 424 | int flags, gfp_t gfp_flags) |
| 425 | { |
| 426 | struct gfs2_holder *gh; |
| 427 | |
| 428 | gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags); |
| 429 | if (!gh) |
| 430 | return NULL; |
| 431 | |
| 432 | gfs2_holder_init(gl, state, flags, gh); |
| 433 | set_bit(HIF_ALLOCED, &gh->gh_iflags); |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 434 | gh->gh_ip = (unsigned long)__builtin_return_address(0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 435 | return gh; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * gfs2_holder_put - get rid of a struct gfs2_holder structure |
| 440 | * @gh: the holder structure |
| 441 | * |
| 442 | */ |
| 443 | |
| 444 | void gfs2_holder_put(struct gfs2_holder *gh) |
| 445 | { |
| 446 | gfs2_holder_uninit(gh); |
| 447 | kfree(gh); |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * handle_recurse - put other holder structures (marked recursive) |
| 452 | * into the holders list |
| 453 | * @gh: the holder structure |
| 454 | * |
| 455 | */ |
| 456 | |
| 457 | static void handle_recurse(struct gfs2_holder *gh) |
| 458 | { |
| 459 | struct gfs2_glock *gl = gh->gh_gl; |
| 460 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 461 | struct gfs2_holder *tmp_gh, *safe; |
| 462 | int found = 0; |
| 463 | |
Steven Whitehouse | fe1bded | 2006-04-18 10:09:15 -0400 | [diff] [blame^] | 464 | printk(KERN_INFO "recursion %016llx, %u\n", gl->gl_name.ln_number, |
| 465 | gl->gl_name.ln_type); |
| 466 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 467 | if (gfs2_assert_warn(sdp, gh->gh_owner)) |
| 468 | return; |
| 469 | |
| 470 | list_for_each_entry_safe(tmp_gh, safe, &gl->gl_waiters3, gh_list) { |
| 471 | if (tmp_gh->gh_owner != gh->gh_owner) |
| 472 | continue; |
| 473 | |
| 474 | gfs2_assert_warn(sdp, |
| 475 | test_bit(HIF_RECURSE, &tmp_gh->gh_iflags)); |
| 476 | |
| 477 | list_move_tail(&tmp_gh->gh_list, &gl->gl_holders); |
| 478 | tmp_gh->gh_error = 0; |
| 479 | set_bit(HIF_HOLDER, &tmp_gh->gh_iflags); |
| 480 | |
| 481 | complete(&tmp_gh->gh_wait); |
| 482 | |
| 483 | found = 1; |
| 484 | } |
| 485 | |
| 486 | gfs2_assert_warn(sdp, found); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * do_unrecurse - a recursive holder was just dropped of the waiters3 list |
| 491 | * @gh: the holder |
| 492 | * |
| 493 | * If there is only one other recursive holder, clear its HIF_RECURSE bit. |
| 494 | * If there is more than one, leave them alone. |
| 495 | * |
| 496 | */ |
| 497 | |
| 498 | static void do_unrecurse(struct gfs2_holder *gh) |
| 499 | { |
| 500 | struct gfs2_glock *gl = gh->gh_gl; |
| 501 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 502 | struct gfs2_holder *tmp_gh, *last_gh = NULL; |
| 503 | int found = 0; |
| 504 | |
| 505 | if (gfs2_assert_warn(sdp, gh->gh_owner)) |
| 506 | return; |
| 507 | |
| 508 | list_for_each_entry(tmp_gh, &gl->gl_waiters3, gh_list) { |
| 509 | if (tmp_gh->gh_owner != gh->gh_owner) |
| 510 | continue; |
| 511 | |
| 512 | gfs2_assert_warn(sdp, |
| 513 | test_bit(HIF_RECURSE, &tmp_gh->gh_iflags)); |
| 514 | |
| 515 | if (found) |
| 516 | return; |
| 517 | |
| 518 | found = 1; |
| 519 | last_gh = tmp_gh; |
| 520 | } |
| 521 | |
| 522 | if (!gfs2_assert_warn(sdp, found)) |
| 523 | clear_bit(HIF_RECURSE, &last_gh->gh_iflags); |
| 524 | } |
| 525 | |
| 526 | /** |
| 527 | * rq_mutex - process a mutex request in the queue |
| 528 | * @gh: the glock holder |
| 529 | * |
| 530 | * Returns: 1 if the queue is blocked |
| 531 | */ |
| 532 | |
| 533 | static int rq_mutex(struct gfs2_holder *gh) |
| 534 | { |
| 535 | struct gfs2_glock *gl = gh->gh_gl; |
| 536 | |
| 537 | list_del_init(&gh->gh_list); |
| 538 | /* gh->gh_error never examined. */ |
| 539 | set_bit(GLF_LOCK, &gl->gl_flags); |
| 540 | complete(&gh->gh_wait); |
| 541 | |
| 542 | return 1; |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * rq_promote - process a promote request in the queue |
| 547 | * @gh: the glock holder |
| 548 | * |
| 549 | * Acquire a new inter-node lock, or change a lock state to more restrictive. |
| 550 | * |
| 551 | * Returns: 1 if the queue is blocked |
| 552 | */ |
| 553 | |
| 554 | static int rq_promote(struct gfs2_holder *gh) |
| 555 | { |
| 556 | struct gfs2_glock *gl = gh->gh_gl; |
| 557 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 558 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 559 | int recurse; |
| 560 | |
| 561 | if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) { |
| 562 | if (list_empty(&gl->gl_holders)) { |
| 563 | gl->gl_req_gh = gh; |
| 564 | set_bit(GLF_LOCK, &gl->gl_flags); |
| 565 | spin_unlock(&gl->gl_spin); |
| 566 | |
| 567 | if (atomic_read(&sdp->sd_reclaim_count) > |
| 568 | gfs2_tune_get(sdp, gt_reclaim_limit) && |
| 569 | !(gh->gh_flags & LM_FLAG_PRIORITY)) { |
| 570 | gfs2_reclaim_glock(sdp); |
| 571 | gfs2_reclaim_glock(sdp); |
| 572 | } |
| 573 | |
| 574 | glops->go_xmote_th(gl, gh->gh_state, |
| 575 | gh->gh_flags); |
| 576 | |
| 577 | spin_lock(&gl->gl_spin); |
| 578 | } |
| 579 | return 1; |
| 580 | } |
| 581 | |
| 582 | if (list_empty(&gl->gl_holders)) { |
| 583 | set_bit(HIF_FIRST, &gh->gh_iflags); |
| 584 | set_bit(GLF_LOCK, &gl->gl_flags); |
| 585 | recurse = 0; |
| 586 | } else { |
| 587 | struct gfs2_holder *next_gh; |
| 588 | if (gh->gh_flags & GL_LOCAL_EXCL) |
| 589 | return 1; |
| 590 | next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder, |
| 591 | gh_list); |
| 592 | if (next_gh->gh_flags & GL_LOCAL_EXCL) |
| 593 | return 1; |
| 594 | recurse = test_bit(HIF_RECURSE, &gh->gh_iflags); |
| 595 | } |
| 596 | |
| 597 | list_move_tail(&gh->gh_list, &gl->gl_holders); |
| 598 | gh->gh_error = 0; |
| 599 | set_bit(HIF_HOLDER, &gh->gh_iflags); |
| 600 | |
| 601 | if (recurse) |
| 602 | handle_recurse(gh); |
| 603 | |
| 604 | complete(&gh->gh_wait); |
| 605 | |
| 606 | return 0; |
| 607 | } |
| 608 | |
| 609 | /** |
| 610 | * rq_demote - process a demote request in the queue |
| 611 | * @gh: the glock holder |
| 612 | * |
| 613 | * Returns: 1 if the queue is blocked |
| 614 | */ |
| 615 | |
| 616 | static int rq_demote(struct gfs2_holder *gh) |
| 617 | { |
| 618 | struct gfs2_glock *gl = gh->gh_gl; |
| 619 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 620 | |
| 621 | if (!list_empty(&gl->gl_holders)) |
| 622 | return 1; |
| 623 | |
| 624 | if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) { |
| 625 | list_del_init(&gh->gh_list); |
| 626 | gh->gh_error = 0; |
| 627 | spin_unlock(&gl->gl_spin); |
| 628 | if (test_bit(HIF_DEALLOC, &gh->gh_iflags)) |
| 629 | gfs2_holder_put(gh); |
| 630 | else |
| 631 | complete(&gh->gh_wait); |
| 632 | spin_lock(&gl->gl_spin); |
| 633 | } else { |
| 634 | gl->gl_req_gh = gh; |
| 635 | set_bit(GLF_LOCK, &gl->gl_flags); |
| 636 | spin_unlock(&gl->gl_spin); |
| 637 | |
| 638 | if (gh->gh_state == LM_ST_UNLOCKED || |
| 639 | gl->gl_state != LM_ST_EXCLUSIVE) |
| 640 | glops->go_drop_th(gl); |
| 641 | else |
| 642 | glops->go_xmote_th(gl, gh->gh_state, gh->gh_flags); |
| 643 | |
| 644 | spin_lock(&gl->gl_spin); |
| 645 | } |
| 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | /** |
| 651 | * rq_greedy - process a queued request to drop greedy status |
| 652 | * @gh: the glock holder |
| 653 | * |
| 654 | * Returns: 1 if the queue is blocked |
| 655 | */ |
| 656 | |
| 657 | static int rq_greedy(struct gfs2_holder *gh) |
| 658 | { |
| 659 | struct gfs2_glock *gl = gh->gh_gl; |
| 660 | |
| 661 | list_del_init(&gh->gh_list); |
| 662 | /* gh->gh_error never examined. */ |
| 663 | clear_bit(GLF_GREEDY, &gl->gl_flags); |
| 664 | spin_unlock(&gl->gl_spin); |
| 665 | |
| 666 | gfs2_holder_uninit(gh); |
| 667 | kfree(container_of(gh, struct greedy, gr_gh)); |
| 668 | |
| 669 | spin_lock(&gl->gl_spin); |
| 670 | |
| 671 | return 0; |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * run_queue - process holder structures on a glock |
| 676 | * @gl: the glock |
| 677 | * |
| 678 | */ |
| 679 | |
| 680 | static void run_queue(struct gfs2_glock *gl) |
| 681 | { |
| 682 | struct gfs2_holder *gh; |
| 683 | int blocked = 1; |
| 684 | |
| 685 | for (;;) { |
| 686 | if (test_bit(GLF_LOCK, &gl->gl_flags)) |
| 687 | break; |
| 688 | |
| 689 | if (!list_empty(&gl->gl_waiters1)) { |
| 690 | gh = list_entry(gl->gl_waiters1.next, |
| 691 | struct gfs2_holder, gh_list); |
| 692 | |
| 693 | if (test_bit(HIF_MUTEX, &gh->gh_iflags)) |
| 694 | blocked = rq_mutex(gh); |
| 695 | else |
| 696 | gfs2_assert_warn(gl->gl_sbd, 0); |
| 697 | |
| 698 | } else if (!list_empty(&gl->gl_waiters2) && |
| 699 | !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) { |
| 700 | gh = list_entry(gl->gl_waiters2.next, |
| 701 | struct gfs2_holder, gh_list); |
| 702 | |
| 703 | if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) |
| 704 | blocked = rq_demote(gh); |
| 705 | else if (test_bit(HIF_GREEDY, &gh->gh_iflags)) |
| 706 | blocked = rq_greedy(gh); |
| 707 | else |
| 708 | gfs2_assert_warn(gl->gl_sbd, 0); |
| 709 | |
| 710 | } else if (!list_empty(&gl->gl_waiters3)) { |
| 711 | gh = list_entry(gl->gl_waiters3.next, |
| 712 | struct gfs2_holder, gh_list); |
| 713 | |
| 714 | if (test_bit(HIF_PROMOTE, &gh->gh_iflags)) |
| 715 | blocked = rq_promote(gh); |
| 716 | else |
| 717 | gfs2_assert_warn(gl->gl_sbd, 0); |
| 718 | |
| 719 | } else |
| 720 | break; |
| 721 | |
| 722 | if (blocked) |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * gfs2_glmutex_lock - acquire a local lock on a glock |
| 729 | * @gl: the glock |
| 730 | * |
| 731 | * Gives caller exclusive access to manipulate a glock structure. |
| 732 | */ |
| 733 | |
| 734 | void gfs2_glmutex_lock(struct gfs2_glock *gl) |
| 735 | { |
| 736 | struct gfs2_holder gh; |
| 737 | |
| 738 | gfs2_holder_init(gl, 0, 0, &gh); |
| 739 | set_bit(HIF_MUTEX, &gh.gh_iflags); |
| 740 | |
| 741 | spin_lock(&gl->gl_spin); |
| 742 | if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) |
| 743 | list_add_tail(&gh.gh_list, &gl->gl_waiters1); |
| 744 | else |
| 745 | complete(&gh.gh_wait); |
| 746 | spin_unlock(&gl->gl_spin); |
| 747 | |
| 748 | wait_for_completion(&gh.gh_wait); |
| 749 | gfs2_holder_uninit(&gh); |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * gfs2_glmutex_trylock - try to acquire a local lock on a glock |
| 754 | * @gl: the glock |
| 755 | * |
| 756 | * Returns: 1 if the glock is acquired |
| 757 | */ |
| 758 | |
| 759 | int gfs2_glmutex_trylock(struct gfs2_glock *gl) |
| 760 | { |
| 761 | int acquired = 1; |
| 762 | |
| 763 | spin_lock(&gl->gl_spin); |
| 764 | if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) |
| 765 | acquired = 0; |
| 766 | spin_unlock(&gl->gl_spin); |
| 767 | |
| 768 | return acquired; |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * gfs2_glmutex_unlock - release a local lock on a glock |
| 773 | * @gl: the glock |
| 774 | * |
| 775 | */ |
| 776 | |
| 777 | void gfs2_glmutex_unlock(struct gfs2_glock *gl) |
| 778 | { |
| 779 | spin_lock(&gl->gl_spin); |
| 780 | clear_bit(GLF_LOCK, &gl->gl_flags); |
| 781 | run_queue(gl); |
| 782 | spin_unlock(&gl->gl_spin); |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * handle_callback - add a demote request to a lock's queue |
| 787 | * @gl: the glock |
| 788 | * @state: the state the caller wants us to change to |
| 789 | * |
| 790 | */ |
| 791 | |
| 792 | static void handle_callback(struct gfs2_glock *gl, unsigned int state) |
| 793 | { |
| 794 | struct gfs2_holder *gh, *new_gh = NULL; |
| 795 | |
| 796 | restart: |
| 797 | spin_lock(&gl->gl_spin); |
| 798 | |
| 799 | list_for_each_entry(gh, &gl->gl_waiters2, gh_list) { |
| 800 | if (test_bit(HIF_DEMOTE, &gh->gh_iflags) && |
| 801 | gl->gl_req_gh != gh) { |
| 802 | if (gh->gh_state != state) |
| 803 | gh->gh_state = LM_ST_UNLOCKED; |
| 804 | goto out; |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | if (new_gh) { |
| 809 | list_add_tail(&new_gh->gh_list, &gl->gl_waiters2); |
| 810 | new_gh = NULL; |
| 811 | } else { |
| 812 | spin_unlock(&gl->gl_spin); |
| 813 | |
| 814 | new_gh = gfs2_holder_get(gl, state, |
| 815 | LM_FLAG_TRY | GL_NEVER_RECURSE, |
| 816 | GFP_KERNEL | __GFP_NOFAIL), |
| 817 | set_bit(HIF_DEMOTE, &new_gh->gh_iflags); |
| 818 | set_bit(HIF_DEALLOC, &new_gh->gh_iflags); |
| 819 | |
| 820 | goto restart; |
| 821 | } |
| 822 | |
| 823 | out: |
| 824 | spin_unlock(&gl->gl_spin); |
| 825 | |
| 826 | if (new_gh) |
| 827 | gfs2_holder_put(new_gh); |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * state_change - record that the glock is now in a different state |
| 832 | * @gl: the glock |
| 833 | * @new_state the new state |
| 834 | * |
| 835 | */ |
| 836 | |
| 837 | static void state_change(struct gfs2_glock *gl, unsigned int new_state) |
| 838 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 839 | int held1, held2; |
| 840 | |
| 841 | held1 = (gl->gl_state != LM_ST_UNLOCKED); |
| 842 | held2 = (new_state != LM_ST_UNLOCKED); |
| 843 | |
| 844 | if (held1 != held2) { |
David Teigland | 6a6b3d0 | 2006-02-23 10:11:47 +0000 | [diff] [blame] | 845 | if (held2) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 846 | gfs2_glock_hold(gl); |
David Teigland | 6a6b3d0 | 2006-02-23 10:11:47 +0000 | [diff] [blame] | 847 | else |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 848 | gfs2_glock_put(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | gl->gl_state = new_state; |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * xmote_bh - Called after the lock module is done acquiring a lock |
| 856 | * @gl: The glock in question |
| 857 | * @ret: the int returned from the lock module |
| 858 | * |
| 859 | */ |
| 860 | |
| 861 | static void xmote_bh(struct gfs2_glock *gl, unsigned int ret) |
| 862 | { |
| 863 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 864 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 865 | struct gfs2_holder *gh = gl->gl_req_gh; |
| 866 | int prev_state = gl->gl_state; |
| 867 | int op_done = 1; |
| 868 | |
| 869 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
| 870 | gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders)); |
| 871 | gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC)); |
| 872 | |
| 873 | state_change(gl, ret & LM_OUT_ST_MASK); |
| 874 | |
| 875 | if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) { |
| 876 | if (glops->go_inval) |
| 877 | glops->go_inval(gl, DIO_METADATA | DIO_DATA); |
| 878 | } else if (gl->gl_state == LM_ST_DEFERRED) { |
| 879 | /* We might not want to do this here. |
| 880 | Look at moving to the inode glops. */ |
| 881 | if (glops->go_inval) |
| 882 | glops->go_inval(gl, DIO_DATA); |
| 883 | } |
| 884 | |
| 885 | /* Deal with each possible exit condition */ |
| 886 | |
| 887 | if (!gh) |
| 888 | gl->gl_stamp = jiffies; |
| 889 | |
| 890 | else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) { |
| 891 | spin_lock(&gl->gl_spin); |
| 892 | list_del_init(&gh->gh_list); |
| 893 | gh->gh_error = -EIO; |
| 894 | if (test_bit(HIF_RECURSE, &gh->gh_iflags)) |
| 895 | do_unrecurse(gh); |
| 896 | spin_unlock(&gl->gl_spin); |
| 897 | |
| 898 | } else if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) { |
| 899 | spin_lock(&gl->gl_spin); |
| 900 | list_del_init(&gh->gh_list); |
| 901 | if (gl->gl_state == gh->gh_state || |
| 902 | gl->gl_state == LM_ST_UNLOCKED) |
| 903 | gh->gh_error = 0; |
| 904 | else { |
| 905 | if (gfs2_assert_warn(sdp, gh->gh_flags & |
| 906 | (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) == -1) |
| 907 | fs_warn(sdp, "ret = 0x%.8X\n", ret); |
| 908 | gh->gh_error = GLR_TRYFAILED; |
| 909 | } |
| 910 | spin_unlock(&gl->gl_spin); |
| 911 | |
| 912 | if (ret & LM_OUT_CANCELED) |
| 913 | handle_callback(gl, LM_ST_UNLOCKED); /* Lame */ |
| 914 | |
| 915 | } else if (ret & LM_OUT_CANCELED) { |
| 916 | spin_lock(&gl->gl_spin); |
| 917 | list_del_init(&gh->gh_list); |
| 918 | gh->gh_error = GLR_CANCELED; |
| 919 | if (test_bit(HIF_RECURSE, &gh->gh_iflags)) |
| 920 | do_unrecurse(gh); |
| 921 | spin_unlock(&gl->gl_spin); |
| 922 | |
| 923 | } else if (relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) { |
| 924 | spin_lock(&gl->gl_spin); |
| 925 | list_move_tail(&gh->gh_list, &gl->gl_holders); |
| 926 | gh->gh_error = 0; |
| 927 | set_bit(HIF_HOLDER, &gh->gh_iflags); |
| 928 | spin_unlock(&gl->gl_spin); |
| 929 | |
| 930 | set_bit(HIF_FIRST, &gh->gh_iflags); |
| 931 | |
| 932 | op_done = 0; |
| 933 | |
| 934 | } else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) { |
| 935 | spin_lock(&gl->gl_spin); |
| 936 | list_del_init(&gh->gh_list); |
| 937 | gh->gh_error = GLR_TRYFAILED; |
| 938 | if (test_bit(HIF_RECURSE, &gh->gh_iflags)) |
| 939 | do_unrecurse(gh); |
| 940 | spin_unlock(&gl->gl_spin); |
| 941 | |
| 942 | } else { |
| 943 | if (gfs2_assert_withdraw(sdp, 0) == -1) |
| 944 | fs_err(sdp, "ret = 0x%.8X\n", ret); |
| 945 | } |
| 946 | |
| 947 | if (glops->go_xmote_bh) |
| 948 | glops->go_xmote_bh(gl); |
| 949 | |
| 950 | if (op_done) { |
| 951 | spin_lock(&gl->gl_spin); |
| 952 | gl->gl_req_gh = NULL; |
| 953 | gl->gl_req_bh = NULL; |
| 954 | clear_bit(GLF_LOCK, &gl->gl_flags); |
| 955 | run_queue(gl); |
| 956 | spin_unlock(&gl->gl_spin); |
| 957 | } |
| 958 | |
| 959 | gfs2_glock_put(gl); |
| 960 | |
| 961 | if (gh) { |
| 962 | if (test_bit(HIF_DEALLOC, &gh->gh_iflags)) |
| 963 | gfs2_holder_put(gh); |
| 964 | else |
| 965 | complete(&gh->gh_wait); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock |
| 971 | * @gl: The glock in question |
| 972 | * @state: the requested state |
| 973 | * @flags: modifier flags to the lock call |
| 974 | * |
| 975 | */ |
| 976 | |
| 977 | void gfs2_glock_xmote_th(struct gfs2_glock *gl, unsigned int state, int flags) |
| 978 | { |
| 979 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 980 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 981 | int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB | |
| 982 | LM_FLAG_NOEXP | LM_FLAG_ANY | |
| 983 | LM_FLAG_PRIORITY); |
| 984 | unsigned int lck_ret; |
| 985 | |
| 986 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
| 987 | gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders)); |
| 988 | gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED); |
| 989 | gfs2_assert_warn(sdp, state != gl->gl_state); |
| 990 | |
| 991 | if (gl->gl_state == LM_ST_EXCLUSIVE) { |
| 992 | if (glops->go_sync) |
| 993 | glops->go_sync(gl, |
| 994 | DIO_METADATA | DIO_DATA | DIO_RELEASE); |
| 995 | } |
| 996 | |
| 997 | gfs2_glock_hold(gl); |
| 998 | gl->gl_req_bh = xmote_bh; |
| 999 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1000 | lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, |
| 1001 | lck_flags); |
| 1002 | |
| 1003 | if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR))) |
| 1004 | return; |
| 1005 | |
| 1006 | if (lck_ret & LM_OUT_ASYNC) |
| 1007 | gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC); |
| 1008 | else |
| 1009 | xmote_bh(gl, lck_ret); |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * drop_bh - Called after a lock module unlock completes |
| 1014 | * @gl: the glock |
| 1015 | * @ret: the return status |
| 1016 | * |
| 1017 | * Doesn't wake up the process waiting on the struct gfs2_holder (if any) |
| 1018 | * Doesn't drop the reference on the glock the top half took out |
| 1019 | * |
| 1020 | */ |
| 1021 | |
| 1022 | static void drop_bh(struct gfs2_glock *gl, unsigned int ret) |
| 1023 | { |
| 1024 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1025 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 1026 | struct gfs2_holder *gh = gl->gl_req_gh; |
| 1027 | |
| 1028 | clear_bit(GLF_PREFETCH, &gl->gl_flags); |
| 1029 | |
| 1030 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
| 1031 | gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders)); |
| 1032 | gfs2_assert_warn(sdp, !ret); |
| 1033 | |
| 1034 | state_change(gl, LM_ST_UNLOCKED); |
| 1035 | |
| 1036 | if (glops->go_inval) |
| 1037 | glops->go_inval(gl, DIO_METADATA | DIO_DATA); |
| 1038 | |
| 1039 | if (gh) { |
| 1040 | spin_lock(&gl->gl_spin); |
| 1041 | list_del_init(&gh->gh_list); |
| 1042 | gh->gh_error = 0; |
| 1043 | spin_unlock(&gl->gl_spin); |
| 1044 | } |
| 1045 | |
| 1046 | if (glops->go_drop_bh) |
| 1047 | glops->go_drop_bh(gl); |
| 1048 | |
| 1049 | spin_lock(&gl->gl_spin); |
| 1050 | gl->gl_req_gh = NULL; |
| 1051 | gl->gl_req_bh = NULL; |
| 1052 | clear_bit(GLF_LOCK, &gl->gl_flags); |
| 1053 | run_queue(gl); |
| 1054 | spin_unlock(&gl->gl_spin); |
| 1055 | |
| 1056 | gfs2_glock_put(gl); |
| 1057 | |
| 1058 | if (gh) { |
| 1059 | if (test_bit(HIF_DEALLOC, &gh->gh_iflags)) |
| 1060 | gfs2_holder_put(gh); |
| 1061 | else |
| 1062 | complete(&gh->gh_wait); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | /** |
| 1067 | * gfs2_glock_drop_th - call into the lock module to unlock a lock |
| 1068 | * @gl: the glock |
| 1069 | * |
| 1070 | */ |
| 1071 | |
| 1072 | void gfs2_glock_drop_th(struct gfs2_glock *gl) |
| 1073 | { |
| 1074 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1075 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 1076 | unsigned int ret; |
| 1077 | |
| 1078 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
| 1079 | gfs2_assert_warn(sdp, queue_empty(gl, &gl->gl_holders)); |
| 1080 | gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED); |
| 1081 | |
| 1082 | if (gl->gl_state == LM_ST_EXCLUSIVE) { |
| 1083 | if (glops->go_sync) |
| 1084 | glops->go_sync(gl, |
| 1085 | DIO_METADATA | DIO_DATA | DIO_RELEASE); |
| 1086 | } |
| 1087 | |
| 1088 | gfs2_glock_hold(gl); |
| 1089 | gl->gl_req_bh = drop_bh; |
| 1090 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1091 | ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state); |
| 1092 | |
| 1093 | if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR))) |
| 1094 | return; |
| 1095 | |
| 1096 | if (!ret) |
| 1097 | drop_bh(gl, ret); |
| 1098 | else |
| 1099 | gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC); |
| 1100 | } |
| 1101 | |
| 1102 | /** |
| 1103 | * do_cancels - cancel requests for locks stuck waiting on an expire flag |
| 1104 | * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock |
| 1105 | * |
| 1106 | * Don't cancel GL_NOCANCEL requests. |
| 1107 | */ |
| 1108 | |
| 1109 | static void do_cancels(struct gfs2_holder *gh) |
| 1110 | { |
| 1111 | struct gfs2_glock *gl = gh->gh_gl; |
| 1112 | |
| 1113 | spin_lock(&gl->gl_spin); |
| 1114 | |
| 1115 | while (gl->gl_req_gh != gh && |
| 1116 | !test_bit(HIF_HOLDER, &gh->gh_iflags) && |
| 1117 | !list_empty(&gh->gh_list)) { |
| 1118 | if (gl->gl_req_bh && |
| 1119 | !(gl->gl_req_gh && |
| 1120 | (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) { |
| 1121 | spin_unlock(&gl->gl_spin); |
| 1122 | gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock); |
| 1123 | msleep(100); |
| 1124 | spin_lock(&gl->gl_spin); |
| 1125 | } else { |
| 1126 | spin_unlock(&gl->gl_spin); |
| 1127 | msleep(100); |
| 1128 | spin_lock(&gl->gl_spin); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | spin_unlock(&gl->gl_spin); |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * glock_wait_internal - wait on a glock acquisition |
| 1137 | * @gh: the glock holder |
| 1138 | * |
| 1139 | * Returns: 0 on success |
| 1140 | */ |
| 1141 | |
| 1142 | static int glock_wait_internal(struct gfs2_holder *gh) |
| 1143 | { |
| 1144 | struct gfs2_glock *gl = gh->gh_gl; |
| 1145 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1146 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 1147 | |
| 1148 | if (test_bit(HIF_ABORTED, &gh->gh_iflags)) |
| 1149 | return -EIO; |
| 1150 | |
| 1151 | if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) { |
| 1152 | spin_lock(&gl->gl_spin); |
| 1153 | if (gl->gl_req_gh != gh && |
| 1154 | !test_bit(HIF_HOLDER, &gh->gh_iflags) && |
| 1155 | !list_empty(&gh->gh_list)) { |
| 1156 | list_del_init(&gh->gh_list); |
| 1157 | gh->gh_error = GLR_TRYFAILED; |
| 1158 | if (test_bit(HIF_RECURSE, &gh->gh_iflags)) |
| 1159 | do_unrecurse(gh); |
| 1160 | run_queue(gl); |
| 1161 | spin_unlock(&gl->gl_spin); |
| 1162 | return gh->gh_error; |
| 1163 | } |
| 1164 | spin_unlock(&gl->gl_spin); |
| 1165 | } |
| 1166 | |
| 1167 | if (gh->gh_flags & LM_FLAG_PRIORITY) |
| 1168 | do_cancels(gh); |
| 1169 | |
| 1170 | wait_for_completion(&gh->gh_wait); |
| 1171 | |
| 1172 | if (gh->gh_error) |
| 1173 | return gh->gh_error; |
| 1174 | |
| 1175 | gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags)); |
| 1176 | gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, |
| 1177 | gh->gh_state, |
| 1178 | gh->gh_flags)); |
| 1179 | |
| 1180 | if (test_bit(HIF_FIRST, &gh->gh_iflags)) { |
| 1181 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
| 1182 | |
| 1183 | if (glops->go_lock) { |
| 1184 | gh->gh_error = glops->go_lock(gh); |
| 1185 | if (gh->gh_error) { |
| 1186 | spin_lock(&gl->gl_spin); |
| 1187 | list_del_init(&gh->gh_list); |
| 1188 | if (test_and_clear_bit(HIF_RECURSE, |
| 1189 | &gh->gh_iflags)) |
| 1190 | do_unrecurse(gh); |
| 1191 | spin_unlock(&gl->gl_spin); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | spin_lock(&gl->gl_spin); |
| 1196 | gl->gl_req_gh = NULL; |
| 1197 | gl->gl_req_bh = NULL; |
| 1198 | clear_bit(GLF_LOCK, &gl->gl_flags); |
| 1199 | if (test_bit(HIF_RECURSE, &gh->gh_iflags)) |
| 1200 | handle_recurse(gh); |
| 1201 | run_queue(gl); |
| 1202 | spin_unlock(&gl->gl_spin); |
| 1203 | } |
| 1204 | |
| 1205 | return gh->gh_error; |
| 1206 | } |
| 1207 | |
| 1208 | static inline struct gfs2_holder * |
| 1209 | find_holder_by_owner(struct list_head *head, struct task_struct *owner) |
| 1210 | { |
| 1211 | struct gfs2_holder *gh; |
| 1212 | |
| 1213 | list_for_each_entry(gh, head, gh_list) { |
| 1214 | if (gh->gh_owner == owner) |
| 1215 | return gh; |
| 1216 | } |
| 1217 | |
| 1218 | return NULL; |
| 1219 | } |
| 1220 | |
| 1221 | /** |
| 1222 | * recurse_check - |
| 1223 | * |
| 1224 | * Make sure the new holder is compatible with the pre-existing one. |
| 1225 | * |
| 1226 | */ |
| 1227 | |
| 1228 | static int recurse_check(struct gfs2_holder *existing, struct gfs2_holder *new, |
| 1229 | unsigned int state) |
| 1230 | { |
| 1231 | struct gfs2_sbd *sdp = existing->gh_gl->gl_sbd; |
| 1232 | |
| 1233 | if (gfs2_assert_warn(sdp, (new->gh_flags & LM_FLAG_ANY) || |
| 1234 | !(existing->gh_flags & LM_FLAG_ANY))) |
| 1235 | goto fail; |
| 1236 | |
| 1237 | if (gfs2_assert_warn(sdp, (existing->gh_flags & GL_LOCAL_EXCL) || |
| 1238 | !(new->gh_flags & GL_LOCAL_EXCL))) |
| 1239 | goto fail; |
| 1240 | |
| 1241 | if (gfs2_assert_warn(sdp, relaxed_state_ok(state, new->gh_state, |
| 1242 | new->gh_flags))) |
| 1243 | goto fail; |
| 1244 | |
| 1245 | return 0; |
| 1246 | |
| 1247 | fail: |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 1248 | print_symbol(KERN_WARNING "GFS2: Existing holder from %s\n", |
| 1249 | existing->gh_ip); |
| 1250 | print_symbol(KERN_WARNING "GFS2: New holder from %s\n", new->gh_ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1251 | set_bit(HIF_ABORTED, &new->gh_iflags); |
| 1252 | return -EINVAL; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * add_to_queue - Add a holder to the wait queue (but look for recursion) |
| 1257 | * @gh: the holder structure to add |
| 1258 | * |
| 1259 | */ |
| 1260 | |
| 1261 | static void add_to_queue(struct gfs2_holder *gh) |
| 1262 | { |
| 1263 | struct gfs2_glock *gl = gh->gh_gl; |
| 1264 | struct gfs2_holder *existing; |
| 1265 | |
| 1266 | if (!gh->gh_owner) |
| 1267 | goto out; |
| 1268 | |
| 1269 | existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner); |
| 1270 | if (existing) { |
| 1271 | if (recurse_check(existing, gh, gl->gl_state)) |
| 1272 | return; |
| 1273 | |
| 1274 | list_add_tail(&gh->gh_list, &gl->gl_holders); |
| 1275 | set_bit(HIF_HOLDER, &gh->gh_iflags); |
| 1276 | |
| 1277 | gh->gh_error = 0; |
| 1278 | complete(&gh->gh_wait); |
| 1279 | |
| 1280 | return; |
| 1281 | } |
| 1282 | |
| 1283 | existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner); |
| 1284 | if (existing) { |
| 1285 | if (recurse_check(existing, gh, existing->gh_state)) |
| 1286 | return; |
| 1287 | |
| 1288 | set_bit(HIF_RECURSE, &gh->gh_iflags); |
| 1289 | set_bit(HIF_RECURSE, &existing->gh_iflags); |
| 1290 | |
| 1291 | list_add_tail(&gh->gh_list, &gl->gl_waiters3); |
| 1292 | |
| 1293 | return; |
| 1294 | } |
| 1295 | |
| 1296 | out: |
| 1297 | if (gh->gh_flags & LM_FLAG_PRIORITY) |
| 1298 | list_add(&gh->gh_list, &gl->gl_waiters3); |
| 1299 | else |
| 1300 | list_add_tail(&gh->gh_list, &gl->gl_waiters3); |
| 1301 | } |
| 1302 | |
| 1303 | /** |
| 1304 | * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock) |
| 1305 | * @gh: the holder structure |
| 1306 | * |
| 1307 | * if (gh->gh_flags & GL_ASYNC), this never returns an error |
| 1308 | * |
| 1309 | * Returns: 0, GLR_TRYFAILED, or errno on failure |
| 1310 | */ |
| 1311 | |
| 1312 | int gfs2_glock_nq(struct gfs2_holder *gh) |
| 1313 | { |
| 1314 | struct gfs2_glock *gl = gh->gh_gl; |
| 1315 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1316 | int error = 0; |
| 1317 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1318 | restart: |
| 1319 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) { |
| 1320 | set_bit(HIF_ABORTED, &gh->gh_iflags); |
| 1321 | return -EIO; |
| 1322 | } |
| 1323 | |
| 1324 | set_bit(HIF_PROMOTE, &gh->gh_iflags); |
| 1325 | |
| 1326 | spin_lock(&gl->gl_spin); |
| 1327 | add_to_queue(gh); |
| 1328 | run_queue(gl); |
| 1329 | spin_unlock(&gl->gl_spin); |
| 1330 | |
| 1331 | if (!(gh->gh_flags & GL_ASYNC)) { |
| 1332 | error = glock_wait_internal(gh); |
| 1333 | if (error == GLR_CANCELED) { |
| 1334 | msleep(1000); |
| 1335 | goto restart; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | clear_bit(GLF_PREFETCH, &gl->gl_flags); |
| 1340 | |
| 1341 | return error; |
| 1342 | } |
| 1343 | |
| 1344 | /** |
| 1345 | * gfs2_glock_poll - poll to see if an async request has been completed |
| 1346 | * @gh: the holder |
| 1347 | * |
| 1348 | * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on |
| 1349 | */ |
| 1350 | |
| 1351 | int gfs2_glock_poll(struct gfs2_holder *gh) |
| 1352 | { |
| 1353 | struct gfs2_glock *gl = gh->gh_gl; |
| 1354 | int ready = 0; |
| 1355 | |
| 1356 | spin_lock(&gl->gl_spin); |
| 1357 | |
| 1358 | if (test_bit(HIF_HOLDER, &gh->gh_iflags)) |
| 1359 | ready = 1; |
| 1360 | else if (list_empty(&gh->gh_list)) { |
| 1361 | if (gh->gh_error == GLR_CANCELED) { |
| 1362 | spin_unlock(&gl->gl_spin); |
| 1363 | msleep(1000); |
| 1364 | if (gfs2_glock_nq(gh)) |
| 1365 | return 1; |
| 1366 | return 0; |
| 1367 | } else |
| 1368 | ready = 1; |
| 1369 | } |
| 1370 | |
| 1371 | spin_unlock(&gl->gl_spin); |
| 1372 | |
| 1373 | return ready; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC |
| 1378 | * @gh: the holder structure |
| 1379 | * |
| 1380 | * Returns: 0, GLR_TRYFAILED, or errno on failure |
| 1381 | */ |
| 1382 | |
| 1383 | int gfs2_glock_wait(struct gfs2_holder *gh) |
| 1384 | { |
| 1385 | int error; |
| 1386 | |
| 1387 | error = glock_wait_internal(gh); |
| 1388 | if (error == GLR_CANCELED) { |
| 1389 | msleep(1000); |
| 1390 | gh->gh_flags &= ~GL_ASYNC; |
| 1391 | error = gfs2_glock_nq(gh); |
| 1392 | } |
| 1393 | |
| 1394 | return error; |
| 1395 | } |
| 1396 | |
| 1397 | /** |
| 1398 | * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock) |
| 1399 | * @gh: the glock holder |
| 1400 | * |
| 1401 | */ |
| 1402 | |
| 1403 | void gfs2_glock_dq(struct gfs2_holder *gh) |
| 1404 | { |
| 1405 | struct gfs2_glock *gl = gh->gh_gl; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1406 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 1407 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1408 | if (gh->gh_flags & GL_SYNC) |
| 1409 | set_bit(GLF_SYNC, &gl->gl_flags); |
| 1410 | |
| 1411 | if (gh->gh_flags & GL_NOCACHE) |
| 1412 | handle_callback(gl, LM_ST_UNLOCKED); |
| 1413 | |
| 1414 | gfs2_glmutex_lock(gl); |
| 1415 | |
| 1416 | spin_lock(&gl->gl_spin); |
| 1417 | list_del_init(&gh->gh_list); |
| 1418 | |
| 1419 | if (list_empty(&gl->gl_holders)) { |
| 1420 | spin_unlock(&gl->gl_spin); |
| 1421 | |
| 1422 | if (glops->go_unlock) |
| 1423 | glops->go_unlock(gh); |
| 1424 | |
| 1425 | if (test_bit(GLF_SYNC, &gl->gl_flags)) { |
| 1426 | if (glops->go_sync) |
| 1427 | glops->go_sync(gl, DIO_METADATA | DIO_DATA); |
| 1428 | } |
| 1429 | |
| 1430 | gl->gl_stamp = jiffies; |
| 1431 | |
| 1432 | spin_lock(&gl->gl_spin); |
| 1433 | } |
| 1434 | |
| 1435 | clear_bit(GLF_LOCK, &gl->gl_flags); |
| 1436 | run_queue(gl); |
| 1437 | spin_unlock(&gl->gl_spin); |
| 1438 | } |
| 1439 | |
| 1440 | /** |
| 1441 | * gfs2_glock_prefetch - Try to prefetch a glock |
| 1442 | * @gl: the glock |
| 1443 | * @state: the state to prefetch in |
| 1444 | * @flags: flags passed to go_xmote_th() |
| 1445 | * |
| 1446 | */ |
| 1447 | |
| 1448 | void gfs2_glock_prefetch(struct gfs2_glock *gl, unsigned int state, int flags) |
| 1449 | { |
| 1450 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 1451 | |
| 1452 | spin_lock(&gl->gl_spin); |
| 1453 | |
| 1454 | if (test_bit(GLF_LOCK, &gl->gl_flags) || |
| 1455 | !list_empty(&gl->gl_holders) || |
| 1456 | !list_empty(&gl->gl_waiters1) || |
| 1457 | !list_empty(&gl->gl_waiters2) || |
| 1458 | !list_empty(&gl->gl_waiters3) || |
| 1459 | relaxed_state_ok(gl->gl_state, state, flags)) { |
| 1460 | spin_unlock(&gl->gl_spin); |
| 1461 | return; |
| 1462 | } |
| 1463 | |
| 1464 | set_bit(GLF_PREFETCH, &gl->gl_flags); |
| 1465 | set_bit(GLF_LOCK, &gl->gl_flags); |
| 1466 | spin_unlock(&gl->gl_spin); |
| 1467 | |
| 1468 | glops->go_xmote_th(gl, state, flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /** |
| 1472 | * gfs2_glock_force_drop - Force a glock to be uncached |
| 1473 | * @gl: the glock |
| 1474 | * |
| 1475 | */ |
| 1476 | |
| 1477 | void gfs2_glock_force_drop(struct gfs2_glock *gl) |
| 1478 | { |
| 1479 | struct gfs2_holder gh; |
| 1480 | |
| 1481 | gfs2_holder_init(gl, LM_ST_UNLOCKED, GL_NEVER_RECURSE, &gh); |
| 1482 | set_bit(HIF_DEMOTE, &gh.gh_iflags); |
| 1483 | |
| 1484 | spin_lock(&gl->gl_spin); |
| 1485 | list_add_tail(&gh.gh_list, &gl->gl_waiters2); |
| 1486 | run_queue(gl); |
| 1487 | spin_unlock(&gl->gl_spin); |
| 1488 | |
| 1489 | wait_for_completion(&gh.gh_wait); |
| 1490 | gfs2_holder_uninit(&gh); |
| 1491 | } |
| 1492 | |
| 1493 | static void greedy_work(void *data) |
| 1494 | { |
| 1495 | struct greedy *gr = (struct greedy *)data; |
| 1496 | struct gfs2_holder *gh = &gr->gr_gh; |
| 1497 | struct gfs2_glock *gl = gh->gh_gl; |
| 1498 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 1499 | |
| 1500 | clear_bit(GLF_SKIP_WAITERS2, &gl->gl_flags); |
| 1501 | |
| 1502 | if (glops->go_greedy) |
| 1503 | glops->go_greedy(gl); |
| 1504 | |
| 1505 | spin_lock(&gl->gl_spin); |
| 1506 | |
| 1507 | if (list_empty(&gl->gl_waiters2)) { |
| 1508 | clear_bit(GLF_GREEDY, &gl->gl_flags); |
| 1509 | spin_unlock(&gl->gl_spin); |
| 1510 | gfs2_holder_uninit(gh); |
| 1511 | kfree(gr); |
| 1512 | } else { |
| 1513 | gfs2_glock_hold(gl); |
| 1514 | list_add_tail(&gh->gh_list, &gl->gl_waiters2); |
| 1515 | run_queue(gl); |
| 1516 | spin_unlock(&gl->gl_spin); |
| 1517 | gfs2_glock_put(gl); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | /** |
| 1522 | * gfs2_glock_be_greedy - |
| 1523 | * @gl: |
| 1524 | * @time: |
| 1525 | * |
| 1526 | * Returns: 0 if go_greedy will be called, 1 otherwise |
| 1527 | */ |
| 1528 | |
| 1529 | int gfs2_glock_be_greedy(struct gfs2_glock *gl, unsigned int time) |
| 1530 | { |
| 1531 | struct greedy *gr; |
| 1532 | struct gfs2_holder *gh; |
| 1533 | |
| 1534 | if (!time || |
| 1535 | gl->gl_sbd->sd_args.ar_localcaching || |
| 1536 | test_and_set_bit(GLF_GREEDY, &gl->gl_flags)) |
| 1537 | return 1; |
| 1538 | |
| 1539 | gr = kmalloc(sizeof(struct greedy), GFP_KERNEL); |
| 1540 | if (!gr) { |
| 1541 | clear_bit(GLF_GREEDY, &gl->gl_flags); |
| 1542 | return 1; |
| 1543 | } |
| 1544 | gh = &gr->gr_gh; |
| 1545 | |
| 1546 | gfs2_holder_init(gl, 0, GL_NEVER_RECURSE, gh); |
| 1547 | set_bit(HIF_GREEDY, &gh->gh_iflags); |
| 1548 | INIT_WORK(&gr->gr_work, greedy_work, gr); |
| 1549 | |
| 1550 | set_bit(GLF_SKIP_WAITERS2, &gl->gl_flags); |
| 1551 | schedule_delayed_work(&gr->gr_work, time); |
| 1552 | |
| 1553 | return 0; |
| 1554 | } |
| 1555 | |
| 1556 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1557 | * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it |
| 1558 | * @gh: the holder structure |
| 1559 | * |
| 1560 | */ |
| 1561 | |
| 1562 | void gfs2_glock_dq_uninit(struct gfs2_holder *gh) |
| 1563 | { |
| 1564 | gfs2_glock_dq(gh); |
| 1565 | gfs2_holder_uninit(gh); |
| 1566 | } |
| 1567 | |
| 1568 | /** |
| 1569 | * gfs2_glock_nq_num - acquire a glock based on lock number |
| 1570 | * @sdp: the filesystem |
| 1571 | * @number: the lock number |
| 1572 | * @glops: the glock operations for the type of glock |
| 1573 | * @state: the state to acquire the glock in |
| 1574 | * @flags: modifier flags for the aquisition |
| 1575 | * @gh: the struct gfs2_holder |
| 1576 | * |
| 1577 | * Returns: errno |
| 1578 | */ |
| 1579 | |
| 1580 | int gfs2_glock_nq_num(struct gfs2_sbd *sdp, uint64_t number, |
| 1581 | struct gfs2_glock_operations *glops, unsigned int state, |
| 1582 | int flags, struct gfs2_holder *gh) |
| 1583 | { |
| 1584 | struct gfs2_glock *gl; |
| 1585 | int error; |
| 1586 | |
| 1587 | error = gfs2_glock_get(sdp, number, glops, CREATE, &gl); |
| 1588 | if (!error) { |
| 1589 | error = gfs2_glock_nq_init(gl, state, flags, gh); |
| 1590 | gfs2_glock_put(gl); |
| 1591 | } |
| 1592 | |
| 1593 | return error; |
| 1594 | } |
| 1595 | |
| 1596 | /** |
| 1597 | * glock_compare - Compare two struct gfs2_glock structures for sorting |
| 1598 | * @arg_a: the first structure |
| 1599 | * @arg_b: the second structure |
| 1600 | * |
| 1601 | */ |
| 1602 | |
| 1603 | static int glock_compare(const void *arg_a, const void *arg_b) |
| 1604 | { |
| 1605 | struct gfs2_holder *gh_a = *(struct gfs2_holder **)arg_a; |
| 1606 | struct gfs2_holder *gh_b = *(struct gfs2_holder **)arg_b; |
| 1607 | struct lm_lockname *a = &gh_a->gh_gl->gl_name; |
| 1608 | struct lm_lockname *b = &gh_b->gh_gl->gl_name; |
| 1609 | int ret = 0; |
| 1610 | |
| 1611 | if (a->ln_number > b->ln_number) |
| 1612 | ret = 1; |
| 1613 | else if (a->ln_number < b->ln_number) |
| 1614 | ret = -1; |
| 1615 | else { |
| 1616 | if (gh_a->gh_state == LM_ST_SHARED && |
| 1617 | gh_b->gh_state == LM_ST_EXCLUSIVE) |
| 1618 | ret = 1; |
| 1619 | else if (!(gh_a->gh_flags & GL_LOCAL_EXCL) && |
| 1620 | (gh_b->gh_flags & GL_LOCAL_EXCL)) |
| 1621 | ret = 1; |
| 1622 | } |
| 1623 | |
| 1624 | return ret; |
| 1625 | } |
| 1626 | |
| 1627 | /** |
| 1628 | * nq_m_sync - synchonously acquire more than one glock in deadlock free order |
| 1629 | * @num_gh: the number of structures |
| 1630 | * @ghs: an array of struct gfs2_holder structures |
| 1631 | * |
| 1632 | * Returns: 0 on success (all glocks acquired), |
| 1633 | * errno on failure (no glocks acquired) |
| 1634 | */ |
| 1635 | |
| 1636 | static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs, |
| 1637 | struct gfs2_holder **p) |
| 1638 | { |
| 1639 | unsigned int x; |
| 1640 | int error = 0; |
| 1641 | |
| 1642 | for (x = 0; x < num_gh; x++) |
| 1643 | p[x] = &ghs[x]; |
| 1644 | |
| 1645 | sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL); |
| 1646 | |
| 1647 | for (x = 0; x < num_gh; x++) { |
| 1648 | p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1649 | |
| 1650 | error = gfs2_glock_nq(p[x]); |
| 1651 | if (error) { |
| 1652 | while (x--) |
| 1653 | gfs2_glock_dq(p[x]); |
| 1654 | break; |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | return error; |
| 1659 | } |
| 1660 | |
| 1661 | /** |
| 1662 | * gfs2_glock_nq_m - acquire multiple glocks |
| 1663 | * @num_gh: the number of structures |
| 1664 | * @ghs: an array of struct gfs2_holder structures |
| 1665 | * |
| 1666 | * Figure out how big an impact this function has. Either: |
| 1667 | * 1) Replace this code with code that calls gfs2_glock_prefetch() |
| 1668 | * 2) Forget async stuff and just call nq_m_sync() |
| 1669 | * 3) Leave it like it is |
| 1670 | * |
| 1671 | * Returns: 0 on success (all glocks acquired), |
| 1672 | * errno on failure (no glocks acquired) |
| 1673 | */ |
| 1674 | |
| 1675 | int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1676 | { |
| 1677 | int *e; |
| 1678 | unsigned int x; |
| 1679 | int borked = 0, serious = 0; |
| 1680 | int error = 0; |
| 1681 | |
| 1682 | if (!num_gh) |
| 1683 | return 0; |
| 1684 | |
| 1685 | if (num_gh == 1) { |
| 1686 | ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1687 | return gfs2_glock_nq(ghs); |
| 1688 | } |
| 1689 | |
| 1690 | e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL); |
| 1691 | if (!e) |
| 1692 | return -ENOMEM; |
| 1693 | |
| 1694 | for (x = 0; x < num_gh; x++) { |
| 1695 | ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC; |
| 1696 | error = gfs2_glock_nq(&ghs[x]); |
| 1697 | if (error) { |
| 1698 | borked = 1; |
| 1699 | serious = error; |
| 1700 | num_gh = x; |
| 1701 | break; |
| 1702 | } |
| 1703 | } |
| 1704 | |
| 1705 | for (x = 0; x < num_gh; x++) { |
| 1706 | error = e[x] = glock_wait_internal(&ghs[x]); |
| 1707 | if (error) { |
| 1708 | borked = 1; |
| 1709 | if (error != GLR_TRYFAILED && error != GLR_CANCELED) |
| 1710 | serious = error; |
| 1711 | } |
| 1712 | } |
| 1713 | |
| 1714 | if (!borked) { |
| 1715 | kfree(e); |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |
| 1719 | for (x = 0; x < num_gh; x++) |
| 1720 | if (!e[x]) |
| 1721 | gfs2_glock_dq(&ghs[x]); |
| 1722 | |
| 1723 | if (serious) |
| 1724 | error = serious; |
| 1725 | else { |
| 1726 | for (x = 0; x < num_gh; x++) |
| 1727 | gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags, |
| 1728 | &ghs[x]); |
| 1729 | error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e); |
| 1730 | } |
| 1731 | |
| 1732 | kfree(e); |
| 1733 | |
| 1734 | return error; |
| 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * gfs2_glock_dq_m - release multiple glocks |
| 1739 | * @num_gh: the number of structures |
| 1740 | * @ghs: an array of struct gfs2_holder structures |
| 1741 | * |
| 1742 | */ |
| 1743 | |
| 1744 | void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1745 | { |
| 1746 | unsigned int x; |
| 1747 | |
| 1748 | for (x = 0; x < num_gh; x++) |
| 1749 | gfs2_glock_dq(&ghs[x]); |
| 1750 | } |
| 1751 | |
| 1752 | /** |
| 1753 | * gfs2_glock_dq_uninit_m - release multiple glocks |
| 1754 | * @num_gh: the number of structures |
| 1755 | * @ghs: an array of struct gfs2_holder structures |
| 1756 | * |
| 1757 | */ |
| 1758 | |
| 1759 | void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1760 | { |
| 1761 | unsigned int x; |
| 1762 | |
| 1763 | for (x = 0; x < num_gh; x++) |
| 1764 | gfs2_glock_dq_uninit(&ghs[x]); |
| 1765 | } |
| 1766 | |
| 1767 | /** |
| 1768 | * gfs2_glock_prefetch_num - prefetch a glock based on lock number |
| 1769 | * @sdp: the filesystem |
| 1770 | * @number: the lock number |
| 1771 | * @glops: the glock operations for the type of glock |
| 1772 | * @state: the state to acquire the glock in |
| 1773 | * @flags: modifier flags for the aquisition |
| 1774 | * |
| 1775 | * Returns: errno |
| 1776 | */ |
| 1777 | |
| 1778 | void gfs2_glock_prefetch_num(struct gfs2_sbd *sdp, uint64_t number, |
| 1779 | struct gfs2_glock_operations *glops, |
| 1780 | unsigned int state, int flags) |
| 1781 | { |
| 1782 | struct gfs2_glock *gl; |
| 1783 | int error; |
| 1784 | |
| 1785 | if (atomic_read(&sdp->sd_reclaim_count) < |
| 1786 | gfs2_tune_get(sdp, gt_reclaim_limit)) { |
| 1787 | error = gfs2_glock_get(sdp, number, glops, CREATE, &gl); |
| 1788 | if (!error) { |
| 1789 | gfs2_glock_prefetch(gl, state, flags); |
| 1790 | gfs2_glock_put(gl); |
| 1791 | } |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | /** |
| 1796 | * gfs2_lvb_hold - attach a LVB from a glock |
| 1797 | * @gl: The glock in question |
| 1798 | * |
| 1799 | */ |
| 1800 | |
| 1801 | int gfs2_lvb_hold(struct gfs2_glock *gl) |
| 1802 | { |
| 1803 | int error; |
| 1804 | |
| 1805 | gfs2_glmutex_lock(gl); |
| 1806 | |
| 1807 | if (!atomic_read(&gl->gl_lvb_count)) { |
| 1808 | error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb); |
| 1809 | if (error) { |
| 1810 | gfs2_glmutex_unlock(gl); |
| 1811 | return error; |
| 1812 | } |
| 1813 | gfs2_glock_hold(gl); |
| 1814 | } |
| 1815 | atomic_inc(&gl->gl_lvb_count); |
| 1816 | |
| 1817 | gfs2_glmutex_unlock(gl); |
| 1818 | |
| 1819 | return 0; |
| 1820 | } |
| 1821 | |
| 1822 | /** |
| 1823 | * gfs2_lvb_unhold - detach a LVB from a glock |
| 1824 | * @gl: The glock in question |
| 1825 | * |
| 1826 | */ |
| 1827 | |
| 1828 | void gfs2_lvb_unhold(struct gfs2_glock *gl) |
| 1829 | { |
| 1830 | gfs2_glock_hold(gl); |
| 1831 | gfs2_glmutex_lock(gl); |
| 1832 | |
| 1833 | gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0); |
| 1834 | if (atomic_dec_and_test(&gl->gl_lvb_count)) { |
| 1835 | gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb); |
| 1836 | gl->gl_lvb = NULL; |
| 1837 | gfs2_glock_put(gl); |
| 1838 | } |
| 1839 | |
| 1840 | gfs2_glmutex_unlock(gl); |
| 1841 | gfs2_glock_put(gl); |
| 1842 | } |
| 1843 | |
| 1844 | void gfs2_lvb_sync(struct gfs2_glock *gl) |
| 1845 | { |
| 1846 | gfs2_glmutex_lock(gl); |
| 1847 | |
| 1848 | gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count)); |
| 1849 | if (!gfs2_assert_warn(gl->gl_sbd, gfs2_glock_is_held_excl(gl))) |
| 1850 | gfs2_lm_sync_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb); |
| 1851 | |
| 1852 | gfs2_glmutex_unlock(gl); |
| 1853 | } |
| 1854 | |
| 1855 | static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name, |
| 1856 | unsigned int state) |
| 1857 | { |
| 1858 | struct gfs2_glock *gl; |
| 1859 | |
| 1860 | gl = gfs2_glock_find(sdp, name); |
| 1861 | if (!gl) |
| 1862 | return; |
| 1863 | |
| 1864 | if (gl->gl_ops->go_callback) |
| 1865 | gl->gl_ops->go_callback(gl, state); |
| 1866 | handle_callback(gl, state); |
| 1867 | |
| 1868 | spin_lock(&gl->gl_spin); |
| 1869 | run_queue(gl); |
| 1870 | spin_unlock(&gl->gl_spin); |
| 1871 | |
| 1872 | gfs2_glock_put(gl); |
| 1873 | } |
| 1874 | |
| 1875 | /** |
| 1876 | * gfs2_glock_cb - Callback used by locking module |
| 1877 | * @fsdata: Pointer to the superblock |
| 1878 | * @type: Type of callback |
| 1879 | * @data: Type dependent data pointer |
| 1880 | * |
| 1881 | * Called by the locking module when it wants to tell us something. |
| 1882 | * Either we need to drop a lock, one of our ASYNC requests completed, or |
| 1883 | * a journal from another client needs to be recovered. |
| 1884 | */ |
| 1885 | |
| 1886 | void gfs2_glock_cb(lm_fsdata_t *fsdata, unsigned int type, void *data) |
| 1887 | { |
| 1888 | struct gfs2_sbd *sdp = (struct gfs2_sbd *)fsdata; |
| 1889 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1890 | switch (type) { |
| 1891 | case LM_CB_NEED_E: |
| 1892 | blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_UNLOCKED); |
| 1893 | return; |
| 1894 | |
| 1895 | case LM_CB_NEED_D: |
| 1896 | blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_DEFERRED); |
| 1897 | return; |
| 1898 | |
| 1899 | case LM_CB_NEED_S: |
| 1900 | blocking_cb(sdp, (struct lm_lockname *)data, LM_ST_SHARED); |
| 1901 | return; |
| 1902 | |
| 1903 | case LM_CB_ASYNC: { |
| 1904 | struct lm_async_cb *async = (struct lm_async_cb *)data; |
| 1905 | struct gfs2_glock *gl; |
| 1906 | |
| 1907 | gl = gfs2_glock_find(sdp, &async->lc_name); |
| 1908 | if (gfs2_assert_warn(sdp, gl)) |
| 1909 | return; |
| 1910 | if (!gfs2_assert_warn(sdp, gl->gl_req_bh)) |
| 1911 | gl->gl_req_bh(gl, async->lc_ret); |
| 1912 | gfs2_glock_put(gl); |
| 1913 | |
| 1914 | return; |
| 1915 | } |
| 1916 | |
| 1917 | case LM_CB_NEED_RECOVERY: |
| 1918 | gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data); |
| 1919 | if (sdp->sd_recoverd_process) |
| 1920 | wake_up_process(sdp->sd_recoverd_process); |
| 1921 | return; |
| 1922 | |
| 1923 | case LM_CB_DROPLOCKS: |
| 1924 | gfs2_gl_hash_clear(sdp, NO_WAIT); |
| 1925 | gfs2_quota_scan(sdp); |
| 1926 | return; |
| 1927 | |
| 1928 | default: |
| 1929 | gfs2_assert_warn(sdp, 0); |
| 1930 | return; |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | /** |
| 1935 | * gfs2_try_toss_inode - try to remove a particular inode struct from cache |
| 1936 | * sdp: the filesystem |
| 1937 | * inum: the inode number |
| 1938 | * |
| 1939 | */ |
| 1940 | |
| 1941 | void gfs2_try_toss_inode(struct gfs2_sbd *sdp, struct gfs2_inum *inum) |
| 1942 | { |
| 1943 | struct gfs2_glock *gl; |
| 1944 | struct gfs2_inode *ip; |
| 1945 | int error; |
| 1946 | |
| 1947 | error = gfs2_glock_get(sdp, inum->no_addr, &gfs2_inode_glops, |
| 1948 | NO_CREATE, &gl); |
| 1949 | if (error || !gl) |
| 1950 | return; |
| 1951 | |
| 1952 | if (!gfs2_glmutex_trylock(gl)) |
| 1953 | goto out; |
| 1954 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1955 | ip = gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1956 | if (!ip) |
| 1957 | goto out_unlock; |
| 1958 | |
| 1959 | if (atomic_read(&ip->i_count)) |
| 1960 | goto out_unlock; |
| 1961 | |
| 1962 | gfs2_inode_destroy(ip); |
| 1963 | |
| 1964 | out_unlock: |
| 1965 | gfs2_glmutex_unlock(gl); |
| 1966 | |
| 1967 | out: |
| 1968 | gfs2_glock_put(gl); |
| 1969 | } |
| 1970 | |
| 1971 | /** |
| 1972 | * gfs2_iopen_go_callback - Try to kick the inode/vnode associated with an |
| 1973 | * iopen glock from memory |
| 1974 | * @io_gl: the iopen glock |
| 1975 | * @state: the state into which the glock should be put |
| 1976 | * |
| 1977 | */ |
| 1978 | |
| 1979 | void gfs2_iopen_go_callback(struct gfs2_glock *io_gl, unsigned int state) |
| 1980 | { |
| 1981 | struct gfs2_glock *i_gl; |
| 1982 | |
| 1983 | if (state != LM_ST_UNLOCKED) |
| 1984 | return; |
| 1985 | |
| 1986 | spin_lock(&io_gl->gl_spin); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1987 | i_gl = io_gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1988 | if (i_gl) { |
| 1989 | gfs2_glock_hold(i_gl); |
| 1990 | spin_unlock(&io_gl->gl_spin); |
| 1991 | } else { |
| 1992 | spin_unlock(&io_gl->gl_spin); |
| 1993 | return; |
| 1994 | } |
| 1995 | |
| 1996 | if (gfs2_glmutex_trylock(i_gl)) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1997 | struct gfs2_inode *ip = i_gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1998 | if (ip) { |
| 1999 | gfs2_try_toss_vnode(ip); |
| 2000 | gfs2_glmutex_unlock(i_gl); |
| 2001 | gfs2_glock_schedule_for_reclaim(i_gl); |
| 2002 | goto out; |
| 2003 | } |
| 2004 | gfs2_glmutex_unlock(i_gl); |
| 2005 | } |
| 2006 | |
| 2007 | out: |
| 2008 | gfs2_glock_put(i_gl); |
| 2009 | } |
| 2010 | |
| 2011 | /** |
| 2012 | * demote_ok - Check to see if it's ok to unlock a glock |
| 2013 | * @gl: the glock |
| 2014 | * |
| 2015 | * Returns: 1 if it's ok |
| 2016 | */ |
| 2017 | |
| 2018 | static int demote_ok(struct gfs2_glock *gl) |
| 2019 | { |
| 2020 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 2021 | struct gfs2_glock_operations *glops = gl->gl_ops; |
| 2022 | int demote = 1; |
| 2023 | |
| 2024 | if (test_bit(GLF_STICKY, &gl->gl_flags)) |
| 2025 | demote = 0; |
| 2026 | else if (test_bit(GLF_PREFETCH, &gl->gl_flags)) |
| 2027 | demote = time_after_eq(jiffies, |
| 2028 | gl->gl_stamp + |
| 2029 | gfs2_tune_get(sdp, gt_prefetch_secs) * HZ); |
| 2030 | else if (glops->go_demote_ok) |
| 2031 | demote = glops->go_demote_ok(gl); |
| 2032 | |
| 2033 | return demote; |
| 2034 | } |
| 2035 | |
| 2036 | /** |
| 2037 | * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list |
| 2038 | * @gl: the glock |
| 2039 | * |
| 2040 | */ |
| 2041 | |
| 2042 | void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl) |
| 2043 | { |
| 2044 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 2045 | |
| 2046 | spin_lock(&sdp->sd_reclaim_lock); |
| 2047 | if (list_empty(&gl->gl_reclaim)) { |
| 2048 | gfs2_glock_hold(gl); |
| 2049 | list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list); |
| 2050 | atomic_inc(&sdp->sd_reclaim_count); |
| 2051 | } |
| 2052 | spin_unlock(&sdp->sd_reclaim_lock); |
| 2053 | |
| 2054 | wake_up(&sdp->sd_reclaim_wq); |
| 2055 | } |
| 2056 | |
| 2057 | /** |
| 2058 | * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list |
| 2059 | * @sdp: the filesystem |
| 2060 | * |
| 2061 | * Called from gfs2_glockd() glock reclaim daemon, or when promoting a |
| 2062 | * different glock and we notice that there are a lot of glocks in the |
| 2063 | * reclaim list. |
| 2064 | * |
| 2065 | */ |
| 2066 | |
| 2067 | void gfs2_reclaim_glock(struct gfs2_sbd *sdp) |
| 2068 | { |
| 2069 | struct gfs2_glock *gl; |
| 2070 | |
| 2071 | spin_lock(&sdp->sd_reclaim_lock); |
| 2072 | if (list_empty(&sdp->sd_reclaim_list)) { |
| 2073 | spin_unlock(&sdp->sd_reclaim_lock); |
| 2074 | return; |
| 2075 | } |
| 2076 | gl = list_entry(sdp->sd_reclaim_list.next, |
| 2077 | struct gfs2_glock, gl_reclaim); |
| 2078 | list_del_init(&gl->gl_reclaim); |
| 2079 | spin_unlock(&sdp->sd_reclaim_lock); |
| 2080 | |
| 2081 | atomic_dec(&sdp->sd_reclaim_count); |
| 2082 | atomic_inc(&sdp->sd_reclaimed); |
| 2083 | |
| 2084 | if (gfs2_glmutex_trylock(gl)) { |
| 2085 | if (gl->gl_ops == &gfs2_inode_glops) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 2086 | struct gfs2_inode *ip = gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2087 | if (ip && !atomic_read(&ip->i_count)) |
| 2088 | gfs2_inode_destroy(ip); |
| 2089 | } |
| 2090 | if (queue_empty(gl, &gl->gl_holders) && |
| 2091 | gl->gl_state != LM_ST_UNLOCKED && |
| 2092 | demote_ok(gl)) |
| 2093 | handle_callback(gl, LM_ST_UNLOCKED); |
| 2094 | gfs2_glmutex_unlock(gl); |
| 2095 | } |
| 2096 | |
| 2097 | gfs2_glock_put(gl); |
| 2098 | } |
| 2099 | |
| 2100 | /** |
| 2101 | * examine_bucket - Call a function for glock in a hash bucket |
| 2102 | * @examiner: the function |
| 2103 | * @sdp: the filesystem |
| 2104 | * @bucket: the bucket |
| 2105 | * |
| 2106 | * Returns: 1 if the bucket has entries |
| 2107 | */ |
| 2108 | |
| 2109 | static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp, |
| 2110 | struct gfs2_gl_hash_bucket *bucket) |
| 2111 | { |
| 2112 | struct glock_plug plug; |
| 2113 | struct list_head *tmp; |
| 2114 | struct gfs2_glock *gl; |
| 2115 | int entries; |
| 2116 | |
| 2117 | /* Add "plug" to end of bucket list, work back up list from there */ |
| 2118 | memset(&plug.gl_flags, 0, sizeof(unsigned long)); |
| 2119 | set_bit(GLF_PLUG, &plug.gl_flags); |
| 2120 | |
| 2121 | write_lock(&bucket->hb_lock); |
| 2122 | list_add(&plug.gl_list, &bucket->hb_list); |
| 2123 | write_unlock(&bucket->hb_lock); |
| 2124 | |
| 2125 | for (;;) { |
| 2126 | write_lock(&bucket->hb_lock); |
| 2127 | |
| 2128 | for (;;) { |
| 2129 | tmp = plug.gl_list.next; |
| 2130 | |
| 2131 | if (tmp == &bucket->hb_list) { |
| 2132 | list_del(&plug.gl_list); |
| 2133 | entries = !list_empty(&bucket->hb_list); |
| 2134 | write_unlock(&bucket->hb_lock); |
| 2135 | return entries; |
| 2136 | } |
| 2137 | gl = list_entry(tmp, struct gfs2_glock, gl_list); |
| 2138 | |
| 2139 | /* Move plug up list */ |
| 2140 | list_move(&plug.gl_list, &gl->gl_list); |
| 2141 | |
| 2142 | if (test_bit(GLF_PLUG, &gl->gl_flags)) |
| 2143 | continue; |
| 2144 | |
| 2145 | /* examiner() must glock_put() */ |
| 2146 | gfs2_glock_hold(gl); |
| 2147 | |
| 2148 | break; |
| 2149 | } |
| 2150 | |
| 2151 | write_unlock(&bucket->hb_lock); |
| 2152 | |
| 2153 | examiner(gl); |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | /** |
| 2158 | * scan_glock - look at a glock and see if we can reclaim it |
| 2159 | * @gl: the glock to look at |
| 2160 | * |
| 2161 | */ |
| 2162 | |
| 2163 | static void scan_glock(struct gfs2_glock *gl) |
| 2164 | { |
| 2165 | if (gfs2_glmutex_trylock(gl)) { |
| 2166 | if (gl->gl_ops == &gfs2_inode_glops) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 2167 | struct gfs2_inode *ip = gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2168 | if (ip && !atomic_read(&ip->i_count)) |
| 2169 | goto out_schedule; |
| 2170 | } |
| 2171 | if (queue_empty(gl, &gl->gl_holders) && |
| 2172 | gl->gl_state != LM_ST_UNLOCKED && |
| 2173 | demote_ok(gl)) |
| 2174 | goto out_schedule; |
| 2175 | |
| 2176 | gfs2_glmutex_unlock(gl); |
| 2177 | } |
| 2178 | |
| 2179 | gfs2_glock_put(gl); |
| 2180 | |
| 2181 | return; |
| 2182 | |
| 2183 | out_schedule: |
| 2184 | gfs2_glmutex_unlock(gl); |
| 2185 | gfs2_glock_schedule_for_reclaim(gl); |
| 2186 | gfs2_glock_put(gl); |
| 2187 | } |
| 2188 | |
| 2189 | /** |
| 2190 | * gfs2_scand_internal - Look for glocks and inodes to toss from memory |
| 2191 | * @sdp: the filesystem |
| 2192 | * |
| 2193 | */ |
| 2194 | |
| 2195 | void gfs2_scand_internal(struct gfs2_sbd *sdp) |
| 2196 | { |
| 2197 | unsigned int x; |
| 2198 | |
| 2199 | for (x = 0; x < GFS2_GL_HASH_SIZE; x++) { |
| 2200 | examine_bucket(scan_glock, sdp, &sdp->sd_gl_hash[x]); |
| 2201 | cond_resched(); |
| 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | /** |
| 2206 | * clear_glock - look at a glock and see if we can free it from glock cache |
| 2207 | * @gl: the glock to look at |
| 2208 | * |
| 2209 | */ |
| 2210 | |
| 2211 | static void clear_glock(struct gfs2_glock *gl) |
| 2212 | { |
| 2213 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 2214 | int released; |
| 2215 | |
| 2216 | spin_lock(&sdp->sd_reclaim_lock); |
| 2217 | if (!list_empty(&gl->gl_reclaim)) { |
| 2218 | list_del_init(&gl->gl_reclaim); |
| 2219 | atomic_dec(&sdp->sd_reclaim_count); |
| 2220 | released = gfs2_glock_put(gl); |
| 2221 | gfs2_assert(sdp, !released); |
| 2222 | } |
| 2223 | spin_unlock(&sdp->sd_reclaim_lock); |
| 2224 | |
| 2225 | if (gfs2_glmutex_trylock(gl)) { |
| 2226 | if (gl->gl_ops == &gfs2_inode_glops) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 2227 | struct gfs2_inode *ip = gl->gl_object; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2228 | if (ip && !atomic_read(&ip->i_count)) |
| 2229 | gfs2_inode_destroy(ip); |
| 2230 | } |
| 2231 | if (queue_empty(gl, &gl->gl_holders) && |
| 2232 | gl->gl_state != LM_ST_UNLOCKED) |
| 2233 | handle_callback(gl, LM_ST_UNLOCKED); |
| 2234 | |
| 2235 | gfs2_glmutex_unlock(gl); |
| 2236 | } |
| 2237 | |
| 2238 | gfs2_glock_put(gl); |
| 2239 | } |
| 2240 | |
| 2241 | /** |
| 2242 | * gfs2_gl_hash_clear - Empty out the glock hash table |
| 2243 | * @sdp: the filesystem |
| 2244 | * @wait: wait until it's all gone |
| 2245 | * |
| 2246 | * Called when unmounting the filesystem, or when inter-node lock manager |
| 2247 | * requests DROPLOCKS because it is running out of capacity. |
| 2248 | */ |
| 2249 | |
| 2250 | void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait) |
| 2251 | { |
| 2252 | unsigned long t; |
| 2253 | unsigned int x; |
| 2254 | int cont; |
| 2255 | |
| 2256 | t = jiffies; |
| 2257 | |
| 2258 | for (;;) { |
| 2259 | cont = 0; |
| 2260 | |
| 2261 | for (x = 0; x < GFS2_GL_HASH_SIZE; x++) |
| 2262 | if (examine_bucket(clear_glock, sdp, |
| 2263 | &sdp->sd_gl_hash[x])) |
| 2264 | cont = 1; |
| 2265 | |
| 2266 | if (!wait || !cont) |
| 2267 | break; |
| 2268 | |
| 2269 | if (time_after_eq(jiffies, |
| 2270 | t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) { |
| 2271 | fs_warn(sdp, "Unmount seems to be stalled. " |
| 2272 | "Dumping lock state...\n"); |
| 2273 | gfs2_dump_lockstate(sdp); |
| 2274 | t = jiffies; |
| 2275 | } |
| 2276 | |
| 2277 | /* invalidate_inodes() requires that the sb inodes list |
| 2278 | not change, but an async completion callback for an |
| 2279 | unlock can occur which does glock_put() which |
| 2280 | can call iput() which will change the sb inodes list. |
| 2281 | invalidate_inodes_mutex prevents glock_put()'s during |
| 2282 | an invalidate_inodes() */ |
| 2283 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 2284 | mutex_lock(&sdp->sd_invalidate_inodes_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2285 | invalidate_inodes(sdp->sd_vfs); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 2286 | mutex_unlock(&sdp->sd_invalidate_inodes_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2287 | yield(); |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | /* |
| 2292 | * Diagnostic routines to help debug distributed deadlock |
| 2293 | */ |
| 2294 | |
| 2295 | /** |
| 2296 | * dump_holder - print information about a glock holder |
| 2297 | * @str: a string naming the type of holder |
| 2298 | * @gh: the glock holder |
| 2299 | * |
| 2300 | * Returns: 0 on success, -ENOBUFS when we run out of space |
| 2301 | */ |
| 2302 | |
| 2303 | static int dump_holder(char *str, struct gfs2_holder *gh) |
| 2304 | { |
| 2305 | unsigned int x; |
| 2306 | int error = -ENOBUFS; |
| 2307 | |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2308 | printk(KERN_INFO " %s\n", str); |
| 2309 | printk(KERN_INFO " owner = %ld\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2310 | (gh->gh_owner) ? (long)gh->gh_owner->pid : -1); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2311 | printk(KERN_INFO " gh_state = %u\n", gh->gh_state); |
| 2312 | printk(KERN_INFO " gh_flags ="); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2313 | for (x = 0; x < 32; x++) |
| 2314 | if (gh->gh_flags & (1 << x)) |
| 2315 | printk(" %u", x); |
| 2316 | printk(" \n"); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2317 | printk(KERN_INFO " error = %d\n", gh->gh_error); |
| 2318 | printk(KERN_INFO " gh_iflags ="); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2319 | for (x = 0; x < 32; x++) |
| 2320 | if (test_bit(x, &gh->gh_iflags)) |
| 2321 | printk(" %u", x); |
| 2322 | printk(" \n"); |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 2323 | print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2324 | |
| 2325 | error = 0; |
| 2326 | |
| 2327 | return error; |
| 2328 | } |
| 2329 | |
| 2330 | /** |
| 2331 | * dump_inode - print information about an inode |
| 2332 | * @ip: the inode |
| 2333 | * |
| 2334 | * Returns: 0 on success, -ENOBUFS when we run out of space |
| 2335 | */ |
| 2336 | |
| 2337 | static int dump_inode(struct gfs2_inode *ip) |
| 2338 | { |
| 2339 | unsigned int x; |
| 2340 | int error = -ENOBUFS; |
| 2341 | |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2342 | printk(KERN_INFO " Inode:\n"); |
| 2343 | printk(KERN_INFO " num = %llu %llu\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2344 | ip->i_num.no_formal_ino, ip->i_num.no_addr); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2345 | printk(KERN_INFO " type = %u\n", IF2DT(ip->i_di.di_mode)); |
| 2346 | printk(KERN_INFO " i_count = %d\n", atomic_read(&ip->i_count)); |
| 2347 | printk(KERN_INFO " i_flags ="); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2348 | for (x = 0; x < 32; x++) |
| 2349 | if (test_bit(x, &ip->i_flags)) |
| 2350 | printk(" %u", x); |
| 2351 | printk(" \n"); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2352 | printk(KERN_INFO " vnode = %s\n", (ip->i_vnode) ? "yes" : "no"); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2353 | |
| 2354 | error = 0; |
| 2355 | |
| 2356 | return error; |
| 2357 | } |
| 2358 | |
| 2359 | /** |
| 2360 | * dump_glock - print information about a glock |
| 2361 | * @gl: the glock |
| 2362 | * @count: where we are in the buffer |
| 2363 | * |
| 2364 | * Returns: 0 on success, -ENOBUFS when we run out of space |
| 2365 | */ |
| 2366 | |
| 2367 | static int dump_glock(struct gfs2_glock *gl) |
| 2368 | { |
| 2369 | struct gfs2_holder *gh; |
| 2370 | unsigned int x; |
| 2371 | int error = -ENOBUFS; |
| 2372 | |
| 2373 | spin_lock(&gl->gl_spin); |
| 2374 | |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2375 | printk(KERN_INFO "Glock (%u, %llu)\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2376 | gl->gl_name.ln_type, |
| 2377 | gl->gl_name.ln_number); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2378 | printk(KERN_INFO " gl_flags ="); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2379 | for (x = 0; x < 32; x++) |
| 2380 | if (test_bit(x, &gl->gl_flags)) |
| 2381 | printk(" %u", x); |
| 2382 | printk(" \n"); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2383 | printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref.refcount)); |
| 2384 | printk(KERN_INFO " gl_state = %u\n", gl->gl_state); |
| 2385 | printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no"); |
| 2386 | printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no"); |
| 2387 | printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count)); |
| 2388 | printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no"); |
| 2389 | printk(KERN_INFO " le = %s\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2390 | (list_empty(&gl->gl_le.le_list)) ? "no" : "yes"); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2391 | printk(KERN_INFO " reclaim = %s\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2392 | (list_empty(&gl->gl_reclaim)) ? "no" : "yes"); |
| 2393 | if (gl->gl_aspace) |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2394 | printk(KERN_INFO " aspace = %lu\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2395 | gl->gl_aspace->i_mapping->nrpages); |
| 2396 | else |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2397 | printk(KERN_INFO " aspace = no\n"); |
| 2398 | printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2399 | if (gl->gl_req_gh) { |
| 2400 | error = dump_holder("Request", gl->gl_req_gh); |
| 2401 | if (error) |
| 2402 | goto out; |
| 2403 | } |
| 2404 | list_for_each_entry(gh, &gl->gl_holders, gh_list) { |
| 2405 | error = dump_holder("Holder", gh); |
| 2406 | if (error) |
| 2407 | goto out; |
| 2408 | } |
| 2409 | list_for_each_entry(gh, &gl->gl_waiters1, gh_list) { |
| 2410 | error = dump_holder("Waiter1", gh); |
| 2411 | if (error) |
| 2412 | goto out; |
| 2413 | } |
| 2414 | list_for_each_entry(gh, &gl->gl_waiters2, gh_list) { |
| 2415 | error = dump_holder("Waiter2", gh); |
| 2416 | if (error) |
| 2417 | goto out; |
| 2418 | } |
| 2419 | list_for_each_entry(gh, &gl->gl_waiters3, gh_list) { |
| 2420 | error = dump_holder("Waiter3", gh); |
| 2421 | if (error) |
| 2422 | goto out; |
| 2423 | } |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 2424 | if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2425 | if (!test_bit(GLF_LOCK, &gl->gl_flags) && |
| 2426 | list_empty(&gl->gl_holders)) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 2427 | error = dump_inode(gl->gl_object); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2428 | if (error) |
| 2429 | goto out; |
| 2430 | } else { |
| 2431 | error = -ENOBUFS; |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 2432 | printk(KERN_INFO " Inode: busy\n"); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | error = 0; |
| 2437 | |
| 2438 | out: |
| 2439 | spin_unlock(&gl->gl_spin); |
| 2440 | |
| 2441 | return error; |
| 2442 | } |
| 2443 | |
| 2444 | /** |
| 2445 | * gfs2_dump_lockstate - print out the current lockstate |
| 2446 | * @sdp: the filesystem |
| 2447 | * @ub: the buffer to copy the information into |
| 2448 | * |
| 2449 | * If @ub is NULL, dump the lockstate to the console. |
| 2450 | * |
| 2451 | */ |
| 2452 | |
| 2453 | int gfs2_dump_lockstate(struct gfs2_sbd *sdp) |
| 2454 | { |
| 2455 | struct gfs2_gl_hash_bucket *bucket; |
| 2456 | struct gfs2_glock *gl; |
| 2457 | unsigned int x; |
| 2458 | int error = 0; |
| 2459 | |
| 2460 | for (x = 0; x < GFS2_GL_HASH_SIZE; x++) { |
| 2461 | bucket = &sdp->sd_gl_hash[x]; |
| 2462 | |
| 2463 | read_lock(&bucket->hb_lock); |
| 2464 | |
| 2465 | list_for_each_entry(gl, &bucket->hb_list, gl_list) { |
| 2466 | if (test_bit(GLF_PLUG, &gl->gl_flags)) |
| 2467 | continue; |
| 2468 | |
| 2469 | error = dump_glock(gl); |
| 2470 | if (error) |
| 2471 | break; |
| 2472 | } |
| 2473 | |
| 2474 | read_unlock(&bucket->hb_lock); |
| 2475 | |
| 2476 | if (error) |
| 2477 | break; |
| 2478 | } |
| 2479 | |
| 2480 | |
| 2481 | return error; |
| 2482 | } |
| 2483 | |