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, 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, lr_type, and l_cookie 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 */
677 local = res->lr_namespace->ns_client;
678 if (!local && (policy = ldlm_res_policy_table[res->lr_type])) {
680 rc = policy(lock, cookie, lock->l_req_mode, NULL);
682 if (rc == ELDLM_LOCK_CHANGED) {
683 res = lock->l_resource;
684 *flags |= LDLM_FL_LOCK_CHANGED;
685 } else if (rc == ELDLM_LOCK_ABORTED) {
686 ldlm_lock_destroy(lock);
691 lock->l_cookie = cookie;
692 lock->l_cookie_len = cookie_len;
694 l_lock(&res->lr_namespace->ns_lock);
695 if (local && lock->l_req_mode == lock->l_granted_mode) {
696 /* The server returned a blocked lock, but it was granted before
697 * we got a chance to actually enqueue it. We don't need to do
699 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
700 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
704 /* This distinction between local lock trees is very important; a client
705 * namespace only has information about locks taken by that client, and
706 * thus doesn't have enough information to decide for itself if it can
707 * be granted (below). In this case, we do exactly what the server
708 * tells us to do, as dictated by the 'flags' */
709 ldlm_resource_unlink_lock(lock);
711 if (*flags & LDLM_FL_BLOCK_CONV)
712 ldlm_resource_add_lock(res, res->lr_converting.prev,
714 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
715 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
717 ldlm_grant_lock(lock);
721 /* FIXME: We may want to optimize by checking lr_most_restr */
722 if (!list_empty(&res->lr_converting)) {
723 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
724 *flags |= LDLM_FL_BLOCK_CONV;
727 if (!list_empty(&res->lr_waiting)) {
728 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
729 *flags |= LDLM_FL_BLOCK_WAIT;
732 if (!ldlm_lock_compat(lock, 0)) {
733 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
734 *flags |= LDLM_FL_BLOCK_GRANTED;
738 ldlm_grant_lock(lock);
741 l_unlock(&res->lr_namespace->ns_lock);
742 /* Don't set 'completion_ast' until here so that if the lock is granted
743 * immediately we don't do an unnecessary completion call. */
744 lock->l_completion_ast = completion;
748 /* Must be called with namespace taken: queue is waiting or converting. */
749 static int ldlm_reprocess_queue(struct ldlm_resource *res,
750 struct list_head *queue)
752 struct list_head *tmp, *pos;
755 list_for_each_safe(tmp, pos, queue) {
756 struct ldlm_lock *pending;
757 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
759 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
761 if (!ldlm_lock_compat(pending, 1))
764 list_del_init(&pending->l_res_link);
765 ldlm_grant_lock(pending);
771 void ldlm_run_ast_work(struct list_head *rpc_list)
773 struct list_head *tmp, *pos;
777 list_for_each_safe(tmp, pos, rpc_list) {
778 struct ldlm_ast_work *w =
779 list_entry(tmp, struct ldlm_ast_work, w_list);
782 rc = w->w_lock->l_blocking_ast
783 (w->w_lock, &w->w_desc, w->w_data,
784 w->w_datalen, LDLM_CB_BLOCKING);
786 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags);
788 CERROR("Failed AST - should clean & disconnect "
790 LDLM_LOCK_PUT(w->w_lock);
791 list_del(&w->w_list);
792 OBD_FREE(w, sizeof(*w));
797 /* Must be called with resource->lr_lock not taken. */
798 void ldlm_reprocess_all(struct ldlm_resource *res)
800 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
803 /* Local lock trees don't get reprocessed. */
804 if (res->lr_namespace->ns_client) {
809 l_lock(&res->lr_namespace->ns_lock);
810 res->lr_tmp = &rpc_list;
812 ldlm_reprocess_queue(res, &res->lr_converting);
813 if (list_empty(&res->lr_converting))
814 ldlm_reprocess_queue(res, &res->lr_waiting);
817 l_unlock(&res->lr_namespace->ns_lock);
819 ldlm_run_ast_work(&rpc_list);
823 void ldlm_cancel_callback(struct ldlm_lock *lock)
825 l_lock(&lock->l_resource->lr_namespace->ns_lock);
826 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
827 lock->l_flags |= LDLM_FL_CANCEL;
828 lock->l_blocking_ast(lock, NULL, lock->l_data,
829 lock->l_data_len, LDLM_CB_CANCELING);
831 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
834 void ldlm_lock_cancel(struct ldlm_lock *lock)
836 struct ldlm_resource *res;
837 struct ldlm_namespace *ns;
840 res = lock->l_resource;
841 ns = res->lr_namespace;
843 l_lock(&ns->ns_lock);
844 if (lock->l_readers || lock->l_writers)
845 CDEBUG(D_INFO, "lock still has references (%d readers, %d "
846 "writers)\n", lock->l_readers, lock->l_writers);
848 ldlm_cancel_callback(lock);
850 ldlm_del_waiting_lock(lock);
851 ldlm_resource_unlink_lock(lock);
852 ldlm_lock_destroy(lock);
853 l_unlock(&ns->ns_lock);
857 void ldlm_cancel_locks_for_export(struct obd_export *exp)
859 struct list_head *iter, *n; /* MUST BE CALLED "n"! */
861 list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
862 struct ldlm_lock *lock;
863 struct ldlm_resource *res;
864 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
865 res = ldlm_resource_getref(lock->l_resource);
866 LDLM_DEBUG(lock, "export %p", exp);
867 ldlm_lock_cancel(lock);
868 ldlm_reprocess_all(res);
869 ldlm_resource_put(res);
873 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
876 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
877 struct ldlm_resource *res;
878 struct ldlm_namespace *ns;
882 res = lock->l_resource;
883 ns = res->lr_namespace;
885 l_lock(&ns->ns_lock);
887 lock->l_req_mode = new_mode;
888 ldlm_resource_unlink_lock(lock);
890 /* If this is a local resource, put it on the appropriate list. */
891 if (res->lr_namespace->ns_client) {
892 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED))
893 ldlm_resource_add_lock(res, res->lr_converting.prev,
896 /* This should never happen, because of the way the
897 * server handles conversions. */
900 res->lr_tmp = &rpc_list;
901 ldlm_grant_lock(lock);
904 /* FIXME: completion handling not with ns_lock held ! */
905 if (lock->l_completion_ast)
906 lock->l_completion_ast(lock, 0);
909 /* FIXME: We should try the conversion right away and possibly
910 * return success without the need for an extra AST */
911 ldlm_resource_add_lock(res, res->lr_converting.prev, lock);
912 *flags |= LDLM_FL_BLOCK_CONV;
915 l_unlock(&ns->ns_lock);
918 ldlm_run_ast_work(&rpc_list);
922 void ldlm_lock_dump(struct ldlm_lock *lock)
926 if (!(portal_debug & D_OTHER))
929 if (RES_VERSION_SIZE != 4)
933 CDEBUG(D_OTHER, " NULL LDLM lock\n");
937 snprintf(ver, sizeof(ver), "%x %x %x %x",
938 lock->l_version[0], lock->l_version[1],
939 lock->l_version[2], lock->l_version[3]);
941 CDEBUG(D_OTHER, " -- Lock dump: %p (%s)\n", lock, ver);
942 if (lock->l_export && lock->l_export->exp_connection)
943 CDEBUG(D_OTHER, " Node: NID %x (rhandle: "LPX64")\n",
944 lock->l_export->exp_connection->c_peer.peer_nid,
945 lock->l_remote_handle.addr);
947 CDEBUG(D_OTHER, " Node: local\n");
948 CDEBUG(D_OTHER, " Parent: %p\n", lock->l_parent);
949 CDEBUG(D_OTHER, " Resource: %p ("LPD64")\n", lock->l_resource,
950 lock->l_resource->lr_name[0]);
951 CDEBUG(D_OTHER, " Requested mode: %d, granted mode: %d\n",
952 (int)lock->l_req_mode, (int)lock->l_granted_mode);
953 CDEBUG(D_OTHER, " Readers: %u ; Writers; %u\n",
954 lock->l_readers, lock->l_writers);
955 if (lock->l_resource->lr_type == LDLM_EXTENT)
956 CDEBUG(D_OTHER, " Extent: %Lu -> %Lu\n",
957 (unsigned long long)lock->l_extent.start,
958 (unsigned long long)lock->l_extent.end);