David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Bob Peterson | cf45b75 | 2008-01-31 10:31:39 -0600 | [diff] [blame] | 3 | * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 | * of the GNU General Public License version 2. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/sort.h> |
| 17 | #include <linux/jhash.h> |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 18 | #include <linux/kallsyms.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 20 | #include <linux/list.h> |
Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 21 | #include <linux/lm_interface.h> |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 22 | #include <linux/wait.h> |
akpm@linux-foundation.org | 95d97b7 | 2007-03-05 23:10:39 -0800 | [diff] [blame] | 23 | #include <linux/module.h> |
Steven Whitehouse | 61be084 | 2007-01-29 11:51:45 +0000 | [diff] [blame] | 24 | #include <linux/rwsem.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 25 | #include <asm/uaccess.h> |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 26 | #include <linux/seq_file.h> |
| 27 | #include <linux/debugfs.h> |
Steven Whitehouse | 8fbbfd2 | 2007-08-01 13:57:10 +0100 | [diff] [blame] | 28 | #include <linux/kthread.h> |
| 29 | #include <linux/freezer.h> |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 30 | #include <linux/workqueue.h> |
| 31 | #include <linux/jiffies.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 32 | |
| 33 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 34 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 35 | #include "glock.h" |
| 36 | #include "glops.h" |
| 37 | #include "inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 38 | #include "lops.h" |
| 39 | #include "meta_io.h" |
| 40 | #include "quota.h" |
| 41 | #include "super.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 42 | #include "util.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 43 | |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 44 | struct gfs2_gl_hash_bucket { |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 45 | struct hlist_head hb_list; |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 46 | }; |
| 47 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 48 | struct gfs2_glock_iter { |
| 49 | int hash; /* hash bucket index */ |
| 50 | struct gfs2_sbd *sdp; /* incore superblock */ |
| 51 | struct gfs2_glock *gl; /* current glock struct */ |
| 52 | char string[512]; /* scratch space */ |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 55 | typedef void (*glock_examiner) (struct gfs2_glock * gl); |
| 56 | |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 57 | static int gfs2_dump_lockstate(struct gfs2_sbd *sdp); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 58 | static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl); |
| 59 | #define GLOCK_BUG_ON(gl,x) do { if (unlikely(x)) { __dump_glock(NULL, gl); BUG(); } } while(0) |
| 60 | static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target); |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 61 | |
Steven Whitehouse | 61be084 | 2007-01-29 11:51:45 +0000 | [diff] [blame] | 62 | static DECLARE_RWSEM(gfs2_umount_flush_sem); |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 63 | static struct dentry *gfs2_root; |
Steven Whitehouse | 8fbbfd2 | 2007-08-01 13:57:10 +0100 | [diff] [blame] | 64 | static struct task_struct *scand_process; |
| 65 | static unsigned int scand_secs = 5; |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 66 | static struct workqueue_struct *glock_workqueue; |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 67 | |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 68 | #define GFS2_GL_HASH_SHIFT 15 |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 69 | #define GFS2_GL_HASH_SIZE (1 << GFS2_GL_HASH_SHIFT) |
| 70 | #define GFS2_GL_HASH_MASK (GFS2_GL_HASH_SIZE - 1) |
| 71 | |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 72 | static struct gfs2_gl_hash_bucket gl_hash_table[GFS2_GL_HASH_SIZE]; |
Robert Peterson | 04b933f | 2007-03-23 17:05:15 -0500 | [diff] [blame] | 73 | static struct dentry *gfs2_root; |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 74 | |
| 75 | /* |
| 76 | * Despite what you might think, the numbers below are not arbitrary :-) |
| 77 | * They are taken from the ipv4 routing hash code, which is well tested |
| 78 | * and thus should be nearly optimal. Later on we might tweek the numbers |
| 79 | * but for now this should be fine. |
| 80 | * |
| 81 | * The reason for putting the locks in a separate array from the list heads |
| 82 | * is that we can have fewer locks than list heads and save memory. We use |
| 83 | * the same hash function for both, but with a different hash mask. |
| 84 | */ |
| 85 | #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK) || \ |
| 86 | defined(CONFIG_PROVE_LOCKING) |
| 87 | |
| 88 | #ifdef CONFIG_LOCKDEP |
| 89 | # define GL_HASH_LOCK_SZ 256 |
| 90 | #else |
| 91 | # if NR_CPUS >= 32 |
| 92 | # define GL_HASH_LOCK_SZ 4096 |
| 93 | # elif NR_CPUS >= 16 |
| 94 | # define GL_HASH_LOCK_SZ 2048 |
| 95 | # elif NR_CPUS >= 8 |
| 96 | # define GL_HASH_LOCK_SZ 1024 |
| 97 | # elif NR_CPUS >= 4 |
| 98 | # define GL_HASH_LOCK_SZ 512 |
| 99 | # else |
| 100 | # define GL_HASH_LOCK_SZ 256 |
| 101 | # endif |
| 102 | #endif |
| 103 | |
| 104 | /* We never want more locks than chains */ |
| 105 | #if GFS2_GL_HASH_SIZE < GL_HASH_LOCK_SZ |
| 106 | # undef GL_HASH_LOCK_SZ |
| 107 | # define GL_HASH_LOCK_SZ GFS2_GL_HASH_SIZE |
| 108 | #endif |
| 109 | |
| 110 | static rwlock_t gl_hash_locks[GL_HASH_LOCK_SZ]; |
| 111 | |
| 112 | static inline rwlock_t *gl_lock_addr(unsigned int x) |
| 113 | { |
Steven Whitehouse | 9461061 | 2006-09-09 18:59:27 -0400 | [diff] [blame] | 114 | return &gl_hash_locks[x & (GL_HASH_LOCK_SZ-1)]; |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 115 | } |
| 116 | #else /* not SMP, so no spinlocks required */ |
Randy Dunlap | 0ac2306 | 2006-11-28 22:29:19 -0800 | [diff] [blame] | 117 | static inline rwlock_t *gl_lock_addr(unsigned int x) |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 118 | { |
| 119 | return NULL; |
| 120 | } |
| 121 | #endif |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 122 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 123 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 124 | * gl_hash() - Turn glock number into hash bucket number |
| 125 | * @lock: The glock number |
| 126 | * |
| 127 | * Returns: The number of the corresponding hash bucket |
| 128 | */ |
| 129 | |
Steven Whitehouse | b854785 | 2006-09-07 13:12:27 -0400 | [diff] [blame] | 130 | static unsigned int gl_hash(const struct gfs2_sbd *sdp, |
| 131 | const struct lm_lockname *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 132 | { |
| 133 | unsigned int h; |
| 134 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 135 | h = jhash(&name->ln_number, sizeof(u64), 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 136 | h = jhash(&name->ln_type, sizeof(unsigned int), h); |
Steven Whitehouse | b854785 | 2006-09-07 13:12:27 -0400 | [diff] [blame] | 137 | h = jhash(&sdp, sizeof(struct gfs2_sbd *), h); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 138 | h &= GFS2_GL_HASH_MASK; |
| 139 | |
| 140 | return h; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * glock_free() - Perform a few checks and then release struct gfs2_glock |
| 145 | * @gl: The glock to release |
| 146 | * |
| 147 | * Also calls lock module to release its internal structure for this glock. |
| 148 | * |
| 149 | */ |
| 150 | |
| 151 | static void glock_free(struct gfs2_glock *gl) |
| 152 | { |
| 153 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 154 | struct inode *aspace = gl->gl_aspace; |
| 155 | |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 156 | if (sdp->sd_lockstruct.ls_ops->lm_put_lock) |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 157 | sdp->sd_lockstruct.ls_ops->lm_put_lock(gl->gl_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 158 | |
| 159 | if (aspace) |
| 160 | gfs2_aspace_put(aspace); |
| 161 | |
| 162 | kmem_cache_free(gfs2_glock_cachep, gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** |
| 166 | * gfs2_glock_hold() - increment reference count on glock |
| 167 | * @gl: The glock to hold |
| 168 | * |
| 169 | */ |
| 170 | |
Adrian Bunk | 048786f | 2008-01-29 00:11:34 +0200 | [diff] [blame] | 171 | static void gfs2_glock_hold(struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 172 | { |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 173 | atomic_inc(&gl->gl_ref); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * gfs2_glock_put() - Decrement reference count on glock |
| 178 | * @gl: The glock to put |
| 179 | * |
| 180 | */ |
| 181 | |
| 182 | int gfs2_glock_put(struct gfs2_glock *gl) |
| 183 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 184 | int rv = 0; |
| 185 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 186 | write_lock(gl_lock_addr(gl->gl_hash)); |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 187 | if (atomic_dec_and_test(&gl->gl_ref)) { |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 188 | hlist_del(&gl->gl_list); |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 189 | write_unlock(gl_lock_addr(gl->gl_hash)); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 190 | GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_UNLOCKED); |
| 191 | GLOCK_BUG_ON(gl, !list_empty(&gl->gl_reclaim)); |
| 192 | GLOCK_BUG_ON(gl, !list_empty(&gl->gl_holders)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 193 | glock_free(gl); |
| 194 | rv = 1; |
| 195 | goto out; |
| 196 | } |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 197 | write_unlock(gl_lock_addr(gl->gl_hash)); |
Steven Whitehouse | a2242db | 2006-08-24 17:03:05 -0400 | [diff] [blame] | 198 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 199 | return rv; |
| 200 | } |
| 201 | |
| 202 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 203 | * search_bucket() - Find struct gfs2_glock by lock number |
| 204 | * @bucket: the bucket to search |
| 205 | * @name: The lock name |
| 206 | * |
| 207 | * Returns: NULL, or the struct gfs2_glock with the requested number |
| 208 | */ |
| 209 | |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 210 | static struct gfs2_glock *search_bucket(unsigned int hash, |
Steven Whitehouse | 899be4d | 2006-08-30 12:50:28 -0400 | [diff] [blame] | 211 | const struct gfs2_sbd *sdp, |
Steven Whitehouse | d6a5372 | 2006-08-30 11:16:23 -0400 | [diff] [blame] | 212 | const struct lm_lockname *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 213 | { |
| 214 | struct gfs2_glock *gl; |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 215 | struct hlist_node *h; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 216 | |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 217 | 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] | 218 | if (!lm_name_equal(&gl->gl_name, name)) |
| 219 | continue; |
Steven Whitehouse | 899be4d | 2006-08-30 12:50:28 -0400 | [diff] [blame] | 220 | if (gl->gl_sbd != sdp) |
| 221 | continue; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 222 | |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 223 | atomic_inc(&gl->gl_ref); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 224 | |
| 225 | return gl; |
| 226 | } |
| 227 | |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * gfs2_glock_find() - Find glock by lock number |
| 233 | * @sdp: The GFS2 superblock |
| 234 | * @name: The lock name |
| 235 | * |
| 236 | * Returns: NULL, or the struct gfs2_glock with the requested number |
| 237 | */ |
| 238 | |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 239 | static struct gfs2_glock *gfs2_glock_find(const struct gfs2_sbd *sdp, |
Steven Whitehouse | d6a5372 | 2006-08-30 11:16:23 -0400 | [diff] [blame] | 240 | const struct lm_lockname *name) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 241 | { |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 242 | unsigned int hash = gl_hash(sdp, name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 243 | struct gfs2_glock *gl; |
| 244 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 245 | read_lock(gl_lock_addr(hash)); |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 246 | gl = search_bucket(hash, sdp, name); |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 247 | read_unlock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 248 | |
| 249 | return gl; |
| 250 | } |
| 251 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 252 | /** |
| 253 | * may_grant - check if its ok to grant a new lock |
| 254 | * @gl: The glock |
| 255 | * @gh: The lock request which we wish to grant |
| 256 | * |
| 257 | * Returns: true if its ok to grant the lock |
| 258 | */ |
| 259 | |
| 260 | static inline int may_grant(const struct gfs2_glock *gl, const struct gfs2_holder *gh) |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 261 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 262 | const struct gfs2_holder *gh_head = list_entry(gl->gl_holders.next, const struct gfs2_holder, gh_list); |
| 263 | if ((gh->gh_state == LM_ST_EXCLUSIVE || |
| 264 | gh_head->gh_state == LM_ST_EXCLUSIVE) && gh != gh_head) |
| 265 | return 0; |
| 266 | if (gl->gl_state == gh->gh_state) |
| 267 | return 1; |
| 268 | if (gh->gh_flags & GL_EXACT) |
| 269 | return 0; |
| 270 | if (gh->gh_state == LM_ST_SHARED && gl->gl_state == LM_ST_EXCLUSIVE) |
| 271 | return 1; |
| 272 | if (gl->gl_state != LM_ST_UNLOCKED && (gh->gh_flags & LM_FLAG_ANY)) |
| 273 | return 1; |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | static void gfs2_holder_wake(struct gfs2_holder *gh) |
| 278 | { |
| 279 | clear_bit(HIF_WAIT, &gh->gh_iflags); |
| 280 | smp_mb__after_clear_bit(); |
| 281 | wake_up_bit(&gh->gh_iflags, HIF_WAIT); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * do_promote - promote as many requests as possible on the current queue |
| 286 | * @gl: The glock |
| 287 | * |
| 288 | * Returns: true if there is a blocked holder at the head of the list |
| 289 | */ |
| 290 | |
| 291 | static int do_promote(struct gfs2_glock *gl) |
| 292 | { |
| 293 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
| 294 | struct gfs2_holder *gh, *tmp; |
| 295 | int ret; |
| 296 | |
| 297 | restart: |
| 298 | list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) { |
| 299 | if (test_bit(HIF_HOLDER, &gh->gh_iflags)) |
| 300 | continue; |
| 301 | if (may_grant(gl, gh)) { |
| 302 | if (gh->gh_list.prev == &gl->gl_holders && |
| 303 | glops->go_lock) { |
| 304 | spin_unlock(&gl->gl_spin); |
| 305 | /* FIXME: eliminate this eventually */ |
| 306 | ret = glops->go_lock(gh); |
| 307 | spin_lock(&gl->gl_spin); |
| 308 | if (ret) { |
| 309 | gh->gh_error = ret; |
| 310 | list_del_init(&gh->gh_list); |
| 311 | gfs2_holder_wake(gh); |
| 312 | goto restart; |
| 313 | } |
| 314 | set_bit(HIF_HOLDER, &gh->gh_iflags); |
| 315 | gfs2_holder_wake(gh); |
| 316 | goto restart; |
| 317 | } |
| 318 | set_bit(HIF_HOLDER, &gh->gh_iflags); |
| 319 | gfs2_holder_wake(gh); |
| 320 | continue; |
| 321 | } |
| 322 | if (gh->gh_list.prev == &gl->gl_holders) |
| 323 | return 1; |
| 324 | break; |
| 325 | } |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * do_error - Something unexpected has happened during a lock request |
| 331 | * |
| 332 | */ |
| 333 | |
| 334 | static inline void do_error(struct gfs2_glock *gl, const int ret) |
| 335 | { |
| 336 | struct gfs2_holder *gh, *tmp; |
| 337 | |
| 338 | list_for_each_entry_safe(gh, tmp, &gl->gl_holders, gh_list) { |
| 339 | if (test_bit(HIF_HOLDER, &gh->gh_iflags)) |
| 340 | continue; |
| 341 | if (ret & LM_OUT_ERROR) |
| 342 | gh->gh_error = -EIO; |
| 343 | else if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) |
| 344 | gh->gh_error = GLR_TRYFAILED; |
| 345 | else |
| 346 | continue; |
| 347 | list_del_init(&gh->gh_list); |
| 348 | gfs2_holder_wake(gh); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * find_first_waiter - find the first gh that's waiting for the glock |
| 354 | * @gl: the glock |
| 355 | */ |
| 356 | |
| 357 | static inline struct gfs2_holder *find_first_waiter(const struct gfs2_glock *gl) |
| 358 | { |
| 359 | struct gfs2_holder *gh; |
| 360 | |
| 361 | list_for_each_entry(gh, &gl->gl_holders, gh_list) { |
| 362 | if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) |
| 363 | return gh; |
| 364 | } |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * state_change - record that the glock is now in a different state |
| 370 | * @gl: the glock |
| 371 | * @new_state the new state |
| 372 | * |
| 373 | */ |
| 374 | |
| 375 | static void state_change(struct gfs2_glock *gl, unsigned int new_state) |
| 376 | { |
| 377 | int held1, held2; |
| 378 | |
| 379 | held1 = (gl->gl_state != LM_ST_UNLOCKED); |
| 380 | held2 = (new_state != LM_ST_UNLOCKED); |
| 381 | |
| 382 | if (held1 != held2) { |
| 383 | if (held2) |
| 384 | gfs2_glock_hold(gl); |
| 385 | else |
| 386 | gfs2_glock_put(gl); |
| 387 | } |
| 388 | |
| 389 | gl->gl_state = new_state; |
| 390 | gl->gl_tchange = jiffies; |
| 391 | } |
| 392 | |
| 393 | static void gfs2_demote_wake(struct gfs2_glock *gl) |
| 394 | { |
| 395 | gl->gl_demote_state = LM_ST_EXCLUSIVE; |
| 396 | clear_bit(GLF_DEMOTE, &gl->gl_flags); |
| 397 | smp_mb__after_clear_bit(); |
| 398 | wake_up_bit(&gl->gl_flags, GLF_DEMOTE); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * finish_xmote - The DLM has replied to one of our lock requests |
| 403 | * @gl: The glock |
| 404 | * @ret: The status from the DLM |
| 405 | * |
| 406 | */ |
| 407 | |
| 408 | static void finish_xmote(struct gfs2_glock *gl, unsigned int ret) |
| 409 | { |
| 410 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
| 411 | struct gfs2_holder *gh; |
| 412 | unsigned state = ret & LM_OUT_ST_MASK; |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 413 | |
| 414 | spin_lock(&gl->gl_spin); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 415 | state_change(gl, state); |
| 416 | gh = find_first_waiter(gl); |
| 417 | |
| 418 | /* Demote to UN request arrived during demote to SH or DF */ |
| 419 | if (test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags) && |
| 420 | state != LM_ST_UNLOCKED && gl->gl_demote_state == LM_ST_UNLOCKED) |
| 421 | gl->gl_target = LM_ST_UNLOCKED; |
| 422 | |
| 423 | /* Check for state != intended state */ |
| 424 | if (unlikely(state != gl->gl_target)) { |
| 425 | if (gh && !test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) { |
| 426 | /* move to back of queue and try next entry */ |
| 427 | if (ret & LM_OUT_CANCELED) { |
| 428 | if ((gh->gh_flags & LM_FLAG_PRIORITY) == 0) |
| 429 | list_move_tail(&gh->gh_list, &gl->gl_holders); |
| 430 | gh = find_first_waiter(gl); |
| 431 | gl->gl_target = gh->gh_state; |
| 432 | goto retry; |
| 433 | } |
| 434 | /* Some error or failed "try lock" - report it */ |
| 435 | if ((ret & LM_OUT_ERROR) || |
| 436 | (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) { |
| 437 | gl->gl_target = gl->gl_state; |
| 438 | do_error(gl, ret); |
| 439 | goto out; |
| 440 | } |
| 441 | } |
| 442 | switch(state) { |
| 443 | /* Unlocked due to conversion deadlock, try again */ |
| 444 | case LM_ST_UNLOCKED: |
| 445 | retry: |
| 446 | do_xmote(gl, gh, gl->gl_target); |
| 447 | break; |
| 448 | /* Conversion fails, unlock and try again */ |
| 449 | case LM_ST_SHARED: |
| 450 | case LM_ST_DEFERRED: |
| 451 | do_xmote(gl, gh, LM_ST_UNLOCKED); |
| 452 | break; |
| 453 | default: /* Everything else */ |
| 454 | printk(KERN_ERR "GFS2: wanted %u got %u\n", gl->gl_target, state); |
| 455 | GLOCK_BUG_ON(gl, 1); |
| 456 | } |
| 457 | spin_unlock(&gl->gl_spin); |
| 458 | gfs2_glock_put(gl); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | /* Fast path - we got what we asked for */ |
| 463 | if (test_and_clear_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)) |
| 464 | gfs2_demote_wake(gl); |
| 465 | if (state != LM_ST_UNLOCKED) { |
| 466 | if (glops->go_xmote_bh) { |
| 467 | int rv; |
| 468 | spin_unlock(&gl->gl_spin); |
| 469 | rv = glops->go_xmote_bh(gl, gh); |
| 470 | if (rv == -EAGAIN) |
| 471 | return; |
| 472 | spin_lock(&gl->gl_spin); |
| 473 | if (rv) { |
| 474 | do_error(gl, rv); |
| 475 | goto out; |
| 476 | } |
| 477 | } |
| 478 | do_promote(gl); |
| 479 | } |
| 480 | out: |
| 481 | clear_bit(GLF_LOCK, &gl->gl_flags); |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 482 | spin_unlock(&gl->gl_spin); |
| 483 | gfs2_glock_put(gl); |
| 484 | } |
| 485 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 486 | static unsigned int gfs2_lm_lock(struct gfs2_sbd *sdp, void *lock, |
| 487 | unsigned int cur_state, unsigned int req_state, |
| 488 | unsigned int flags) |
| 489 | { |
| 490 | int ret = LM_OUT_ERROR; |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 491 | |
| 492 | if (!sdp->sd_lockstruct.ls_ops->lm_lock) |
| 493 | return req_state == LM_ST_UNLOCKED ? 0 : req_state; |
| 494 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 495 | if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 496 | ret = sdp->sd_lockstruct.ls_ops->lm_lock(lock, cur_state, |
| 497 | req_state, flags); |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * do_xmote - Calls the DLM to change the state of a lock |
| 503 | * @gl: The lock state |
| 504 | * @gh: The holder (only for promotes) |
| 505 | * @target: The target lock state |
| 506 | * |
| 507 | */ |
| 508 | |
| 509 | static void do_xmote(struct gfs2_glock *gl, struct gfs2_holder *gh, unsigned int target) |
| 510 | { |
| 511 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
| 512 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 513 | unsigned int lck_flags = gh ? gh->gh_flags : 0; |
| 514 | int ret; |
| 515 | |
| 516 | lck_flags &= (LM_FLAG_TRY | LM_FLAG_TRY_1CB | LM_FLAG_NOEXP | |
| 517 | LM_FLAG_PRIORITY); |
| 518 | BUG_ON(gl->gl_state == target); |
| 519 | BUG_ON(gl->gl_state == gl->gl_target); |
| 520 | if ((target == LM_ST_UNLOCKED || target == LM_ST_DEFERRED) && |
| 521 | glops->go_inval) { |
| 522 | set_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags); |
| 523 | do_error(gl, 0); /* Fail queued try locks */ |
| 524 | } |
| 525 | spin_unlock(&gl->gl_spin); |
| 526 | if (glops->go_xmote_th) |
| 527 | glops->go_xmote_th(gl); |
| 528 | if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) |
| 529 | glops->go_inval(gl, target == LM_ST_DEFERRED ? 0 : DIO_METADATA); |
| 530 | clear_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags); |
| 531 | |
| 532 | gfs2_glock_hold(gl); |
| 533 | if (target != LM_ST_UNLOCKED && (gl->gl_state == LM_ST_SHARED || |
| 534 | gl->gl_state == LM_ST_DEFERRED) && |
| 535 | !(lck_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) |
| 536 | lck_flags |= LM_FLAG_TRY_1CB; |
| 537 | ret = gfs2_lm_lock(sdp, gl->gl_lock, gl->gl_state, target, lck_flags); |
| 538 | |
| 539 | if (!(ret & LM_OUT_ASYNC)) { |
| 540 | finish_xmote(gl, ret); |
| 541 | gfs2_glock_hold(gl); |
| 542 | if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) |
| 543 | gfs2_glock_put(gl); |
| 544 | } else { |
| 545 | GLOCK_BUG_ON(gl, ret != LM_OUT_ASYNC); |
| 546 | } |
| 547 | spin_lock(&gl->gl_spin); |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * find_first_holder - find the first "holder" gh |
| 552 | * @gl: the glock |
| 553 | */ |
| 554 | |
| 555 | static inline struct gfs2_holder *find_first_holder(const struct gfs2_glock *gl) |
| 556 | { |
| 557 | struct gfs2_holder *gh; |
| 558 | |
| 559 | if (!list_empty(&gl->gl_holders)) { |
| 560 | gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list); |
| 561 | if (test_bit(HIF_HOLDER, &gh->gh_iflags)) |
| 562 | return gh; |
| 563 | } |
| 564 | return NULL; |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * run_queue - do all outstanding tasks related to a glock |
| 569 | * @gl: The glock in question |
| 570 | * @nonblock: True if we must not block in run_queue |
| 571 | * |
| 572 | */ |
| 573 | |
| 574 | static void run_queue(struct gfs2_glock *gl, const int nonblock) |
| 575 | { |
| 576 | struct gfs2_holder *gh = NULL; |
| 577 | |
| 578 | if (test_and_set_bit(GLF_LOCK, &gl->gl_flags)) |
| 579 | return; |
| 580 | |
| 581 | GLOCK_BUG_ON(gl, test_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags)); |
| 582 | |
| 583 | if (test_bit(GLF_DEMOTE, &gl->gl_flags) && |
| 584 | gl->gl_demote_state != gl->gl_state) { |
| 585 | if (find_first_holder(gl)) |
| 586 | goto out; |
| 587 | if (nonblock) |
| 588 | goto out_sched; |
| 589 | set_bit(GLF_DEMOTE_IN_PROGRESS, &gl->gl_flags); |
Steven Whitehouse | 265d529c | 2008-07-07 10:02:36 +0100 | [diff] [blame^] | 590 | GLOCK_BUG_ON(gl, gl->gl_demote_state == LM_ST_EXCLUSIVE); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 591 | gl->gl_target = gl->gl_demote_state; |
| 592 | } else { |
| 593 | if (test_bit(GLF_DEMOTE, &gl->gl_flags)) |
| 594 | gfs2_demote_wake(gl); |
| 595 | if (do_promote(gl) == 0) |
| 596 | goto out; |
| 597 | gh = find_first_waiter(gl); |
| 598 | gl->gl_target = gh->gh_state; |
| 599 | if (!(gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB))) |
| 600 | do_error(gl, 0); /* Fail queued try locks */ |
| 601 | } |
| 602 | do_xmote(gl, gh, gl->gl_target); |
| 603 | return; |
| 604 | |
| 605 | out_sched: |
| 606 | gfs2_glock_hold(gl); |
| 607 | if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) |
| 608 | gfs2_glock_put(gl); |
| 609 | out: |
| 610 | clear_bit(GLF_LOCK, &gl->gl_flags); |
| 611 | } |
| 612 | |
| 613 | static void glock_work_func(struct work_struct *work) |
| 614 | { |
| 615 | unsigned long delay = 0; |
| 616 | struct gfs2_glock *gl = container_of(work, struct gfs2_glock, gl_work.work); |
| 617 | |
| 618 | if (test_and_clear_bit(GLF_REPLY_PENDING, &gl->gl_flags)) |
| 619 | finish_xmote(gl, gl->gl_reply); |
| 620 | spin_lock(&gl->gl_spin); |
Steven Whitehouse | 265d529c | 2008-07-07 10:02:36 +0100 | [diff] [blame^] | 621 | if (test_and_clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) && |
| 622 | gl->gl_state != LM_ST_UNLOCKED && |
| 623 | gl->gl_demote_state != LM_ST_EXCLUSIVE) { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 624 | unsigned long holdtime, now = jiffies; |
| 625 | holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time; |
| 626 | if (time_before(now, holdtime)) |
| 627 | delay = holdtime - now; |
| 628 | set_bit(delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE, &gl->gl_flags); |
| 629 | } |
| 630 | run_queue(gl, 0); |
| 631 | spin_unlock(&gl->gl_spin); |
| 632 | if (!delay || |
| 633 | queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0) |
| 634 | gfs2_glock_put(gl); |
| 635 | } |
| 636 | |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 637 | static int gfs2_lm_get_lock(struct gfs2_sbd *sdp, struct lm_lockname *name, |
| 638 | void **lockp) |
| 639 | { |
| 640 | int error = -EIO; |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 641 | if (!sdp->sd_lockstruct.ls_ops->lm_get_lock) |
| 642 | return 0; |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 643 | if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 644 | error = sdp->sd_lockstruct.ls_ops->lm_get_lock( |
| 645 | sdp->sd_lockstruct.ls_lockspace, name, lockp); |
| 646 | return error; |
| 647 | } |
| 648 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 649 | /** |
| 650 | * gfs2_glock_get() - Get a glock, or create one if one doesn't exist |
| 651 | * @sdp: The GFS2 superblock |
| 652 | * @number: the lock number |
| 653 | * @glops: The glock_operations to use |
| 654 | * @create: If 0, don't create the glock if it doesn't exist |
| 655 | * @glp: the glock is returned here |
| 656 | * |
| 657 | * This does not lock a glock, just finds/creates structures for one. |
| 658 | * |
| 659 | * Returns: errno |
| 660 | */ |
| 661 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 662 | int gfs2_glock_get(struct gfs2_sbd *sdp, u64 number, |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 663 | const struct gfs2_glock_operations *glops, int create, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 664 | struct gfs2_glock **glp) |
| 665 | { |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 666 | struct lm_lockname name = { .ln_number = number, .ln_type = glops->go_type }; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 667 | struct gfs2_glock *gl, *tmp; |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 668 | unsigned int hash = gl_hash(sdp, &name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 669 | int error; |
| 670 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 671 | read_lock(gl_lock_addr(hash)); |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 672 | gl = search_bucket(hash, sdp, &name); |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 673 | read_unlock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 674 | |
| 675 | if (gl || !create) { |
| 676 | *glp = gl; |
| 677 | return 0; |
| 678 | } |
| 679 | |
| 680 | gl = kmem_cache_alloc(gfs2_glock_cachep, GFP_KERNEL); |
| 681 | if (!gl) |
| 682 | return -ENOMEM; |
| 683 | |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 684 | gl->gl_flags = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 685 | gl->gl_name = name; |
Steven Whitehouse | 16feb9f | 2006-09-13 10:43:37 -0400 | [diff] [blame] | 686 | atomic_set(&gl->gl_ref, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 687 | gl->gl_state = LM_ST_UNLOCKED; |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 688 | gl->gl_target = LM_ST_UNLOCKED; |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 689 | gl->gl_demote_state = LM_ST_EXCLUSIVE; |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 690 | gl->gl_hash = hash; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 691 | gl->gl_ops = glops; |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 692 | gl->gl_stamp = jiffies; |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 693 | gl->gl_tchange = jiffies; |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 694 | gl->gl_object = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 695 | gl->gl_sbd = sdp; |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 696 | gl->gl_aspace = NULL; |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 697 | INIT_DELAYED_WORK(&gl->gl_work, glock_work_func); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 698 | |
| 699 | /* If this glock protects actual on-disk data or metadata blocks, |
| 700 | create a VFS inode to manage the pages/buffers holding them. */ |
Steven Whitehouse | 5029996 | 2006-09-04 09:49:55 -0400 | [diff] [blame] | 701 | if (glops == &gfs2_inode_glops || glops == &gfs2_rgrp_glops) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 702 | gl->gl_aspace = gfs2_aspace_get(sdp); |
| 703 | if (!gl->gl_aspace) { |
| 704 | error = -ENOMEM; |
| 705 | goto fail; |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | error = gfs2_lm_get_lock(sdp, &name, &gl->gl_lock); |
| 710 | if (error) |
| 711 | goto fail_aspace; |
| 712 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 713 | write_lock(gl_lock_addr(hash)); |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 714 | tmp = search_bucket(hash, sdp, &name); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 715 | if (tmp) { |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 716 | write_unlock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 717 | glock_free(gl); |
| 718 | gl = tmp; |
| 719 | } else { |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 720 | hlist_add_head(&gl->gl_list, &gl_hash_table[hash].hb_list); |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 721 | write_unlock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | *glp = gl; |
| 725 | |
| 726 | return 0; |
| 727 | |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 728 | fail_aspace: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 729 | if (gl->gl_aspace) |
| 730 | gfs2_aspace_put(gl->gl_aspace); |
Steven Whitehouse | ec45d9f | 2006-08-30 10:36:52 -0400 | [diff] [blame] | 731 | fail: |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 732 | kmem_cache_free(gfs2_glock_cachep, gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 733 | return error; |
| 734 | } |
| 735 | |
| 736 | /** |
| 737 | * gfs2_holder_init - initialize a struct gfs2_holder in the default way |
| 738 | * @gl: the glock |
| 739 | * @state: the state we're requesting |
| 740 | * @flags: the modifier flags |
| 741 | * @gh: the holder structure |
| 742 | * |
| 743 | */ |
| 744 | |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 745 | 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] | 746 | struct gfs2_holder *gh) |
| 747 | { |
| 748 | INIT_LIST_HEAD(&gh->gh_list); |
| 749 | gh->gh_gl = gl; |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 750 | gh->gh_ip = (unsigned long)__builtin_return_address(0); |
Pavel Emelyanov | b1e058d | 2008-02-07 00:13:19 -0800 | [diff] [blame] | 751 | gh->gh_owner_pid = get_pid(task_pid(current)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 752 | gh->gh_state = state; |
| 753 | gh->gh_flags = flags; |
| 754 | gh->gh_error = 0; |
| 755 | gh->gh_iflags = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 756 | gfs2_glock_hold(gl); |
| 757 | } |
| 758 | |
| 759 | /** |
| 760 | * gfs2_holder_reinit - reinitialize a struct gfs2_holder so we can requeue it |
| 761 | * @state: the state we're requesting |
| 762 | * @flags: the modifier flags |
| 763 | * @gh: the holder structure |
| 764 | * |
| 765 | * Don't mess with the glock. |
| 766 | * |
| 767 | */ |
| 768 | |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 769 | 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] | 770 | { |
| 771 | gh->gh_state = state; |
Steven Whitehouse | 579b78a | 2006-04-26 14:58:26 -0400 | [diff] [blame] | 772 | gh->gh_flags = flags; |
Steven Whitehouse | 3b8249f | 2007-03-16 09:40:31 +0000 | [diff] [blame] | 773 | gh->gh_iflags = 0; |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 774 | gh->gh_ip = (unsigned long)__builtin_return_address(0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | /** |
| 778 | * gfs2_holder_uninit - uninitialize a holder structure (drop glock reference) |
| 779 | * @gh: the holder structure |
| 780 | * |
| 781 | */ |
| 782 | |
| 783 | void gfs2_holder_uninit(struct gfs2_holder *gh) |
| 784 | { |
Pavel Emelyanov | b1e058d | 2008-02-07 00:13:19 -0800 | [diff] [blame] | 785 | put_pid(gh->gh_owner_pid); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 786 | gfs2_glock_put(gh->gh_gl); |
| 787 | gh->gh_gl = NULL; |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 788 | gh->gh_ip = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Abhijith Das | d93cfa9 | 2007-06-11 08:22:32 +0100 | [diff] [blame] | 791 | static int just_schedule(void *word) |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 792 | { |
| 793 | schedule(); |
| 794 | return 0; |
| 795 | } |
| 796 | |
| 797 | static void wait_on_holder(struct gfs2_holder *gh) |
| 798 | { |
| 799 | might_sleep(); |
Abhijith Das | d93cfa9 | 2007-06-11 08:22:32 +0100 | [diff] [blame] | 800 | wait_on_bit(&gh->gh_iflags, HIF_WAIT, just_schedule, TASK_UNINTERRUPTIBLE); |
| 801 | } |
| 802 | |
Abhijith Das | d93cfa9 | 2007-06-11 08:22:32 +0100 | [diff] [blame] | 803 | static void wait_on_demote(struct gfs2_glock *gl) |
| 804 | { |
| 805 | might_sleep(); |
| 806 | wait_on_bit(&gl->gl_flags, GLF_DEMOTE, just_schedule, TASK_UNINTERRUPTIBLE); |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 807 | } |
| 808 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 809 | /** |
Steven Whitehouse | 3b8249f | 2007-03-16 09:40:31 +0000 | [diff] [blame] | 810 | * handle_callback - process a demote request |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 811 | * @gl: the glock |
| 812 | * @state: the state the caller wants us to change to |
| 813 | * |
Steven Whitehouse | 3b8249f | 2007-03-16 09:40:31 +0000 | [diff] [blame] | 814 | * There are only two requests that we are going to see in actual |
| 815 | * practise: LM_ST_SHARED and LM_ST_UNLOCKED |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 816 | */ |
| 817 | |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 818 | static void handle_callback(struct gfs2_glock *gl, unsigned int state, |
| 819 | int remote, unsigned long delay) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 820 | { |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 821 | int bit = delay ? GLF_PENDING_DEMOTE : GLF_DEMOTE; |
| 822 | |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 823 | set_bit(bit, &gl->gl_flags); |
| 824 | if (gl->gl_demote_state == LM_ST_EXCLUSIVE) { |
Steven Whitehouse | 3b8249f | 2007-03-16 09:40:31 +0000 | [diff] [blame] | 825 | gl->gl_demote_state = state; |
| 826 | gl->gl_demote_time = jiffies; |
Abhijith Das | d93cfa9 | 2007-06-11 08:22:32 +0100 | [diff] [blame] | 827 | if (remote && gl->gl_ops->go_type == LM_TYPE_IOPEN && |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 828 | gl->gl_object) |
Wendy Cheng | 49e61f2 | 2007-09-13 17:52:42 -0400 | [diff] [blame] | 829 | gfs2_glock_schedule_for_reclaim(gl); |
Josef Whiter | 26caee5 | 2007-07-23 10:02:40 +0100 | [diff] [blame] | 830 | } else if (gl->gl_demote_state != LM_ST_UNLOCKED && |
| 831 | gl->gl_demote_state != state) { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 832 | gl->gl_demote_state = LM_ST_UNLOCKED; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 833 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 837 | * gfs2_glock_wait - wait on a glock acquisition |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 838 | * @gh: the glock holder |
| 839 | * |
| 840 | * Returns: 0 on success |
| 841 | */ |
| 842 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 843 | int gfs2_glock_wait(struct gfs2_holder *gh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 844 | { |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 845 | wait_on_holder(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 846 | return gh->gh_error; |
| 847 | } |
| 848 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 849 | void gfs2_print_dbg(struct seq_file *seq, const char *fmt, ...) |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 850 | { |
| 851 | va_list args; |
| 852 | |
| 853 | va_start(args, fmt); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 854 | if (seq) { |
| 855 | struct gfs2_glock_iter *gi = seq->private; |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 856 | vsprintf(gi->string, fmt, args); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 857 | seq_printf(seq, gi->string); |
| 858 | } else { |
| 859 | printk(KERN_ERR " "); |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 860 | vprintk(fmt, args); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 861 | } |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 862 | va_end(args); |
| 863 | } |
| 864 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 865 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 866 | * add_to_queue - Add a holder to the wait queue (but look for recursion) |
| 867 | * @gh: the holder structure to add |
| 868 | * |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 869 | * Eventually we should move the recursive locking trap to a |
| 870 | * debugging option or something like that. This is the fast |
| 871 | * path and needs to have the minimum number of distractions. |
| 872 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 873 | */ |
| 874 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 875 | static inline void add_to_queue(struct gfs2_holder *gh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 876 | { |
| 877 | struct gfs2_glock *gl = gh->gh_gl; |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 878 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 879 | struct list_head *insert_pt = NULL; |
| 880 | struct gfs2_holder *gh2; |
| 881 | int try_lock = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 882 | |
Pavel Emelyanov | b1e058d | 2008-02-07 00:13:19 -0800 | [diff] [blame] | 883 | BUG_ON(gh->gh_owner_pid == NULL); |
Steven Whitehouse | fee852e | 2007-01-17 15:33:23 +0000 | [diff] [blame] | 884 | if (test_and_set_bit(HIF_WAIT, &gh->gh_iflags)) |
| 885 | BUG(); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 886 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 887 | if (gh->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) { |
| 888 | if (test_bit(GLF_LOCK, &gl->gl_flags)) |
| 889 | try_lock = 1; |
| 890 | if (test_bit(GLF_INVALIDATE_IN_PROGRESS, &gl->gl_flags)) |
| 891 | goto fail; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 894 | list_for_each_entry(gh2, &gl->gl_holders, gh_list) { |
| 895 | if (unlikely(gh2->gh_owner_pid == gh->gh_owner_pid && |
| 896 | (gh->gh_gl->gl_ops->go_type != LM_TYPE_FLOCK))) |
| 897 | goto trap_recursive; |
| 898 | if (try_lock && |
| 899 | !(gh2->gh_flags & (LM_FLAG_TRY | LM_FLAG_TRY_1CB)) && |
| 900 | !may_grant(gl, gh)) { |
| 901 | fail: |
| 902 | gh->gh_error = GLR_TRYFAILED; |
| 903 | gfs2_holder_wake(gh); |
| 904 | return; |
| 905 | } |
| 906 | if (test_bit(HIF_HOLDER, &gh2->gh_iflags)) |
| 907 | continue; |
| 908 | if (unlikely((gh->gh_flags & LM_FLAG_PRIORITY) && !insert_pt)) |
| 909 | insert_pt = &gh2->gh_list; |
| 910 | } |
| 911 | if (likely(insert_pt == NULL)) { |
| 912 | list_add_tail(&gh->gh_list, &gl->gl_holders); |
| 913 | if (unlikely(gh->gh_flags & LM_FLAG_PRIORITY)) |
| 914 | goto do_cancel; |
| 915 | return; |
| 916 | } |
| 917 | list_add_tail(&gh->gh_list, insert_pt); |
| 918 | do_cancel: |
| 919 | gh = list_entry(gl->gl_holders.next, struct gfs2_holder, gh_list); |
| 920 | if (!(gh->gh_flags & LM_FLAG_PRIORITY)) { |
| 921 | spin_unlock(&gl->gl_spin); |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 922 | if (sdp->sd_lockstruct.ls_ops->lm_cancel) |
| 923 | sdp->sd_lockstruct.ls_ops->lm_cancel(gl->gl_lock); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 924 | spin_lock(&gl->gl_spin); |
| 925 | } |
| 926 | return; |
| 927 | |
| 928 | trap_recursive: |
| 929 | print_symbol(KERN_ERR "original: %s\n", gh2->gh_ip); |
| 930 | printk(KERN_ERR "pid: %d\n", pid_nr(gh2->gh_owner_pid)); |
| 931 | printk(KERN_ERR "lock type: %d req lock state : %d\n", |
| 932 | gh2->gh_gl->gl_name.ln_type, gh2->gh_state); |
| 933 | print_symbol(KERN_ERR "new: %s\n", gh->gh_ip); |
| 934 | printk(KERN_ERR "pid: %d\n", pid_nr(gh->gh_owner_pid)); |
| 935 | printk(KERN_ERR "lock type: %d req lock state : %d\n", |
| 936 | gh->gh_gl->gl_name.ln_type, gh->gh_state); |
| 937 | __dump_glock(NULL, gl); |
| 938 | BUG(); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /** |
| 942 | * gfs2_glock_nq - enqueue a struct gfs2_holder onto a glock (acquire a glock) |
| 943 | * @gh: the holder structure |
| 944 | * |
| 945 | * if (gh->gh_flags & GL_ASYNC), this never returns an error |
| 946 | * |
| 947 | * Returns: 0, GLR_TRYFAILED, or errno on failure |
| 948 | */ |
| 949 | |
| 950 | int gfs2_glock_nq(struct gfs2_holder *gh) |
| 951 | { |
| 952 | struct gfs2_glock *gl = gh->gh_gl; |
| 953 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 954 | int error = 0; |
| 955 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 956 | if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 957 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 958 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 959 | spin_lock(&gl->gl_spin); |
| 960 | add_to_queue(gh); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 961 | run_queue(gl, 1); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 962 | spin_unlock(&gl->gl_spin); |
| 963 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 964 | if (!(gh->gh_flags & GL_ASYNC)) |
| 965 | error = gfs2_glock_wait(gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 966 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 967 | return error; |
| 968 | } |
| 969 | |
| 970 | /** |
| 971 | * gfs2_glock_poll - poll to see if an async request has been completed |
| 972 | * @gh: the holder |
| 973 | * |
| 974 | * Returns: 1 if the request is ready to be gfs2_glock_wait()ed on |
| 975 | */ |
| 976 | |
| 977 | int gfs2_glock_poll(struct gfs2_holder *gh) |
| 978 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 979 | return test_bit(HIF_WAIT, &gh->gh_iflags) ? 0 : 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | /** |
| 983 | * gfs2_glock_dq - dequeue a struct gfs2_holder from a glock (release a glock) |
| 984 | * @gh: the glock holder |
| 985 | * |
| 986 | */ |
| 987 | |
| 988 | void gfs2_glock_dq(struct gfs2_holder *gh) |
| 989 | { |
| 990 | struct gfs2_glock *gl = gh->gh_gl; |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 991 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 992 | unsigned delay = 0; |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 993 | int fast_path = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 994 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 995 | spin_lock(&gl->gl_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 996 | if (gh->gh_flags & GL_NOCACHE) |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 997 | handle_callback(gl, LM_ST_UNLOCKED, 0, 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 998 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 999 | list_del_init(&gh->gh_list); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1000 | if (find_first_holder(gl) == NULL) { |
Steven Whitehouse | 3042a2c | 2007-11-02 08:39:34 +0000 | [diff] [blame] | 1001 | if (glops->go_unlock) { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1002 | GLOCK_BUG_ON(gl, test_and_set_bit(GLF_LOCK, &gl->gl_flags)); |
Steven Whitehouse | 3042a2c | 2007-11-02 08:39:34 +0000 | [diff] [blame] | 1003 | spin_unlock(&gl->gl_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1004 | glops->go_unlock(gh); |
Steven Whitehouse | 3042a2c | 2007-11-02 08:39:34 +0000 | [diff] [blame] | 1005 | spin_lock(&gl->gl_spin); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1006 | clear_bit(GLF_LOCK, &gl->gl_flags); |
Steven Whitehouse | 3042a2c | 2007-11-02 08:39:34 +0000 | [diff] [blame] | 1007 | } |
Steven Whitehouse | 3b8249f | 2007-03-16 09:40:31 +0000 | [diff] [blame] | 1008 | gl->gl_stamp = jiffies; |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1009 | if (list_empty(&gl->gl_holders) && |
| 1010 | !test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) && |
| 1011 | !test_bit(GLF_DEMOTE, &gl->gl_flags)) |
| 1012 | fast_path = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1013 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1014 | spin_unlock(&gl->gl_spin); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1015 | if (likely(fast_path)) |
| 1016 | return; |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 1017 | |
| 1018 | gfs2_glock_hold(gl); |
| 1019 | if (test_bit(GLF_PENDING_DEMOTE, &gl->gl_flags) && |
| 1020 | !test_bit(GLF_DEMOTE, &gl->gl_flags)) |
| 1021 | delay = gl->gl_ops->go_min_hold_time; |
| 1022 | if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0) |
| 1023 | gfs2_glock_put(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
Abhijith Das | d93cfa9 | 2007-06-11 08:22:32 +0100 | [diff] [blame] | 1026 | void gfs2_glock_dq_wait(struct gfs2_holder *gh) |
| 1027 | { |
| 1028 | struct gfs2_glock *gl = gh->gh_gl; |
| 1029 | gfs2_glock_dq(gh); |
| 1030 | wait_on_demote(gl); |
| 1031 | } |
| 1032 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1033 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1034 | * gfs2_glock_dq_uninit - dequeue a holder from a glock and initialize it |
| 1035 | * @gh: the holder structure |
| 1036 | * |
| 1037 | */ |
| 1038 | |
| 1039 | void gfs2_glock_dq_uninit(struct gfs2_holder *gh) |
| 1040 | { |
| 1041 | gfs2_glock_dq(gh); |
| 1042 | gfs2_holder_uninit(gh); |
| 1043 | } |
| 1044 | |
| 1045 | /** |
| 1046 | * gfs2_glock_nq_num - acquire a glock based on lock number |
| 1047 | * @sdp: the filesystem |
| 1048 | * @number: the lock number |
| 1049 | * @glops: the glock operations for the type of glock |
| 1050 | * @state: the state to acquire the glock in |
| 1051 | * @flags: modifier flags for the aquisition |
| 1052 | * @gh: the struct gfs2_holder |
| 1053 | * |
| 1054 | * Returns: errno |
| 1055 | */ |
| 1056 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 1057 | int gfs2_glock_nq_num(struct gfs2_sbd *sdp, u64 number, |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 1058 | const struct gfs2_glock_operations *glops, |
| 1059 | unsigned int state, int flags, struct gfs2_holder *gh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1060 | { |
| 1061 | struct gfs2_glock *gl; |
| 1062 | int error; |
| 1063 | |
| 1064 | error = gfs2_glock_get(sdp, number, glops, CREATE, &gl); |
| 1065 | if (!error) { |
| 1066 | error = gfs2_glock_nq_init(gl, state, flags, gh); |
| 1067 | gfs2_glock_put(gl); |
| 1068 | } |
| 1069 | |
| 1070 | return error; |
| 1071 | } |
| 1072 | |
| 1073 | /** |
| 1074 | * glock_compare - Compare two struct gfs2_glock structures for sorting |
| 1075 | * @arg_a: the first structure |
| 1076 | * @arg_b: the second structure |
| 1077 | * |
| 1078 | */ |
| 1079 | |
| 1080 | static int glock_compare(const void *arg_a, const void *arg_b) |
| 1081 | { |
Steven Whitehouse | a5e08a9 | 2006-09-09 17:07:05 -0400 | [diff] [blame] | 1082 | const struct gfs2_holder *gh_a = *(const struct gfs2_holder **)arg_a; |
| 1083 | const struct gfs2_holder *gh_b = *(const struct gfs2_holder **)arg_b; |
| 1084 | const struct lm_lockname *a = &gh_a->gh_gl->gl_name; |
| 1085 | const struct lm_lockname *b = &gh_b->gh_gl->gl_name; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1086 | |
| 1087 | if (a->ln_number > b->ln_number) |
Steven Whitehouse | a5e08a9 | 2006-09-09 17:07:05 -0400 | [diff] [blame] | 1088 | return 1; |
| 1089 | if (a->ln_number < b->ln_number) |
| 1090 | return -1; |
Steven Whitehouse | 1c0f487 | 2007-01-22 12:10:39 -0500 | [diff] [blame] | 1091 | BUG_ON(gh_a->gh_gl->gl_ops->go_type == gh_b->gh_gl->gl_ops->go_type); |
Steven Whitehouse | a5e08a9 | 2006-09-09 17:07:05 -0400 | [diff] [blame] | 1092 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * nq_m_sync - synchonously acquire more than one glock in deadlock free order |
| 1097 | * @num_gh: the number of structures |
| 1098 | * @ghs: an array of struct gfs2_holder structures |
| 1099 | * |
| 1100 | * Returns: 0 on success (all glocks acquired), |
| 1101 | * errno on failure (no glocks acquired) |
| 1102 | */ |
| 1103 | |
| 1104 | static int nq_m_sync(unsigned int num_gh, struct gfs2_holder *ghs, |
| 1105 | struct gfs2_holder **p) |
| 1106 | { |
| 1107 | unsigned int x; |
| 1108 | int error = 0; |
| 1109 | |
| 1110 | for (x = 0; x < num_gh; x++) |
| 1111 | p[x] = &ghs[x]; |
| 1112 | |
| 1113 | sort(p, num_gh, sizeof(struct gfs2_holder *), glock_compare, NULL); |
| 1114 | |
| 1115 | for (x = 0; x < num_gh; x++) { |
| 1116 | p[x]->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1117 | |
| 1118 | error = gfs2_glock_nq(p[x]); |
| 1119 | if (error) { |
| 1120 | while (x--) |
| 1121 | gfs2_glock_dq(p[x]); |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | return error; |
| 1127 | } |
| 1128 | |
| 1129 | /** |
| 1130 | * gfs2_glock_nq_m - acquire multiple glocks |
| 1131 | * @num_gh: the number of structures |
| 1132 | * @ghs: an array of struct gfs2_holder structures |
| 1133 | * |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1134 | * |
| 1135 | * Returns: 0 on success (all glocks acquired), |
| 1136 | * errno on failure (no glocks acquired) |
| 1137 | */ |
| 1138 | |
| 1139 | int gfs2_glock_nq_m(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1140 | { |
Steven Whitehouse | eaf5bd3 | 2007-06-19 15:38:17 +0100 | [diff] [blame] | 1141 | struct gfs2_holder *tmp[4]; |
| 1142 | struct gfs2_holder **pph = tmp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1143 | int error = 0; |
| 1144 | |
Steven Whitehouse | eaf5bd3 | 2007-06-19 15:38:17 +0100 | [diff] [blame] | 1145 | switch(num_gh) { |
| 1146 | case 0: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1147 | return 0; |
Steven Whitehouse | eaf5bd3 | 2007-06-19 15:38:17 +0100 | [diff] [blame] | 1148 | case 1: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1149 | ghs->gh_flags &= ~(LM_FLAG_TRY | GL_ASYNC); |
| 1150 | return gfs2_glock_nq(ghs); |
Steven Whitehouse | eaf5bd3 | 2007-06-19 15:38:17 +0100 | [diff] [blame] | 1151 | default: |
| 1152 | if (num_gh <= 4) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1153 | break; |
Steven Whitehouse | eaf5bd3 | 2007-06-19 15:38:17 +0100 | [diff] [blame] | 1154 | pph = kmalloc(num_gh * sizeof(struct gfs2_holder *), GFP_NOFS); |
| 1155 | if (!pph) |
| 1156 | return -ENOMEM; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1157 | } |
| 1158 | |
Steven Whitehouse | eaf5bd3 | 2007-06-19 15:38:17 +0100 | [diff] [blame] | 1159 | error = nq_m_sync(num_gh, ghs, pph); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1160 | |
Steven Whitehouse | eaf5bd3 | 2007-06-19 15:38:17 +0100 | [diff] [blame] | 1161 | if (pph != tmp) |
| 1162 | kfree(pph); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1163 | |
| 1164 | return error; |
| 1165 | } |
| 1166 | |
| 1167 | /** |
| 1168 | * gfs2_glock_dq_m - release multiple glocks |
| 1169 | * @num_gh: the number of structures |
| 1170 | * @ghs: an array of struct gfs2_holder structures |
| 1171 | * |
| 1172 | */ |
| 1173 | |
| 1174 | void gfs2_glock_dq_m(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1175 | { |
| 1176 | unsigned int x; |
| 1177 | |
| 1178 | for (x = 0; x < num_gh; x++) |
| 1179 | gfs2_glock_dq(&ghs[x]); |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * gfs2_glock_dq_uninit_m - release multiple glocks |
| 1184 | * @num_gh: the number of structures |
| 1185 | * @ghs: an array of struct gfs2_holder structures |
| 1186 | * |
| 1187 | */ |
| 1188 | |
| 1189 | void gfs2_glock_dq_uninit_m(unsigned int num_gh, struct gfs2_holder *ghs) |
| 1190 | { |
| 1191 | unsigned int x; |
| 1192 | |
| 1193 | for (x = 0; x < num_gh; x++) |
| 1194 | gfs2_glock_dq_uninit(&ghs[x]); |
| 1195 | } |
| 1196 | |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 1197 | static int gfs2_lm_hold_lvb(struct gfs2_sbd *sdp, void *lock, char **lvbp) |
| 1198 | { |
| 1199 | int error = -EIO; |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 1200 | if (!sdp->sd_lockstruct.ls_ops->lm_hold_lvb) |
| 1201 | return 0; |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 1202 | if (likely(!test_bit(SDF_SHUTDOWN, &sdp->sd_flags))) |
| 1203 | error = sdp->sd_lockstruct.ls_ops->lm_hold_lvb(lock, lvbp); |
| 1204 | return error; |
| 1205 | } |
| 1206 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1207 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1208 | * gfs2_lvb_hold - attach a LVB from a glock |
| 1209 | * @gl: The glock in question |
| 1210 | * |
| 1211 | */ |
| 1212 | |
| 1213 | int gfs2_lvb_hold(struct gfs2_glock *gl) |
| 1214 | { |
| 1215 | int error; |
| 1216 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1217 | if (!atomic_read(&gl->gl_lvb_count)) { |
| 1218 | error = gfs2_lm_hold_lvb(gl->gl_sbd, gl->gl_lock, &gl->gl_lvb); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1219 | if (error) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1220 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1221 | gfs2_glock_hold(gl); |
| 1222 | } |
| 1223 | atomic_inc(&gl->gl_lvb_count); |
| 1224 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1225 | return 0; |
| 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * gfs2_lvb_unhold - detach a LVB from a glock |
| 1230 | * @gl: The glock in question |
| 1231 | * |
| 1232 | */ |
| 1233 | |
| 1234 | void gfs2_lvb_unhold(struct gfs2_glock *gl) |
| 1235 | { |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 1236 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1237 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1238 | gfs2_glock_hold(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1239 | gfs2_assert(gl->gl_sbd, atomic_read(&gl->gl_lvb_count) > 0); |
| 1240 | if (atomic_dec_and_test(&gl->gl_lvb_count)) { |
Steven Whitehouse | 048bca2 | 2008-05-23 14:46:04 +0100 | [diff] [blame] | 1241 | if (sdp->sd_lockstruct.ls_ops->lm_unhold_lvb) |
Steven Whitehouse | da755fd | 2008-01-30 15:34:04 +0000 | [diff] [blame] | 1242 | sdp->sd_lockstruct.ls_ops->lm_unhold_lvb(gl->gl_lock, gl->gl_lvb); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1243 | gl->gl_lvb = NULL; |
| 1244 | gfs2_glock_put(gl); |
| 1245 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1246 | gfs2_glock_put(gl); |
| 1247 | } |
| 1248 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1249 | static void blocking_cb(struct gfs2_sbd *sdp, struct lm_lockname *name, |
| 1250 | unsigned int state) |
| 1251 | { |
| 1252 | struct gfs2_glock *gl; |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 1253 | unsigned long delay = 0; |
| 1254 | unsigned long holdtime; |
| 1255 | unsigned long now = jiffies; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1256 | |
| 1257 | gl = gfs2_glock_find(sdp, name); |
| 1258 | if (!gl) |
| 1259 | return; |
| 1260 | |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 1261 | holdtime = gl->gl_tchange + gl->gl_ops->go_min_hold_time; |
| 1262 | if (time_before(now, holdtime)) |
| 1263 | delay = holdtime - now; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1264 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1265 | spin_lock(&gl->gl_spin); |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 1266 | handle_callback(gl, state, 1, delay); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1267 | spin_unlock(&gl->gl_spin); |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 1268 | if (queue_delayed_work(glock_workqueue, &gl->gl_work, delay) == 0) |
| 1269 | gfs2_glock_put(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | /** |
| 1273 | * gfs2_glock_cb - Callback used by locking module |
Steven Whitehouse | 1c089c3 | 2006-09-07 15:50:20 -0400 | [diff] [blame] | 1274 | * @sdp: Pointer to the superblock |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1275 | * @type: Type of callback |
| 1276 | * @data: Type dependent data pointer |
| 1277 | * |
| 1278 | * Called by the locking module when it wants to tell us something. |
| 1279 | * Either we need to drop a lock, one of our ASYNC requests completed, or |
| 1280 | * a journal from another client needs to be recovered. |
| 1281 | */ |
| 1282 | |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 1283 | void gfs2_glock_cb(void *cb_data, unsigned int type, void *data) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1284 | { |
Steven Whitehouse | 9b47c11 | 2006-09-08 10:17:58 -0400 | [diff] [blame] | 1285 | struct gfs2_sbd *sdp = cb_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1286 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1287 | switch (type) { |
| 1288 | case LM_CB_NEED_E: |
David Teigland | e7f5c01 | 2006-04-27 11:25:45 -0400 | [diff] [blame] | 1289 | blocking_cb(sdp, data, LM_ST_UNLOCKED); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1290 | return; |
| 1291 | |
| 1292 | case LM_CB_NEED_D: |
David Teigland | e7f5c01 | 2006-04-27 11:25:45 -0400 | [diff] [blame] | 1293 | blocking_cb(sdp, data, LM_ST_DEFERRED); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1294 | return; |
| 1295 | |
| 1296 | case LM_CB_NEED_S: |
David Teigland | e7f5c01 | 2006-04-27 11:25:45 -0400 | [diff] [blame] | 1297 | blocking_cb(sdp, data, LM_ST_SHARED); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1298 | return; |
| 1299 | |
| 1300 | case LM_CB_ASYNC: { |
David Teigland | e7f5c01 | 2006-04-27 11:25:45 -0400 | [diff] [blame] | 1301 | struct lm_async_cb *async = data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1302 | struct gfs2_glock *gl; |
| 1303 | |
Steven Whitehouse | 61be084 | 2007-01-29 11:51:45 +0000 | [diff] [blame] | 1304 | down_read(&gfs2_umount_flush_sem); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1305 | gl = gfs2_glock_find(sdp, &async->lc_name); |
| 1306 | if (gfs2_assert_warn(sdp, gl)) |
| 1307 | return; |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1308 | gl->gl_reply = async->lc_ret; |
| 1309 | set_bit(GLF_REPLY_PENDING, &gl->gl_flags); |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 1310 | if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) |
| 1311 | gfs2_glock_put(gl); |
Steven Whitehouse | 61be084 | 2007-01-29 11:51:45 +0000 | [diff] [blame] | 1312 | up_read(&gfs2_umount_flush_sem); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1313 | return; |
| 1314 | } |
| 1315 | |
| 1316 | case LM_CB_NEED_RECOVERY: |
| 1317 | gfs2_jdesc_make_dirty(sdp, *(unsigned int *)data); |
| 1318 | if (sdp->sd_recoverd_process) |
| 1319 | wake_up_process(sdp->sd_recoverd_process); |
| 1320 | return; |
| 1321 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1322 | default: |
| 1323 | gfs2_assert_warn(sdp, 0); |
| 1324 | return; |
| 1325 | } |
| 1326 | } |
| 1327 | |
| 1328 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1329 | * demote_ok - Check to see if it's ok to unlock a glock |
| 1330 | * @gl: the glock |
| 1331 | * |
| 1332 | * Returns: 1 if it's ok |
| 1333 | */ |
| 1334 | |
| 1335 | static int demote_ok(struct gfs2_glock *gl) |
| 1336 | { |
Steven Whitehouse | 8fb4b53 | 2006-08-30 09:30:00 -0400 | [diff] [blame] | 1337 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1338 | int demote = 1; |
| 1339 | |
| 1340 | if (test_bit(GLF_STICKY, &gl->gl_flags)) |
| 1341 | demote = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1342 | else if (glops->go_demote_ok) |
| 1343 | demote = glops->go_demote_ok(gl); |
| 1344 | |
| 1345 | return demote; |
| 1346 | } |
| 1347 | |
| 1348 | /** |
| 1349 | * gfs2_glock_schedule_for_reclaim - Add a glock to the reclaim list |
| 1350 | * @gl: the glock |
| 1351 | * |
| 1352 | */ |
| 1353 | |
| 1354 | void gfs2_glock_schedule_for_reclaim(struct gfs2_glock *gl) |
| 1355 | { |
| 1356 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1357 | |
| 1358 | spin_lock(&sdp->sd_reclaim_lock); |
| 1359 | if (list_empty(&gl->gl_reclaim)) { |
| 1360 | gfs2_glock_hold(gl); |
| 1361 | list_add(&gl->gl_reclaim, &sdp->sd_reclaim_list); |
| 1362 | atomic_inc(&sdp->sd_reclaim_count); |
Bob Peterson | ef8c441 | 2008-01-28 14:54:16 -0600 | [diff] [blame] | 1363 | spin_unlock(&sdp->sd_reclaim_lock); |
| 1364 | wake_up(&sdp->sd_reclaim_wq); |
| 1365 | } else |
| 1366 | spin_unlock(&sdp->sd_reclaim_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | /** |
| 1370 | * gfs2_reclaim_glock - process the next glock on the filesystem's reclaim list |
| 1371 | * @sdp: the filesystem |
| 1372 | * |
| 1373 | * Called from gfs2_glockd() glock reclaim daemon, or when promoting a |
| 1374 | * different glock and we notice that there are a lot of glocks in the |
| 1375 | * reclaim list. |
| 1376 | * |
| 1377 | */ |
| 1378 | |
| 1379 | void gfs2_reclaim_glock(struct gfs2_sbd *sdp) |
| 1380 | { |
| 1381 | struct gfs2_glock *gl; |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1382 | int done_callback = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1383 | |
| 1384 | spin_lock(&sdp->sd_reclaim_lock); |
| 1385 | if (list_empty(&sdp->sd_reclaim_list)) { |
| 1386 | spin_unlock(&sdp->sd_reclaim_lock); |
| 1387 | return; |
| 1388 | } |
| 1389 | gl = list_entry(sdp->sd_reclaim_list.next, |
| 1390 | struct gfs2_glock, gl_reclaim); |
| 1391 | list_del_init(&gl->gl_reclaim); |
| 1392 | spin_unlock(&sdp->sd_reclaim_lock); |
| 1393 | |
| 1394 | atomic_dec(&sdp->sd_reclaim_count); |
| 1395 | atomic_inc(&sdp->sd_reclaimed); |
| 1396 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1397 | spin_lock(&gl->gl_spin); |
| 1398 | if (find_first_holder(gl) == NULL && |
| 1399 | gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl)) { |
| 1400 | handle_callback(gl, LM_ST_UNLOCKED, 0, 0); |
| 1401 | done_callback = 1; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1402 | } |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1403 | spin_unlock(&gl->gl_spin); |
| 1404 | if (!done_callback || |
| 1405 | queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) |
| 1406 | gfs2_glock_put(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | /** |
| 1410 | * examine_bucket - Call a function for glock in a hash bucket |
| 1411 | * @examiner: the function |
| 1412 | * @sdp: the filesystem |
| 1413 | * @bucket: the bucket |
| 1414 | * |
| 1415 | * Returns: 1 if the bucket has entries |
| 1416 | */ |
| 1417 | |
| 1418 | static int examine_bucket(glock_examiner examiner, struct gfs2_sbd *sdp, |
Steven Whitehouse | 37b2fa6 | 2006-09-08 13:35:56 -0400 | [diff] [blame] | 1419 | unsigned int hash) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1420 | { |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1421 | struct gfs2_glock *gl, *prev = NULL; |
| 1422 | int has_entries = 0; |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1423 | struct hlist_head *head = &gl_hash_table[hash].hb_list; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1424 | |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1425 | read_lock(gl_lock_addr(hash)); |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1426 | /* Can't use hlist_for_each_entry - don't want prefetch here */ |
| 1427 | if (hlist_empty(head)) |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1428 | goto out; |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1429 | gl = list_entry(head->first, struct gfs2_glock, gl_list); |
| 1430 | while(1) { |
Steven Whitehouse | 8fbbfd2 | 2007-08-01 13:57:10 +0100 | [diff] [blame] | 1431 | if (!sdp || gl->gl_sbd == sdp) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1432 | gfs2_glock_hold(gl); |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1433 | read_unlock(gl_lock_addr(hash)); |
| 1434 | if (prev) |
| 1435 | gfs2_glock_put(prev); |
| 1436 | prev = gl; |
| 1437 | examiner(gl); |
Steven Whitehouse | a833634 | 2006-09-14 13:57:38 -0400 | [diff] [blame] | 1438 | has_entries = 1; |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1439 | read_lock(gl_lock_addr(hash)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1440 | } |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1441 | if (gl->gl_list.next == NULL) |
| 1442 | break; |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1443 | gl = list_entry(gl->gl_list.next, struct gfs2_glock, gl_list); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1444 | } |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1445 | out: |
| 1446 | read_unlock(gl_lock_addr(hash)); |
| 1447 | if (prev) |
| 1448 | gfs2_glock_put(prev); |
Steven Whitehouse | 8fbbfd2 | 2007-08-01 13:57:10 +0100 | [diff] [blame] | 1449 | cond_resched(); |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1450 | return has_entries; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /** |
| 1454 | * scan_glock - look at a glock and see if we can reclaim it |
| 1455 | * @gl: the glock to look at |
| 1456 | * |
| 1457 | */ |
| 1458 | |
| 1459 | static void scan_glock(struct gfs2_glock *gl) |
| 1460 | { |
Steven Whitehouse | b004157 | 2006-11-23 10:51:34 -0500 | [diff] [blame] | 1461 | if (gl->gl_ops == &gfs2_inode_glops && gl->gl_object) |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1462 | return; |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1463 | if (test_bit(GLF_LOCK, &gl->gl_flags)) |
| 1464 | return; |
Steven Whitehouse | a2242db | 2006-08-24 17:03:05 -0400 | [diff] [blame] | 1465 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1466 | spin_lock(&gl->gl_spin); |
| 1467 | if (find_first_holder(gl) == NULL && |
| 1468 | gl->gl_state != LM_ST_UNLOCKED && demote_ok(gl)) |
| 1469 | gfs2_glock_schedule_for_reclaim(gl); |
| 1470 | spin_unlock(&gl->gl_spin); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | /** |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1474 | * clear_glock - look at a glock and see if we can free it from glock cache |
| 1475 | * @gl: the glock to look at |
| 1476 | * |
| 1477 | */ |
| 1478 | |
| 1479 | static void clear_glock(struct gfs2_glock *gl) |
| 1480 | { |
| 1481 | struct gfs2_sbd *sdp = gl->gl_sbd; |
| 1482 | int released; |
| 1483 | |
| 1484 | spin_lock(&sdp->sd_reclaim_lock); |
| 1485 | if (!list_empty(&gl->gl_reclaim)) { |
| 1486 | list_del_init(&gl->gl_reclaim); |
| 1487 | atomic_dec(&sdp->sd_reclaim_count); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 1488 | spin_unlock(&sdp->sd_reclaim_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1489 | released = gfs2_glock_put(gl); |
| 1490 | gfs2_assert(sdp, !released); |
Steven Whitehouse | 190562b | 2006-04-20 16:57:23 -0400 | [diff] [blame] | 1491 | } else { |
| 1492 | spin_unlock(&sdp->sd_reclaim_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1493 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1494 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1495 | spin_lock(&gl->gl_spin); |
| 1496 | if (find_first_holder(gl) == NULL && gl->gl_state != LM_ST_UNLOCKED) |
| 1497 | handle_callback(gl, LM_ST_UNLOCKED, 0, 0); |
| 1498 | spin_unlock(&gl->gl_spin); |
| 1499 | gfs2_glock_hold(gl); |
| 1500 | if (queue_delayed_work(glock_workqueue, &gl->gl_work, 0) == 0) |
| 1501 | gfs2_glock_put(gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | /** |
| 1505 | * gfs2_gl_hash_clear - Empty out the glock hash table |
| 1506 | * @sdp: the filesystem |
| 1507 | * @wait: wait until it's all gone |
| 1508 | * |
Steven Whitehouse | 1bdad60 | 2008-06-03 14:09:53 +0100 | [diff] [blame] | 1509 | * Called when unmounting the filesystem. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1510 | */ |
| 1511 | |
Steven Whitehouse | 1bdad60 | 2008-06-03 14:09:53 +0100 | [diff] [blame] | 1512 | void gfs2_gl_hash_clear(struct gfs2_sbd *sdp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1513 | { |
| 1514 | unsigned long t; |
| 1515 | unsigned int x; |
| 1516 | int cont; |
| 1517 | |
| 1518 | t = jiffies; |
| 1519 | |
| 1520 | for (;;) { |
| 1521 | cont = 0; |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1522 | for (x = 0; x < GFS2_GL_HASH_SIZE; x++) { |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 1523 | if (examine_bucket(clear_glock, sdp, x)) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1524 | cont = 1; |
Steven Whitehouse | 2426443 | 2006-09-11 21:40:30 -0400 | [diff] [blame] | 1525 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1526 | |
Steven Whitehouse | 1bdad60 | 2008-06-03 14:09:53 +0100 | [diff] [blame] | 1527 | if (!cont) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | |
| 1530 | if (time_after_eq(jiffies, |
| 1531 | t + gfs2_tune_get(sdp, gt_stall_secs) * HZ)) { |
| 1532 | fs_warn(sdp, "Unmount seems to be stalled. " |
| 1533 | "Dumping lock state...\n"); |
| 1534 | gfs2_dump_lockstate(sdp); |
| 1535 | t = jiffies; |
| 1536 | } |
| 1537 | |
Steven Whitehouse | 61be084 | 2007-01-29 11:51:45 +0000 | [diff] [blame] | 1538 | down_write(&gfs2_umount_flush_sem); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1539 | invalidate_inodes(sdp->sd_vfs); |
Steven Whitehouse | 61be084 | 2007-01-29 11:51:45 +0000 | [diff] [blame] | 1540 | up_write(&gfs2_umount_flush_sem); |
Steven Whitehouse | fd88de56 | 2006-05-05 16:59:11 -0400 | [diff] [blame] | 1541 | msleep(10); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1542 | } |
| 1543 | } |
| 1544 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1545 | static const char *state2str(unsigned state) |
Robert Peterson | 04b933f | 2007-03-23 17:05:15 -0500 | [diff] [blame] | 1546 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1547 | switch(state) { |
| 1548 | case LM_ST_UNLOCKED: |
| 1549 | return "UN"; |
| 1550 | case LM_ST_SHARED: |
| 1551 | return "SH"; |
| 1552 | case LM_ST_DEFERRED: |
| 1553 | return "DF"; |
| 1554 | case LM_ST_EXCLUSIVE: |
| 1555 | return "EX"; |
| 1556 | } |
| 1557 | return "??"; |
| 1558 | } |
Robert Peterson | 04b933f | 2007-03-23 17:05:15 -0500 | [diff] [blame] | 1559 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1560 | static const char *hflags2str(char *buf, unsigned flags, unsigned long iflags) |
| 1561 | { |
| 1562 | char *p = buf; |
| 1563 | if (flags & LM_FLAG_TRY) |
| 1564 | *p++ = 't'; |
| 1565 | if (flags & LM_FLAG_TRY_1CB) |
| 1566 | *p++ = 'T'; |
| 1567 | if (flags & LM_FLAG_NOEXP) |
| 1568 | *p++ = 'e'; |
| 1569 | if (flags & LM_FLAG_ANY) |
| 1570 | *p++ = 'a'; |
| 1571 | if (flags & LM_FLAG_PRIORITY) |
| 1572 | *p++ = 'p'; |
| 1573 | if (flags & GL_ASYNC) |
| 1574 | *p++ = 'a'; |
| 1575 | if (flags & GL_EXACT) |
| 1576 | *p++ = 'E'; |
| 1577 | if (flags & GL_ATIME) |
| 1578 | *p++ = 'a'; |
| 1579 | if (flags & GL_NOCACHE) |
| 1580 | *p++ = 'c'; |
| 1581 | if (test_bit(HIF_HOLDER, &iflags)) |
| 1582 | *p++ = 'H'; |
| 1583 | if (test_bit(HIF_WAIT, &iflags)) |
| 1584 | *p++ = 'W'; |
| 1585 | if (test_bit(HIF_FIRST, &iflags)) |
| 1586 | *p++ = 'F'; |
| 1587 | *p = 0; |
| 1588 | return buf; |
Robert Peterson | 04b933f | 2007-03-23 17:05:15 -0500 | [diff] [blame] | 1589 | } |
| 1590 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1591 | /** |
| 1592 | * dump_holder - print information about a glock holder |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1593 | * @seq: the seq_file struct |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1594 | * @gh: the glock holder |
| 1595 | * |
| 1596 | * Returns: 0 on success, -ENOBUFS when we run out of space |
| 1597 | */ |
| 1598 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1599 | static int dump_holder(struct seq_file *seq, const struct gfs2_holder *gh) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1600 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1601 | struct task_struct *gh_owner = NULL; |
| 1602 | char buffer[KSYM_SYMBOL_LEN]; |
| 1603 | char flags_buf[32]; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1604 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1605 | sprint_symbol(buffer, gh->gh_ip); |
| 1606 | if (gh->gh_owner_pid) |
Pavel Emelyanov | b1e058d | 2008-02-07 00:13:19 -0800 | [diff] [blame] | 1607 | gh_owner = pid_task(gh->gh_owner_pid, PIDTYPE_PID); |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1608 | gfs2_print_dbg(seq, " H: s:%s f:%s e:%d p:%ld [%s] %s\n", |
| 1609 | state2str(gh->gh_state), |
| 1610 | hflags2str(flags_buf, gh->gh_flags, gh->gh_iflags), |
| 1611 | gh->gh_error, |
| 1612 | gh->gh_owner_pid ? (long)pid_nr(gh->gh_owner_pid) : -1, |
| 1613 | gh_owner ? gh_owner->comm : "(ended)", buffer); |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1614 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1617 | static const char *gflags2str(char *buf, const unsigned long *gflags) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1618 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1619 | char *p = buf; |
| 1620 | if (test_bit(GLF_LOCK, gflags)) |
| 1621 | *p++ = 'l'; |
| 1622 | if (test_bit(GLF_STICKY, gflags)) |
| 1623 | *p++ = 's'; |
| 1624 | if (test_bit(GLF_DEMOTE, gflags)) |
| 1625 | *p++ = 'D'; |
| 1626 | if (test_bit(GLF_PENDING_DEMOTE, gflags)) |
| 1627 | *p++ = 'd'; |
| 1628 | if (test_bit(GLF_DEMOTE_IN_PROGRESS, gflags)) |
| 1629 | *p++ = 'p'; |
| 1630 | if (test_bit(GLF_DIRTY, gflags)) |
| 1631 | *p++ = 'y'; |
| 1632 | if (test_bit(GLF_LFLUSH, gflags)) |
| 1633 | *p++ = 'f'; |
| 1634 | if (test_bit(GLF_INVALIDATE_IN_PROGRESS, gflags)) |
| 1635 | *p++ = 'i'; |
| 1636 | if (test_bit(GLF_REPLY_PENDING, gflags)) |
| 1637 | *p++ = 'r'; |
| 1638 | *p = 0; |
| 1639 | return buf; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | /** |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1643 | * __dump_glock - print information about a glock |
| 1644 | * @seq: The seq_file struct |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1645 | * @gl: the glock |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1646 | * |
| 1647 | * The file format is as follows: |
| 1648 | * One line per object, capital letters are used to indicate objects |
| 1649 | * G = glock, I = Inode, R = rgrp, H = holder. Glocks are not indented, |
| 1650 | * other objects are indented by a single space and follow the glock to |
| 1651 | * which they are related. Fields are indicated by lower case letters |
| 1652 | * followed by a colon and the field value, except for strings which are in |
| 1653 | * [] so that its possible to see if they are composed of spaces for |
| 1654 | * example. The field's are n = number (id of the object), f = flags, |
| 1655 | * t = type, s = state, r = refcount, e = error, p = pid. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1656 | * |
| 1657 | * Returns: 0 on success, -ENOBUFS when we run out of space |
| 1658 | */ |
| 1659 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1660 | static int __dump_glock(struct seq_file *seq, const struct gfs2_glock *gl) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1661 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1662 | const struct gfs2_glock_operations *glops = gl->gl_ops; |
| 1663 | unsigned long long dtime; |
| 1664 | const struct gfs2_holder *gh; |
| 1665 | char gflags_buf[32]; |
| 1666 | int error = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1667 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1668 | dtime = jiffies - gl->gl_demote_time; |
| 1669 | dtime *= 1000000/HZ; /* demote time in uSec */ |
| 1670 | if (!test_bit(GLF_DEMOTE, &gl->gl_flags)) |
| 1671 | dtime = 0; |
| 1672 | gfs2_print_dbg(seq, "G: s:%s n:%u/%llu f:%s t:%s d:%s/%llu l:%d a:%d r:%d\n", |
| 1673 | state2str(gl->gl_state), |
| 1674 | gl->gl_name.ln_type, |
| 1675 | (unsigned long long)gl->gl_name.ln_number, |
| 1676 | gflags2str(gflags_buf, &gl->gl_flags), |
| 1677 | state2str(gl->gl_target), |
| 1678 | state2str(gl->gl_demote_state), dtime, |
| 1679 | atomic_read(&gl->gl_lvb_count), |
| 1680 | atomic_read(&gl->gl_ail_count), |
| 1681 | atomic_read(&gl->gl_ref)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1682 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1683 | list_for_each_entry(gh, &gl->gl_holders, gh_list) { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1684 | error = dump_holder(seq, gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1685 | if (error) |
| 1686 | goto out; |
| 1687 | } |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1688 | if (gl->gl_state != LM_ST_UNLOCKED && glops->go_dump) |
| 1689 | error = glops->go_dump(seq, gl); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 1690 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1691 | return error; |
| 1692 | } |
| 1693 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1694 | static int dump_glock(struct seq_file *seq, struct gfs2_glock *gl) |
| 1695 | { |
| 1696 | int ret; |
| 1697 | spin_lock(&gl->gl_spin); |
| 1698 | ret = __dump_glock(seq, gl); |
| 1699 | spin_unlock(&gl->gl_spin); |
| 1700 | return ret; |
| 1701 | } |
| 1702 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1703 | /** |
| 1704 | * gfs2_dump_lockstate - print out the current lockstate |
| 1705 | * @sdp: the filesystem |
| 1706 | * @ub: the buffer to copy the information into |
| 1707 | * |
| 1708 | * If @ub is NULL, dump the lockstate to the console. |
| 1709 | * |
| 1710 | */ |
| 1711 | |
Adrian Bunk | 08bc2db | 2006-04-28 10:59:12 -0400 | [diff] [blame] | 1712 | static int gfs2_dump_lockstate(struct gfs2_sbd *sdp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1713 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1714 | struct gfs2_glock *gl; |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1715 | struct hlist_node *h; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1716 | unsigned int x; |
| 1717 | int error = 0; |
| 1718 | |
| 1719 | for (x = 0; x < GFS2_GL_HASH_SIZE; x++) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1720 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 1721 | read_lock(gl_lock_addr(x)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1722 | |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1723 | 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] | 1724 | if (gl->gl_sbd != sdp) |
| 1725 | continue; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1726 | |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1727 | error = dump_glock(NULL, gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1728 | if (error) |
| 1729 | break; |
| 1730 | } |
| 1731 | |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 1732 | read_unlock(gl_lock_addr(x)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1733 | |
| 1734 | if (error) |
| 1735 | break; |
| 1736 | } |
| 1737 | |
| 1738 | |
| 1739 | return error; |
| 1740 | } |
| 1741 | |
Steven Whitehouse | 8fbbfd2 | 2007-08-01 13:57:10 +0100 | [diff] [blame] | 1742 | /** |
| 1743 | * gfs2_scand - Look for cached glocks and inodes to toss from memory |
| 1744 | * @sdp: Pointer to GFS2 superblock |
| 1745 | * |
| 1746 | * One of these daemons runs, finding candidates to add to sd_reclaim_list. |
| 1747 | * See gfs2_glockd() |
| 1748 | */ |
| 1749 | |
| 1750 | static int gfs2_scand(void *data) |
| 1751 | { |
| 1752 | unsigned x; |
| 1753 | unsigned delay; |
| 1754 | |
| 1755 | while (!kthread_should_stop()) { |
| 1756 | for (x = 0; x < GFS2_GL_HASH_SIZE; x++) |
| 1757 | examine_bucket(scan_glock, NULL, x); |
| 1758 | if (freezing(current)) |
| 1759 | refrigerator(); |
| 1760 | delay = scand_secs; |
| 1761 | if (delay < 1) |
| 1762 | delay = 1; |
| 1763 | schedule_timeout_interruptible(delay * HZ); |
| 1764 | } |
| 1765 | |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 1771 | int __init gfs2_glock_init(void) |
| 1772 | { |
| 1773 | unsigned i; |
| 1774 | for(i = 0; i < GFS2_GL_HASH_SIZE; i++) { |
Steven Whitehouse | b639789 | 2006-09-12 10:10:01 -0400 | [diff] [blame] | 1775 | INIT_HLIST_HEAD(&gl_hash_table[i].hb_list); |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 1776 | } |
Steven Whitehouse | 087efdd | 2006-09-09 16:59:11 -0400 | [diff] [blame] | 1777 | #ifdef GL_HASH_LOCK_SZ |
| 1778 | for(i = 0; i < GL_HASH_LOCK_SZ; i++) { |
| 1779 | rwlock_init(&gl_hash_locks[i]); |
| 1780 | } |
| 1781 | #endif |
Steven Whitehouse | 8fbbfd2 | 2007-08-01 13:57:10 +0100 | [diff] [blame] | 1782 | |
| 1783 | scand_process = kthread_run(gfs2_scand, NULL, "gfs2_scand"); |
| 1784 | if (IS_ERR(scand_process)) |
| 1785 | return PTR_ERR(scand_process); |
| 1786 | |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 1787 | glock_workqueue = create_workqueue("glock_workqueue"); |
| 1788 | if (IS_ERR(glock_workqueue)) { |
| 1789 | kthread_stop(scand_process); |
| 1790 | return PTR_ERR(glock_workqueue); |
| 1791 | } |
| 1792 | |
Steven Whitehouse | 85d1da6 | 2006-09-07 14:40:21 -0400 | [diff] [blame] | 1793 | return 0; |
| 1794 | } |
| 1795 | |
Steven Whitehouse | 8fbbfd2 | 2007-08-01 13:57:10 +0100 | [diff] [blame] | 1796 | void gfs2_glock_exit(void) |
| 1797 | { |
Benjamin Marzinski | c4f68a1 | 2007-08-23 13:19:05 -0500 | [diff] [blame] | 1798 | destroy_workqueue(glock_workqueue); |
Steven Whitehouse | 8fbbfd2 | 2007-08-01 13:57:10 +0100 | [diff] [blame] | 1799 | kthread_stop(scand_process); |
| 1800 | } |
| 1801 | |
| 1802 | module_param(scand_secs, uint, S_IRUGO|S_IWUSR); |
| 1803 | MODULE_PARM_DESC(scand_secs, "The number of seconds between scand runs"); |
| 1804 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1805 | static int gfs2_glock_iter_next(struct gfs2_glock_iter *gi) |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1806 | { |
Steven Whitehouse | 7b08fc6 | 2007-07-24 13:53:36 +0100 | [diff] [blame] | 1807 | struct gfs2_glock *gl; |
| 1808 | |
Abhijith Das | a947e03 | 2007-08-21 09:57:29 -0500 | [diff] [blame] | 1809 | restart: |
Robert Peterson | 7a0079d | 2007-04-17 11:37:11 -0500 | [diff] [blame] | 1810 | read_lock(gl_lock_addr(gi->hash)); |
Steven Whitehouse | 7b08fc6 | 2007-07-24 13:53:36 +0100 | [diff] [blame] | 1811 | gl = gi->gl; |
| 1812 | if (gl) { |
Abhijith Das | a947e03 | 2007-08-21 09:57:29 -0500 | [diff] [blame] | 1813 | gi->gl = hlist_entry(gl->gl_list.next, |
| 1814 | struct gfs2_glock, gl_list); |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1815 | if (gi->gl) |
Steven Whitehouse | 7b08fc6 | 2007-07-24 13:53:36 +0100 | [diff] [blame] | 1816 | gfs2_glock_hold(gi->gl); |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1817 | } |
Robert Peterson | 7a0079d | 2007-04-17 11:37:11 -0500 | [diff] [blame] | 1818 | read_unlock(gl_lock_addr(gi->hash)); |
Steven Whitehouse | 7b08fc6 | 2007-07-24 13:53:36 +0100 | [diff] [blame] | 1819 | if (gl) |
| 1820 | gfs2_glock_put(gl); |
Abhijith Das | a947e03 | 2007-08-21 09:57:29 -0500 | [diff] [blame] | 1821 | if (gl && gi->gl == NULL) |
Steven Whitehouse | 7b08fc6 | 2007-07-24 13:53:36 +0100 | [diff] [blame] | 1822 | gi->hash++; |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1823 | while (gi->gl == NULL) { |
Steven Whitehouse | 7b08fc6 | 2007-07-24 13:53:36 +0100 | [diff] [blame] | 1824 | if (gi->hash >= GFS2_GL_HASH_SIZE) |
| 1825 | return 1; |
| 1826 | read_lock(gl_lock_addr(gi->hash)); |
| 1827 | gi->gl = hlist_entry(gl_hash_table[gi->hash].hb_list.first, |
| 1828 | struct gfs2_glock, gl_list); |
| 1829 | if (gi->gl) |
| 1830 | gfs2_glock_hold(gi->gl); |
| 1831 | read_unlock(gl_lock_addr(gi->hash)); |
Abhijith Das | a947e03 | 2007-08-21 09:57:29 -0500 | [diff] [blame] | 1832 | gi->hash++; |
Steven Whitehouse | 7b08fc6 | 2007-07-24 13:53:36 +0100 | [diff] [blame] | 1833 | } |
Abhijith Das | a947e03 | 2007-08-21 09:57:29 -0500 | [diff] [blame] | 1834 | |
| 1835 | if (gi->sdp != gi->gl->gl_sbd) |
| 1836 | goto restart; |
| 1837 | |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1838 | return 0; |
| 1839 | } |
| 1840 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1841 | static void gfs2_glock_iter_free(struct gfs2_glock_iter *gi) |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1842 | { |
Steven Whitehouse | 7b08fc6 | 2007-07-24 13:53:36 +0100 | [diff] [blame] | 1843 | if (gi->gl) |
| 1844 | gfs2_glock_put(gi->gl); |
Abhijith Das | a947e03 | 2007-08-21 09:57:29 -0500 | [diff] [blame] | 1845 | gi->gl = NULL; |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1848 | static void *gfs2_glock_seq_start(struct seq_file *seq, loff_t *pos) |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1849 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1850 | struct gfs2_glock_iter *gi = seq->private; |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1851 | loff_t n = *pos; |
| 1852 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1853 | gi->hash = 0; |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1854 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1855 | do { |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1856 | if (gfs2_glock_iter_next(gi)) { |
| 1857 | gfs2_glock_iter_free(gi); |
| 1858 | return NULL; |
| 1859 | } |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1860 | } while (n--); |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1861 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1862 | return gi->gl; |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1865 | static void *gfs2_glock_seq_next(struct seq_file *seq, void *iter_ptr, |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1866 | loff_t *pos) |
| 1867 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1868 | struct gfs2_glock_iter *gi = seq->private; |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1869 | |
| 1870 | (*pos)++; |
| 1871 | |
| 1872 | if (gfs2_glock_iter_next(gi)) { |
| 1873 | gfs2_glock_iter_free(gi); |
| 1874 | return NULL; |
| 1875 | } |
| 1876 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1877 | return gi->gl; |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1880 | static void gfs2_glock_seq_stop(struct seq_file *seq, void *iter_ptr) |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1881 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1882 | struct gfs2_glock_iter *gi = seq->private; |
| 1883 | gfs2_glock_iter_free(gi); |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1884 | } |
| 1885 | |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1886 | static int gfs2_glock_seq_show(struct seq_file *seq, void *iter_ptr) |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1887 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1888 | return dump_glock(seq, iter_ptr); |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Denis Cheng | 4ef2900 | 2007-07-31 18:31:11 +0800 | [diff] [blame] | 1891 | static const struct seq_operations gfs2_glock_seq_ops = { |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1892 | .start = gfs2_glock_seq_start, |
| 1893 | .next = gfs2_glock_seq_next, |
| 1894 | .stop = gfs2_glock_seq_stop, |
| 1895 | .show = gfs2_glock_seq_show, |
| 1896 | }; |
| 1897 | |
| 1898 | static int gfs2_debugfs_open(struct inode *inode, struct file *file) |
| 1899 | { |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1900 | int ret = seq_open_private(file, &gfs2_glock_seq_ops, |
| 1901 | sizeof(struct gfs2_glock_iter)); |
| 1902 | if (ret == 0) { |
| 1903 | struct seq_file *seq = file->private_data; |
| 1904 | struct gfs2_glock_iter *gi = seq->private; |
| 1905 | gi->sdp = inode->i_private; |
| 1906 | } |
| 1907 | return ret; |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | static const struct file_operations gfs2_debug_fops = { |
| 1911 | .owner = THIS_MODULE, |
| 1912 | .open = gfs2_debugfs_open, |
| 1913 | .read = seq_read, |
| 1914 | .llseek = seq_lseek, |
Steven Whitehouse | 6802e34 | 2008-05-21 17:03:22 +0100 | [diff] [blame] | 1915 | .release = seq_release_private, |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1916 | }; |
| 1917 | |
| 1918 | int gfs2_create_debugfs_file(struct gfs2_sbd *sdp) |
| 1919 | { |
Robert Peterson | 5f88209 | 2007-04-18 11:41:11 -0500 | [diff] [blame] | 1920 | sdp->debugfs_dir = debugfs_create_dir(sdp->sd_table_name, gfs2_root); |
| 1921 | if (!sdp->debugfs_dir) |
| 1922 | return -ENOMEM; |
| 1923 | sdp->debugfs_dentry_glocks = debugfs_create_file("glocks", |
| 1924 | S_IFREG | S_IRUGO, |
| 1925 | sdp->debugfs_dir, sdp, |
| 1926 | &gfs2_debug_fops); |
| 1927 | if (!sdp->debugfs_dentry_glocks) |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1928 | return -ENOMEM; |
| 1929 | |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
| 1933 | void gfs2_delete_debugfs_file(struct gfs2_sbd *sdp) |
| 1934 | { |
Robert Peterson | 5f88209 | 2007-04-18 11:41:11 -0500 | [diff] [blame] | 1935 | if (sdp && sdp->debugfs_dir) { |
| 1936 | if (sdp->debugfs_dentry_glocks) { |
| 1937 | debugfs_remove(sdp->debugfs_dentry_glocks); |
| 1938 | sdp->debugfs_dentry_glocks = NULL; |
| 1939 | } |
| 1940 | debugfs_remove(sdp->debugfs_dir); |
| 1941 | sdp->debugfs_dir = NULL; |
| 1942 | } |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | int gfs2_register_debugfs(void) |
| 1946 | { |
| 1947 | gfs2_root = debugfs_create_dir("gfs2", NULL); |
| 1948 | return gfs2_root ? 0 : -ENOMEM; |
| 1949 | } |
| 1950 | |
| 1951 | void gfs2_unregister_debugfs(void) |
| 1952 | { |
| 1953 | debugfs_remove(gfs2_root); |
Robert Peterson | 5f88209 | 2007-04-18 11:41:11 -0500 | [diff] [blame] | 1954 | gfs2_root = NULL; |
Robert Peterson | 7c52b16 | 2007-03-16 10:26:37 +0000 | [diff] [blame] | 1955 | } |