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>
31 # include <liblustre.h>
32 # include <linux/kp30.h>
35 #include <linux/obd_class.h>
36 #include "ldlm_internal.h"
38 //struct lustre_lock ldlm_everything_lock;
41 char *ldlm_lockname[] = {
50 char *ldlm_typename[] = {
56 char *ldlm_it2str(int it)
63 case (IT_OPEN | IT_CREAT):
76 CERROR("Unknown intent %d\n", it);
81 extern kmem_cache_t *ldlm_lock_slab;
82 struct lustre_lock ldlm_handle_lock;
84 static ldlm_processing_policy ldlm_processing_policy_table[] = {
85 [LDLM_PLAIN] ldlm_process_plain_lock,
86 [LDLM_EXTENT] ldlm_process_extent_lock,
88 [LDLM_FLOCK] ldlm_process_flock_lock,
92 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
94 return ldlm_processing_policy_table[res->lr_type];
97 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
103 * REFCOUNTED LOCK OBJECTS
108 * Lock refcounts, during creation:
109 * - one special one for allocation, dec'd only once in destroy
110 * - one for being a lock that's in-use
111 * - one for the addref associated with a new lock
113 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
115 atomic_inc(&lock->l_refc);
119 void ldlm_lock_put(struct ldlm_lock *lock)
123 if (atomic_dec_and_test(&lock->l_refc)) {
124 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
126 l_lock(&ns->ns_lock);
127 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
128 LASSERT(lock->l_destroyed);
129 LASSERT(list_empty(&lock->l_res_link));
131 spin_lock(&ns->ns_counter_lock);
133 spin_unlock(&ns->ns_counter_lock);
135 ldlm_resource_putref(lock->l_resource);
136 lock->l_resource = NULL;
138 class_export_put(lock->l_export);
141 LDLM_LOCK_PUT(lock->l_parent);
143 if (lock->l_lvb_data != NULL)
144 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
146 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
147 l_unlock(&ns->ns_lock);
153 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
156 l_lock(&lock->l_resource->lr_namespace->ns_lock);
157 if (!list_empty(&lock->l_lru)) {
158 list_del_init(&lock->l_lru);
159 lock->l_resource->lr_namespace->ns_nr_unused--;
160 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
162 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
166 /* This used to have a 'strict' flact, which recovery would use to mark an
167 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
168 * shall explain why it's gone: with the new hash table scheme, once you call
169 * ldlm_lock_destroy, you can never drop your final references on this lock.
170 * Because it's not in the hash table anymore. -phil */
171 void ldlm_lock_destroy(struct ldlm_lock *lock)
174 l_lock(&lock->l_resource->lr_namespace->ns_lock);
176 if (!list_empty(&lock->l_children)) {
177 LDLM_ERROR(lock, "still has children (%p)!",
178 lock->l_children.next);
179 ldlm_lock_dump(D_ERROR, lock, 0);
182 if (lock->l_readers || lock->l_writers) {
183 LDLM_ERROR(lock, "lock still has references");
184 ldlm_lock_dump(D_ERROR, lock, 0);
188 if (!list_empty(&lock->l_res_link)) {
189 ldlm_lock_dump(D_ERROR, lock, 0);
193 if (lock->l_destroyed) {
194 LASSERT(list_empty(&lock->l_lru));
195 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
199 lock->l_destroyed = 1;
201 list_del_init(&lock->l_export_chain);
202 ldlm_lock_remove_from_lru(lock);
203 class_handle_unhash(&lock->l_handle);
206 /* Wake anyone waiting for this lock */
207 /* FIXME: I should probably add yet another flag, instead of using
208 * l_export to only call this on clients */
210 class_export_put(lock->l_export);
211 lock->l_export = NULL;
212 if (lock->l_export && lock->l_completion_ast)
213 lock->l_completion_ast(lock, 0);
216 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
221 /* this is called by portals_handle2object with the handle lock taken */
222 static void lock_handle_addref(void *lock)
224 LDLM_LOCK_GET((struct ldlm_lock *)lock);
228 * usage: pass in a resource on which you have done ldlm_resource_get
229 * pass in a parent lock on which you have done a ldlm_lock_get
230 * after return, ldlm_*_put the resource and parent
231 * returns: lock with refcount 1
233 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
234 struct ldlm_resource *resource)
236 struct ldlm_lock *lock;
239 if (resource == NULL)
242 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, SLAB_NOFS, sizeof(*lock));
246 lock->l_resource = ldlm_resource_getref(resource);
248 atomic_set(&lock->l_refc, 2);
249 INIT_LIST_HEAD(&lock->l_children);
250 INIT_LIST_HEAD(&lock->l_res_link);
251 INIT_LIST_HEAD(&lock->l_lru);
252 INIT_LIST_HEAD(&lock->l_export_chain);
253 INIT_LIST_HEAD(&lock->l_pending_chain);
254 init_waitqueue_head(&lock->l_waitq);
256 spin_lock(&resource->lr_namespace->ns_counter_lock);
257 resource->lr_namespace->ns_locks++;
258 spin_unlock(&resource->lr_namespace->ns_counter_lock);
260 if (parent != NULL) {
261 l_lock(&parent->l_resource->lr_namespace->ns_lock);
262 lock->l_parent = LDLM_LOCK_GET(parent);
263 list_add(&lock->l_childof, &parent->l_children);
264 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
267 INIT_LIST_HEAD(&lock->l_handle.h_link);
268 class_handle_hash(&lock->l_handle, lock_handle_addref);
273 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
274 struct ldlm_res_id new_resid)
276 struct ldlm_resource *oldres = lock->l_resource;
279 l_lock(&ns->ns_lock);
280 if (memcmp(&new_resid, &lock->l_resource->lr_name,
281 sizeof(lock->l_resource->lr_name)) == 0) {
283 l_unlock(&ns->ns_lock);
287 LASSERT(new_resid.name[0] != 0);
289 /* This function assumes that the lock isn't on any lists */
290 LASSERT(list_empty(&lock->l_res_link));
292 lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
293 lock->l_resource->lr_type, 1);
294 if (lock->l_resource == NULL) {
299 /* ...and the flowers are still standing! */
300 ldlm_resource_putref(oldres);
302 l_unlock(&ns->ns_lock);
310 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
312 lockh->cookie = lock->l_handle.h_cookie;
315 /* if flags: atomically get the lock and set the flags.
316 * Return NULL if flag already set
319 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
321 struct ldlm_namespace *ns;
322 struct ldlm_lock *lock = NULL, *retval = NULL;
327 lock = class_handle2object(handle->cookie);
331 LASSERT(lock->l_resource != NULL);
332 ns = lock->l_resource->lr_namespace;
335 l_lock(&ns->ns_lock);
337 /* It's unlikely but possible that someone marked the lock as
338 * destroyed after we did handle2object on it */
339 if (lock->l_destroyed) {
340 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
345 if (flags && (lock->l_flags & flags)) {
351 lock->l_flags |= flags;
356 l_unlock(&ns->ns_lock);
360 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
361 struct lustre_handle *handle)
363 struct ldlm_lock *retval = NULL;
365 l_lock(&ns->ns_lock);
366 retval = __ldlm_handle2lock(handle, 0);
367 l_unlock(&ns->ns_lock);
372 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
374 ldlm_res2desc(lock->l_resource, &desc->l_resource);
375 desc->l_req_mode = lock->l_req_mode;
376 desc->l_granted_mode = lock->l_granted_mode;
377 memcpy(&desc->l_policy_data, &lock->l_policy_data,
378 sizeof(desc->l_policy_data));
381 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
382 void *data, int datalen)
384 struct ldlm_ast_work *w;
387 l_lock(&lock->l_resource->lr_namespace->ns_lock);
388 if (new && (lock->l_flags & LDLM_FL_AST_SENT))
391 CDEBUG(D_OTHER, "lock %p incompatible; sending blocking AST.\n", lock);
393 OBD_ALLOC(w, sizeof(*w));
400 w->w_datalen = datalen;
402 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
403 lock->l_flags |= LDLM_FL_AST_SENT;
404 /* If the enqueuing client said so, tell the AST recipient to
405 * discard dirty data, rather than writing back. */
406 if (new->l_flags & LDLM_AST_DISCARD_DATA)
407 lock->l_flags |= LDLM_FL_DISCARD_DATA;
409 ldlm_lock2desc(new, &w->w_desc);
412 w->w_lock = LDLM_LOCK_GET(lock);
413 list_add(&w->w_list, lock->l_resource->lr_tmp);
416 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
419 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
421 struct ldlm_lock *lock;
423 lock = ldlm_handle2lock(lockh);
424 ldlm_lock_addref_internal(lock, mode);
428 /* only called for local locks */
429 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
431 l_lock(&lock->l_resource->lr_namespace->ns_lock);
432 ldlm_lock_remove_from_lru(lock);
433 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
437 lock->l_last_used = jiffies;
438 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
440 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
443 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
445 struct ldlm_namespace *ns;
448 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
449 ns = lock->l_resource->lr_namespace;
450 l_lock(&ns->ns_lock);
451 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR) {
452 LASSERT(lock->l_readers > 0);
455 LASSERT(lock->l_writers > 0);
459 if (lock->l_flags & LDLM_FL_LOCAL &&
460 !lock->l_readers && !lock->l_writers) {
461 /* If this is a local lock on a server namespace and this was
462 * the last reference, cancel the lock. */
463 CDEBUG(D_INFO, "forcing cancel of local lock\n");
464 lock->l_flags |= LDLM_FL_CBPENDING;
467 if (!lock->l_readers && !lock->l_writers &&
468 (lock->l_flags & LDLM_FL_CBPENDING)) {
469 /* If we received a blocked AST and this was the last reference,
470 * run the callback. */
471 if (ns->ns_client == LDLM_NAMESPACE_SERVER && lock->l_export)
472 CERROR("FL_CBPENDING set on non-local lock--just a "
475 LDLM_DEBUG(lock, "final decref done on cbpending lock");
476 l_unlock(&ns->ns_lock);
478 l_check_no_ns_lock(ns);
479 /* FIXME: need a real 'desc' here */
480 if (lock->l_blocking_ast != NULL)
481 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
483 } else if (ns->ns_client == LDLM_NAMESPACE_CLIENT &&
484 !lock->l_readers && !lock->l_writers) {
485 /* If this is a client-side namespace and this was the last
486 * reference, put it on the LRU. */
487 LASSERT(list_empty(&lock->l_lru));
488 LASSERT(ns->ns_nr_unused >= 0);
489 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
491 l_unlock(&ns->ns_lock);
494 l_unlock(&ns->ns_lock);
497 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
502 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
504 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
505 LASSERT(lock != NULL);
506 ldlm_lock_decref_internal(lock, mode);
510 /* This will drop a lock reference and mark it for destruction, but will not
511 * necessarily cancel the lock before returning. */
512 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
514 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
517 LASSERT(lock != NULL);
519 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
520 l_lock(&lock->l_resource->lr_namespace->ns_lock);
521 lock->l_flags |= LDLM_FL_CBPENDING;
522 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
523 ldlm_lock_decref_internal(lock, mode);
528 * - ldlm_lock_enqueue
529 * - ldlm_reprocess_queue
530 * - ldlm_lock_convert
532 void ldlm_grant_lock(struct ldlm_lock *lock, void *data, int datalen,
535 struct ldlm_resource *res = lock->l_resource;
538 l_lock(&lock->l_resource->lr_namespace->ns_lock);
539 lock->l_granted_mode = lock->l_req_mode;
540 ldlm_resource_add_lock(res, &res->lr_granted, lock);
542 if (lock->l_granted_mode < res->lr_most_restr)
543 res->lr_most_restr = lock->l_granted_mode;
545 if (run_ast && lock->l_completion_ast != NULL)
546 ldlm_add_ast_work_item(lock, NULL, data, datalen);
548 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
552 /* returns a referenced lock or NULL. See the flag descriptions below, in the
553 * comment above ldlm_lock_match */
554 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
555 ldlm_policy_data_t *policy,
556 struct ldlm_lock *old_lock, int flags)
558 struct ldlm_lock *lock;
559 struct list_head *tmp;
561 list_for_each(tmp, queue) {
562 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
564 if (lock == old_lock)
567 /* llite sometimes wants to match locks that will be
568 * canceled when their users drop, but we allow it to match
569 * if it passes in CBPENDING and the lock still has users.
570 * this is generally only going to be used by children
571 * whose parents already hold a lock so forward progress
572 * can still happen. */
573 if (lock->l_flags & LDLM_FL_CBPENDING &&
574 !(flags & LDLM_FL_CBPENDING))
576 if (lock->l_flags & LDLM_FL_CBPENDING &&
577 lock->l_readers == 0 && lock->l_writers == 0)
580 if (lock->l_req_mode != mode)
583 if (lock->l_resource->lr_type == LDLM_EXTENT &&
584 (lock->l_policy_data.l_extent.start >
585 policy->l_extent.start ||
586 lock->l_policy_data.l_extent.end < policy->l_extent.end))
589 if (lock->l_destroyed)
592 if ((flags & LDLM_FL_LOCAL_ONLY) &&
593 !(lock->l_flags & LDLM_FL_LOCAL))
596 ldlm_lock_addref_internal(lock, mode);
603 void ldlm_lock_allow_match(struct ldlm_lock *lock)
605 l_lock(&lock->l_resource->lr_namespace->ns_lock);
606 lock->l_flags |= LDLM_FL_CAN_MATCH;
607 wake_up(&lock->l_waitq);
608 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
611 /* Can be called in two ways:
613 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
614 * for a duplicate of.
616 * Otherwise, all of the fields must be filled in, to match against.
618 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
619 * server (ie, connh is NULL)
620 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
621 * list will be considered
622 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
623 * to be canceled can still be matched as long as they still have reader
624 * or writer refernces
626 * Returns 1 if it finds an already-existing lock that is compatible; in this
627 * case, lockh is filled in with a addref()ed lock
629 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
630 struct ldlm_res_id *res_id, __u32 type,
631 ldlm_policy_data_t *policy, ldlm_mode_t mode,
632 struct lustre_handle *lockh)
634 struct ldlm_resource *res;
635 struct ldlm_lock *lock, *old_lock = NULL;
640 old_lock = ldlm_handle2lock(lockh);
643 ns = old_lock->l_resource->lr_namespace;
644 res_id = &old_lock->l_resource->lr_name;
645 type = old_lock->l_resource->lr_type;
646 mode = old_lock->l_req_mode;
649 res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
651 LASSERT(old_lock == NULL);
655 l_lock(&ns->ns_lock);
657 lock = search_queue(&res->lr_granted, mode, policy, old_lock, flags);
660 if (flags & LDLM_FL_BLOCK_GRANTED)
662 lock = search_queue(&res->lr_converting, mode, policy, old_lock, flags);
665 lock = search_queue(&res->lr_waiting, mode, policy, old_lock, flags);
671 ldlm_resource_putref(res);
672 l_unlock(&ns->ns_lock);
675 struct l_wait_info lwi;
676 ldlm_lock2handle(lock, lockh);
677 if (lock->l_completion_ast)
678 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
681 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, NULL, NULL, NULL);
683 /* XXX FIXME see comment about CAN_MATCH in lustre_dlm.h */
684 l_wait_event(lock->l_waitq,
685 (lock->l_flags & LDLM_FL_CAN_MATCH), &lwi);
688 LDLM_DEBUG(lock, "matched");
690 LDLM_DEBUG_NOLOCK("not matched");
693 LDLM_LOCK_PUT(old_lock);
698 /* Returns a referenced lock */
699 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
700 struct lustre_handle *parent_lock_handle,
701 struct ldlm_res_id res_id, __u32 type,
703 ldlm_blocking_callback blocking,
704 ldlm_completion_callback completion,
705 ldlm_glimpse_callback glimpse,
706 void *data, __u32 lvb_len)
708 struct ldlm_resource *res, *parent_res = NULL;
709 struct ldlm_lock *lock, *parent_lock = NULL;
712 if (parent_lock_handle) {
713 parent_lock = ldlm_handle2lock(parent_lock_handle);
715 parent_res = parent_lock->l_resource;
718 res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
722 lock = ldlm_lock_new(parent_lock, res);
723 ldlm_resource_putref(res);
724 if (parent_lock != NULL)
725 LDLM_LOCK_PUT(parent_lock);
730 lock->l_req_mode = mode;
731 lock->l_ast_data = data;
732 lock->l_blocking_ast = blocking;
733 lock->l_completion_ast = completion;
734 lock->l_glimpse_ast = glimpse;
736 lock->l_lvb_len = lvb_len;
737 OBD_ALLOC(lock->l_lvb_data, lvb_len);
738 if (lock->l_lvb_data == NULL) {
739 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
746 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
747 struct ldlm_lock **lockp,
748 void *cookie, int *flags)
750 struct ldlm_lock *lock = *lockp;
751 struct ldlm_resource *res = lock->l_resource;
752 int local = res->lr_namespace->ns_client;
753 ldlm_processing_policy policy;
754 ldlm_error_t rc = ELDLM_OK;
757 /* policies are not executed on the client or during replay */
758 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
759 && !local && ns->ns_policy) {
760 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
762 if (rc == ELDLM_LOCK_REPLACED) {
763 /* The lock that was returned has already been granted,
764 * and placed into lockp. If it's not the same as the
765 * one we passed in, then destroy the old one and our
766 * work here is done. */
767 if (lock != *lockp) {
768 ldlm_lock_destroy(lock);
771 *flags |= LDLM_FL_LOCK_CHANGED;
773 } else if (rc == ELDLM_LOCK_ABORTED ||
774 (rc == 0 && (*flags & LDLM_FL_INTENT_ONLY))) {
775 ldlm_lock_destroy(lock);
778 LASSERT(rc == ELDLM_OK);
781 l_lock(&ns->ns_lock);
782 if (local && lock->l_req_mode == lock->l_granted_mode) {
783 /* The server returned a blocked lock, but it was granted before
784 * we got a chance to actually enqueue it. We don't need to do
786 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
787 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
791 /* Some flags from the enqueue want to make it into the AST, via the
793 lock->l_flags |= (*flags & LDLM_AST_DISCARD_DATA);
795 /* This distinction between local lock trees is very important; a client
796 * namespace only has information about locks taken by that client, and
797 * thus doesn't have enough information to decide for itself if it can
798 * be granted (below). In this case, we do exactly what the server
799 * tells us to do, as dictated by the 'flags'.
801 * We do exactly the same thing during recovery, when the server is
802 * more or less trusting the clients not to lie.
804 * FIXME (bug 268): Detect obvious lies by checking compatibility in
805 * granted/converting queues. */
806 ldlm_resource_unlink_lock(lock);
808 if (*flags & LDLM_FL_BLOCK_CONV)
809 ldlm_resource_add_lock(res, &res->lr_converting, lock);
810 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
811 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
813 ldlm_grant_lock(lock, NULL, 0, 0);
815 } else if (*flags & LDLM_FL_REPLAY) {
816 if (*flags & LDLM_FL_BLOCK_CONV) {
817 ldlm_resource_add_lock(res, &res->lr_converting, lock);
819 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
820 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
822 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
823 ldlm_grant_lock(lock, NULL, 0, 0);
826 /* If no flags, fall through to normal enqueue path. */
829 policy = ldlm_processing_policy_table[res->lr_type];
830 policy(lock, flags, 1, &rc);
833 l_unlock(&ns->ns_lock);
837 /* Must be called with namespace taken: queue is waiting or converting. */
838 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue)
840 struct list_head *tmp, *pos;
841 ldlm_processing_policy policy;
843 int rc = LDLM_ITER_CONTINUE;
847 policy = ldlm_processing_policy_table[res->lr_type];
850 list_for_each_safe(tmp, pos, queue) {
851 struct ldlm_lock *pending;
852 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
854 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
857 rc = policy(pending, &flags, 0, &err);
858 if (rc != LDLM_ITER_CONTINUE)
865 int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list)
867 struct list_head *tmp, *pos;
871 l_check_no_ns_lock(ns);
873 list_for_each_safe(tmp, pos, rpc_list) {
874 struct ldlm_ast_work *w =
875 list_entry(tmp, struct ldlm_ast_work, w_list);
877 /* It's possible to receive a completion AST before we've set
878 * the l_completion_ast pointer: either because the AST arrived
879 * before the reply, or simply because there's a small race
880 * window between receiving the reply and finishing the local
883 * This can't happen with the blocking_ast, however, because we
884 * will never call the local blocking_ast until we drop our
885 * reader/writer reference, which we won't do until we get the
886 * reply and finish enqueueing. */
887 LASSERT(w->w_lock != NULL);
889 LASSERT(w->w_lock->l_blocking_ast != NULL);
890 rc = w->w_lock->l_blocking_ast
891 (w->w_lock, &w->w_desc, w->w_data,
893 } else if (w->w_lock->l_completion_ast != NULL) {
894 LASSERT(w->w_lock->l_completion_ast != NULL);
895 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
903 CDEBUG(D_DLMTRACE, "Failed AST - should clean & "
904 "disconnect client\n");
905 LDLM_LOCK_PUT(w->w_lock);
906 list_del(&w->w_list);
907 OBD_FREE(w, sizeof(*w));
912 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
914 ldlm_reprocess_all(res);
915 return LDLM_ITER_CONTINUE;
918 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
922 l_lock(&ns->ns_lock);
923 for (i = 0; i < RES_HASH_SIZE; i++) {
924 struct list_head *tmp, *next;
925 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
926 struct ldlm_resource *res =
927 list_entry(tmp, struct ldlm_resource, lr_hash);
929 ldlm_resource_getref(res);
930 l_unlock(&ns->ns_lock);
931 rc = reprocess_one_queue(res, NULL);
932 l_lock(&ns->ns_lock);
934 ldlm_resource_putref(res);
935 if (rc == LDLM_ITER_STOP)
940 l_unlock(&ns->ns_lock);
944 void ldlm_reprocess_all(struct ldlm_resource *res)
946 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
950 /* Local lock trees don't get reprocessed. */
951 if (res->lr_namespace->ns_client) {
957 l_lock(&res->lr_namespace->ns_lock);
958 res->lr_tmp = &rpc_list;
960 rc = ldlm_reprocess_queue(res, &res->lr_converting);
961 if (rc == LDLM_ITER_CONTINUE)
962 ldlm_reprocess_queue(res, &res->lr_waiting);
965 l_unlock(&res->lr_namespace->ns_lock);
967 rc = ldlm_run_ast_work(res->lr_namespace, &rpc_list);
968 if (rc == -ERESTART) {
969 LASSERT(list_empty(&rpc_list));
975 void ldlm_cancel_callback(struct ldlm_lock *lock)
977 l_lock(&lock->l_resource->lr_namespace->ns_lock);
978 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
979 lock->l_flags |= LDLM_FL_CANCEL;
980 if (lock->l_blocking_ast) {
981 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
982 // l_check_no_ns_lock(lock->l_resource->lr_namespace);
983 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
987 LDLM_DEBUG(lock, "no blocking ast");
990 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
993 void ldlm_lock_cancel(struct ldlm_lock *lock)
995 struct ldlm_resource *res;
996 struct ldlm_namespace *ns;
999 /* There's no race between calling this and taking the ns lock below;
1000 * a lock can only be put on the waiting list once, because it can only
1001 * issue a blocking AST once. */
1002 ldlm_del_waiting_lock(lock);
1004 res = lock->l_resource;
1005 ns = res->lr_namespace;
1007 l_lock(&ns->ns_lock);
1008 /* Please do not, no matter how tempting, remove this LBUG without
1009 * talking to me first. -phik */
1010 if (lock->l_readers || lock->l_writers) {
1011 LDLM_DEBUG(lock, "lock still has references");
1012 ldlm_lock_dump(D_OTHER, lock, 0);
1016 ldlm_cancel_callback(lock);
1018 ldlm_resource_unlink_lock(lock);
1019 ldlm_lock_destroy(lock);
1020 l_unlock(&ns->ns_lock);
1024 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1026 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1032 lock->l_ast_data = data;
1033 LDLM_LOCK_PUT(lock);
1037 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1039 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
1040 struct ldlm_lock *lock;
1041 struct ldlm_resource *res;
1043 l_lock(&ns->ns_lock);
1044 while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
1045 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1046 struct ldlm_lock, l_export_chain);
1047 res = ldlm_resource_getref(lock->l_resource);
1048 LDLM_DEBUG(lock, "export %p", exp);
1049 ldlm_lock_cancel(lock);
1050 l_unlock(&ns->ns_lock);
1051 ldlm_reprocess_all(res);
1052 ldlm_resource_putref(res);
1053 l_lock(&ns->ns_lock);
1055 l_unlock(&ns->ns_lock);
1058 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1061 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1062 struct ldlm_resource *res;
1063 struct ldlm_namespace *ns;
1069 res = lock->l_resource;
1070 ns = res->lr_namespace;
1072 l_lock(&ns->ns_lock);
1074 lock->l_req_mode = new_mode;
1075 ldlm_resource_unlink_lock(lock);
1077 /* If this is a local resource, put it on the appropriate list. */
1078 if (res->lr_namespace->ns_client) {
1079 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1080 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1082 /* This should never happen, because of the way the
1083 * server handles conversions. */
1086 res->lr_tmp = &rpc_list;
1087 ldlm_grant_lock(lock, NULL, 0, 0);
1090 /* FIXME: completion handling not with ns_lock held ! */
1091 if (lock->l_completion_ast)
1092 lock->l_completion_ast(lock, 0, NULL);
1095 /* FIXME: We should try the conversion right away and possibly
1096 * return success without the need for an extra AST */
1097 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1098 *flags |= LDLM_FL_BLOCK_CONV;
1101 l_unlock(&ns->ns_lock);
1104 ldlm_run_ast_work(ns, &rpc_list);
1108 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1110 char str[PTL_NALFMT_SIZE];
1111 struct obd_device *obd = NULL;
1113 if (!((portal_debug | D_ERROR) & level))
1117 CDEBUG(level, " NULL LDLM lock\n");
1121 CDEBUG(level, " -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d)\n",
1122 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1124 if (lock->l_conn_export != NULL)
1125 obd = lock->l_conn_export->exp_obd;
1126 if (lock->l_export && lock->l_export->exp_connection) {
1127 CDEBUG(level, " Node: NID "LPX64" (%s) on %s (rhandle: "LPX64")\n",
1128 lock->l_export->exp_connection->c_peer.peer_nid,
1129 portals_nid2str(lock->l_export->exp_connection->c_peer.peer_ni->pni_number,
1130 lock->l_export->exp_connection->c_peer.peer_nid, str),
1131 lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1132 lock->l_remote_handle.cookie);
1133 } else if (obd == NULL) {
1134 CDEBUG(level, " Node: local\n");
1136 struct obd_import *imp = obd->u.cli.cl_import;
1137 CDEBUG(level, " Node: NID "LPX64" (%s) on %s (rhandle: "LPX64")\n",
1138 imp->imp_connection->c_peer.peer_nid,
1139 portals_nid2str(imp->imp_connection->c_peer.peer_ni->pni_number,
1140 imp->imp_connection->c_peer.peer_nid, str),
1141 imp->imp_connection->c_peer.peer_ni->pni_name,
1142 lock->l_remote_handle.cookie);
1144 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64")\n", lock->l_resource,
1145 lock->l_resource->lr_name.name[0],
1146 lock->l_resource->lr_name.name[1]);
1147 CDEBUG(level, " Req mode: %d, grant mode: %d, rc: %u, read: %d, "
1148 "write: %d\n", (int)lock->l_req_mode, (int)lock->l_granted_mode,
1149 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers);
1150 if (lock->l_resource->lr_type == LDLM_EXTENT)
1151 CDEBUG(level, " Extent: "LPU64" -> "LPU64"\n",
1152 lock->l_policy_data.l_extent.start,
1153 lock->l_policy_data.l_extent.end);
1154 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1155 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1156 lock->l_policy_data.l_flock.pid,
1157 lock->l_policy_data.l_flock.start,
1158 lock->l_policy_data.l_flock.end);
1161 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1163 struct ldlm_lock *lock;
1165 lock = ldlm_handle2lock(lockh);
1169 ldlm_lock_dump(D_OTHER, lock, 0);
1171 LDLM_LOCK_PUT(lock);