4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lock.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_LDLM
45 # include <libcfs/libcfs.h>
46 # include <linux/lustre_intent.h>
48 # include <liblustre.h>
51 #include <obd_class.h>
52 #include "ldlm_internal.h"
55 char *ldlm_lockname[] = {
67 char *ldlm_typename[] = {
74 static ldlm_policy_wire_to_local_t ldlm_policy_wire18_to_local[] = {
75 [LDLM_PLAIN - LDLM_MIN_TYPE] ldlm_plain_policy_wire_to_local,
76 [LDLM_EXTENT - LDLM_MIN_TYPE] ldlm_extent_policy_wire_to_local,
77 [LDLM_FLOCK - LDLM_MIN_TYPE] ldlm_flock_policy_wire18_to_local,
78 [LDLM_IBITS - LDLM_MIN_TYPE] ldlm_ibits_policy_wire_to_local,
81 static ldlm_policy_wire_to_local_t ldlm_policy_wire21_to_local[] = {
82 [LDLM_PLAIN - LDLM_MIN_TYPE] ldlm_plain_policy_wire_to_local,
83 [LDLM_EXTENT - LDLM_MIN_TYPE] ldlm_extent_policy_wire_to_local,
84 [LDLM_FLOCK - LDLM_MIN_TYPE] ldlm_flock_policy_wire21_to_local,
85 [LDLM_IBITS - LDLM_MIN_TYPE] ldlm_ibits_policy_wire_to_local,
88 static ldlm_policy_local_to_wire_t ldlm_policy_local_to_wire[] = {
89 [LDLM_PLAIN - LDLM_MIN_TYPE] ldlm_plain_policy_local_to_wire,
90 [LDLM_EXTENT - LDLM_MIN_TYPE] ldlm_extent_policy_local_to_wire,
91 [LDLM_FLOCK - LDLM_MIN_TYPE] ldlm_flock_policy_local_to_wire,
92 [LDLM_IBITS - LDLM_MIN_TYPE] ldlm_ibits_policy_local_to_wire,
96 * Converts lock policy from local format to on the wire lock_desc format
98 void ldlm_convert_policy_to_wire(ldlm_type_t type,
99 const ldlm_policy_data_t *lpolicy,
100 ldlm_wire_policy_data_t *wpolicy)
102 ldlm_policy_local_to_wire_t convert;
104 convert = ldlm_policy_local_to_wire[type - LDLM_MIN_TYPE];
106 convert(lpolicy, wpolicy);
110 * Converts lock policy from on the wire lock_desc format to local format
112 void ldlm_convert_policy_to_local(struct obd_export *exp, ldlm_type_t type,
113 const ldlm_wire_policy_data_t *wpolicy,
114 ldlm_policy_data_t *lpolicy)
116 ldlm_policy_wire_to_local_t convert;
119 /** some badnes for 2.0.0 clients, but 2.0.0 isn't supported */
120 new_client = (exp->exp_connect_flags & OBD_CONNECT_FULL20) != 0;
122 convert = ldlm_policy_wire21_to_local[type - LDLM_MIN_TYPE];
124 convert = ldlm_policy_wire18_to_local[type - LDLM_MIN_TYPE];
126 convert(wpolicy, lpolicy);
129 char *ldlm_it2str(int it)
136 case (IT_OPEN | IT_CREAT):
151 CERROR("Unknown intent %d\n", it);
156 extern cfs_mem_cache_t *ldlm_lock_slab;
158 #ifdef HAVE_SERVER_SUPPORT
159 static ldlm_processing_policy ldlm_processing_policy_table[] = {
160 [LDLM_PLAIN] ldlm_process_plain_lock,
161 [LDLM_EXTENT] ldlm_process_extent_lock,
163 [LDLM_FLOCK] ldlm_process_flock_lock,
165 [LDLM_IBITS] ldlm_process_inodebits_lock,
168 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
170 return ldlm_processing_policy_table[res->lr_type];
172 #endif /* HAVE_SERVER_SUPPORT */
174 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
180 * REFCOUNTED LOCK OBJECTS
185 * Lock refcounts, during creation:
186 * - one special one for allocation, dec'd only once in destroy
187 * - one for being a lock that's in-use
188 * - one for the addref associated with a new lock
190 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
192 cfs_atomic_inc(&lock->l_refc);
196 void ldlm_lock_put(struct ldlm_lock *lock)
200 LASSERT(lock->l_resource != LP_POISON);
201 LASSERT(cfs_atomic_read(&lock->l_refc) > 0);
202 if (cfs_atomic_dec_and_test(&lock->l_refc)) {
203 struct ldlm_resource *res;
206 "final lock_put on destroyed lock, freeing it.");
208 res = lock->l_resource;
209 LASSERT(lock->l_destroyed);
210 LASSERT(cfs_list_empty(&lock->l_res_link));
211 LASSERT(cfs_list_empty(&lock->l_pending_chain));
213 lprocfs_counter_decr(ldlm_res_to_ns(res)->ns_stats,
215 lu_ref_del(&res->lr_reference, "lock", lock);
216 ldlm_resource_putref(res);
217 lock->l_resource = NULL;
218 if (lock->l_export) {
219 class_export_lock_put(lock->l_export, lock);
220 lock->l_export = NULL;
223 if (lock->l_lvb_data != NULL)
224 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
226 ldlm_interval_free(ldlm_interval_detach(lock));
227 lu_ref_fini(&lock->l_reference);
228 OBD_FREE_RCU(lock, sizeof(*lock), &lock->l_handle);
234 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
237 if (!cfs_list_empty(&lock->l_lru)) {
238 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
240 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
241 cfs_list_del_init(&lock->l_lru);
242 if (lock->l_flags & LDLM_FL_SKIPPED)
243 lock->l_flags &= ~LDLM_FL_SKIPPED;
244 LASSERT(ns->ns_nr_unused > 0);
251 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
253 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
257 if (lock->l_ns_srv) {
258 LASSERT(cfs_list_empty(&lock->l_lru));
262 cfs_spin_lock(&ns->ns_lock);
263 rc = ldlm_lock_remove_from_lru_nolock(lock);
264 cfs_spin_unlock(&ns->ns_lock);
269 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
271 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
273 lock->l_last_used = cfs_time_current();
274 LASSERT(cfs_list_empty(&lock->l_lru));
275 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
276 cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
277 LASSERT(ns->ns_nr_unused >= 0);
281 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
283 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
286 cfs_spin_lock(&ns->ns_lock);
287 ldlm_lock_add_to_lru_nolock(lock);
288 cfs_spin_unlock(&ns->ns_lock);
292 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
294 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
297 if (lock->l_ns_srv) {
298 LASSERT(cfs_list_empty(&lock->l_lru));
303 cfs_spin_lock(&ns->ns_lock);
304 if (!cfs_list_empty(&lock->l_lru)) {
305 ldlm_lock_remove_from_lru_nolock(lock);
306 ldlm_lock_add_to_lru_nolock(lock);
308 cfs_spin_unlock(&ns->ns_lock);
312 /* This used to have a 'strict' flag, which recovery would use to mark an
313 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
314 * shall explain why it's gone: with the new hash table scheme, once you call
315 * ldlm_lock_destroy, you can never drop your final references on this lock.
316 * Because it's not in the hash table anymore. -phil */
317 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
321 if (lock->l_readers || lock->l_writers) {
322 LDLM_ERROR(lock, "lock still has references");
326 if (!cfs_list_empty(&lock->l_res_link)) {
327 LDLM_ERROR(lock, "lock still on resource");
331 if (lock->l_destroyed) {
332 LASSERT(cfs_list_empty(&lock->l_lru));
336 lock->l_destroyed = 1;
338 if (lock->l_export && lock->l_export->exp_lock_hash &&
339 !cfs_hlist_unhashed(&lock->l_exp_hash))
340 cfs_hash_del(lock->l_export->exp_lock_hash,
341 &lock->l_remote_handle, &lock->l_exp_hash);
343 ldlm_lock_remove_from_lru(lock);
344 class_handle_unhash(&lock->l_handle);
347 /* Wake anyone waiting for this lock */
348 /* FIXME: I should probably add yet another flag, instead of using
349 * l_export to only call this on clients */
351 class_export_put(lock->l_export);
352 lock->l_export = NULL;
353 if (lock->l_export && lock->l_completion_ast)
354 lock->l_completion_ast(lock, 0);
360 void ldlm_lock_destroy(struct ldlm_lock *lock)
364 lock_res_and_lock(lock);
365 first = ldlm_lock_destroy_internal(lock);
366 unlock_res_and_lock(lock);
368 /* drop reference from hashtable only for first destroy */
370 lu_ref_del(&lock->l_reference, "hash", lock);
371 LDLM_LOCK_RELEASE(lock);
376 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
380 first = ldlm_lock_destroy_internal(lock);
381 /* drop reference from hashtable only for first destroy */
383 lu_ref_del(&lock->l_reference, "hash", lock);
384 LDLM_LOCK_RELEASE(lock);
389 /* this is called by portals_handle2object with the handle lock taken */
390 static void lock_handle_addref(void *lock)
392 LDLM_LOCK_GET((struct ldlm_lock *)lock);
395 static void lock_handle_free(void *lock, int size)
397 LASSERT(size == sizeof(struct ldlm_lock));
398 OBD_SLAB_FREE(lock, ldlm_lock_slab, size);
401 struct portals_handle_ops lock_handle_ops = {
402 .hop_addref = lock_handle_addref,
403 .hop_free = lock_handle_free,
407 * usage: pass in a resource on which you have done ldlm_resource_get
408 * new lock will take over the refcount.
409 * returns: lock with refcount 2 - one for current caller and one for remote
411 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
413 struct ldlm_lock *lock;
416 if (resource == NULL)
419 OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
423 cfs_spin_lock_init(&lock->l_lock);
424 lock->l_resource = resource;
425 lu_ref_add(&resource->lr_reference, "lock", lock);
427 cfs_atomic_set(&lock->l_refc, 2);
428 CFS_INIT_LIST_HEAD(&lock->l_res_link);
429 CFS_INIT_LIST_HEAD(&lock->l_lru);
430 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
431 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
432 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
433 CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
434 cfs_waitq_init(&lock->l_waitq);
435 lock->l_blocking_lock = NULL;
436 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
437 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
438 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
440 lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
442 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
443 class_handle_hash(&lock->l_handle, &lock_handle_ops);
445 lu_ref_init(&lock->l_reference);
446 lu_ref_add(&lock->l_reference, "hash", lock);
447 lock->l_callback_timeout = 0;
449 #if LUSTRE_TRACKS_LOCK_EXP_REFS
450 CFS_INIT_LIST_HEAD(&lock->l_exp_refs_link);
451 lock->l_exp_refs_nr = 0;
452 lock->l_exp_refs_target = NULL;
454 CFS_INIT_LIST_HEAD(&lock->l_exp_list);
459 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
460 const struct ldlm_res_id *new_resid)
462 struct ldlm_resource *oldres = lock->l_resource;
463 struct ldlm_resource *newres;
467 LASSERT(ns_is_client(ns));
469 lock_res_and_lock(lock);
470 if (memcmp(new_resid, &lock->l_resource->lr_name,
471 sizeof(lock->l_resource->lr_name)) == 0) {
473 unlock_res_and_lock(lock);
477 LASSERT(new_resid->name[0] != 0);
479 /* This function assumes that the lock isn't on any lists */
480 LASSERT(cfs_list_empty(&lock->l_res_link));
482 type = oldres->lr_type;
483 unlock_res_and_lock(lock);
485 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
489 lu_ref_add(&newres->lr_reference, "lock", lock);
491 * To flip the lock from the old to the new resource, lock, oldres and
492 * newres have to be locked. Resource spin-locks are nested within
493 * lock->l_lock, and are taken in the memory address order to avoid
496 cfs_spin_lock(&lock->l_lock);
497 oldres = lock->l_resource;
498 if (oldres < newres) {
500 lock_res_nested(newres, LRT_NEW);
503 lock_res_nested(oldres, LRT_NEW);
505 LASSERT(memcmp(new_resid, &oldres->lr_name,
506 sizeof oldres->lr_name) != 0);
507 lock->l_resource = newres;
509 unlock_res_and_lock(lock);
511 /* ...and the flowers are still standing! */
512 lu_ref_del(&oldres->lr_reference, "lock", lock);
513 ldlm_resource_putref(oldres);
522 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
524 lockh->cookie = lock->l_handle.h_cookie;
527 /* if flags: atomically get the lock and set the flags.
528 * Return NULL if flag already set
531 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
534 struct ldlm_lock *lock;
539 lock = class_handle2object(handle->cookie);
543 /* It's unlikely but possible that someone marked the lock as
544 * destroyed after we did handle2object on it */
545 if (flags == 0 && !lock->l_destroyed) {
546 lu_ref_add(&lock->l_reference, "handle", cfs_current());
550 lock_res_and_lock(lock);
552 LASSERT(lock->l_resource != NULL);
554 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
555 if (unlikely(lock->l_destroyed)) {
556 unlock_res_and_lock(lock);
557 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
562 if (flags && (lock->l_flags & flags)) {
563 unlock_res_and_lock(lock);
569 lock->l_flags |= flags;
571 unlock_res_and_lock(lock);
575 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
577 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
578 /* INODEBITS_INTEROP: If the other side does not support
579 * inodebits, reply with a plain lock descriptor.
581 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
582 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
583 /* Make sure all the right bits are set in this lock we
584 are going to pass to client */
585 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
586 (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
587 MDS_INODELOCK_LAYOUT),
588 "Inappropriate inode lock bits during "
589 "conversion " LPU64 "\n",
590 lock->l_policy_data.l_inodebits.bits);
592 ldlm_res2desc(lock->l_resource, &desc->l_resource);
593 desc->l_resource.lr_type = LDLM_PLAIN;
595 /* Convert "new" lock mode to something old client can
597 if ((lock->l_req_mode == LCK_CR) ||
598 (lock->l_req_mode == LCK_CW))
599 desc->l_req_mode = LCK_PR;
601 desc->l_req_mode = lock->l_req_mode;
602 if ((lock->l_granted_mode == LCK_CR) ||
603 (lock->l_granted_mode == LCK_CW)) {
604 desc->l_granted_mode = LCK_PR;
606 /* We never grant PW/EX locks to clients */
607 LASSERT((lock->l_granted_mode != LCK_PW) &&
608 (lock->l_granted_mode != LCK_EX));
609 desc->l_granted_mode = lock->l_granted_mode;
612 /* We do not copy policy here, because there is no
613 policy for plain locks */
615 ldlm_res2desc(lock->l_resource, &desc->l_resource);
616 desc->l_req_mode = lock->l_req_mode;
617 desc->l_granted_mode = lock->l_granted_mode;
618 ldlm_convert_policy_to_wire(lock->l_resource->lr_type,
619 &lock->l_policy_data,
620 &desc->l_policy_data);
624 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
625 cfs_list_t *work_list)
627 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
628 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
629 lock->l_flags |= LDLM_FL_AST_SENT;
630 /* If the enqueuing client said so, tell the AST recipient to
631 * discard dirty data, rather than writing back. */
632 if (new->l_flags & LDLM_AST_DISCARD_DATA)
633 lock->l_flags |= LDLM_FL_DISCARD_DATA;
634 LASSERT(cfs_list_empty(&lock->l_bl_ast));
635 cfs_list_add(&lock->l_bl_ast, work_list);
637 LASSERT(lock->l_blocking_lock == NULL);
638 lock->l_blocking_lock = LDLM_LOCK_GET(new);
642 void ldlm_add_cp_work_item(struct ldlm_lock *lock, cfs_list_t *work_list)
644 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
645 lock->l_flags |= LDLM_FL_CP_REQD;
646 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
647 LASSERT(cfs_list_empty(&lock->l_cp_ast));
648 cfs_list_add(&lock->l_cp_ast, work_list);
653 /* must be called with lr_lock held */
654 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
655 cfs_list_t *work_list)
658 check_res_locked(lock->l_resource);
660 ldlm_add_bl_work_item(lock, new, work_list);
662 ldlm_add_cp_work_item(lock, work_list);
666 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
668 struct ldlm_lock *lock;
670 lock = ldlm_handle2lock(lockh);
671 LASSERT(lock != NULL);
672 ldlm_lock_addref_internal(lock, mode);
676 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
678 ldlm_lock_remove_from_lru(lock);
679 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
681 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
683 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
685 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
688 lu_ref_add_atomic(&lock->l_reference, "user", lock);
689 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
693 * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
696 * \retval 0 success, lock was addref-ed
698 * \retval -EAGAIN lock is being canceled.
700 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
702 struct ldlm_lock *lock;
706 lock = ldlm_handle2lock(lockh);
708 lock_res_and_lock(lock);
709 if (lock->l_readers != 0 || lock->l_writers != 0 ||
710 !(lock->l_flags & LDLM_FL_CBPENDING)) {
711 ldlm_lock_addref_internal_nolock(lock, mode);
714 unlock_res_and_lock(lock);
720 /* only called for local locks */
721 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
723 lock_res_and_lock(lock);
724 ldlm_lock_addref_internal_nolock(lock, mode);
725 unlock_res_and_lock(lock);
728 /* only called in ldlm_flock_destroy and for local locks.
729 * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
730 * * ldlm_lock_remove_from_lru() does nothing, it is safe
731 * * for ldlm_flock_destroy usage by dropping some code */
732 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
734 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
735 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
736 LASSERT(lock->l_readers > 0);
737 lu_ref_del(&lock->l_reference, "reader", lock);
740 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
741 LASSERT(lock->l_writers > 0);
742 lu_ref_del(&lock->l_reference, "writer", lock);
746 lu_ref_del(&lock->l_reference, "user", lock);
747 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
750 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
752 struct ldlm_namespace *ns;
755 lock_res_and_lock(lock);
757 ns = ldlm_lock_to_ns(lock);
759 ldlm_lock_decref_internal_nolock(lock, mode);
761 if (lock->l_flags & LDLM_FL_LOCAL &&
762 !lock->l_readers && !lock->l_writers) {
763 /* If this is a local lock on a server namespace and this was
764 * the last reference, cancel the lock. */
765 CDEBUG(D_INFO, "forcing cancel of local lock\n");
766 lock->l_flags |= LDLM_FL_CBPENDING;
769 if (!lock->l_readers && !lock->l_writers &&
770 (lock->l_flags & LDLM_FL_CBPENDING)) {
771 /* If we received a blocked AST and this was the last reference,
772 * run the callback. */
773 if (lock->l_ns_srv && lock->l_export)
774 CERROR("FL_CBPENDING set on non-local lock--just a "
777 LDLM_DEBUG(lock, "final decref done on cbpending lock");
779 LDLM_LOCK_GET(lock); /* dropped by bl thread */
780 ldlm_lock_remove_from_lru(lock);
781 unlock_res_and_lock(lock);
783 if (lock->l_flags & LDLM_FL_FAIL_LOC)
784 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
786 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
787 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
788 ldlm_handle_bl_callback(ns, NULL, lock);
789 } else if (ns_is_client(ns) &&
790 !lock->l_readers && !lock->l_writers &&
791 !(lock->l_flags & LDLM_FL_NO_LRU) &&
792 !(lock->l_flags & LDLM_FL_BL_AST)) {
794 LDLM_DEBUG(lock, "add lock into lru list");
796 /* If this is a client-side namespace and this was the last
797 * reference, put it on the LRU. */
798 ldlm_lock_add_to_lru(lock);
799 unlock_res_and_lock(lock);
801 if (lock->l_flags & LDLM_FL_FAIL_LOC)
802 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
804 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
805 * are not supported by the server, otherwise, it is done on
807 if (!exp_connect_cancelset(lock->l_conn_export) &&
808 !ns_connect_lru_resize(ns))
809 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
811 LDLM_DEBUG(lock, "do not add lock into lru list");
812 unlock_res_and_lock(lock);
818 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
820 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
821 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
822 ldlm_lock_decref_internal(lock, mode);
826 /* This will drop a lock reference and mark it for destruction, but will not
827 * necessarily cancel the lock before returning. */
828 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
830 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
833 LASSERT(lock != NULL);
835 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
836 lock_res_and_lock(lock);
837 lock->l_flags |= LDLM_FL_CBPENDING;
838 unlock_res_and_lock(lock);
839 ldlm_lock_decref_internal(lock, mode);
843 struct sl_insert_point {
844 cfs_list_t *res_link;
845 cfs_list_t *mode_link;
846 cfs_list_t *policy_link;
850 * search_granted_lock
853 * Finds a position to insert the new lock.
855 * queue [input]: the granted list where search acts on;
856 * req [input]: the lock whose position to be located;
857 * prev [output]: positions within 3 lists to insert @req to
861 * - ldlm_grant_lock_with_skiplist
863 static void search_granted_lock(cfs_list_t *queue,
864 struct ldlm_lock *req,
865 struct sl_insert_point *prev)
868 struct ldlm_lock *lock, *mode_end, *policy_end;
871 cfs_list_for_each(tmp, queue) {
872 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
874 mode_end = cfs_list_entry(lock->l_sl_mode.prev,
875 struct ldlm_lock, l_sl_mode);
877 if (lock->l_req_mode != req->l_req_mode) {
878 /* jump to last lock of mode group */
879 tmp = &mode_end->l_res_link;
883 /* suitable mode group is found */
884 if (lock->l_resource->lr_type == LDLM_PLAIN) {
885 /* insert point is last lock of the mode group */
886 prev->res_link = &mode_end->l_res_link;
887 prev->mode_link = &mode_end->l_sl_mode;
888 prev->policy_link = &req->l_sl_policy;
891 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
894 cfs_list_entry(lock->l_sl_policy.prev,
898 if (lock->l_policy_data.l_inodebits.bits ==
899 req->l_policy_data.l_inodebits.bits) {
900 /* insert point is last lock of
901 * the policy group */
903 &policy_end->l_res_link;
905 &policy_end->l_sl_mode;
907 &policy_end->l_sl_policy;
912 if (policy_end == mode_end)
913 /* done with mode group */
916 /* go to next policy group within mode group */
917 tmp = policy_end->l_res_link.next;
918 lock = cfs_list_entry(tmp, struct ldlm_lock,
920 } /* loop over policy groups within the mode group */
922 /* insert point is last lock of the mode group,
923 * new policy group is started */
924 prev->res_link = &mode_end->l_res_link;
925 prev->mode_link = &mode_end->l_sl_mode;
926 prev->policy_link = &req->l_sl_policy;
930 LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
935 /* insert point is last lock on the queue,
936 * new mode group and new policy group are started */
937 prev->res_link = queue->prev;
938 prev->mode_link = &req->l_sl_mode;
939 prev->policy_link = &req->l_sl_policy;
944 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
945 struct sl_insert_point *prev)
947 struct ldlm_resource *res = lock->l_resource;
950 check_res_locked(res);
952 ldlm_resource_dump(D_INFO, res);
953 LDLM_DEBUG(lock, "About to add lock:");
955 if (lock->l_destroyed) {
956 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
960 LASSERT(cfs_list_empty(&lock->l_res_link));
961 LASSERT(cfs_list_empty(&lock->l_sl_mode));
962 LASSERT(cfs_list_empty(&lock->l_sl_policy));
964 cfs_list_add(&lock->l_res_link, prev->res_link);
965 cfs_list_add(&lock->l_sl_mode, prev->mode_link);
966 cfs_list_add(&lock->l_sl_policy, prev->policy_link);
971 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
973 struct sl_insert_point prev;
976 LASSERT(lock->l_req_mode == lock->l_granted_mode);
978 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
979 ldlm_granted_list_add_lock(lock, &prev);
984 * - ldlm_lock_enqueue
985 * - ldlm_reprocess_queue
986 * - ldlm_lock_convert
988 * must be called with lr_lock held
990 void ldlm_grant_lock(struct ldlm_lock *lock, cfs_list_t *work_list)
992 struct ldlm_resource *res = lock->l_resource;
995 check_res_locked(res);
997 lock->l_granted_mode = lock->l_req_mode;
998 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
999 ldlm_grant_lock_with_skiplist(lock);
1000 else if (res->lr_type == LDLM_EXTENT)
1001 ldlm_extent_add_lock(res, lock);
1003 ldlm_resource_add_lock(res, &res->lr_granted, lock);
1005 if (lock->l_granted_mode < res->lr_most_restr)
1006 res->lr_most_restr = lock->l_granted_mode;
1008 if (work_list && lock->l_completion_ast != NULL)
1009 ldlm_add_ast_work_item(lock, NULL, work_list);
1011 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
1015 /* returns a referenced lock or NULL. See the flag descriptions below, in the
1016 * comment above ldlm_lock_match */
1017 static struct ldlm_lock *search_queue(cfs_list_t *queue,
1019 ldlm_policy_data_t *policy,
1020 struct ldlm_lock *old_lock,
1021 int flags, int unref)
1023 struct ldlm_lock *lock;
1026 cfs_list_for_each(tmp, queue) {
1029 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1031 if (lock == old_lock)
1034 /* llite sometimes wants to match locks that will be
1035 * canceled when their users drop, but we allow it to match
1036 * if it passes in CBPENDING and the lock still has users.
1037 * this is generally only going to be used by children
1038 * whose parents already hold a lock so forward progress
1039 * can still happen. */
1040 if (lock->l_flags & LDLM_FL_CBPENDING &&
1041 !(flags & LDLM_FL_CBPENDING))
1043 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
1044 lock->l_readers == 0 && lock->l_writers == 0)
1047 if (!(lock->l_req_mode & *mode))
1049 match = lock->l_req_mode;
1051 if (lock->l_resource->lr_type == LDLM_EXTENT &&
1052 (lock->l_policy_data.l_extent.start >
1053 policy->l_extent.start ||
1054 lock->l_policy_data.l_extent.end < policy->l_extent.end))
1057 if (unlikely(match == LCK_GROUP) &&
1058 lock->l_resource->lr_type == LDLM_EXTENT &&
1059 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
1062 /* We match if we have existing lock with same or wider set
1064 if (lock->l_resource->lr_type == LDLM_IBITS &&
1065 ((lock->l_policy_data.l_inodebits.bits &
1066 policy->l_inodebits.bits) !=
1067 policy->l_inodebits.bits))
1071 (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
1075 if ((flags & LDLM_FL_LOCAL_ONLY) &&
1076 !(lock->l_flags & LDLM_FL_LOCAL))
1079 if (flags & LDLM_FL_TEST_LOCK) {
1080 LDLM_LOCK_GET(lock);
1081 ldlm_lock_touch_in_lru(lock);
1083 ldlm_lock_addref_internal_nolock(lock, match);
1092 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock)
1094 if (!lock->l_failed) {
1096 cfs_waitq_broadcast(&lock->l_waitq);
1099 EXPORT_SYMBOL(ldlm_lock_fail_match_locked);
1101 void ldlm_lock_fail_match(struct ldlm_lock *lock)
1103 lock_res_and_lock(lock);
1104 ldlm_lock_fail_match_locked(lock);
1105 unlock_res_and_lock(lock);
1107 EXPORT_SYMBOL(ldlm_lock_fail_match);
1109 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1111 lock->l_flags |= LDLM_FL_LVB_READY;
1112 cfs_waitq_broadcast(&lock->l_waitq);
1115 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1117 lock_res_and_lock(lock);
1118 ldlm_lock_allow_match_locked(lock);
1119 unlock_res_and_lock(lock);
1122 /* Can be called in two ways:
1124 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1125 * for a duplicate of.
1127 * Otherwise, all of the fields must be filled in, to match against.
1129 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1130 * server (ie, connh is NULL)
1131 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1132 * list will be considered
1133 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1134 * to be canceled can still be matched as long as they still have reader
1135 * or writer refernces
1136 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1137 * just tell us if we would have matched.
1139 * Returns 1 if it finds an already-existing lock that is compatible; in this
1140 * case, lockh is filled in with a addref()ed lock
1142 * we also check security context, if that failed we simply return 0 (to keep
1143 * caller code unchanged), the context failure will be discovered by caller
1146 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1147 const struct ldlm_res_id *res_id, ldlm_type_t type,
1148 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1149 struct lustre_handle *lockh, int unref)
1151 struct ldlm_resource *res;
1152 struct ldlm_lock *lock, *old_lock = NULL;
1157 old_lock = ldlm_handle2lock(lockh);
1160 ns = ldlm_lock_to_ns(old_lock);
1161 res_id = &old_lock->l_resource->lr_name;
1162 type = old_lock->l_resource->lr_type;
1163 mode = old_lock->l_req_mode;
1166 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1168 LASSERT(old_lock == NULL);
1172 LDLM_RESOURCE_ADDREF(res);
1175 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1179 if (flags & LDLM_FL_BLOCK_GRANTED)
1181 lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1185 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1193 LDLM_RESOURCE_DELREF(res);
1194 ldlm_resource_putref(res);
1197 ldlm_lock2handle(lock, lockh);
1198 if ((flags & LDLM_FL_LVB_READY) &&
1199 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1200 struct l_wait_info lwi;
1201 if (lock->l_completion_ast) {
1202 int err = lock->l_completion_ast(lock,
1203 LDLM_FL_WAIT_NOREPROC,
1206 if (flags & LDLM_FL_TEST_LOCK)
1207 LDLM_LOCK_RELEASE(lock);
1209 ldlm_lock_decref_internal(lock,
1216 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1217 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1219 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1220 l_wait_event(lock->l_waitq,
1221 lock->l_flags & LDLM_FL_LVB_READY ||
1224 if (!(lock->l_flags & LDLM_FL_LVB_READY)) {
1225 if (flags & LDLM_FL_TEST_LOCK)
1226 LDLM_LOCK_RELEASE(lock);
1228 ldlm_lock_decref_internal(lock, mode);
1235 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1236 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1237 res_id->name[2] : policy->l_extent.start,
1238 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1239 res_id->name[3] : policy->l_extent.end);
1241 /* check user's security context */
1242 if (lock->l_conn_export &&
1243 sptlrpc_import_check_ctx(
1244 class_exp2cliimp(lock->l_conn_export))) {
1245 if (!(flags & LDLM_FL_TEST_LOCK))
1246 ldlm_lock_decref_internal(lock, mode);
1250 if (flags & LDLM_FL_TEST_LOCK)
1251 LDLM_LOCK_RELEASE(lock);
1253 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1254 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1255 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1256 type, mode, res_id->name[0], res_id->name[1],
1257 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1258 res_id->name[2] :policy->l_extent.start,
1259 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1260 res_id->name[3] : policy->l_extent.end);
1263 LDLM_LOCK_PUT(old_lock);
1265 return rc ? mode : 0;
1268 ldlm_mode_t ldlm_revalidate_lock_handle(struct lustre_handle *lockh,
1271 struct ldlm_lock *lock;
1272 ldlm_mode_t mode = 0;
1275 lock = ldlm_handle2lock(lockh);
1277 lock_res_and_lock(lock);
1278 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
1282 if (lock->l_flags & LDLM_FL_CBPENDING &&
1283 lock->l_readers == 0 && lock->l_writers == 0)
1287 *bits = lock->l_policy_data.l_inodebits.bits;
1288 mode = lock->l_granted_mode;
1289 ldlm_lock_addref_internal_nolock(lock, mode);
1296 unlock_res_and_lock(lock);
1297 LDLM_LOCK_PUT(lock);
1301 EXPORT_SYMBOL(ldlm_revalidate_lock_handle);
1303 /* Returns a referenced lock */
1304 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1305 const struct ldlm_res_id *res_id,
1308 const struct ldlm_callback_suite *cbs,
1309 void *data, __u32 lvb_len)
1311 struct ldlm_lock *lock;
1312 struct ldlm_resource *res;
1315 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1319 lock = ldlm_lock_new(res);
1324 lock->l_req_mode = mode;
1325 lock->l_ast_data = data;
1326 lock->l_pid = cfs_curproc_pid();
1327 lock->l_ns_srv = !!ns_is_server(ns);
1329 lock->l_blocking_ast = cbs->lcs_blocking;
1330 lock->l_completion_ast = cbs->lcs_completion;
1331 lock->l_glimpse_ast = cbs->lcs_glimpse;
1332 lock->l_weigh_ast = cbs->lcs_weigh;
1335 lock->l_tree_node = NULL;
1336 /* if this is the extent lock, allocate the interval tree node */
1337 if (type == LDLM_EXTENT) {
1338 if (ldlm_interval_alloc(lock) == NULL)
1343 lock->l_lvb_len = lvb_len;
1344 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1345 if (lock->l_lvb_data == NULL)
1349 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
1355 ldlm_lock_destroy(lock);
1356 LDLM_LOCK_RELEASE(lock);
1360 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1361 struct ldlm_lock **lockp,
1362 void *cookie, int *flags)
1364 struct ldlm_lock *lock = *lockp;
1365 struct ldlm_resource *res = lock->l_resource;
1366 int local = ns_is_client(ldlm_res_to_ns(res));
1367 #ifdef HAVE_SERVER_SUPPORT
1368 ldlm_processing_policy policy;
1370 ldlm_error_t rc = ELDLM_OK;
1371 struct ldlm_interval *node = NULL;
1374 lock->l_last_activity = cfs_time_current_sec();
1375 /* policies are not executed on the client or during replay */
1376 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1377 && !local && ns->ns_policy) {
1378 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1380 if (rc == ELDLM_LOCK_REPLACED) {
1381 /* The lock that was returned has already been granted,
1382 * and placed into lockp. If it's not the same as the
1383 * one we passed in, then destroy the old one and our
1384 * work here is done. */
1385 if (lock != *lockp) {
1386 ldlm_lock_destroy(lock);
1387 LDLM_LOCK_RELEASE(lock);
1389 *flags |= LDLM_FL_LOCK_CHANGED;
1391 } else if (rc != ELDLM_OK ||
1392 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1393 ldlm_lock_destroy(lock);
1398 /* For a replaying lock, it might be already in granted list. So
1399 * unlinking the lock will cause the interval node to be freed, we
1400 * have to allocate the interval node early otherwise we can't regrant
1401 * this lock in the future. - jay */
1402 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1403 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1405 lock_res_and_lock(lock);
1406 if (local && lock->l_req_mode == lock->l_granted_mode) {
1407 /* The server returned a blocked lock, but it was granted
1408 * before we got a chance to actually enqueue it. We don't
1409 * need to do anything else. */
1410 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1411 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1412 GOTO(out, ELDLM_OK);
1415 ldlm_resource_unlink_lock(lock);
1416 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1418 ldlm_lock_destroy_nolock(lock);
1419 GOTO(out, rc = -ENOMEM);
1422 CFS_INIT_LIST_HEAD(&node->li_group);
1423 ldlm_interval_attach(node, lock);
1427 /* Some flags from the enqueue want to make it into the AST, via the
1428 * lock's l_flags. */
1429 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1431 /* This distinction between local lock trees is very important; a client
1432 * namespace only has information about locks taken by that client, and
1433 * thus doesn't have enough information to decide for itself if it can
1434 * be granted (below). In this case, we do exactly what the server
1435 * tells us to do, as dictated by the 'flags'.
1437 * We do exactly the same thing during recovery, when the server is
1438 * more or less trusting the clients not to lie.
1440 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1441 * granted/converting queues. */
1443 if (*flags & LDLM_FL_BLOCK_CONV)
1444 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1445 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1446 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1448 ldlm_grant_lock(lock, NULL);
1449 GOTO(out, ELDLM_OK);
1450 #ifdef HAVE_SERVER_SUPPORT
1451 } else if (*flags & LDLM_FL_REPLAY) {
1452 if (*flags & LDLM_FL_BLOCK_CONV) {
1453 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1454 GOTO(out, ELDLM_OK);
1455 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1456 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1457 GOTO(out, ELDLM_OK);
1458 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1459 ldlm_grant_lock(lock, NULL);
1460 GOTO(out, ELDLM_OK);
1462 /* If no flags, fall through to normal enqueue path. */
1465 policy = ldlm_processing_policy_table[res->lr_type];
1466 policy(lock, flags, 1, &rc, NULL);
1470 CERROR("This is client-side-only module, cannot handle "
1471 "LDLM_NAMESPACE_SERVER resource type lock.\n");
1477 unlock_res_and_lock(lock);
1479 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1483 #ifdef HAVE_SERVER_SUPPORT
1484 /* Must be called with namespace taken: queue is waiting or converting. */
1485 int ldlm_reprocess_queue(struct ldlm_resource *res, cfs_list_t *queue,
1486 cfs_list_t *work_list)
1488 cfs_list_t *tmp, *pos;
1489 ldlm_processing_policy policy;
1491 int rc = LDLM_ITER_CONTINUE;
1495 check_res_locked(res);
1497 policy = ldlm_processing_policy_table[res->lr_type];
1500 cfs_list_for_each_safe(tmp, pos, queue) {
1501 struct ldlm_lock *pending;
1502 pending = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1504 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1507 rc = policy(pending, &flags, 0, &err, work_list);
1508 if (rc != LDLM_ITER_CONTINUE)
1517 ldlm_work_bl_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1519 struct ldlm_lock_desc d;
1520 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1525 /* nobody should touch l_bl_ast */
1526 lock_res_and_lock(lock);
1527 cfs_list_del_init(&lock->l_bl_ast);
1529 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1530 LASSERT(lock->l_bl_ast_run == 0);
1531 LASSERT(lock->l_blocking_lock);
1532 lock->l_bl_ast_run++;
1533 unlock_res_and_lock(lock);
1535 ldlm_lock2desc(lock->l_blocking_lock, &d);
1537 rc = lock->l_blocking_ast(lock, &d, (void *)arg,
1539 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1540 lock->l_blocking_lock = NULL;
1541 LDLM_LOCK_RELEASE(lock);
1547 ldlm_work_cp_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1549 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock, l_cp_ast);
1550 ldlm_completion_callback completion_callback;
1554 /* It's possible to receive a completion AST before we've set
1555 * the l_completion_ast pointer: either because the AST arrived
1556 * before the reply, or simply because there's a small race
1557 * window between receiving the reply and finishing the local
1558 * enqueue. (bug 842)
1560 * This can't happen with the blocking_ast, however, because we
1561 * will never call the local blocking_ast until we drop our
1562 * reader/writer reference, which we won't do until we get the
1563 * reply and finish enqueueing. */
1565 /* nobody should touch l_cp_ast */
1566 lock_res_and_lock(lock);
1567 cfs_list_del_init(&lock->l_cp_ast);
1568 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1569 /* save l_completion_ast since it can be changed by
1570 * mds_intent_policy(), see bug 14225 */
1571 completion_callback = lock->l_completion_ast;
1572 lock->l_flags &= ~LDLM_FL_CP_REQD;
1573 unlock_res_and_lock(lock);
1575 if (completion_callback != NULL)
1576 rc = completion_callback(lock, 0, (void *)arg);
1577 LDLM_LOCK_RELEASE(lock);
1583 ldlm_work_revoke_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1585 struct ldlm_lock_desc desc;
1586 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1591 cfs_list_del_init(&lock->l_rk_ast);
1593 /* the desc just pretend to exclusive */
1594 ldlm_lock2desc(lock, &desc);
1595 desc.l_req_mode = LCK_EX;
1596 desc.l_granted_mode = 0;
1598 rc = lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1599 LDLM_LOCK_RELEASE(lock);
1604 int ldlm_run_ast_work(struct ldlm_namespace *ns, cfs_list_t *rpc_list,
1605 ldlm_desc_ast_t ast_type)
1607 struct l_wait_info lwi = { 0 };
1608 struct ldlm_cb_set_arg *arg;
1609 cfs_list_t *tmp, *pos;
1610 int (*work_ast_lock)(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg);
1611 unsigned int max_ast_count;
1615 if (cfs_list_empty(rpc_list))
1622 cfs_atomic_set(&arg->restart, 0);
1623 cfs_atomic_set(&arg->rpcs, 0);
1624 cfs_atomic_set(&arg->refcount, 1);
1625 cfs_waitq_init(&arg->waitq);
1628 case LDLM_WORK_BL_AST:
1629 arg->type = LDLM_BL_CALLBACK;
1630 work_ast_lock = ldlm_work_bl_ast_lock;
1632 case LDLM_WORK_CP_AST:
1633 arg->type = LDLM_CP_CALLBACK;
1634 work_ast_lock = ldlm_work_cp_ast_lock;
1636 case LDLM_WORK_REVOKE_AST:
1637 arg->type = LDLM_BL_CALLBACK;
1638 work_ast_lock = ldlm_work_revoke_ast_lock;
1644 max_ast_count = ns->ns_max_parallel_ast ? : UINT_MAX;
1645 arg->threshold = max_ast_count;
1647 cfs_list_for_each_safe(tmp, pos, rpc_list) {
1648 (void)work_ast_lock(tmp, arg);
1649 if (cfs_atomic_read(&arg->rpcs) < max_ast_count)
1652 l_wait_event(arg->waitq,
1653 cfs_atomic_read(&arg->rpcs) < arg->threshold,
1658 l_wait_event(arg->waitq, cfs_atomic_read(&arg->rpcs) == 0, &lwi);
1660 rc = cfs_atomic_read(&arg->restart) ? -ERESTART : 0;
1665 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1667 ldlm_reprocess_all(res);
1668 return LDLM_ITER_CONTINUE;
1671 static int ldlm_reprocess_res(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1672 cfs_hlist_node_t *hnode, void *arg)
1674 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1677 rc = reprocess_one_queue(res, arg);
1679 return rc == LDLM_ITER_STOP;
1682 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1687 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1688 ldlm_reprocess_res, NULL);
1693 void ldlm_reprocess_all(struct ldlm_resource *res)
1695 CFS_LIST_HEAD(rpc_list);
1697 #ifdef HAVE_SERVER_SUPPORT
1700 /* Local lock trees don't get reprocessed. */
1701 if (ns_is_client(ldlm_res_to_ns(res))) {
1708 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1709 if (rc == LDLM_ITER_CONTINUE)
1710 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1713 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
1715 if (rc == -ERESTART) {
1716 LASSERT(cfs_list_empty(&rpc_list));
1721 if (!ns_is_client(ldlm_res_to_ns(res))) {
1722 CERROR("This is client-side-only module, cannot handle "
1723 "LDLM_NAMESPACE_SERVER resource type lock.\n");
1730 void ldlm_cancel_callback(struct ldlm_lock *lock)
1732 check_res_locked(lock->l_resource);
1733 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1734 lock->l_flags |= LDLM_FL_CANCEL;
1735 if (lock->l_blocking_ast) {
1736 // l_check_no_ns_lock(ns);
1737 unlock_res_and_lock(lock);
1738 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1740 lock_res_and_lock(lock);
1742 LDLM_DEBUG(lock, "no blocking ast");
1745 lock->l_flags |= LDLM_FL_BL_DONE;
1748 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1750 if (req->l_resource->lr_type != LDLM_PLAIN &&
1751 req->l_resource->lr_type != LDLM_IBITS)
1754 cfs_list_del_init(&req->l_sl_policy);
1755 cfs_list_del_init(&req->l_sl_mode);
1758 void ldlm_lock_cancel(struct ldlm_lock *lock)
1760 struct ldlm_resource *res;
1761 struct ldlm_namespace *ns;
1764 lock_res_and_lock(lock);
1766 res = lock->l_resource;
1767 ns = ldlm_res_to_ns(res);
1769 /* Please do not, no matter how tempting, remove this LBUG without
1770 * talking to me first. -phik */
1771 if (lock->l_readers || lock->l_writers) {
1772 LDLM_ERROR(lock, "lock still has references");
1776 ldlm_del_waiting_lock(lock);
1778 /* Releases cancel callback. */
1779 ldlm_cancel_callback(lock);
1781 /* Yes, second time, just in case it was added again while we were
1782 running with no res lock in ldlm_cancel_callback */
1783 ldlm_del_waiting_lock(lock);
1784 ldlm_resource_unlink_lock(lock);
1785 ldlm_lock_destroy_nolock(lock);
1787 if (lock->l_granted_mode == lock->l_req_mode)
1788 ldlm_pool_del(&ns->ns_pool, lock);
1790 /* Make sure we will not be called again for same lock what is possible
1791 * if not to zero out lock->l_granted_mode */
1792 lock->l_granted_mode = LCK_MINMODE;
1793 unlock_res_and_lock(lock);
1798 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1800 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1805 if (lock->l_ast_data == NULL)
1806 lock->l_ast_data = data;
1807 if (lock->l_ast_data == data)
1809 LDLM_LOCK_PUT(lock);
1813 EXPORT_SYMBOL(ldlm_lock_set_data);
1815 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1816 cfs_hlist_node_t *hnode, void *data)
1819 struct obd_export *exp = data;
1820 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
1821 struct ldlm_resource *res;
1823 res = ldlm_resource_getref(lock->l_resource);
1824 LDLM_LOCK_GET(lock);
1826 LDLM_DEBUG(lock, "export %p", exp);
1827 ldlm_res_lvbo_update(res, NULL, 1);
1828 ldlm_lock_cancel(lock);
1829 ldlm_reprocess_all(res);
1830 ldlm_resource_putref(res);
1831 LDLM_LOCK_RELEASE(lock);
1835 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1837 cfs_hash_for_each_empty(exp->exp_lock_hash,
1838 ldlm_cancel_locks_for_export_cb, exp);
1842 * Downgrade an exclusive lock.
1844 * A fast variant of ldlm_lock_convert for convertion of exclusive
1845 * locks. The convertion is always successful.
1847 * \param lock A lock to convert
1848 * \param new_mode new lock mode
1850 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1854 LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1855 LASSERT(new_mode == LCK_COS);
1857 lock_res_and_lock(lock);
1858 ldlm_resource_unlink_lock(lock);
1860 * Remove the lock from pool as it will be added again in
1861 * ldlm_grant_lock() called below.
1863 ldlm_pool_del(&ldlm_lock_to_ns(lock)->ns_pool, lock);
1865 lock->l_req_mode = new_mode;
1866 ldlm_grant_lock(lock, NULL);
1867 unlock_res_and_lock(lock);
1868 ldlm_reprocess_all(lock->l_resource);
1873 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1876 CFS_LIST_HEAD(rpc_list);
1877 struct ldlm_resource *res;
1878 struct ldlm_namespace *ns;
1881 struct sl_insert_point prev;
1882 struct ldlm_interval *node;
1885 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1886 *flags |= LDLM_FL_BLOCK_GRANTED;
1887 RETURN(lock->l_resource);
1890 /* I can't check the type of lock here because the bitlock of lock
1891 * is not held here, so do the allocation blindly. -jay */
1892 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1893 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1896 LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1897 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1899 lock_res_and_lock(lock);
1901 res = lock->l_resource;
1902 ns = ldlm_res_to_ns(res);
1904 old_mode = lock->l_req_mode;
1905 lock->l_req_mode = new_mode;
1906 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1907 /* remember the lock position where the lock might be
1908 * added back to the granted list later and also
1909 * remember the join mode for skiplist fixing. */
1910 prev.res_link = lock->l_res_link.prev;
1911 prev.mode_link = lock->l_sl_mode.prev;
1912 prev.policy_link = lock->l_sl_policy.prev;
1913 ldlm_resource_unlink_lock(lock);
1915 ldlm_resource_unlink_lock(lock);
1916 if (res->lr_type == LDLM_EXTENT) {
1917 /* FIXME: ugly code, I have to attach the lock to a
1918 * interval node again since perhaps it will be granted
1920 CFS_INIT_LIST_HEAD(&node->li_group);
1921 ldlm_interval_attach(node, lock);
1927 * Remove old lock from the pool before adding the lock with new
1928 * mode below in ->policy()
1930 ldlm_pool_del(&ns->ns_pool, lock);
1932 /* If this is a local resource, put it on the appropriate list. */
1933 if (ns_is_client(ldlm_res_to_ns(res))) {
1934 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1935 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1937 /* This should never happen, because of the way the
1938 * server handles conversions. */
1939 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1943 ldlm_grant_lock(lock, &rpc_list);
1945 /* FIXME: completion handling not with lr_lock held ! */
1946 if (lock->l_completion_ast)
1947 lock->l_completion_ast(lock, 0, NULL);
1949 #ifdef HAVE_SERVER_SUPPORT
1954 ldlm_processing_policy policy;
1955 policy = ldlm_processing_policy_table[res->lr_type];
1956 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1957 if (rc == LDLM_ITER_STOP) {
1958 lock->l_req_mode = old_mode;
1959 if (res->lr_type == LDLM_EXTENT)
1960 ldlm_extent_add_lock(res, lock);
1962 ldlm_granted_list_add_lock(lock, &prev);
1966 *flags |= LDLM_FL_BLOCK_GRANTED;
1972 CERROR("This is client-side-only module, cannot handle "
1973 "LDLM_NAMESPACE_SERVER resource type lock.\n");
1977 unlock_res_and_lock(lock);
1980 ldlm_run_ast_work(ns, &rpc_list, LDLM_WORK_CP_AST);
1982 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1986 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1988 struct ldlm_lock *lock;
1990 if (!((libcfs_debug | D_ERROR) & level))
1993 lock = ldlm_handle2lock(lockh);
1997 LDLM_DEBUG_LIMIT(level, lock, "###");
1999 LDLM_LOCK_PUT(lock);
2002 void _ldlm_lock_debug(struct ldlm_lock *lock,
2003 struct libcfs_debug_msg_data *msgdata,
2004 const char *fmt, ...)
2007 struct obd_export *exp = lock->l_export;
2008 struct ldlm_resource *resource = lock->l_resource;
2009 char *nid = "local";
2011 va_start(args, fmt);
2013 if (exp && exp->exp_connection) {
2014 nid = libcfs_nid2str(exp->exp_connection->c_peer.nid);
2015 } else if (exp && exp->exp_obd != NULL) {
2016 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
2017 nid = libcfs_nid2str(imp->imp_connection->c_peer.nid);
2020 if (resource == NULL) {
2021 libcfs_debug_vmsg2(msgdata, fmt, args,
2022 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2023 "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" nid: %s "
2024 "remote: "LPX64" expref: %d pid: %u timeout: %lu\n",
2026 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2027 lock->l_readers, lock->l_writers,
2028 ldlm_lockname[lock->l_granted_mode],
2029 ldlm_lockname[lock->l_req_mode],
2030 lock->l_flags, nid, lock->l_remote_handle.cookie,
2031 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2032 lock->l_pid, lock->l_callback_timeout);
2037 switch (resource->lr_type) {
2039 libcfs_debug_vmsg2(msgdata, fmt, args,
2040 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2041 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
2042 "] (req "LPU64"->"LPU64") flags: "LPX64" nid: %s remote:"
2043 " "LPX64" expref: %d pid: %u timeout %lu\n",
2044 ldlm_lock_to_ns_name(lock), lock,
2045 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2046 lock->l_readers, lock->l_writers,
2047 ldlm_lockname[lock->l_granted_mode],
2048 ldlm_lockname[lock->l_req_mode],
2049 resource->lr_name.name[0],
2050 resource->lr_name.name[1],
2051 cfs_atomic_read(&resource->lr_refcount),
2052 ldlm_typename[resource->lr_type],
2053 lock->l_policy_data.l_extent.start,
2054 lock->l_policy_data.l_extent.end,
2055 lock->l_req_extent.start, lock->l_req_extent.end,
2056 lock->l_flags, nid, lock->l_remote_handle.cookie,
2057 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2058 lock->l_pid, lock->l_callback_timeout);
2062 libcfs_debug_vmsg2(msgdata, fmt, args,
2063 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2064 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
2065 "["LPU64"->"LPU64"] flags: "LPX64" nid: %s remote: "LPX64
2066 " expref: %d pid: %u timeout: %lu\n",
2067 ldlm_lock_to_ns_name(lock), lock,
2068 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2069 lock->l_readers, lock->l_writers,
2070 ldlm_lockname[lock->l_granted_mode],
2071 ldlm_lockname[lock->l_req_mode],
2072 resource->lr_name.name[0],
2073 resource->lr_name.name[1],
2074 cfs_atomic_read(&resource->lr_refcount),
2075 ldlm_typename[resource->lr_type],
2076 lock->l_policy_data.l_flock.pid,
2077 lock->l_policy_data.l_flock.start,
2078 lock->l_policy_data.l_flock.end,
2079 lock->l_flags, nid, lock->l_remote_handle.cookie,
2080 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2081 lock->l_pid, lock->l_callback_timeout);
2085 libcfs_debug_vmsg2(msgdata, fmt, args,
2086 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2087 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
2088 "flags: "LPX64" nid: %s remote: "LPX64" expref: %d "
2089 "pid: %u timeout: %lu\n",
2090 ldlm_lock_to_ns_name(lock),
2091 lock, lock->l_handle.h_cookie,
2092 cfs_atomic_read (&lock->l_refc),
2093 lock->l_readers, lock->l_writers,
2094 ldlm_lockname[lock->l_granted_mode],
2095 ldlm_lockname[lock->l_req_mode],
2096 resource->lr_name.name[0],
2097 resource->lr_name.name[1],
2098 lock->l_policy_data.l_inodebits.bits,
2099 cfs_atomic_read(&resource->lr_refcount),
2100 ldlm_typename[resource->lr_type],
2101 lock->l_flags, nid, lock->l_remote_handle.cookie,
2102 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2103 lock->l_pid, lock->l_callback_timeout);
2107 libcfs_debug_vmsg2(msgdata, fmt, args,
2108 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2109 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2110 "nid: %s remote: "LPX64" expref: %d pid: %u timeout %lu"
2112 ldlm_lock_to_ns_name(lock),
2113 lock, lock->l_handle.h_cookie,
2114 cfs_atomic_read (&lock->l_refc),
2115 lock->l_readers, lock->l_writers,
2116 ldlm_lockname[lock->l_granted_mode],
2117 ldlm_lockname[lock->l_req_mode],
2118 resource->lr_name.name[0],
2119 resource->lr_name.name[1],
2120 cfs_atomic_read(&resource->lr_refcount),
2121 ldlm_typename[resource->lr_type],
2122 lock->l_flags, nid, lock->l_remote_handle.cookie,
2123 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2124 lock->l_pid, lock->l_callback_timeout);
2129 EXPORT_SYMBOL(_ldlm_lock_debug);