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