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