1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #define DEBUG_SUBSYSTEM S_LDLM
27 # include <linux/slab.h>
28 # include <linux/module.h>
29 # include <linux/lustre_dlm.h>
30 # include <linux/lustre_mds.h>
32 # include <liblustre.h>
33 # include <linux/kp30.h>
36 #include <linux/obd_class.h>
37 #include "ldlm_internal.h"
39 //struct lustre_lock ldlm_everything_lock;
42 char *ldlm_lockname[] = {
51 char *ldlm_typename[] = {
57 char *ldlm_it2str(int it)
64 case (IT_OPEN | IT_CREAT):
77 CERROR("Unknown intent %d\n", it);
83 extern kmem_cache_t *ldlm_lock_slab;
84 struct lustre_lock ldlm_handle_lock;
86 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
88 static ldlm_res_compat ldlm_res_compat_table[] = {
89 [LDLM_PLAIN] ldlm_plain_compat,
90 [LDLM_EXTENT] ldlm_extent_compat,
93 static ldlm_res_policy ldlm_intent_policy_func;
95 static int ldlm_plain_policy(struct ldlm_namespace *ns, struct ldlm_lock **lock,
96 void *req_cookie, ldlm_mode_t mode, int flags,
99 if ((flags & LDLM_FL_HAS_INTENT) && ldlm_intent_policy_func) {
100 return ldlm_intent_policy_func(ns, lock, req_cookie, mode,
107 static ldlm_res_policy ldlm_res_policy_table[] = {
108 [LDLM_PLAIN] ldlm_plain_policy,
109 [LDLM_EXTENT] ldlm_extent_policy,
112 void ldlm_register_intent(ldlm_res_policy arg)
114 ldlm_intent_policy_func = arg;
117 void ldlm_unregister_intent(void)
119 ldlm_intent_policy_func = NULL;
123 * REFCOUNTED LOCK OBJECTS
128 * Lock refcounts, during creation:
129 * - one special one for allocation, dec'd only once in destroy
130 * - one for being a lock that's in-use
131 * - one for the addref associated with a new lock
133 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
135 atomic_inc(&lock->l_refc);
139 void ldlm_lock_put(struct ldlm_lock *lock)
141 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
144 if (atomic_dec_and_test(&lock->l_refc)) {
145 l_lock(&ns->ns_lock);
146 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
147 LASSERT(lock->l_destroyed);
148 LASSERT(list_empty(&lock->l_res_link));
150 spin_lock(&ns->ns_counter_lock);
152 spin_unlock(&ns->ns_counter_lock);
154 ldlm_resource_putref(lock->l_resource);
155 lock->l_resource = NULL;
157 class_export_put(lock->l_export);
160 LDLM_LOCK_PUT(lock->l_parent);
162 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
163 l_unlock(&ns->ns_lock);
169 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
172 l_lock(&lock->l_resource->lr_namespace->ns_lock);
173 if (!list_empty(&lock->l_lru)) {
174 list_del_init(&lock->l_lru);
175 lock->l_resource->lr_namespace->ns_nr_unused--;
176 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
178 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
182 /* This used to have a 'strict' flact, which recovery would use to mark an
183 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
184 * shall explain why it's gone: with the new hash table scheme, once you call
185 * ldlm_lock_destroy, you can never drop your final references on this lock.
186 * Because it's not in the hash table anymore. -phil */
187 void ldlm_lock_destroy(struct ldlm_lock *lock)
190 l_lock(&lock->l_resource->lr_namespace->ns_lock);
192 if (!list_empty(&lock->l_children)) {
193 LDLM_ERROR(lock, "still has children (%p)!",
194 lock->l_children.next);
195 ldlm_lock_dump(D_ERROR, lock);
198 if (lock->l_readers || lock->l_writers) {
199 LDLM_ERROR(lock, "lock still has references");
200 ldlm_lock_dump(D_ERROR, lock);
204 if (!list_empty(&lock->l_res_link)) {
205 ldlm_lock_dump(D_ERROR, lock);
209 if (lock->l_destroyed) {
210 LASSERT(list_empty(&lock->l_lru));
211 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
215 lock->l_destroyed = 1;
217 list_del_init(&lock->l_export_chain);
218 ldlm_lock_remove_from_lru(lock);
219 class_handle_unhash(&lock->l_handle);
222 /* Wake anyone waiting for this lock */
223 /* FIXME: I should probably add yet another flag, instead of using
224 * l_export to only call this on clients */
226 class_export_put(lock->l_export);
227 lock->l_export = NULL;
228 if (lock->l_export && lock->l_completion_ast)
229 lock->l_completion_ast(lock, 0);
232 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
237 /* this is called by portals_handle2object with the handle lock taken */
238 static void lock_handle_addref(void *lock)
240 LDLM_LOCK_GET((struct ldlm_lock *)lock);
244 * usage: pass in a resource on which you have done ldlm_resource_get
245 * pass in a parent lock on which you have done a ldlm_lock_get
246 * after return, ldlm_*_put the resource and parent
247 * returns: lock with refcount 1
249 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
250 struct ldlm_resource *resource)
252 struct ldlm_lock *lock;
255 if (resource == NULL)
258 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, SLAB_KERNEL, sizeof(*lock));
262 lock->l_resource = ldlm_resource_getref(resource);
264 atomic_set(&lock->l_refc, 2);
265 INIT_LIST_HEAD(&lock->l_children);
266 INIT_LIST_HEAD(&lock->l_res_link);
267 INIT_LIST_HEAD(&lock->l_lru);
268 INIT_LIST_HEAD(&lock->l_export_chain);
269 INIT_LIST_HEAD(&lock->l_pending_chain);
270 init_waitqueue_head(&lock->l_waitq);
272 spin_lock(&resource->lr_namespace->ns_counter_lock);
273 resource->lr_namespace->ns_locks++;
274 spin_unlock(&resource->lr_namespace->ns_counter_lock);
276 if (parent != NULL) {
277 l_lock(&parent->l_resource->lr_namespace->ns_lock);
278 lock->l_parent = LDLM_LOCK_GET(parent);
279 list_add(&lock->l_childof, &parent->l_children);
280 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
283 INIT_LIST_HEAD(&lock->l_handle.h_link);
284 class_handle_hash(&lock->l_handle, lock_handle_addref);
289 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
290 struct ldlm_res_id new_resid)
292 struct ldlm_resource *oldres = lock->l_resource;
295 l_lock(&ns->ns_lock);
296 if (memcmp(&new_resid, &lock->l_resource->lr_name,
297 sizeof(lock->l_resource->lr_name)) == 0) {
299 l_unlock(&ns->ns_lock);
303 LASSERT(new_resid.name[0] != 0);
305 /* This function assumes that the lock isn't on any lists */
306 LASSERT(list_empty(&lock->l_res_link));
308 lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
309 lock->l_resource->lr_type, 1);
310 if (lock->l_resource == NULL) {
315 /* ...and the flowers are still standing! */
316 ldlm_resource_putref(oldres);
318 l_unlock(&ns->ns_lock);
326 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
328 lockh->cookie = lock->l_handle.h_cookie;
331 /* if flags: atomically get the lock and set the flags.
332 * Return NULL if flag already set
335 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
337 struct ldlm_namespace *ns;
338 struct ldlm_lock *lock = NULL, *retval = NULL;
343 lock = class_handle2object(handle->cookie);
347 LASSERT(lock->l_resource != NULL);
348 ns = lock->l_resource->lr_namespace;
351 l_lock(&ns->ns_lock);
353 /* It's unlikely but possible that someone marked the lock as
354 * destroyed after we did handle2object on it */
355 if (lock->l_destroyed) {
356 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
361 if (flags && (lock->l_flags & flags)) {
367 lock->l_flags |= flags;
372 l_unlock(&ns->ns_lock);
376 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
377 struct lustre_handle *handle)
379 struct ldlm_lock *retval = NULL;
381 l_lock(&ns->ns_lock);
382 retval = __ldlm_handle2lock(handle, 0);
383 l_unlock(&ns->ns_lock);
388 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
390 return lockmode_compat(a->l_req_mode, b->l_req_mode);
393 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
395 ldlm_res2desc(lock->l_resource, &desc->l_resource);
396 desc->l_req_mode = lock->l_req_mode;
397 desc->l_granted_mode = lock->l_granted_mode;
398 memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
399 memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
402 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
403 struct ldlm_lock *new,
404 void *data, int datalen)
406 struct ldlm_ast_work *w;
409 l_lock(&lock->l_resource->lr_namespace->ns_lock);
410 if (new && (lock->l_flags & LDLM_FL_AST_SENT))
413 OBD_ALLOC(w, sizeof(*w));
420 w->w_datalen = datalen;
422 lock->l_flags |= LDLM_FL_AST_SENT;
424 ldlm_lock2desc(new, &w->w_desc);
427 w->w_lock = LDLM_LOCK_GET(lock);
428 list_add(&w->w_list, lock->l_resource->lr_tmp);
431 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
435 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
437 struct ldlm_lock *lock;
439 lock = ldlm_handle2lock(lockh);
440 ldlm_lock_addref_internal(lock, mode);
444 /* only called for local locks */
445 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
447 l_lock(&lock->l_resource->lr_namespace->ns_lock);
448 ldlm_lock_remove_from_lru(lock);
449 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
453 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
455 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
458 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
460 struct ldlm_namespace *ns;
463 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
464 ns = lock->l_resource->lr_namespace;
465 l_lock(&ns->ns_lock);
466 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR) {
467 LASSERT(lock->l_readers > 0);
470 LASSERT(lock->l_writers > 0);
474 if (lock->l_flags & LDLM_FL_LOCAL &&
475 !lock->l_readers && !lock->l_writers) {
476 /* If this is a local lock on a server namespace and this was
477 * the last reference, cancel the lock. */
478 CDEBUG(D_INFO, "forcing cancel of local lock\n");
479 lock->l_flags |= LDLM_FL_CBPENDING;
482 if (!lock->l_readers && !lock->l_writers &&
483 (lock->l_flags & LDLM_FL_CBPENDING)) {
484 /* If we received a blocked AST and this was the last reference,
485 * run the callback. */
486 if (!ns->ns_client && lock->l_export)
487 CERROR("FL_CBPENDING set on non-local lock--just a "
490 LDLM_DEBUG(lock, "final decref done on cbpending lock");
491 l_unlock(&ns->ns_lock);
493 /* FIXME: need a real 'desc' here */
494 if (lock->l_blocking_ast != NULL)
495 lock->l_blocking_ast(lock, NULL, lock->l_data,
498 LDLM_DEBUG(lock, "No blocking AST?");
499 } else if (ns->ns_client && !lock->l_readers && !lock->l_writers) {
500 /* If this is a client-side namespace and this was the last
501 * reference, put it on the LRU. */
502 LASSERT(list_empty(&lock->l_lru));
503 LASSERT(ns->ns_nr_unused >= 0);
504 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
506 l_unlock(&ns->ns_lock);
509 l_unlock(&ns->ns_lock);
512 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
517 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
519 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
520 LASSERT(lock != NULL);
521 ldlm_lock_decref_internal(lock, mode);
525 /* This will drop a lock reference and mark it for destruction, but will not
526 * necessarily cancel the lock before returning. */
527 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
529 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
532 LASSERT(lock != NULL);
534 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
535 l_lock(&lock->l_resource->lr_namespace->ns_lock);
536 lock->l_flags |= LDLM_FL_CBPENDING;
537 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
538 ldlm_lock_decref_internal(lock, mode);
542 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
543 struct list_head *queue)
545 struct list_head *tmp, *pos;
548 list_for_each_safe(tmp, pos, queue) {
549 struct ldlm_lock *child;
550 ldlm_res_compat compat;
552 child = list_entry(tmp, struct ldlm_lock, l_res_link);
556 compat = ldlm_res_compat_table[child->l_resource->lr_type];
557 if (compat && compat(child, lock)) {
558 CDEBUG(D_OTHER, "compat function succeded, next.\n");
561 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
562 CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
568 if (send_cbs && child->l_blocking_ast != NULL) {
569 CDEBUG(D_OTHER, "lock %p incompatible; sending "
570 "blocking AST.\n", child);
571 ldlm_add_ast_work_item(child, lock, NULL, 0);
578 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
583 l_lock(&lock->l_resource->lr_namespace->ns_lock);
584 rc = ldlm_lock_compat_list(lock, send_cbs,
585 &lock->l_resource->lr_granted);
586 /* FIXME: should we be sending ASTs to converting? */
588 rc = ldlm_lock_compat_list
589 (lock, send_cbs, &lock->l_resource->lr_converting);
591 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
596 * - ldlm_lock_enqueue
597 * - ldlm_reprocess_queue
598 * - ldlm_lock_convert
600 void ldlm_grant_lock(struct ldlm_lock *lock, void *data, int datalen)
602 struct ldlm_resource *res = lock->l_resource;
605 l_lock(&lock->l_resource->lr_namespace->ns_lock);
606 ldlm_resource_add_lock(res, &res->lr_granted, lock);
607 lock->l_granted_mode = lock->l_req_mode;
609 if (lock->l_granted_mode < res->lr_most_restr)
610 res->lr_most_restr = lock->l_granted_mode;
612 if (lock->l_completion_ast != NULL)
613 ldlm_add_ast_work_item(lock, NULL, data, datalen);
615 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
619 /* returns a referenced lock or NULL. See the flag descriptions below, in the
620 * comment above ldlm_lock_match */
621 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
622 struct ldlm_extent *extent,
623 struct ldlm_lock *old_lock, void *data,
626 struct ldlm_lock *lock;
627 struct list_head *tmp;
629 list_for_each(tmp, queue) {
630 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
632 if (lock == old_lock)
635 /* llite sometimes wants to match locks that will be
636 * canceled when their users drop, but we allow it to match
637 * if it passes in CBPENDING and the lock still has users.
638 * this is generally only going to be used by children
639 * whose parents already hold a lock so forward progress
640 * can still happen. */
641 if (lock->l_flags & LDLM_FL_CBPENDING &&
642 !(flags & LDLM_FL_CBPENDING))
644 if (lock->l_flags & LDLM_FL_CBPENDING &&
645 lock->l_readers == 0 && lock->l_writers == 0)
648 if (lock->l_req_mode != mode)
651 if (lock->l_resource->lr_type == LDLM_EXTENT &&
652 (lock->l_extent.start > extent->start ||
653 lock->l_extent.end < extent->end))
656 if (lock->l_destroyed)
659 if ((flags & LDLM_FL_LOCAL_ONLY) &&
660 !(lock->l_flags & LDLM_FL_LOCAL))
663 if ((flags & LDLM_FL_MATCH_DATA) && lock->l_data != data) {
664 LDLM_DEBUG(lock, "data mismatch: have %p, want %p",
669 ldlm_lock_addref_internal(lock, mode);
676 /* Can be called in two ways:
678 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
679 * for a duplicate of.
681 * Otherwise, all of the fields must be filled in, to match against.
683 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
684 * server (ie, connh is NULL)
685 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
686 * list will be considered
687 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
688 * to be canceled can still be matched as long as they still have reader
689 * or writer refernces
690 * If 'flags' contains LDLM_FL_MATCH_DATA, then only match a lock if the opaque
693 * Returns 1 if it finds an already-existing lock that is compatible; in this
694 * case, lockh is filled in with a addref()ed lock
696 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
697 struct ldlm_res_id *res_id, __u32 type, void *cookie,
698 int cookielen, ldlm_mode_t mode, void *data,
699 struct lustre_handle *lockh)
701 struct ldlm_resource *res;
702 struct ldlm_lock *lock, *old_lock = NULL;
707 old_lock = ldlm_handle2lock(lockh);
710 ns = old_lock->l_resource->lr_namespace;
711 res_id = &old_lock->l_resource->lr_name;
712 type = old_lock->l_resource->lr_type;
713 mode = old_lock->l_req_mode;
716 res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
718 LASSERT(old_lock == NULL);
722 l_lock(&ns->ns_lock);
724 lock = search_queue(&res->lr_granted, mode, cookie, old_lock, data,
728 if (flags & LDLM_FL_BLOCK_GRANTED)
730 lock = search_queue(&res->lr_converting, mode, cookie, old_lock, data,
734 lock = search_queue(&res->lr_waiting, mode, cookie, old_lock, data,
741 ldlm_resource_putref(res);
742 l_unlock(&ns->ns_lock);
745 ldlm_lock2handle(lock, lockh);
746 if (lock->l_completion_ast)
747 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
751 LDLM_DEBUG(lock, "matched");
753 LDLM_DEBUG_NOLOCK("not matched");
756 LDLM_LOCK_PUT(old_lock);
761 /* Returns a referenced lock */
762 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
763 struct lustre_handle *parent_lock_handle,
764 struct ldlm_res_id res_id, __u32 type,
766 ldlm_blocking_callback blocking,
769 struct ldlm_resource *res, *parent_res = NULL;
770 struct ldlm_lock *lock, *parent_lock = NULL;
773 if (parent_lock_handle) {
774 parent_lock = ldlm_handle2lock(parent_lock_handle);
776 parent_res = parent_lock->l_resource;
779 res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
783 lock = ldlm_lock_new(parent_lock, res);
784 ldlm_resource_putref(res);
785 if (parent_lock != NULL)
786 LDLM_LOCK_PUT(parent_lock);
791 lock->l_req_mode = mode;
793 lock->l_blocking_ast = blocking;
798 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
799 struct ldlm_lock **lockp,
800 void *cookie, int cookie_len,
802 ldlm_completion_callback completion)
804 struct ldlm_resource *res;
805 struct ldlm_lock *lock = *lockp;
807 ldlm_res_policy policy;
810 res = lock->l_resource;
812 if (res->lr_type == LDLM_EXTENT)
813 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
815 /* policies are not executed on the client or during replay */
816 local = res->lr_namespace->ns_client;
817 if (!local && !(*flags & LDLM_FL_REPLAY) &&
818 (policy = ldlm_res_policy_table[res->lr_type])) {
820 rc = policy(ns, lockp, cookie, lock->l_req_mode, *flags, NULL);
821 if (rc == ELDLM_LOCK_CHANGED) {
822 res = lock->l_resource;
823 *flags |= LDLM_FL_LOCK_CHANGED;
824 } else if (rc == ELDLM_LOCK_REPLACED) {
825 /* The lock that was returned has already been granted,
826 * and placed into lockp. Destroy the old one and our
827 * work here is done. */
828 ldlm_lock_destroy(lock);
830 *flags |= LDLM_FL_LOCK_CHANGED;
832 } else if (rc == ELDLM_LOCK_ABORTED) {
833 ldlm_lock_destroy(lock);
838 l_lock(&ns->ns_lock);
839 if (local && lock->l_req_mode == lock->l_granted_mode) {
840 /* The server returned a blocked lock, but it was granted before
841 * we got a chance to actually enqueue it. We don't need to do
843 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
844 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
848 /* This distinction between local lock trees is very important; a client
849 * namespace only has information about locks taken by that client, and
850 * thus doesn't have enough information to decide for itself if it can
851 * be granted (below). In this case, we do exactly what the server
852 * tells us to do, as dictated by the 'flags'.
854 * We do exactly the same thing during recovery, when the server is
855 * more or less trusting the clients not to lie.
857 * FIXME (bug 268): Detect obvious lies by checking compatibility in
858 * granted/converting queues. */
859 ldlm_resource_unlink_lock(lock);
861 if (*flags & LDLM_FL_BLOCK_CONV)
862 ldlm_resource_add_lock(res, &res->lr_converting, lock);
863 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
864 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
866 ldlm_grant_lock(lock, NULL, 0);
868 } else if (*flags & LDLM_FL_REPLAY) {
869 if (*flags & LDLM_FL_BLOCK_CONV) {
870 ldlm_resource_add_lock(res, &res->lr_converting, lock);
872 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
873 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
875 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
876 ldlm_grant_lock(lock, NULL, 0);
879 /* If no flags, fall through to normal enqueue path. */
882 /* FIXME: We may want to optimize by checking lr_most_restr */
883 if (!list_empty(&res->lr_converting)) {
884 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
885 *flags |= LDLM_FL_BLOCK_CONV;
888 if (!list_empty(&res->lr_waiting)) {
889 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
890 *flags |= LDLM_FL_BLOCK_WAIT;
893 if (!ldlm_lock_compat(lock, 0)) {
894 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
895 *flags |= LDLM_FL_BLOCK_GRANTED;
898 ldlm_grant_lock(lock, NULL, 0);
901 /* Don't set 'completion_ast' until here so that if the lock is granted
902 * immediately we don't do an unnecessary completion call. */
903 lock->l_completion_ast = completion;
904 l_unlock(&ns->ns_lock);
908 /* Must be called with namespace taken: queue is waiting or converting. */
909 static int ldlm_reprocess_queue(struct ldlm_resource *res,
910 struct list_head *queue)
912 struct list_head *tmp, *pos;
915 list_for_each_safe(tmp, pos, queue) {
916 struct ldlm_lock *pending;
917 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
919 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
921 if (!ldlm_lock_compat(pending, 1))
924 list_del_init(&pending->l_res_link);
925 ldlm_grant_lock(pending, NULL, 0);
931 int ldlm_run_ast_work(struct list_head *rpc_list)
933 struct list_head *tmp, *pos;
937 list_for_each_safe(tmp, pos, rpc_list) {
938 struct ldlm_ast_work *w =
939 list_entry(tmp, struct ldlm_ast_work, w_list);
941 /* It's possible to receive a completion AST before we've set
942 * the l_completion_ast pointer: either because the AST arrived
943 * before the reply, or simply because there's a small race
944 * window between receiving the reply and finishing the local
947 * This can't happen with the blocking_ast, however, because we
948 * will never call the local blocking_ast until we drop our
949 * reader/writer reference, which we won't do until we get the
950 * reply and finish enqueueing. */
952 LASSERT(w->w_lock->l_blocking_ast != NULL);
953 rc = w->w_lock->l_blocking_ast
954 (w->w_lock, &w->w_desc, w->w_data,
956 } else if (w->w_lock->l_completion_ast != NULL) {
957 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
965 CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
966 "disconnect client\n");
967 LDLM_LOCK_PUT(w->w_lock);
968 list_del(&w->w_list);
969 OBD_FREE(w, sizeof(*w));
974 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
976 ldlm_reprocess_all(res);
977 return LDLM_ITER_CONTINUE;
980 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
982 (void)ldlm_namespace_foreach_res(ns, reprocess_one_queue, NULL);
985 void ldlm_reprocess_all(struct ldlm_resource *res)
987 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
991 /* Local lock trees don't get reprocessed. */
992 if (res->lr_namespace->ns_client) {
998 l_lock(&res->lr_namespace->ns_lock);
999 res->lr_tmp = &rpc_list;
1001 ldlm_reprocess_queue(res, &res->lr_converting);
1002 if (list_empty(&res->lr_converting))
1003 ldlm_reprocess_queue(res, &res->lr_waiting);
1006 l_unlock(&res->lr_namespace->ns_lock);
1008 rc = ldlm_run_ast_work(&rpc_list);
1009 if (rc == -ERESTART)
1014 void ldlm_cancel_callback(struct ldlm_lock *lock)
1016 l_lock(&lock->l_resource->lr_namespace->ns_lock);
1017 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1018 lock->l_flags |= LDLM_FL_CANCEL;
1019 if (lock->l_blocking_ast) {
1020 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1021 lock->l_blocking_ast(lock, NULL, lock->l_data,
1025 LDLM_DEBUG(lock, "no blocking ast");
1028 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1031 void ldlm_lock_cancel(struct ldlm_lock *lock)
1033 struct ldlm_resource *res;
1034 struct ldlm_namespace *ns;
1037 ldlm_del_waiting_lock(lock);
1039 res = lock->l_resource;
1040 ns = res->lr_namespace;
1042 l_lock(&ns->ns_lock);
1043 /* Please do not, no matter how tempting, remove this LBUG without
1044 * talking to me first. -phik */
1045 if (lock->l_readers || lock->l_writers) {
1046 LDLM_DEBUG(lock, "lock still has references");
1047 ldlm_lock_dump(D_OTHER, lock);
1051 ldlm_cancel_callback(lock); /* XXX FIXME bug 1030 */
1053 ldlm_resource_unlink_lock(lock);
1054 ldlm_lock_destroy(lock);
1055 l_unlock(&ns->ns_lock);
1059 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1061 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1067 lock->l_data = data;
1069 LDLM_LOCK_PUT(lock);
1074 /* This function is only called from one thread (per export); no locking around
1075 * the list ops needed */
1076 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1078 struct list_head *iter, *n;
1080 list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
1081 struct ldlm_lock *lock;
1082 struct ldlm_resource *res;
1083 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1084 res = ldlm_resource_getref(lock->l_resource);
1085 LDLM_DEBUG(lock, "export %p", exp);
1086 ldlm_lock_cancel(lock);
1087 ldlm_reprocess_all(res);
1088 ldlm_resource_putref(res);
1092 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1095 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1096 struct ldlm_resource *res;
1097 struct ldlm_namespace *ns;
1103 res = lock->l_resource;
1104 ns = res->lr_namespace;
1106 l_lock(&ns->ns_lock);
1108 lock->l_req_mode = new_mode;
1109 ldlm_resource_unlink_lock(lock);
1111 /* If this is a local resource, put it on the appropriate list. */
1112 if (res->lr_namespace->ns_client) {
1113 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1114 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1116 /* This should never happen, because of the way the
1117 * server handles conversions. */
1120 res->lr_tmp = &rpc_list;
1121 ldlm_grant_lock(lock, NULL, 0);
1124 /* FIXME: completion handling not with ns_lock held ! */
1125 if (lock->l_completion_ast)
1126 lock->l_completion_ast(lock, 0, NULL);
1129 /* FIXME: We should try the conversion right away and possibly
1130 * return success without the need for an extra AST */
1131 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1132 *flags |= LDLM_FL_BLOCK_CONV;
1135 l_unlock(&ns->ns_lock);
1138 ldlm_run_ast_work(&rpc_list);
1142 void ldlm_lock_dump(int level, struct ldlm_lock *lock)
1145 struct obd_device *obd;
1147 if (!((portal_debug | D_ERROR) & level))
1150 if (RES_VERSION_SIZE != 4)
1154 CDEBUG(level, " NULL LDLM lock\n");
1158 snprintf(ver, sizeof(ver), "%x %x %x %x",
1159 lock->l_version[0], lock->l_version[1],
1160 lock->l_version[2], lock->l_version[3]);
1162 CDEBUG(level, " -- Lock dump: %p (%s) (rc: %d)\n", lock, ver,
1163 atomic_read(&lock->l_refc));
1164 obd = class_conn2obd(lock->l_connh);
1165 if (lock->l_export && lock->l_export->exp_connection) {
1166 CDEBUG(level, " Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1167 lock->l_export->exp_connection->c_peer.peer_nid,
1168 lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1169 lock->l_remote_handle.cookie);
1170 } else if (obd == NULL) {
1171 CDEBUG(level, " Node: local\n");
1173 struct obd_import *imp = obd->u.cli.cl_import;
1174 CDEBUG(level, " Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1175 imp->imp_connection->c_peer.peer_nid,
1176 imp->imp_connection->c_peer.peer_ni->pni_name,
1177 lock->l_remote_handle.cookie);
1179 CDEBUG(level, " Parent: %p\n", lock->l_parent);
1180 CDEBUG(level, " Resource: %p ("LPD64")\n", lock->l_resource,
1181 lock->l_resource->lr_name.name[0]);
1182 CDEBUG(level, " Requested mode: %d, granted mode: %d\n",
1183 (int)lock->l_req_mode, (int)lock->l_granted_mode);
1184 CDEBUG(level, " Readers: %u ; Writers; %u\n",
1185 lock->l_readers, lock->l_writers);
1186 if (lock->l_resource->lr_type == LDLM_EXTENT)
1187 CDEBUG(level, " Extent: "LPU64" -> "LPU64"\n",
1188 lock->l_extent.start, lock->l_extent.end);
1191 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1193 struct ldlm_lock *lock;
1195 lock = ldlm_handle2lock(lockh);
1199 ldlm_lock_dump(D_OTHER, lock);
1201 LDLM_LOCK_PUT(lock);