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 |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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> |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 18 | #include <linux/kallsyms.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 20 | #include <linux/list.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 21 | #include <linux/lm_interface.h> |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 22 | #include <linux/wait.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 23 | #include <asm/uaccess.h> |
| 24 | |
| 25 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 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 | |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 37 | struct gfs2_gl_hash_bucket { |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 38 | struct hlist_head hb_list; |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 39 | }; |
| 40 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 41 | typedef void (*glock_examiner) (struct gfs2_glock * gl); |
| 42 | |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 43 | static int gfs2_dump_lockstate(struct gfs2_sbd *sdp); |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 44 | static int dump_glock(struct gfs2_glock *gl); |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 45 | static int dump_inode(struct gfs2_inode *ip); |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 46 | static void gfs2_glock_xmote_th(struct gfs2_holder *gh); |
| 47 | static void gfs2_glock_drop_th(struct gfs2_glock *gl); |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 48 | |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 49 | #define GFS2_GL_HASH_SHIFT 15 |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 50 | #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT) |
| 51 | #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1) |
| 52 | |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 53 | static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE]; |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * Despite what you might think, the numbers below are not arbitrary :-) |
| 57 | * They are taken from the ipv4 routing hash code, which is well tested |
| 58 | * and thus should be nearly optimal. Later on we might tweek the numbers |
| 59 | * but for now this should be fine. |
| 60 | * |
| 61 | * The reason for putting the locks in a separate array from the list heads |
| 62 | * is that we can have fewer locks than list heads and save memory. We use |
| 63 | * the same hash function for both, but with a different hash mask. |
| 64 | */ |
| 65 | #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \ |
| 66 | defined(CONFIG_PROVE_LOCKING) |
| 67 | |
| 68 | #ifdef CONFIG_LOCKDEP |
| 69 | # define GL_HASH_LOCK_SZ 256 |
| 70 | #else |
| 71 | # if NR_CPUS >= 32 |
| 72 | # define GL_HASH_LOCK_SZ 4096 |
| 73 | # elif NR_CPUS >= 16 |
| 74 | # define GL_HASH_LOCK_SZ 2048 |
| 75 | # elif NR_CPUS >= 8 |
| 76 | # define GL_HASH_LOCK_SZ 1024 |
| 77 | # elif NR_CPUS >= 4 |
| 78 | # define GL_HASH_LOCK_SZ 512 |
| 79 | # else |
| 80 | # define GL_HASH_LOCK_SZ 256 |
| 81 | # endif |
| 82 | #endif |
| 83 | |
| 84 | /* We never want more locks than chains */ |
| 85 | #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ |
| 86 | # undef GL_HASH_LOCK_SZ |
| 87 | # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE |
| 88 | #endif |
| 89 | |
| 90 | static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ]; |
| 91 | |
| 92 | static inline rwlock_t *gl_lock_addr(unsigned int x) |
| 93 | { |
Steven Whitehouse | 9461061 | 2006-09-09 18:59:27 -0400 | [diff] [blame] | 94 | return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)]; |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 95 | } |
| 96 | #else /* not SMP, so no spinlocks required */ |
Randy Dunlap | 0ac2306 | 2006-11-28 22:29:19 -0800 | [diff] [blame] | 97 | static inline rwlock_t *gl_lock_addr(unsigned int x) |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 98 | { |
| 99 | return NULL; |
| 100 | } |
| 101 | #endif |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 102 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 103 | /** |
| 104 | * relaxed_state_ok - is a requested lock compatible with the current lock mode? |
| 105 | * @actual: the current state of the lock |
| 106 | * @requested: the lock state that was requested by the caller |
| 107 | * @flags: the modifier flags passed in by the caller |
| 108 | * |
| 109 | * Returns: 1 if the locks are compatible, 0 otherwise |
| 110 | */ |
| 111 | |
| 112 | static inline int relaxed_state_ok(unsigned int actual, unsigned requested, |
| 113 | int flags) |
| 114 | { |
| 115 | if (actual == requested) |
| 116 | return 1; |
| 117 | |
| 118 | if (flags & GL_EXACT) |
| 119 | return 0; |
| 120 | |
| 121 | if (actual == LM_ST_EXCLUSIVE && requested == LM_ST_SHARED) |
| 122 | return 1; |
| 123 | |
| 124 | if (actual != LM_ST_UNLOCKED && (flags & LM_FLAG_ANY)) |
| 125 | return 1; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * gl_hash() - Turn glock number into hash bucket number |
| 132 | * @lock: The glock number |
| 133 | * |
| 134 | * Returns: The number of the corresponding hash bucket |
| 135 | */ |
| 136 | |
Steven Whitehouse | b854785 | 2006-09-07 13:12:27 -0400 | [diff] [blame] | 137 | static unsigned int gl_hash(const struct gfs2_sbd *sdp, |
| 138 | const struct lm_lockname *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 139 | { |
| 140 | unsigned int h; |
| 141 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 142 | h = jhash(&name->ln_number, sizeof(u64), 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 143 | h = jhash(&name->ln_type, sizeof(unsigned int), h); |
Steven Whitehouse | b854785 | 2006-09-07 13:12:27 -0400 | [diff] [blame] | 144 | h = jhash(&sdp, sizeof(struct gfs2_sbd *), h); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 145 | h &= GFS2_GL_HASH_MASK; |
| 146 | |
| 147 | return h; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * glock_free() - Perform a few checks and then release struct gfs2_glock |
| 152 | * @gl: The glock to release |
| 153 | * |
| 154 | * Also calls lock module to release its internal structure for this glock. |
| 155 | * |
| 156 | */ |
| 157 | |
| 158 | static void glock_free(struct gfs2_glock *gl) |
| 159 | { |
| 160 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 161 | struct inode *aspace = gl->gl_aspace; |
| 162 | |
| 163 | gfs2_lm_put_lock(sdp, gl->gl_lock); |
| 164 | |
| 165 | if (aspace) |
| 166 | gfs2_aspace_put(aspace); |
| 167 | |
| 168 | kmem_cache_free(gfs2_glock_cachep, gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * gfs2_glock_hold() - increment reference count on glock |
| 173 | * @gl: The glock to hold |
| 174 | * |
| 175 | */ |
| 176 | |
| 177 | void gfs2_glock_hold(struct gfs2_glock *gl) |
| 178 | { |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 179 | atomic_inc(&gl->gl_ref); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
| 183 | * gfs2_glock_put() - Decrement reference count on glock |
| 184 | * @gl: The glock to put |
| 185 | * |
| 186 | */ |
| 187 | |
| 188 | int gfs2_glock_put(struct gfs2_glock *gl) |
| 189 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 190 | int rv = 0; |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 191 | struct gfs2_sbd *sdp = gl->gl_sbd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 192 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 193 | write_lock(gl_lock_addr(gl->gl_hash)); |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 194 | if (atomic_dec_and_test(&gl->gl_ref)) { |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 195 | hlist_del(&gl->gl_list); |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 196 | write_unlock(gl_lock_addr(gl->gl_hash)); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 197 | BUG_ON(spin_is_locked(&gl->gl_spin)); |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 198 | gfs2_assert(sdp, gl->gl_state == LM_ST_UNLOCKED); |
| 199 | gfs2_assert(sdp, list_empty(&gl->gl_reclaim)); |
| 200 | gfs2_assert(sdp, list_empty(&gl->gl_holders)); |
| 201 | gfs2_assert(sdp, list_empty(&gl->gl_waiters1)); |
| 202 | gfs2_assert(sdp, list_empty(&gl->gl_waiters2)); |
| 203 | gfs2_assert(sdp, list_empty(&gl->gl_waiters3)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 204 | glock_free(gl); |
| 205 | rv = 1; |
| 206 | goto out; |
| 207 | } |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 208 | write_unlock(gl_lock_addr(gl->gl_hash)); |
Steven Whitehouse | a2242db | 2006-08-24 17:03:05 -0400 | [diff] [blame] | 209 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 210 | return rv; |
| 211 | } |
| 212 | |
| 213 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 214 | * search_bucket() - Find struct gfs2_glock by lock number |
| 215 | * @bucket: the bucket to search |
| 216 | * @name: The lock name |
| 217 | * |
| 218 | * Returns: NULL, or the struct gfs2_glock with the requested number |
| 219 | */ |
| 220 | |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 221 | static struct gfs2_glock *search_bucket(unsigned int hash, |
Steven Whitehouse | 899be4d | 2006-08-30 12:50:28 -0400 | [diff] [blame] | 222 | const struct gfs2_sbd *sdp, |
Steven Whitehouse | d6a5372 | 2006-08-30 11:16:23 -0400 | [diff] [blame] | 223 | const struct lm_lockname *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 224 | { |
| 225 | struct gfs2_glock *gl; |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 226 | struct hlist_node *h; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 227 | |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 228 | hlist_for_each_entry(gl, h, &gl_hash_table[hash].hb_list, gl_list) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 229 | if (!lm_name_equal(&gl->gl_name, name)) |
| 230 | continue; |
Steven Whitehouse | 899be4d | 2006-08-30 12:50:28 -0400 | [diff] [blame] | 231 | if (gl->gl_sbd != sdp) |
| 232 | continue; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 233 | |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 234 | atomic_inc(&gl->gl_ref); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 235 | |
| 236 | return gl; |
| 237 | } |
| 238 | |
| 239 | return NULL; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * gfs2_glock_find() - Find glock by lock number |
| 244 | * @sdp: The GFS2 superblock |
| 245 | * @name: The lock name |
| 246 | * |
| 247 | * Returns: NULL, or the struct gfs2_glock with the requested number |
| 248 | */ |
| 249 | |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 250 | static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp, |
Steven Whitehouse | d6a5372 | 2006-08-30 11:16:23 -0400 | [diff] [blame] | 251 | const struct lm_lockname *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 252 | { |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 253 | unsigned int hash = gl_hash(sdp, name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 254 | struct gfs2_glock *gl; |
| 255 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 256 | read_lock(gl_lock_addr(hash)); |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 257 | gl = search_bucket(hash, sdp, name); |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 258 | read_unlock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 259 | |
| 260 | return gl; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * gfs2_glock_get() - Get a glock, or create one if one doesn't exist |
| 265 | * @sdp: The GFS2 superblock |
| 266 | * @number: the lock number |
| 267 | * @glops: The glock_operations to use |
| 268 | * @create: If 0, don't create the glock if it doesn't exist |
| 269 | * @glp: the glock is returned here |
| 270 | * |
| 271 | * This does not lock a glock, just finds/creates structures for one. |
| 272 | * |
| 273 | * Returns: errno |
| 274 | */ |
| 275 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 276 | int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number, |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 277 | const struct gfs2_glock_operations *glops, int create, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 278 | struct gfs2_glock **glp) |
| 279 | { |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 280 | struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type }; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 281 | struct gfs2_glock *gl, *tmp; |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 282 | unsigned int hash = gl_hash(sdp, &name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 283 | int error; |
| 284 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 285 | read_lock(gl_lock_addr(hash)); |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 286 | gl = search_bucket(hash, sdp, &name); |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 287 | read_unlock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 288 | |
| 289 | if (gl || !create) { |
| 290 | *glp = gl; |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL); |
| 295 | if (!gl) |
| 296 | return -ENOMEM; |
| 297 | |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 298 | gl->gl_flags = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 299 | gl->gl_name = name; |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 300 | atomic_set(&gl->gl_ref, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 301 | gl->gl_state = LM_ST_UNLOCKED; |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 302 | gl->gl_hash = hash; |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 303 | gl->gl_owner = NULL; |
| 304 | gl->gl_ip = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 305 | gl->gl_ops = glops; |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 306 | gl->gl_req_gh = NULL; |
| 307 | gl->gl_req_bh = NULL; |
| 308 | gl->gl_vn = 0; |
| 309 | gl->gl_stamp = jiffies; |
| 310 | gl->gl_object = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 311 | gl->gl_sbd = sdp; |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 312 | gl->gl_aspace = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 313 | lops_init_le(&gl->gl_le, &gfs2_glock_lops); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 314 | |
| 315 | /* If this glock protects actual on-disk data or metadata blocks, |
| 316 | create a VFS inode to manage the pages/buffers holding them. */ |
Steven Whitehouse | 5029996 | 2006-09-04 09:49:55 -0400 | [diff] [blame] | 317 | if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 318 | gl->gl_aspace = gfs2_aspace_get(sdp); |
| 319 | if (!gl->gl_aspace) { |
| 320 | error = -ENOMEM; |
| 321 | goto fail; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock); |
| 326 | if (error) |
| 327 | goto fail_aspace; |
| 328 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 329 | write_lock(gl_lock_addr(hash)); |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 330 | tmp = search_bucket(hash, sdp, &name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 331 | if (tmp) { |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 332 | write_unlock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 333 | glock_free(gl); |
| 334 | gl = tmp; |
| 335 | } else { |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 336 | hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list); |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 337 | write_unlock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | *glp = gl; |
| 341 | |
| 342 | return 0; |
| 343 | |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 344 | fail_aspace: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 345 | if (gl->gl_aspace) |
| 346 | gfs2_aspace_put(gl->gl_aspace); |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 347 | fail: |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 348 | kmem_cache_free(gfs2_glock_cachep, gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 349 | return error; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * gfs2_holder_init - initialize a struct gfs2_holder in the default way |
| 354 | * @gl: the glock |
| 355 | * @state: the state we're requesting |
| 356 | * @flags: the modifier flags |
| 357 | * @gh: the holder structure |
| 358 | * |
| 359 | */ |
| 360 | |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 361 | 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] | 362 | struct gfs2_holder *gh) |
| 363 | { |
| 364 | INIT_LIST_HEAD(&gh->gh_list); |
| 365 | gh->gh_gl = gl; |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 366 | gh->gh_ip = (unsigned long)__builtin_return_address(0); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 367 | gh->gh_owner = current; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 368 | gh->gh_state = state; |
| 369 | gh->gh_flags = flags; |
| 370 | gh->gh_error = 0; |
| 371 | gh->gh_iflags = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | gh->gh_iflags &= 1 << HIF_ALLOCED; |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 390 | gh->gh_ip = (unsigned long)__builtin_return_address(0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | /** |
| 394 | * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference) |
| 395 | * @gh: the holder structure |
| 396 | * |
| 397 | */ |
| 398 | |
| 399 | void gfs2_holder_uninit(struct gfs2_holder *gh) |
| 400 | { |
| 401 | gfs2_glock_put(gh->gh_gl); |
| 402 | gh->gh_gl = NULL; |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 403 | gh->gh_ip = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
| 407 | * gfs2_holder_get - get a struct gfs2_holder structure |
| 408 | * @gl: the glock |
| 409 | * @state: the state we're requesting |
| 410 | * @flags: the modifier flags |
Steven Whitehouse | af18ddb8 | 2006-06-24 15:42:21 -0400 | [diff] [blame] | 411 | * @gfp_flags: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 412 | * |
| 413 | * Figure out how big an impact this function has. Either: |
| 414 | * 1) Replace it with a cache of structures hanging off the struct gfs2_sbd |
| 415 | * 2) Leave it like it is |
| 416 | * |
| 417 | * Returns: the holder structure, NULL on ENOMEM |
| 418 | */ |
| 419 | |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 420 | static struct gfs2_holder *gfs2_holder_get(struct gfs2_glock *gl, |
| 421 | unsigned int state, |
| 422 | int flags, gfp_t gfp_flags) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 423 | { |
| 424 | struct gfs2_holder *gh; |
| 425 | |
| 426 | gh = kmalloc(sizeof(struct gfs2_holder), gfp_flags); |
| 427 | if (!gh) |
| 428 | return NULL; |
| 429 | |
| 430 | gfs2_holder_init(gl, state, flags, gh); |
| 431 | set_bit(HIF_ALLOCED, &gh->gh_iflags); |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 432 | gh->gh_ip = (unsigned long)__builtin_return_address(0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 433 | return gh; |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * gfs2_holder_put - get rid of a struct gfs2_holder structure |
| 438 | * @gh: the holder structure |
| 439 | * |
| 440 | */ |
| 441 | |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 442 | static void gfs2_holder_put(struct gfs2_holder *gh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 443 | { |
| 444 | gfs2_holder_uninit(gh); |
| 445 | kfree(gh); |
| 446 | } |
| 447 | |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 448 | static void gfs2_holder_dispose_or_wake(struct gfs2_holder *gh) |
| 449 | { |
| 450 | if (test_bit(HIF_DEALLOC, &gh->gh_iflags)) { |
| 451 | gfs2_holder_put(gh); |
| 452 | return; |
| 453 | } |
| 454 | clear_bit(HIF_WAIT, &gh->gh_iflags); |
| 455 | smp_mb(); |
| 456 | wake_up_bit(&gh->gh_iflags, HIF_WAIT); |
| 457 | } |
| 458 | |
| 459 | static int holder_wait(void *word) |
| 460 | { |
| 461 | schedule(); |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static void wait_on_holder(struct gfs2_holder *gh) |
| 466 | { |
| 467 | might_sleep(); |
| 468 | wait_on_bit(&gh->gh_iflags, HIF_WAIT, holder_wait, TASK_UNINTERRUPTIBLE); |
| 469 | } |
| 470 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 471 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 472 | * rq_mutex - process a mutex request in the queue |
| 473 | * @gh: the glock holder |
| 474 | * |
| 475 | * Returns: 1 if the queue is blocked |
| 476 | */ |
| 477 | |
| 478 | static int rq_mutex(struct gfs2_holder *gh) |
| 479 | { |
| 480 | struct gfs2_glock *gl = gh->gh_gl; |
| 481 | |
| 482 | list_del_init(&gh->gh_list); |
| 483 | /* gh->gh_error never examined. */ |
| 484 | set_bit(GLF_LOCK, &gl->gl_flags); |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 485 | clear_bit(HIF_WAIT, &gh->gh_flags); |
| 486 | smp_mb(); |
| 487 | wake_up_bit(&gh->gh_iflags, HIF_WAIT); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 488 | |
| 489 | return 1; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * rq_promote - process a promote request in the queue |
| 494 | * @gh: the glock holder |
| 495 | * |
| 496 | * Acquire a new inter-node lock, or change a lock state to more restrictive. |
| 497 | * |
| 498 | * Returns: 1 if the queue is blocked |
| 499 | */ |
| 500 | |
| 501 | static int rq_promote(struct gfs2_holder *gh) |
| 502 | { |
| 503 | struct gfs2_glock *gl = gh->gh_gl; |
| 504 | struct gfs2_sbd *sdp = gl->gl_sbd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 505 | |
| 506 | if (!relaxed_state_ok(gl->gl_state, gh->gh_state, gh->gh_flags)) { |
| 507 | if (list_empty(&gl->gl_holders)) { |
| 508 | gl->gl_req_gh = gh; |
| 509 | set_bit(GLF_LOCK, &gl->gl_flags); |
| 510 | spin_unlock(&gl->gl_spin); |
| 511 | |
| 512 | if (atomic_read(&sdp->sd_reclaim_count) > |
| 513 | gfs2_tune_get(sdp, gt_reclaim_limit) && |
| 514 | !(gh->gh_flags & LM_FLAG_PRIORITY)) { |
| 515 | gfs2_reclaim_glock(sdp); |
| 516 | gfs2_reclaim_glock(sdp); |
| 517 | } |
| 518 | |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 519 | gfs2_glock_xmote_th(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 520 | spin_lock(&gl->gl_spin); |
| 521 | } |
| 522 | return 1; |
| 523 | } |
| 524 | |
| 525 | if (list_empty(&gl->gl_holders)) { |
| 526 | set_bit(HIF_FIRST, &gh->gh_iflags); |
| 527 | set_bit(GLF_LOCK, &gl->gl_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 528 | } else { |
| 529 | struct gfs2_holder *next_gh; |
Steven Whitehouse | 1c0f487 | 2007-01-22 12:10:39 -0500 | [diff] [blame] | 530 | if (gh->gh_state == LM_ST_EXCLUSIVE) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 531 | return 1; |
| 532 | next_gh = list_entry(gl->gl_holders.next, struct gfs2_holder, |
| 533 | gh_list); |
Steven Whitehouse | 1c0f487 | 2007-01-22 12:10:39 -0500 | [diff] [blame] | 534 | if (next_gh->gh_state == LM_ST_EXCLUSIVE) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 535 | return 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | list_move_tail(&gh->gh_list, &gl->gl_holders); |
| 539 | gh->gh_error = 0; |
| 540 | set_bit(HIF_HOLDER, &gh->gh_iflags); |
| 541 | |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 542 | gfs2_holder_dispose_or_wake(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | /** |
| 548 | * rq_demote - process a demote request in the queue |
| 549 | * @gh: the glock holder |
| 550 | * |
| 551 | * Returns: 1 if the queue is blocked |
| 552 | */ |
| 553 | |
| 554 | static int rq_demote(struct gfs2_holder *gh) |
| 555 | { |
| 556 | struct gfs2_glock *gl = gh->gh_gl; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 557 | |
| 558 | if (!list_empty(&gl->gl_holders)) |
| 559 | return 1; |
| 560 | |
| 561 | if (gl->gl_state == gh->gh_state || gl->gl_state == LM_ST_UNLOCKED) { |
| 562 | list_del_init(&gh->gh_list); |
| 563 | gh->gh_error = 0; |
| 564 | spin_unlock(&gl->gl_spin); |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 565 | gfs2_holder_dispose_or_wake(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 566 | spin_lock(&gl->gl_spin); |
| 567 | } else { |
| 568 | gl->gl_req_gh = gh; |
| 569 | set_bit(GLF_LOCK, &gl->gl_flags); |
| 570 | spin_unlock(&gl->gl_spin); |
| 571 | |
| 572 | if (gh->gh_state == LM_ST_UNLOCKED || |
| 573 | gl->gl_state != LM_ST_EXCLUSIVE) |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 574 | gfs2_glock_drop_th(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 575 | else |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 576 | gfs2_glock_xmote_th(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 577 | |
| 578 | spin_lock(&gl->gl_spin); |
| 579 | } |
| 580 | |
| 581 | return 0; |
| 582 | } |
| 583 | |
| 584 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 585 | * run_queue - process holder structures on a glock |
| 586 | * @gl: the glock |
| 587 | * |
| 588 | */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 589 | static void run_queue(struct gfs2_glock *gl) |
| 590 | { |
| 591 | struct gfs2_holder *gh; |
| 592 | int blocked = 1; |
| 593 | |
| 594 | for (;;) { |
| 595 | if (test_bit(GLF_LOCK, &gl->gl_flags)) |
| 596 | break; |
| 597 | |
| 598 | if (!list_empty(&gl->gl_waiters1)) { |
| 599 | gh = list_entry(gl->gl_waiters1.next, |
| 600 | struct gfs2_holder, gh_list); |
| 601 | |
| 602 | if (test_bit(HIF_MUTEX, &gh->gh_iflags)) |
| 603 | blocked = rq_mutex(gh); |
| 604 | else |
| 605 | gfs2_assert_warn(gl->gl_sbd, 0); |
| 606 | |
| 607 | } else if (!list_empty(&gl->gl_waiters2) && |
| 608 | !test_bit(GLF_SKIP_WAITERS2, &gl->gl_flags)) { |
| 609 | gh = list_entry(gl->gl_waiters2.next, |
| 610 | struct gfs2_holder, gh_list); |
| 611 | |
| 612 | if (test_bit(HIF_DEMOTE, &gh->gh_iflags)) |
| 613 | blocked = rq_demote(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 614 | else |
| 615 | gfs2_assert_warn(gl->gl_sbd, 0); |
| 616 | |
| 617 | } else if (!list_empty(&gl->gl_waiters3)) { |
| 618 | gh = list_entry(gl->gl_waiters3.next, |
| 619 | struct gfs2_holder, gh_list); |
| 620 | |
| 621 | if (test_bit(HIF_PROMOTE, &gh->gh_iflags)) |
| 622 | blocked = rq_promote(gh); |
| 623 | else |
| 624 | gfs2_assert_warn(gl->gl_sbd, 0); |
| 625 | |
| 626 | } else |
| 627 | break; |
| 628 | |
| 629 | if (blocked) |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | /** |
| 635 | * gfs2_glmutex_lock - acquire a local lock on a glock |
| 636 | * @gl: the glock |
| 637 | * |
| 638 | * Gives caller exclusive access to manipulate a glock structure. |
| 639 | */ |
| 640 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 641 | static void gfs2_glmutex_lock(struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 642 | { |
| 643 | struct gfs2_holder gh; |
| 644 | |
| 645 | gfs2_holder_init(gl, 0, 0, &gh); |
| 646 | set_bit(HIF_MUTEX, &gh.gh_iflags); |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 647 | if (test_and_set_bit(HIF_WAIT, &gh.gh_iflags)) |
| 648 | BUG(); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 649 | |
| 650 | spin_lock(&gl->gl_spin); |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 651 | if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 652 | list_add_tail(&gh.gh_list, &gl->gl_waiters1); |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 653 | } else { |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 654 | gl->gl_owner = current; |
| 655 | gl->gl_ip = (unsigned long)__builtin_return_address(0); |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 656 | clear_bit(HIF_WAIT, &gh.gh_iflags); |
| 657 | smp_mb(); |
| 658 | wake_up_bit(&gh.gh_iflags, HIF_WAIT); |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 659 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 660 | spin_unlock(&gl->gl_spin); |
| 661 | |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 662 | wait_on_holder(&gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 663 | gfs2_holder_uninit(&gh); |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * gfs2_glmutex_trylock - try to acquire a local lock on a glock |
| 668 | * @gl: the glock |
| 669 | * |
| 670 | * Returns: 1 if the glock is acquired |
| 671 | */ |
| 672 | |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 673 | static int gfs2_glmutex_trylock(struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 674 | { |
| 675 | int acquired = 1; |
| 676 | |
| 677 | spin_lock(&gl->gl_spin); |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 678 | if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 679 | acquired = 0; |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 680 | } else { |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 681 | gl->gl_owner = current; |
| 682 | gl->gl_ip = (unsigned long)__builtin_return_address(0); |
| 683 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 684 | spin_unlock(&gl->gl_spin); |
| 685 | |
| 686 | return acquired; |
| 687 | } |
| 688 | |
| 689 | /** |
| 690 | * gfs2_glmutex_unlock - release a local lock on a glock |
| 691 | * @gl: the glock |
| 692 | * |
| 693 | */ |
| 694 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 695 | static void gfs2_glmutex_unlock(struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 696 | { |
| 697 | spin_lock(&gl->gl_spin); |
| 698 | clear_bit(GLF_LOCK, &gl->gl_flags); |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 699 | gl->gl_owner = NULL; |
| 700 | gl->gl_ip = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 701 | run_queue(gl); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 702 | BUG_ON(!spin_is_locked(&gl->gl_spin)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 703 | spin_unlock(&gl->gl_spin); |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * handle_callback - add a demote request to a lock's queue |
| 708 | * @gl: the glock |
| 709 | * @state: the state the caller wants us to change to |
| 710 | * |
Steven Whitehouse | af18ddb8 | 2006-06-24 15:42:21 -0400 | [diff] [blame] | 711 | * Note: This may fail sliently if we are out of memory. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 712 | */ |
| 713 | |
| 714 | static void handle_callback(struct gfs2_glock *gl, unsigned int state) |
| 715 | { |
| 716 | struct gfs2_holder *gh, *new_gh = NULL; |
| 717 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 718 | restart: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | ab92303 | 2006-11-15 15:17:03 -0500 | [diff] [blame] | 736 | new_gh = gfs2_holder_get(gl, state, LM_FLAG_TRY, GFP_NOFS); |
Steven Whitehouse | af18ddb8 | 2006-06-24 15:42:21 -0400 | [diff] [blame] | 737 | if (!new_gh) |
| 738 | return; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 739 | set_bit(HIF_DEMOTE, &new_gh->gh_iflags); |
| 740 | set_bit(HIF_DEALLOC, &new_gh->gh_iflags); |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 741 | set_bit(HIF_WAIT, &new_gh->gh_iflags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 742 | |
| 743 | goto restart; |
| 744 | } |
| 745 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 746 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 747 | spin_unlock(&gl->gl_spin); |
| 748 | |
| 749 | if (new_gh) |
| 750 | gfs2_holder_put(new_gh); |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * state_change - record that the glock is now in a different state |
| 755 | * @gl: the glock |
| 756 | * @new_state the new state |
| 757 | * |
| 758 | */ |
| 759 | |
| 760 | static void state_change(struct gfs2_glock *gl, unsigned int new_state) |
| 761 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 762 | int held1, held2; |
| 763 | |
| 764 | held1 = (gl->gl_state != LM_ST_UNLOCKED); |
| 765 | held2 = (new_state != LM_ST_UNLOCKED); |
| 766 | |
| 767 | if (held1 != held2) { |
David Teigland | 6a6b3d0 | 2006-02-23 10:11:47 +0000 | [diff] [blame] | 768 | if (held2) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 769 | gfs2_glock_hold(gl); |
David Teigland | 6a6b3d0 | 2006-02-23 10:11:47 +0000 | [diff] [blame] | 770 | else |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 771 | gfs2_glock_put(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | gl->gl_state = new_state; |
| 775 | } |
| 776 | |
| 777 | /** |
| 778 | * xmote_bh - Called after the lock module is done acquiring a lock |
| 779 | * @gl: The glock in question |
| 780 | * @ret: the int returned from the lock module |
| 781 | * |
| 782 | */ |
| 783 | |
| 784 | static void xmote_bh(struct gfs2_glock *gl, unsigned int ret) |
| 785 | { |
| 786 | struct gfs2_sbd *sdp = gl->gl_sbd; |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 787 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 788 | struct gfs2_holder *gh = gl->gl_req_gh; |
| 789 | int prev_state = gl->gl_state; |
| 790 | int op_done = 1; |
| 791 | |
| 792 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
Steven Whitehouse | 1213293 | 2007-01-22 13:09:04 -0500 | [diff] [blame^] | 793 | gfs2_assert_warn(sdp, list_empty(&gl->gl_holders)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 794 | gfs2_assert_warn(sdp, !(ret & LM_OUT_ASYNC)); |
| 795 | |
| 796 | state_change(gl, ret & LM_OUT_ST_MASK); |
| 797 | |
| 798 | if (prev_state != LM_ST_UNLOCKED && !(ret & LM_OUT_CACHEABLE)) { |
| 799 | if (glops->go_inval) |
Steven Whitehouse | 1a14d3a | 2006-11-20 10:37:45 -0500 | [diff] [blame] | 800 | glops->go_inval(gl, DIO_METADATA); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 801 | } else if (gl->gl_state == LM_ST_DEFERRED) { |
| 802 | /* We might not want to do this here. |
| 803 | Look at moving to the inode glops. */ |
| 804 | if (glops->go_inval) |
Steven Whitehouse | 1a14d3a | 2006-11-20 10:37:45 -0500 | [diff] [blame] | 805 | glops->go_inval(gl, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /* Deal with each possible exit condition */ |
| 809 | |
| 810 | if (!gh) |
| 811 | gl->gl_stamp = jiffies; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 812 | else if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) { |
| 813 | spin_lock(&gl->gl_spin); |
| 814 | list_del_init(&gh->gh_list); |
| 815 | gh->gh_error = -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 816 | spin_unlock(&gl->gl_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 || |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 821 | gl->gl_state == LM_ST_UNLOCKED) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 822 | gh->gh_error = 0; |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 823 | } else { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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) |
Steven Whitehouse | 5029996 | 2006-09-04 09:49:55 -0400 | [diff] [blame] | 832 | handle_callback(gl, LM_ST_UNLOCKED); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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 | |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 876 | if (gh) |
| 877 | gfs2_holder_dispose_or_wake(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | /** |
| 881 | * gfs2_glock_xmote_th - Call into the lock module to acquire or change a glock |
| 882 | * @gl: The glock in question |
| 883 | * @state: the requested state |
| 884 | * @flags: modifier flags to the lock call |
| 885 | * |
| 886 | */ |
| 887 | |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 888 | void gfs2_glock_xmote_th(struct gfs2_holder *gh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 889 | { |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 890 | struct gfs2_glock *gl = gh->gh_gl; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 891 | struct gfs2_sbd *sdp = gl->gl_sbd; |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 892 | int flags = gh->gh_flags; |
| 893 | unsigned state = gh->gh_state; |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 894 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 895 | int lck_flags = flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB | |
| 896 | LM_FLAG_NOEXP | LM_FLAG_ANY | |
| 897 | LM_FLAG_PRIORITY); |
| 898 | unsigned int lck_ret; |
| 899 | |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 900 | if (glops->go_xmote_th) |
| 901 | glops->go_xmote_th(gl); |
| 902 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 903 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
Steven Whitehouse | 1213293 | 2007-01-22 13:09:04 -0500 | [diff] [blame^] | 904 | gfs2_assert_warn(sdp, list_empty(&gl->gl_holders)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 905 | gfs2_assert_warn(sdp, state != LM_ST_UNLOCKED); |
| 906 | gfs2_assert_warn(sdp, state != gl->gl_state); |
| 907 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 908 | gfs2_glock_hold(gl); |
| 909 | gl->gl_req_bh = xmote_bh; |
| 910 | |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 911 | lck_ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, state, lck_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 912 | |
| 913 | if (gfs2_assert_withdraw(sdp, !(lck_ret & LM_OUT_ERROR))) |
| 914 | return; |
| 915 | |
| 916 | if (lck_ret & LM_OUT_ASYNC) |
| 917 | gfs2_assert_warn(sdp, lck_ret == LM_OUT_ASYNC); |
| 918 | else |
| 919 | xmote_bh(gl, lck_ret); |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * drop_bh - Called after a lock module unlock completes |
| 924 | * @gl: the glock |
| 925 | * @ret: the return status |
| 926 | * |
| 927 | * Doesn't wake up the process waiting on the struct gfs2_holder (if any) |
| 928 | * Doesn't drop the reference on the glock the top half took out |
| 929 | * |
| 930 | */ |
| 931 | |
| 932 | static void drop_bh(struct gfs2_glock *gl, unsigned int ret) |
| 933 | { |
| 934 | struct gfs2_sbd *sdp = gl->gl_sbd; |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 935 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 936 | struct gfs2_holder *gh = gl->gl_req_gh; |
| 937 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 938 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
Steven Whitehouse | 1213293 | 2007-01-22 13:09:04 -0500 | [diff] [blame^] | 939 | gfs2_assert_warn(sdp, list_empty(&gl->gl_holders)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 940 | gfs2_assert_warn(sdp, !ret); |
| 941 | |
| 942 | state_change(gl, LM_ST_UNLOCKED); |
| 943 | |
| 944 | if (glops->go_inval) |
Steven Whitehouse | 1a14d3a | 2006-11-20 10:37:45 -0500 | [diff] [blame] | 945 | glops->go_inval(gl, DIO_METADATA); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 946 | |
| 947 | if (gh) { |
| 948 | spin_lock(&gl->gl_spin); |
| 949 | list_del_init(&gh->gh_list); |
| 950 | gh->gh_error = 0; |
| 951 | spin_unlock(&gl->gl_spin); |
| 952 | } |
| 953 | |
| 954 | if (glops->go_drop_bh) |
| 955 | glops->go_drop_bh(gl); |
| 956 | |
| 957 | spin_lock(&gl->gl_spin); |
| 958 | gl->gl_req_gh = NULL; |
| 959 | gl->gl_req_bh = NULL; |
| 960 | clear_bit(GLF_LOCK, &gl->gl_flags); |
| 961 | run_queue(gl); |
| 962 | spin_unlock(&gl->gl_spin); |
| 963 | |
| 964 | gfs2_glock_put(gl); |
| 965 | |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 966 | if (gh) |
| 967 | gfs2_holder_dispose_or_wake(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /** |
| 971 | * gfs2_glock_drop_th - call into the lock module to unlock a lock |
| 972 | * @gl: the glock |
| 973 | * |
| 974 | */ |
| 975 | |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 976 | static void gfs2_glock_drop_th(struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 977 | { |
| 978 | struct gfs2_sbd *sdp = gl->gl_sbd; |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 979 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 980 | unsigned int ret; |
| 981 | |
Steven Whitehouse | b5d32be | 2007-01-22 12:15:34 -0500 | [diff] [blame] | 982 | if (glops->go_drop_th) |
| 983 | glops->go_drop_th(gl); |
| 984 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 985 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
Steven Whitehouse | 1213293 | 2007-01-22 13:09:04 -0500 | [diff] [blame^] | 986 | gfs2_assert_warn(sdp, list_empty(&gl->gl_holders)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 987 | gfs2_assert_warn(sdp, gl->gl_state != LM_ST_UNLOCKED); |
| 988 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 989 | gfs2_glock_hold(gl); |
| 990 | gl->gl_req_bh = drop_bh; |
| 991 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 992 | ret = gfs2_lm_unlock(sdp, gl->gl_lock, gl->gl_state); |
| 993 | |
| 994 | if (gfs2_assert_withdraw(sdp, !(ret & LM_OUT_ERROR))) |
| 995 | return; |
| 996 | |
| 997 | if (!ret) |
| 998 | drop_bh(gl, ret); |
| 999 | else |
| 1000 | gfs2_assert_warn(sdp, ret == LM_OUT_ASYNC); |
| 1001 | } |
| 1002 | |
| 1003 | /** |
| 1004 | * do_cancels - cancel requests for locks stuck waiting on an expire flag |
| 1005 | * @gh: the LM_FLAG_PRIORITY holder waiting to acquire the lock |
| 1006 | * |
| 1007 | * Don't cancel GL_NOCANCEL requests. |
| 1008 | */ |
| 1009 | |
| 1010 | static void do_cancels(struct gfs2_holder *gh) |
| 1011 | { |
| 1012 | struct gfs2_glock *gl = gh->gh_gl; |
| 1013 | |
| 1014 | spin_lock(&gl->gl_spin); |
| 1015 | |
| 1016 | while (gl->gl_req_gh != gh && |
| 1017 | !test_bit(HIF_HOLDER, &gh->gh_iflags) && |
| 1018 | !list_empty(&gh->gh_list)) { |
Steven Whitehouse | 5029996 | 2006-09-04 09:49:55 -0400 | [diff] [blame] | 1019 | if (gl->gl_req_bh && !(gl->gl_req_gh && |
| 1020 | (gl->gl_req_gh->gh_flags & GL_NOCANCEL))) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1021 | spin_unlock(&gl->gl_spin); |
| 1022 | gfs2_lm_cancel(gl->gl_sbd, gl->gl_lock); |
| 1023 | msleep(100); |
| 1024 | spin_lock(&gl->gl_spin); |
| 1025 | } else { |
| 1026 | spin_unlock(&gl->gl_spin); |
| 1027 | msleep(100); |
| 1028 | spin_lock(&gl->gl_spin); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | spin_unlock(&gl->gl_spin); |
| 1033 | } |
| 1034 | |
| 1035 | /** |
| 1036 | * glock_wait_internal - wait on a glock acquisition |
| 1037 | * @gh: the glock holder |
| 1038 | * |
| 1039 | * Returns: 0 on success |
| 1040 | */ |
| 1041 | |
| 1042 | static int glock_wait_internal(struct gfs2_holder *gh) |
| 1043 | { |
| 1044 | struct gfs2_glock *gl = gh->gh_gl; |
| 1045 | struct gfs2_sbd *sdp = gl->gl_sbd; |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 1046 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (test_bit(HIF_ABORTED, &gh->gh_iflags)) |
| 1049 | return -EIO; |
| 1050 | |
| 1051 | if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) { |
| 1052 | spin_lock(&gl->gl_spin); |
| 1053 | if (gl->gl_req_gh != gh && |
| 1054 | !test_bit(HIF_HOLDER, &gh->gh_iflags) && |
| 1055 | !list_empty(&gh->gh_list)) { |
| 1056 | list_del_init(&gh->gh_list); |
| 1057 | gh->gh_error = GLR_TRYFAILED; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1058 | run_queue(gl); |
| 1059 | spin_unlock(&gl->gl_spin); |
| 1060 | return gh->gh_error; |
| 1061 | } |
| 1062 | spin_unlock(&gl->gl_spin); |
| 1063 | } |
| 1064 | |
| 1065 | if (gh->gh_flags & LM_FLAG_PRIORITY) |
| 1066 | do_cancels(gh); |
| 1067 | |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 1068 | wait_on_holder(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1069 | if (gh->gh_error) |
| 1070 | return gh->gh_error; |
| 1071 | |
| 1072 | gfs2_assert_withdraw(sdp, test_bit(HIF_HOLDER, &gh->gh_iflags)); |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 1073 | gfs2_assert_withdraw(sdp, relaxed_state_ok(gl->gl_state, gh->gh_state, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1074 | gh->gh_flags)); |
| 1075 | |
| 1076 | if (test_bit(HIF_FIRST, &gh->gh_iflags)) { |
| 1077 | gfs2_assert_warn(sdp, test_bit(GLF_LOCK, &gl->gl_flags)); |
| 1078 | |
| 1079 | if (glops->go_lock) { |
| 1080 | gh->gh_error = glops->go_lock(gh); |
| 1081 | if (gh->gh_error) { |
| 1082 | spin_lock(&gl->gl_spin); |
| 1083 | list_del_init(&gh->gh_list); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1084 | spin_unlock(&gl->gl_spin); |
| 1085 | } |
| 1086 | } |
| 1087 | |
| 1088 | spin_lock(&gl->gl_spin); |
| 1089 | gl->gl_req_gh = NULL; |
| 1090 | gl->gl_req_bh = NULL; |
| 1091 | clear_bit(GLF_LOCK, &gl->gl_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1092 | run_queue(gl); |
| 1093 | spin_unlock(&gl->gl_spin); |
| 1094 | } |
| 1095 | |
| 1096 | return gh->gh_error; |
| 1097 | } |
| 1098 | |
| 1099 | static inline struct gfs2_holder * |
| 1100 | find_holder_by_owner(struct list_head *head, struct task_struct *owner) |
| 1101 | { |
| 1102 | struct gfs2_holder *gh; |
| 1103 | |
| 1104 | list_for_each_entry(gh, head, gh_list) { |
| 1105 | if (gh->gh_owner == owner) |
| 1106 | return gh; |
| 1107 | } |
| 1108 | |
| 1109 | return NULL; |
| 1110 | } |
| 1111 | |
| 1112 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1113 | * add_to_queue - Add a holder to the wait queue (but look for recursion) |
| 1114 | * @gh: the holder structure to add |
| 1115 | * |
| 1116 | */ |
| 1117 | |
| 1118 | static void add_to_queue(struct gfs2_holder *gh) |
| 1119 | { |
| 1120 | struct gfs2_glock *gl = gh->gh_gl; |
| 1121 | struct gfs2_holder *existing; |
| 1122 | |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 1123 | BUG_ON(!gh->gh_owner); |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 1124 | if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags)) |
| 1125 | BUG(); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 1126 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1127 | existing = find_holder_by_owner(&gl->gl_holders, gh->gh_owner); |
| 1128 | if (existing) { |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 1129 | print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip); |
Abhijith Das | 8638460 | 2006-08-25 11:13:37 -0500 | [diff] [blame] | 1130 | printk(KERN_INFO "pid : %d\n", existing->gh_owner->pid); |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 1131 | printk(KERN_INFO "lock type : %d lock state : %d\n", |
Abhijith Das | 8638460 | 2006-08-25 11:13:37 -0500 | [diff] [blame] | 1132 | existing->gh_gl->gl_name.ln_type, existing->gh_gl->gl_state); |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 1133 | print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip); |
Abhijith Das | 8638460 | 2006-08-25 11:13:37 -0500 | [diff] [blame] | 1134 | printk(KERN_INFO "pid : %d\n", gh->gh_owner->pid); |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 1135 | printk(KERN_INFO "lock type : %d lock state : %d\n", |
Abhijith Das | 8638460 | 2006-08-25 11:13:37 -0500 | [diff] [blame] | 1136 | gl->gl_name.ln_type, gl->gl_state); |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 1137 | BUG(); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | existing = find_holder_by_owner(&gl->gl_waiters3, gh->gh_owner); |
| 1141 | if (existing) { |
Steven Whitehouse | 5965b1f | 2006-04-26 13:21:55 -0400 | [diff] [blame] | 1142 | print_symbol(KERN_WARNING "original: %s\n", existing->gh_ip); |
| 1143 | print_symbol(KERN_WARNING "new: %s\n", gh->gh_ip); |
| 1144 | BUG(); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1147 | if (gh->gh_flags & LM_FLAG_PRIORITY) |
| 1148 | list_add(&gh->gh_list, &gl->gl_waiters3); |
| 1149 | else |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 1150 | list_add_tail(&gh->gh_list, &gl->gl_waiters3); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock) |
| 1155 | * @gh: the holder structure |
| 1156 | * |
| 1157 | * if (gh->gh_flags & GL_ASYNC), this never returns an error |
| 1158 | * |
| 1159 | * Returns: 0, GLR_TRYFAILED, or errno on failure |
| 1160 | */ |
| 1161 | |
| 1162 | int gfs2_glock_nq(struct gfs2_holder *gh) |
| 1163 | { |
| 1164 | struct gfs2_glock *gl = gh->gh_gl; |
| 1165 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1166 | int error = 0; |
| 1167 | |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 1168 | restart: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1169 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) { |
| 1170 | set_bit(HIF_ABORTED, &gh->gh_iflags); |
| 1171 | return -EIO; |
| 1172 | } |
| 1173 | |
| 1174 | set_bit(HIF_PROMOTE, &gh->gh_iflags); |
| 1175 | |
| 1176 | spin_lock(&gl->gl_spin); |
| 1177 | add_to_queue(gh); |
| 1178 | run_queue(gl); |
| 1179 | spin_unlock(&gl->gl_spin); |
| 1180 | |
| 1181 | if (!(gh->gh_flags & GL_ASYNC)) { |
| 1182 | error = glock_wait_internal(gh); |
| 1183 | if (error == GLR_CANCELED) { |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 1184 | msleep(100); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1185 | goto restart; |
| 1186 | } |
| 1187 | } |
| 1188 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1189 | return error; |
| 1190 | } |
| 1191 | |
| 1192 | /** |
| 1193 | * gfs2_glock_poll - poll to see if an async request has been completed |
| 1194 | * @gh: the holder |
| 1195 | * |
| 1196 | * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on |
| 1197 | */ |
| 1198 | |
| 1199 | int gfs2_glock_poll(struct gfs2_holder *gh) |
| 1200 | { |
| 1201 | struct gfs2_glock *gl = gh->gh_gl; |
| 1202 | int ready = 0; |
| 1203 | |
| 1204 | spin_lock(&gl->gl_spin); |
| 1205 | |
| 1206 | if (test_bit(HIF_HOLDER, &gh->gh_iflags)) |
| 1207 | ready = 1; |
| 1208 | else if (list_empty(&gh->gh_list)) { |
| 1209 | if (gh->gh_error == GLR_CANCELED) { |
| 1210 | spin_unlock(&gl->gl_spin); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 1211 | msleep(100); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1212 | if (gfs2_glock_nq(gh)) |
| 1213 | return 1; |
| 1214 | return 0; |
| 1215 | } else |
| 1216 | ready = 1; |
| 1217 | } |
| 1218 | |
| 1219 | spin_unlock(&gl->gl_spin); |
| 1220 | |
| 1221 | return ready; |
| 1222 | } |
| 1223 | |
| 1224 | /** |
| 1225 | * gfs2_glock_wait - wait for a lock acquisition that ended in a GLR_ASYNC |
| 1226 | * @gh: the holder structure |
| 1227 | * |
| 1228 | * Returns: 0, GLR_TRYFAILED, or errno on failure |
| 1229 | */ |
| 1230 | |
| 1231 | int gfs2_glock_wait(struct gfs2_holder *gh) |
| 1232 | { |
| 1233 | int error; |
| 1234 | |
| 1235 | error = glock_wait_internal(gh); |
| 1236 | if (error == GLR_CANCELED) { |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 1237 | msleep(100); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1238 | gh->gh_flags &= ~GL_ASYNC; |
| 1239 | error = gfs2_glock_nq(gh); |
| 1240 | } |
| 1241 | |
| 1242 | return error; |
| 1243 | } |
| 1244 | |
| 1245 | /** |
| 1246 | * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock) |
| 1247 | * @gh: the glock holder |
| 1248 | * |
| 1249 | */ |
| 1250 | |
| 1251 | void gfs2_glock_dq(struct gfs2_holder *gh) |
| 1252 | { |
| 1253 | struct gfs2_glock *gl = gh->gh_gl; |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 1254 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1255 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1256 | if (gh->gh_flags & GL_NOCACHE) |
| 1257 | handle_callback(gl, LM_ST_UNLOCKED); |
| 1258 | |
| 1259 | gfs2_glmutex_lock(gl); |
| 1260 | |
| 1261 | spin_lock(&gl->gl_spin); |
| 1262 | list_del_init(&gh->gh_list); |
| 1263 | |
| 1264 | if (list_empty(&gl->gl_holders)) { |
| 1265 | spin_unlock(&gl->gl_spin); |
| 1266 | |
| 1267 | if (glops->go_unlock) |
| 1268 | glops->go_unlock(gh); |
| 1269 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1270 | gl->gl_stamp = jiffies; |
| 1271 | |
| 1272 | spin_lock(&gl->gl_spin); |
| 1273 | } |
| 1274 | |
| 1275 | clear_bit(GLF_LOCK, &gl->gl_flags); |
| 1276 | run_queue(gl); |
| 1277 | spin_unlock(&gl->gl_spin); |
| 1278 | } |
| 1279 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1280 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1281 | * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it |
| 1282 | * @gh: the holder structure |
| 1283 | * |
| 1284 | */ |
| 1285 | |
| 1286 | void gfs2_glock_dq_uninit(struct gfs2_holder *gh) |
| 1287 | { |
| 1288 | gfs2_glock_dq(gh); |
| 1289 | gfs2_holder_uninit(gh); |
| 1290 | } |
| 1291 | |
| 1292 | /** |
| 1293 | * gfs2_glock_nq_num - acquire a glock based on lock number |
| 1294 | * @sdp: the filesystem |
| 1295 | * @number: the lock number |
| 1296 | * @glops: the glock operations for the type of glock |
| 1297 | * @state: the state to acquire the glock in |
| 1298 | * @flags: modifier flags for the aquisition |
| 1299 | * @gh: the struct gfs2_holder |
| 1300 | * |
| 1301 | * Returns: errno |
| 1302 | */ |
| 1303 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1304 | int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number, |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 1305 | const struct gfs2_glock_operations *glops, |
| 1306 | unsigned int state, int flags, struct gfs2_holder *gh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1307 | { |
| 1308 | struct gfs2_glock *gl; |
| 1309 | int error; |
| 1310 | |
| 1311 | error = gfs2_glock_get(sdp, number, glops, CREATE, &gl); |
| 1312 | if (!error) { |
| 1313 | error = gfs2_glock_nq_init(gl, state, flags, gh); |
| 1314 | gfs2_glock_put(gl); |
| 1315 | } |
| 1316 | |
| 1317 | return error; |
| 1318 | } |
| 1319 | |
| 1320 | /** |
| 1321 | * glock_compare - Compare two struct gfs2_glock structures for sorting |
| 1322 | * @arg_a: the first structure |
| 1323 | * @arg_b: the second structure |
| 1324 | * |
| 1325 | */ |
| 1326 | |
| 1327 | static int glock_compare(const void *arg_a, const void *arg_b) |
| 1328 | { |
Steven Whitehouse | a5e08a9 | 2006-09-09 17:07:05 -0400 | [diff] [blame] | 1329 | const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a; |
| 1330 | const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b; |
| 1331 | const struct lm_lockname *a = &gh_a->gh_gl->gl_name; |
| 1332 | const struct lm_lockname *b = &gh_b->gh_gl->gl_name; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1333 | |
| 1334 | if (a->ln_number > b->ln_number) |
Steven Whitehouse | a5e08a9 | 2006-09-09 17:07:05 -0400 | [diff] [blame] | 1335 | return 1; |
| 1336 | if (a->ln_number < b->ln_number) |
| 1337 | return -1; |
Steven Whitehouse | 1c0f487 | 2007-01-22 12:10:39 -0500 | [diff] [blame] | 1338 | BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type); |
Steven Whitehouse | a5e08a9 | 2006-09-09 17:07:05 -0400 | [diff] [blame] | 1339 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * nq_m_sync - synchonously acquire more than one glock in deadlock free order |
| 1344 | * @num_gh: the number of structures |
| 1345 | * @ghs: an array of struct gfs2_holder structures |
| 1346 | * |
| 1347 | * Returns: 0 on success (all glocks acquired), |
| 1348 | * errno on failure (no glocks acquired) |
| 1349 | */ |
| 1350 | |
| 1351 | static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs, |
| 1352 | struct gfs2_holder **p) |
| 1353 | { |
| 1354 | unsigned int x; |
| 1355 | int error = 0; |
| 1356 | |
| 1357 | for (x = 0; x < num_gh; x++) |
| 1358 | p[x] = &ghs[x]; |
| 1359 | |
| 1360 | sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL); |
| 1361 | |
| 1362 | for (x = 0; x < num_gh; x++) { |
| 1363 | p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1364 | |
| 1365 | error = gfs2_glock_nq(p[x]); |
| 1366 | if (error) { |
| 1367 | while (x--) |
| 1368 | gfs2_glock_dq(p[x]); |
| 1369 | break; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | return error; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * gfs2_glock_nq_m - acquire multiple glocks |
| 1378 | * @num_gh: the number of structures |
| 1379 | * @ghs: an array of struct gfs2_holder structures |
| 1380 | * |
| 1381 | * Figure out how big an impact this function has. Either: |
| 1382 | * 1) Replace this code with code that calls gfs2_glock_prefetch() |
| 1383 | * 2) Forget async stuff and just call nq_m_sync() |
| 1384 | * 3) Leave it like it is |
| 1385 | * |
| 1386 | * Returns: 0 on success (all glocks acquired), |
| 1387 | * errno on failure (no glocks acquired) |
| 1388 | */ |
| 1389 | |
| 1390 | int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1391 | { |
| 1392 | int *e; |
| 1393 | unsigned int x; |
| 1394 | int borked = 0, serious = 0; |
| 1395 | int error = 0; |
| 1396 | |
| 1397 | if (!num_gh) |
| 1398 | return 0; |
| 1399 | |
| 1400 | if (num_gh == 1) { |
| 1401 | ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1402 | return gfs2_glock_nq(ghs); |
| 1403 | } |
| 1404 | |
| 1405 | e = kcalloc(num_gh, sizeof(struct gfs2_holder *), GFP_KERNEL); |
| 1406 | if (!e) |
| 1407 | return -ENOMEM; |
| 1408 | |
| 1409 | for (x = 0; x < num_gh; x++) { |
| 1410 | ghs[x].gh_flags |= LM_FLAG_TRY | GL_ASYNC; |
| 1411 | error = gfs2_glock_nq(&ghs[x]); |
| 1412 | if (error) { |
| 1413 | borked = 1; |
| 1414 | serious = error; |
| 1415 | num_gh = x; |
| 1416 | break; |
| 1417 | } |
| 1418 | } |
| 1419 | |
| 1420 | for (x = 0; x < num_gh; x++) { |
| 1421 | error = e[x] = glock_wait_internal(&ghs[x]); |
| 1422 | if (error) { |
| 1423 | borked = 1; |
| 1424 | if (error != GLR_TRYFAILED && error != GLR_CANCELED) |
| 1425 | serious = error; |
| 1426 | } |
| 1427 | } |
| 1428 | |
| 1429 | if (!borked) { |
| 1430 | kfree(e); |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | for (x = 0; x < num_gh; x++) |
| 1435 | if (!e[x]) |
| 1436 | gfs2_glock_dq(&ghs[x]); |
| 1437 | |
| 1438 | if (serious) |
| 1439 | error = serious; |
| 1440 | else { |
| 1441 | for (x = 0; x < num_gh; x++) |
| 1442 | gfs2_holder_reinit(ghs[x].gh_state, ghs[x].gh_flags, |
| 1443 | &ghs[x]); |
| 1444 | error = nq_m_sync(num_gh, ghs, (struct gfs2_holder **)e); |
| 1445 | } |
| 1446 | |
| 1447 | kfree(e); |
| 1448 | |
| 1449 | return error; |
| 1450 | } |
| 1451 | |
| 1452 | /** |
| 1453 | * gfs2_glock_dq_m - release multiple glocks |
| 1454 | * @num_gh: the number of structures |
| 1455 | * @ghs: an array of struct gfs2_holder structures |
| 1456 | * |
| 1457 | */ |
| 1458 | |
| 1459 | void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1460 | { |
| 1461 | unsigned int x; |
| 1462 | |
| 1463 | for (x = 0; x < num_gh; x++) |
| 1464 | gfs2_glock_dq(&ghs[x]); |
| 1465 | } |
| 1466 | |
| 1467 | /** |
| 1468 | * gfs2_glock_dq_uninit_m - release multiple glocks |
| 1469 | * @num_gh: the number of structures |
| 1470 | * @ghs: an array of struct gfs2_holder structures |
| 1471 | * |
| 1472 | */ |
| 1473 | |
| 1474 | void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1475 | { |
| 1476 | unsigned int x; |
| 1477 | |
| 1478 | for (x = 0; x < num_gh; x++) |
| 1479 | gfs2_glock_dq_uninit(&ghs[x]); |
| 1480 | } |
| 1481 | |
| 1482 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1483 | * gfs2_lvb_hold - attach a LVB from a glock |
| 1484 | * @gl: The glock in question |
| 1485 | * |
| 1486 | */ |
| 1487 | |
| 1488 | int gfs2_lvb_hold(struct gfs2_glock *gl) |
| 1489 | { |
| 1490 | int error; |
| 1491 | |
| 1492 | gfs2_glmutex_lock(gl); |
| 1493 | |
| 1494 | if (!atomic_read(&gl->gl_lvb_count)) { |
| 1495 | error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb); |
| 1496 | if (error) { |
| 1497 | gfs2_glmutex_unlock(gl); |
| 1498 | return error; |
| 1499 | } |
| 1500 | gfs2_glock_hold(gl); |
| 1501 | } |
| 1502 | atomic_inc(&gl->gl_lvb_count); |
| 1503 | |
| 1504 | gfs2_glmutex_unlock(gl); |
| 1505 | |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | /** |
| 1510 | * gfs2_lvb_unhold - detach a LVB from a glock |
| 1511 | * @gl: The glock in question |
| 1512 | * |
| 1513 | */ |
| 1514 | |
| 1515 | void gfs2_lvb_unhold(struct gfs2_glock *gl) |
| 1516 | { |
| 1517 | gfs2_glock_hold(gl); |
| 1518 | gfs2_glmutex_lock(gl); |
| 1519 | |
| 1520 | gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0); |
| 1521 | if (atomic_dec_and_test(&gl->gl_lvb_count)) { |
| 1522 | gfs2_lm_unhold_lvb(gl->gl_sbd, gl->gl_lock, gl->gl_lvb); |
| 1523 | gl->gl_lvb = NULL; |
| 1524 | gfs2_glock_put(gl); |
| 1525 | } |
| 1526 | |
| 1527 | gfs2_glmutex_unlock(gl); |
| 1528 | gfs2_glock_put(gl); |
| 1529 | } |
| 1530 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1531 | static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name, |
| 1532 | unsigned int state) |
| 1533 | { |
| 1534 | struct gfs2_glock *gl; |
| 1535 | |
| 1536 | gl = gfs2_glock_find(sdp, name); |
| 1537 | if (!gl) |
| 1538 | return; |
| 1539 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1540 | handle_callback(gl, state); |
| 1541 | |
| 1542 | spin_lock(&gl->gl_spin); |
| 1543 | run_queue(gl); |
| 1544 | spin_unlock(&gl->gl_spin); |
| 1545 | |
| 1546 | gfs2_glock_put(gl); |
| 1547 | } |
| 1548 | |
| 1549 | /** |
| 1550 | * gfs2_glock_cb - Callback used by locking module |
Steven Whitehouse | 1c089c3 | 2006-09-07 15:50:20 -0400 | [diff] [blame] | 1551 | * @sdp: Pointer to the superblock |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1552 | * @type: Type of callback |
| 1553 | * @data: Type dependent data pointer |
| 1554 | * |
| 1555 | * Called by the locking module when it wants to tell us something. |
| 1556 | * Either we need to drop a lock, one of our ASYNC requests completed, or |
| 1557 | * a journal from another client needs to be recovered. |
| 1558 | */ |
| 1559 | |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 1560 | void gfs2_glock_cb(void *cb_data, unsigned int type, void *data) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1561 | { |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 1562 | struct gfs2_sbd *sdp = cb_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1563 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1564 | switch (type) { |
| 1565 | case LM_CB_NEED_E: |
David Teigland | e7f5c01 | 2006-04-27 11:25:45 -0400 | [diff] [blame] | 1566 | blocking_cb(sdp, data, LM_ST_UNLOCKED); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1567 | return; |
| 1568 | |
| 1569 | case LM_CB_NEED_D: |
David Teigland | e7f5c01 | 2006-04-27 11:25:45 -0400 | [diff] [blame] | 1570 | blocking_cb(sdp, data, LM_ST_DEFERRED); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1571 | return; |
| 1572 | |
| 1573 | case LM_CB_NEED_S: |
David Teigland | e7f5c01 | 2006-04-27 11:25:45 -0400 | [diff] [blame] | 1574 | blocking_cb(sdp, data, LM_ST_SHARED); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1575 | return; |
| 1576 | |
| 1577 | case LM_CB_ASYNC: { |
David Teigland | e7f5c01 | 2006-04-27 11:25:45 -0400 | [diff] [blame] | 1578 | struct lm_async_cb *async = data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1579 | struct gfs2_glock *gl; |
| 1580 | |
| 1581 | gl = gfs2_glock_find(sdp, &async->lc_name); |
| 1582 | if (gfs2_assert_warn(sdp, gl)) |
| 1583 | return; |
| 1584 | if (!gfs2_assert_warn(sdp, gl->gl_req_bh)) |
| 1585 | gl->gl_req_bh(gl, async->lc_ret); |
| 1586 | gfs2_glock_put(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1587 | return; |
| 1588 | } |
| 1589 | |
| 1590 | case LM_CB_NEED_RECOVERY: |
| 1591 | gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data); |
| 1592 | if (sdp->sd_recoverd_process) |
| 1593 | wake_up_process(sdp->sd_recoverd_process); |
| 1594 | return; |
| 1595 | |
| 1596 | case LM_CB_DROPLOCKS: |
| 1597 | gfs2_gl_hash_clear(sdp, NO_WAIT); |
| 1598 | gfs2_quota_scan(sdp); |
| 1599 | return; |
| 1600 | |
| 1601 | default: |
| 1602 | gfs2_assert_warn(sdp, 0); |
| 1603 | return; |
| 1604 | } |
| 1605 | } |
| 1606 | |
| 1607 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1608 | * demote_ok - Check to see if it's ok to unlock a glock |
| 1609 | * @gl: the glock |
| 1610 | * |
| 1611 | * Returns: 1 if it's ok |
| 1612 | */ |
| 1613 | |
| 1614 | static int demote_ok(struct gfs2_glock *gl) |
| 1615 | { |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 1616 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1617 | int demote = 1; |
| 1618 | |
| 1619 | if (test_bit(GLF_STICKY, &gl->gl_flags)) |
| 1620 | demote = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1621 | else if (glops->go_demote_ok) |
| 1622 | demote = glops->go_demote_ok(gl); |
| 1623 | |
| 1624 | return demote; |
| 1625 | } |
| 1626 | |
| 1627 | /** |
| 1628 | * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list |
| 1629 | * @gl: the glock |
| 1630 | * |
| 1631 | */ |
| 1632 | |
| 1633 | void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl) |
| 1634 | { |
| 1635 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1636 | |
| 1637 | spin_lock(&sdp->sd_reclaim_lock); |
| 1638 | if (list_empty(&gl->gl_reclaim)) { |
| 1639 | gfs2_glock_hold(gl); |
| 1640 | list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list); |
| 1641 | atomic_inc(&sdp->sd_reclaim_count); |
| 1642 | } |
| 1643 | spin_unlock(&sdp->sd_reclaim_lock); |
| 1644 | |
| 1645 | wake_up(&sdp->sd_reclaim_wq); |
| 1646 | } |
| 1647 | |
| 1648 | /** |
| 1649 | * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list |
| 1650 | * @sdp: the filesystem |
| 1651 | * |
| 1652 | * Called from gfs2_glockd() glock reclaim daemon, or when promoting a |
| 1653 | * different glock and we notice that there are a lot of glocks in the |
| 1654 | * reclaim list. |
| 1655 | * |
| 1656 | */ |
| 1657 | |
| 1658 | void gfs2_reclaim_glock(struct gfs2_sbd *sdp) |
| 1659 | { |
| 1660 | struct gfs2_glock *gl; |
| 1661 | |
| 1662 | spin_lock(&sdp->sd_reclaim_lock); |
| 1663 | if (list_empty(&sdp->sd_reclaim_list)) { |
| 1664 | spin_unlock(&sdp->sd_reclaim_lock); |
| 1665 | return; |
| 1666 | } |
| 1667 | gl = list_entry(sdp->sd_reclaim_list.next, |
| 1668 | struct gfs2_glock, gl_reclaim); |
| 1669 | list_del_init(&gl->gl_reclaim); |
| 1670 | spin_unlock(&sdp->sd_reclaim_lock); |
| 1671 | |
| 1672 | atomic_dec(&sdp->sd_reclaim_count); |
| 1673 | atomic_inc(&sdp->sd_reclaimed); |
| 1674 | |
| 1675 | if (gfs2_glmutex_trylock(gl)) { |
Steven Whitehouse | 1213293 | 2007-01-22 13:09:04 -0500 | [diff] [blame^] | 1676 | if (list_empty(&gl->gl_holders) && |
Steven Whitehouse | 5029996 | 2006-09-04 09:49:55 -0400 | [diff] [blame] | 1677 | gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1678 | handle_callback(gl, LM_ST_UNLOCKED); |
| 1679 | gfs2_glmutex_unlock(gl); |
| 1680 | } |
| 1681 | |
| 1682 | gfs2_glock_put(gl); |
| 1683 | } |
| 1684 | |
| 1685 | /** |
| 1686 | * examine_bucket - Call a function for glock in a hash bucket |
| 1687 | * @examiner: the function |
| 1688 | * @sdp: the filesystem |
| 1689 | * @bucket: the bucket |
| 1690 | * |
| 1691 | * Returns: 1 if the bucket has entries |
| 1692 | */ |
| 1693 | |
| 1694 | static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp, |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 1695 | unsigned int hash) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1696 | { |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1697 | struct gfs2_glock *gl, *prev = NULL; |
| 1698 | int has_entries = 0; |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1699 | struct hlist_head *head = &gl_hash_table[hash].hb_list; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1700 | |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1701 | read_lock(gl_lock_addr(hash)); |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1702 | /* Can't use hlist_for_each_entry - don't want prefetch here */ |
| 1703 | if (hlist_empty(head)) |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1704 | goto out; |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1705 | gl = list_entry(head->first, struct gfs2_glock, gl_list); |
| 1706 | while(1) { |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1707 | if (gl->gl_sbd == sdp) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1708 | gfs2_glock_hold(gl); |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1709 | read_unlock(gl_lock_addr(hash)); |
| 1710 | if (prev) |
| 1711 | gfs2_glock_put(prev); |
| 1712 | prev = gl; |
| 1713 | examiner(gl); |
Steven Whitehouse | a833634 | 2006-09-14 13:57:38 -0400 | [diff] [blame] | 1714 | has_entries = 1; |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1715 | read_lock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1716 | } |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1717 | if (gl->gl_list.next == NULL) |
| 1718 | break; |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1719 | gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1720 | } |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1721 | out: |
| 1722 | read_unlock(gl_lock_addr(hash)); |
| 1723 | if (prev) |
| 1724 | gfs2_glock_put(prev); |
| 1725 | return has_entries; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /** |
| 1729 | * scan_glock - look at a glock and see if we can reclaim it |
| 1730 | * @gl: the glock to look at |
| 1731 | * |
| 1732 | */ |
| 1733 | |
| 1734 | static void scan_glock(struct gfs2_glock *gl) |
| 1735 | { |
Steven Whitehouse | b004157 | 2006-11-23 10:51:34 -0500 | [diff] [blame] | 1736 | if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1737 | return; |
Steven Whitehouse | a2242db | 2006-08-24 17:03:05 -0400 | [diff] [blame] | 1738 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1739 | if (gfs2_glmutex_trylock(gl)) { |
Steven Whitehouse | 1213293 | 2007-01-22 13:09:04 -0500 | [diff] [blame^] | 1740 | if (list_empty(&gl->gl_holders) && |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1741 | gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1742 | goto out_schedule; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1743 | gfs2_glmutex_unlock(gl); |
| 1744 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1745 | return; |
| 1746 | |
Steven Whitehouse | 627add2 | 2006-07-05 13:16:19 -0400 | [diff] [blame] | 1747 | out_schedule: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1748 | gfs2_glmutex_unlock(gl); |
| 1749 | gfs2_glock_schedule_for_reclaim(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | /** |
| 1753 | * gfs2_scand_internal - Look for glocks and inodes to toss from memory |
| 1754 | * @sdp: the filesystem |
| 1755 | * |
| 1756 | */ |
| 1757 | |
| 1758 | void gfs2_scand_internal(struct gfs2_sbd *sdp) |
| 1759 | { |
| 1760 | unsigned int x; |
| 1761 | |
Steven Whitehouse | 9461061 | 2006-09-09 18:59:27 -0400 | [diff] [blame] | 1762 | for (x = 0; x < GFS2_GL_HASH_SIZE; x++) |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 1763 | examine_bucket(scan_glock, sdp, x); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | /** |
| 1767 | * clear_glock - look at a glock and see if we can free it from glock cache |
| 1768 | * @gl: the glock to look at |
| 1769 | * |
| 1770 | */ |
| 1771 | |
| 1772 | static void clear_glock(struct gfs2_glock *gl) |
| 1773 | { |
| 1774 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1775 | int released; |
| 1776 | |
| 1777 | spin_lock(&sdp->sd_reclaim_lock); |
| 1778 | if (!list_empty(&gl->gl_reclaim)) { |
| 1779 | list_del_init(&gl->gl_reclaim); |
| 1780 | atomic_dec(&sdp->sd_reclaim_count); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 1781 | spin_unlock(&sdp->sd_reclaim_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1782 | released = gfs2_glock_put(gl); |
| 1783 | gfs2_assert(sdp, !released); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 1784 | } else { |
| 1785 | spin_unlock(&sdp->sd_reclaim_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1786 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1787 | |
| 1788 | if (gfs2_glmutex_trylock(gl)) { |
Steven Whitehouse | 1213293 | 2007-01-22 13:09:04 -0500 | [diff] [blame^] | 1789 | if (list_empty(gl, &gl->gl_holders) && |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1790 | gl->gl_state != LM_ST_UNLOCKED) |
| 1791 | handle_callback(gl, LM_ST_UNLOCKED); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1792 | gfs2_glmutex_unlock(gl); |
| 1793 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
| 1796 | /** |
| 1797 | * gfs2_gl_hash_clear - Empty out the glock hash table |
| 1798 | * @sdp: the filesystem |
| 1799 | * @wait: wait until it's all gone |
| 1800 | * |
| 1801 | * Called when unmounting the filesystem, or when inter-node lock manager |
| 1802 | * requests DROPLOCKS because it is running out of capacity. |
| 1803 | */ |
| 1804 | |
| 1805 | void gfs2_gl_hash_clear(struct gfs2_sbd *sdp, int wait) |
| 1806 | { |
| 1807 | unsigned long t; |
| 1808 | unsigned int x; |
| 1809 | int cont; |
| 1810 | |
| 1811 | t = jiffies; |
| 1812 | |
| 1813 | for (;;) { |
| 1814 | cont = 0; |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1815 | for (x = 0; x < GFS2_GL_HASH_SIZE; x++) { |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 1816 | if (examine_bucket(clear_glock, sdp, x)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1817 | cont = 1; |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1818 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1819 | |
| 1820 | if (!wait || !cont) |
| 1821 | break; |
| 1822 | |
| 1823 | if (time_after_eq(jiffies, |
| 1824 | t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) { |
| 1825 | fs_warn(sdp, "Unmount seems to be stalled. " |
| 1826 | "Dumping lock state...\n"); |
| 1827 | gfs2_dump_lockstate(sdp); |
| 1828 | t = jiffies; |
| 1829 | } |
| 1830 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1831 | invalidate_inodes(sdp->sd_vfs); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 1832 | msleep(10); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1833 | } |
| 1834 | } |
| 1835 | |
| 1836 | /* |
| 1837 | * Diagnostic routines to help debug distributed deadlock |
| 1838 | */ |
| 1839 | |
| 1840 | /** |
| 1841 | * dump_holder - print information about a glock holder |
| 1842 | * @str: a string naming the type of holder |
| 1843 | * @gh: the glock holder |
| 1844 | * |
| 1845 | * Returns: 0 on success, -ENOBUFS when we run out of space |
| 1846 | */ |
| 1847 | |
| 1848 | static int dump_holder(char *str, struct gfs2_holder *gh) |
| 1849 | { |
| 1850 | unsigned int x; |
| 1851 | int error = -ENOBUFS; |
| 1852 | |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1853 | printk(KERN_INFO " %s\n", str); |
| 1854 | printk(KERN_INFO " owner = %ld\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1855 | (gh->gh_owner) ? (long)gh->gh_owner->pid : -1); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1856 | printk(KERN_INFO " gh_state = %u\n", gh->gh_state); |
| 1857 | printk(KERN_INFO " gh_flags ="); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1858 | for (x = 0; x < 32; x++) |
| 1859 | if (gh->gh_flags & (1 << x)) |
| 1860 | printk(" %u", x); |
| 1861 | printk(" \n"); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1862 | printk(KERN_INFO " error = %d\n", gh->gh_error); |
| 1863 | printk(KERN_INFO " gh_iflags ="); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1864 | for (x = 0; x < 32; x++) |
| 1865 | if (test_bit(x, &gh->gh_iflags)) |
| 1866 | printk(" %u", x); |
| 1867 | printk(" \n"); |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 1868 | print_symbol(KERN_INFO " initialized at: %s\n", gh->gh_ip); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1869 | |
| 1870 | error = 0; |
| 1871 | |
| 1872 | return error; |
| 1873 | } |
| 1874 | |
| 1875 | /** |
| 1876 | * dump_inode - print information about an inode |
| 1877 | * @ip: the inode |
| 1878 | * |
| 1879 | * Returns: 0 on success, -ENOBUFS when we run out of space |
| 1880 | */ |
| 1881 | |
| 1882 | static int dump_inode(struct gfs2_inode *ip) |
| 1883 | { |
| 1884 | unsigned int x; |
| 1885 | int error = -ENOBUFS; |
| 1886 | |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1887 | printk(KERN_INFO " Inode:\n"); |
| 1888 | printk(KERN_INFO " num = %llu %llu\n", |
Steven Whitehouse | 382066d | 2006-05-24 10:22:09 -0400 | [diff] [blame] | 1889 | (unsigned long long)ip->i_num.no_formal_ino, |
| 1890 | (unsigned long long)ip->i_num.no_addr); |
Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 1891 | printk(KERN_INFO " type = %u\n", IF2DT(ip->i_inode.i_mode)); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1892 | printk(KERN_INFO " i_flags ="); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1893 | for (x = 0; x < 32; x++) |
| 1894 | if (test_bit(x, &ip->i_flags)) |
| 1895 | printk(" %u", x); |
| 1896 | printk(" \n"); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1897 | |
| 1898 | error = 0; |
| 1899 | |
| 1900 | return error; |
| 1901 | } |
| 1902 | |
| 1903 | /** |
| 1904 | * dump_glock - print information about a glock |
| 1905 | * @gl: the glock |
| 1906 | * @count: where we are in the buffer |
| 1907 | * |
| 1908 | * Returns: 0 on success, -ENOBUFS when we run out of space |
| 1909 | */ |
| 1910 | |
| 1911 | static int dump_glock(struct gfs2_glock *gl) |
| 1912 | { |
| 1913 | struct gfs2_holder *gh; |
| 1914 | unsigned int x; |
| 1915 | int error = -ENOBUFS; |
| 1916 | |
| 1917 | spin_lock(&gl->gl_spin); |
| 1918 | |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 1919 | printk(KERN_INFO "Glock 0x%p (%u, %llu)\n", gl, gl->gl_name.ln_type, |
Steven Whitehouse | 382066d | 2006-05-24 10:22:09 -0400 | [diff] [blame] | 1920 | (unsigned long long)gl->gl_name.ln_number); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1921 | printk(KERN_INFO " gl_flags ="); |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 1922 | for (x = 0; x < 32; x++) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1923 | if (test_bit(x, &gl->gl_flags)) |
| 1924 | printk(" %u", x); |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 1925 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1926 | printk(" \n"); |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 1927 | printk(KERN_INFO " gl_ref = %d\n", atomic_read(&gl->gl_ref)); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1928 | printk(KERN_INFO " gl_state = %u\n", gl->gl_state); |
Steven Whitehouse | 320dd10 | 2006-05-18 16:25:27 -0400 | [diff] [blame] | 1929 | printk(KERN_INFO " gl_owner = %s\n", gl->gl_owner->comm); |
| 1930 | print_symbol(KERN_INFO " gl_ip = %s\n", gl->gl_ip); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1931 | printk(KERN_INFO " req_gh = %s\n", (gl->gl_req_gh) ? "yes" : "no"); |
| 1932 | printk(KERN_INFO " req_bh = %s\n", (gl->gl_req_bh) ? "yes" : "no"); |
| 1933 | printk(KERN_INFO " lvb_count = %d\n", atomic_read(&gl->gl_lvb_count)); |
| 1934 | printk(KERN_INFO " object = %s\n", (gl->gl_object) ? "yes" : "no"); |
| 1935 | printk(KERN_INFO " le = %s\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1936 | (list_empty(&gl->gl_le.le_list)) ? "no" : "yes"); |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1937 | printk(KERN_INFO " reclaim = %s\n", |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1938 | (list_empty(&gl->gl_reclaim)) ? "no" : "yes"); |
| 1939 | if (gl->gl_aspace) |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 1940 | printk(KERN_INFO " aspace = 0x%p nrpages = %lu\n", gl->gl_aspace, |
Russell Cattelan | 8872187 | 2006-08-10 11:08:40 -0500 | [diff] [blame] | 1941 | gl->gl_aspace->i_mapping->nrpages); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1942 | else |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1943 | printk(KERN_INFO " aspace = no\n"); |
| 1944 | printk(KERN_INFO " ail = %d\n", atomic_read(&gl->gl_ail_count)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1945 | if (gl->gl_req_gh) { |
| 1946 | error = dump_holder("Request", gl->gl_req_gh); |
| 1947 | if (error) |
| 1948 | goto out; |
| 1949 | } |
| 1950 | list_for_each_entry(gh, &gl->gl_holders, gh_list) { |
| 1951 | error = dump_holder("Holder", gh); |
| 1952 | if (error) |
| 1953 | goto out; |
| 1954 | } |
| 1955 | list_for_each_entry(gh, &gl->gl_waiters1, gh_list) { |
| 1956 | error = dump_holder("Waiter1", gh); |
| 1957 | if (error) |
| 1958 | goto out; |
| 1959 | } |
| 1960 | list_for_each_entry(gh, &gl->gl_waiters2, gh_list) { |
| 1961 | error = dump_holder("Waiter2", gh); |
| 1962 | if (error) |
| 1963 | goto out; |
| 1964 | } |
| 1965 | list_for_each_entry(gh, &gl->gl_waiters3, gh_list) { |
| 1966 | error = dump_holder("Waiter3", gh); |
| 1967 | if (error) |
| 1968 | goto out; |
| 1969 | } |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1970 | if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1971 | if (!test_bit(GLF_LOCK, &gl->gl_flags) && |
| 1972 | list_empty(&gl->gl_holders)) { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 1973 | error = dump_inode(gl->gl_object); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1974 | if (error) |
| 1975 | goto out; |
| 1976 | } else { |
| 1977 | error = -ENOBUFS; |
Steven Whitehouse | d92a8d4 | 2006-02-27 10:57:14 -0500 | [diff] [blame] | 1978 | printk(KERN_INFO " Inode: busy\n"); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1979 | } |
| 1980 | } |
| 1981 | |
| 1982 | error = 0; |
| 1983 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1984 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1985 | spin_unlock(&gl->gl_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1986 | return error; |
| 1987 | } |
| 1988 | |
| 1989 | /** |
| 1990 | * gfs2_dump_lockstate - print out the current lockstate |
| 1991 | * @sdp: the filesystem |
| 1992 | * @ub: the buffer to copy the information into |
| 1993 | * |
| 1994 | * If @ub is NULL, dump the lockstate to the console. |
| 1995 | * |
| 1996 | */ |
| 1997 | |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 1998 | static int gfs2_dump_lockstate(struct gfs2_sbd *sdp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1999 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2000 | struct gfs2_glock *gl; |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 2001 | struct hlist_node *h; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2002 | unsigned int x; |
| 2003 | int error = 0; |
| 2004 | |
| 2005 | for (x = 0; x < GFS2_GL_HASH_SIZE; x++) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2006 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 2007 | read_lock(gl_lock_addr(x)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2008 | |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 2009 | hlist_for_each_entry(gl, h, &gl_hash_table[x].hb_list, gl_list) { |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 2010 | if (gl->gl_sbd != sdp) |
| 2011 | continue; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2012 | |
| 2013 | error = dump_glock(gl); |
| 2014 | if (error) |
| 2015 | break; |
| 2016 | } |
| 2017 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 2018 | read_unlock(gl_lock_addr(x)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2019 | |
| 2020 | if (error) |
| 2021 | break; |
| 2022 | } |
| 2023 | |
| 2024 | |
| 2025 | return error; |
| 2026 | } |
| 2027 | |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 2028 | int __init gfs2_glock_init(void) |
| 2029 | { |
| 2030 | unsigned i; |
| 2031 | for(i = 0; i < GFS2_GL_HASH_SIZE; i++) { |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 2032 | INIT_HLIST_HEAD(&gl_hash_table[i].hb_list); |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 2033 | } |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 2034 | #ifdef GL_HASH_LOCK_SZ |
| 2035 | for(i = 0; i < GL_HASH_LOCK_SZ; i++) { |
| 2036 | rwlock_init(&gl_hash_locks[i]); |
| 2037 | } |
| 2038 | #endif |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 2039 | return 0; |
| 2040 | } |
| 2041 | |