4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lock.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_LDLM
44 #include "../../include/linux/libcfs/libcfs.h"
45 #include "../include/lustre_intent.h"
46 #include "../include/obd_class.h"
47 #include "ldlm_internal.h"
50 char *ldlm_lockname[] = {
58 [LCK_GROUP] = "GROUP",
61 EXPORT_SYMBOL(ldlm_lockname);
63 static char *ldlm_typename[] = {
65 [LDLM_EXTENT] = "EXT",
70 static ldlm_policy_wire_to_local_t ldlm_policy_wire18_to_local[] = {
71 [LDLM_PLAIN - LDLM_MIN_TYPE] = ldlm_plain_policy_wire_to_local,
72 [LDLM_EXTENT - LDLM_MIN_TYPE] = ldlm_extent_policy_wire_to_local,
73 [LDLM_FLOCK - LDLM_MIN_TYPE] = ldlm_flock_policy_wire18_to_local,
74 [LDLM_IBITS - LDLM_MIN_TYPE] = ldlm_ibits_policy_wire_to_local,
77 static ldlm_policy_wire_to_local_t ldlm_policy_wire21_to_local[] = {
78 [LDLM_PLAIN - LDLM_MIN_TYPE] = ldlm_plain_policy_wire_to_local,
79 [LDLM_EXTENT - LDLM_MIN_TYPE] = ldlm_extent_policy_wire_to_local,
80 [LDLM_FLOCK - LDLM_MIN_TYPE] = ldlm_flock_policy_wire21_to_local,
81 [LDLM_IBITS - LDLM_MIN_TYPE] = ldlm_ibits_policy_wire_to_local,
84 static ldlm_policy_local_to_wire_t ldlm_policy_local_to_wire[] = {
85 [LDLM_PLAIN - LDLM_MIN_TYPE] = ldlm_plain_policy_local_to_wire,
86 [LDLM_EXTENT - LDLM_MIN_TYPE] = ldlm_extent_policy_local_to_wire,
87 [LDLM_FLOCK - LDLM_MIN_TYPE] = ldlm_flock_policy_local_to_wire,
88 [LDLM_IBITS - LDLM_MIN_TYPE] = ldlm_ibits_policy_local_to_wire,
92 * Converts lock policy from local format to on the wire lock_desc format
94 static void ldlm_convert_policy_to_wire(enum ldlm_type type,
95 const ldlm_policy_data_t *lpolicy,
96 ldlm_wire_policy_data_t *wpolicy)
98 ldlm_policy_local_to_wire_t convert;
100 convert = ldlm_policy_local_to_wire[type - LDLM_MIN_TYPE];
102 convert(lpolicy, wpolicy);
106 * Converts lock policy from on the wire lock_desc format to local format
108 void ldlm_convert_policy_to_local(struct obd_export *exp, enum ldlm_type type,
109 const ldlm_wire_policy_data_t *wpolicy,
110 ldlm_policy_data_t *lpolicy)
112 ldlm_policy_wire_to_local_t convert;
115 /** some badness for 2.0.0 clients, but 2.0.0 isn't supported */
116 new_client = (exp_connect_flags(exp) & OBD_CONNECT_FULL20) != 0;
118 convert = ldlm_policy_wire21_to_local[type - LDLM_MIN_TYPE];
120 convert = ldlm_policy_wire18_to_local[type - LDLM_MIN_TYPE];
122 convert(wpolicy, lpolicy);
125 char *ldlm_it2str(int it)
132 case (IT_OPEN | IT_CREAT):
147 CERROR("Unknown intent %d\n", it);
151 EXPORT_SYMBOL(ldlm_it2str);
154 * REFCOUNTED LOCK OBJECTS
158 * Get a reference on a lock.
160 * Lock refcounts, during creation:
161 * - one special one for allocation, dec'd only once in destroy
162 * - one for being a lock that's in-use
163 * - one for the addref associated with a new lock
165 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
167 atomic_inc(&lock->l_refc);
170 EXPORT_SYMBOL(ldlm_lock_get);
173 * Release lock reference.
175 * Also frees the lock if it was last reference.
177 void ldlm_lock_put(struct ldlm_lock *lock)
179 LASSERT(lock->l_resource != LP_POISON);
180 LASSERT(atomic_read(&lock->l_refc) > 0);
181 if (atomic_dec_and_test(&lock->l_refc)) {
182 struct ldlm_resource *res;
185 "final lock_put on destroyed lock, freeing it.");
187 res = lock->l_resource;
188 LASSERT(ldlm_is_destroyed(lock));
189 LASSERT(list_empty(&lock->l_res_link));
190 LASSERT(list_empty(&lock->l_pending_chain));
192 lprocfs_counter_decr(ldlm_res_to_ns(res)->ns_stats,
194 lu_ref_del(&res->lr_reference, "lock", lock);
195 ldlm_resource_putref(res);
196 lock->l_resource = NULL;
197 if (lock->l_export) {
198 class_export_lock_put(lock->l_export, lock);
199 lock->l_export = NULL;
202 kfree(lock->l_lvb_data);
204 ldlm_interval_free(ldlm_interval_detach(lock));
205 lu_ref_fini(&lock->l_reference);
206 OBD_FREE_RCU(lock, sizeof(*lock), &lock->l_handle);
209 EXPORT_SYMBOL(ldlm_lock_put);
212 * Removes LDLM lock \a lock from LRU. Assumes LRU is already locked.
214 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
218 if (!list_empty(&lock->l_lru)) {
219 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
221 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
222 list_del_init(&lock->l_lru);
223 LASSERT(ns->ns_nr_unused > 0);
231 * Removes LDLM lock \a lock from LRU. Obtains the LRU lock first.
233 * If \a last_use is non-zero, it will remove the lock from LRU only if
234 * it matches lock's l_last_used.
236 * \retval 0 if \a last_use is set, the lock is not in LRU list or \a last_use
237 * doesn't match lock's l_last_used;
238 * otherwise, the lock hasn't been in the LRU list.
239 * \retval 1 the lock was in LRU list and removed.
241 int ldlm_lock_remove_from_lru_check(struct ldlm_lock *lock, time_t last_use)
243 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
246 spin_lock(&ns->ns_lock);
247 if (last_use == 0 || last_use == lock->l_last_used)
248 rc = ldlm_lock_remove_from_lru_nolock(lock);
249 spin_unlock(&ns->ns_lock);
255 * Adds LDLM lock \a lock to namespace LRU. Assumes LRU is already locked.
257 static void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
259 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
261 lock->l_last_used = cfs_time_current();
262 LASSERT(list_empty(&lock->l_lru));
263 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
264 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
265 ldlm_clear_skipped(lock);
266 LASSERT(ns->ns_nr_unused >= 0);
271 * Adds LDLM lock \a lock to namespace LRU. Obtains necessary LRU locks
274 static void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
276 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
278 spin_lock(&ns->ns_lock);
279 ldlm_lock_add_to_lru_nolock(lock);
280 spin_unlock(&ns->ns_lock);
284 * Moves LDLM lock \a lock that is already in namespace LRU to the tail of
285 * the LRU. Performs necessary LRU locking
287 static void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
289 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
291 spin_lock(&ns->ns_lock);
292 if (!list_empty(&lock->l_lru)) {
293 ldlm_lock_remove_from_lru_nolock(lock);
294 ldlm_lock_add_to_lru_nolock(lock);
296 spin_unlock(&ns->ns_lock);
300 * Helper to destroy a locked lock.
302 * Used by ldlm_lock_destroy and ldlm_lock_destroy_nolock
303 * Must be called with l_lock and lr_lock held.
305 * Does not actually free the lock data, but rather marks the lock as
306 * destroyed by setting l_destroyed field in the lock to 1. Destroys a
307 * handle->lock association too, so that the lock can no longer be found
308 * and removes the lock from LRU list. Actual lock freeing occurs when
309 * last lock reference goes away.
311 * Original comment (of some historical value):
312 * This used to have a 'strict' flag, which recovery would use to mark an
313 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
314 * shall explain why it's gone: with the new hash table scheme, once you call
315 * ldlm_lock_destroy, you can never drop your final references on this lock.
316 * Because it's not in the hash table anymore. -phil
318 static int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
320 if (lock->l_readers || lock->l_writers) {
321 LDLM_ERROR(lock, "lock still has references");
325 if (!list_empty(&lock->l_res_link)) {
326 LDLM_ERROR(lock, "lock still on resource");
330 if (ldlm_is_destroyed(lock)) {
331 LASSERT(list_empty(&lock->l_lru));
334 ldlm_set_destroyed(lock);
336 if (lock->l_export && lock->l_export->exp_lock_hash) {
337 /* NB: it's safe to call cfs_hash_del() even lock isn't
340 /* In the function below, .hs_keycmp resolves to
341 * ldlm_export_lock_keycmp()
343 /* coverity[overrun-buffer-val] */
344 cfs_hash_del(lock->l_export->exp_lock_hash,
345 &lock->l_remote_handle, &lock->l_exp_hash);
348 ldlm_lock_remove_from_lru(lock);
349 class_handle_unhash(&lock->l_handle);
355 * Destroys a LDLM lock \a lock. Performs necessary locking first.
357 static void ldlm_lock_destroy(struct ldlm_lock *lock)
361 lock_res_and_lock(lock);
362 first = ldlm_lock_destroy_internal(lock);
363 unlock_res_and_lock(lock);
365 /* drop reference from hashtable only for first destroy */
367 lu_ref_del(&lock->l_reference, "hash", lock);
368 LDLM_LOCK_RELEASE(lock);
373 * Destroys a LDLM lock \a lock that is already locked.
375 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
379 first = ldlm_lock_destroy_internal(lock);
380 /* drop reference from hashtable only for first destroy */
382 lu_ref_del(&lock->l_reference, "hash", lock);
383 LDLM_LOCK_RELEASE(lock);
387 /* this is called by portals_handle2object with the handle lock taken */
388 static void lock_handle_addref(void *lock)
390 LDLM_LOCK_GET((struct ldlm_lock *)lock);
393 static void lock_handle_free(void *lock, int size)
395 LASSERT(size == sizeof(struct ldlm_lock));
396 kmem_cache_free(ldlm_lock_slab, lock);
399 static struct portals_handle_ops lock_handle_ops = {
400 .hop_addref = lock_handle_addref,
401 .hop_free = lock_handle_free,
406 * Allocate and initialize new lock structure.
408 * usage: pass in a resource on which you have done ldlm_resource_get
409 * new lock will take over the refcount.
410 * returns: lock with refcount 2 - one for current caller and one for remote
412 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
414 struct ldlm_lock *lock;
418 lock = kmem_cache_zalloc(ldlm_lock_slab, GFP_NOFS);
422 spin_lock_init(&lock->l_lock);
423 lock->l_resource = resource;
424 lu_ref_add(&resource->lr_reference, "lock", lock);
426 atomic_set(&lock->l_refc, 2);
427 INIT_LIST_HEAD(&lock->l_res_link);
428 INIT_LIST_HEAD(&lock->l_lru);
429 INIT_LIST_HEAD(&lock->l_pending_chain);
430 INIT_LIST_HEAD(&lock->l_bl_ast);
431 INIT_LIST_HEAD(&lock->l_cp_ast);
432 INIT_LIST_HEAD(&lock->l_rk_ast);
433 init_waitqueue_head(&lock->l_waitq);
434 lock->l_blocking_lock = NULL;
435 INIT_LIST_HEAD(&lock->l_sl_mode);
436 INIT_LIST_HEAD(&lock->l_sl_policy);
437 INIT_HLIST_NODE(&lock->l_exp_hash);
438 INIT_HLIST_NODE(&lock->l_exp_flock_hash);
440 lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
442 INIT_LIST_HEAD(&lock->l_handle.h_link);
443 class_handle_hash(&lock->l_handle, &lock_handle_ops);
445 lu_ref_init(&lock->l_reference);
446 lu_ref_add(&lock->l_reference, "hash", lock);
447 lock->l_callback_timeout = 0;
449 #if LUSTRE_TRACKS_LOCK_EXP_REFS
450 INIT_LIST_HEAD(&lock->l_exp_refs_link);
451 lock->l_exp_refs_nr = 0;
452 lock->l_exp_refs_target = NULL;
454 INIT_LIST_HEAD(&lock->l_exp_list);
460 * Moves LDLM lock \a lock to another resource.
461 * This is used on client when server returns some other lock than requested
462 * (typically as a result of intent operation)
464 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
465 const struct ldlm_res_id *new_resid)
467 struct ldlm_resource *oldres = lock->l_resource;
468 struct ldlm_resource *newres;
471 lock_res_and_lock(lock);
472 if (memcmp(new_resid, &lock->l_resource->lr_name,
473 sizeof(lock->l_resource->lr_name)) == 0) {
475 unlock_res_and_lock(lock);
479 LASSERT(new_resid->name[0] != 0);
481 /* This function assumes that the lock isn't on any lists */
482 LASSERT(list_empty(&lock->l_res_link));
484 type = oldres->lr_type;
485 unlock_res_and_lock(lock);
487 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
491 lu_ref_add(&newres->lr_reference, "lock", lock);
493 * To flip the lock from the old to the new resource, lock, oldres and
494 * newres have to be locked. Resource spin-locks are nested within
495 * lock->l_lock, and are taken in the memory address order to avoid
498 spin_lock(&lock->l_lock);
499 oldres = lock->l_resource;
500 if (oldres < newres) {
502 lock_res_nested(newres, LRT_NEW);
505 lock_res_nested(oldres, LRT_NEW);
507 LASSERT(memcmp(new_resid, &oldres->lr_name,
508 sizeof(oldres->lr_name)) != 0);
509 lock->l_resource = newres;
511 unlock_res_and_lock(lock);
513 /* ...and the flowers are still standing! */
514 lu_ref_del(&oldres->lr_reference, "lock", lock);
515 ldlm_resource_putref(oldres);
519 EXPORT_SYMBOL(ldlm_lock_change_resource);
521 /** \defgroup ldlm_handles LDLM HANDLES
522 * Ways to get hold of locks without any addresses.
527 * Fills in handle for LDLM lock \a lock into supplied \a lockh
528 * Does not take any references.
530 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
532 lockh->cookie = lock->l_handle.h_cookie;
534 EXPORT_SYMBOL(ldlm_lock2handle);
537 * Obtain a lock reference by handle.
539 * if \a flags: atomically get the lock and set the flags.
540 * Return NULL if flag already set
542 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
545 struct ldlm_lock *lock;
549 lock = class_handle2object(handle->cookie);
553 /* It's unlikely but possible that someone marked the lock as
554 * destroyed after we did handle2object on it
556 if (flags == 0 && !ldlm_is_destroyed(lock)) {
557 lu_ref_add(&lock->l_reference, "handle", current);
561 lock_res_and_lock(lock);
563 LASSERT(lock->l_resource);
565 lu_ref_add_atomic(&lock->l_reference, "handle", current);
566 if (unlikely(ldlm_is_destroyed(lock))) {
567 unlock_res_and_lock(lock);
568 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
574 if (lock->l_flags & flags) {
575 unlock_res_and_lock(lock);
580 lock->l_flags |= flags;
583 unlock_res_and_lock(lock);
586 EXPORT_SYMBOL(__ldlm_handle2lock);
587 /** @} ldlm_handles */
590 * Fill in "on the wire" representation for given LDLM lock into supplied
591 * lock descriptor \a desc structure.
593 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
595 ldlm_res2desc(lock->l_resource, &desc->l_resource);
596 desc->l_req_mode = lock->l_req_mode;
597 desc->l_granted_mode = lock->l_granted_mode;
598 ldlm_convert_policy_to_wire(lock->l_resource->lr_type,
599 &lock->l_policy_data,
600 &desc->l_policy_data);
602 EXPORT_SYMBOL(ldlm_lock2desc);
605 * Add a lock to list of conflicting locks to send AST to.
607 * Only add if we have not sent a blocking AST to the lock yet.
609 static void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
610 struct list_head *work_list)
612 if (!ldlm_is_ast_sent(lock)) {
613 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
614 ldlm_set_ast_sent(lock);
615 /* If the enqueuing client said so, tell the AST recipient to
616 * discard dirty data, rather than writing back.
618 if (ldlm_is_ast_discard_data(new))
619 ldlm_set_discard_data(lock);
620 LASSERT(list_empty(&lock->l_bl_ast));
621 list_add(&lock->l_bl_ast, work_list);
623 LASSERT(!lock->l_blocking_lock);
624 lock->l_blocking_lock = LDLM_LOCK_GET(new);
629 * Add a lock to list of just granted locks to send completion AST to.
631 static void ldlm_add_cp_work_item(struct ldlm_lock *lock,
632 struct list_head *work_list)
634 if (!ldlm_is_cp_reqd(lock)) {
635 ldlm_set_cp_reqd(lock);
636 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
637 LASSERT(list_empty(&lock->l_cp_ast));
638 list_add(&lock->l_cp_ast, work_list);
644 * Aggregator function to add AST work items into a list. Determines
645 * what sort of an AST work needs to be done and calls the proper
647 * Must be called with lr_lock held.
649 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
650 struct ldlm_lock *new,
651 struct list_head *work_list)
653 check_res_locked(lock->l_resource);
655 ldlm_add_bl_work_item(lock, new, work_list);
657 ldlm_add_cp_work_item(lock, work_list);
661 * Add specified reader/writer reference to LDLM lock with handle \a lockh.
662 * r/w reference type is determined by \a mode
663 * Calls ldlm_lock_addref_internal.
665 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
667 struct ldlm_lock *lock;
669 lock = ldlm_handle2lock(lockh);
670 LASSERTF(lock, "Non-existing lock: %llx\n", lockh->cookie);
671 ldlm_lock_addref_internal(lock, mode);
674 EXPORT_SYMBOL(ldlm_lock_addref);
678 * Add specified reader/writer reference to LDLM lock \a lock.
679 * r/w reference type is determined by \a mode
680 * Removes lock from LRU if it is there.
681 * Assumes the LDLM lock is already locked.
683 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
685 ldlm_lock_remove_from_lru(lock);
686 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
688 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
690 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
692 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
695 lu_ref_add_atomic(&lock->l_reference, "user", lock);
696 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
700 * Attempts to add reader/writer reference to a lock with handle \a lockh, and
701 * fails if lock is already LDLM_FL_CBPENDING or destroyed.
703 * \retval 0 success, lock was addref-ed
705 * \retval -EAGAIN lock is being canceled.
707 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
709 struct ldlm_lock *lock;
713 lock = ldlm_handle2lock(lockh);
715 lock_res_and_lock(lock);
716 if (lock->l_readers != 0 || lock->l_writers != 0 ||
717 !ldlm_is_cbpending(lock)) {
718 ldlm_lock_addref_internal_nolock(lock, mode);
721 unlock_res_and_lock(lock);
726 EXPORT_SYMBOL(ldlm_lock_addref_try);
729 * Add specified reader/writer reference to LDLM lock \a lock.
730 * Locks LDLM lock and calls ldlm_lock_addref_internal_nolock to do the work.
731 * Only called for local locks.
733 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
735 lock_res_and_lock(lock);
736 ldlm_lock_addref_internal_nolock(lock, mode);
737 unlock_res_and_lock(lock);
741 * Removes reader/writer reference for LDLM lock \a lock.
742 * Assumes LDLM lock is already locked.
743 * only called in ldlm_flock_destroy and for local locks.
744 * Does NOT add lock to LRU if no r/w references left to accommodate flock locks
745 * that cannot be placed in LRU.
747 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
749 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
750 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
751 LASSERT(lock->l_readers > 0);
752 lu_ref_del(&lock->l_reference, "reader", lock);
755 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
756 LASSERT(lock->l_writers > 0);
757 lu_ref_del(&lock->l_reference, "writer", lock);
761 lu_ref_del(&lock->l_reference, "user", lock);
762 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
766 * Removes reader/writer reference for LDLM lock \a lock.
767 * Locks LDLM lock first.
768 * If the lock is determined to be client lock on a client and r/w refcount
769 * drops to zero and the lock is not blocked, the lock is added to LRU lock
771 * For blocked LDLM locks if r/w count drops to zero, blocking_ast is called.
773 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
775 struct ldlm_namespace *ns;
777 lock_res_and_lock(lock);
779 ns = ldlm_lock_to_ns(lock);
781 ldlm_lock_decref_internal_nolock(lock, mode);
783 if (ldlm_is_local(lock) &&
784 !lock->l_readers && !lock->l_writers) {
785 /* If this is a local lock on a server namespace and this was
786 * the last reference, cancel the lock.
788 CDEBUG(D_INFO, "forcing cancel of local lock\n");
789 ldlm_set_cbpending(lock);
792 if (!lock->l_readers && !lock->l_writers &&
793 ldlm_is_cbpending(lock)) {
794 /* If we received a blocked AST and this was the last reference,
798 LDLM_DEBUG(lock, "final decref done on cbpending lock");
800 LDLM_LOCK_GET(lock); /* dropped by bl thread */
801 ldlm_lock_remove_from_lru(lock);
802 unlock_res_and_lock(lock);
804 if (ldlm_is_fail_loc(lock))
805 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
807 if (ldlm_is_atomic_cb(lock) ||
808 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
809 ldlm_handle_bl_callback(ns, NULL, lock);
810 } else if (!lock->l_readers && !lock->l_writers &&
811 !ldlm_is_no_lru(lock) && !ldlm_is_bl_ast(lock)) {
812 LDLM_DEBUG(lock, "add lock into lru list");
814 /* If this is a client-side namespace and this was the last
815 * reference, put it on the LRU.
817 ldlm_lock_add_to_lru(lock);
818 unlock_res_and_lock(lock);
820 if (ldlm_is_fail_loc(lock))
821 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
823 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
824 * are not supported by the server, otherwise, it is done on
827 if (!exp_connect_cancelset(lock->l_conn_export) &&
828 !ns_connect_lru_resize(ns))
829 ldlm_cancel_lru(ns, 0, LCF_ASYNC, 0);
831 LDLM_DEBUG(lock, "do not add lock into lru list");
832 unlock_res_and_lock(lock);
837 * Decrease reader/writer refcount for LDLM lock with handle \a lockh
839 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
841 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
843 LASSERTF(lock, "Non-existing lock: %#llx\n", lockh->cookie);
844 ldlm_lock_decref_internal(lock, mode);
847 EXPORT_SYMBOL(ldlm_lock_decref);
850 * Decrease reader/writer refcount for LDLM lock with handle
851 * \a lockh and mark it for subsequent cancellation once r/w refcount
852 * drops to zero instead of putting into LRU.
854 * Typical usage is for GROUP locks which we cannot allow to be cached.
856 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
858 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
862 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
863 lock_res_and_lock(lock);
864 ldlm_set_cbpending(lock);
865 unlock_res_and_lock(lock);
866 ldlm_lock_decref_internal(lock, mode);
869 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
871 struct sl_insert_point {
872 struct list_head *res_link;
873 struct list_head *mode_link;
874 struct list_head *policy_link;
878 * Finds a position to insert the new lock into granted lock list.
880 * Used for locks eligible for skiplist optimization.
883 * queue [input]: the granted list where search acts on;
884 * req [input]: the lock whose position to be located;
885 * prev [output]: positions within 3 lists to insert @req to
889 * - ldlm_grant_lock_with_skiplist
891 static void search_granted_lock(struct list_head *queue,
892 struct ldlm_lock *req,
893 struct sl_insert_point *prev)
895 struct list_head *tmp;
896 struct ldlm_lock *lock, *mode_end, *policy_end;
898 list_for_each(tmp, queue) {
899 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
901 mode_end = list_prev_entry(lock, l_sl_mode);
903 if (lock->l_req_mode != req->l_req_mode) {
904 /* jump to last lock of mode group */
905 tmp = &mode_end->l_res_link;
909 /* suitable mode group is found */
910 if (lock->l_resource->lr_type == LDLM_PLAIN) {
911 /* insert point is last lock of the mode group */
912 prev->res_link = &mode_end->l_res_link;
913 prev->mode_link = &mode_end->l_sl_mode;
914 prev->policy_link = &req->l_sl_policy;
918 if (lock->l_resource->lr_type == LDLM_IBITS) {
921 list_prev_entry(lock, l_sl_policy);
923 if (lock->l_policy_data.l_inodebits.bits ==
924 req->l_policy_data.l_inodebits.bits) {
925 /* insert point is last lock of
929 &policy_end->l_res_link;
931 &policy_end->l_sl_mode;
933 &policy_end->l_sl_policy;
937 if (policy_end == mode_end)
938 /* done with mode group */
941 /* go to next policy group within mode group */
942 tmp = policy_end->l_res_link.next;
943 lock = list_entry(tmp, struct ldlm_lock,
945 } /* loop over policy groups within the mode group */
947 /* insert point is last lock of the mode group,
948 * new policy group is started
950 prev->res_link = &mode_end->l_res_link;
951 prev->mode_link = &mode_end->l_sl_mode;
952 prev->policy_link = &req->l_sl_policy;
956 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
960 /* insert point is last lock on the queue,
961 * new mode group and new policy group are started
963 prev->res_link = queue->prev;
964 prev->mode_link = &req->l_sl_mode;
965 prev->policy_link = &req->l_sl_policy;
969 * Add a lock into resource granted list after a position described by
972 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
973 struct sl_insert_point *prev)
975 struct ldlm_resource *res = lock->l_resource;
977 check_res_locked(res);
979 ldlm_resource_dump(D_INFO, res);
980 LDLM_DEBUG(lock, "About to add lock:");
982 if (ldlm_is_destroyed(lock)) {
983 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
987 LASSERT(list_empty(&lock->l_res_link));
988 LASSERT(list_empty(&lock->l_sl_mode));
989 LASSERT(list_empty(&lock->l_sl_policy));
992 * lock->link == prev->link means lock is first starting the group.
993 * Don't re-add to itself to suppress kernel warnings.
995 if (&lock->l_res_link != prev->res_link)
996 list_add(&lock->l_res_link, prev->res_link);
997 if (&lock->l_sl_mode != prev->mode_link)
998 list_add(&lock->l_sl_mode, prev->mode_link);
999 if (&lock->l_sl_policy != prev->policy_link)
1000 list_add(&lock->l_sl_policy, prev->policy_link);
1004 * Add a lock to granted list on a resource maintaining skiplist
1007 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
1009 struct sl_insert_point prev;
1011 LASSERT(lock->l_req_mode == lock->l_granted_mode);
1013 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
1014 ldlm_granted_list_add_lock(lock, &prev);
1018 * Perform lock granting bookkeeping.
1020 * Includes putting the lock into granted list and updating lock mode.
1022 * - ldlm_lock_enqueue
1023 * - ldlm_reprocess_queue
1024 * - ldlm_lock_convert
1026 * must be called with lr_lock held
1028 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
1030 struct ldlm_resource *res = lock->l_resource;
1032 check_res_locked(res);
1034 lock->l_granted_mode = lock->l_req_mode;
1035 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
1036 ldlm_grant_lock_with_skiplist(lock);
1037 else if (res->lr_type == LDLM_EXTENT)
1038 ldlm_extent_add_lock(res, lock);
1040 ldlm_resource_add_lock(res, &res->lr_granted, lock);
1042 if (work_list && lock->l_completion_ast)
1043 ldlm_add_ast_work_item(lock, NULL, work_list);
1045 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
1049 * Search for a lock with given properties in a queue.
1051 * \retval a referenced lock or NULL. See the flag descriptions below, in the
1052 * comment above ldlm_lock_match
1054 static struct ldlm_lock *search_queue(struct list_head *queue,
1055 enum ldlm_mode *mode,
1056 ldlm_policy_data_t *policy,
1057 struct ldlm_lock *old_lock,
1058 __u64 flags, int unref)
1060 struct ldlm_lock *lock;
1061 struct list_head *tmp;
1063 list_for_each(tmp, queue) {
1064 enum ldlm_mode match;
1066 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1068 if (lock == old_lock)
1071 /* Check if this lock can be matched.
1072 * Used by LU-2919(exclusive open) for open lease lock
1074 if (ldlm_is_excl(lock))
1077 /* llite sometimes wants to match locks that will be
1078 * canceled when their users drop, but we allow it to match
1079 * if it passes in CBPENDING and the lock still has users.
1080 * this is generally only going to be used by children
1081 * whose parents already hold a lock so forward progress
1084 if (ldlm_is_cbpending(lock) && !(flags & LDLM_FL_CBPENDING))
1086 if (!unref && ldlm_is_cbpending(lock) &&
1087 lock->l_readers == 0 && lock->l_writers == 0)
1090 if (!(lock->l_req_mode & *mode))
1092 match = lock->l_req_mode;
1094 if (lock->l_resource->lr_type == LDLM_EXTENT &&
1095 (lock->l_policy_data.l_extent.start >
1096 policy->l_extent.start ||
1097 lock->l_policy_data.l_extent.end < policy->l_extent.end))
1100 if (unlikely(match == LCK_GROUP) &&
1101 lock->l_resource->lr_type == LDLM_EXTENT &&
1102 policy->l_extent.gid != LDLM_GID_ANY &&
1103 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
1106 /* We match if we have existing lock with same or wider set
1109 if (lock->l_resource->lr_type == LDLM_IBITS &&
1110 ((lock->l_policy_data.l_inodebits.bits &
1111 policy->l_inodebits.bits) !=
1112 policy->l_inodebits.bits))
1115 if (!unref && LDLM_HAVE_MASK(lock, GONE))
1118 if ((flags & LDLM_FL_LOCAL_ONLY) && !ldlm_is_local(lock))
1121 if (flags & LDLM_FL_TEST_LOCK) {
1122 LDLM_LOCK_GET(lock);
1123 ldlm_lock_touch_in_lru(lock);
1125 ldlm_lock_addref_internal_nolock(lock, match);
1134 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock)
1136 if ((lock->l_flags & LDLM_FL_FAIL_NOTIFIED) == 0) {
1137 lock->l_flags |= LDLM_FL_FAIL_NOTIFIED;
1138 wake_up_all(&lock->l_waitq);
1141 EXPORT_SYMBOL(ldlm_lock_fail_match_locked);
1144 * Mark lock as "matchable" by OST.
1146 * Used to prevent certain races in LOV/OSC where the lock is granted, but LVB
1148 * Assumes LDLM lock is already locked.
1150 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1152 ldlm_set_lvb_ready(lock);
1153 wake_up_all(&lock->l_waitq);
1155 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
1158 * Mark lock as "matchable" by OST.
1159 * Locks the lock and then \see ldlm_lock_allow_match_locked
1161 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1163 lock_res_and_lock(lock);
1164 ldlm_lock_allow_match_locked(lock);
1165 unlock_res_and_lock(lock);
1167 EXPORT_SYMBOL(ldlm_lock_allow_match);
1170 * Attempt to find a lock with specified properties.
1172 * Typically returns a reference to matched lock unless LDLM_FL_TEST_LOCK is
1175 * Can be called in two ways:
1177 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1178 * for a duplicate of.
1180 * Otherwise, all of the fields must be filled in, to match against.
1182 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1183 * server (ie, connh is NULL)
1184 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1185 * list will be considered
1186 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1187 * to be canceled can still be matched as long as they still have reader
1188 * or writer referneces
1189 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1190 * just tell us if we would have matched.
1192 * \retval 1 if it finds an already-existing lock that is compatible; in this
1193 * case, lockh is filled in with a addref()ed lock
1195 * We also check security context, and if that fails we simply return 0 (to
1196 * keep caller code unchanged), the context failure will be discovered by
1197 * caller sometime later.
1199 enum ldlm_mode ldlm_lock_match(struct ldlm_namespace *ns, __u64 flags,
1200 const struct ldlm_res_id *res_id,
1201 enum ldlm_type type,
1202 ldlm_policy_data_t *policy,
1203 enum ldlm_mode mode,
1204 struct lustre_handle *lockh, int unref)
1206 struct ldlm_resource *res;
1207 struct ldlm_lock *lock, *old_lock = NULL;
1211 old_lock = ldlm_handle2lock(lockh);
1214 ns = ldlm_lock_to_ns(old_lock);
1215 res_id = &old_lock->l_resource->lr_name;
1216 type = old_lock->l_resource->lr_type;
1217 mode = old_lock->l_req_mode;
1220 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1226 LDLM_RESOURCE_ADDREF(res);
1229 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1235 if (flags & LDLM_FL_BLOCK_GRANTED) {
1239 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1248 LDLM_RESOURCE_DELREF(res);
1249 ldlm_resource_putref(res);
1252 ldlm_lock2handle(lock, lockh);
1253 if ((flags & LDLM_FL_LVB_READY) && !ldlm_is_lvb_ready(lock)) {
1254 __u64 wait_flags = LDLM_FL_LVB_READY |
1255 LDLM_FL_DESTROYED | LDLM_FL_FAIL_NOTIFIED;
1256 struct l_wait_info lwi;
1258 if (lock->l_completion_ast) {
1259 int err = lock->l_completion_ast(lock,
1260 LDLM_FL_WAIT_NOREPROC,
1263 if (flags & LDLM_FL_TEST_LOCK)
1264 LDLM_LOCK_RELEASE(lock);
1266 ldlm_lock_decref_internal(lock,
1273 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1274 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1276 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1277 l_wait_event(lock->l_waitq,
1278 lock->l_flags & wait_flags,
1280 if (!ldlm_is_lvb_ready(lock)) {
1281 if (flags & LDLM_FL_TEST_LOCK)
1282 LDLM_LOCK_RELEASE(lock);
1284 ldlm_lock_decref_internal(lock, mode);
1291 LDLM_DEBUG(lock, "matched (%llu %llu)",
1292 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1293 res_id->name[2] : policy->l_extent.start,
1294 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1295 res_id->name[3] : policy->l_extent.end);
1297 /* check user's security context */
1298 if (lock->l_conn_export &&
1299 sptlrpc_import_check_ctx(
1300 class_exp2cliimp(lock->l_conn_export))) {
1301 if (!(flags & LDLM_FL_TEST_LOCK))
1302 ldlm_lock_decref_internal(lock, mode);
1306 if (flags & LDLM_FL_TEST_LOCK)
1307 LDLM_LOCK_RELEASE(lock);
1309 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1310 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res %llu/%llu (%llu %llu)",
1311 ns, type, mode, res_id->name[0],
1313 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1314 res_id->name[2] : policy->l_extent.start,
1315 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1316 res_id->name[3] : policy->l_extent.end);
1319 LDLM_LOCK_PUT(old_lock);
1321 return rc ? mode : 0;
1323 EXPORT_SYMBOL(ldlm_lock_match);
1325 enum ldlm_mode ldlm_revalidate_lock_handle(struct lustre_handle *lockh,
1328 struct ldlm_lock *lock;
1329 enum ldlm_mode mode = 0;
1331 lock = ldlm_handle2lock(lockh);
1333 lock_res_and_lock(lock);
1334 if (LDLM_HAVE_MASK(lock, GONE))
1337 if (ldlm_is_cbpending(lock) &&
1338 lock->l_readers == 0 && lock->l_writers == 0)
1342 *bits = lock->l_policy_data.l_inodebits.bits;
1343 mode = lock->l_granted_mode;
1344 ldlm_lock_addref_internal_nolock(lock, mode);
1349 unlock_res_and_lock(lock);
1350 LDLM_LOCK_PUT(lock);
1354 EXPORT_SYMBOL(ldlm_revalidate_lock_handle);
1356 /** The caller must guarantee that the buffer is large enough. */
1357 int ldlm_fill_lvb(struct ldlm_lock *lock, struct req_capsule *pill,
1358 enum req_location loc, void *data, int size)
1365 switch (lock->l_lvb_type) {
1367 if (size == sizeof(struct ost_lvb)) {
1368 if (loc == RCL_CLIENT)
1369 lvb = req_capsule_client_swab_get(pill,
1371 lustre_swab_ost_lvb);
1373 lvb = req_capsule_server_swab_get(pill,
1375 lustre_swab_ost_lvb);
1376 if (unlikely(!lvb)) {
1377 LDLM_ERROR(lock, "no LVB");
1381 memcpy(data, lvb, size);
1382 } else if (size == sizeof(struct ost_lvb_v1)) {
1383 struct ost_lvb *olvb = data;
1385 if (loc == RCL_CLIENT)
1386 lvb = req_capsule_client_swab_get(pill,
1388 lustre_swab_ost_lvb_v1);
1390 lvb = req_capsule_server_sized_swab_get(pill,
1392 lustre_swab_ost_lvb_v1);
1393 if (unlikely(!lvb)) {
1394 LDLM_ERROR(lock, "no LVB");
1398 memcpy(data, lvb, size);
1399 olvb->lvb_mtime_ns = 0;
1400 olvb->lvb_atime_ns = 0;
1401 olvb->lvb_ctime_ns = 0;
1403 LDLM_ERROR(lock, "Replied unexpected ost LVB size %d",
1409 if (size == sizeof(struct lquota_lvb)) {
1410 if (loc == RCL_CLIENT)
1411 lvb = req_capsule_client_swab_get(pill,
1413 lustre_swab_lquota_lvb);
1415 lvb = req_capsule_server_swab_get(pill,
1417 lustre_swab_lquota_lvb);
1418 if (unlikely(!lvb)) {
1419 LDLM_ERROR(lock, "no LVB");
1423 memcpy(data, lvb, size);
1426 "Replied unexpected lquota LVB size %d",
1435 if (loc == RCL_CLIENT)
1436 lvb = req_capsule_client_get(pill, &RMF_DLM_LVB);
1438 lvb = req_capsule_server_get(pill, &RMF_DLM_LVB);
1439 if (unlikely(!lvb)) {
1440 LDLM_ERROR(lock, "no LVB");
1444 memcpy(data, lvb, size);
1447 LDLM_ERROR(lock, "Unknown LVB type: %d\n", lock->l_lvb_type);
1456 * Create and fill in new LDLM lock with specified properties.
1457 * Returns a referenced lock
1459 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1460 const struct ldlm_res_id *res_id,
1461 enum ldlm_type type,
1462 enum ldlm_mode mode,
1463 const struct ldlm_callback_suite *cbs,
1464 void *data, __u32 lvb_len,
1465 enum lvb_type lvb_type)
1467 struct ldlm_lock *lock;
1468 struct ldlm_resource *res;
1470 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1474 lock = ldlm_lock_new(res);
1479 lock->l_req_mode = mode;
1480 lock->l_ast_data = data;
1481 lock->l_pid = current_pid();
1483 lock->l_blocking_ast = cbs->lcs_blocking;
1484 lock->l_completion_ast = cbs->lcs_completion;
1485 lock->l_glimpse_ast = cbs->lcs_glimpse;
1488 lock->l_tree_node = NULL;
1489 /* if this is the extent lock, allocate the interval tree node */
1490 if (type == LDLM_EXTENT) {
1491 if (!ldlm_interval_alloc(lock))
1496 lock->l_lvb_len = lvb_len;
1497 lock->l_lvb_data = kzalloc(lvb_len, GFP_NOFS);
1498 if (!lock->l_lvb_data)
1502 lock->l_lvb_type = lvb_type;
1503 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
1509 ldlm_lock_destroy(lock);
1510 LDLM_LOCK_RELEASE(lock);
1515 * Enqueue (request) a lock.
1516 * On the client this is called from ldlm_cli_enqueue_fini
1517 * after we already got an initial reply from the server with some status.
1519 * Does not block. As a result of enqueue the lock would be put
1520 * into granted or waiting list.
1522 enum ldlm_error ldlm_lock_enqueue(struct ldlm_namespace *ns,
1523 struct ldlm_lock **lockp,
1524 void *cookie, __u64 *flags)
1526 struct ldlm_lock *lock = *lockp;
1527 struct ldlm_resource *res = lock->l_resource;
1529 lock->l_last_activity = ktime_get_real_seconds();
1531 lock_res_and_lock(lock);
1532 if (lock->l_req_mode == lock->l_granted_mode) {
1533 /* The server returned a blocked lock, but it was granted
1534 * before we got a chance to actually enqueue it. We don't
1535 * need to do anything else.
1537 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1538 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1542 ldlm_resource_unlink_lock(lock);
1544 /* Cannot happen unless on the server */
1545 if (res->lr_type == LDLM_EXTENT && !lock->l_tree_node)
1548 /* Some flags from the enqueue want to make it into the AST, via the
1551 if (*flags & LDLM_FL_AST_DISCARD_DATA)
1552 ldlm_set_ast_discard_data(lock);
1555 * This distinction between local lock trees is very important; a client
1556 * namespace only has information about locks taken by that client, and
1557 * thus doesn't have enough information to decide for itself if it can
1558 * be granted (below). In this case, we do exactly what the server
1559 * tells us to do, as dictated by the 'flags'.
1561 if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1562 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1564 ldlm_grant_lock(lock, NULL);
1567 unlock_res_and_lock(lock);
1572 * Process a call to blocking AST callback for a lock in ast_work list
1575 ldlm_work_bl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1577 struct ldlm_cb_set_arg *arg = opaq;
1578 struct ldlm_lock_desc d;
1580 struct ldlm_lock *lock;
1582 if (list_empty(arg->list))
1585 lock = list_entry(arg->list->next, struct ldlm_lock, l_bl_ast);
1587 /* nobody should touch l_bl_ast */
1588 lock_res_and_lock(lock);
1589 list_del_init(&lock->l_bl_ast);
1591 LASSERT(ldlm_is_ast_sent(lock));
1592 LASSERT(lock->l_bl_ast_run == 0);
1593 LASSERT(lock->l_blocking_lock);
1594 lock->l_bl_ast_run++;
1595 unlock_res_and_lock(lock);
1597 ldlm_lock2desc(lock->l_blocking_lock, &d);
1599 rc = lock->l_blocking_ast(lock, &d, (void *)arg, LDLM_CB_BLOCKING);
1600 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1601 lock->l_blocking_lock = NULL;
1602 LDLM_LOCK_RELEASE(lock);
1608 * Process a call to completion AST callback for a lock in ast_work list
1611 ldlm_work_cp_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1613 struct ldlm_cb_set_arg *arg = opaq;
1615 struct ldlm_lock *lock;
1616 ldlm_completion_callback completion_callback;
1618 if (list_empty(arg->list))
1621 lock = list_entry(arg->list->next, struct ldlm_lock, l_cp_ast);
1623 /* It's possible to receive a completion AST before we've set
1624 * the l_completion_ast pointer: either because the AST arrived
1625 * before the reply, or simply because there's a small race
1626 * window between receiving the reply and finishing the local
1627 * enqueue. (bug 842)
1629 * This can't happen with the blocking_ast, however, because we
1630 * will never call the local blocking_ast until we drop our
1631 * reader/writer reference, which we won't do until we get the
1632 * reply and finish enqueueing.
1635 /* nobody should touch l_cp_ast */
1636 lock_res_and_lock(lock);
1637 list_del_init(&lock->l_cp_ast);
1638 LASSERT(ldlm_is_cp_reqd(lock));
1639 /* save l_completion_ast since it can be changed by
1640 * mds_intent_policy(), see bug 14225
1642 completion_callback = lock->l_completion_ast;
1643 ldlm_clear_cp_reqd(lock);
1644 unlock_res_and_lock(lock);
1646 if (completion_callback)
1647 rc = completion_callback(lock, 0, (void *)arg);
1648 LDLM_LOCK_RELEASE(lock);
1654 * Process a call to revocation AST callback for a lock in ast_work list
1657 ldlm_work_revoke_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1659 struct ldlm_cb_set_arg *arg = opaq;
1660 struct ldlm_lock_desc desc;
1662 struct ldlm_lock *lock;
1664 if (list_empty(arg->list))
1667 lock = list_entry(arg->list->next, struct ldlm_lock, l_rk_ast);
1668 list_del_init(&lock->l_rk_ast);
1670 /* the desc just pretend to exclusive */
1671 ldlm_lock2desc(lock, &desc);
1672 desc.l_req_mode = LCK_EX;
1673 desc.l_granted_mode = 0;
1675 rc = lock->l_blocking_ast(lock, &desc, (void *)arg, LDLM_CB_BLOCKING);
1676 LDLM_LOCK_RELEASE(lock);
1682 * Process a call to glimpse AST callback for a lock in ast_work list
1684 static int ldlm_work_gl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1686 struct ldlm_cb_set_arg *arg = opaq;
1687 struct ldlm_glimpse_work *gl_work;
1688 struct ldlm_lock *lock;
1691 if (list_empty(arg->list))
1694 gl_work = list_entry(arg->list->next, struct ldlm_glimpse_work,
1696 list_del_init(&gl_work->gl_list);
1698 lock = gl_work->gl_lock;
1700 /* transfer the glimpse descriptor to ldlm_cb_set_arg */
1701 arg->gl_desc = gl_work->gl_desc;
1703 /* invoke the actual glimpse callback */
1704 if (lock->l_glimpse_ast(lock, (void *)arg) == 0)
1707 LDLM_LOCK_RELEASE(lock);
1709 if ((gl_work->gl_flags & LDLM_GL_WORK_NOFREE) == 0)
1716 * Process list of locks in need of ASTs being sent.
1718 * Used on server to send multiple ASTs together instead of sending one by
1721 int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list,
1722 enum ldlm_desc_ast_t ast_type)
1724 struct ldlm_cb_set_arg *arg;
1725 set_producer_func work_ast_lock;
1728 if (list_empty(rpc_list))
1731 arg = kzalloc(sizeof(*arg), GFP_NOFS);
1735 atomic_set(&arg->restart, 0);
1736 arg->list = rpc_list;
1739 case LDLM_WORK_BL_AST:
1740 arg->type = LDLM_BL_CALLBACK;
1741 work_ast_lock = ldlm_work_bl_ast_lock;
1743 case LDLM_WORK_CP_AST:
1744 arg->type = LDLM_CP_CALLBACK;
1745 work_ast_lock = ldlm_work_cp_ast_lock;
1747 case LDLM_WORK_REVOKE_AST:
1748 arg->type = LDLM_BL_CALLBACK;
1749 work_ast_lock = ldlm_work_revoke_ast_lock;
1751 case LDLM_WORK_GL_AST:
1752 arg->type = LDLM_GL_CALLBACK;
1753 work_ast_lock = ldlm_work_gl_ast_lock;
1759 /* We create a ptlrpc request set with flow control extension.
1760 * This request set will use the work_ast_lock function to produce new
1761 * requests and will send a new request each time one completes in order
1762 * to keep the number of requests in flight to ns_max_parallel_ast
1764 arg->set = ptlrpc_prep_fcset(ns->ns_max_parallel_ast ? : UINT_MAX,
1765 work_ast_lock, arg);
1771 ptlrpc_set_wait(arg->set);
1772 ptlrpc_set_destroy(arg->set);
1774 rc = atomic_read(&arg->restart) ? -ERESTART : 0;
1782 * Helper function to call blocking AST for LDLM lock \a lock in a
1783 * "cancelling" mode.
1785 void ldlm_cancel_callback(struct ldlm_lock *lock)
1787 check_res_locked(lock->l_resource);
1788 if (!ldlm_is_cancel(lock)) {
1789 ldlm_set_cancel(lock);
1790 if (lock->l_blocking_ast) {
1791 unlock_res_and_lock(lock);
1792 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1794 lock_res_and_lock(lock);
1796 LDLM_DEBUG(lock, "no blocking ast");
1799 ldlm_set_bl_done(lock);
1803 * Remove skiplist-enabled LDLM lock \a req from granted list
1805 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1807 if (req->l_resource->lr_type != LDLM_PLAIN &&
1808 req->l_resource->lr_type != LDLM_IBITS)
1811 list_del_init(&req->l_sl_policy);
1812 list_del_init(&req->l_sl_mode);
1816 * Attempts to cancel LDLM lock \a lock that has no reader/writer references.
1818 void ldlm_lock_cancel(struct ldlm_lock *lock)
1820 struct ldlm_resource *res;
1821 struct ldlm_namespace *ns;
1823 lock_res_and_lock(lock);
1825 res = lock->l_resource;
1826 ns = ldlm_res_to_ns(res);
1828 /* Please do not, no matter how tempting, remove this LBUG without
1829 * talking to me first. -phik
1831 if (lock->l_readers || lock->l_writers) {
1832 LDLM_ERROR(lock, "lock still has references");
1836 /* Releases cancel callback. */
1837 ldlm_cancel_callback(lock);
1839 ldlm_resource_unlink_lock(lock);
1840 ldlm_lock_destroy_nolock(lock);
1842 if (lock->l_granted_mode == lock->l_req_mode)
1843 ldlm_pool_del(&ns->ns_pool, lock);
1845 /* Make sure we will not be called again for same lock what is possible
1846 * if not to zero out lock->l_granted_mode
1848 lock->l_granted_mode = LCK_MINMODE;
1849 unlock_res_and_lock(lock);
1851 EXPORT_SYMBOL(ldlm_lock_cancel);
1854 * Set opaque data into the lock that only makes sense to upper layer.
1856 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1858 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1862 if (!lock->l_ast_data)
1863 lock->l_ast_data = data;
1864 if (lock->l_ast_data == data)
1866 LDLM_LOCK_PUT(lock);
1870 EXPORT_SYMBOL(ldlm_lock_set_data);
1872 struct export_cl_data {
1873 struct obd_export *ecl_exp;
1878 * Print lock with lock handle \a lockh description into debug log.
1880 * Used when printing all locks on a resource for debug purposes.
1882 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1884 struct ldlm_lock *lock;
1886 if (!((libcfs_debug | D_ERROR) & level))
1889 lock = ldlm_handle2lock(lockh);
1893 LDLM_DEBUG_LIMIT(level, lock, "###");
1895 LDLM_LOCK_PUT(lock);
1897 EXPORT_SYMBOL(ldlm_lock_dump_handle);
1900 * Print lock information with custom message into debug log.
1903 void _ldlm_lock_debug(struct ldlm_lock *lock,
1904 struct libcfs_debug_msg_data *msgdata,
1905 const char *fmt, ...)
1908 struct obd_export *exp = lock->l_export;
1909 struct ldlm_resource *resource = lock->l_resource;
1910 char *nid = "local";
1912 va_start(args, fmt);
1914 if (exp && exp->exp_connection) {
1915 nid = libcfs_nid2str(exp->exp_connection->c_peer.nid);
1916 } else if (exp && exp->exp_obd) {
1917 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
1919 nid = libcfs_nid2str(imp->imp_connection->c_peer.nid);
1923 libcfs_debug_vmsg2(msgdata, fmt, args,
1924 " 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",
1926 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1927 lock->l_readers, lock->l_writers,
1928 ldlm_lockname[lock->l_granted_mode],
1929 ldlm_lockname[lock->l_req_mode],
1930 lock->l_flags, nid, lock->l_remote_handle.cookie,
1931 exp ? atomic_read(&exp->exp_refcount) : -99,
1932 lock->l_pid, lock->l_callback_timeout, lock->l_lvb_type);
1937 switch (resource->lr_type) {
1939 libcfs_debug_vmsg2(msgdata, fmt, args,
1940 " 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",
1941 ldlm_lock_to_ns_name(lock), lock,
1942 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1943 lock->l_readers, lock->l_writers,
1944 ldlm_lockname[lock->l_granted_mode],
1945 ldlm_lockname[lock->l_req_mode],
1947 atomic_read(&resource->lr_refcount),
1948 ldlm_typename[resource->lr_type],
1949 lock->l_policy_data.l_extent.start,
1950 lock->l_policy_data.l_extent.end,
1951 lock->l_req_extent.start, lock->l_req_extent.end,
1952 lock->l_flags, nid, lock->l_remote_handle.cookie,
1953 exp ? atomic_read(&exp->exp_refcount) : -99,
1954 lock->l_pid, lock->l_callback_timeout,
1959 libcfs_debug_vmsg2(msgdata, fmt, args,
1960 " 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",
1961 ldlm_lock_to_ns_name(lock), lock,
1962 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1963 lock->l_readers, lock->l_writers,
1964 ldlm_lockname[lock->l_granted_mode],
1965 ldlm_lockname[lock->l_req_mode],
1967 atomic_read(&resource->lr_refcount),
1968 ldlm_typename[resource->lr_type],
1969 lock->l_policy_data.l_flock.pid,
1970 lock->l_policy_data.l_flock.start,
1971 lock->l_policy_data.l_flock.end,
1972 lock->l_flags, nid, lock->l_remote_handle.cookie,
1973 exp ? atomic_read(&exp->exp_refcount) : -99,
1974 lock->l_pid, lock->l_callback_timeout);
1978 libcfs_debug_vmsg2(msgdata, fmt, args,
1979 " 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",
1980 ldlm_lock_to_ns_name(lock),
1981 lock, lock->l_handle.h_cookie,
1982 atomic_read(&lock->l_refc),
1983 lock->l_readers, lock->l_writers,
1984 ldlm_lockname[lock->l_granted_mode],
1985 ldlm_lockname[lock->l_req_mode],
1987 lock->l_policy_data.l_inodebits.bits,
1988 atomic_read(&resource->lr_refcount),
1989 ldlm_typename[resource->lr_type],
1990 lock->l_flags, nid, lock->l_remote_handle.cookie,
1991 exp ? atomic_read(&exp->exp_refcount) : -99,
1992 lock->l_pid, lock->l_callback_timeout,
1997 libcfs_debug_vmsg2(msgdata, fmt, args,
1998 " 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",
1999 ldlm_lock_to_ns_name(lock),
2000 lock, lock->l_handle.h_cookie,
2001 atomic_read(&lock->l_refc),
2002 lock->l_readers, lock->l_writers,
2003 ldlm_lockname[lock->l_granted_mode],
2004 ldlm_lockname[lock->l_req_mode],
2006 atomic_read(&resource->lr_refcount),
2007 ldlm_typename[resource->lr_type],
2008 lock->l_flags, nid, lock->l_remote_handle.cookie,
2009 exp ? atomic_read(&exp->exp_refcount) : -99,
2010 lock->l_pid, lock->l_callback_timeout,
2016 EXPORT_SYMBOL(_ldlm_lock_debug);