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