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 <linux/libcfs/libcfs.h>
41 #include <lustre_intent.h>
42 #include <lustre_swab.h>
43 #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;
443 * Moves LDLM lock \a lock to another resource.
444 * This is used on client when server returns some other lock than requested
445 * (typically as a result of intent operation)
447 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
448 const struct ldlm_res_id *new_resid)
450 struct ldlm_resource *oldres = lock->l_resource;
451 struct ldlm_resource *newres;
454 lock_res_and_lock(lock);
455 if (memcmp(new_resid, &lock->l_resource->lr_name,
456 sizeof(lock->l_resource->lr_name)) == 0) {
458 unlock_res_and_lock(lock);
462 LASSERT(new_resid->name[0] != 0);
464 /* This function assumes that the lock isn't on any lists */
465 LASSERT(list_empty(&lock->l_res_link));
467 type = oldres->lr_type;
468 unlock_res_and_lock(lock);
470 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
472 return PTR_ERR(newres);
474 lu_ref_add(&newres->lr_reference, "lock", lock);
476 * To flip the lock from the old to the new resource, lock, oldres and
477 * newres have to be locked. Resource spin-locks are nested within
478 * lock->l_lock, and are taken in the memory address order to avoid
481 spin_lock(&lock->l_lock);
482 oldres = lock->l_resource;
483 if (oldres < newres) {
485 lock_res_nested(newres, LRT_NEW);
488 lock_res_nested(oldres, LRT_NEW);
490 LASSERT(memcmp(new_resid, &oldres->lr_name,
491 sizeof(oldres->lr_name)) != 0);
492 lock->l_resource = newres;
494 unlock_res_and_lock(lock);
496 /* ...and the flowers are still standing! */
497 lu_ref_del(&oldres->lr_reference, "lock", lock);
498 ldlm_resource_putref(oldres);
503 /** \defgroup ldlm_handles LDLM HANDLES
504 * Ways to get hold of locks without any addresses.
509 * Fills in handle for LDLM lock \a lock into supplied \a lockh
510 * Does not take any references.
512 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
514 lockh->cookie = lock->l_handle.h_cookie;
516 EXPORT_SYMBOL(ldlm_lock2handle);
519 * Obtain a lock reference by handle.
521 * if \a flags: atomically get the lock and set the flags.
522 * Return NULL if flag already set
524 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
527 struct ldlm_lock *lock;
531 lock = class_handle2object(handle->cookie, NULL);
535 if (lock->l_export && lock->l_export->exp_failed) {
536 CDEBUG(D_INFO, "lock export failed: lock %p, exp %p\n",
537 lock, lock->l_export);
542 /* It's unlikely but possible that someone marked the lock as
543 * destroyed after we did handle2object on it
545 if (flags == 0 && !ldlm_is_destroyed(lock)) {
546 lu_ref_add(&lock->l_reference, "handle", current);
550 lock_res_and_lock(lock);
552 LASSERT(lock->l_resource);
554 lu_ref_add_atomic(&lock->l_reference, "handle", current);
555 if (unlikely(ldlm_is_destroyed(lock))) {
556 unlock_res_and_lock(lock);
557 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
563 if (lock->l_flags & flags) {
564 unlock_res_and_lock(lock);
569 lock->l_flags |= flags;
572 unlock_res_and_lock(lock);
575 EXPORT_SYMBOL(__ldlm_handle2lock);
576 /** @} ldlm_handles */
579 * Fill in "on the wire" representation for given LDLM lock into supplied
580 * lock descriptor \a desc structure.
582 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
584 ldlm_res2desc(lock->l_resource, &desc->l_resource);
585 desc->l_req_mode = lock->l_req_mode;
586 desc->l_granted_mode = lock->l_granted_mode;
587 ldlm_convert_policy_to_wire(lock->l_resource->lr_type,
588 &lock->l_policy_data,
589 &desc->l_policy_data);
593 * Add a lock to list of conflicting locks to send AST to.
595 * Only add if we have not sent a blocking AST to the lock yet.
597 static void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
598 struct list_head *work_list)
600 if (!ldlm_is_ast_sent(lock)) {
601 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
602 ldlm_set_ast_sent(lock);
603 /* If the enqueuing client said so, tell the AST recipient to
604 * discard dirty data, rather than writing back.
606 if (ldlm_is_ast_discard_data(new))
607 ldlm_set_discard_data(lock);
608 LASSERT(list_empty(&lock->l_bl_ast));
609 list_add(&lock->l_bl_ast, work_list);
611 LASSERT(!lock->l_blocking_lock);
612 lock->l_blocking_lock = LDLM_LOCK_GET(new);
617 * Add a lock to list of just granted locks to send completion AST to.
619 static void ldlm_add_cp_work_item(struct ldlm_lock *lock,
620 struct list_head *work_list)
622 if (!ldlm_is_cp_reqd(lock)) {
623 ldlm_set_cp_reqd(lock);
624 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
625 LASSERT(list_empty(&lock->l_cp_ast));
626 list_add(&lock->l_cp_ast, work_list);
632 * Aggregator function to add AST work items into a list. Determines
633 * what sort of an AST work needs to be done and calls the proper
635 * Must be called with lr_lock held.
637 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
638 struct ldlm_lock *new,
639 struct list_head *work_list)
641 check_res_locked(lock->l_resource);
643 ldlm_add_bl_work_item(lock, new, work_list);
645 ldlm_add_cp_work_item(lock, work_list);
649 * Add specified reader/writer reference to LDLM lock with handle \a lockh.
650 * r/w reference type is determined by \a mode
651 * Calls ldlm_lock_addref_internal.
653 void ldlm_lock_addref(const struct lustre_handle *lockh, enum ldlm_mode mode)
655 struct ldlm_lock *lock;
657 lock = ldlm_handle2lock(lockh);
658 LASSERTF(lock, "Non-existing lock: %llx\n", lockh->cookie);
659 ldlm_lock_addref_internal(lock, mode);
662 EXPORT_SYMBOL(ldlm_lock_addref);
666 * Add specified reader/writer reference to LDLM lock \a lock.
667 * r/w reference type is determined by \a mode
668 * Removes lock from LRU if it is there.
669 * Assumes the LDLM lock is already locked.
671 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock,
674 ldlm_lock_remove_from_lru(lock);
675 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
677 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
679 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
681 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
684 lu_ref_add_atomic(&lock->l_reference, "user", lock);
685 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
689 * Attempts to add reader/writer reference to a lock with handle \a lockh, and
690 * fails if lock is already LDLM_FL_CBPENDING or destroyed.
692 * \retval 0 success, lock was addref-ed
694 * \retval -EAGAIN lock is being canceled.
696 int ldlm_lock_addref_try(const struct lustre_handle *lockh, enum ldlm_mode mode)
698 struct ldlm_lock *lock;
702 lock = ldlm_handle2lock(lockh);
704 lock_res_and_lock(lock);
705 if (lock->l_readers != 0 || lock->l_writers != 0 ||
706 !ldlm_is_cbpending(lock)) {
707 ldlm_lock_addref_internal_nolock(lock, mode);
710 unlock_res_and_lock(lock);
715 EXPORT_SYMBOL(ldlm_lock_addref_try);
718 * Add specified reader/writer reference to LDLM lock \a lock.
719 * Locks LDLM lock and calls ldlm_lock_addref_internal_nolock to do the work.
720 * Only called for local locks.
722 void ldlm_lock_addref_internal(struct ldlm_lock *lock, enum ldlm_mode mode)
724 lock_res_and_lock(lock);
725 ldlm_lock_addref_internal_nolock(lock, mode);
726 unlock_res_and_lock(lock);
730 * Removes reader/writer reference for LDLM lock \a lock.
731 * Assumes LDLM lock is already locked.
732 * only called in ldlm_flock_destroy and for local locks.
733 * Does NOT add lock to LRU if no r/w references left to accommodate flock locks
734 * that cannot be placed in LRU.
736 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock,
739 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
740 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
741 LASSERT(lock->l_readers > 0);
742 lu_ref_del(&lock->l_reference, "reader", lock);
745 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
746 LASSERT(lock->l_writers > 0);
747 lu_ref_del(&lock->l_reference, "writer", lock);
751 lu_ref_del(&lock->l_reference, "user", lock);
752 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
756 * Removes reader/writer reference for LDLM lock \a lock.
757 * Locks LDLM lock first.
758 * If the lock is determined to be client lock on a client and r/w refcount
759 * drops to zero and the lock is not blocked, the lock is added to LRU lock
761 * For blocked LDLM locks if r/w count drops to zero, blocking_ast is called.
763 void ldlm_lock_decref_internal(struct ldlm_lock *lock, enum ldlm_mode mode)
765 struct ldlm_namespace *ns;
767 lock_res_and_lock(lock);
769 ns = ldlm_lock_to_ns(lock);
771 ldlm_lock_decref_internal_nolock(lock, mode);
773 if ((ldlm_is_local(lock) || lock->l_req_mode == LCK_GROUP) &&
774 !lock->l_readers && !lock->l_writers) {
775 /* If this is a local lock on a server namespace and this was
776 * the last reference, cancel the lock.
778 * Group locks are special:
779 * They must not go in LRU, but they are not called back
780 * like non-group locks, instead they are manually released.
781 * They have an l_writers reference which they keep until
782 * they are manually released, so we remove them when they have
783 * no more reader or writer references. - LU-6368
785 ldlm_set_cbpending(lock);
788 if (!lock->l_readers && !lock->l_writers && ldlm_is_cbpending(lock)) {
789 /* If we received a blocked AST and this was the last reference,
792 LDLM_DEBUG(lock, "final decref done on cbpending lock");
794 LDLM_LOCK_GET(lock); /* dropped by bl thread */
795 ldlm_lock_remove_from_lru(lock);
796 unlock_res_and_lock(lock);
798 if (ldlm_is_fail_loc(lock))
799 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
801 if (ldlm_is_atomic_cb(lock) ||
802 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
803 ldlm_handle_bl_callback(ns, NULL, lock);
804 } else if (!lock->l_readers && !lock->l_writers &&
805 !ldlm_is_no_lru(lock) && !ldlm_is_bl_ast(lock)) {
806 LDLM_DEBUG(lock, "add lock into lru list");
808 /* If this is a client-side namespace and this was the last
809 * reference, put it on the LRU.
811 ldlm_lock_add_to_lru(lock);
812 unlock_res_and_lock(lock);
814 if (ldlm_is_fail_loc(lock))
815 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
817 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
818 * are not supported by the server, otherwise, it is done on
821 if (!exp_connect_cancelset(lock->l_conn_export) &&
822 !ns_connect_lru_resize(ns))
823 ldlm_cancel_lru(ns, 0, LCF_ASYNC, 0);
825 LDLM_DEBUG(lock, "do not add lock into lru list");
826 unlock_res_and_lock(lock);
831 * Decrease reader/writer refcount for LDLM lock with handle \a lockh
833 void ldlm_lock_decref(const struct lustre_handle *lockh, enum ldlm_mode mode)
835 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
837 LASSERTF(lock, "Non-existing lock: %#llx\n", lockh->cookie);
838 ldlm_lock_decref_internal(lock, mode);
841 EXPORT_SYMBOL(ldlm_lock_decref);
844 * Decrease reader/writer refcount for LDLM lock with handle
845 * \a lockh and mark it for subsequent cancellation once r/w refcount
846 * drops to zero instead of putting into LRU.
848 void ldlm_lock_decref_and_cancel(const struct lustre_handle *lockh,
851 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
855 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
856 lock_res_and_lock(lock);
857 ldlm_set_cbpending(lock);
858 unlock_res_and_lock(lock);
859 ldlm_lock_decref_internal(lock, mode);
862 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
864 struct sl_insert_point {
865 struct list_head *res_link;
866 struct list_head *mode_link;
867 struct list_head *policy_link;
871 * Finds a position to insert the new lock into granted lock list.
873 * Used for locks eligible for skiplist optimization.
876 * queue [input]: the granted list where search acts on;
877 * req [input]: the lock whose position to be located;
878 * prev [output]: positions within 3 lists to insert @req to
882 * - ldlm_grant_lock_with_skiplist
884 static void search_granted_lock(struct list_head *queue,
885 struct ldlm_lock *req,
886 struct sl_insert_point *prev)
888 struct list_head *tmp;
889 struct ldlm_lock *lock, *mode_end, *policy_end;
891 list_for_each(tmp, queue) {
892 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
894 mode_end = list_prev_entry(lock, l_sl_mode);
896 if (lock->l_req_mode != req->l_req_mode) {
897 /* jump to last lock of mode group */
898 tmp = &mode_end->l_res_link;
902 /* suitable mode group is found */
903 if (lock->l_resource->lr_type == LDLM_PLAIN) {
904 /* insert point is last lock of the mode group */
905 prev->res_link = &mode_end->l_res_link;
906 prev->mode_link = &mode_end->l_sl_mode;
907 prev->policy_link = &req->l_sl_policy;
911 if (lock->l_resource->lr_type == LDLM_IBITS) {
914 list_prev_entry(lock, l_sl_policy);
916 if (lock->l_policy_data.l_inodebits.bits ==
917 req->l_policy_data.l_inodebits.bits) {
918 /* insert point is last lock of
922 &policy_end->l_res_link;
924 &policy_end->l_sl_mode;
926 &policy_end->l_sl_policy;
930 if (policy_end == mode_end)
931 /* done with mode group */
934 /* go to next policy group within mode group */
935 tmp = policy_end->l_res_link.next;
936 lock = list_entry(tmp, struct ldlm_lock,
938 } /* loop over policy groups within the mode group */
940 /* insert point is last lock of the mode group,
941 * new policy group is started
943 prev->res_link = &mode_end->l_res_link;
944 prev->mode_link = &mode_end->l_sl_mode;
945 prev->policy_link = &req->l_sl_policy;
949 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
953 /* insert point is last lock on the queue,
954 * new mode group and new policy group are started
956 prev->res_link = queue->prev;
957 prev->mode_link = &req->l_sl_mode;
958 prev->policy_link = &req->l_sl_policy;
962 * Add a lock into resource granted list after a position described by
965 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
966 struct sl_insert_point *prev)
968 struct ldlm_resource *res = lock->l_resource;
970 check_res_locked(res);
972 ldlm_resource_dump(D_INFO, res);
973 LDLM_DEBUG(lock, "About to add lock:");
975 if (ldlm_is_destroyed(lock)) {
976 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
980 LASSERT(list_empty(&lock->l_res_link));
981 LASSERT(list_empty(&lock->l_sl_mode));
982 LASSERT(list_empty(&lock->l_sl_policy));
985 * lock->link == prev->link means lock is first starting the group.
986 * Don't re-add to itself to suppress kernel warnings.
988 if (&lock->l_res_link != prev->res_link)
989 list_add(&lock->l_res_link, prev->res_link);
990 if (&lock->l_sl_mode != prev->mode_link)
991 list_add(&lock->l_sl_mode, prev->mode_link);
992 if (&lock->l_sl_policy != prev->policy_link)
993 list_add(&lock->l_sl_policy, prev->policy_link);
997 * Add a lock to granted list on a resource maintaining skiplist
1000 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
1002 struct sl_insert_point prev;
1004 LASSERT(lock->l_req_mode == lock->l_granted_mode);
1006 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
1007 ldlm_granted_list_add_lock(lock, &prev);
1011 * Perform lock granting bookkeeping.
1013 * Includes putting the lock into granted list and updating lock mode.
1015 * - ldlm_lock_enqueue
1016 * - ldlm_reprocess_queue
1017 * - ldlm_lock_convert
1019 * must be called with lr_lock held
1021 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
1023 struct ldlm_resource *res = lock->l_resource;
1025 check_res_locked(res);
1027 lock->l_granted_mode = lock->l_req_mode;
1029 if (work_list && lock->l_completion_ast)
1030 ldlm_add_ast_work_item(lock, NULL, work_list);
1032 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1033 ldlm_grant_lock_with_skiplist(lock);
1034 } else if (res->lr_type == LDLM_EXTENT) {
1035 ldlm_extent_add_lock(res, lock);
1036 } else if (res->lr_type == LDLM_FLOCK) {
1038 * We should not add locks to granted list in the following cases:
1039 * - this is an UNLOCK but not a real lock;
1040 * - this is a TEST lock;
1041 * - this is a F_CANCELLK lock (async flock has req_mode == 0)
1042 * - this is a deadlock (flock cannot be granted)
1044 if (!lock->l_req_mode || lock->l_req_mode == LCK_NL ||
1045 ldlm_is_test_lock(lock) || ldlm_is_flock_deadlock(lock))
1047 ldlm_resource_add_lock(res, &res->lr_granted, lock);
1052 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
1056 * Describe the overlap between two locks. itree_overlap_cb data.
1058 struct lock_match_data {
1059 struct ldlm_lock *lmd_old;
1060 struct ldlm_lock *lmd_lock;
1061 enum ldlm_mode *lmd_mode;
1062 union ldlm_policy_data *lmd_policy;
1068 * Check if the given @lock meets the criteria for a match.
1069 * A reference on the lock is taken if matched.
1071 * \param lock test-against this lock
1072 * \param data parameters
1074 static int lock_matches(struct ldlm_lock *lock, struct lock_match_data *data)
1076 union ldlm_policy_data *lpol = &lock->l_policy_data;
1077 enum ldlm_mode match;
1079 if (lock == data->lmd_old)
1080 return INTERVAL_ITER_STOP;
1083 * Check if this lock can be matched.
1084 * Used by LU-2919(exclusive open) for open lease lock
1086 if (ldlm_is_excl(lock))
1087 return INTERVAL_ITER_CONT;
1090 * llite sometimes wants to match locks that will be
1091 * canceled when their users drop, but we allow it to match
1092 * if it passes in CBPENDING and the lock still has users.
1093 * this is generally only going to be used by children
1094 * whose parents already hold a lock so forward progress
1097 if (ldlm_is_cbpending(lock) &&
1098 !(data->lmd_flags & LDLM_FL_CBPENDING))
1099 return INTERVAL_ITER_CONT;
1101 if (!data->lmd_unref && ldlm_is_cbpending(lock) &&
1102 !lock->l_readers && !lock->l_writers)
1103 return INTERVAL_ITER_CONT;
1105 if (!(lock->l_req_mode & *data->lmd_mode))
1106 return INTERVAL_ITER_CONT;
1107 match = lock->l_req_mode;
1109 switch (lock->l_resource->lr_type) {
1111 if (lpol->l_extent.start > data->lmd_policy->l_extent.start ||
1112 lpol->l_extent.end < data->lmd_policy->l_extent.end)
1113 return INTERVAL_ITER_CONT;
1115 if (unlikely(match == LCK_GROUP) &&
1116 data->lmd_policy->l_extent.gid != LDLM_GID_ANY &&
1117 lpol->l_extent.gid != data->lmd_policy->l_extent.gid)
1118 return INTERVAL_ITER_CONT;
1122 * We match if we have existing lock with same or wider set
1125 if ((lpol->l_inodebits.bits &
1126 data->lmd_policy->l_inodebits.bits) !=
1127 data->lmd_policy->l_inodebits.bits)
1128 return INTERVAL_ITER_CONT;
1134 * We match if we have existing lock with same or wider set
1137 if (!data->lmd_unref && LDLM_HAVE_MASK(lock, GONE))
1138 return INTERVAL_ITER_CONT;
1140 if (!equi(data->lmd_flags & LDLM_FL_LOCAL_ONLY, ldlm_is_local(lock)))
1141 return INTERVAL_ITER_CONT;
1143 if (data->lmd_flags & LDLM_FL_TEST_LOCK) {
1144 LDLM_LOCK_GET(lock);
1145 ldlm_lock_touch_in_lru(lock);
1147 ldlm_lock_addref_internal_nolock(lock, match);
1150 *data->lmd_mode = match;
1151 data->lmd_lock = lock;
1153 return INTERVAL_ITER_STOP;
1156 static enum interval_iter itree_overlap_cb(struct interval_node *in, void *args)
1158 struct ldlm_interval *node = to_ldlm_interval(in);
1159 struct lock_match_data *data = args;
1160 struct ldlm_lock *lock;
1163 list_for_each_entry(lock, &node->li_group, l_sl_policy) {
1164 rc = lock_matches(lock, data);
1165 if (rc == INTERVAL_ITER_STOP)
1166 return INTERVAL_ITER_STOP;
1168 return INTERVAL_ITER_CONT;
1172 * Search for a lock with given parameters in interval trees.
1174 * \param res search for a lock in this resource
1175 * \param data parameters
1177 * \retval a referenced lock or NULL.
1179 static struct ldlm_lock *search_itree(struct ldlm_resource *res,
1180 struct lock_match_data *data)
1182 struct interval_node_extent ext = {
1183 .start = data->lmd_policy->l_extent.start,
1184 .end = data->lmd_policy->l_extent.end
1188 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
1189 struct ldlm_interval_tree *tree = &res->lr_itree[idx];
1191 if (!tree->lit_root)
1194 if (!(tree->lit_mode & *data->lmd_mode))
1197 interval_search(tree->lit_root, &ext,
1198 itree_overlap_cb, data);
1200 return data->lmd_lock;
1204 * Search for a lock with given properties in a queue.
1206 * \param queue search for a lock in this queue
1207 * \param data parameters
1209 * \retval a referenced lock or NULL.
1211 static struct ldlm_lock *search_queue(struct list_head *queue,
1212 struct lock_match_data *data)
1214 struct ldlm_lock *lock;
1217 list_for_each_entry(lock, queue, l_res_link) {
1218 rc = lock_matches(lock, data);
1219 if (rc == INTERVAL_ITER_STOP)
1220 return data->lmd_lock;
1225 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock)
1227 if ((lock->l_flags & LDLM_FL_FAIL_NOTIFIED) == 0) {
1228 lock->l_flags |= LDLM_FL_FAIL_NOTIFIED;
1229 wake_up_all(&lock->l_waitq);
1234 * Mark lock as "matchable" by OST.
1236 * Used to prevent certain races in LOV/OSC where the lock is granted, but LVB
1238 * Assumes LDLM lock is already locked.
1240 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1242 ldlm_set_lvb_ready(lock);
1243 wake_up_all(&lock->l_waitq);
1245 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
1248 * Mark lock as "matchable" by OST.
1249 * Locks the lock and then \see ldlm_lock_allow_match_locked
1251 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1253 lock_res_and_lock(lock);
1254 ldlm_lock_allow_match_locked(lock);
1255 unlock_res_and_lock(lock);
1257 EXPORT_SYMBOL(ldlm_lock_allow_match);
1260 * Attempt to find a lock with specified properties.
1262 * Typically returns a reference to matched lock unless LDLM_FL_TEST_LOCK is
1265 * Can be called in two ways:
1267 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1268 * for a duplicate of.
1270 * Otherwise, all of the fields must be filled in, to match against.
1272 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1273 * server (ie, connh is NULL)
1274 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1275 * list will be considered
1276 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1277 * to be canceled can still be matched as long as they still have reader
1278 * or writer referneces
1279 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1280 * just tell us if we would have matched.
1282 * \retval 1 if it finds an already-existing lock that is compatible; in this
1283 * case, lockh is filled in with a addref()ed lock
1285 * We also check security context, and if that fails we simply return 0 (to
1286 * keep caller code unchanged), the context failure will be discovered by
1287 * caller sometime later.
1289 enum ldlm_mode ldlm_lock_match(struct ldlm_namespace *ns, __u64 flags,
1290 const struct ldlm_res_id *res_id,
1291 enum ldlm_type type,
1292 union ldlm_policy_data *policy,
1293 enum ldlm_mode mode,
1294 struct lustre_handle *lockh, int unref)
1296 struct lock_match_data data = {
1300 .lmd_policy = policy,
1304 struct ldlm_resource *res;
1305 struct ldlm_lock *lock;
1309 data.lmd_old = ldlm_handle2lock(lockh);
1310 LASSERT(data.lmd_old);
1312 ns = ldlm_lock_to_ns(data.lmd_old);
1313 res_id = &data.lmd_old->l_resource->lr_name;
1314 type = data.lmd_old->l_resource->lr_type;
1315 *data.lmd_mode = data.lmd_old->l_req_mode;
1318 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1320 LASSERT(!data.lmd_old);
1324 LDLM_RESOURCE_ADDREF(res);
1327 if (res->lr_type == LDLM_EXTENT)
1328 lock = search_itree(res, &data);
1330 lock = search_queue(&res->lr_granted, &data);
1335 if (flags & LDLM_FL_BLOCK_GRANTED) {
1339 lock = search_queue(&res->lr_waiting, &data);
1346 LDLM_RESOURCE_DELREF(res);
1347 ldlm_resource_putref(res);
1350 ldlm_lock2handle(lock, lockh);
1351 if ((flags & LDLM_FL_LVB_READY) && !ldlm_is_lvb_ready(lock)) {
1352 __u64 wait_flags = LDLM_FL_LVB_READY |
1353 LDLM_FL_DESTROYED | LDLM_FL_FAIL_NOTIFIED;
1354 struct l_wait_info lwi;
1356 if (lock->l_completion_ast) {
1357 int err = lock->l_completion_ast(lock,
1358 LDLM_FL_WAIT_NOREPROC,
1361 if (flags & LDLM_FL_TEST_LOCK)
1362 LDLM_LOCK_RELEASE(lock);
1364 ldlm_lock_decref_internal(lock,
1371 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1372 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1374 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1375 l_wait_event(lock->l_waitq,
1376 lock->l_flags & wait_flags,
1378 if (!ldlm_is_lvb_ready(lock)) {
1379 if (flags & LDLM_FL_TEST_LOCK)
1380 LDLM_LOCK_RELEASE(lock);
1382 ldlm_lock_decref_internal(lock, mode);
1389 LDLM_DEBUG(lock, "matched (%llu %llu)",
1390 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1391 res_id->name[2] : policy->l_extent.start,
1392 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1393 res_id->name[3] : policy->l_extent.end);
1395 /* check user's security context */
1396 if (lock->l_conn_export &&
1397 sptlrpc_import_check_ctx(
1398 class_exp2cliimp(lock->l_conn_export))) {
1399 if (!(flags & LDLM_FL_TEST_LOCK))
1400 ldlm_lock_decref_internal(lock, mode);
1404 if (flags & LDLM_FL_TEST_LOCK)
1405 LDLM_LOCK_RELEASE(lock);
1407 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1408 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res %llu/%llu (%llu %llu)",
1409 ns, type, mode, res_id->name[0],
1411 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1412 res_id->name[2] : policy->l_extent.start,
1413 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1414 res_id->name[3] : policy->l_extent.end);
1417 LDLM_LOCK_PUT(data.lmd_old);
1419 return rc ? mode : 0;
1421 EXPORT_SYMBOL(ldlm_lock_match);
1423 enum ldlm_mode ldlm_revalidate_lock_handle(const struct lustre_handle *lockh,
1426 struct ldlm_lock *lock;
1427 enum ldlm_mode mode = 0;
1429 lock = ldlm_handle2lock(lockh);
1431 lock_res_and_lock(lock);
1432 if (LDLM_HAVE_MASK(lock, GONE))
1435 if (ldlm_is_cbpending(lock) &&
1436 lock->l_readers == 0 && lock->l_writers == 0)
1440 *bits = lock->l_policy_data.l_inodebits.bits;
1441 mode = lock->l_granted_mode;
1442 ldlm_lock_addref_internal_nolock(lock, mode);
1447 unlock_res_and_lock(lock);
1448 LDLM_LOCK_PUT(lock);
1452 EXPORT_SYMBOL(ldlm_revalidate_lock_handle);
1454 /** The caller must guarantee that the buffer is large enough. */
1455 int ldlm_fill_lvb(struct ldlm_lock *lock, struct req_capsule *pill,
1456 enum req_location loc, void *data, int size)
1463 switch (lock->l_lvb_type) {
1465 if (size == sizeof(struct ost_lvb)) {
1466 if (loc == RCL_CLIENT)
1467 lvb = req_capsule_client_swab_get(pill,
1469 lustre_swab_ost_lvb);
1471 lvb = req_capsule_server_swab_get(pill,
1473 lustre_swab_ost_lvb);
1474 if (unlikely(!lvb)) {
1475 LDLM_ERROR(lock, "no LVB");
1479 memcpy(data, lvb, size);
1480 } else if (size == sizeof(struct ost_lvb_v1)) {
1481 struct ost_lvb *olvb = data;
1483 if (loc == RCL_CLIENT)
1484 lvb = req_capsule_client_swab_get(pill,
1486 lustre_swab_ost_lvb_v1);
1488 lvb = req_capsule_server_sized_swab_get(pill,
1490 lustre_swab_ost_lvb_v1);
1491 if (unlikely(!lvb)) {
1492 LDLM_ERROR(lock, "no LVB");
1496 memcpy(data, lvb, size);
1497 olvb->lvb_mtime_ns = 0;
1498 olvb->lvb_atime_ns = 0;
1499 olvb->lvb_ctime_ns = 0;
1501 LDLM_ERROR(lock, "Replied unexpected ost LVB size %d",
1507 if (size == sizeof(struct lquota_lvb)) {
1508 if (loc == RCL_CLIENT)
1509 lvb = req_capsule_client_swab_get(pill,
1511 lustre_swab_lquota_lvb);
1513 lvb = req_capsule_server_swab_get(pill,
1515 lustre_swab_lquota_lvb);
1516 if (unlikely(!lvb)) {
1517 LDLM_ERROR(lock, "no LVB");
1521 memcpy(data, lvb, size);
1524 "Replied unexpected lquota LVB size %d",
1533 if (loc == RCL_CLIENT)
1534 lvb = req_capsule_client_get(pill, &RMF_DLM_LVB);
1536 lvb = req_capsule_server_get(pill, &RMF_DLM_LVB);
1537 if (unlikely(!lvb)) {
1538 LDLM_ERROR(lock, "no LVB");
1542 memcpy(data, lvb, size);
1545 LDLM_ERROR(lock, "Unknown LVB type: %d", lock->l_lvb_type);
1554 * Create and fill in new LDLM lock with specified properties.
1555 * Returns a referenced lock
1557 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1558 const struct ldlm_res_id *res_id,
1559 enum ldlm_type type,
1560 enum ldlm_mode mode,
1561 const struct ldlm_callback_suite *cbs,
1562 void *data, __u32 lvb_len,
1563 enum lvb_type lvb_type)
1565 struct ldlm_lock *lock;
1566 struct ldlm_resource *res;
1569 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1571 return ERR_CAST(res);
1573 lock = ldlm_lock_new(res);
1575 ldlm_resource_putref(res);
1576 return ERR_PTR(-ENOMEM);
1579 lock->l_req_mode = mode;
1580 lock->l_ast_data = data;
1581 lock->l_pid = current_pid();
1583 lock->l_blocking_ast = cbs->lcs_blocking;
1584 lock->l_completion_ast = cbs->lcs_completion;
1585 lock->l_glimpse_ast = cbs->lcs_glimpse;
1588 lock->l_tree_node = NULL;
1589 /* if this is the extent lock, allocate the interval tree node */
1590 if (type == LDLM_EXTENT) {
1591 if (!ldlm_interval_alloc(lock)) {
1598 lock->l_lvb_len = lvb_len;
1599 lock->l_lvb_data = kzalloc(lvb_len, GFP_NOFS);
1600 if (!lock->l_lvb_data) {
1606 lock->l_lvb_type = lvb_type;
1607 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK)) {
1615 ldlm_lock_destroy(lock);
1616 LDLM_LOCK_RELEASE(lock);
1623 * Enqueue (request) a lock.
1624 * On the client this is called from ldlm_cli_enqueue_fini
1625 * after we already got an initial reply from the server with some status.
1627 * Does not block. As a result of enqueue the lock would be put
1628 * into granted or waiting list.
1630 enum ldlm_error ldlm_lock_enqueue(struct ldlm_namespace *ns,
1631 struct ldlm_lock **lockp,
1632 void *cookie, __u64 *flags)
1634 struct ldlm_lock *lock = *lockp;
1635 struct ldlm_resource *res = lock->l_resource;
1637 lock_res_and_lock(lock);
1638 if (lock->l_req_mode == lock->l_granted_mode) {
1639 /* The server returned a blocked lock, but it was granted
1640 * before we got a chance to actually enqueue it. We don't
1641 * need to do anything else.
1643 *flags &= ~LDLM_FL_BLOCKED_MASK;
1647 ldlm_resource_unlink_lock(lock);
1649 /* Cannot happen unless on the server */
1650 if (res->lr_type == LDLM_EXTENT && !lock->l_tree_node)
1653 /* Some flags from the enqueue want to make it into the AST, via the
1656 if (*flags & LDLM_FL_AST_DISCARD_DATA)
1657 ldlm_set_ast_discard_data(lock);
1658 if (*flags & LDLM_FL_TEST_LOCK)
1659 ldlm_set_test_lock(lock);
1662 * This distinction between local lock trees is very important; a client
1663 * namespace only has information about locks taken by that client, and
1664 * thus doesn't have enough information to decide for itself if it can
1665 * be granted (below). In this case, we do exactly what the server
1666 * tells us to do, as dictated by the 'flags'.
1668 if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1669 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1671 ldlm_grant_lock(lock, NULL);
1674 unlock_res_and_lock(lock);
1679 * Process a call to blocking AST callback for a lock in ast_work list
1682 ldlm_work_bl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1684 struct ldlm_cb_set_arg *arg = opaq;
1685 struct ldlm_lock_desc d;
1687 struct ldlm_lock *lock;
1689 if (list_empty(arg->list))
1692 lock = list_entry(arg->list->next, struct ldlm_lock, l_bl_ast);
1694 /* nobody should touch l_bl_ast */
1695 lock_res_and_lock(lock);
1696 list_del_init(&lock->l_bl_ast);
1698 LASSERT(ldlm_is_ast_sent(lock));
1699 LASSERT(lock->l_bl_ast_run == 0);
1700 LASSERT(lock->l_blocking_lock);
1701 lock->l_bl_ast_run++;
1702 unlock_res_and_lock(lock);
1704 ldlm_lock2desc(lock->l_blocking_lock, &d);
1706 rc = lock->l_blocking_ast(lock, &d, (void *)arg, LDLM_CB_BLOCKING);
1707 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1708 lock->l_blocking_lock = NULL;
1709 LDLM_LOCK_RELEASE(lock);
1715 * Process a call to completion AST callback for a lock in ast_work list
1718 ldlm_work_cp_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1720 struct ldlm_cb_set_arg *arg = opaq;
1722 struct ldlm_lock *lock;
1723 ldlm_completion_callback completion_callback;
1725 if (list_empty(arg->list))
1728 lock = list_entry(arg->list->next, struct ldlm_lock, l_cp_ast);
1730 /* It's possible to receive a completion AST before we've set
1731 * the l_completion_ast pointer: either because the AST arrived
1732 * before the reply, or simply because there's a small race
1733 * window between receiving the reply and finishing the local
1734 * enqueue. (bug 842)
1736 * This can't happen with the blocking_ast, however, because we
1737 * will never call the local blocking_ast until we drop our
1738 * reader/writer reference, which we won't do until we get the
1739 * reply and finish enqueueing.
1742 /* nobody should touch l_cp_ast */
1743 lock_res_and_lock(lock);
1744 list_del_init(&lock->l_cp_ast);
1745 LASSERT(ldlm_is_cp_reqd(lock));
1746 /* save l_completion_ast since it can be changed by
1747 * mds_intent_policy(), see bug 14225
1749 completion_callback = lock->l_completion_ast;
1750 ldlm_clear_cp_reqd(lock);
1751 unlock_res_and_lock(lock);
1753 if (completion_callback)
1754 rc = completion_callback(lock, 0, (void *)arg);
1755 LDLM_LOCK_RELEASE(lock);
1761 * Process a call to revocation AST callback for a lock in ast_work list
1764 ldlm_work_revoke_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1766 struct ldlm_cb_set_arg *arg = opaq;
1767 struct ldlm_lock_desc desc;
1769 struct ldlm_lock *lock;
1771 if (list_empty(arg->list))
1774 lock = list_entry(arg->list->next, struct ldlm_lock, l_rk_ast);
1775 list_del_init(&lock->l_rk_ast);
1777 /* the desc just pretend to exclusive */
1778 ldlm_lock2desc(lock, &desc);
1779 desc.l_req_mode = LCK_EX;
1780 desc.l_granted_mode = 0;
1782 rc = lock->l_blocking_ast(lock, &desc, (void *)arg, LDLM_CB_BLOCKING);
1783 LDLM_LOCK_RELEASE(lock);
1789 * Process a call to glimpse AST callback for a lock in ast_work list
1791 static int ldlm_work_gl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1793 struct ldlm_cb_set_arg *arg = opaq;
1794 struct ldlm_glimpse_work *gl_work;
1795 struct ldlm_lock *lock;
1798 if (list_empty(arg->list))
1801 gl_work = list_entry(arg->list->next, struct ldlm_glimpse_work,
1803 list_del_init(&gl_work->gl_list);
1805 lock = gl_work->gl_lock;
1807 /* transfer the glimpse descriptor to ldlm_cb_set_arg */
1808 arg->gl_desc = gl_work->gl_desc;
1810 /* invoke the actual glimpse callback */
1811 if (lock->l_glimpse_ast(lock, (void *)arg) == 0)
1814 LDLM_LOCK_RELEASE(lock);
1816 if ((gl_work->gl_flags & LDLM_GL_WORK_NOFREE) == 0)
1823 * Process list of locks in need of ASTs being sent.
1825 * Used on server to send multiple ASTs together instead of sending one by
1828 int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list,
1829 enum ldlm_desc_ast_t ast_type)
1831 struct ldlm_cb_set_arg *arg;
1832 set_producer_func work_ast_lock;
1835 if (list_empty(rpc_list))
1838 arg = kzalloc(sizeof(*arg), GFP_NOFS);
1842 atomic_set(&arg->restart, 0);
1843 arg->list = rpc_list;
1846 case LDLM_WORK_BL_AST:
1847 arg->type = LDLM_BL_CALLBACK;
1848 work_ast_lock = ldlm_work_bl_ast_lock;
1850 case LDLM_WORK_CP_AST:
1851 arg->type = LDLM_CP_CALLBACK;
1852 work_ast_lock = ldlm_work_cp_ast_lock;
1854 case LDLM_WORK_REVOKE_AST:
1855 arg->type = LDLM_BL_CALLBACK;
1856 work_ast_lock = ldlm_work_revoke_ast_lock;
1858 case LDLM_WORK_GL_AST:
1859 arg->type = LDLM_GL_CALLBACK;
1860 work_ast_lock = ldlm_work_gl_ast_lock;
1866 /* We create a ptlrpc request set with flow control extension.
1867 * This request set will use the work_ast_lock function to produce new
1868 * requests and will send a new request each time one completes in order
1869 * to keep the number of requests in flight to ns_max_parallel_ast
1871 arg->set = ptlrpc_prep_fcset(ns->ns_max_parallel_ast ? : UINT_MAX,
1872 work_ast_lock, arg);
1878 ptlrpc_set_wait(arg->set);
1879 ptlrpc_set_destroy(arg->set);
1881 rc = atomic_read(&arg->restart) ? -ERESTART : 0;
1888 static bool is_bl_done(struct ldlm_lock *lock)
1890 bool bl_done = true;
1892 if (!ldlm_is_bl_done(lock)) {
1893 lock_res_and_lock(lock);
1894 bl_done = ldlm_is_bl_done(lock);
1895 unlock_res_and_lock(lock);
1902 * Helper function to call blocking AST for LDLM lock \a lock in a
1903 * "cancelling" mode.
1905 void ldlm_cancel_callback(struct ldlm_lock *lock)
1907 check_res_locked(lock->l_resource);
1908 if (!ldlm_is_cancel(lock)) {
1909 ldlm_set_cancel(lock);
1910 if (lock->l_blocking_ast) {
1911 unlock_res_and_lock(lock);
1912 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1914 lock_res_and_lock(lock);
1916 LDLM_DEBUG(lock, "no blocking ast");
1918 /* only canceller can set bl_done bit */
1919 ldlm_set_bl_done(lock);
1920 wake_up_all(&lock->l_waitq);
1921 } else if (!ldlm_is_bl_done(lock)) {
1922 struct l_wait_info lwi = { 0 };
1925 * The lock is guaranteed to have been canceled once
1926 * returning from this function.
1928 unlock_res_and_lock(lock);
1929 l_wait_event(lock->l_waitq, is_bl_done(lock), &lwi);
1930 lock_res_and_lock(lock);
1935 * Remove skiplist-enabled LDLM lock \a req from granted list
1937 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1939 if (req->l_resource->lr_type != LDLM_PLAIN &&
1940 req->l_resource->lr_type != LDLM_IBITS)
1943 list_del_init(&req->l_sl_policy);
1944 list_del_init(&req->l_sl_mode);
1948 * Attempts to cancel LDLM lock \a lock that has no reader/writer references.
1950 void ldlm_lock_cancel(struct ldlm_lock *lock)
1952 struct ldlm_resource *res;
1953 struct ldlm_namespace *ns;
1955 lock_res_and_lock(lock);
1957 res = lock->l_resource;
1958 ns = ldlm_res_to_ns(res);
1960 /* Please do not, no matter how tempting, remove this LBUG without
1961 * talking to me first. -phik
1963 if (lock->l_readers || lock->l_writers) {
1964 LDLM_ERROR(lock, "lock still has references");
1968 /* Releases cancel callback. */
1969 ldlm_cancel_callback(lock);
1971 ldlm_resource_unlink_lock(lock);
1972 ldlm_lock_destroy_nolock(lock);
1974 if (lock->l_granted_mode == lock->l_req_mode)
1975 ldlm_pool_del(&ns->ns_pool, lock);
1977 /* Make sure we will not be called again for same lock what is possible
1978 * if not to zero out lock->l_granted_mode
1980 lock->l_granted_mode = LCK_MINMODE;
1981 unlock_res_and_lock(lock);
1983 EXPORT_SYMBOL(ldlm_lock_cancel);
1986 * Set opaque data into the lock that only makes sense to upper layer.
1988 int ldlm_lock_set_data(const struct lustre_handle *lockh, void *data)
1990 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1994 if (!lock->l_ast_data)
1995 lock->l_ast_data = data;
1996 if (lock->l_ast_data == data)
1998 LDLM_LOCK_PUT(lock);
2002 EXPORT_SYMBOL(ldlm_lock_set_data);
2004 struct export_cl_data {
2005 struct obd_export *ecl_exp;
2010 * Print lock with lock handle \a lockh description into debug log.
2012 * Used when printing all locks on a resource for debug purposes.
2014 void ldlm_lock_dump_handle(int level, const struct lustre_handle *lockh)
2016 struct ldlm_lock *lock;
2018 if (!((libcfs_debug | D_ERROR) & level))
2021 lock = ldlm_handle2lock(lockh);
2025 LDLM_DEBUG_LIMIT(level, lock, "###");
2027 LDLM_LOCK_PUT(lock);
2029 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2032 * Print lock information with custom message into debug log.
2035 void _ldlm_lock_debug(struct ldlm_lock *lock,
2036 struct libcfs_debug_msg_data *msgdata,
2037 const char *fmt, ...)
2040 struct obd_export *exp = lock->l_export;
2041 struct ldlm_resource *resource = lock->l_resource;
2042 char *nid = "local";
2044 va_start(args, fmt);
2046 if (exp && exp->exp_connection) {
2047 nid = libcfs_nid2str(exp->exp_connection->c_peer.nid);
2048 } else if (exp && exp->exp_obd) {
2049 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
2051 nid = libcfs_nid2str(imp->imp_connection->c_peer.nid);
2055 libcfs_debug_vmsg2(msgdata, fmt, args,
2056 " 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",
2058 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
2059 lock->l_readers, lock->l_writers,
2060 ldlm_lockname[lock->l_granted_mode],
2061 ldlm_lockname[lock->l_req_mode],
2062 lock->l_flags, nid, lock->l_remote_handle.cookie,
2063 exp ? atomic_read(&exp->exp_refcount) : -99,
2064 lock->l_pid, lock->l_callback_timeout, lock->l_lvb_type);
2069 switch (resource->lr_type) {
2071 libcfs_debug_vmsg2(msgdata, fmt, args,
2072 " 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",
2073 ldlm_lock_to_ns_name(lock), lock,
2074 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
2075 lock->l_readers, lock->l_writers,
2076 ldlm_lockname[lock->l_granted_mode],
2077 ldlm_lockname[lock->l_req_mode],
2079 atomic_read(&resource->lr_refcount),
2080 ldlm_typename[resource->lr_type],
2081 lock->l_policy_data.l_extent.start,
2082 lock->l_policy_data.l_extent.end,
2083 lock->l_req_extent.start, lock->l_req_extent.end,
2084 lock->l_flags, nid, lock->l_remote_handle.cookie,
2085 exp ? atomic_read(&exp->exp_refcount) : -99,
2086 lock->l_pid, lock->l_callback_timeout,
2091 libcfs_debug_vmsg2(msgdata, fmt, args,
2092 " 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",
2093 ldlm_lock_to_ns_name(lock), lock,
2094 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
2095 lock->l_readers, lock->l_writers,
2096 ldlm_lockname[lock->l_granted_mode],
2097 ldlm_lockname[lock->l_req_mode],
2099 atomic_read(&resource->lr_refcount),
2100 ldlm_typename[resource->lr_type],
2101 lock->l_policy_data.l_flock.pid,
2102 lock->l_policy_data.l_flock.start,
2103 lock->l_policy_data.l_flock.end,
2104 lock->l_flags, nid, lock->l_remote_handle.cookie,
2105 exp ? atomic_read(&exp->exp_refcount) : -99,
2106 lock->l_pid, lock->l_callback_timeout);
2110 libcfs_debug_vmsg2(msgdata, fmt, args,
2111 " 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",
2112 ldlm_lock_to_ns_name(lock),
2113 lock, lock->l_handle.h_cookie,
2114 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],
2119 lock->l_policy_data.l_inodebits.bits,
2120 atomic_read(&resource->lr_refcount),
2121 ldlm_typename[resource->lr_type],
2122 lock->l_flags, nid, lock->l_remote_handle.cookie,
2123 exp ? atomic_read(&exp->exp_refcount) : -99,
2124 lock->l_pid, lock->l_callback_timeout,
2129 libcfs_debug_vmsg2(msgdata, fmt, args,
2130 " 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",
2131 ldlm_lock_to_ns_name(lock),
2132 lock, lock->l_handle.h_cookie,
2133 atomic_read(&lock->l_refc),
2134 lock->l_readers, lock->l_writers,
2135 ldlm_lockname[lock->l_granted_mode],
2136 ldlm_lockname[lock->l_req_mode],
2138 atomic_read(&resource->lr_refcount),
2139 ldlm_typename[resource->lr_type],
2140 lock->l_flags, nid, lock->l_remote_handle.cookie,
2141 exp ? atomic_read(&exp->exp_refcount) : -99,
2142 lock->l_pid, lock->l_callback_timeout,
2148 EXPORT_SYMBOL(_ldlm_lock_debug);