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 /* this lock protects ldlm_handle2lock's integrity */
34 //static spinlock_t ldlm_handle_lock = SPIN_LOCK_UNLOCKED;
37 char *ldlm_lockname[] = {
46 char *ldlm_typename[] = {
49 [LDLM_MDSINTENT] "INT"
52 char *ldlm_it2str(int it)
59 case (IT_OPEN | IT_CREAT):
90 CERROR("Unknown intent %d\n", it);
95 extern kmem_cache_t *ldlm_lock_slab;
97 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
99 ldlm_res_compat ldlm_res_compat_table[] = {
100 [LDLM_PLAIN] ldlm_plain_compat,
101 [LDLM_EXTENT] ldlm_extent_compat,
102 [LDLM_MDSINTENT] ldlm_plain_compat
105 ldlm_res_policy ldlm_res_policy_table[] = {
107 [LDLM_EXTENT] ldlm_extent_policy,
108 [LDLM_MDSINTENT] NULL
111 void ldlm_register_intent(int (*arg) (struct ldlm_lock * lock, void *req_cookie,
112 ldlm_mode_t mode, void *data))
114 ldlm_res_policy_table[LDLM_MDSINTENT] = arg;
117 void ldlm_unregister_intent(void)
119 ldlm_res_policy_table[LDLM_MDSINTENT] = 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 l_lock(&lock->l_resource->lr_namespace->ns_lock);
137 ldlm_resource_getref(lock->l_resource);
138 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
142 void ldlm_lock_put(struct ldlm_lock *lock)
144 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
147 l_lock(&ns->ns_lock);
149 //LDLM_DEBUG(lock, "after refc--");
150 if (lock->l_refc < 0)
153 if (ldlm_resource_put(lock->l_resource))
154 lock->l_resource = NULL;
156 LDLM_LOCK_PUT(lock->l_parent);
158 if (lock->l_refc == 0 && (lock->l_flags & LDLM_FL_DESTROYED)) {
159 l_unlock(&ns->ns_lock);
160 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
162 //spin_lock(&ldlm_handle_lock);
163 spin_lock(&ns->ns_counter_lock);
165 spin_unlock(&ns->ns_counter_lock);
167 lock->l_resource = NULL;
168 lock->l_random = 0xdeadbeefcafebabe;
169 if (lock->l_export && lock->l_export->exp_connection)
170 ptlrpc_put_connection(lock->l_export->exp_connection);
171 kmem_cache_free(ldlm_lock_slab, lock);
172 //spin_unlock(&ldlm_handle_lock);
173 CDEBUG(D_MALLOC, "kfreed 'lock': %d at %p (tot 0).\n",
174 sizeof(*lock), lock);
176 l_unlock(&ns->ns_lock);
181 void ldlm_lock_destroy(struct ldlm_lock *lock)
184 l_lock(&lock->l_resource->lr_namespace->ns_lock);
186 if (!list_empty(&lock->l_children)) {
187 LDLM_DEBUG(lock, "still has children (%p)!",
188 lock->l_children.next);
189 ldlm_lock_dump(lock);
192 if (lock->l_readers || lock->l_writers) {
193 LDLM_DEBUG(lock, "lock still has references");
194 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 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
219 usage: pass in a resource on which you have done get
220 pass in a parent lock on which you have done a get
221 do not put the resource or the parent
222 returns: lock with refcount 1
224 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
225 struct ldlm_resource *resource)
227 struct ldlm_lock *lock;
230 if (resource == NULL)
233 lock = kmem_cache_alloc(ldlm_lock_slab, SLAB_KERNEL);
237 memset(lock, 0, sizeof(*lock));
238 get_random_bytes(&lock->l_random, sizeof(__u64));
240 lock->l_resource = resource;
241 /* this refcount matches the one of the resource passed
242 in which is not being put away */
244 INIT_LIST_HEAD(&lock->l_children);
245 INIT_LIST_HEAD(&lock->l_res_link);
246 INIT_LIST_HEAD(&lock->l_export_chain);
247 INIT_LIST_HEAD(&lock->l_pending_chain);
248 init_waitqueue_head(&lock->l_waitq);
250 spin_lock(&resource->lr_namespace->ns_counter_lock);
251 resource->lr_namespace->ns_locks++;
252 spin_unlock(&resource->lr_namespace->ns_counter_lock);
254 if (parent != NULL) {
255 l_lock(&parent->l_resource->lr_namespace->ns_lock);
256 lock->l_parent = parent;
257 list_add(&lock->l_childof, &parent->l_children);
258 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
261 CDEBUG(D_MALLOC, "kmalloced 'lock': %d at "
262 "%p (tot %d).\n", sizeof(*lock), lock, 1);
263 /* this is the extra refcount, to prevent the lock from evaporating */
268 int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3])
270 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
271 struct ldlm_resource *oldres = lock->l_resource;
275 l_lock(&ns->ns_lock);
276 if (memcmp(new_resid, lock->l_resource->lr_name,
277 sizeof(lock->l_resource->lr_name)) == 0) {
279 l_unlock(&ns->ns_lock);
283 type = lock->l_resource->lr_type;
284 if (new_resid[0] == 0)
286 lock->l_resource = ldlm_resource_get(ns, NULL, new_resid, type, 1);
287 if (lock->l_resource == NULL) {
292 /* move references over */
293 for (i = 0; i < lock->l_refc; i++) {
295 ldlm_resource_getref(lock->l_resource);
296 rc = ldlm_resource_put(oldres);
297 if (rc == 1 && i != lock->l_refc - 1)
300 /* compensate for the initial get above.. */
301 ldlm_resource_put(lock->l_resource);
303 l_unlock(&ns->ns_lock);
311 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
313 lockh->addr = (__u64) (unsigned long)lock;
314 lockh->cookie = lock->l_random;
317 struct ldlm_lock *ldlm_handle2lock(struct lustre_handle *handle)
319 struct ldlm_lock *lock = NULL, *retval = NULL;
322 if (!handle || !handle->addr) {
323 CERROR("bogus handle %p->"LPX64"\n", handle,
324 handle ? handle->addr : -1);
328 //spin_lock(&ldlm_handle_lock);
329 lock = (struct ldlm_lock *)(unsigned long)(handle->addr);
330 if (!kmem_cache_validate(ldlm_lock_slab, (void *)lock)) {
331 CERROR("bogus lock %p\n", lock);
335 if (lock->l_random != handle->cookie) {
336 CERROR("bogus cookie: lock "LPX64", handle "LPX64"\n",
337 lock->l_random, handle->cookie);
340 if (!lock->l_resource) {
341 CERROR("trying to lock bogus resource: lock %p\n", lock);
342 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
345 if (!lock->l_resource->lr_namespace) {
346 CERROR("trying to lock bogus namespace: lock %p\n", lock);
347 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
351 l_lock(&lock->l_resource->lr_namespace->ns_lock);
352 if (lock->l_flags & LDLM_FL_DESTROYED) {
353 CERROR("lock already destroyed: lock %p\n", lock);
354 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
358 retval = LDLM_LOCK_GET(lock);
360 CERROR("lock disappeared below us!!! %p\n", lock);
363 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
365 //spin_unlock(&ldlm_handle_lock);
369 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
371 return lockmode_compat(a->l_req_mode, b->l_req_mode);
374 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
376 ldlm_res2desc(lock->l_resource, &desc->l_resource);
377 desc->l_req_mode = lock->l_req_mode;
378 desc->l_granted_mode = lock->l_granted_mode;
379 memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
380 memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
383 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
384 struct ldlm_lock *new)
386 struct ldlm_ast_work *w;
389 l_lock(&lock->l_resource->lr_namespace->ns_lock);
390 if (new && (lock->l_flags & LDLM_FL_AST_SENT))
393 OBD_ALLOC(w, sizeof(*w));
400 lock->l_flags |= LDLM_FL_AST_SENT;
402 ldlm_lock2desc(new, &w->w_desc);
405 w->w_lock = LDLM_LOCK_GET(lock);
406 list_add(&w->w_list, lock->l_resource->lr_tmp);
408 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
412 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
414 struct ldlm_lock *lock;
416 lock = ldlm_handle2lock(lockh);
417 ldlm_lock_addref_internal(lock, mode);
421 /* only called for local locks */
422 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
424 l_lock(&lock->l_resource->lr_namespace->ns_lock);
425 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
429 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
431 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
434 /* Args: unlocked lock */
435 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
437 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
443 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
444 l_lock(&lock->l_resource->lr_namespace->ns_lock);
445 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
450 /* If we received a blocked AST and this was the last reference,
451 * run the callback. */
452 if (!lock->l_readers && !lock->l_writers &&
453 (lock->l_flags & LDLM_FL_CBPENDING)) {
454 if (!lock->l_resource->lr_namespace->ns_client &&
456 CERROR("FL_CBPENDING set on non-local lock--just a "
459 LDLM_DEBUG(lock, "final decref done on cbpending lock");
460 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
462 /* FIXME: need a real 'desc' here */
463 lock->l_blocking_ast(lock, NULL, lock->l_data,
464 lock->l_data_len, LDLM_CB_BLOCKING);
466 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
468 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
469 LDLM_LOCK_PUT(lock); /* matches the handle2lock above */
474 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
475 struct list_head *queue)
477 struct list_head *tmp, *pos;
480 list_for_each_safe(tmp, pos, queue) {
481 struct ldlm_lock *child;
482 ldlm_res_compat compat;
484 child = list_entry(tmp, struct ldlm_lock, l_res_link);
488 compat = ldlm_res_compat_table[child->l_resource->lr_type];
489 if (compat && compat(child, lock)) {
490 CDEBUG(D_OTHER, "compat function succeded, next.\n");
493 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
494 CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
500 if (send_cbs && child->l_blocking_ast != NULL) {
501 CDEBUG(D_OTHER, "lock %p incompatible; sending "
502 "blocking AST.\n", child);
503 ldlm_add_ast_work_item(child, lock);
510 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
515 l_lock(&lock->l_resource->lr_namespace->ns_lock);
516 rc = ldlm_lock_compat_list(lock, send_cbs,
517 &lock->l_resource->lr_granted);
518 /* FIXME: should we be sending ASTs to converting? */
520 rc = ldlm_lock_compat_list
521 (lock, send_cbs, &lock->l_resource->lr_converting);
523 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
528 - ldlm_handle_enqueuque - resource
530 void ldlm_grant_lock(struct ldlm_lock *lock)
532 struct ldlm_resource *res = lock->l_resource;
535 l_lock(&lock->l_resource->lr_namespace->ns_lock);
536 ldlm_resource_add_lock(res, &res->lr_granted, lock);
537 lock->l_granted_mode = lock->l_req_mode;
539 if (lock->l_granted_mode < res->lr_most_restr)
540 res->lr_most_restr = lock->l_granted_mode;
542 if (lock->l_completion_ast) {
543 ldlm_add_ast_work_item(lock, NULL);
545 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
549 /* returns a referenced lock or NULL */
550 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
551 struct ldlm_extent *extent)
553 struct ldlm_lock *lock;
554 struct list_head *tmp;
556 list_for_each(tmp, queue) {
557 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
559 if (lock->l_flags & (LDLM_FL_CBPENDING | LDLM_FL_DESTROYED))
562 /* lock_convert() takes the resource lock, so we're sure that
563 * req_mode, lr_type, and l_cookie won't change beneath us */
564 if (lock->l_req_mode != mode)
567 if (lock->l_resource->lr_type == LDLM_EXTENT &&
568 (lock->l_extent.start > extent->start ||
569 lock->l_extent.end < extent->end))
572 ldlm_lock_addref_internal(lock, mode);
579 /* Must be called with no resource or lock locks held.
581 * Returns 1 if it finds an already-existing lock that is compatible; in this
582 * case, lockh is filled in with a addref()ed lock
584 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 * res_id, __u32 type,
585 void *cookie, int cookielen, ldlm_mode_t mode,
586 struct lustre_handle *lockh)
588 struct ldlm_resource *res;
589 struct ldlm_lock *lock;
593 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
597 ns = res->lr_namespace;
598 l_lock(&ns->ns_lock);
600 if ((lock = search_queue(&res->lr_granted, mode, cookie)))
602 if ((lock = search_queue(&res->lr_converting, mode, cookie)))
604 if ((lock = search_queue(&res->lr_waiting, mode, cookie)))
609 ldlm_resource_put(res);
610 l_unlock(&ns->ns_lock);
613 ldlm_lock2handle(lock, lockh);
614 if (lock->l_completion_ast)
615 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
618 LDLM_DEBUG(lock, "matched");
620 LDLM_DEBUG_NOLOCK("not matched");
624 /* Returns a referenced lock */
625 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
626 struct lustre_handle *parent_lock_handle,
627 __u64 * res_id, __u32 type,
628 ldlm_mode_t mode, void *data, __u32 data_len)
630 struct ldlm_resource *res, *parent_res = NULL;
631 struct ldlm_lock *lock, *parent_lock;
633 parent_lock = ldlm_handle2lock(parent_lock_handle);
635 parent_res = parent_lock->l_resource;
637 res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
641 lock = ldlm_lock_new(parent_lock, res);
643 ldlm_resource_put(res);
647 lock->l_req_mode = mode;
649 lock->l_data_len = data_len;
654 /* Must be called with lock->l_lock and lock->l_resource->lr_lock not held */
655 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock * lock,
656 void *cookie, int cookie_len,
658 ldlm_completion_callback completion,
659 ldlm_blocking_callback blocking)
661 struct ldlm_resource *res;
663 ldlm_res_policy policy;
666 res = lock->l_resource;
667 lock->l_blocking_ast = blocking;
669 if (res->lr_type == LDLM_EXTENT)
670 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
672 /* policies are not executed on the client */
673 local = res->lr_namespace->ns_client;
674 if (!local && (policy = ldlm_res_policy_table[res->lr_type])) {
676 rc = policy(lock, cookie, lock->l_req_mode, NULL);
678 if (rc == ELDLM_LOCK_CHANGED) {
679 res = lock->l_resource;
680 *flags |= LDLM_FL_LOCK_CHANGED;
681 } else if (rc == ELDLM_LOCK_ABORTED) {
682 ldlm_lock_destroy(lock);
687 lock->l_cookie = cookie;
688 lock->l_cookie_len = cookie_len;
690 l_lock(&res->lr_namespace->ns_lock);
691 if (local && lock->l_req_mode == lock->l_granted_mode) {
692 /* The server returned a blocked lock, but it was granted before
693 * we got a chance to actually enqueue it. We don't need to do
695 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
696 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
700 /* This distinction between local lock trees is very important; a client
701 * namespace only has information about locks taken by that client, and
702 * thus doesn't have enough information to decide for itself if it can
703 * be granted (below). In this case, we do exactly what the server
704 * tells us to do, as dictated by the 'flags' */
705 ldlm_resource_unlink_lock(lock);
707 if (*flags & LDLM_FL_BLOCK_CONV)
708 ldlm_resource_add_lock(res, res->lr_converting.prev,
710 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
711 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
713 ldlm_grant_lock(lock);
717 /* FIXME: We may want to optimize by checking lr_most_restr */
718 if (!list_empty(&res->lr_converting)) {
719 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
720 *flags |= LDLM_FL_BLOCK_CONV;
723 if (!list_empty(&res->lr_waiting)) {
724 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
725 *flags |= LDLM_FL_BLOCK_WAIT;
728 if (!ldlm_lock_compat(lock, 0)) {
729 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
730 *flags |= LDLM_FL_BLOCK_GRANTED;
734 ldlm_grant_lock(lock);
737 l_unlock(&res->lr_namespace->ns_lock);
738 /* Don't set 'completion_ast' until here so that if the lock is granted
739 * immediately we don't do an unnecessary completion call. */
740 lock->l_completion_ast = completion;
744 /* Must be called with namespace taken: queue is waiting or converting. */
745 static int ldlm_reprocess_queue(struct ldlm_resource *res,
746 struct list_head *queue)
748 struct list_head *tmp, *pos;
751 list_for_each_safe(tmp, pos, queue) {
752 struct ldlm_lock *pending;
753 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
755 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
757 if (!ldlm_lock_compat(pending, 1))
760 list_del_init(&pending->l_res_link);
761 ldlm_grant_lock(pending);
767 void ldlm_run_ast_work(struct list_head *rpc_list)
769 struct list_head *tmp, *pos;
773 list_for_each_safe(tmp, pos, rpc_list) {
774 struct ldlm_ast_work *w =
775 list_entry(tmp, struct ldlm_ast_work, w_list);
778 rc = w->w_lock->l_blocking_ast
779 (w->w_lock, &w->w_desc, w->w_data,
780 w->w_datalen, LDLM_CB_BLOCKING);
782 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags);
784 CERROR("Failed AST - should clean & disconnect "
786 LDLM_LOCK_PUT(w->w_lock);
787 list_del(&w->w_list);
788 OBD_FREE(w, sizeof(*w));
793 /* Must be called with resource->lr_lock not taken. */
794 void ldlm_reprocess_all(struct ldlm_resource *res)
796 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
799 /* Local lock trees don't get reprocessed. */
800 if (res->lr_namespace->ns_client) {
805 l_lock(&res->lr_namespace->ns_lock);
806 res->lr_tmp = &rpc_list;
808 ldlm_reprocess_queue(res, &res->lr_converting);
809 if (list_empty(&res->lr_converting))
810 ldlm_reprocess_queue(res, &res->lr_waiting);
813 l_unlock(&res->lr_namespace->ns_lock);
815 ldlm_run_ast_work(&rpc_list);
819 void ldlm_cancel_callback(struct ldlm_lock *lock)
821 l_lock(&lock->l_resource->lr_namespace->ns_lock);
822 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
823 lock->l_flags |= LDLM_FL_CANCEL;
824 lock->l_blocking_ast(lock, NULL, lock->l_data,
825 lock->l_data_len, LDLM_CB_CANCELING);
827 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
830 void ldlm_lock_cancel(struct ldlm_lock *lock)
832 struct ldlm_resource *res;
833 struct ldlm_namespace *ns;
836 res = lock->l_resource;
837 ns = res->lr_namespace;
839 l_lock(&ns->ns_lock);
840 if (lock->l_readers || lock->l_writers)
841 CDEBUG(D_INFO, "lock still has references (%d readers, %d "
842 "writers)\n", lock->l_readers, lock->l_writers);
844 ldlm_cancel_callback(lock);
846 ldlm_del_waiting_lock(lock);
847 ldlm_resource_unlink_lock(lock);
848 ldlm_lock_destroy(lock);
849 l_unlock(&ns->ns_lock);
853 void ldlm_cancel_locks_for_export(struct obd_export *exp)
855 struct list_head *iter, *n; /* MUST BE CALLED "n"! */
857 list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
858 struct ldlm_lock *lock;
859 struct ldlm_resource *res;
860 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
861 res = ldlm_resource_getref(lock->l_resource);
862 LDLM_DEBUG(lock, "cancelling lock for export %p", exp);
863 ldlm_lock_cancel(lock);
864 ldlm_reprocess_all(res);
865 ldlm_resource_put(res);
869 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
872 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
873 struct ldlm_resource *res;
874 struct ldlm_namespace *ns;
878 res = lock->l_resource;
879 ns = res->lr_namespace;
881 l_lock(&ns->ns_lock);
883 lock->l_req_mode = new_mode;
884 ldlm_resource_unlink_lock(lock);
886 /* If this is a local resource, put it on the appropriate list. */
887 if (res->lr_namespace->ns_client) {
888 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED))
889 ldlm_resource_add_lock(res, res->lr_converting.prev,
892 /* This should never happen, because of the way the
893 * server handles conversions. */
896 res->lr_tmp = &rpc_list;
897 ldlm_grant_lock(lock);
900 /* FIXME: completion handling not with ns_lock held ! */
901 if (lock->l_completion_ast)
902 lock->l_completion_ast(lock, 0);
905 /* FIXME: We should try the conversion right away and possibly
906 * return success without the need for an extra AST */
907 ldlm_resource_add_lock(res, res->lr_converting.prev, lock);
908 *flags |= LDLM_FL_BLOCK_CONV;
911 l_unlock(&ns->ns_lock);
914 ldlm_run_ast_work(&rpc_list);
918 void ldlm_lock_dump(struct ldlm_lock *lock)
922 if (!(portal_debug & D_OTHER))
925 if (RES_VERSION_SIZE != 4)
929 CDEBUG(D_OTHER, " NULL LDLM lock\n");
933 snprintf(ver, sizeof(ver), "%x %x %x %x",
934 lock->l_version[0], lock->l_version[1],
935 lock->l_version[2], lock->l_version[3]);
937 CDEBUG(D_OTHER, " -- Lock dump: %p (%s)\n", lock, ver);
938 if (lock->l_export && lock->l_export->exp_connection)
939 CDEBUG(D_OTHER, " Node: NID %x (rhandle: "LPX64")\n",
940 lock->l_export->exp_connection->c_peer.peer_nid,
941 lock->l_remote_handle.addr);
943 CDEBUG(D_OTHER, " Node: local\n");
944 CDEBUG(D_OTHER, " Parent: %p\n", lock->l_parent);
945 CDEBUG(D_OTHER, " Resource: %p ("LPD64")\n", lock->l_resource,
946 lock->l_resource->lr_name[0]);
947 CDEBUG(D_OTHER, " Requested mode: %d, granted mode: %d\n",
948 (int)lock->l_req_mode, (int)lock->l_granted_mode);
949 CDEBUG(D_OTHER, " Readers: %u ; Writers; %u\n",
950 lock->l_readers, lock->l_writers);
951 if (lock->l_resource->lr_type == LDLM_EXTENT)
952 CDEBUG(D_OTHER, " Extent: %Lu -> %Lu\n",
953 (unsigned long long)lock->l_extent.start,
954 (unsigned long long)lock->l_extent.end);