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