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 = DEAD_HANDLE_MAGIC;
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)
325 //spin_lock(&ldlm_handle_lock);
326 lock = (struct ldlm_lock *)(unsigned long)(handle->addr);
327 if (!kmem_cache_validate(ldlm_lock_slab, (void *)lock)) {
328 CERROR("bogus lock %p\n", lock);
332 if (lock->l_random != handle->cookie) {
333 CERROR("bogus cookie: lock %p has "LPX64" vs. handle "LPX64"\n",
334 lock, lock->l_random, handle->cookie);
337 if (!lock->l_resource) {
338 CERROR("trying to lock bogus resource: lock %p\n", lock);
339 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
342 if (!lock->l_resource->lr_namespace) {
343 CERROR("trying to lock bogus namespace: lock %p\n", lock);
344 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
348 l_lock(&lock->l_resource->lr_namespace->ns_lock);
349 if (lock->l_flags & LDLM_FL_DESTROYED) {
350 CERROR("lock already destroyed: lock %p\n", lock);
351 LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
355 retval = LDLM_LOCK_GET(lock);
357 CERROR("lock disappeared below us!!! %p\n", lock);
360 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
362 //spin_unlock(&ldlm_handle_lock);
366 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
368 return lockmode_compat(a->l_req_mode, b->l_req_mode);
371 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
373 ldlm_res2desc(lock->l_resource, &desc->l_resource);
374 desc->l_req_mode = lock->l_req_mode;
375 desc->l_granted_mode = lock->l_granted_mode;
376 memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
377 memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
380 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
381 struct ldlm_lock *new)
383 struct ldlm_ast_work *w;
386 l_lock(&lock->l_resource->lr_namespace->ns_lock);
387 if (new && (lock->l_flags & LDLM_FL_AST_SENT))
390 OBD_ALLOC(w, sizeof(*w));
397 lock->l_flags |= LDLM_FL_AST_SENT;
399 ldlm_lock2desc(new, &w->w_desc);
402 w->w_lock = LDLM_LOCK_GET(lock);
403 list_add(&w->w_list, lock->l_resource->lr_tmp);
405 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
409 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
411 struct ldlm_lock *lock;
413 lock = ldlm_handle2lock(lockh);
414 ldlm_lock_addref_internal(lock, mode);
418 /* only called for local locks */
419 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
421 l_lock(&lock->l_resource->lr_namespace->ns_lock);
422 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
426 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
428 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
431 /* Args: unlocked lock */
432 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
434 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
440 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
441 l_lock(&lock->l_resource->lr_namespace->ns_lock);
442 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
447 /* If we received a blocked AST and this was the last reference,
448 * run the callback. */
449 if (!lock->l_readers && !lock->l_writers &&
450 (lock->l_flags & LDLM_FL_CBPENDING)) {
451 if (!lock->l_resource->lr_namespace->ns_client &&
453 CERROR("FL_CBPENDING set on non-local lock--just a "
456 LDLM_DEBUG(lock, "final decref done on cbpending lock");
457 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
459 /* FIXME: need a real 'desc' here */
460 lock->l_blocking_ast(lock, NULL, lock->l_data,
461 lock->l_data_len, LDLM_CB_BLOCKING);
463 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
465 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
466 LDLM_LOCK_PUT(lock); /* matches the handle2lock above */
471 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
472 struct list_head *queue)
474 struct list_head *tmp, *pos;
477 list_for_each_safe(tmp, pos, queue) {
478 struct ldlm_lock *child;
479 ldlm_res_compat compat;
481 child = list_entry(tmp, struct ldlm_lock, l_res_link);
485 compat = ldlm_res_compat_table[child->l_resource->lr_type];
486 if (compat && compat(child, lock)) {
487 CDEBUG(D_OTHER, "compat function succeded, next.\n");
490 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
491 CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
497 if (send_cbs && child->l_blocking_ast != NULL) {
498 CDEBUG(D_OTHER, "lock %p incompatible; sending "
499 "blocking AST.\n", child);
500 ldlm_add_ast_work_item(child, lock);
507 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
512 l_lock(&lock->l_resource->lr_namespace->ns_lock);
513 rc = ldlm_lock_compat_list(lock, send_cbs,
514 &lock->l_resource->lr_granted);
515 /* FIXME: should we be sending ASTs to converting? */
517 rc = ldlm_lock_compat_list
518 (lock, send_cbs, &lock->l_resource->lr_converting);
520 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
525 - ldlm_handle_enqueuque - resource
527 void ldlm_grant_lock(struct ldlm_lock *lock)
529 struct ldlm_resource *res = lock->l_resource;
532 l_lock(&lock->l_resource->lr_namespace->ns_lock);
533 ldlm_resource_add_lock(res, &res->lr_granted, lock);
534 lock->l_granted_mode = lock->l_req_mode;
536 if (lock->l_granted_mode < res->lr_most_restr)
537 res->lr_most_restr = lock->l_granted_mode;
539 if (lock->l_completion_ast) {
540 ldlm_add_ast_work_item(lock, NULL);
542 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
546 /* returns a referenced lock or NULL */
547 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
548 struct ldlm_extent *extent)
550 struct ldlm_lock *lock;
551 struct list_head *tmp;
553 list_for_each(tmp, queue) {
554 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
556 if (lock->l_flags & (LDLM_FL_CBPENDING | LDLM_FL_DESTROYED))
559 /* lock_convert() takes the resource lock, so we're sure that
560 * req_mode, lr_type, and l_cookie won't change beneath us */
561 if (lock->l_req_mode != mode)
564 if (lock->l_resource->lr_type == LDLM_EXTENT &&
565 (lock->l_extent.start > extent->start ||
566 lock->l_extent.end < extent->end))
569 ldlm_lock_addref_internal(lock, mode);
576 /* Must be called with no resource or lock locks held.
578 * Returns 1 if it finds an already-existing lock that is compatible; in this
579 * case, lockh is filled in with a addref()ed lock
581 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 * res_id, __u32 type,
582 void *cookie, int cookielen, ldlm_mode_t mode,
583 struct lustre_handle *lockh)
585 struct ldlm_resource *res;
586 struct ldlm_lock *lock;
590 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
594 ns = res->lr_namespace;
595 l_lock(&ns->ns_lock);
597 if ((lock = search_queue(&res->lr_granted, mode, cookie)))
599 if ((lock = search_queue(&res->lr_converting, mode, cookie)))
601 if ((lock = search_queue(&res->lr_waiting, mode, cookie)))
606 ldlm_resource_put(res);
607 l_unlock(&ns->ns_lock);
610 ldlm_lock2handle(lock, lockh);
611 if (lock->l_completion_ast)
612 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
615 LDLM_DEBUG(lock, "matched");
617 LDLM_DEBUG_NOLOCK("not matched");
621 /* Returns a referenced lock */
622 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
623 struct lustre_handle *parent_lock_handle,
624 __u64 * res_id, __u32 type,
625 ldlm_mode_t mode, void *data, __u32 data_len)
627 struct ldlm_resource *res, *parent_res = NULL;
628 struct ldlm_lock *lock, *parent_lock;
630 parent_lock = ldlm_handle2lock(parent_lock_handle);
632 parent_res = parent_lock->l_resource;
634 res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
638 lock = ldlm_lock_new(parent_lock, res);
640 ldlm_resource_put(res);
644 lock->l_req_mode = mode;
646 lock->l_data_len = data_len;
651 /* Must be called with lock->l_lock and lock->l_resource->lr_lock not held */
652 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock * lock,
653 void *cookie, int cookie_len,
655 ldlm_completion_callback completion,
656 ldlm_blocking_callback blocking)
658 struct ldlm_resource *res;
660 ldlm_res_policy policy;
663 res = lock->l_resource;
664 lock->l_blocking_ast = blocking;
666 if (res->lr_type == LDLM_EXTENT)
667 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
669 /* policies are not executed on the client */
670 local = res->lr_namespace->ns_client;
671 if (!local && (policy = ldlm_res_policy_table[res->lr_type])) {
673 rc = policy(lock, cookie, lock->l_req_mode, NULL);
675 if (rc == ELDLM_LOCK_CHANGED) {
676 res = lock->l_resource;
677 *flags |= LDLM_FL_LOCK_CHANGED;
678 } else if (rc == ELDLM_LOCK_ABORTED) {
679 ldlm_lock_destroy(lock);
684 lock->l_cookie = cookie;
685 lock->l_cookie_len = cookie_len;
687 l_lock(&res->lr_namespace->ns_lock);
688 if (local && lock->l_req_mode == lock->l_granted_mode) {
689 /* The server returned a blocked lock, but it was granted before
690 * we got a chance to actually enqueue it. We don't need to do
692 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
693 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
697 /* This distinction between local lock trees is very important; a client
698 * namespace only has information about locks taken by that client, and
699 * thus doesn't have enough information to decide for itself if it can
700 * be granted (below). In this case, we do exactly what the server
701 * tells us to do, as dictated by the 'flags' */
702 ldlm_resource_unlink_lock(lock);
704 if (*flags & LDLM_FL_BLOCK_CONV)
705 ldlm_resource_add_lock(res, res->lr_converting.prev,
707 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
708 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
710 ldlm_grant_lock(lock);
714 /* FIXME: We may want to optimize by checking lr_most_restr */
715 if (!list_empty(&res->lr_converting)) {
716 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
717 *flags |= LDLM_FL_BLOCK_CONV;
720 if (!list_empty(&res->lr_waiting)) {
721 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
722 *flags |= LDLM_FL_BLOCK_WAIT;
725 if (!ldlm_lock_compat(lock, 0)) {
726 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
727 *flags |= LDLM_FL_BLOCK_GRANTED;
731 ldlm_grant_lock(lock);
734 l_unlock(&res->lr_namespace->ns_lock);
735 /* Don't set 'completion_ast' until here so that if the lock is granted
736 * immediately we don't do an unnecessary completion call. */
737 lock->l_completion_ast = completion;
741 /* Must be called with namespace taken: queue is waiting or converting. */
742 static int ldlm_reprocess_queue(struct ldlm_resource *res,
743 struct list_head *queue)
745 struct list_head *tmp, *pos;
748 list_for_each_safe(tmp, pos, queue) {
749 struct ldlm_lock *pending;
750 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
752 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
754 if (!ldlm_lock_compat(pending, 1))
757 list_del_init(&pending->l_res_link);
758 ldlm_grant_lock(pending);
764 void ldlm_run_ast_work(struct list_head *rpc_list)
766 struct list_head *tmp, *pos;
770 list_for_each_safe(tmp, pos, rpc_list) {
771 struct ldlm_ast_work *w =
772 list_entry(tmp, struct ldlm_ast_work, w_list);
775 rc = w->w_lock->l_blocking_ast
776 (w->w_lock, &w->w_desc, w->w_data,
777 w->w_datalen, LDLM_CB_BLOCKING);
779 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags);
781 CERROR("Failed AST - should clean & disconnect "
783 LDLM_LOCK_PUT(w->w_lock);
784 list_del(&w->w_list);
785 OBD_FREE(w, sizeof(*w));
790 /* Must be called with resource->lr_lock not taken. */
791 void ldlm_reprocess_all(struct ldlm_resource *res)
793 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
796 /* Local lock trees don't get reprocessed. */
797 if (res->lr_namespace->ns_client) {
802 l_lock(&res->lr_namespace->ns_lock);
803 res->lr_tmp = &rpc_list;
805 ldlm_reprocess_queue(res, &res->lr_converting);
806 if (list_empty(&res->lr_converting))
807 ldlm_reprocess_queue(res, &res->lr_waiting);
810 l_unlock(&res->lr_namespace->ns_lock);
812 ldlm_run_ast_work(&rpc_list);
816 void ldlm_cancel_callback(struct ldlm_lock *lock)
818 l_lock(&lock->l_resource->lr_namespace->ns_lock);
819 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
820 lock->l_flags |= LDLM_FL_CANCEL;
821 lock->l_blocking_ast(lock, NULL, lock->l_data,
822 lock->l_data_len, LDLM_CB_CANCELING);
824 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
827 void ldlm_lock_cancel(struct ldlm_lock *lock)
829 struct ldlm_resource *res;
830 struct ldlm_namespace *ns;
833 res = lock->l_resource;
834 ns = res->lr_namespace;
836 l_lock(&ns->ns_lock);
837 if (lock->l_readers || lock->l_writers)
838 CDEBUG(D_INFO, "lock still has references (%d readers, %d "
839 "writers)\n", lock->l_readers, lock->l_writers);
841 ldlm_cancel_callback(lock);
843 ldlm_del_waiting_lock(lock);
844 ldlm_resource_unlink_lock(lock);
845 ldlm_lock_destroy(lock);
846 l_unlock(&ns->ns_lock);
850 void ldlm_cancel_locks_for_export(struct obd_export *exp)
852 struct list_head *iter, *n; /* MUST BE CALLED "n"! */
854 list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
855 struct ldlm_lock *lock;
856 struct ldlm_resource *res;
857 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
858 res = ldlm_resource_getref(lock->l_resource);
859 LDLM_DEBUG(lock, "cancelling lock for export %p", exp);
860 ldlm_lock_cancel(lock);
861 ldlm_reprocess_all(res);
862 ldlm_resource_put(res);
866 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
869 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
870 struct ldlm_resource *res;
871 struct ldlm_namespace *ns;
875 res = lock->l_resource;
876 ns = res->lr_namespace;
878 l_lock(&ns->ns_lock);
880 lock->l_req_mode = new_mode;
881 ldlm_resource_unlink_lock(lock);
883 /* If this is a local resource, put it on the appropriate list. */
884 if (res->lr_namespace->ns_client) {
885 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED))
886 ldlm_resource_add_lock(res, res->lr_converting.prev,
889 /* This should never happen, because of the way the
890 * server handles conversions. */
893 res->lr_tmp = &rpc_list;
894 ldlm_grant_lock(lock);
897 /* FIXME: completion handling not with ns_lock held ! */
898 if (lock->l_completion_ast)
899 lock->l_completion_ast(lock, 0);
902 /* FIXME: We should try the conversion right away and possibly
903 * return success without the need for an extra AST */
904 ldlm_resource_add_lock(res, res->lr_converting.prev, lock);
905 *flags |= LDLM_FL_BLOCK_CONV;
908 l_unlock(&ns->ns_lock);
911 ldlm_run_ast_work(&rpc_list);
915 void ldlm_lock_dump(struct ldlm_lock *lock)
919 if (!(portal_debug & D_OTHER))
922 if (RES_VERSION_SIZE != 4)
926 CDEBUG(D_OTHER, " NULL LDLM lock\n");
930 snprintf(ver, sizeof(ver), "%x %x %x %x",
931 lock->l_version[0], lock->l_version[1],
932 lock->l_version[2], lock->l_version[3]);
934 CDEBUG(D_OTHER, " -- Lock dump: %p (%s)\n", lock, ver);
935 if (lock->l_export && lock->l_export->exp_connection)
936 CDEBUG(D_OTHER, " Node: NID %x (rhandle: "LPX64")\n",
937 lock->l_export->exp_connection->c_peer.peer_nid,
938 lock->l_remote_handle.addr);
940 CDEBUG(D_OTHER, " Node: local\n");
941 CDEBUG(D_OTHER, " Parent: %p\n", lock->l_parent);
942 CDEBUG(D_OTHER, " Resource: %p ("LPD64")\n", lock->l_resource,
943 lock->l_resource->lr_name[0]);
944 CDEBUG(D_OTHER, " Requested mode: %d, granted mode: %d\n",
945 (int)lock->l_req_mode, (int)lock->l_granted_mode);
946 CDEBUG(D_OTHER, " Readers: %u ; Writers; %u\n",
947 lock->l_readers, lock->l_writers);
948 if (lock->l_resource->lr_type == LDLM_EXTENT)
949 CDEBUG(D_OTHER, " Extent: %Lu -> %Lu\n",
950 (unsigned long long)lock->l_extent.start,
951 (unsigned long long)lock->l_extent.end);