1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002 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
26 #include <linux/slab.h>
27 #include <linux/module.h>
28 #include <linux/random.h>
29 #include <linux/lustre_dlm.h>
30 #include <linux/lustre_mds.h>
31 #include <linux/obd_class.h>
33 //struct lustre_lock ldlm_everything_lock;
36 char *ldlm_lockname[] = {
45 char *ldlm_typename[] = {
48 [LDLM_MDSINTENT] "INT"
51 char *ldlm_it2str(int it)
58 case (IT_OPEN | IT_CREAT):
89 CERROR("Unknown intent %d\n", it);
94 extern kmem_cache_t *ldlm_lock_slab;
96 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
98 ldlm_res_compat ldlm_res_compat_table[] = {
99 [LDLM_PLAIN] ldlm_plain_compat,
100 [LDLM_EXTENT] ldlm_extent_compat,
101 [LDLM_MDSINTENT] ldlm_plain_compat
104 ldlm_res_policy ldlm_res_policy_table[] = {
106 [LDLM_EXTENT] ldlm_extent_policy,
107 [LDLM_MDSINTENT] NULL
110 void ldlm_register_intent(int (*arg) (struct ldlm_lock * lock, void *req_cookie,
111 ldlm_mode_t mode, int flags, void *data))
113 ldlm_res_policy_table[LDLM_MDSINTENT] = arg;
116 void ldlm_unregister_intent(void)
118 ldlm_res_policy_table[LDLM_MDSINTENT] = NULL;
122 * REFCOUNTED LOCK OBJECTS
127 * Lock refcounts, during creation:
128 * - one special one for allocation, dec'd only once in destroy
129 * - one for being a lock that's in-use
130 * - one for the addref associated with a new lock
132 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
134 l_lock(&lock->l_resource->lr_namespace->ns_lock);
136 ldlm_resource_getref(lock->l_resource);
137 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
141 void ldlm_lock_put(struct ldlm_lock *lock)
143 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
146 l_lock(&ns->ns_lock);
148 //LDLM_DEBUG(lock, "after refc--");
149 if (lock->l_refc < 0)
152 if (ldlm_resource_put(lock->l_resource)) {
153 LASSERT(lock->l_refc == 0);
154 lock->l_resource = NULL;
157 LDLM_LOCK_PUT(lock->l_parent);
159 if (lock->l_refc == 0 && (lock->l_flags & LDLM_FL_DESTROYED)) {
160 l_unlock(&ns->ns_lock);
161 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
163 //spin_lock(&ldlm_handle_lock);
164 spin_lock(&ns->ns_counter_lock);
166 spin_unlock(&ns->ns_counter_lock);
168 lock->l_resource = NULL;
169 lock->l_random = DEAD_HANDLE_MAGIC;
170 if (lock->l_export && lock->l_export->exp_connection)
171 ptlrpc_put_connection(lock->l_export->exp_connection);
172 kmem_cache_free(ldlm_lock_slab, lock);
173 //spin_unlock(&ldlm_handle_lock);
174 CDEBUG(D_MALLOC, "kfreed 'lock': %d at %p (tot 0).\n",
175 sizeof(*lock), lock);
177 l_unlock(&ns->ns_lock);
182 void ldlm_lock_destroy(struct ldlm_lock *lock)
185 l_lock(&lock->l_resource->lr_namespace->ns_lock);
187 if (!list_empty(&lock->l_children)) {
188 LDLM_DEBUG(lock, "still has children (%p)!",
189 lock->l_children.next);
190 ldlm_lock_dump(lock);
193 if (lock->l_readers || lock->l_writers) {
194 LDLM_DEBUG(lock, "lock still has references");
195 ldlm_lock_dump(lock);
198 if (!list_empty(&lock->l_res_link)) {
199 ldlm_lock_dump(lock);
203 if (lock->l_flags & LDLM_FL_DESTROYED) {
204 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
209 list_del(&lock->l_export_chain);
210 lock->l_export = NULL;
211 lock->l_flags |= LDLM_FL_DESTROYED;
213 /* Wake anyone waiting for this lock */
214 /* FIXME: I should probably add yet another flag, instead of using
215 * l_export to only call this on clients */
216 if (lock->l_export && lock->l_completion_ast)
217 lock->l_completion_ast(lock, 0);
219 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
225 usage: pass in a resource on which you have done get
226 pass in a parent lock on which you have done a get
227 do not put the resource or the parent
228 returns: lock with refcount 1
230 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
231 struct ldlm_resource *resource)
233 struct ldlm_lock *lock;
236 if (resource == NULL)
239 lock = kmem_cache_alloc(ldlm_lock_slab, SLAB_KERNEL);
243 memset(lock, 0, sizeof(*lock));
244 get_random_bytes(&lock->l_random, sizeof(__u64));
246 lock->l_resource = resource;
247 /* this refcount matches the one of the resource passed
248 in which is not being put away */
250 INIT_LIST_HEAD(&lock->l_children);
251 INIT_LIST_HEAD(&lock->l_res_link);
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 = parent;
263 list_add(&lock->l_childof, &parent->l_children);
264 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
267 CDEBUG(D_MALLOC, "kmalloced 'lock': %d at "
268 "%p (tot %d).\n", sizeof(*lock), lock, 1);
269 /* this is the extra refcount, to prevent the lock from evaporating */
274 int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3])
276 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
277 struct ldlm_resource *oldres = lock->l_resource;
281 l_lock(&ns->ns_lock);
282 if (memcmp(new_resid, lock->l_resource->lr_name,
283 sizeof(lock->l_resource->lr_name)) == 0) {
285 l_unlock(&ns->ns_lock);
289 type = lock->l_resource->lr_type;
290 if (new_resid[0] == 0)
292 lock->l_resource = ldlm_resource_get(ns, NULL, new_resid, type, 1);
293 if (lock->l_resource == NULL) {
298 /* move references over */
299 for (i = 0; i < lock->l_refc; i++) {
301 ldlm_resource_getref(lock->l_resource);
302 rc = ldlm_resource_put(oldres);
303 if (rc == 1 && i != lock->l_refc - 1)
306 /* compensate for the initial get above.. */
307 ldlm_resource_put(lock->l_resource);
309 l_unlock(&ns->ns_lock);
317 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
319 lockh->addr = (__u64) (unsigned long)lock;
320 lockh->cookie = lock->l_random;
323 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle,
326 struct ldlm_lock *lock = NULL, *retval = NULL;
329 if (!handle || !handle->addr)
332 //spin_lock(&ldlm_handle_lock);
333 lock = (struct ldlm_lock *)(unsigned long)(handle->addr);
334 if (!kmem_cache_validate(ldlm_lock_slab, (void *)lock)) {
335 CERROR("bogus lock %p\n", lock);
339 if (lock->l_random != handle->cookie) {
340 CERROR("bogus cookie: lock %p has "LPX64" vs. handle "LPX64"\n",
341 lock, lock->l_random, handle->cookie);
344 if (!lock->l_resource) {
345 CERROR("trying to lock bogus resource: lock %p\n", lock);
346 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
349 if (!lock->l_resource->lr_namespace) {
350 CERROR("trying to lock bogus namespace: lock %p\n", lock);
351 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
355 l_lock(&lock->l_resource->lr_namespace->ns_lock);
356 if (strict && lock->l_flags & LDLM_FL_DESTROYED) {
357 CERROR("lock already destroyed: lock %p\n", lock);
358 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
362 retval = LDLM_LOCK_GET(lock);
364 CERROR("lock disappeared below us!!! %p\n", lock);
367 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
369 //spin_unlock(&ldlm_handle_lock);
373 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
375 return lockmode_compat(a->l_req_mode, b->l_req_mode);
378 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
380 ldlm_res2desc(lock->l_resource, &desc->l_resource);
381 desc->l_req_mode = lock->l_req_mode;
382 desc->l_granted_mode = lock->l_granted_mode;
383 memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
384 memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
387 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
388 struct ldlm_lock *new)
390 struct ldlm_ast_work *w;
393 l_lock(&lock->l_resource->lr_namespace->ns_lock);
394 if (new && (lock->l_flags & LDLM_FL_AST_SENT))
397 OBD_ALLOC(w, sizeof(*w));
404 lock->l_flags |= LDLM_FL_AST_SENT;
406 ldlm_lock2desc(new, &w->w_desc);
409 w->w_lock = LDLM_LOCK_GET(lock);
410 list_add(&w->w_list, lock->l_resource->lr_tmp);
412 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
416 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
418 struct ldlm_lock *lock;
420 lock = ldlm_handle2lock(lockh);
421 ldlm_lock_addref_internal(lock, mode);
425 /* only called for local locks */
426 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
428 l_lock(&lock->l_resource->lr_namespace->ns_lock);
429 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
433 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
435 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
438 /* Args: unlocked lock */
439 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
441 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
447 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
448 l_lock(&lock->l_resource->lr_namespace->ns_lock);
449 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
454 /* If we received a blocked AST and this was the last reference,
455 * run the callback. */
456 if (!lock->l_readers && !lock->l_writers &&
457 (lock->l_flags & LDLM_FL_CBPENDING)) {
458 if (!lock->l_resource->lr_namespace->ns_client &&
460 CERROR("FL_CBPENDING set on non-local lock--just a "
463 LDLM_DEBUG(lock, "final decref done on cbpending lock");
464 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
466 /* FIXME: need a real 'desc' here */
467 lock->l_blocking_ast(lock, NULL, lock->l_data,
468 lock->l_data_len, LDLM_CB_BLOCKING);
470 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
472 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
473 LDLM_LOCK_PUT(lock); /* matches the handle2lock above */
478 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
479 struct list_head *queue)
481 struct list_head *tmp, *pos;
484 list_for_each_safe(tmp, pos, queue) {
485 struct ldlm_lock *child;
486 ldlm_res_compat compat;
488 child = list_entry(tmp, struct ldlm_lock, l_res_link);
492 compat = ldlm_res_compat_table[child->l_resource->lr_type];
493 if (compat && compat(child, lock)) {
494 CDEBUG(D_OTHER, "compat function succeded, next.\n");
497 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
498 CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
504 if (send_cbs && child->l_blocking_ast != NULL) {
505 CDEBUG(D_OTHER, "lock %p incompatible; sending "
506 "blocking AST.\n", child);
507 ldlm_add_ast_work_item(child, lock);
514 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
519 l_lock(&lock->l_resource->lr_namespace->ns_lock);
520 rc = ldlm_lock_compat_list(lock, send_cbs,
521 &lock->l_resource->lr_granted);
522 /* FIXME: should we be sending ASTs to converting? */
524 rc = ldlm_lock_compat_list
525 (lock, send_cbs, &lock->l_resource->lr_converting);
527 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
532 - ldlm_handle_enqueuque - resource
534 void ldlm_grant_lock(struct ldlm_lock *lock)
536 struct ldlm_resource *res = lock->l_resource;
539 l_lock(&lock->l_resource->lr_namespace->ns_lock);
540 ldlm_resource_add_lock(res, &res->lr_granted, lock);
541 lock->l_granted_mode = lock->l_req_mode;
543 if (lock->l_granted_mode < res->lr_most_restr)
544 res->lr_most_restr = lock->l_granted_mode;
546 if (lock->l_completion_ast) {
547 ldlm_add_ast_work_item(lock, NULL);
549 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
553 /* returns a referenced lock or NULL */
554 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
555 struct ldlm_extent *extent)
557 struct ldlm_lock *lock;
558 struct list_head *tmp;
560 list_for_each(tmp, queue) {
561 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
563 if (lock->l_flags & (LDLM_FL_CBPENDING | LDLM_FL_DESTROYED))
566 /* lock_convert() takes the resource lock, so we're sure that
567 * req_mode and lr_type won't change beneath us */
568 if (lock->l_req_mode != mode)
571 if (lock->l_resource->lr_type == LDLM_EXTENT &&
572 (lock->l_extent.start > extent->start ||
573 lock->l_extent.end < extent->end))
576 ldlm_lock_addref_internal(lock, mode);
583 /* Must be called with no resource or lock locks held.
585 * Returns 1 if it finds an already-existing lock that is compatible; in this
586 * case, lockh is filled in with a addref()ed lock
588 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 * res_id, __u32 type,
589 void *cookie, int cookielen, ldlm_mode_t mode,
590 struct lustre_handle *lockh)
592 struct ldlm_resource *res;
593 struct ldlm_lock *lock;
597 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
601 ns = res->lr_namespace;
602 l_lock(&ns->ns_lock);
604 if ((lock = search_queue(&res->lr_granted, mode, cookie)))
606 if ((lock = search_queue(&res->lr_converting, mode, cookie)))
608 if ((lock = search_queue(&res->lr_waiting, mode, cookie)))
613 ldlm_resource_put(res);
614 l_unlock(&ns->ns_lock);
617 ldlm_lock2handle(lock, lockh);
618 if (lock->l_completion_ast)
619 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
622 LDLM_DEBUG(lock, "matched");
624 LDLM_DEBUG_NOLOCK("not matched");
628 /* Returns a referenced lock */
629 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
630 struct lustre_handle *parent_lock_handle,
631 __u64 * res_id, __u32 type,
632 ldlm_mode_t mode, void *data, __u32 data_len)
634 struct ldlm_resource *res, *parent_res = NULL;
635 struct ldlm_lock *lock, *parent_lock;
637 parent_lock = ldlm_handle2lock(parent_lock_handle);
639 parent_res = parent_lock->l_resource;
641 res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
645 lock = ldlm_lock_new(parent_lock, res);
647 ldlm_resource_put(res);
651 lock->l_req_mode = mode;
653 lock->l_data_len = data_len;
658 /* Must be called with lock->l_lock and lock->l_resource->lr_lock not held */
659 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock * lock,
660 void *cookie, int cookie_len,
662 ldlm_completion_callback completion,
663 ldlm_blocking_callback blocking)
665 struct ldlm_resource *res;
667 ldlm_res_policy policy;
670 res = lock->l_resource;
671 lock->l_blocking_ast = blocking;
673 if (res->lr_type == LDLM_EXTENT)
674 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
676 /* policies are not executed on the client or during replay */
677 local = res->lr_namespace->ns_client;
678 if (!local && !(*flags & LDLM_FL_REPLAY) &&
679 (policy = ldlm_res_policy_table[res->lr_type])) {
681 rc = policy(lock, cookie, lock->l_req_mode, *flags, NULL);
683 if (rc == ELDLM_LOCK_CHANGED) {
684 res = lock->l_resource;
685 *flags |= LDLM_FL_LOCK_CHANGED;
686 } else if (rc == ELDLM_LOCK_ABORTED) {
687 ldlm_lock_destroy(lock);
692 l_lock(&res->lr_namespace->ns_lock);
693 if (local && lock->l_req_mode == lock->l_granted_mode) {
694 /* The server returned a blocked lock, but it was granted before
695 * we got a chance to actually enqueue it. We don't need to do
697 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
698 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
702 /* This distinction between local lock trees is very important; a client
703 * namespace only has information about locks taken by that client, and
704 * thus doesn't have enough information to decide for itself if it can
705 * be granted (below). In this case, we do exactly what the server
706 * tells us to do, as dictated by the 'flags'.
708 * We do exactly the same thing during recovery, when the server is
709 * more or less trusting the clients not to lie.
711 * FIXME (bug 629283): Detect obvious lies by checking compatibility in
712 * granted/converting queues. */
713 ldlm_resource_unlink_lock(lock);
714 if (local || (*flags & LDLM_FL_REPLAY)) {
715 if (*flags & LDLM_FL_BLOCK_CONV)
716 ldlm_resource_add_lock(res, res->lr_converting.prev,
718 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
719 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
721 ldlm_grant_lock(lock);
725 /* FIXME: We may want to optimize by checking lr_most_restr */
726 if (!list_empty(&res->lr_converting)) {
727 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
728 *flags |= LDLM_FL_BLOCK_CONV;
731 if (!list_empty(&res->lr_waiting)) {
732 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
733 *flags |= LDLM_FL_BLOCK_WAIT;
736 if (!ldlm_lock_compat(lock, 0)) {
737 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
738 *flags |= LDLM_FL_BLOCK_GRANTED;
742 ldlm_grant_lock(lock);
745 l_unlock(&res->lr_namespace->ns_lock);
746 /* Don't set 'completion_ast' until here so that if the lock is granted
747 * immediately we don't do an unnecessary completion call. */
748 lock->l_completion_ast = completion;
752 /* Must be called with namespace taken: queue is waiting or converting. */
753 static int ldlm_reprocess_queue(struct ldlm_resource *res,
754 struct list_head *queue)
756 struct list_head *tmp, *pos;
759 list_for_each_safe(tmp, pos, queue) {
760 struct ldlm_lock *pending;
761 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
763 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
765 if (!ldlm_lock_compat(pending, 1))
768 list_del_init(&pending->l_res_link);
769 ldlm_grant_lock(pending);
775 void ldlm_run_ast_work(struct list_head *rpc_list)
777 struct list_head *tmp, *pos;
781 list_for_each_safe(tmp, pos, rpc_list) {
782 struct ldlm_ast_work *w =
783 list_entry(tmp, struct ldlm_ast_work, w_list);
786 rc = w->w_lock->l_blocking_ast
787 (w->w_lock, &w->w_desc, w->w_data,
788 w->w_datalen, LDLM_CB_BLOCKING);
790 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags);
792 CERROR("Failed AST - should clean & disconnect "
794 LDLM_LOCK_PUT(w->w_lock);
795 list_del(&w->w_list);
796 OBD_FREE(w, sizeof(*w));
801 /* Must be called with resource->lr_lock not taken. */
802 void ldlm_reprocess_all(struct ldlm_resource *res)
804 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
807 /* Local lock trees don't get reprocessed. */
808 if (res->lr_namespace->ns_client) {
813 l_lock(&res->lr_namespace->ns_lock);
814 res->lr_tmp = &rpc_list;
816 ldlm_reprocess_queue(res, &res->lr_converting);
817 if (list_empty(&res->lr_converting))
818 ldlm_reprocess_queue(res, &res->lr_waiting);
821 l_unlock(&res->lr_namespace->ns_lock);
823 ldlm_run_ast_work(&rpc_list);
827 void ldlm_cancel_callback(struct ldlm_lock *lock)
829 l_lock(&lock->l_resource->lr_namespace->ns_lock);
830 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
831 lock->l_flags |= LDLM_FL_CANCEL;
832 lock->l_blocking_ast(lock, NULL, lock->l_data,
833 lock->l_data_len, LDLM_CB_CANCELING);
835 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
838 void ldlm_lock_cancel(struct ldlm_lock *lock)
840 struct ldlm_resource *res;
841 struct ldlm_namespace *ns;
844 res = lock->l_resource;
845 ns = res->lr_namespace;
847 l_lock(&ns->ns_lock);
848 if (lock->l_readers || lock->l_writers)
849 CDEBUG(D_INFO, "lock still has references (%d readers, %d "
850 "writers)\n", lock->l_readers, lock->l_writers);
852 ldlm_cancel_callback(lock);
854 ldlm_del_waiting_lock(lock);
855 ldlm_resource_unlink_lock(lock);
856 ldlm_lock_destroy(lock);
857 l_unlock(&ns->ns_lock);
861 void ldlm_cancel_locks_for_export(struct obd_export *exp)
863 struct list_head *iter, *n; /* MUST BE CALLED "n"! */
865 list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
866 struct ldlm_lock *lock;
867 struct ldlm_resource *res;
868 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
869 res = ldlm_resource_getref(lock->l_resource);
870 LDLM_DEBUG(lock, "export %p", exp);
871 ldlm_lock_cancel(lock);
872 ldlm_reprocess_all(res);
873 ldlm_resource_put(res);
877 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
880 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
881 struct ldlm_resource *res;
882 struct ldlm_namespace *ns;
886 res = lock->l_resource;
887 ns = res->lr_namespace;
889 l_lock(&ns->ns_lock);
891 lock->l_req_mode = new_mode;
892 ldlm_resource_unlink_lock(lock);
894 /* If this is a local resource, put it on the appropriate list. */
895 if (res->lr_namespace->ns_client) {
896 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED))
897 ldlm_resource_add_lock(res, res->lr_converting.prev,
900 /* This should never happen, because of the way the
901 * server handles conversions. */
904 res->lr_tmp = &rpc_list;
905 ldlm_grant_lock(lock);
908 /* FIXME: completion handling not with ns_lock held ! */
909 if (lock->l_completion_ast)
910 lock->l_completion_ast(lock, 0);
913 /* FIXME: We should try the conversion right away and possibly
914 * return success without the need for an extra AST */
915 ldlm_resource_add_lock(res, res->lr_converting.prev, lock);
916 *flags |= LDLM_FL_BLOCK_CONV;
919 l_unlock(&ns->ns_lock);
922 ldlm_run_ast_work(&rpc_list);
926 void ldlm_lock_dump(struct ldlm_lock *lock)
930 if (!(portal_debug & D_OTHER))
933 if (RES_VERSION_SIZE != 4)
937 CDEBUG(D_OTHER, " NULL LDLM lock\n");
941 snprintf(ver, sizeof(ver), "%x %x %x %x",
942 lock->l_version[0], lock->l_version[1],
943 lock->l_version[2], lock->l_version[3]);
945 CDEBUG(D_OTHER, " -- Lock dump: %p (%s)\n", lock, ver);
946 if (lock->l_export && lock->l_export->exp_connection)
947 CDEBUG(D_OTHER, " Node: NID %x (rhandle: "LPX64")\n",
948 lock->l_export->exp_connection->c_peer.peer_nid,
949 lock->l_remote_handle.addr);
951 CDEBUG(D_OTHER, " Node: local\n");
952 CDEBUG(D_OTHER, " Parent: %p\n", lock->l_parent);
953 CDEBUG(D_OTHER, " Resource: %p ("LPD64")\n", lock->l_resource,
954 lock->l_resource->lr_name[0]);
955 CDEBUG(D_OTHER, " Requested mode: %d, granted mode: %d\n",
956 (int)lock->l_req_mode, (int)lock->l_granted_mode);
957 CDEBUG(D_OTHER, " Readers: %u ; Writers; %u\n",
958 lock->l_readers, lock->l_writers);
959 if (lock->l_resource->lr_type == LDLM_EXTENT)
960 CDEBUG(D_OTHER, " Extent: %Lu -> %Lu\n",
961 (unsigned long long)lock->l_extent.start,
962 (unsigned long long)lock->l_extent.end);