1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 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
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/lustre_dlm.h>
30 #include <linux/lustre_mds.h>
32 #include <liblustre.h>
33 #include <linux/kp30.h>
36 #include <linux/obd_class.h>
38 //struct lustre_lock ldlm_everything_lock;
41 char *ldlm_lockname[] = {
50 char *ldlm_typename[] = {
56 char *ldlm_it2str(int it)
63 case (IT_OPEN | IT_CREAT):
74 CERROR("Unknown intent %d\n", it);
80 extern kmem_cache_t *ldlm_lock_slab;
81 struct lustre_lock ldlm_handle_lock;
83 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b);
85 static ldlm_res_compat ldlm_res_compat_table[] = {
86 [LDLM_PLAIN] ldlm_plain_compat,
87 [LDLM_EXTENT] ldlm_extent_compat,
90 static ldlm_res_policy ldlm_intent_policy_func;
92 static int ldlm_plain_policy(struct ldlm_namespace *ns, struct ldlm_lock **lock,
93 void *req_cookie, ldlm_mode_t mode, int flags,
96 if ((flags & LDLM_FL_HAS_INTENT) && ldlm_intent_policy_func) {
97 return ldlm_intent_policy_func(ns, lock, req_cookie, mode,
104 static ldlm_res_policy ldlm_res_policy_table[] = {
105 [LDLM_PLAIN] ldlm_plain_policy,
106 [LDLM_EXTENT] ldlm_extent_policy,
109 void ldlm_register_intent(ldlm_res_policy arg)
111 ldlm_intent_policy_func = arg;
114 void ldlm_unregister_intent(void)
116 ldlm_intent_policy_func = NULL;
120 * REFCOUNTED LOCK OBJECTS
125 * Lock refcounts, during creation:
126 * - one special one for allocation, dec'd only once in destroy
127 * - one for being a lock that's in-use
128 * - one for the addref associated with a new lock
130 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
132 atomic_inc(&lock->l_refc);
136 void ldlm_lock_put(struct ldlm_lock *lock)
138 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
141 if (atomic_dec_and_test(&lock->l_refc)) {
142 l_lock(&ns->ns_lock);
143 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing");
144 LASSERT(lock->l_destroyed);
145 LASSERT(list_empty(&lock->l_res_link));
147 spin_lock(&ns->ns_counter_lock);
149 spin_unlock(&ns->ns_counter_lock);
151 ldlm_resource_putref(lock->l_resource);
152 lock->l_resource = NULL;
155 LDLM_LOCK_PUT(lock->l_parent);
157 PORTAL_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
158 l_unlock(&ns->ns_lock);
164 void ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
167 l_lock(&lock->l_resource->lr_namespace->ns_lock);
168 if (!list_empty(&lock->l_lru)) {
169 list_del_init(&lock->l_lru);
170 lock->l_resource->lr_namespace->ns_nr_unused--;
171 LASSERT(lock->l_resource->lr_namespace->ns_nr_unused >= 0);
173 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
177 /* This used to have a 'strict' flact, which recovery would use to mark an
178 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
179 * shall explain why it's gone: with the new hash table scheme, once you call
180 * ldlm_lock_destroy, you can never drop your final references on this lock.
181 * Because it's not in the hash table anymore. -phil */
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_ERROR(lock, "still has children (%p)!",
189 lock->l_children.next);
190 ldlm_lock_dump(D_ERROR, lock);
193 if (lock->l_readers || lock->l_writers) {
194 LDLM_ERROR(lock, "lock still has references");
195 ldlm_lock_dump(D_ERROR, lock);
199 if (!list_empty(&lock->l_res_link)) {
200 ldlm_lock_dump(D_ERROR, lock);
204 if (lock->l_destroyed) {
205 LASSERT(list_empty(&lock->l_lru));
206 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
210 lock->l_destroyed = 1;
212 list_del_init(&lock->l_export_chain);
213 ldlm_lock_remove_from_lru(lock);
214 class_handle_unhash(&lock->l_handle);
217 /* Wake anyone waiting for this lock */
218 /* FIXME: I should probably add yet another flag, instead of using
219 * l_export to only call this on clients */
220 lock->l_export = NULL;
221 if (lock->l_export && lock->l_completion_ast)
222 lock->l_completion_ast(lock, 0);
225 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
230 /* this is called by portals_handle2object with the handle lock taken */
231 static void lock_handle_addref(void *lock)
233 LDLM_LOCK_GET((struct ldlm_lock *)lock);
237 * usage: pass in a resource on which you have done ldlm_resource_get
238 * pass in a parent lock on which you have done a ldlm_lock_get
239 * after return, ldlm_*_put the resource and parent
240 * returns: lock with refcount 1
242 static struct ldlm_lock *ldlm_lock_new(struct ldlm_lock *parent,
243 struct ldlm_resource *resource)
245 struct ldlm_lock *lock;
248 if (resource == NULL)
251 PORTAL_SLAB_ALLOC(lock, ldlm_lock_slab, sizeof(*lock));
255 lock->l_resource = ldlm_resource_getref(resource);
257 atomic_set(&lock->l_refc, 2);
258 INIT_LIST_HEAD(&lock->l_children);
259 INIT_LIST_HEAD(&lock->l_res_link);
260 INIT_LIST_HEAD(&lock->l_lru);
261 INIT_LIST_HEAD(&lock->l_export_chain);
262 INIT_LIST_HEAD(&lock->l_pending_chain);
263 init_waitqueue_head(&lock->l_waitq);
265 spin_lock(&resource->lr_namespace->ns_counter_lock);
266 resource->lr_namespace->ns_locks++;
267 spin_unlock(&resource->lr_namespace->ns_counter_lock);
269 if (parent != NULL) {
270 l_lock(&parent->l_resource->lr_namespace->ns_lock);
271 lock->l_parent = LDLM_LOCK_GET(parent);
272 list_add(&lock->l_childof, &parent->l_children);
273 l_unlock(&parent->l_resource->lr_namespace->ns_lock);
276 INIT_LIST_HEAD(&lock->l_handle.h_link);
277 class_handle_hash(&lock->l_handle, lock_handle_addref);
282 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
283 struct ldlm_res_id new_resid)
285 struct ldlm_resource *oldres = lock->l_resource;
288 l_lock(&ns->ns_lock);
289 if (memcmp(&new_resid, &lock->l_resource->lr_name,
290 sizeof(lock->l_resource->lr_name)) == 0) {
292 l_unlock(&ns->ns_lock);
296 LASSERT(new_resid.name[0] != 0);
298 /* This function assumes that the lock isn't on any lists */
299 LASSERT(list_empty(&lock->l_res_link));
301 lock->l_resource = ldlm_resource_get(ns, NULL, new_resid,
302 lock->l_resource->lr_type, 1);
303 if (lock->l_resource == NULL) {
308 /* ...and the flowers are still standing! */
309 ldlm_resource_putref(oldres);
311 l_unlock(&ns->ns_lock);
319 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
321 POISON(&lockh->addr, 0x69, sizeof(lockh->addr));
322 lockh->cookie = lock->l_handle.h_cookie;
325 /* if flags: atomically get the lock and set the flags.
326 * Return NULL if flag already set
329 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
331 struct ldlm_lock *lock = NULL, *retval = NULL;
336 lock = class_handle2object(handle->cookie);
340 LASSERT(lock->l_resource != NULL);
341 LASSERT(lock->l_resource->lr_namespace != NULL);
343 l_lock(&lock->l_resource->lr_namespace->ns_lock);
345 /* It's unlikely but possible that someone marked the lock as
346 * destroyed after we did handle2object on it */
347 if (lock->l_destroyed) {
348 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
353 if (flags && (lock->l_flags & flags)) {
359 lock->l_flags |= flags;
364 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
368 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
369 struct lustre_handle *handle)
371 struct ldlm_lock *retval = NULL;
373 l_lock(&ns->ns_lock);
374 retval = __ldlm_handle2lock(handle, 0);
375 l_unlock(&ns->ns_lock);
380 static int ldlm_plain_compat(struct ldlm_lock *a, struct ldlm_lock *b)
382 return lockmode_compat(a->l_req_mode, b->l_req_mode);
385 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
387 ldlm_res2desc(lock->l_resource, &desc->l_resource);
388 desc->l_req_mode = lock->l_req_mode;
389 desc->l_granted_mode = lock->l_granted_mode;
390 memcpy(&desc->l_extent, &lock->l_extent, sizeof(desc->l_extent));
391 memcpy(desc->l_version, lock->l_version, sizeof(desc->l_version));
394 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
395 struct ldlm_lock *new,
396 void *data, int datalen)
398 struct ldlm_ast_work *w;
401 l_lock(&lock->l_resource->lr_namespace->ns_lock);
402 if (new && (lock->l_flags & LDLM_FL_AST_SENT))
405 OBD_ALLOC(w, sizeof(*w));
412 w->w_datalen = datalen;
414 lock->l_flags |= LDLM_FL_AST_SENT;
416 ldlm_lock2desc(new, &w->w_desc);
419 w->w_lock = LDLM_LOCK_GET(lock);
420 list_add(&w->w_list, lock->l_resource->lr_tmp);
423 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
427 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
429 struct ldlm_lock *lock;
431 lock = ldlm_handle2lock(lockh);
432 ldlm_lock_addref_internal(lock, mode);
436 /* only called for local locks */
437 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
439 l_lock(&lock->l_resource->lr_namespace->ns_lock);
440 ldlm_lock_remove_from_lru(lock);
441 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR)
445 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
447 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
450 /* Args: unlocked lock */
451 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
452 struct ldlm_res_id, int flags);
454 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
456 struct ldlm_namespace *ns;
459 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
460 ns = lock->l_resource->lr_namespace;
461 l_lock(&ns->ns_lock);
462 if (mode == LCK_NL || mode == LCK_CR || mode == LCK_PR) {
463 LASSERT(lock->l_readers > 0);
466 LASSERT(lock->l_writers > 0);
470 if (lock->l_flags & LDLM_FL_LOCAL &&
471 !lock->l_readers && !lock->l_writers) {
472 /* If this is a local lock on a server namespace and this was
473 * the last reference, cancel the lock. */
474 CDEBUG(D_INFO, "forcing cancel of local lock\n");
475 lock->l_flags |= LDLM_FL_CBPENDING;
478 if (!lock->l_readers && !lock->l_writers &&
479 (lock->l_flags & LDLM_FL_CBPENDING)) {
480 /* If we received a blocked AST and this was the last reference,
481 * run the callback. */
482 if (!ns->ns_client && lock->l_export)
483 CERROR("FL_CBPENDING set on non-local lock--just a "
486 LDLM_DEBUG(lock, "final decref done on cbpending lock");
488 if (lock->l_blocking_ast == NULL) {
489 /* The lock wasn't even fully formed; just destroy it */
490 ldlm_lock_destroy(lock);
492 l_unlock(&ns->ns_lock);
494 /* FIXME: need a real 'desc' here */
495 if (lock->l_blocking_ast != NULL)
496 lock->l_blocking_ast(lock, NULL, lock->l_data,
498 } else if (ns->ns_client && !lock->l_readers && !lock->l_writers) {
499 /* If this is a client-side namespace and this was the last
500 * reference, put it on the LRU. */
501 LASSERT(list_empty(&lock->l_lru));
502 LASSERT(ns->ns_nr_unused >= 0);
503 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
505 l_unlock(&ns->ns_lock);
508 l_unlock(&ns->ns_lock);
511 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
516 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
518 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
519 LASSERT(lock != NULL);
520 ldlm_lock_decref_internal(lock, mode);
524 /* This will drop a lock reference and mark it for destruction, but will not
525 * necessarily cancel the lock before returning. */
526 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
528 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
531 LASSERT(lock != NULL);
533 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
534 l_lock(&lock->l_resource->lr_namespace->ns_lock);
535 lock->l_flags |= LDLM_FL_CBPENDING;
536 ldlm_lock_decref_internal(lock, mode);
537 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
541 static int ldlm_lock_compat_list(struct ldlm_lock *lock, int send_cbs,
542 struct list_head *queue)
544 struct list_head *tmp, *pos;
547 list_for_each_safe(tmp, pos, queue) {
548 struct ldlm_lock *child;
549 ldlm_res_compat compat;
551 child = list_entry(tmp, struct ldlm_lock, l_res_link);
555 compat = ldlm_res_compat_table[child->l_resource->lr_type];
556 if (compat && compat(child, lock)) {
557 CDEBUG(D_OTHER, "compat function succeded, next.\n");
560 if (lockmode_compat(child->l_granted_mode, lock->l_req_mode)) {
561 CDEBUG(D_OTHER, "lock modes are compatible, next.\n");
567 if (send_cbs && child->l_blocking_ast != NULL) {
568 CDEBUG(D_OTHER, "lock %p incompatible; sending "
569 "blocking AST.\n", child);
570 ldlm_add_ast_work_item(child, lock, NULL, 0);
577 static int ldlm_lock_compat(struct ldlm_lock *lock, int send_cbs)
582 l_lock(&lock->l_resource->lr_namespace->ns_lock);
583 rc = ldlm_lock_compat_list(lock, send_cbs,
584 &lock->l_resource->lr_granted);
585 /* FIXME: should we be sending ASTs to converting? */
587 rc = ldlm_lock_compat_list
588 (lock, send_cbs, &lock->l_resource->lr_converting);
590 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
595 * - ldlm_lock_enqueue
596 * - ldlm_reprocess_queue
597 * - ldlm_lock_convert
599 void ldlm_grant_lock(struct ldlm_lock *lock, void *data, int datalen)
601 struct ldlm_resource *res = lock->l_resource;
604 l_lock(&lock->l_resource->lr_namespace->ns_lock);
605 ldlm_resource_add_lock(res, &res->lr_granted, lock);
606 lock->l_granted_mode = lock->l_req_mode;
608 if (lock->l_granted_mode < res->lr_most_restr)
609 res->lr_most_restr = lock->l_granted_mode;
611 if (lock->l_completion_ast != NULL)
612 ldlm_add_ast_work_item(lock, NULL, data, datalen);
614 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
618 /* returns a referenced lock or NULL. See the flag descriptions below, in the
619 * comment above ldlm_lock_match */
620 static struct ldlm_lock *search_queue(struct list_head *queue, ldlm_mode_t mode,
621 struct ldlm_extent *extent,
622 struct ldlm_lock *old_lock, int flags)
624 struct ldlm_lock *lock;
625 struct list_head *tmp;
627 list_for_each(tmp, queue) {
628 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
630 if (lock == old_lock)
633 if (lock->l_flags & LDLM_FL_CBPENDING)
636 if (lock->l_req_mode != mode)
639 if (lock->l_resource->lr_type == LDLM_EXTENT &&
640 (lock->l_extent.start > extent->start ||
641 lock->l_extent.end < extent->end))
644 if (lock->l_destroyed)
647 if ((flags & LDLM_FL_LOCAL_ONLY) &&
648 !(lock->l_flags & LDLM_FL_LOCAL))
651 ldlm_lock_addref_internal(lock, mode);
658 /* Can be called in two ways:
660 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
661 * for a duplicate of.
663 * Otherwise, all of the fields must be filled in, to match against.
665 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
666 * server (ie, connh is NULL)
667 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
668 * list will be considered
670 * Returns 1 if it finds an already-existing lock that is compatible; in this
671 * case, lockh is filled in with a addref()ed lock
673 int ldlm_lock_match(struct ldlm_namespace *ns, int flags,
674 struct ldlm_res_id *res_id, __u32 type, void *cookie,
675 int cookielen, ldlm_mode_t mode,struct lustre_handle *lockh)
677 struct ldlm_resource *res;
678 struct ldlm_lock *lock, *old_lock = NULL;
683 old_lock = ldlm_handle2lock(lockh);
686 ns = old_lock->l_resource->lr_namespace;
687 res_id = &old_lock->l_resource->lr_name;
688 type = old_lock->l_resource->lr_type;
689 mode = old_lock->l_req_mode;
692 res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
694 LASSERT(old_lock == NULL);
698 l_lock(&ns->ns_lock);
700 lock = search_queue(&res->lr_granted, mode, cookie, old_lock, flags);
703 if (flags & LDLM_FL_BLOCK_GRANTED)
705 lock = search_queue(&res->lr_converting, mode, cookie, old_lock, flags);
708 lock = search_queue(&res->lr_waiting, mode, cookie, old_lock, flags);
714 ldlm_resource_putref(res);
715 l_unlock(&ns->ns_lock);
718 ldlm_lock2handle(lock, lockh);
719 if (lock->l_completion_ast)
720 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
723 LDLM_DEBUG(lock, "matched");
725 LDLM_DEBUG_NOLOCK("not matched");
728 LDLM_LOCK_PUT(old_lock);
733 /* Returns a referenced lock */
734 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
735 struct lustre_handle *parent_lock_handle,
736 struct ldlm_res_id res_id, __u32 type,
737 ldlm_mode_t mode, void *data, void *cp_data)
739 struct ldlm_resource *res, *parent_res = NULL;
740 struct ldlm_lock *lock, *parent_lock = NULL;
743 if (parent_lock_handle) {
744 parent_lock = ldlm_handle2lock(parent_lock_handle);
746 parent_res = parent_lock->l_resource;
749 res = ldlm_resource_get(ns, parent_res, res_id, type, 1);
753 lock = ldlm_lock_new(parent_lock, res);
754 ldlm_resource_putref(res);
755 if (parent_lock != NULL)
756 LDLM_LOCK_PUT(parent_lock);
761 lock->l_req_mode = mode;
763 lock->l_cp_data = cp_data;
768 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
769 struct ldlm_lock **lockp,
770 void *cookie, int cookie_len,
772 ldlm_completion_callback completion,
773 ldlm_blocking_callback blocking)
775 struct ldlm_resource *res;
776 struct ldlm_lock *lock = *lockp;
778 ldlm_res_policy policy;
781 res = lock->l_resource;
782 lock->l_blocking_ast = blocking;
784 if (res->lr_type == LDLM_EXTENT)
785 memcpy(&lock->l_extent, cookie, sizeof(lock->l_extent));
787 /* policies are not executed on the client or during replay */
788 local = res->lr_namespace->ns_client;
789 if (!local && !(*flags & LDLM_FL_REPLAY) &&
790 (policy = ldlm_res_policy_table[res->lr_type])) {
792 rc = policy(ns, lockp, cookie, lock->l_req_mode, *flags, NULL);
793 if (rc == ELDLM_LOCK_CHANGED) {
794 res = lock->l_resource;
795 *flags |= LDLM_FL_LOCK_CHANGED;
796 } else if (rc == ELDLM_LOCK_REPLACED) {
797 /* The lock that was returned has already been granted,
798 * and placed into lockp. Destroy the old one and our
799 * work here is done. */
800 ldlm_lock_destroy(lock);
802 *flags |= LDLM_FL_LOCK_CHANGED;
804 } else if (rc == ELDLM_LOCK_ABORTED) {
805 ldlm_lock_destroy(lock);
810 l_lock(&ns->ns_lock);
811 if (local && lock->l_req_mode == lock->l_granted_mode) {
812 /* The server returned a blocked lock, but it was granted before
813 * we got a chance to actually enqueue it. We don't need to do
815 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
816 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
820 /* This distinction between local lock trees is very important; a client
821 * namespace only has information about locks taken by that client, and
822 * thus doesn't have enough information to decide for itself if it can
823 * be granted (below). In this case, we do exactly what the server
824 * tells us to do, as dictated by the 'flags'.
826 * We do exactly the same thing during recovery, when the server is
827 * more or less trusting the clients not to lie.
829 * FIXME (bug 268): Detect obvious lies by checking compatibility in
830 * granted/converting queues. */
831 ldlm_resource_unlink_lock(lock);
833 if (*flags & LDLM_FL_BLOCK_CONV)
834 ldlm_resource_add_lock(res, &res->lr_converting, lock);
835 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
836 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
838 ldlm_grant_lock(lock, NULL, 0);
840 } else if (*flags & LDLM_FL_REPLAY) {
841 if (*flags & LDLM_FL_BLOCK_CONV) {
842 ldlm_resource_add_lock(res, &res->lr_converting, lock);
844 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
845 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
847 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
848 ldlm_grant_lock(lock, NULL, 0);
851 /* If no flags, fall through to normal enqueue path. */
854 /* FIXME: We may want to optimize by checking lr_most_restr */
855 if (!list_empty(&res->lr_converting)) {
856 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
857 *flags |= LDLM_FL_BLOCK_CONV;
860 if (!list_empty(&res->lr_waiting)) {
861 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
862 *flags |= LDLM_FL_BLOCK_WAIT;
865 if (!ldlm_lock_compat(lock, 0)) {
866 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
867 *flags |= LDLM_FL_BLOCK_GRANTED;
871 if (lock->l_granted_cb != NULL && lock->l_data != NULL) {
873 struct ptlrpc_request *req = lock->l_data;
874 lock->l_granted_cb(lock, req->rq_repmsg, 0);
876 ldlm_grant_lock(lock, NULL, 0);
879 /* Don't set 'completion_ast' until here so that if the lock is granted
880 * immediately we don't do an unnecessary completion call. */
881 lock->l_completion_ast = completion;
882 l_unlock(&ns->ns_lock);
886 /* Must be called with namespace taken: queue is waiting or converting. */
887 static int ldlm_reprocess_queue(struct ldlm_resource *res,
888 struct list_head *queue)
890 struct list_head *tmp, *pos;
893 list_for_each_safe(tmp, pos, queue) {
894 struct ldlm_lock *pending;
895 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
897 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
899 if (!ldlm_lock_compat(pending, 1))
902 list_del_init(&pending->l_res_link);
903 ldlm_grant_lock(pending, NULL, 0);
909 int ldlm_run_ast_work(struct list_head *rpc_list)
911 struct list_head *tmp, *pos;
915 list_for_each_safe(tmp, pos, rpc_list) {
916 struct ldlm_ast_work *w =
917 list_entry(tmp, struct ldlm_ast_work, w_list);
919 /* It's possible to receive a completion AST before we've set
920 * the l_completion_ast pointer: either because the AST arrived
921 * before the reply, or simply because there's a small race
922 * window between receiving the reply and finishing the local
925 * This can't happen with the blocking_ast, however, because we
926 * will never call the local blocking_ast until we drop our
927 * reader/writer reference, which we won't do until we get the
928 * reply and finish enqueueing. */
930 LASSERT(w->w_lock->l_blocking_ast != NULL);
931 rc = w->w_lock->l_blocking_ast
932 (w->w_lock, &w->w_desc, w->w_data,
934 } else if (w->w_lock->l_completion_ast != NULL) {
935 rc = w->w_lock->l_completion_ast(w->w_lock, w->w_flags,
943 CERROR("Failed AST - should clean & disconnect "
945 LDLM_LOCK_PUT(w->w_lock);
946 list_del(&w->w_list);
947 OBD_FREE(w, sizeof(*w));
952 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
954 ldlm_reprocess_all(res);
955 return LDLM_ITER_CONTINUE;
958 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
960 (void)ldlm_namespace_foreach_res(ns, reprocess_one_queue, NULL);
963 void ldlm_reprocess_all(struct ldlm_resource *res)
965 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
969 /* Local lock trees don't get reprocessed. */
970 if (res->lr_namespace->ns_client) {
976 l_lock(&res->lr_namespace->ns_lock);
977 res->lr_tmp = &rpc_list;
979 ldlm_reprocess_queue(res, &res->lr_converting);
980 if (list_empty(&res->lr_converting))
981 ldlm_reprocess_queue(res, &res->lr_waiting);
984 l_unlock(&res->lr_namespace->ns_lock);
986 rc = ldlm_run_ast_work(&rpc_list);
992 void ldlm_cancel_callback(struct ldlm_lock *lock)
994 l_lock(&lock->l_resource->lr_namespace->ns_lock);
995 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
996 lock->l_flags |= LDLM_FL_CANCEL;
997 if (lock->l_blocking_ast)
998 lock->l_blocking_ast(lock, NULL, lock->l_data,
1001 LDLM_DEBUG(lock, "no blocking ast");
1003 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
1006 void ldlm_lock_cancel(struct ldlm_lock *lock)
1008 struct ldlm_resource *res;
1009 struct ldlm_namespace *ns;
1012 ldlm_del_waiting_lock(lock);
1014 res = lock->l_resource;
1015 ns = res->lr_namespace;
1017 l_lock(&ns->ns_lock);
1018 /* Please do not, no matter how tempting, remove this LBUG without
1019 * talking to me first. -phik */
1020 if (lock->l_readers || lock->l_writers) {
1021 LDLM_DEBUG(lock, "lock still has references");
1022 ldlm_lock_dump(D_OTHER, lock);
1026 ldlm_cancel_callback(lock);
1028 ldlm_resource_unlink_lock(lock);
1029 ldlm_lock_destroy(lock);
1030 l_unlock(&ns->ns_lock);
1034 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data, void *cp_data)
1036 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1042 lock->l_data = data;
1043 lock->l_cp_data = cp_data;
1045 LDLM_LOCK_PUT(lock);
1050 /* This function is only called from one thread (per export); no locking around
1051 * the list ops needed */
1052 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1054 struct list_head *iter, *n;
1056 list_for_each_safe(iter, n, &exp->exp_ldlm_data.led_held_locks) {
1057 struct ldlm_lock *lock;
1058 struct ldlm_resource *res;
1059 lock = list_entry(iter, struct ldlm_lock, l_export_chain);
1060 res = ldlm_resource_getref(lock->l_resource);
1061 LDLM_DEBUG(lock, "export %p", exp);
1062 ldlm_lock_cancel(lock);
1063 ldlm_reprocess_all(res);
1064 ldlm_resource_putref(res);
1068 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1071 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1072 struct ldlm_resource *res;
1073 struct ldlm_namespace *ns;
1079 res = lock->l_resource;
1080 ns = res->lr_namespace;
1082 l_lock(&ns->ns_lock);
1084 lock->l_req_mode = new_mode;
1085 ldlm_resource_unlink_lock(lock);
1087 /* If this is a local resource, put it on the appropriate list. */
1088 if (res->lr_namespace->ns_client) {
1089 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1090 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1092 /* This should never happen, because of the way the
1093 * server handles conversions. */
1096 res->lr_tmp = &rpc_list;
1097 ldlm_grant_lock(lock, NULL, 0);
1100 /* FIXME: completion handling not with ns_lock held ! */
1101 if (lock->l_completion_ast)
1102 lock->l_completion_ast(lock, 0, NULL);
1105 /* FIXME: We should try the conversion right away and possibly
1106 * return success without the need for an extra AST */
1107 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1108 *flags |= LDLM_FL_BLOCK_CONV;
1111 l_unlock(&ns->ns_lock);
1114 ldlm_run_ast_work(&rpc_list);
1118 void ldlm_lock_dump(int level, struct ldlm_lock *lock)
1122 if (!((portal_debug | D_ERROR) & level))
1125 if (RES_VERSION_SIZE != 4)
1129 CDEBUG(level, " NULL LDLM lock\n");
1133 snprintf(ver, sizeof(ver), "%x %x %x %x",
1134 lock->l_version[0], lock->l_version[1],
1135 lock->l_version[2], lock->l_version[3]);
1137 CDEBUG(level, " -- Lock dump: %p (%s) (rc: %d)\n", lock, ver,
1138 atomic_read(&lock->l_refc));
1139 if (lock->l_export && lock->l_export->exp_connection)
1140 CDEBUG(level, " Node: NID "LPX64" on %s (rhandle: "LPX64")\n",
1141 lock->l_export->exp_connection->c_peer.peer_nid,
1142 lock->l_export->exp_connection->c_peer.peer_ni->pni_name,
1143 lock->l_remote_handle.cookie);
1145 CDEBUG(level, " Node: local\n");
1146 CDEBUG(level, " Parent: %p\n", lock->l_parent);
1147 CDEBUG(level, " Resource: %p ("LPD64")\n", lock->l_resource,
1148 lock->l_resource->lr_name.name[0]);
1149 CDEBUG(level, " Requested mode: %d, granted mode: %d\n",
1150 (int)lock->l_req_mode, (int)lock->l_granted_mode);
1151 CDEBUG(level, " Readers: %u ; Writers; %u\n",
1152 lock->l_readers, lock->l_writers);
1153 if (lock->l_resource->lr_type == LDLM_EXTENT)
1154 CDEBUG(level, " Extent: "LPU64" -> "LPU64"\n",
1155 lock->l_extent.start, lock->l_extent.end);
1158 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1160 struct ldlm_lock *lock;
1162 lock = ldlm_handle2lock(lockh);
1166 ldlm_lock_dump(D_OTHER, lock);
1168 LDLM_LOCK_PUT(lock);