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):
75 CERROR("Unknown intent %d\n", it);
81 extern kmem_cache_t *ldlm_lock_slab;
82 struct lustre_lock ldlm_handle_lock;
84 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
86 static ldlm_res_compat ldlm_res_compat_table[] = {
87 [LDLM_PLAIN] ldlm_plain_compat,
88 [LDLM_EXTENT] ldlm_extent_compat,
91 static ldlm_res_policy ldlm_intent_policy_func;
93 static int ldlm_plain_policy(struct ldlm_namespace *ns, struct ldlm_lock **lock,
94 void *req_cookie, ldlm_mode_t mode, int flags,
97 if ((flags & LDLM_FL_HAS_INTENT) && ldlm_intent_policy_func) {
98 return ldlm_intent_policy_func(ns, lock, req_cookie, mode,
105 static ldlm_res_policy ldlm_res_policy_table[] = {
106 [LDLM_PLAIN] ldlm_plain_policy,
107 [LDLM_EXTENT] ldlm_extent_policy,
110 void ldlm_register_intent(ldlm_res_policy arg)
112 ldlm_intent_policy_func = arg;
115 void ldlm_unregister_intent(void)
117 ldlm_intent_policy_func = NULL;
121 * REFCOUNTED LOCK OBJECTS
126 * Lock refcounts, during creation:
127 * - one special one for allocation, dec'd only once in destroy
128 * - one for being a lock that's in-use
129 * - one for the addref associated with a new lock
131 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
133 atomic_inc(&lock->l_refc);
137 void ldlm_lock_put(struct ldlm_lock *lock)
139 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
142 if (atomic_dec_and_test(&lock->l_refc)) {
143 l_lock(&ns->ns_lock);
144 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
145 LASSERT(lock->l_destroyed);
146 LASSERT(list_empty(&lock->l_res_link));
148 spin_lock(&ns->ns_counter_lock);
150 spin_unlock(&ns->ns_counter_lock);
152 ldlm_resource_putref(lock->l_resource);
153 lock->l_resource = NULL;
156 LDLM_LOCK_PUT(lock->l_parent);
158 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
159 l_unlock(&ns->ns_lock);
165 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
168 l_lock(&lock->l_resource->lr_namespace->ns_lock);
169 if (!list_empty(&lock->l_lru)) {
170 list_del_init(&lock->l_lru);
171 lock->l_resource->lr_namespace->ns_nr_unused--;
172 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
174 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
178 /* This used to have a 'strict' flact, which recovery would use to mark an
179 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
180 * shall explain why it's gone: with the new hash table scheme, once you call
181 * ldlm_lock_destroy, you can never drop your final references on this lock.
182 * Because it's not in the hash table anymore. -phil */
183 void ldlm_lock_destroy(struct ldlm_lock *lock)
186 l_lock(&lock->l_resource->lr_namespace->ns_lock);
188 if (!list_empty(&lock->l_children)) {
189 LDLM_ERROR(lock, "still has children (%p)!",
190 lock->l_children.next);
191 ldlm_lock_dump(D_ERROR, lock);
194 if (lock->l_readers || lock->l_writers) {
195 LDLM_ERROR(lock, "lock still has references");
196 ldlm_lock_dump(D_ERROR, lock);
200 if (!list_empty(&lock->l_res_link)) {
201 ldlm_lock_dump(D_ERROR, lock);
205 if (lock->l_destroyed) {
206 LASSERT(list_empty(&lock->l_lru));
207 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
211 lock->l_destroyed = 1;
213 list_del_init(&lock->l_export_chain);
214 ldlm_lock_remove_from_lru(lock);
215 class_handle_unhash(&lock->l_handle);
218 /* Wake anyone waiting for this lock */
219 /* FIXME: I should probably add yet another flag, instead of using
220 * l_export to only call this on clients */
221 lock->l_export = NULL;
222 if (lock->l_export && lock->l_completion_ast)
223 lock->l_completion_ast(lock, 0);
226 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
231 /* this is called by portals_handle2object with the handle lock taken */
232 static void lock_handle_addref(void *lock)
234 LDLM_LOCK_GET((struct ldlm_lock *)lock);
238 * usage: pass in a resource on which you have done ldlm_resource_get
239 * pass in a parent lock on which you have done a ldlm_lock_get
240 * after return, ldlm_*_put the resource and parent
241 * returns: lock with refcount 1
243 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
244 struct ldlm_resource *resource)
246 struct ldlm_lock *lock;
249 if (resource == NULL)
252 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, SLAB_KERNEL, sizeof(*lock));
256 lock->l_resource = ldlm_resource_getref(resource);
258 atomic_set(&lock->l_refc, 2);
259 INIT_LIST_HEAD(&lock->l_children);
260 INIT_LIST_HEAD(&lock->l_res_link);
261 INIT_LIST_HEAD(&lock->l_lru);
262 INIT_LIST_HEAD(&lock->l_export_chain);
263 INIT_LIST_HEAD(&lock->l_pending_chain);
264 init_waitqueue_head(&lock->l_waitq);
266 spin_lock(&resource->lr_namespace->ns_counter_lock);
267 resource->lr_namespace->ns_locks++;
268 spin_unlock(&resource->lr_namespace->ns_counter_lock);
270 if (parent != NULL) {
271 l_lock(&parent->l_resource->lr_namespace->ns_lock);
272 lock->l_parent = LDLM_LOCK_GET(parent);
273 list_add(&lock->l_childof, &parent->l_children);
274 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
277 INIT_LIST_HEAD(&lock->l_handle.h_link);
278 class_handle_hash(&lock->l_handle, lock_handle_addref);
283 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
284 struct ldlm_res_id new_resid)
286 struct ldlm_resource *oldres = lock->l_resource;
289 l_lock(&ns->ns_lock);
290 if (memcmp(&new_resid, &lock->l_resource->lr_name,
291 sizeof(lock->l_resource->lr_name)) == 0) {
293 l_unlock(&ns->ns_lock);
297 LASSERT(new_resid.name[0] != 0);
299 /* This function assumes that the lock isn't on any lists */
300 LASSERT(list_empty(&lock->l_res_link));
302 lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
303 lock->l_resource->lr_type, 1);
304 if (lock->l_resource == NULL) {
309 /* ...and the flowers are still standing! */
310 ldlm_resource_putref(oldres);
312 l_unlock(&ns->ns_lock);
320 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
322 lockh->cookie = lock->l_handle.h_cookie;
325 /* if flags: atomically get the lock and set the flags.
326 * Return NULL if flag already set
329 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
331 struct ldlm_lock *lock = NULL, *retval = NULL;
336 lock = class_handle2object(handle->cookie);
340 LASSERT(lock->l_resource != NULL);
341 LASSERT(lock->l_resource->lr_namespace != NULL);
343 l_lock(&lock->l_resource->lr_namespace->ns_lock);
345 /* It's unlikely but possible that someone marked the lock as
346 * destroyed after we did handle2object on it */
347 if (lock->l_destroyed) {
348 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
353 if (flags && (lock->l_flags & flags)) {
359 lock->l_flags |= flags;
364 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
368 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
369 struct lustre_handle *handle)
371 struct ldlm_lock *retval = NULL;
373 l_lock(&ns->ns_lock);
374 retval = __ldlm_handle2lock(handle, 0);
375 l_unlock(&ns->ns_lock);
380 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
382 return lockmode_compat(a->l_req_mode, b->l_req_mode);
385 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
387 ldlm_res2desc(lock->l_resource, &desc->l_resource);
388 desc->l_req_mode = lock->l_req_mode;
389 desc->l_granted_mode = lock->l_granted_mode;
390 memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
391 memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
394 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
395 struct ldlm_lock *new,
396 void *data, int datalen)
398 struct ldlm_ast_work *w;
401 l_lock(&lock->l_resource->lr_namespace->ns_lock);
402 if (new && (lock->l_flags & LDLM_FL_AST_SENT))
405 OBD_ALLOC(w, sizeof(*w));
412 w->w_datalen = datalen;
414 lock->l_flags |= LDLM_FL_AST_SENT;
416 ldlm_lock2desc(new, &w->w_desc);
419 w->w_lock = LDLM_LOCK_GET(lock);
420 list_add(&w->w_list, lock->l_resource->lr_tmp);
423 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
427 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
429 struct ldlm_lock *lock;
431 lock = ldlm_handle2lock(lockh);
432 ldlm_lock_addref_internal(lock, mode);
436 /* only called for local locks */
437 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
439 l_lock(&lock->l_resource->lr_namespace->ns_lock);
440 ldlm_lock_remove_from_lru(lock);
441 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
445 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
447 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
450 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
452 struct ldlm_namespace *ns;
455 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
456 ns = lock->l_resource->lr_namespace;
457 l_lock(&ns->ns_lock);
458 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR) {
459 LASSERT(lock->l_readers > 0);
462 LASSERT(lock->l_writers > 0);
466 if (lock->l_flags & LDLM_FL_LOCAL &&
467 !lock->l_readers && !lock->l_writers) {
468 /* If this is a local lock on a server namespace and this was
469 * the last reference, cancel the lock. */
470 CDEBUG(D_INFO, "forcing cancel of local lock\n");
471 lock->l_flags |= LDLM_FL_CBPENDING;
474 if (!lock->l_readers && !lock->l_writers &&
475 (lock->l_flags & LDLM_FL_CBPENDING)) {
476 /* If we received a blocked AST and this was the last reference,
477 * run the callback. */
478 if (!ns->ns_client && lock->l_export)
479 CERROR("FL_CBPENDING set on non-local lock--just a "
482 LDLM_DEBUG(lock, "final decref done on cbpending lock");
483 l_unlock(&ns->ns_lock);
485 /* FIXME: need a real 'desc' here */
486 if (lock->l_blocking_ast != NULL)
487 lock->l_blocking_ast(lock, NULL, lock->l_data,
490 LDLM_DEBUG(lock, "No blocking AST?");
491 } else if (ns->ns_client && !lock->l_readers && !lock->l_writers) {
492 /* If this is a client-side namespace and this was the last
493 * reference, put it on the LRU. */
494 LASSERT(list_empty(&lock->l_lru));
495 LASSERT(ns->ns_nr_unused >= 0);
496 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
498 l_unlock(&ns->ns_lock);
501 l_unlock(&ns->ns_lock);
504 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
509 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
511 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
512 LASSERT(lock != NULL);
513 ldlm_lock_decref_internal(lock, mode);
517 /* This will drop a lock reference and mark it for destruction, but will not
518 * necessarily cancel the lock before returning. */
519 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
521 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
524 LASSERT(lock != NULL);
526 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
527 l_lock(&lock->l_resource->lr_namespace->ns_lock);
528 lock->l_flags |= LDLM_FL_CBPENDING;
529 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
530 ldlm_lock_decref_internal(lock, mode);
534 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
535 struct list_head *queue)
537 struct list_head *tmp, *pos;
540 list_for_each_safe(tmp, pos, queue) {
541 struct ldlm_lock *child;
542 ldlm_res_compat compat;
544 child = list_entry(tmp, struct ldlm_lock, l_res_link);
548 compat = ldlm_res_compat_table[child->l_resource->lr_type];
549 if (compat && compat(child, lock)) {
550 CDEBUG(D_OTHER, "compat function succeded, next.\n");
553 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
554 CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
560 if (send_cbs && child->l_blocking_ast != NULL) {
561 CDEBUG(D_OTHER, "lock %p incompatible; sending "
562 "blocking AST.\n", child);
563 ldlm_add_ast_work_item(child, lock, NULL, 0);
570 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
575 l_lock(&lock->l_resource->lr_namespace->ns_lock);
576 rc = ldlm_lock_compat_list(lock, send_cbs,
577 &lock->l_resource->lr_granted);
578 /* FIXME: should we be sending ASTs to converting? */
580 rc = ldlm_lock_compat_list
581 (lock, send_cbs, &lock->l_resource->lr_converting);
583 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
588 * - ldlm_lock_enqueue
589 * - ldlm_reprocess_queue
590 * - ldlm_lock_convert
592 void ldlm_grant_lock(struct ldlm_lock *lock, void *data, int datalen)
594 struct ldlm_resource *res = lock->l_resource;
597 l_lock(&lock->l_resource->lr_namespace->ns_lock);
598 ldlm_resource_add_lock(res, &res->lr_granted, lock);
599 lock->l_granted_mode = lock->l_req_mode;
601 if (lock->l_granted_mode < res->lr_most_restr)
602 res->lr_most_restr = lock->l_granted_mode;
604 if (lock->l_completion_ast != NULL)
605 ldlm_add_ast_work_item(lock, NULL, data, datalen);
607 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
611 /* returns a referenced lock or NULL. See the flag descriptions below, in the
612 * comment above ldlm_lock_match */
613 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
614 struct ldlm_extent *extent,
615 struct ldlm_lock *old_lock, void *data,
618 struct ldlm_lock *lock;
619 struct list_head *tmp;
621 list_for_each(tmp, queue) {
622 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
624 if (lock == old_lock)
627 /* llite sometimes wants to match locks that will be
628 * canceled when their users drop, but we allow it to match
629 * if it passes in CBPENDING and the lock still has users.
630 * this is generally only going to be used by children
631 * whose parents already hold a lock so forward progress
632 * can still happen. */
633 if (lock->l_flags & LDLM_FL_CBPENDING &&
634 !(flags & LDLM_FL_CBPENDING))
636 if (lock->l_flags & LDLM_FL_CBPENDING &&
637 lock->l_readers == 0 && lock->l_writers == 0)
640 if (lock->l_req_mode != mode)
643 if (lock->l_resource->lr_type == LDLM_EXTENT &&
644 (lock->l_extent.start > extent->start ||
645 lock->l_extent.end < extent->end))
648 if (lock->l_destroyed)
651 if ((flags & LDLM_FL_LOCAL_ONLY) &&
652 !(lock->l_flags & LDLM_FL_LOCAL))
655 if ((flags & LDLM_FL_MATCH_DATA) && lock->l_data != data) {
656 LDLM_DEBUG(lock, "data mismatch: have %p, want %p",
661 ldlm_lock_addref_internal(lock, mode);
668 /* Can be called in two ways:
670 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
671 * for a duplicate of.
673 * Otherwise, all of the fields must be filled in, to match against.
675 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
676 * server (ie, connh is NULL)
677 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
678 * list will be considered
679 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
680 * to be canceled can still be matched as long as they still have reader
681 * or writer refernces
682 * If 'flags' contains LDLM_FL_MATCH_DATA, then only match a lock if the opaque
685 * Returns 1 if it finds an already-existing lock that is compatible; in this
686 * case, lockh is filled in with a addref()ed lock
688 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
689 struct ldlm_res_id *res_id, __u32 type, void *cookie,
690 int cookielen, ldlm_mode_t mode, void *data,
691 struct lustre_handle *lockh)
693 struct ldlm_resource *res;
694 struct ldlm_lock *lock, *old_lock = NULL;
699 old_lock = ldlm_handle2lock(lockh);
702 ns = old_lock->l_resource->lr_namespace;
703 res_id = &old_lock->l_resource->lr_name;
704 type = old_lock->l_resource->lr_type;
705 mode = old_lock->l_req_mode;
708 res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
710 LASSERT(old_lock == NULL);
714 l_lock(&ns->ns_lock);
716 lock = search_queue(&res->lr_granted, mode, cookie, old_lock, data,
720 if (flags & LDLM_FL_BLOCK_GRANTED)
722 lock = search_queue(&res->lr_converting, mode, cookie, old_lock, data,
726 lock = search_queue(&res->lr_waiting, mode, cookie, old_lock, data,
733 ldlm_resource_putref(res);
734 l_unlock(&ns->ns_lock);
737 ldlm_lock2handle(lock, lockh);
738 if (lock->l_completion_ast)
739 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
743 LDLM_DEBUG(lock, "matched");
745 LDLM_DEBUG_NOLOCK("not matched");
748 LDLM_LOCK_PUT(old_lock);
753 /* Returns a referenced lock */
754 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
755 struct lustre_handle *parent_lock_handle,
756 struct ldlm_res_id res_id, __u32 type,
758 ldlm_blocking_callback blocking,
761 struct ldlm_resource *res, *parent_res = NULL;
762 struct ldlm_lock *lock, *parent_lock = NULL;
765 if (parent_lock_handle) {
766 parent_lock = ldlm_handle2lock(parent_lock_handle);
768 parent_res = parent_lock->l_resource;
771 res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
775 lock = ldlm_lock_new(parent_lock, res);
776 ldlm_resource_putref(res);
777 if (parent_lock != NULL)
778 LDLM_LOCK_PUT(parent_lock);
783 lock->l_req_mode = mode;
785 lock->l_blocking_ast = blocking;
790 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
791 struct ldlm_lock **lockp,
792 void *cookie, int cookie_len,
794 ldlm_completion_callback completion)
796 struct ldlm_resource *res;
797 struct ldlm_lock *lock = *lockp;
799 ldlm_res_policy policy;
802 res = lock->l_resource;
804 if (res->lr_type == LDLM_EXTENT)
805 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
807 /* policies are not executed on the client or during replay */
808 local = res->lr_namespace->ns_client;
809 if (!local && !(*flags & LDLM_FL_REPLAY) &&
810 (policy = ldlm_res_policy_table[res->lr_type])) {
812 rc = policy(ns, lockp, cookie, lock->l_req_mode, *flags, NULL);
813 if (rc == ELDLM_LOCK_CHANGED) {
814 res = lock->l_resource;
815 *flags |= LDLM_FL_LOCK_CHANGED;
816 } else if (rc == ELDLM_LOCK_REPLACED) {
817 /* The lock that was returned has already been granted,
818 * and placed into lockp. Destroy the old one and our
819 * work here is done. */
820 ldlm_lock_destroy(lock);
822 *flags |= LDLM_FL_LOCK_CHANGED;
824 } else if (rc == ELDLM_LOCK_ABORTED) {
825 ldlm_lock_destroy(lock);
830 l_lock(&ns->ns_lock);
831 if (local && lock->l_req_mode == lock->l_granted_mode) {
832 /* The server returned a blocked lock, but it was granted before
833 * we got a chance to actually enqueue it. We don't need to do
835 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
836 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
840 /* This distinction between local lock trees is very important; a client
841 * namespace only has information about locks taken by that client, and
842 * thus doesn't have enough information to decide for itself if it can
843 * be granted (below). In this case, we do exactly what the server
844 * tells us to do, as dictated by the 'flags'.
846 * We do exactly the same thing during recovery, when the server is
847 * more or less trusting the clients not to lie.
849 * FIXME (bug 268): Detect obvious lies by checking compatibility in
850 * granted/converting queues. */
851 ldlm_resource_unlink_lock(lock);
853 if (*flags & LDLM_FL_BLOCK_CONV)
854 ldlm_resource_add_lock(res, &res->lr_converting, lock);
855 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
856 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
858 ldlm_grant_lock(lock, NULL, 0);
860 } else if (*flags & LDLM_FL_REPLAY) {
861 if (*flags & LDLM_FL_BLOCK_CONV) {
862 ldlm_resource_add_lock(res, &res->lr_converting, lock);
864 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
865 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
867 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
868 ldlm_grant_lock(lock, NULL, 0);
871 /* If no flags, fall through to normal enqueue path. */
874 /* FIXME: We may want to optimize by checking lr_most_restr */
875 if (!list_empty(&res->lr_converting)) {
876 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
877 *flags |= LDLM_FL_BLOCK_CONV;
880 if (!list_empty(&res->lr_waiting)) {
881 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
882 *flags |= LDLM_FL_BLOCK_WAIT;
885 if (!ldlm_lock_compat(lock, 0)) {
886 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
887 *flags |= LDLM_FL_BLOCK_GRANTED;
890 ldlm_grant_lock(lock, NULL, 0);
893 /* Don't set 'completion_ast' until here so that if the lock is granted
894 * immediately we don't do an unnecessary completion call. */
895 lock->l_completion_ast = completion;
896 l_unlock(&ns->ns_lock);
900 /* Must be called with namespace taken: queue is waiting or converting. */
901 static int ldlm_reprocess_queue(struct ldlm_resource *res,
902 struct list_head *queue)
904 struct list_head *tmp, *pos;
907 list_for_each_safe(tmp, pos, queue) {
908 struct ldlm_lock *pending;
909 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
911 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
913 if (!ldlm_lock_compat(pending, 1))
916 list_del_init(&pending->l_res_link);
917 ldlm_grant_lock(pending, NULL, 0);
923 int ldlm_run_ast_work(struct list_head *rpc_list)
925 struct list_head *tmp, *pos;
929 list_for_each_safe(tmp, pos, rpc_list) {
930 struct ldlm_ast_work *w =
931 list_entry(tmp, struct ldlm_ast_work, w_list);
933 /* It's possible to receive a completion AST before we've set
934 * the l_completion_ast pointer: either because the AST arrived
935 * before the reply, or simply because there's a small race
936 * window between receiving the reply and finishing the local
939 * This can't happen with the blocking_ast, however, because we
940 * will never call the local blocking_ast until we drop our
941 * reader/writer reference, which we won't do until we get the
942 * reply and finish enqueueing. */
944 LASSERT(w->w_lock->l_blocking_ast != NULL);
945 rc = w->w_lock->l_blocking_ast
946 (w->w_lock, &w->w_desc, w->w_data,
948 } else if (w->w_lock->l_completion_ast != NULL) {
949 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
957 CERROR("Failed AST - should clean & disconnect "
959 LDLM_LOCK_PUT(w->w_lock);
960 list_del(&w->w_list);
961 OBD_FREE(w, sizeof(*w));
966 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
968 ldlm_reprocess_all(res);
969 return LDLM_ITER_CONTINUE;
972 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
974 (void)ldlm_namespace_foreach_res(ns, reprocess_one_queue, NULL);
977 void ldlm_reprocess_all(struct ldlm_resource *res)
979 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
983 /* Local lock trees don't get reprocessed. */
984 if (res->lr_namespace->ns_client) {
990 l_lock(&res->lr_namespace->ns_lock);
991 res->lr_tmp = &rpc_list;
993 ldlm_reprocess_queue(res, &res->lr_converting);
994 if (list_empty(&res->lr_converting))
995 ldlm_reprocess_queue(res, &res->lr_waiting);
998 l_unlock(&res->lr_namespace->ns_lock);
1000 rc = ldlm_run_ast_work(&rpc_list);
1001 if (rc == -ERESTART)
1006 void ldlm_cancel_callback(struct ldlm_lock *lock)
1008 l_lock(&lock->l_resource->lr_namespace->ns_lock);
1009 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1010 lock->l_flags |= LDLM_FL_CANCEL;
1011 if (lock->l_blocking_ast) {
1012 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1013 lock->l_blocking_ast(lock, NULL, lock->l_data,
1017 LDLM_DEBUG(lock, "no blocking ast");
1020 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1023 void ldlm_lock_cancel(struct ldlm_lock *lock)
1025 struct ldlm_resource *res;
1026 struct ldlm_namespace *ns;
1029 ldlm_del_waiting_lock(lock);
1031 res = lock->l_resource;
1032 ns = res->lr_namespace;
1034 l_lock(&ns->ns_lock);
1035 /* Please do not, no matter how tempting, remove this LBUG without
1036 * talking to me first. -phik */
1037 if (lock->l_readers || lock->l_writers) {
1038 LDLM_DEBUG(lock, "lock still has references");
1039 ldlm_lock_dump(D_OTHER, lock);
1043 ldlm_cancel_callback(lock); /* XXX FIXME bug 1030 */
1045 ldlm_resource_unlink_lock(lock);
1046 ldlm_lock_destroy(lock);
1047 l_unlock(&ns->ns_lock);
1051 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1053 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1059 lock->l_data = data;
1061 LDLM_LOCK_PUT(lock);
1066 /* This function is only called from one thread (per export); no locking around
1067 * the list ops needed */
1068 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1070 struct list_head *iter, *n;
1072 list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
1073 struct ldlm_lock *lock;
1074 struct ldlm_resource *res;
1075 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1076 res = ldlm_resource_getref(lock->l_resource);
1077 LDLM_DEBUG(lock, "export %p", exp);
1078 ldlm_lock_cancel(lock);
1079 ldlm_reprocess_all(res);
1080 ldlm_resource_putref(res);
1084 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1087 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1088 struct ldlm_resource *res;
1089 struct ldlm_namespace *ns;
1095 res = lock->l_resource;
1096 ns = res->lr_namespace;
1098 l_lock(&ns->ns_lock);
1100 lock->l_req_mode = new_mode;
1101 ldlm_resource_unlink_lock(lock);
1103 /* If this is a local resource, put it on the appropriate list. */
1104 if (res->lr_namespace->ns_client) {
1105 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1106 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1108 /* This should never happen, because of the way the
1109 * server handles conversions. */
1112 res->lr_tmp = &rpc_list;
1113 ldlm_grant_lock(lock, NULL, 0);
1116 /* FIXME: completion handling not with ns_lock held ! */
1117 if (lock->l_completion_ast)
1118 lock->l_completion_ast(lock, 0, NULL);
1121 /* FIXME: We should try the conversion right away and possibly
1122 * return success without the need for an extra AST */
1123 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1124 *flags |= LDLM_FL_BLOCK_CONV;
1127 l_unlock(&ns->ns_lock);
1130 ldlm_run_ast_work(&rpc_list);
1134 void ldlm_lock_dump(int level, struct ldlm_lock *lock)
1137 struct obd_device *obd;
1139 if (!((portal_debug | D_ERROR) & level))
1142 if (RES_VERSION_SIZE != 4)
1146 CDEBUG(level, " NULL LDLM lock\n");
1150 snprintf(ver, sizeof(ver), "%x %x %x %x",
1151 lock->l_version[0], lock->l_version[1],
1152 lock->l_version[2], lock->l_version[3]);
1154 CDEBUG(level, " -- Lock dump: %p (%s) (rc: %d)\n", lock, ver,
1155 atomic_read(&lock->l_refc));
1156 obd = class_conn2obd(lock->l_connh);
1157 if (lock->l_export && lock->l_export->exp_connection) {
1158 CDEBUG(level, " Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1159 lock->l_export->exp_connection->c_peer.peer_nid,
1160 lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1161 lock->l_remote_handle.cookie);
1162 } else if (obd == NULL) {
1163 CDEBUG(level, " Node: local\n");
1165 struct obd_import *imp = obd->u.cli.cl_import;
1166 CDEBUG(level, " Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1167 imp->imp_connection->c_peer.peer_nid,
1168 imp->imp_connection->c_peer.peer_ni->pni_name,
1169 lock->l_remote_handle.cookie);
1171 CDEBUG(level, " Parent: %p\n", lock->l_parent);
1172 CDEBUG(level, " Resource: %p ("LPD64")\n", lock->l_resource,
1173 lock->l_resource->lr_name.name[0]);
1174 CDEBUG(level, " Requested mode: %d, granted mode: %d\n",
1175 (int)lock->l_req_mode, (int)lock->l_granted_mode);
1176 CDEBUG(level, " Readers: %u ; Writers; %u\n",
1177 lock->l_readers, lock->l_writers);
1178 if (lock->l_resource->lr_type == LDLM_EXTENT)
1179 CDEBUG(level, " Extent: "LPU64" -> "LPU64"\n",
1180 lock->l_extent.start, lock->l_extent.end);
1183 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1185 struct ldlm_lock *lock;
1187 lock = ldlm_handle2lock(lockh);
1191 ldlm_lock_dump(D_OTHER, lock);
1193 LDLM_LOCK_PUT(lock);