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;
158 LDLM_LOCK_PUT(lock->l_parent);
160 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
161 l_unlock(&ns->ns_lock);
167 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
170 l_lock(&lock->l_resource->lr_namespace->ns_lock);
171 if (!list_empty(&lock->l_lru)) {
172 list_del_init(&lock->l_lru);
173 lock->l_resource->lr_namespace->ns_nr_unused--;
174 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
176 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
180 /* This used to have a 'strict' flact, which recovery would use to mark an
181 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
182 * shall explain why it's gone: with the new hash table scheme, once you call
183 * ldlm_lock_destroy, you can never drop your final references on this lock.
184 * Because it's not in the hash table anymore. -phil */
185 void ldlm_lock_destroy(struct ldlm_lock *lock)
188 l_lock(&lock->l_resource->lr_namespace->ns_lock);
190 if (!list_empty(&lock->l_children)) {
191 LDLM_ERROR(lock, "still has children (%p)!",
192 lock->l_children.next);
193 ldlm_lock_dump(D_ERROR, lock);
196 if (lock->l_readers || lock->l_writers) {
197 LDLM_ERROR(lock, "lock still has references");
198 ldlm_lock_dump(D_ERROR, lock);
202 if (!list_empty(&lock->l_res_link)) {
203 ldlm_lock_dump(D_ERROR, lock);
207 if (lock->l_destroyed) {
208 LASSERT(list_empty(&lock->l_lru));
209 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
213 lock->l_destroyed = 1;
215 list_del_init(&lock->l_export_chain);
216 ldlm_lock_remove_from_lru(lock);
217 class_handle_unhash(&lock->l_handle);
220 /* Wake anyone waiting for this lock */
221 /* FIXME: I should probably add yet another flag, instead of using
222 * l_export to only call this on clients */
223 lock->l_export = NULL;
224 if (lock->l_export && lock->l_completion_ast)
225 lock->l_completion_ast(lock, 0);
228 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
233 /* this is called by portals_handle2object with the handle lock taken */
234 static void lock_handle_addref(void *lock)
236 LDLM_LOCK_GET((struct ldlm_lock *)lock);
240 * usage: pass in a resource on which you have done ldlm_resource_get
241 * pass in a parent lock on which you have done a ldlm_lock_get
242 * after return, ldlm_*_put the resource and parent
243 * returns: lock with refcount 1
245 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
246 struct ldlm_resource *resource)
248 struct ldlm_lock *lock;
251 if (resource == NULL)
254 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, SLAB_KERNEL, sizeof(*lock));
258 lock->l_resource = ldlm_resource_getref(resource);
260 atomic_set(&lock->l_refc, 2);
261 INIT_LIST_HEAD(&lock->l_children);
262 INIT_LIST_HEAD(&lock->l_res_link);
263 INIT_LIST_HEAD(&lock->l_lru);
264 INIT_LIST_HEAD(&lock->l_export_chain);
265 INIT_LIST_HEAD(&lock->l_pending_chain);
266 init_waitqueue_head(&lock->l_waitq);
268 spin_lock(&resource->lr_namespace->ns_counter_lock);
269 resource->lr_namespace->ns_locks++;
270 spin_unlock(&resource->lr_namespace->ns_counter_lock);
272 if (parent != NULL) {
273 l_lock(&parent->l_resource->lr_namespace->ns_lock);
274 lock->l_parent = LDLM_LOCK_GET(parent);
275 list_add(&lock->l_childof, &parent->l_children);
276 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
279 INIT_LIST_HEAD(&lock->l_handle.h_link);
280 class_handle_hash(&lock->l_handle, lock_handle_addref);
285 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
286 struct ldlm_res_id new_resid)
288 struct ldlm_resource *oldres = lock->l_resource;
291 l_lock(&ns->ns_lock);
292 if (memcmp(&new_resid, &lock->l_resource->lr_name,
293 sizeof(lock->l_resource->lr_name)) == 0) {
295 l_unlock(&ns->ns_lock);
299 LASSERT(new_resid.name[0] != 0);
301 /* This function assumes that the lock isn't on any lists */
302 LASSERT(list_empty(&lock->l_res_link));
304 lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
305 lock->l_resource->lr_type, 1);
306 if (lock->l_resource == NULL) {
311 /* ...and the flowers are still standing! */
312 ldlm_resource_putref(oldres);
314 l_unlock(&ns->ns_lock);
322 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
324 lockh->cookie = lock->l_handle.h_cookie;
327 /* if flags: atomically get the lock and set the flags.
328 * Return NULL if flag already set
331 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
333 struct ldlm_namespace *ns;
334 struct ldlm_lock *lock = NULL, *retval = NULL;
339 lock = class_handle2object(handle->cookie);
343 LASSERT(lock->l_resource != NULL);
344 ns = lock->l_resource->lr_namespace;
347 l_lock(&ns->ns_lock);
349 /* It's unlikely but possible that someone marked the lock as
350 * destroyed after we did handle2object on it */
351 if (lock->l_destroyed) {
352 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
357 if (flags && (lock->l_flags & flags)) {
363 lock->l_flags |= flags;
368 l_unlock(&ns->ns_lock);
372 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
373 struct lustre_handle *handle)
375 struct ldlm_lock *retval = NULL;
377 l_lock(&ns->ns_lock);
378 retval = __ldlm_handle2lock(handle, 0);
379 l_unlock(&ns->ns_lock);
384 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
386 return lockmode_compat(a->l_req_mode, b->l_req_mode);
389 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
391 ldlm_res2desc(lock->l_resource, &desc->l_resource);
392 desc->l_req_mode = lock->l_req_mode;
393 desc->l_granted_mode = lock->l_granted_mode;
394 memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
395 memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
398 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
399 struct ldlm_lock *new,
400 void *data, int datalen)
402 struct ldlm_ast_work *w;
405 l_lock(&lock->l_resource->lr_namespace->ns_lock);
406 if (new && (lock->l_flags & LDLM_FL_AST_SENT))
409 OBD_ALLOC(w, sizeof(*w));
416 w->w_datalen = datalen;
418 lock->l_flags |= LDLM_FL_AST_SENT;
420 ldlm_lock2desc(new, &w->w_desc);
423 w->w_lock = LDLM_LOCK_GET(lock);
424 list_add(&w->w_list, lock->l_resource->lr_tmp);
427 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
431 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
433 struct ldlm_lock *lock;
435 lock = ldlm_handle2lock(lockh);
436 ldlm_lock_addref_internal(lock, mode);
440 /* only called for local locks */
441 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
443 l_lock(&lock->l_resource->lr_namespace->ns_lock);
444 ldlm_lock_remove_from_lru(lock);
445 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
449 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
451 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
454 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
456 struct ldlm_namespace *ns;
459 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
460 ns = lock->l_resource->lr_namespace;
461 l_lock(&ns->ns_lock);
462 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR) {
463 LASSERT(lock->l_readers > 0);
466 LASSERT(lock->l_writers > 0);
470 if (lock->l_flags & LDLM_FL_LOCAL &&
471 !lock->l_readers && !lock->l_writers) {
472 /* If this is a local lock on a server namespace and this was
473 * the last reference, cancel the lock. */
474 CDEBUG(D_INFO, "forcing cancel of local lock\n");
475 lock->l_flags |= LDLM_FL_CBPENDING;
478 if (!lock->l_readers && !lock->l_writers &&
479 (lock->l_flags & LDLM_FL_CBPENDING)) {
480 /* If we received a blocked AST and this was the last reference,
481 * run the callback. */
482 if (!ns->ns_client && lock->l_export)
483 CERROR("FL_CBPENDING set on non-local lock--just a "
486 LDLM_DEBUG(lock, "final decref done on cbpending lock");
487 l_unlock(&ns->ns_lock);
489 /* FIXME: need a real 'desc' here */
490 if (lock->l_blocking_ast != NULL)
491 lock->l_blocking_ast(lock, NULL, lock->l_data,
494 LDLM_DEBUG(lock, "No blocking AST?");
495 } else if (ns->ns_client && !lock->l_readers && !lock->l_writers) {
496 /* If this is a client-side namespace and this was the last
497 * reference, put it on the LRU. */
498 LASSERT(list_empty(&lock->l_lru));
499 LASSERT(ns->ns_nr_unused >= 0);
500 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
502 l_unlock(&ns->ns_lock);
505 l_unlock(&ns->ns_lock);
508 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
513 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
515 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
516 LASSERT(lock != NULL);
517 ldlm_lock_decref_internal(lock, mode);
521 /* This will drop a lock reference and mark it for destruction, but will not
522 * necessarily cancel the lock before returning. */
523 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
525 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
528 LASSERT(lock != NULL);
530 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
531 l_lock(&lock->l_resource->lr_namespace->ns_lock);
532 lock->l_flags |= LDLM_FL_CBPENDING;
533 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
534 ldlm_lock_decref_internal(lock, mode);
538 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
539 struct list_head *queue)
541 struct list_head *tmp, *pos;
544 list_for_each_safe(tmp, pos, queue) {
545 struct ldlm_lock *child;
546 ldlm_res_compat compat;
548 child = list_entry(tmp, struct ldlm_lock, l_res_link);
552 compat = ldlm_res_compat_table[child->l_resource->lr_type];
553 if (compat && compat(child, lock)) {
554 CDEBUG(D_OTHER, "compat function succeded, next.\n");
557 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
558 CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
564 if (send_cbs && child->l_blocking_ast != NULL) {
565 CDEBUG(D_OTHER, "lock %p incompatible; sending "
566 "blocking AST.\n", child);
567 ldlm_add_ast_work_item(child, lock, NULL, 0);
574 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
579 l_lock(&lock->l_resource->lr_namespace->ns_lock);
580 rc = ldlm_lock_compat_list(lock, send_cbs,
581 &lock->l_resource->lr_granted);
582 /* FIXME: should we be sending ASTs to converting? */
584 rc = ldlm_lock_compat_list
585 (lock, send_cbs, &lock->l_resource->lr_converting);
587 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
592 * - ldlm_lock_enqueue
593 * - ldlm_reprocess_queue
594 * - ldlm_lock_convert
596 void ldlm_grant_lock(struct ldlm_lock *lock, void *data, int datalen)
598 struct ldlm_resource *res = lock->l_resource;
601 l_lock(&lock->l_resource->lr_namespace->ns_lock);
602 ldlm_resource_add_lock(res, &res->lr_granted, lock);
603 lock->l_granted_mode = lock->l_req_mode;
605 if (lock->l_granted_mode < res->lr_most_restr)
606 res->lr_most_restr = lock->l_granted_mode;
608 if (lock->l_completion_ast != NULL)
609 ldlm_add_ast_work_item(lock, NULL, data, datalen);
611 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
615 /* returns a referenced lock or NULL. See the flag descriptions below, in the
616 * comment above ldlm_lock_match */
617 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
618 struct ldlm_extent *extent,
619 struct ldlm_lock *old_lock, void *data,
622 struct ldlm_lock *lock;
623 struct list_head *tmp;
625 list_for_each(tmp, queue) {
626 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
628 if (lock == old_lock)
631 /* llite sometimes wants to match locks that will be
632 * canceled when their users drop, but we allow it to match
633 * if it passes in CBPENDING and the lock still has users.
634 * this is generally only going to be used by children
635 * whose parents already hold a lock so forward progress
636 * can still happen. */
637 if (lock->l_flags & LDLM_FL_CBPENDING &&
638 !(flags & LDLM_FL_CBPENDING))
640 if (lock->l_flags & LDLM_FL_CBPENDING &&
641 lock->l_readers == 0 && lock->l_writers == 0)
644 if (lock->l_req_mode != mode)
647 if (lock->l_resource->lr_type == LDLM_EXTENT &&
648 (lock->l_extent.start > extent->start ||
649 lock->l_extent.end < extent->end))
652 if (lock->l_destroyed)
655 if ((flags & LDLM_FL_LOCAL_ONLY) &&
656 !(lock->l_flags & LDLM_FL_LOCAL))
659 if ((flags & LDLM_FL_MATCH_DATA) && lock->l_data != data) {
660 LDLM_DEBUG(lock, "data mismatch: have %p, want %p",
665 ldlm_lock_addref_internal(lock, mode);
672 /* Can be called in two ways:
674 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
675 * for a duplicate of.
677 * Otherwise, all of the fields must be filled in, to match against.
679 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
680 * server (ie, connh is NULL)
681 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
682 * list will be considered
683 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
684 * to be canceled can still be matched as long as they still have reader
685 * or writer refernces
686 * If 'flags' contains LDLM_FL_MATCH_DATA, then only match a lock if the opaque
689 * Returns 1 if it finds an already-existing lock that is compatible; in this
690 * case, lockh is filled in with a addref()ed lock
692 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
693 struct ldlm_res_id *res_id, __u32 type, void *cookie,
694 int cookielen, ldlm_mode_t mode, void *data,
695 struct lustre_handle *lockh)
697 struct ldlm_resource *res;
698 struct ldlm_lock *lock, *old_lock = NULL;
703 old_lock = ldlm_handle2lock(lockh);
706 ns = old_lock->l_resource->lr_namespace;
707 res_id = &old_lock->l_resource->lr_name;
708 type = old_lock->l_resource->lr_type;
709 mode = old_lock->l_req_mode;
712 res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
714 LASSERT(old_lock == NULL);
718 l_lock(&ns->ns_lock);
720 lock = search_queue(&res->lr_granted, mode, cookie, old_lock, data,
724 if (flags & LDLM_FL_BLOCK_GRANTED)
726 lock = search_queue(&res->lr_converting, mode, cookie, old_lock, data,
730 lock = search_queue(&res->lr_waiting, mode, cookie, old_lock, data,
737 ldlm_resource_putref(res);
738 l_unlock(&ns->ns_lock);
741 ldlm_lock2handle(lock, lockh);
742 if (lock->l_completion_ast)
743 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
747 LDLM_DEBUG(lock, "matched");
749 LDLM_DEBUG_NOLOCK("not matched");
752 LDLM_LOCK_PUT(old_lock);
757 /* Returns a referenced lock */
758 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
759 struct lustre_handle *parent_lock_handle,
760 struct ldlm_res_id res_id, __u32 type,
762 ldlm_blocking_callback blocking,
765 struct ldlm_resource *res, *parent_res = NULL;
766 struct ldlm_lock *lock, *parent_lock = NULL;
769 if (parent_lock_handle) {
770 parent_lock = ldlm_handle2lock(parent_lock_handle);
772 parent_res = parent_lock->l_resource;
775 res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
779 lock = ldlm_lock_new(parent_lock, res);
780 ldlm_resource_putref(res);
781 if (parent_lock != NULL)
782 LDLM_LOCK_PUT(parent_lock);
787 lock->l_req_mode = mode;
789 lock->l_blocking_ast = blocking;
794 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
795 struct ldlm_lock **lockp,
796 void *cookie, int cookie_len,
798 ldlm_completion_callback completion)
800 struct ldlm_resource *res;
801 struct ldlm_lock *lock = *lockp;
803 ldlm_res_policy policy;
806 res = lock->l_resource;
808 if (res->lr_type == LDLM_EXTENT)
809 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
811 /* policies are not executed on the client or during replay */
812 local = res->lr_namespace->ns_client;
813 if (!local && !(*flags & LDLM_FL_REPLAY) &&
814 (policy = ldlm_res_policy_table[res->lr_type])) {
816 rc = policy(ns, lockp, cookie, lock->l_req_mode, *flags, NULL);
817 if (rc == ELDLM_LOCK_CHANGED) {
818 res = lock->l_resource;
819 *flags |= LDLM_FL_LOCK_CHANGED;
820 } else if (rc == ELDLM_LOCK_REPLACED) {
821 /* The lock that was returned has already been granted,
822 * and placed into lockp. Destroy the old one and our
823 * work here is done. */
824 ldlm_lock_destroy(lock);
826 *flags |= LDLM_FL_LOCK_CHANGED;
828 } else if (rc == ELDLM_LOCK_ABORTED) {
829 ldlm_lock_destroy(lock);
834 l_lock(&ns->ns_lock);
835 if (local && lock->l_req_mode == lock->l_granted_mode) {
836 /* The server returned a blocked lock, but it was granted before
837 * we got a chance to actually enqueue it. We don't need to do
839 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
840 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
844 /* This distinction between local lock trees is very important; a client
845 * namespace only has information about locks taken by that client, and
846 * thus doesn't have enough information to decide for itself if it can
847 * be granted (below). In this case, we do exactly what the server
848 * tells us to do, as dictated by the 'flags'.
850 * We do exactly the same thing during recovery, when the server is
851 * more or less trusting the clients not to lie.
853 * FIXME (bug 268): Detect obvious lies by checking compatibility in
854 * granted/converting queues. */
855 ldlm_resource_unlink_lock(lock);
857 if (*flags & LDLM_FL_BLOCK_CONV)
858 ldlm_resource_add_lock(res, &res->lr_converting, lock);
859 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
860 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
862 ldlm_grant_lock(lock, NULL, 0);
864 } else if (*flags & LDLM_FL_REPLAY) {
865 if (*flags & LDLM_FL_BLOCK_CONV) {
866 ldlm_resource_add_lock(res, &res->lr_converting, lock);
868 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
869 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
871 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
872 ldlm_grant_lock(lock, NULL, 0);
875 /* If no flags, fall through to normal enqueue path. */
878 /* FIXME: We may want to optimize by checking lr_most_restr */
879 if (!list_empty(&res->lr_converting)) {
880 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
881 *flags |= LDLM_FL_BLOCK_CONV;
884 if (!list_empty(&res->lr_waiting)) {
885 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
886 *flags |= LDLM_FL_BLOCK_WAIT;
889 if (!ldlm_lock_compat(lock, 0)) {
890 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
891 *flags |= LDLM_FL_BLOCK_GRANTED;
894 ldlm_grant_lock(lock, NULL, 0);
897 /* Don't set 'completion_ast' until here so that if the lock is granted
898 * immediately we don't do an unnecessary completion call. */
899 lock->l_completion_ast = completion;
900 l_unlock(&ns->ns_lock);
904 /* Must be called with namespace taken: queue is waiting or converting. */
905 static int ldlm_reprocess_queue(struct ldlm_resource *res,
906 struct list_head *queue)
908 struct list_head *tmp, *pos;
911 list_for_each_safe(tmp, pos, queue) {
912 struct ldlm_lock *pending;
913 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
915 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
917 if (!ldlm_lock_compat(pending, 1))
920 list_del_init(&pending->l_res_link);
921 ldlm_grant_lock(pending, NULL, 0);
927 int ldlm_run_ast_work(struct list_head *rpc_list)
929 struct list_head *tmp, *pos;
933 list_for_each_safe(tmp, pos, rpc_list) {
934 struct ldlm_ast_work *w =
935 list_entry(tmp, struct ldlm_ast_work, w_list);
937 /* It's possible to receive a completion AST before we've set
938 * the l_completion_ast pointer: either because the AST arrived
939 * before the reply, or simply because there's a small race
940 * window between receiving the reply and finishing the local
943 * This can't happen with the blocking_ast, however, because we
944 * will never call the local blocking_ast until we drop our
945 * reader/writer reference, which we won't do until we get the
946 * reply and finish enqueueing. */
948 LASSERT(w->w_lock->l_blocking_ast != NULL);
949 rc = w->w_lock->l_blocking_ast
950 (w->w_lock, &w->w_desc, w->w_data,
952 } else if (w->w_lock->l_completion_ast != NULL) {
953 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
961 CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
962 "disconnect client\n");
963 LDLM_LOCK_PUT(w->w_lock);
964 list_del(&w->w_list);
965 OBD_FREE(w, sizeof(*w));
970 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
972 ldlm_reprocess_all(res);
973 return LDLM_ITER_CONTINUE;
976 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
978 (void)ldlm_namespace_foreach_res(ns, reprocess_one_queue, NULL);
981 void ldlm_reprocess_all(struct ldlm_resource *res)
983 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
987 /* Local lock trees don't get reprocessed. */
988 if (res->lr_namespace->ns_client) {
994 l_lock(&res->lr_namespace->ns_lock);
995 res->lr_tmp = &rpc_list;
997 ldlm_reprocess_queue(res, &res->lr_converting);
998 if (list_empty(&res->lr_converting))
999 ldlm_reprocess_queue(res, &res->lr_waiting);
1002 l_unlock(&res->lr_namespace->ns_lock);
1004 rc = ldlm_run_ast_work(&rpc_list);
1005 if (rc == -ERESTART)
1010 void ldlm_cancel_callback(struct ldlm_lock *lock)
1012 l_lock(&lock->l_resource->lr_namespace->ns_lock);
1013 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1014 lock->l_flags |= LDLM_FL_CANCEL;
1015 if (lock->l_blocking_ast) {
1016 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1017 lock->l_blocking_ast(lock, NULL, lock->l_data,
1021 LDLM_DEBUG(lock, "no blocking ast");
1024 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1027 void ldlm_lock_cancel(struct ldlm_lock *lock)
1029 struct ldlm_resource *res;
1030 struct ldlm_namespace *ns;
1033 ldlm_del_waiting_lock(lock);
1035 res = lock->l_resource;
1036 ns = res->lr_namespace;
1038 l_lock(&ns->ns_lock);
1039 /* Please do not, no matter how tempting, remove this LBUG without
1040 * talking to me first. -phik */
1041 if (lock->l_readers || lock->l_writers) {
1042 LDLM_DEBUG(lock, "lock still has references");
1043 ldlm_lock_dump(D_OTHER, lock);
1047 ldlm_cancel_callback(lock); /* XXX FIXME bug 1030 */
1049 ldlm_resource_unlink_lock(lock);
1050 ldlm_lock_destroy(lock);
1051 l_unlock(&ns->ns_lock);
1055 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1057 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1063 lock->l_data = data;
1065 LDLM_LOCK_PUT(lock);
1070 /* This function is only called from one thread (per export); no locking around
1071 * the list ops needed */
1072 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1074 struct list_head *iter, *n;
1076 list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
1077 struct ldlm_lock *lock;
1078 struct ldlm_resource *res;
1079 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1080 res = ldlm_resource_getref(lock->l_resource);
1081 LDLM_DEBUG(lock, "export %p", exp);
1082 ldlm_lock_cancel(lock);
1083 ldlm_reprocess_all(res);
1084 ldlm_resource_putref(res);
1088 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1091 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1092 struct ldlm_resource *res;
1093 struct ldlm_namespace *ns;
1099 res = lock->l_resource;
1100 ns = res->lr_namespace;
1102 l_lock(&ns->ns_lock);
1104 lock->l_req_mode = new_mode;
1105 ldlm_resource_unlink_lock(lock);
1107 /* If this is a local resource, put it on the appropriate list. */
1108 if (res->lr_namespace->ns_client) {
1109 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1110 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1112 /* This should never happen, because of the way the
1113 * server handles conversions. */
1116 res->lr_tmp = &rpc_list;
1117 ldlm_grant_lock(lock, NULL, 0);
1120 /* FIXME: completion handling not with ns_lock held ! */
1121 if (lock->l_completion_ast)
1122 lock->l_completion_ast(lock, 0, NULL);
1125 /* FIXME: We should try the conversion right away and possibly
1126 * return success without the need for an extra AST */
1127 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1128 *flags |= LDLM_FL_BLOCK_CONV;
1131 l_unlock(&ns->ns_lock);
1134 ldlm_run_ast_work(&rpc_list);
1138 void ldlm_lock_dump(int level, struct ldlm_lock *lock)
1141 struct obd_device *obd;
1143 if (!((portal_debug | D_ERROR) & level))
1146 if (RES_VERSION_SIZE != 4)
1150 CDEBUG(level, " NULL LDLM lock\n");
1154 snprintf(ver, sizeof(ver), "%x %x %x %x",
1155 lock->l_version[0], lock->l_version[1],
1156 lock->l_version[2], lock->l_version[3]);
1158 CDEBUG(level, " -- Lock dump: %p (%s) (rc: %d)\n", lock, ver,
1159 atomic_read(&lock->l_refc));
1160 obd = class_conn2obd(lock->l_connh);
1161 if (lock->l_export && lock->l_export->exp_connection) {
1162 CDEBUG(level, " Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1163 lock->l_export->exp_connection->c_peer.peer_nid,
1164 lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1165 lock->l_remote_handle.cookie);
1166 } else if (obd == NULL) {
1167 CDEBUG(level, " Node: local\n");
1169 struct obd_import *imp = obd->u.cli.cl_import;
1170 CDEBUG(level, " Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1171 imp->imp_connection->c_peer.peer_nid,
1172 imp->imp_connection->c_peer.peer_ni->pni_name,
1173 lock->l_remote_handle.cookie);
1175 CDEBUG(level, " Parent: %p\n", lock->l_parent);
1176 CDEBUG(level, " Resource: %p ("LPD64")\n", lock->l_resource,
1177 lock->l_resource->lr_name.name[0]);
1178 CDEBUG(level, " Requested mode: %d, granted mode: %d\n",
1179 (int)lock->l_req_mode, (int)lock->l_granted_mode);
1180 CDEBUG(level, " Readers: %u ; Writers; %u\n",
1181 lock->l_readers, lock->l_writers);
1182 if (lock->l_resource->lr_type == LDLM_EXTENT)
1183 CDEBUG(level, " Extent: "LPU64" -> "LPU64"\n",
1184 lock->l_extent.start, lock->l_extent.end);
1187 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1189 struct ldlm_lock *lock;
1191 lock = ldlm_handle2lock(lockh);
1195 ldlm_lock_dump(D_OTHER, lock);
1197 LDLM_LOCK_PUT(lock);