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