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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ldlm/ldlm_lock.c
34 * Author: Peter Braam <braam@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_LDLM
40 #include "../../include/linux/libcfs/libcfs.h"
41 #include "../include/lustre_intent.h"
42 #include "../include/lustre_swab.h"
43 #include "../include/obd_class.h"
44 #include "ldlm_internal.h"
47 char *ldlm_lockname[] = {
55 [LCK_GROUP] = "GROUP",
58 EXPORT_SYMBOL(ldlm_lockname);
60 static char *ldlm_typename[] = {
62 [LDLM_EXTENT] = "EXT",
67 static ldlm_policy_wire_to_local_t ldlm_policy_wire_to_local[] = {
68 [LDLM_PLAIN - LDLM_MIN_TYPE] = ldlm_plain_policy_wire_to_local,
69 [LDLM_EXTENT - LDLM_MIN_TYPE] = ldlm_extent_policy_wire_to_local,
70 [LDLM_FLOCK - LDLM_MIN_TYPE] = ldlm_flock_policy_wire_to_local,
71 [LDLM_IBITS - LDLM_MIN_TYPE] = ldlm_ibits_policy_wire_to_local,
74 static ldlm_policy_local_to_wire_t ldlm_policy_local_to_wire[] = {
75 [LDLM_PLAIN - LDLM_MIN_TYPE] = ldlm_plain_policy_local_to_wire,
76 [LDLM_EXTENT - LDLM_MIN_TYPE] = ldlm_extent_policy_local_to_wire,
77 [LDLM_FLOCK - LDLM_MIN_TYPE] = ldlm_flock_policy_local_to_wire,
78 [LDLM_IBITS - LDLM_MIN_TYPE] = ldlm_ibits_policy_local_to_wire,
82 * Converts lock policy from local format to on the wire lock_desc format
84 static void ldlm_convert_policy_to_wire(enum ldlm_type type,
85 const union ldlm_policy_data *lpolicy,
86 union ldlm_wire_policy_data *wpolicy)
88 ldlm_policy_local_to_wire_t convert;
90 convert = ldlm_policy_local_to_wire[type - LDLM_MIN_TYPE];
92 convert(lpolicy, wpolicy);
96 * Converts lock policy from on the wire lock_desc format to local format
98 void ldlm_convert_policy_to_local(struct obd_export *exp, enum ldlm_type type,
99 const union ldlm_wire_policy_data *wpolicy,
100 union ldlm_policy_data *lpolicy)
102 ldlm_policy_wire_to_local_t convert;
104 convert = ldlm_policy_wire_to_local[type - LDLM_MIN_TYPE];
106 convert(wpolicy, lpolicy);
109 const char *ldlm_it2str(enum ldlm_intent_flags it)
116 case (IT_OPEN | IT_CREAT):
131 CERROR("Unknown intent 0x%08x\n", it);
135 EXPORT_SYMBOL(ldlm_it2str);
138 * REFCOUNTED LOCK OBJECTS
142 * Get a reference on a lock.
144 * Lock refcounts, during creation:
145 * - one special one for allocation, dec'd only once in destroy
146 * - one for being a lock that's in-use
147 * - one for the addref associated with a new lock
149 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
151 atomic_inc(&lock->l_refc);
154 EXPORT_SYMBOL(ldlm_lock_get);
157 * Release lock reference.
159 * Also frees the lock if it was last reference.
161 void ldlm_lock_put(struct ldlm_lock *lock)
163 LASSERT(lock->l_resource != LP_POISON);
164 LASSERT(atomic_read(&lock->l_refc) > 0);
165 if (atomic_dec_and_test(&lock->l_refc)) {
166 struct ldlm_resource *res;
169 "final lock_put on destroyed lock, freeing it.");
171 res = lock->l_resource;
172 LASSERT(ldlm_is_destroyed(lock));
173 LASSERT(list_empty(&lock->l_res_link));
174 LASSERT(list_empty(&lock->l_pending_chain));
176 lprocfs_counter_decr(ldlm_res_to_ns(res)->ns_stats,
178 lu_ref_del(&res->lr_reference, "lock", lock);
179 ldlm_resource_putref(res);
180 lock->l_resource = NULL;
181 if (lock->l_export) {
182 class_export_lock_put(lock->l_export, lock);
183 lock->l_export = NULL;
186 kfree(lock->l_lvb_data);
188 ldlm_interval_free(ldlm_interval_detach(lock));
189 lu_ref_fini(&lock->l_reference);
190 OBD_FREE_RCU(lock, sizeof(*lock), &lock->l_handle);
193 EXPORT_SYMBOL(ldlm_lock_put);
196 * Removes LDLM lock \a lock from LRU. Assumes LRU is already locked.
198 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
202 if (!list_empty(&lock->l_lru)) {
203 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
205 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
206 list_del_init(&lock->l_lru);
207 LASSERT(ns->ns_nr_unused > 0);
215 * Removes LDLM lock \a lock from LRU. Obtains the LRU lock first.
217 * If \a last_use is non-zero, it will remove the lock from LRU only if
218 * it matches lock's l_last_used.
220 * \retval 0 if \a last_use is set, the lock is not in LRU list or \a last_use
221 * doesn't match lock's l_last_used;
222 * otherwise, the lock hasn't been in the LRU list.
223 * \retval 1 the lock was in LRU list and removed.
225 int ldlm_lock_remove_from_lru_check(struct ldlm_lock *lock, time_t last_use)
227 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
230 spin_lock(&ns->ns_lock);
231 if (last_use == 0 || last_use == lock->l_last_used)
232 rc = ldlm_lock_remove_from_lru_nolock(lock);
233 spin_unlock(&ns->ns_lock);
239 * Adds LDLM lock \a lock to namespace LRU. Assumes LRU is already locked.
241 static void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
243 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
245 lock->l_last_used = cfs_time_current();
246 LASSERT(list_empty(&lock->l_lru));
247 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
248 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
249 ldlm_clear_skipped(lock);
250 LASSERT(ns->ns_nr_unused >= 0);
255 * Adds LDLM lock \a lock to namespace LRU. Obtains necessary LRU locks
258 static void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
260 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
262 spin_lock(&ns->ns_lock);
263 ldlm_lock_add_to_lru_nolock(lock);
264 spin_unlock(&ns->ns_lock);
268 * Moves LDLM lock \a lock that is already in namespace LRU to the tail of
269 * the LRU. Performs necessary LRU locking
271 static void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
273 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
275 spin_lock(&ns->ns_lock);
276 if (!list_empty(&lock->l_lru)) {
277 ldlm_lock_remove_from_lru_nolock(lock);
278 ldlm_lock_add_to_lru_nolock(lock);
280 spin_unlock(&ns->ns_lock);
284 * Helper to destroy a locked lock.
286 * Used by ldlm_lock_destroy and ldlm_lock_destroy_nolock
287 * Must be called with l_lock and lr_lock held.
289 * Does not actually free the lock data, but rather marks the lock as
290 * destroyed by setting l_destroyed field in the lock to 1. Destroys a
291 * handle->lock association too, so that the lock can no longer be found
292 * and removes the lock from LRU list. Actual lock freeing occurs when
293 * last lock reference goes away.
295 * Original comment (of some historical value):
296 * This used to have a 'strict' flag, which recovery would use to mark an
297 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
298 * shall explain why it's gone: with the new hash table scheme, once you call
299 * ldlm_lock_destroy, you can never drop your final references on this lock.
300 * Because it's not in the hash table anymore. -phil
302 static int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
304 if (lock->l_readers || lock->l_writers) {
305 LDLM_ERROR(lock, "lock still has references");
309 if (!list_empty(&lock->l_res_link)) {
310 LDLM_ERROR(lock, "lock still on resource");
314 if (ldlm_is_destroyed(lock)) {
315 LASSERT(list_empty(&lock->l_lru));
318 ldlm_set_destroyed(lock);
320 if (lock->l_export && lock->l_export->exp_lock_hash) {
321 /* NB: it's safe to call cfs_hash_del() even lock isn't
324 /* In the function below, .hs_keycmp resolves to
325 * ldlm_export_lock_keycmp()
327 /* coverity[overrun-buffer-val] */
328 cfs_hash_del(lock->l_export->exp_lock_hash,
329 &lock->l_remote_handle, &lock->l_exp_hash);
332 ldlm_lock_remove_from_lru(lock);
333 class_handle_unhash(&lock->l_handle);
339 * Destroys a LDLM lock \a lock. Performs necessary locking first.
341 static void ldlm_lock_destroy(struct ldlm_lock *lock)
345 lock_res_and_lock(lock);
346 first = ldlm_lock_destroy_internal(lock);
347 unlock_res_and_lock(lock);
349 /* drop reference from hashtable only for first destroy */
351 lu_ref_del(&lock->l_reference, "hash", lock);
352 LDLM_LOCK_RELEASE(lock);
357 * Destroys a LDLM lock \a lock that is already locked.
359 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
363 first = ldlm_lock_destroy_internal(lock);
364 /* drop reference from hashtable only for first destroy */
366 lu_ref_del(&lock->l_reference, "hash", lock);
367 LDLM_LOCK_RELEASE(lock);
371 /* this is called by portals_handle2object with the handle lock taken */
372 static void lock_handle_addref(void *lock)
374 LDLM_LOCK_GET((struct ldlm_lock *)lock);
377 static void lock_handle_free(void *lock, int size)
379 LASSERT(size == sizeof(struct ldlm_lock));
380 kmem_cache_free(ldlm_lock_slab, lock);
383 static struct portals_handle_ops lock_handle_ops = {
384 .hop_addref = lock_handle_addref,
385 .hop_free = lock_handle_free,
390 * Allocate and initialize new lock structure.
392 * usage: pass in a resource on which you have done ldlm_resource_get
393 * new lock will take over the refcount.
394 * returns: lock with refcount 2 - one for current caller and one for remote
396 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
398 struct ldlm_lock *lock;
402 lock = kmem_cache_zalloc(ldlm_lock_slab, GFP_NOFS);
406 spin_lock_init(&lock->l_lock);
407 lock->l_resource = resource;
408 lu_ref_add(&resource->lr_reference, "lock", lock);
410 atomic_set(&lock->l_refc, 2);
411 INIT_LIST_HEAD(&lock->l_res_link);
412 INIT_LIST_HEAD(&lock->l_lru);
413 INIT_LIST_HEAD(&lock->l_pending_chain);
414 INIT_LIST_HEAD(&lock->l_bl_ast);
415 INIT_LIST_HEAD(&lock->l_cp_ast);
416 INIT_LIST_HEAD(&lock->l_rk_ast);
417 init_waitqueue_head(&lock->l_waitq);
418 lock->l_blocking_lock = NULL;
419 INIT_LIST_HEAD(&lock->l_sl_mode);
420 INIT_LIST_HEAD(&lock->l_sl_policy);
421 INIT_HLIST_NODE(&lock->l_exp_hash);
422 INIT_HLIST_NODE(&lock->l_exp_flock_hash);
424 lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
426 INIT_LIST_HEAD(&lock->l_handle.h_link);
427 class_handle_hash(&lock->l_handle, &lock_handle_ops);
429 lu_ref_init(&lock->l_reference);
430 lu_ref_add(&lock->l_reference, "hash", lock);
431 lock->l_callback_timeout = 0;
433 #if LUSTRE_TRACKS_LOCK_EXP_REFS
434 INIT_LIST_HEAD(&lock->l_exp_refs_link);
435 lock->l_exp_refs_nr = 0;
436 lock->l_exp_refs_target = NULL;
438 INIT_LIST_HEAD(&lock->l_exp_list);
444 * Moves LDLM lock \a lock to another resource.
445 * This is used on client when server returns some other lock than requested
446 * (typically as a result of intent operation)
448 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
449 const struct ldlm_res_id *new_resid)
451 struct ldlm_resource *oldres = lock->l_resource;
452 struct ldlm_resource *newres;
455 lock_res_and_lock(lock);
456 if (memcmp(new_resid, &lock->l_resource->lr_name,
457 sizeof(lock->l_resource->lr_name)) == 0) {
459 unlock_res_and_lock(lock);
463 LASSERT(new_resid->name[0] != 0);
465 /* This function assumes that the lock isn't on any lists */
466 LASSERT(list_empty(&lock->l_res_link));
468 type = oldres->lr_type;
469 unlock_res_and_lock(lock);
471 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
473 return PTR_ERR(newres);
475 lu_ref_add(&newres->lr_reference, "lock", lock);
477 * To flip the lock from the old to the new resource, lock, oldres and
478 * newres have to be locked. Resource spin-locks are nested within
479 * lock->l_lock, and are taken in the memory address order to avoid
482 spin_lock(&lock->l_lock);
483 oldres = lock->l_resource;
484 if (oldres < newres) {
486 lock_res_nested(newres, LRT_NEW);
489 lock_res_nested(oldres, LRT_NEW);
491 LASSERT(memcmp(new_resid, &oldres->lr_name,
492 sizeof(oldres->lr_name)) != 0);
493 lock->l_resource = newres;
495 unlock_res_and_lock(lock);
497 /* ...and the flowers are still standing! */
498 lu_ref_del(&oldres->lr_reference, "lock", lock);
499 ldlm_resource_putref(oldres);
504 /** \defgroup ldlm_handles LDLM HANDLES
505 * Ways to get hold of locks without any addresses.
510 * Fills in handle for LDLM lock \a lock into supplied \a lockh
511 * Does not take any references.
513 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
515 lockh->cookie = lock->l_handle.h_cookie;
517 EXPORT_SYMBOL(ldlm_lock2handle);
520 * Obtain a lock reference by handle.
522 * if \a flags: atomically get the lock and set the flags.
523 * Return NULL if flag already set
525 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
528 struct ldlm_lock *lock;
532 lock = class_handle2object(handle->cookie, NULL);
536 /* It's unlikely but possible that someone marked the lock as
537 * destroyed after we did handle2object on it
539 if (flags == 0 && !ldlm_is_destroyed(lock)) {
540 lu_ref_add(&lock->l_reference, "handle", current);
544 lock_res_and_lock(lock);
546 LASSERT(lock->l_resource);
548 lu_ref_add_atomic(&lock->l_reference, "handle", current);
549 if (unlikely(ldlm_is_destroyed(lock))) {
550 unlock_res_and_lock(lock);
551 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
557 if (lock->l_flags & flags) {
558 unlock_res_and_lock(lock);
563 lock->l_flags |= flags;
566 unlock_res_and_lock(lock);
569 EXPORT_SYMBOL(__ldlm_handle2lock);
570 /** @} ldlm_handles */
573 * Fill in "on the wire" representation for given LDLM lock into supplied
574 * lock descriptor \a desc structure.
576 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
578 ldlm_res2desc(lock->l_resource, &desc->l_resource);
579 desc->l_req_mode = lock->l_req_mode;
580 desc->l_granted_mode = lock->l_granted_mode;
581 ldlm_convert_policy_to_wire(lock->l_resource->lr_type,
582 &lock->l_policy_data,
583 &desc->l_policy_data);
587 * Add a lock to list of conflicting locks to send AST to.
589 * Only add if we have not sent a blocking AST to the lock yet.
591 static void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
592 struct list_head *work_list)
594 if (!ldlm_is_ast_sent(lock)) {
595 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
596 ldlm_set_ast_sent(lock);
597 /* If the enqueuing client said so, tell the AST recipient to
598 * discard dirty data, rather than writing back.
600 if (ldlm_is_ast_discard_data(new))
601 ldlm_set_discard_data(lock);
602 LASSERT(list_empty(&lock->l_bl_ast));
603 list_add(&lock->l_bl_ast, work_list);
605 LASSERT(!lock->l_blocking_lock);
606 lock->l_blocking_lock = LDLM_LOCK_GET(new);
611 * Add a lock to list of just granted locks to send completion AST to.
613 static void ldlm_add_cp_work_item(struct ldlm_lock *lock,
614 struct list_head *work_list)
616 if (!ldlm_is_cp_reqd(lock)) {
617 ldlm_set_cp_reqd(lock);
618 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
619 LASSERT(list_empty(&lock->l_cp_ast));
620 list_add(&lock->l_cp_ast, work_list);
626 * Aggregator function to add AST work items into a list. Determines
627 * what sort of an AST work needs to be done and calls the proper
629 * Must be called with lr_lock held.
631 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
632 struct ldlm_lock *new,
633 struct list_head *work_list)
635 check_res_locked(lock->l_resource);
637 ldlm_add_bl_work_item(lock, new, work_list);
639 ldlm_add_cp_work_item(lock, work_list);
643 * Add specified reader/writer reference to LDLM lock with handle \a lockh.
644 * r/w reference type is determined by \a mode
645 * Calls ldlm_lock_addref_internal.
647 void ldlm_lock_addref(const struct lustre_handle *lockh, enum ldlm_mode mode)
649 struct ldlm_lock *lock;
651 lock = ldlm_handle2lock(lockh);
652 LASSERTF(lock, "Non-existing lock: %llx\n", lockh->cookie);
653 ldlm_lock_addref_internal(lock, mode);
656 EXPORT_SYMBOL(ldlm_lock_addref);
660 * Add specified reader/writer reference to LDLM lock \a lock.
661 * r/w reference type is determined by \a mode
662 * Removes lock from LRU if it is there.
663 * Assumes the LDLM lock is already locked.
665 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock,
668 ldlm_lock_remove_from_lru(lock);
669 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
671 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
673 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
675 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
678 lu_ref_add_atomic(&lock->l_reference, "user", lock);
679 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
683 * Attempts to add reader/writer reference to a lock with handle \a lockh, and
684 * fails if lock is already LDLM_FL_CBPENDING or destroyed.
686 * \retval 0 success, lock was addref-ed
688 * \retval -EAGAIN lock is being canceled.
690 int ldlm_lock_addref_try(const struct lustre_handle *lockh, enum ldlm_mode mode)
692 struct ldlm_lock *lock;
696 lock = ldlm_handle2lock(lockh);
698 lock_res_and_lock(lock);
699 if (lock->l_readers != 0 || lock->l_writers != 0 ||
700 !ldlm_is_cbpending(lock)) {
701 ldlm_lock_addref_internal_nolock(lock, mode);
704 unlock_res_and_lock(lock);
709 EXPORT_SYMBOL(ldlm_lock_addref_try);
712 * Add specified reader/writer reference to LDLM lock \a lock.
713 * Locks LDLM lock and calls ldlm_lock_addref_internal_nolock to do the work.
714 * Only called for local locks.
716 void ldlm_lock_addref_internal(struct ldlm_lock *lock, enum ldlm_mode mode)
718 lock_res_and_lock(lock);
719 ldlm_lock_addref_internal_nolock(lock, mode);
720 unlock_res_and_lock(lock);
724 * Removes reader/writer reference for LDLM lock \a lock.
725 * Assumes LDLM lock is already locked.
726 * only called in ldlm_flock_destroy and for local locks.
727 * Does NOT add lock to LRU if no r/w references left to accommodate flock locks
728 * that cannot be placed in LRU.
730 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock,
733 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
734 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
735 LASSERT(lock->l_readers > 0);
736 lu_ref_del(&lock->l_reference, "reader", lock);
739 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
740 LASSERT(lock->l_writers > 0);
741 lu_ref_del(&lock->l_reference, "writer", lock);
745 lu_ref_del(&lock->l_reference, "user", lock);
746 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
750 * Removes reader/writer reference for LDLM lock \a lock.
751 * Locks LDLM lock first.
752 * If the lock is determined to be client lock on a client and r/w refcount
753 * drops to zero and the lock is not blocked, the lock is added to LRU lock
755 * For blocked LDLM locks if r/w count drops to zero, blocking_ast is called.
757 void ldlm_lock_decref_internal(struct ldlm_lock *lock, enum ldlm_mode mode)
759 struct ldlm_namespace *ns;
761 lock_res_and_lock(lock);
763 ns = ldlm_lock_to_ns(lock);
765 ldlm_lock_decref_internal_nolock(lock, mode);
767 if (ldlm_is_local(lock) &&
768 !lock->l_readers && !lock->l_writers) {
769 /* If this is a local lock on a server namespace and this was
770 * the last reference, cancel the lock.
772 CDEBUG(D_INFO, "forcing cancel of local lock\n");
773 ldlm_set_cbpending(lock);
776 if (!lock->l_readers && !lock->l_writers &&
777 (ldlm_is_cbpending(lock) || lock->l_req_mode == LCK_GROUP)) {
778 /* If we received a blocked AST and this was the last reference,
780 * Group locks are special:
781 * They must not go in LRU, but they are not called back
782 * like non-group locks, instead they are manually released.
783 * They have an l_writers reference which they keep until
784 * they are manually released, so we remove them when they have
785 * no more reader or writer references. - LU-6368
787 LDLM_DEBUG(lock, "final decref done on cbpending lock");
789 LDLM_LOCK_GET(lock); /* dropped by bl thread */
790 ldlm_lock_remove_from_lru(lock);
791 unlock_res_and_lock(lock);
793 if (ldlm_is_fail_loc(lock))
794 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
796 if (ldlm_is_atomic_cb(lock) ||
797 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
798 ldlm_handle_bl_callback(ns, NULL, lock);
799 } else if (!lock->l_readers && !lock->l_writers &&
800 !ldlm_is_no_lru(lock) && !ldlm_is_bl_ast(lock)) {
801 LDLM_DEBUG(lock, "add lock into lru list");
803 /* If this is a client-side namespace and this was the last
804 * reference, put it on the LRU.
806 ldlm_lock_add_to_lru(lock);
807 unlock_res_and_lock(lock);
809 if (ldlm_is_fail_loc(lock))
810 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
812 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
813 * are not supported by the server, otherwise, it is done on
816 if (!exp_connect_cancelset(lock->l_conn_export) &&
817 !ns_connect_lru_resize(ns))
818 ldlm_cancel_lru(ns, 0, LCF_ASYNC, 0);
820 LDLM_DEBUG(lock, "do not add lock into lru list");
821 unlock_res_and_lock(lock);
826 * Decrease reader/writer refcount for LDLM lock with handle \a lockh
828 void ldlm_lock_decref(const struct lustre_handle *lockh, enum ldlm_mode mode)
830 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
832 LASSERTF(lock, "Non-existing lock: %#llx\n", lockh->cookie);
833 ldlm_lock_decref_internal(lock, mode);
836 EXPORT_SYMBOL(ldlm_lock_decref);
839 * Decrease reader/writer refcount for LDLM lock with handle
840 * \a lockh and mark it for subsequent cancellation once r/w refcount
841 * drops to zero instead of putting into LRU.
843 void ldlm_lock_decref_and_cancel(const struct lustre_handle *lockh,
846 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
850 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
851 lock_res_and_lock(lock);
852 ldlm_set_cbpending(lock);
853 unlock_res_and_lock(lock);
854 ldlm_lock_decref_internal(lock, mode);
857 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
859 struct sl_insert_point {
860 struct list_head *res_link;
861 struct list_head *mode_link;
862 struct list_head *policy_link;
866 * Finds a position to insert the new lock into granted lock list.
868 * Used for locks eligible for skiplist optimization.
871 * queue [input]: the granted list where search acts on;
872 * req [input]: the lock whose position to be located;
873 * prev [output]: positions within 3 lists to insert @req to
877 * - ldlm_grant_lock_with_skiplist
879 static void search_granted_lock(struct list_head *queue,
880 struct ldlm_lock *req,
881 struct sl_insert_point *prev)
883 struct list_head *tmp;
884 struct ldlm_lock *lock, *mode_end, *policy_end;
886 list_for_each(tmp, queue) {
887 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
889 mode_end = list_prev_entry(lock, l_sl_mode);
891 if (lock->l_req_mode != req->l_req_mode) {
892 /* jump to last lock of mode group */
893 tmp = &mode_end->l_res_link;
897 /* suitable mode group is found */
898 if (lock->l_resource->lr_type == LDLM_PLAIN) {
899 /* insert point is last lock of the mode group */
900 prev->res_link = &mode_end->l_res_link;
901 prev->mode_link = &mode_end->l_sl_mode;
902 prev->policy_link = &req->l_sl_policy;
906 if (lock->l_resource->lr_type == LDLM_IBITS) {
909 list_prev_entry(lock, l_sl_policy);
911 if (lock->l_policy_data.l_inodebits.bits ==
912 req->l_policy_data.l_inodebits.bits) {
913 /* insert point is last lock of
917 &policy_end->l_res_link;
919 &policy_end->l_sl_mode;
921 &policy_end->l_sl_policy;
925 if (policy_end == mode_end)
926 /* done with mode group */
929 /* go to next policy group within mode group */
930 tmp = policy_end->l_res_link.next;
931 lock = list_entry(tmp, struct ldlm_lock,
933 } /* loop over policy groups within the mode group */
935 /* insert point is last lock of the mode group,
936 * new policy group is started
938 prev->res_link = &mode_end->l_res_link;
939 prev->mode_link = &mode_end->l_sl_mode;
940 prev->policy_link = &req->l_sl_policy;
944 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
948 /* insert point is last lock on the queue,
949 * new mode group and new policy group are started
951 prev->res_link = queue->prev;
952 prev->mode_link = &req->l_sl_mode;
953 prev->policy_link = &req->l_sl_policy;
957 * Add a lock into resource granted list after a position described by
960 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
961 struct sl_insert_point *prev)
963 struct ldlm_resource *res = lock->l_resource;
965 check_res_locked(res);
967 ldlm_resource_dump(D_INFO, res);
968 LDLM_DEBUG(lock, "About to add lock:");
970 if (ldlm_is_destroyed(lock)) {
971 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
975 LASSERT(list_empty(&lock->l_res_link));
976 LASSERT(list_empty(&lock->l_sl_mode));
977 LASSERT(list_empty(&lock->l_sl_policy));
980 * lock->link == prev->link means lock is first starting the group.
981 * Don't re-add to itself to suppress kernel warnings.
983 if (&lock->l_res_link != prev->res_link)
984 list_add(&lock->l_res_link, prev->res_link);
985 if (&lock->l_sl_mode != prev->mode_link)
986 list_add(&lock->l_sl_mode, prev->mode_link);
987 if (&lock->l_sl_policy != prev->policy_link)
988 list_add(&lock->l_sl_policy, prev->policy_link);
992 * Add a lock to granted list on a resource maintaining skiplist
995 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
997 struct sl_insert_point prev;
999 LASSERT(lock->l_req_mode == lock->l_granted_mode);
1001 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
1002 ldlm_granted_list_add_lock(lock, &prev);
1006 * Perform lock granting bookkeeping.
1008 * Includes putting the lock into granted list and updating lock mode.
1010 * - ldlm_lock_enqueue
1011 * - ldlm_reprocess_queue
1012 * - ldlm_lock_convert
1014 * must be called with lr_lock held
1016 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
1018 struct ldlm_resource *res = lock->l_resource;
1020 check_res_locked(res);
1022 lock->l_granted_mode = lock->l_req_mode;
1024 if (work_list && lock->l_completion_ast)
1025 ldlm_add_ast_work_item(lock, NULL, work_list);
1027 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
1028 ldlm_grant_lock_with_skiplist(lock);
1029 else if (res->lr_type == LDLM_EXTENT)
1030 ldlm_extent_add_lock(res, lock);
1031 else if (res->lr_type == LDLM_FLOCK) {
1033 * We should not add locks to granted list in the following cases:
1034 * - this is an UNLOCK but not a real lock;
1035 * - this is a TEST lock;
1036 * - this is a F_CANCELLK lock (async flock has req_mode == 0)
1037 * - this is a deadlock (flock cannot be granted)
1039 if (!lock->l_req_mode || lock->l_req_mode == LCK_NL ||
1040 ldlm_is_test_lock(lock) || ldlm_is_flock_deadlock(lock))
1042 ldlm_resource_add_lock(res, &res->lr_granted, lock);
1046 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
1050 * Describe the overlap between two locks. itree_overlap_cb data.
1052 struct lock_match_data {
1053 struct ldlm_lock *lmd_old;
1054 struct ldlm_lock *lmd_lock;
1055 enum ldlm_mode *lmd_mode;
1056 union ldlm_policy_data *lmd_policy;
1062 * Check if the given @lock meets the criteria for a match.
1063 * A reference on the lock is taken if matched.
1065 * \param lock test-against this lock
1066 * \param data parameters
1068 static int lock_matches(struct ldlm_lock *lock, struct lock_match_data *data)
1070 union ldlm_policy_data *lpol = &lock->l_policy_data;
1071 enum ldlm_mode match;
1073 if (lock == data->lmd_old)
1074 return INTERVAL_ITER_STOP;
1077 * Check if this lock can be matched.
1078 * Used by LU-2919(exclusive open) for open lease lock
1080 if (ldlm_is_excl(lock))
1081 return INTERVAL_ITER_CONT;
1084 * llite sometimes wants to match locks that will be
1085 * canceled when their users drop, but we allow it to match
1086 * if it passes in CBPENDING and the lock still has users.
1087 * this is generally only going to be used by children
1088 * whose parents already hold a lock so forward progress
1091 if (ldlm_is_cbpending(lock) &&
1092 !(data->lmd_flags & LDLM_FL_CBPENDING))
1093 return INTERVAL_ITER_CONT;
1095 if (!data->lmd_unref && ldlm_is_cbpending(lock) &&
1096 !lock->l_readers && !lock->l_writers)
1097 return INTERVAL_ITER_CONT;
1099 if (!(lock->l_req_mode & *data->lmd_mode))
1100 return INTERVAL_ITER_CONT;
1101 match = lock->l_req_mode;
1103 switch (lock->l_resource->lr_type) {
1105 if (lpol->l_extent.start > data->lmd_policy->l_extent.start ||
1106 lpol->l_extent.end < data->lmd_policy->l_extent.end)
1107 return INTERVAL_ITER_CONT;
1109 if (unlikely(match == LCK_GROUP) &&
1110 data->lmd_policy->l_extent.gid != LDLM_GID_ANY &&
1111 lpol->l_extent.gid != data->lmd_policy->l_extent.gid)
1112 return INTERVAL_ITER_CONT;
1116 * We match if we have existing lock with same or wider set
1119 if ((lpol->l_inodebits.bits &
1120 data->lmd_policy->l_inodebits.bits) !=
1121 data->lmd_policy->l_inodebits.bits)
1122 return INTERVAL_ITER_CONT;
1128 * We match if we have existing lock with same or wider set
1131 if (!data->lmd_unref && LDLM_HAVE_MASK(lock, GONE))
1132 return INTERVAL_ITER_CONT;
1134 if ((data->lmd_flags & LDLM_FL_LOCAL_ONLY) &&
1135 !ldlm_is_local(lock))
1136 return INTERVAL_ITER_CONT;
1138 if (data->lmd_flags & LDLM_FL_TEST_LOCK) {
1139 LDLM_LOCK_GET(lock);
1140 ldlm_lock_touch_in_lru(lock);
1142 ldlm_lock_addref_internal_nolock(lock, match);
1145 *data->lmd_mode = match;
1146 data->lmd_lock = lock;
1148 return INTERVAL_ITER_STOP;
1151 static unsigned int itree_overlap_cb(struct interval_node *in, void *args)
1153 struct ldlm_interval *node = to_ldlm_interval(in);
1154 struct lock_match_data *data = args;
1155 struct ldlm_lock *lock;
1158 list_for_each_entry(lock, &node->li_group, l_sl_policy) {
1159 rc = lock_matches(lock, data);
1160 if (rc == INTERVAL_ITER_STOP)
1161 return INTERVAL_ITER_STOP;
1163 return INTERVAL_ITER_CONT;
1167 * Search for a lock with given parameters in interval trees.
1169 * \param res search for a lock in this resource
1170 * \param data parameters
1172 * \retval a referenced lock or NULL.
1174 static struct ldlm_lock *search_itree(struct ldlm_resource *res,
1175 struct lock_match_data *data)
1177 struct interval_node_extent ext = {
1178 .start = data->lmd_policy->l_extent.start,
1179 .end = data->lmd_policy->l_extent.end
1183 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1184 struct ldlm_interval_tree *tree = &res->lr_itree[idx];
1186 if (!tree->lit_root)
1189 if (!(tree->lit_mode & *data->lmd_mode))
1192 interval_search(tree->lit_root, &ext,
1193 itree_overlap_cb, data);
1195 return data->lmd_lock;
1199 * Search for a lock with given properties in a queue.
1201 * \param queue search for a lock in this queue
1202 * \param data parameters
1204 * \retval a referenced lock or NULL.
1206 static struct ldlm_lock *search_queue(struct list_head *queue,
1207 struct lock_match_data *data)
1209 struct ldlm_lock *lock;
1212 list_for_each_entry(lock, queue, l_res_link) {
1213 rc = lock_matches(lock, data);
1214 if (rc == INTERVAL_ITER_STOP)
1215 return data->lmd_lock;
1220 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock)
1222 if ((lock->l_flags & LDLM_FL_FAIL_NOTIFIED) == 0) {
1223 lock->l_flags |= LDLM_FL_FAIL_NOTIFIED;
1224 wake_up_all(&lock->l_waitq);
1229 * Mark lock as "matchable" by OST.
1231 * Used to prevent certain races in LOV/OSC where the lock is granted, but LVB
1233 * Assumes LDLM lock is already locked.
1235 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1237 ldlm_set_lvb_ready(lock);
1238 wake_up_all(&lock->l_waitq);
1240 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
1243 * Mark lock as "matchable" by OST.
1244 * Locks the lock and then \see ldlm_lock_allow_match_locked
1246 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1248 lock_res_and_lock(lock);
1249 ldlm_lock_allow_match_locked(lock);
1250 unlock_res_and_lock(lock);
1252 EXPORT_SYMBOL(ldlm_lock_allow_match);
1255 * Attempt to find a lock with specified properties.
1257 * Typically returns a reference to matched lock unless LDLM_FL_TEST_LOCK is
1260 * Can be called in two ways:
1262 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1263 * for a duplicate of.
1265 * Otherwise, all of the fields must be filled in, to match against.
1267 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1268 * server (ie, connh is NULL)
1269 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1270 * list will be considered
1271 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1272 * to be canceled can still be matched as long as they still have reader
1273 * or writer referneces
1274 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1275 * just tell us if we would have matched.
1277 * \retval 1 if it finds an already-existing lock that is compatible; in this
1278 * case, lockh is filled in with a addref()ed lock
1280 * We also check security context, and if that fails we simply return 0 (to
1281 * keep caller code unchanged), the context failure will be discovered by
1282 * caller sometime later.
1284 enum ldlm_mode ldlm_lock_match(struct ldlm_namespace *ns, __u64 flags,
1285 const struct ldlm_res_id *res_id,
1286 enum ldlm_type type,
1287 union ldlm_policy_data *policy,
1288 enum ldlm_mode mode,
1289 struct lustre_handle *lockh, int unref)
1291 struct lock_match_data data = {
1295 .lmd_policy = policy,
1299 struct ldlm_resource *res;
1300 struct ldlm_lock *lock;
1304 data.lmd_old = ldlm_handle2lock(lockh);
1305 LASSERT(data.lmd_old);
1307 ns = ldlm_lock_to_ns(data.lmd_old);
1308 res_id = &data.lmd_old->l_resource->lr_name;
1309 type = data.lmd_old->l_resource->lr_type;
1310 *data.lmd_mode = data.lmd_old->l_req_mode;
1313 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1315 LASSERT(!data.lmd_old);
1319 LDLM_RESOURCE_ADDREF(res);
1322 if (res->lr_type == LDLM_EXTENT)
1323 lock = search_itree(res, &data);
1325 lock = search_queue(&res->lr_granted, &data);
1330 if (flags & LDLM_FL_BLOCK_GRANTED) {
1334 lock = search_queue(&res->lr_waiting, &data);
1341 LDLM_RESOURCE_DELREF(res);
1342 ldlm_resource_putref(res);
1345 ldlm_lock2handle(lock, lockh);
1346 if ((flags & LDLM_FL_LVB_READY) && !ldlm_is_lvb_ready(lock)) {
1347 __u64 wait_flags = LDLM_FL_LVB_READY |
1348 LDLM_FL_DESTROYED | LDLM_FL_FAIL_NOTIFIED;
1349 struct l_wait_info lwi;
1351 if (lock->l_completion_ast) {
1352 int err = lock->l_completion_ast(lock,
1353 LDLM_FL_WAIT_NOREPROC,
1356 if (flags & LDLM_FL_TEST_LOCK)
1357 LDLM_LOCK_RELEASE(lock);
1359 ldlm_lock_decref_internal(lock,
1366 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1367 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1369 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1370 l_wait_event(lock->l_waitq,
1371 lock->l_flags & wait_flags,
1373 if (!ldlm_is_lvb_ready(lock)) {
1374 if (flags & LDLM_FL_TEST_LOCK)
1375 LDLM_LOCK_RELEASE(lock);
1377 ldlm_lock_decref_internal(lock, mode);
1384 LDLM_DEBUG(lock, "matched (%llu %llu)",
1385 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1386 res_id->name[2] : policy->l_extent.start,
1387 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1388 res_id->name[3] : policy->l_extent.end);
1390 /* check user's security context */
1391 if (lock->l_conn_export &&
1392 sptlrpc_import_check_ctx(
1393 class_exp2cliimp(lock->l_conn_export))) {
1394 if (!(flags & LDLM_FL_TEST_LOCK))
1395 ldlm_lock_decref_internal(lock, mode);
1399 if (flags & LDLM_FL_TEST_LOCK)
1400 LDLM_LOCK_RELEASE(lock);
1402 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1403 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res %llu/%llu (%llu %llu)",
1404 ns, type, mode, res_id->name[0],
1406 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1407 res_id->name[2] : policy->l_extent.start,
1408 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1409 res_id->name[3] : policy->l_extent.end);
1412 LDLM_LOCK_PUT(data.lmd_old);
1414 return rc ? mode : 0;
1416 EXPORT_SYMBOL(ldlm_lock_match);
1418 enum ldlm_mode ldlm_revalidate_lock_handle(const struct lustre_handle *lockh,
1421 struct ldlm_lock *lock;
1422 enum ldlm_mode mode = 0;
1424 lock = ldlm_handle2lock(lockh);
1426 lock_res_and_lock(lock);
1427 if (LDLM_HAVE_MASK(lock, GONE))
1430 if (ldlm_is_cbpending(lock) &&
1431 lock->l_readers == 0 && lock->l_writers == 0)
1435 *bits = lock->l_policy_data.l_inodebits.bits;
1436 mode = lock->l_granted_mode;
1437 ldlm_lock_addref_internal_nolock(lock, mode);
1442 unlock_res_and_lock(lock);
1443 LDLM_LOCK_PUT(lock);
1447 EXPORT_SYMBOL(ldlm_revalidate_lock_handle);
1449 /** The caller must guarantee that the buffer is large enough. */
1450 int ldlm_fill_lvb(struct ldlm_lock *lock, struct req_capsule *pill,
1451 enum req_location loc, void *data, int size)
1458 switch (lock->l_lvb_type) {
1460 if (size == sizeof(struct ost_lvb)) {
1461 if (loc == RCL_CLIENT)
1462 lvb = req_capsule_client_swab_get(pill,
1464 lustre_swab_ost_lvb);
1466 lvb = req_capsule_server_swab_get(pill,
1468 lustre_swab_ost_lvb);
1469 if (unlikely(!lvb)) {
1470 LDLM_ERROR(lock, "no LVB");
1474 memcpy(data, lvb, size);
1475 } else if (size == sizeof(struct ost_lvb_v1)) {
1476 struct ost_lvb *olvb = data;
1478 if (loc == RCL_CLIENT)
1479 lvb = req_capsule_client_swab_get(pill,
1481 lustre_swab_ost_lvb_v1);
1483 lvb = req_capsule_server_sized_swab_get(pill,
1485 lustre_swab_ost_lvb_v1);
1486 if (unlikely(!lvb)) {
1487 LDLM_ERROR(lock, "no LVB");
1491 memcpy(data, lvb, size);
1492 olvb->lvb_mtime_ns = 0;
1493 olvb->lvb_atime_ns = 0;
1494 olvb->lvb_ctime_ns = 0;
1496 LDLM_ERROR(lock, "Replied unexpected ost LVB size %d",
1502 if (size == sizeof(struct lquota_lvb)) {
1503 if (loc == RCL_CLIENT)
1504 lvb = req_capsule_client_swab_get(pill,
1506 lustre_swab_lquota_lvb);
1508 lvb = req_capsule_server_swab_get(pill,
1510 lustre_swab_lquota_lvb);
1511 if (unlikely(!lvb)) {
1512 LDLM_ERROR(lock, "no LVB");
1516 memcpy(data, lvb, size);
1519 "Replied unexpected lquota LVB size %d",
1528 if (loc == RCL_CLIENT)
1529 lvb = req_capsule_client_get(pill, &RMF_DLM_LVB);
1531 lvb = req_capsule_server_get(pill, &RMF_DLM_LVB);
1532 if (unlikely(!lvb)) {
1533 LDLM_ERROR(lock, "no LVB");
1537 memcpy(data, lvb, size);
1540 LDLM_ERROR(lock, "Unknown LVB type: %d", lock->l_lvb_type);
1549 * Create and fill in new LDLM lock with specified properties.
1550 * Returns a referenced lock
1552 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1553 const struct ldlm_res_id *res_id,
1554 enum ldlm_type type,
1555 enum ldlm_mode mode,
1556 const struct ldlm_callback_suite *cbs,
1557 void *data, __u32 lvb_len,
1558 enum lvb_type lvb_type)
1560 struct ldlm_lock *lock;
1561 struct ldlm_resource *res;
1564 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1566 return ERR_CAST(res);
1568 lock = ldlm_lock_new(res);
1570 return ERR_PTR(-ENOMEM);
1572 lock->l_req_mode = mode;
1573 lock->l_ast_data = data;
1574 lock->l_pid = current_pid();
1576 lock->l_blocking_ast = cbs->lcs_blocking;
1577 lock->l_completion_ast = cbs->lcs_completion;
1578 lock->l_glimpse_ast = cbs->lcs_glimpse;
1581 lock->l_tree_node = NULL;
1582 /* if this is the extent lock, allocate the interval tree node */
1583 if (type == LDLM_EXTENT) {
1584 if (!ldlm_interval_alloc(lock)) {
1591 lock->l_lvb_len = lvb_len;
1592 lock->l_lvb_data = kzalloc(lvb_len, GFP_NOFS);
1593 if (!lock->l_lvb_data) {
1599 lock->l_lvb_type = lvb_type;
1600 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK)) {
1608 ldlm_lock_destroy(lock);
1609 LDLM_LOCK_RELEASE(lock);
1614 * Enqueue (request) a lock.
1615 * On the client this is called from ldlm_cli_enqueue_fini
1616 * after we already got an initial reply from the server with some status.
1618 * Does not block. As a result of enqueue the lock would be put
1619 * into granted or waiting list.
1621 enum ldlm_error ldlm_lock_enqueue(struct ldlm_namespace *ns,
1622 struct ldlm_lock **lockp,
1623 void *cookie, __u64 *flags)
1625 struct ldlm_lock *lock = *lockp;
1626 struct ldlm_resource *res = lock->l_resource;
1628 lock_res_and_lock(lock);
1629 if (lock->l_req_mode == lock->l_granted_mode) {
1630 /* The server returned a blocked lock, but it was granted
1631 * before we got a chance to actually enqueue it. We don't
1632 * need to do anything else.
1634 *flags &= ~LDLM_FL_BLOCKED_MASK;
1638 ldlm_resource_unlink_lock(lock);
1640 /* Cannot happen unless on the server */
1641 if (res->lr_type == LDLM_EXTENT && !lock->l_tree_node)
1644 /* Some flags from the enqueue want to make it into the AST, via the
1647 if (*flags & LDLM_FL_AST_DISCARD_DATA)
1648 ldlm_set_ast_discard_data(lock);
1649 if (*flags & LDLM_FL_TEST_LOCK)
1650 ldlm_set_test_lock(lock);
1653 * This distinction between local lock trees is very important; a client
1654 * namespace only has information about locks taken by that client, and
1655 * thus doesn't have enough information to decide for itself if it can
1656 * be granted (below). In this case, we do exactly what the server
1657 * tells us to do, as dictated by the 'flags'.
1659 if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1660 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1662 ldlm_grant_lock(lock, NULL);
1665 unlock_res_and_lock(lock);
1670 * Process a call to blocking AST callback for a lock in ast_work list
1673 ldlm_work_bl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1675 struct ldlm_cb_set_arg *arg = opaq;
1676 struct ldlm_lock_desc d;
1678 struct ldlm_lock *lock;
1680 if (list_empty(arg->list))
1683 lock = list_entry(arg->list->next, struct ldlm_lock, l_bl_ast);
1685 /* nobody should touch l_bl_ast */
1686 lock_res_and_lock(lock);
1687 list_del_init(&lock->l_bl_ast);
1689 LASSERT(ldlm_is_ast_sent(lock));
1690 LASSERT(lock->l_bl_ast_run == 0);
1691 LASSERT(lock->l_blocking_lock);
1692 lock->l_bl_ast_run++;
1693 unlock_res_and_lock(lock);
1695 ldlm_lock2desc(lock->l_blocking_lock, &d);
1697 rc = lock->l_blocking_ast(lock, &d, (void *)arg, LDLM_CB_BLOCKING);
1698 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1699 lock->l_blocking_lock = NULL;
1700 LDLM_LOCK_RELEASE(lock);
1706 * Process a call to completion AST callback for a lock in ast_work list
1709 ldlm_work_cp_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1711 struct ldlm_cb_set_arg *arg = opaq;
1713 struct ldlm_lock *lock;
1714 ldlm_completion_callback completion_callback;
1716 if (list_empty(arg->list))
1719 lock = list_entry(arg->list->next, struct ldlm_lock, l_cp_ast);
1721 /* It's possible to receive a completion AST before we've set
1722 * the l_completion_ast pointer: either because the AST arrived
1723 * before the reply, or simply because there's a small race
1724 * window between receiving the reply and finishing the local
1725 * enqueue. (bug 842)
1727 * This can't happen with the blocking_ast, however, because we
1728 * will never call the local blocking_ast until we drop our
1729 * reader/writer reference, which we won't do until we get the
1730 * reply and finish enqueueing.
1733 /* nobody should touch l_cp_ast */
1734 lock_res_and_lock(lock);
1735 list_del_init(&lock->l_cp_ast);
1736 LASSERT(ldlm_is_cp_reqd(lock));
1737 /* save l_completion_ast since it can be changed by
1738 * mds_intent_policy(), see bug 14225
1740 completion_callback = lock->l_completion_ast;
1741 ldlm_clear_cp_reqd(lock);
1742 unlock_res_and_lock(lock);
1744 if (completion_callback)
1745 rc = completion_callback(lock, 0, (void *)arg);
1746 LDLM_LOCK_RELEASE(lock);
1752 * Process a call to revocation AST callback for a lock in ast_work list
1755 ldlm_work_revoke_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1757 struct ldlm_cb_set_arg *arg = opaq;
1758 struct ldlm_lock_desc desc;
1760 struct ldlm_lock *lock;
1762 if (list_empty(arg->list))
1765 lock = list_entry(arg->list->next, struct ldlm_lock, l_rk_ast);
1766 list_del_init(&lock->l_rk_ast);
1768 /* the desc just pretend to exclusive */
1769 ldlm_lock2desc(lock, &desc);
1770 desc.l_req_mode = LCK_EX;
1771 desc.l_granted_mode = 0;
1773 rc = lock->l_blocking_ast(lock, &desc, (void *)arg, LDLM_CB_BLOCKING);
1774 LDLM_LOCK_RELEASE(lock);
1780 * Process a call to glimpse AST callback for a lock in ast_work list
1782 static int ldlm_work_gl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1784 struct ldlm_cb_set_arg *arg = opaq;
1785 struct ldlm_glimpse_work *gl_work;
1786 struct ldlm_lock *lock;
1789 if (list_empty(arg->list))
1792 gl_work = list_entry(arg->list->next, struct ldlm_glimpse_work,
1794 list_del_init(&gl_work->gl_list);
1796 lock = gl_work->gl_lock;
1798 /* transfer the glimpse descriptor to ldlm_cb_set_arg */
1799 arg->gl_desc = gl_work->gl_desc;
1801 /* invoke the actual glimpse callback */
1802 if (lock->l_glimpse_ast(lock, (void *)arg) == 0)
1805 LDLM_LOCK_RELEASE(lock);
1807 if ((gl_work->gl_flags & LDLM_GL_WORK_NOFREE) == 0)
1814 * Process list of locks in need of ASTs being sent.
1816 * Used on server to send multiple ASTs together instead of sending one by
1819 int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list,
1820 enum ldlm_desc_ast_t ast_type)
1822 struct ldlm_cb_set_arg *arg;
1823 set_producer_func work_ast_lock;
1826 if (list_empty(rpc_list))
1829 arg = kzalloc(sizeof(*arg), GFP_NOFS);
1833 atomic_set(&arg->restart, 0);
1834 arg->list = rpc_list;
1837 case LDLM_WORK_BL_AST:
1838 arg->type = LDLM_BL_CALLBACK;
1839 work_ast_lock = ldlm_work_bl_ast_lock;
1841 case LDLM_WORK_CP_AST:
1842 arg->type = LDLM_CP_CALLBACK;
1843 work_ast_lock = ldlm_work_cp_ast_lock;
1845 case LDLM_WORK_REVOKE_AST:
1846 arg->type = LDLM_BL_CALLBACK;
1847 work_ast_lock = ldlm_work_revoke_ast_lock;
1849 case LDLM_WORK_GL_AST:
1850 arg->type = LDLM_GL_CALLBACK;
1851 work_ast_lock = ldlm_work_gl_ast_lock;
1857 /* We create a ptlrpc request set with flow control extension.
1858 * This request set will use the work_ast_lock function to produce new
1859 * requests and will send a new request each time one completes in order
1860 * to keep the number of requests in flight to ns_max_parallel_ast
1862 arg->set = ptlrpc_prep_fcset(ns->ns_max_parallel_ast ? : UINT_MAX,
1863 work_ast_lock, arg);
1869 ptlrpc_set_wait(arg->set);
1870 ptlrpc_set_destroy(arg->set);
1872 rc = atomic_read(&arg->restart) ? -ERESTART : 0;
1880 * Helper function to call blocking AST for LDLM lock \a lock in a
1881 * "cancelling" mode.
1883 void ldlm_cancel_callback(struct ldlm_lock *lock)
1885 check_res_locked(lock->l_resource);
1886 if (!ldlm_is_cancel(lock)) {
1887 ldlm_set_cancel(lock);
1888 if (lock->l_blocking_ast) {
1889 unlock_res_and_lock(lock);
1890 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1892 lock_res_and_lock(lock);
1894 LDLM_DEBUG(lock, "no blocking ast");
1897 ldlm_set_bl_done(lock);
1901 * Remove skiplist-enabled LDLM lock \a req from granted list
1903 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1905 if (req->l_resource->lr_type != LDLM_PLAIN &&
1906 req->l_resource->lr_type != LDLM_IBITS)
1909 list_del_init(&req->l_sl_policy);
1910 list_del_init(&req->l_sl_mode);
1914 * Attempts to cancel LDLM lock \a lock that has no reader/writer references.
1916 void ldlm_lock_cancel(struct ldlm_lock *lock)
1918 struct ldlm_resource *res;
1919 struct ldlm_namespace *ns;
1921 lock_res_and_lock(lock);
1923 res = lock->l_resource;
1924 ns = ldlm_res_to_ns(res);
1926 /* Please do not, no matter how tempting, remove this LBUG without
1927 * talking to me first. -phik
1929 if (lock->l_readers || lock->l_writers) {
1930 LDLM_ERROR(lock, "lock still has references");
1934 /* Releases cancel callback. */
1935 ldlm_cancel_callback(lock);
1937 ldlm_resource_unlink_lock(lock);
1938 ldlm_lock_destroy_nolock(lock);
1940 if (lock->l_granted_mode == lock->l_req_mode)
1941 ldlm_pool_del(&ns->ns_pool, lock);
1943 /* Make sure we will not be called again for same lock what is possible
1944 * if not to zero out lock->l_granted_mode
1946 lock->l_granted_mode = LCK_MINMODE;
1947 unlock_res_and_lock(lock);
1949 EXPORT_SYMBOL(ldlm_lock_cancel);
1952 * Set opaque data into the lock that only makes sense to upper layer.
1954 int ldlm_lock_set_data(const struct lustre_handle *lockh, void *data)
1956 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1960 if (!lock->l_ast_data)
1961 lock->l_ast_data = data;
1962 if (lock->l_ast_data == data)
1964 LDLM_LOCK_PUT(lock);
1968 EXPORT_SYMBOL(ldlm_lock_set_data);
1970 struct export_cl_data {
1971 struct obd_export *ecl_exp;
1976 * Print lock with lock handle \a lockh description into debug log.
1978 * Used when printing all locks on a resource for debug purposes.
1980 void ldlm_lock_dump_handle(int level, const struct lustre_handle *lockh)
1982 struct ldlm_lock *lock;
1984 if (!((libcfs_debug | D_ERROR) & level))
1987 lock = ldlm_handle2lock(lockh);
1991 LDLM_DEBUG_LIMIT(level, lock, "###");
1993 LDLM_LOCK_PUT(lock);
1995 EXPORT_SYMBOL(ldlm_lock_dump_handle);
1998 * Print lock information with custom message into debug log.
2001 void _ldlm_lock_debug(struct ldlm_lock *lock,
2002 struct libcfs_debug_msg_data *msgdata,
2003 const char *fmt, ...)
2006 struct obd_export *exp = lock->l_export;
2007 struct ldlm_resource *resource = lock->l_resource;
2008 char *nid = "local";
2010 va_start(args, fmt);
2012 if (exp && exp->exp_connection) {
2013 nid = libcfs_nid2str(exp->exp_connection->c_peer.nid);
2014 } else if (exp && exp->exp_obd) {
2015 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
2017 nid = libcfs_nid2str(imp->imp_connection->c_peer.nid);
2021 libcfs_debug_vmsg2(msgdata, fmt, args,
2022 " ns: \?\? lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: \?\? rrc=\?\? type: \?\?\? flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lu lvb_type: %d\n",
2024 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
2025 lock->l_readers, lock->l_writers,
2026 ldlm_lockname[lock->l_granted_mode],
2027 ldlm_lockname[lock->l_req_mode],
2028 lock->l_flags, nid, lock->l_remote_handle.cookie,
2029 exp ? atomic_read(&exp->exp_refcount) : -99,
2030 lock->l_pid, lock->l_callback_timeout, lock->l_lvb_type);
2035 switch (resource->lr_type) {
2037 libcfs_debug_vmsg2(msgdata, fmt, args,
2038 " ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: " DLDLMRES " rrc: %d type: %s [%llu->%llu] (req %llu->%llu) flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lu lvb_type: %d\n",
2039 ldlm_lock_to_ns_name(lock), lock,
2040 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
2041 lock->l_readers, lock->l_writers,
2042 ldlm_lockname[lock->l_granted_mode],
2043 ldlm_lockname[lock->l_req_mode],
2045 atomic_read(&resource->lr_refcount),
2046 ldlm_typename[resource->lr_type],
2047 lock->l_policy_data.l_extent.start,
2048 lock->l_policy_data.l_extent.end,
2049 lock->l_req_extent.start, lock->l_req_extent.end,
2050 lock->l_flags, nid, lock->l_remote_handle.cookie,
2051 exp ? atomic_read(&exp->exp_refcount) : -99,
2052 lock->l_pid, lock->l_callback_timeout,
2057 libcfs_debug_vmsg2(msgdata, fmt, args,
2058 " ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: " DLDLMRES " rrc: %d type: %s pid: %d [%llu->%llu] flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lu\n",
2059 ldlm_lock_to_ns_name(lock), lock,
2060 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
2061 lock->l_readers, lock->l_writers,
2062 ldlm_lockname[lock->l_granted_mode],
2063 ldlm_lockname[lock->l_req_mode],
2065 atomic_read(&resource->lr_refcount),
2066 ldlm_typename[resource->lr_type],
2067 lock->l_policy_data.l_flock.pid,
2068 lock->l_policy_data.l_flock.start,
2069 lock->l_policy_data.l_flock.end,
2070 lock->l_flags, nid, lock->l_remote_handle.cookie,
2071 exp ? atomic_read(&exp->exp_refcount) : -99,
2072 lock->l_pid, lock->l_callback_timeout);
2076 libcfs_debug_vmsg2(msgdata, fmt, args,
2077 " ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: " DLDLMRES " bits %#llx rrc: %d type: %s flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lu lvb_type: %d\n",
2078 ldlm_lock_to_ns_name(lock),
2079 lock, lock->l_handle.h_cookie,
2080 atomic_read(&lock->l_refc),
2081 lock->l_readers, lock->l_writers,
2082 ldlm_lockname[lock->l_granted_mode],
2083 ldlm_lockname[lock->l_req_mode],
2085 lock->l_policy_data.l_inodebits.bits,
2086 atomic_read(&resource->lr_refcount),
2087 ldlm_typename[resource->lr_type],
2088 lock->l_flags, nid, lock->l_remote_handle.cookie,
2089 exp ? atomic_read(&exp->exp_refcount) : -99,
2090 lock->l_pid, lock->l_callback_timeout,
2095 libcfs_debug_vmsg2(msgdata, fmt, args,
2096 " ns: %s lock: %p/%#llx lrc: %d/%d,%d mode: %s/%s res: " DLDLMRES " rrc: %d type: %s flags: %#llx nid: %s remote: %#llx expref: %d pid: %u timeout: %lu lvb_type: %d\n",
2097 ldlm_lock_to_ns_name(lock),
2098 lock, lock->l_handle.h_cookie,
2099 atomic_read(&lock->l_refc),
2100 lock->l_readers, lock->l_writers,
2101 ldlm_lockname[lock->l_granted_mode],
2102 ldlm_lockname[lock->l_req_mode],
2104 atomic_read(&resource->lr_refcount),
2105 ldlm_typename[resource->lr_type],
2106 lock->l_flags, nid, lock->l_remote_handle.cookie,
2107 exp ? atomic_read(&exp->exp_refcount) : -99,
2108 lock->l_pid, lock->l_callback_timeout,
2114 EXPORT_SYMBOL(_ldlm_lock_debug);