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[] = {
50 char *ldlm_it2str(int it)
57 case (IT_OPEN | IT_CREAT):
88 CERROR("Unknown intent %d\n", it);
93 extern kmem_cache_t *ldlm_lock_slab;
94 struct lustre_lock ldlm_handle_lock;
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,
103 static ldlm_res_policy ldlm_intent_policy_func;
105 static int ldlm_plain_policy(struct ldlm_lock *lock, void *req_cookie,
106 ldlm_mode_t mode, int flags, void *data)
108 if ((flags & LDLM_FL_HAS_INTENT) && ldlm_intent_policy_func) {
109 return ldlm_intent_policy_func(lock, req_cookie, mode, flags,
116 ldlm_res_policy ldlm_res_policy_table[] = {
117 [LDLM_PLAIN] ldlm_plain_policy,
118 [LDLM_EXTENT] ldlm_extent_policy,
121 void ldlm_register_intent(ldlm_res_policy arg)
123 ldlm_intent_policy_func = arg;
126 void ldlm_unregister_intent(void)
128 ldlm_intent_policy_func = NULL;
132 * REFCOUNTED LOCK OBJECTS
137 * Lock refcounts, during creation:
138 * - one special one for allocation, dec'd only once in destroy
139 * - one for being a lock that's in-use
140 * - one for the addref associated with a new lock
142 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
144 atomic_inc(&lock->l_refc);
148 void ldlm_lock_put(struct ldlm_lock *lock)
150 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
153 if (atomic_dec_and_test(&lock->l_refc)) {
154 l_lock(&ns->ns_lock);
155 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
156 LASSERT(lock->l_destroyed);
157 LASSERT(list_empty(&lock->l_res_link));
159 spin_lock(&ns->ns_counter_lock);
161 spin_unlock(&ns->ns_counter_lock);
163 ldlm_resource_putref(lock->l_resource);
164 lock->l_resource = NULL;
167 LDLM_LOCK_PUT(lock->l_parent);
169 PORTAL_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
170 l_unlock(&ns->ns_lock);
176 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
179 l_lock(&lock->l_resource->lr_namespace->ns_lock);
180 if (!list_empty(&lock->l_lru)) {
181 list_del_init(&lock->l_lru);
182 lock->l_resource->lr_namespace->ns_nr_unused--;
183 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
185 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
189 void ldlm_lock_destroy(struct ldlm_lock *lock)
192 l_lock(&lock->l_resource->lr_namespace->ns_lock);
194 if (!list_empty(&lock->l_children)) {
195 LDLM_DEBUG(lock, "still has children (%p)!",
196 lock->l_children.next);
197 ldlm_lock_dump(lock);
200 if (lock->l_readers || lock->l_writers) {
201 LDLM_DEBUG(lock, "lock still has references");
202 ldlm_lock_dump(lock);
205 if (!list_empty(&lock->l_res_link)) {
206 ldlm_lock_dump(lock);
210 if (lock->l_destroyed) {
211 LASSERT(list_empty(&lock->l_lru));
212 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
216 lock->l_destroyed = 1;
218 list_del_init(&lock->l_export_chain);
219 ldlm_lock_remove_from_lru(lock);
222 /* Wake anyone waiting for this lock */
223 /* FIXME: I should probably add yet another flag, instead of using
224 * l_export to only call this on clients */
225 lock->l_export = NULL;
226 if (lock->l_export && lock->l_completion_ast)
227 lock->l_completion_ast(lock, 0);
230 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
235 /* this is called by portals_handle2object with the handle lock taken */
236 static void lock_handle_addref(void *lock)
242 * usage: pass in a resource on which you have done ldlm_resource_get
243 * pass in a parent lock on which you have done a ldlm_lock_get
244 * after return, ldlm_*_put the resource and parent
245 * returns: lock with refcount 1
247 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
248 struct ldlm_resource *resource)
250 struct ldlm_lock *lock;
253 if (resource == NULL)
256 PORTAL_SLAB_ALLOC(lock, ldlm_lock_slab, sizeof(*lock));
260 get_random_bytes(&lock->l_random, sizeof(__u64));
261 lock->l_resource = ldlm_resource_getref(resource);
263 atomic_set(&lock->l_refc, 2);
264 INIT_LIST_HEAD(&lock->l_children);
265 INIT_LIST_HEAD(&lock->l_res_link);
266 INIT_LIST_HEAD(&lock->l_lru);
267 INIT_LIST_HEAD(&lock->l_export_chain);
268 INIT_LIST_HEAD(&lock->l_pending_chain);
269 init_waitqueue_head(&lock->l_waitq);
271 spin_lock(&resource->lr_namespace->ns_counter_lock);
272 resource->lr_namespace->ns_locks++;
273 spin_unlock(&resource->lr_namespace->ns_counter_lock);
275 if (parent != NULL) {
276 l_lock(&parent->l_resource->lr_namespace->ns_lock);
277 lock->l_parent = LDLM_LOCK_GET(parent);
278 list_add(&lock->l_childof, &parent->l_children);
279 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
285 int ldlm_lock_change_resource(struct ldlm_lock *lock, __u64 new_resid[3])
287 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
288 struct ldlm_resource *oldres = lock->l_resource;
291 l_lock(&ns->ns_lock);
292 if (memcmp(new_resid, lock->l_resource->lr_name,
293 sizeof(lock->l_resource->lr_name)) == 0) {
295 l_unlock(&ns->ns_lock);
299 LASSERT(new_resid[0] != 0);
301 /* This function assumes that the lock isn't on any lists */
302 LASSERT(list_empty(&lock->l_res_link));
304 lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
305 lock->l_resource->lr_type, 1);
306 if (lock->l_resource == NULL) {
311 /* ...and the flowers are still standing! */
312 ldlm_resource_putref(oldres);
314 l_unlock(&ns->ns_lock);
322 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
324 lockh->addr = (__u64) (unsigned long)lock;
325 lockh->cookie = lock->l_random;
329 * if flags: atomically get the lock and set the flags.
330 * Return NULL if flag already set
333 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int strict,
336 struct ldlm_lock *lock = NULL, *retval = NULL;
344 lock = (struct ldlm_lock *)(unsigned long)(handle->addr);
345 if (!kmem_cache_validate(ldlm_lock_slab, (void *)lock)) {
346 //CERROR("bogus lock %p\n", lock);
350 if (lock->l_random != handle->cookie) {
351 //CERROR("bogus cookie: lock %p has "LPX64" vs. handle "LPX64
352 // "\n", lock, lock->l_random, handle->cookie);
355 if (!lock->l_resource) {
356 CERROR("trying to lock bogus resource: lock %p\n", lock);
357 //LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
360 if (!lock->l_resource->lr_namespace) {
361 CERROR("trying to lock bogus namespace: lock %p\n", lock);
362 //LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
366 l_lock(&lock->l_resource->lr_namespace->ns_lock);
367 if (strict && lock->l_destroyed) {
368 CERROR("lock already destroyed: lock %p\n", lock);
369 //LDLM_DEBUG(lock, "ldlm_handle2lock(%p)", lock);
373 if (flags && (lock->l_flags & flags))
377 lock->l_flags |= flags;
379 retval = LDLM_LOCK_GET(lock);
382 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
387 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
389 return lockmode_compat(a->l_req_mode, b->l_req_mode);
392 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
394 ldlm_res2desc(lock->l_resource, &desc->l_resource);
395 desc->l_req_mode = lock->l_req_mode;
396 desc->l_granted_mode = lock->l_granted_mode;
397 memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
398 memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
401 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
402 struct ldlm_lock *new)
404 struct ldlm_ast_work *w;
407 l_lock(&lock->l_resource->lr_namespace->ns_lock);
408 if (new && (lock->l_flags & LDLM_FL_AST_SENT))
411 OBD_ALLOC(w, sizeof(*w));
418 lock->l_flags |= LDLM_FL_AST_SENT;
420 ldlm_lock2desc(new, &w->w_desc);
423 w->w_lock = LDLM_LOCK_GET(lock);
424 list_add(&w->w_list, lock->l_resource->lr_tmp);
426 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
430 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
432 struct ldlm_lock *lock;
434 lock = ldlm_handle2lock(lockh);
435 ldlm_lock_addref_internal(lock, mode);
439 /* only called for local locks */
440 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
442 l_lock(&lock->l_resource->lr_namespace->ns_lock);
443 ldlm_lock_remove_from_lru(lock);
444 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
448 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
450 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
453 /* Args: unlocked lock */
454 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
455 __u64 *res_id, int flags);
457 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
459 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0, 0);
460 struct ldlm_namespace *ns;
466 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
467 ns = lock->l_resource->lr_namespace;
468 l_lock(&lock->l_resource->lr_namespace->ns_lock);
469 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
474 /* If we received a blocked AST and this was the last reference,
475 * run the callback. */
476 if (!lock->l_readers && !lock->l_writers &&
477 (lock->l_flags & LDLM_FL_CBPENDING)) {
478 if (!lock->l_resource->lr_namespace->ns_client &&
480 CERROR("FL_CBPENDING set on non-local lock--just a "
483 LDLM_DEBUG(lock, "final decref done on cbpending lock");
484 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
486 /* FIXME: need a real 'desc' here */
487 lock->l_blocking_ast(lock, NULL, lock->l_data,
488 lock->l_data_len, LDLM_CB_BLOCKING);
489 } else if (ns->ns_client && !lock->l_readers && !lock->l_writers) {
490 LASSERT(list_empty(&lock->l_lru));
491 LASSERT(ns->ns_nr_unused >= 0);
492 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
494 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
497 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
499 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
500 LDLM_LOCK_PUT(lock); /* matches the handle2lock above */
505 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
506 struct list_head *queue)
508 struct list_head *tmp, *pos;
511 list_for_each_safe(tmp, pos, queue) {
512 struct ldlm_lock *child;
513 ldlm_res_compat compat;
515 child = list_entry(tmp, struct ldlm_lock, l_res_link);
519 compat = ldlm_res_compat_table[child->l_resource->lr_type];
520 if (compat && compat(child, lock)) {
521 CDEBUG(D_OTHER, "compat function succeded, next.\n");
524 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
525 CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
531 if (send_cbs && child->l_blocking_ast != NULL) {
532 CDEBUG(D_OTHER, "lock %p incompatible; sending "
533 "blocking AST.\n", child);
534 ldlm_add_ast_work_item(child, lock);
541 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
546 l_lock(&lock->l_resource->lr_namespace->ns_lock);
547 rc = ldlm_lock_compat_list(lock, send_cbs,
548 &lock->l_resource->lr_granted);
549 /* FIXME: should we be sending ASTs to converting? */
551 rc = ldlm_lock_compat_list
552 (lock, send_cbs, &lock->l_resource->lr_converting);
554 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
559 - ldlm_handle_enqueuque - resource
561 void ldlm_grant_lock(struct ldlm_lock *lock)
563 struct ldlm_resource *res = lock->l_resource;
566 l_lock(&lock->l_resource->lr_namespace->ns_lock);
567 ldlm_resource_add_lock(res, &res->lr_granted, lock);
568 lock->l_granted_mode = lock->l_req_mode;
570 if (lock->l_granted_mode < res->lr_most_restr)
571 res->lr_most_restr = lock->l_granted_mode;
573 if (lock->l_completion_ast) {
574 ldlm_add_ast_work_item(lock, NULL);
576 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
580 /* returns a referenced lock or NULL */
581 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
582 struct ldlm_extent *extent,
583 struct ldlm_lock *old_lock)
585 struct ldlm_lock *lock;
586 struct list_head *tmp;
588 list_for_each(tmp, queue) {
589 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
591 if (lock == old_lock)
594 if (lock->l_flags & LDLM_FL_CBPENDING)
597 if (lock->l_req_mode != mode)
600 if (lock->l_resource->lr_type == LDLM_EXTENT &&
601 (lock->l_extent.start > extent->start ||
602 lock->l_extent.end < extent->end))
605 if (lock->l_destroyed)
608 ldlm_lock_addref_internal(lock, mode);
615 /* Can be called in two ways:
617 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
618 * for a duplicate of.
620 * Otherwise, all of the fields must be filled in, to match against.
622 * Returns 1 if it finds an already-existing lock that is compatible; in this
623 * case, lockh is filled in with a addref()ed lock
625 int ldlm_lock_match(struct ldlm_namespace *ns, __u64 *res_id, __u32 type,
626 void *cookie, int cookielen, ldlm_mode_t mode,
627 struct lustre_handle *lockh)
629 struct ldlm_resource *res;
630 struct ldlm_lock *lock, *old_lock = NULL;
635 old_lock = ldlm_handle2lock(lockh);
638 ns = old_lock->l_resource->lr_namespace;
639 res_id = old_lock->l_resource->lr_name;
640 type = old_lock->l_resource->lr_type;
641 mode = old_lock->l_req_mode;
644 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
646 LASSERT(old_lock == NULL);
650 l_lock(&ns->ns_lock);
652 if ((lock = search_queue(&res->lr_granted, mode, cookie, old_lock)))
654 if ((lock = search_queue(&res->lr_converting, mode, cookie, old_lock)))
656 if ((lock = search_queue(&res->lr_waiting, mode, cookie, old_lock)))
661 ldlm_resource_putref(res);
662 l_unlock(&ns->ns_lock);
665 ldlm_lock2handle(lock, lockh);
666 if (lock->l_completion_ast)
667 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC);
670 LDLM_DEBUG(lock, "matched");
672 LDLM_DEBUG_NOLOCK("not matched");
675 LDLM_LOCK_PUT(old_lock);
680 /* Returns a referenced lock */
681 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
682 struct lustre_handle *parent_lock_handle,
683 __u64 * res_id, __u32 type,
684 ldlm_mode_t mode, void *data, __u32 data_len)
686 struct ldlm_resource *res, *parent_res = NULL;
687 struct ldlm_lock *lock, *parent_lock = NULL;
689 if (parent_lock_handle) {
690 parent_lock = ldlm_handle2lock(parent_lock_handle);
692 parent_res = parent_lock->l_resource;
695 res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
699 lock = ldlm_lock_new(parent_lock, res);
700 ldlm_resource_putref(res);
701 if (parent_lock != NULL)
702 LDLM_LOCK_PUT(parent_lock);
707 lock->l_req_mode = mode;
709 lock->l_data_len = data_len;
714 /* Must be called with lock->l_lock and lock->l_resource->lr_lock not held */
715 ldlm_error_t ldlm_lock_enqueue(struct ldlm_lock * lock,
716 void *cookie, int cookie_len,
718 ldlm_completion_callback completion,
719 ldlm_blocking_callback blocking)
721 struct ldlm_resource *res;
723 ldlm_res_policy policy;
726 res = lock->l_resource;
727 lock->l_blocking_ast = blocking;
729 if (res->lr_type == LDLM_EXTENT)
730 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
732 /* policies are not executed on the client or during replay */
733 local = res->lr_namespace->ns_client;
734 if (!local && !(*flags & LDLM_FL_REPLAY) &&
735 (policy = ldlm_res_policy_table[res->lr_type])) {
737 rc = policy(lock, cookie, lock->l_req_mode, *flags, NULL);
739 if (rc == ELDLM_LOCK_CHANGED) {
740 res = lock->l_resource;
741 *flags |= LDLM_FL_LOCK_CHANGED;
742 } else if (rc == ELDLM_LOCK_ABORTED) {
743 ldlm_lock_destroy(lock);
748 l_lock(&res->lr_namespace->ns_lock);
749 if (local && lock->l_req_mode == lock->l_granted_mode) {
750 /* The server returned a blocked lock, but it was granted before
751 * we got a chance to actually enqueue it. We don't need to do
753 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
754 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
758 /* This distinction between local lock trees is very important; a client
759 * namespace only has information about locks taken by that client, and
760 * thus doesn't have enough information to decide for itself if it can
761 * be granted (below). In this case, we do exactly what the server
762 * tells us to do, as dictated by the 'flags'.
764 * We do exactly the same thing during recovery, when the server is
765 * more or less trusting the clients not to lie.
767 * FIXME (bug 268): Detect obvious lies by checking compatibility in
768 * granted/converting queues. */
769 ldlm_resource_unlink_lock(lock);
770 if (local || (*flags & LDLM_FL_REPLAY)) {
771 if (*flags & LDLM_FL_BLOCK_CONV)
772 ldlm_resource_add_lock(res, res->lr_converting.prev,
774 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
775 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
777 ldlm_grant_lock(lock);
781 /* FIXME: We may want to optimize by checking lr_most_restr */
782 if (!list_empty(&res->lr_converting)) {
783 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
784 *flags |= LDLM_FL_BLOCK_CONV;
787 if (!list_empty(&res->lr_waiting)) {
788 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
789 *flags |= LDLM_FL_BLOCK_WAIT;
792 if (!ldlm_lock_compat(lock, 0)) {
793 ldlm_resource_add_lock(res, res->lr_waiting.prev, lock);
794 *flags |= LDLM_FL_BLOCK_GRANTED;
798 ldlm_grant_lock(lock);
801 l_unlock(&res->lr_namespace->ns_lock);
802 /* Don't set 'completion_ast' until here so that if the lock is granted
803 * immediately we don't do an unnecessary completion call. */
804 lock->l_completion_ast = completion;
808 /* Must be called with namespace taken: queue is waiting or converting. */
809 static int ldlm_reprocess_queue(struct ldlm_resource *res,
810 struct list_head *queue)
812 struct list_head *tmp, *pos;
815 list_for_each_safe(tmp, pos, queue) {
816 struct ldlm_lock *pending;
817 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
819 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
821 if (!ldlm_lock_compat(pending, 1))
824 list_del_init(&pending->l_res_link);
825 ldlm_grant_lock(pending);
831 void ldlm_run_ast_work(struct list_head *rpc_list)
833 struct list_head *tmp, *pos;
837 list_for_each_safe(tmp, pos, rpc_list) {
838 struct ldlm_ast_work *w =
839 list_entry(tmp, struct ldlm_ast_work, w_list);
842 rc = w->w_lock->l_blocking_ast
843 (w->w_lock, &w->w_desc, w->w_data,
844 w->w_datalen, LDLM_CB_BLOCKING);
846 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags);
848 CERROR("Failed AST - should clean & disconnect "
850 LDLM_LOCK_PUT(w->w_lock);
851 list_del(&w->w_list);
852 OBD_FREE(w, sizeof(*w));
857 /* Must be called with resource->lr_lock not taken. */
858 void ldlm_reprocess_all(struct ldlm_resource *res)
860 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
863 /* Local lock trees don't get reprocessed. */
864 if (res->lr_namespace->ns_client) {
869 l_lock(&res->lr_namespace->ns_lock);
870 res->lr_tmp = &rpc_list;
872 ldlm_reprocess_queue(res, &res->lr_converting);
873 if (list_empty(&res->lr_converting))
874 ldlm_reprocess_queue(res, &res->lr_waiting);
877 l_unlock(&res->lr_namespace->ns_lock);
879 ldlm_run_ast_work(&rpc_list);
883 void ldlm_cancel_callback(struct ldlm_lock *lock)
885 l_lock(&lock->l_resource->lr_namespace->ns_lock);
886 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
887 lock->l_flags |= LDLM_FL_CANCEL;
888 if (lock->l_blocking_ast)
889 lock->l_blocking_ast(lock, NULL, lock->l_data,
893 LDLM_DEBUG(lock, "no blocking ast");
895 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
898 void ldlm_lock_cancel(struct ldlm_lock *lock)
900 struct ldlm_resource *res;
901 struct ldlm_namespace *ns;
904 res = lock->l_resource;
905 ns = res->lr_namespace;
907 l_lock(&ns->ns_lock);
908 if (lock->l_readers || lock->l_writers) {
909 LDLM_DEBUG(lock, "lock still has references");
910 ldlm_lock_dump(lock);
914 ldlm_cancel_callback(lock);
916 ldlm_del_waiting_lock(lock);
917 ldlm_resource_unlink_lock(lock);
918 ldlm_lock_destroy(lock);
919 l_unlock(&ns->ns_lock);
923 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data, int datalen)
925 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
932 lock->l_data_len = datalen;
939 void ldlm_cancel_locks_for_export(struct obd_export *exp)
941 struct list_head *iter, *n; /* MUST BE CALLED "n"! */
943 list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
944 struct ldlm_lock *lock;
945 struct ldlm_resource *res;
946 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
947 res = ldlm_resource_getref(lock->l_resource);
948 LDLM_DEBUG(lock, "export %p", exp);
949 ldlm_lock_cancel(lock);
950 ldlm_reprocess_all(res);
951 ldlm_resource_putref(res);
955 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
958 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
959 struct ldlm_resource *res;
960 struct ldlm_namespace *ns;
964 res = lock->l_resource;
965 ns = res->lr_namespace;
967 l_lock(&ns->ns_lock);
969 lock->l_req_mode = new_mode;
970 ldlm_resource_unlink_lock(lock);
972 /* If this is a local resource, put it on the appropriate list. */
973 if (res->lr_namespace->ns_client) {
974 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED))
975 ldlm_resource_add_lock(res, res->lr_converting.prev,
978 /* This should never happen, because of the way the
979 * server handles conversions. */
982 res->lr_tmp = &rpc_list;
983 ldlm_grant_lock(lock);
986 /* FIXME: completion handling not with ns_lock held ! */
987 if (lock->l_completion_ast)
988 lock->l_completion_ast(lock, 0);
991 /* FIXME: We should try the conversion right away and possibly
992 * return success without the need for an extra AST */
993 ldlm_resource_add_lock(res, res->lr_converting.prev, lock);
994 *flags |= LDLM_FL_BLOCK_CONV;
997 l_unlock(&ns->ns_lock);
1000 ldlm_run_ast_work(&rpc_list);
1004 void ldlm_lock_dump(struct ldlm_lock *lock)
1008 if (!(portal_debug & D_OTHER))
1011 if (RES_VERSION_SIZE != 4)
1015 CDEBUG(D_OTHER, " NULL LDLM lock\n");
1019 snprintf(ver, sizeof(ver), "%x %x %x %x",
1020 lock->l_version[0], lock->l_version[1],
1021 lock->l_version[2], lock->l_version[3]);
1023 CDEBUG(D_OTHER, " -- Lock dump: %p (%s)\n", lock, ver);
1024 if (lock->l_export && lock->l_export->exp_connection)
1025 CDEBUG(D_OTHER, " Node: NID %x (rhandle: "LPX64")\n",
1026 lock->l_export->exp_connection->c_peer.peer_nid,
1027 lock->l_remote_handle.addr);
1029 CDEBUG(D_OTHER, " Node: local\n");
1030 CDEBUG(D_OTHER, " Parent: %p\n", lock->l_parent);
1031 CDEBUG(D_OTHER, " Resource: %p ("LPD64")\n", lock->l_resource,
1032 lock->l_resource->lr_name[0]);
1033 CDEBUG(D_OTHER, " Requested mode: %d, granted mode: %d\n",
1034 (int)lock->l_req_mode, (int)lock->l_granted_mode);
1035 CDEBUG(D_OTHER, " Readers: %u ; Writers; %u\n",
1036 lock->l_readers, lock->l_writers);
1037 if (lock->l_resource->lr_type == LDLM_EXTENT)
1038 CDEBUG(D_OTHER, " Extent: %Lu -> %Lu\n",
1039 (unsigned long long)lock->l_extent.start,
1040 (unsigned long long)lock->l_extent.end);
1043 void ldlm_lock_dump_handle(struct lustre_handle *lockh)
1045 struct ldlm_lock *lock;
1047 lock = ldlm_handle2lock(lockh);
1051 ldlm_lock_dump(lock);
1053 LDLM_LOCK_PUT(lock);