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, 2012, 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(ldlm_type_t 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, ldlm_type_t 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(lock->l_flags & LDLM_FL_DESTROYED);
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 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
235 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
238 spin_lock(&ns->ns_lock);
239 rc = ldlm_lock_remove_from_lru_nolock(lock);
240 spin_unlock(&ns->ns_lock);
245 * Adds LDLM lock \a lock to namespace LRU. Assumes LRU is already locked.
247 static void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
249 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
251 lock->l_last_used = cfs_time_current();
252 LASSERT(list_empty(&lock->l_lru));
253 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
254 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
255 if (lock->l_flags & LDLM_FL_SKIPPED)
256 lock->l_flags &= ~LDLM_FL_SKIPPED;
257 LASSERT(ns->ns_nr_unused >= 0);
262 * Adds LDLM lock \a lock to namespace LRU. Obtains necessary LRU locks
265 static void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
267 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
269 spin_lock(&ns->ns_lock);
270 ldlm_lock_add_to_lru_nolock(lock);
271 spin_unlock(&ns->ns_lock);
275 * Moves LDLM lock \a lock that is already in namespace LRU to the tail of
276 * the LRU. Performs necessary LRU locking
278 static void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
280 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
282 spin_lock(&ns->ns_lock);
283 if (!list_empty(&lock->l_lru)) {
284 ldlm_lock_remove_from_lru_nolock(lock);
285 ldlm_lock_add_to_lru_nolock(lock);
287 spin_unlock(&ns->ns_lock);
291 * Helper to destroy a locked lock.
293 * Used by ldlm_lock_destroy and ldlm_lock_destroy_nolock
294 * Must be called with l_lock and lr_lock held.
296 * Does not actually free the lock data, but rather marks the lock as
297 * destroyed by setting l_destroyed field in the lock to 1. Destroys a
298 * handle->lock association too, so that the lock can no longer be found
299 * and removes the lock from LRU list. Actual lock freeing occurs when
300 * last lock reference goes away.
302 * Original comment (of some historical value):
303 * This used to have a 'strict' flag, which recovery would use to mark an
304 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
305 * shall explain why it's gone: with the new hash table scheme, once you call
306 * ldlm_lock_destroy, you can never drop your final references on this lock.
307 * Because it's not in the hash table anymore. -phil
309 static int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
311 if (lock->l_readers || lock->l_writers) {
312 LDLM_ERROR(lock, "lock still has references");
316 if (!list_empty(&lock->l_res_link)) {
317 LDLM_ERROR(lock, "lock still on resource");
321 if (lock->l_flags & LDLM_FL_DESTROYED) {
322 LASSERT(list_empty(&lock->l_lru));
325 lock->l_flags |= LDLM_FL_DESTROYED;
327 if (lock->l_export && lock->l_export->exp_lock_hash) {
328 /* NB: it's safe to call cfs_hash_del() even lock isn't
329 * in exp_lock_hash. */
330 /* In the function below, .hs_keycmp resolves to
331 * ldlm_export_lock_keycmp() */
332 /* coverity[overrun-buffer-val] */
333 cfs_hash_del(lock->l_export->exp_lock_hash,
334 &lock->l_remote_handle, &lock->l_exp_hash);
337 ldlm_lock_remove_from_lru(lock);
338 class_handle_unhash(&lock->l_handle);
341 /* Wake anyone waiting for this lock */
342 /* FIXME: I should probably add yet another flag, instead of using
343 * l_export to only call this on clients */
345 class_export_put(lock->l_export);
346 lock->l_export = NULL;
347 if (lock->l_export && lock->l_completion_ast)
348 lock->l_completion_ast(lock, 0);
354 * Destroys a LDLM lock \a lock. Performs necessary locking first.
356 static void ldlm_lock_destroy(struct ldlm_lock *lock)
360 lock_res_and_lock(lock);
361 first = ldlm_lock_destroy_internal(lock);
362 unlock_res_and_lock(lock);
364 /* drop reference from hashtable only for first destroy */
366 lu_ref_del(&lock->l_reference, "hash", lock);
367 LDLM_LOCK_RELEASE(lock);
372 * Destroys a LDLM lock \a lock that is already locked.
374 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
378 first = ldlm_lock_destroy_internal(lock);
379 /* drop reference from hashtable only for first destroy */
381 lu_ref_del(&lock->l_reference, "hash", lock);
382 LDLM_LOCK_RELEASE(lock);
386 /* this is called by portals_handle2object with the handle lock taken */
387 static void lock_handle_addref(void *lock)
389 LDLM_LOCK_GET((struct ldlm_lock *)lock);
392 static void lock_handle_free(void *lock, int size)
394 LASSERT(size == sizeof(struct ldlm_lock));
395 kmem_cache_free(ldlm_lock_slab, lock);
398 static struct portals_handle_ops lock_handle_ops = {
399 .hop_addref = lock_handle_addref,
400 .hop_free = lock_handle_free,
405 * Allocate and initialize new lock structure.
407 * usage: pass in a resource on which you have done ldlm_resource_get
408 * new lock will take over the refcount.
409 * returns: lock with refcount 2 - one for current caller and one for remote
411 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
413 struct ldlm_lock *lock;
415 if (resource == NULL)
418 lock = kmem_cache_alloc(ldlm_lock_slab, GFP_NOFS | __GFP_ZERO);
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 if (lock->l_export && lock->l_export->exp_failed) {
554 CDEBUG(D_INFO, "lock export failed: lock %p, exp %p\n",
555 lock, lock->l_export);
560 /* It's unlikely but possible that someone marked the lock as
561 * destroyed after we did handle2object on it */
562 if (flags == 0 && ((lock->l_flags & LDLM_FL_DESTROYED) == 0)) {
563 lu_ref_add(&lock->l_reference, "handle", current);
567 lock_res_and_lock(lock);
569 LASSERT(lock->l_resource != NULL);
571 lu_ref_add_atomic(&lock->l_reference, "handle", current);
572 if (unlikely(lock->l_flags & LDLM_FL_DESTROYED)) {
573 unlock_res_and_lock(lock);
574 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
579 if (flags && (lock->l_flags & flags)) {
580 unlock_res_and_lock(lock);
586 lock->l_flags |= flags;
588 unlock_res_and_lock(lock);
591 EXPORT_SYMBOL(__ldlm_handle2lock);
592 /** @} ldlm_handles */
595 * Fill in "on the wire" representation for given LDLM lock into supplied
596 * lock descriptor \a desc structure.
598 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
600 ldlm_res2desc(lock->l_resource, &desc->l_resource);
601 desc->l_req_mode = lock->l_req_mode;
602 desc->l_granted_mode = lock->l_granted_mode;
603 ldlm_convert_policy_to_wire(lock->l_resource->lr_type,
604 &lock->l_policy_data,
605 &desc->l_policy_data);
607 EXPORT_SYMBOL(ldlm_lock2desc);
610 * Add a lock to list of conflicting locks to send AST to.
612 * Only add if we have not sent a blocking AST to the lock yet.
614 static void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
615 struct list_head *work_list)
617 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
618 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
619 lock->l_flags |= LDLM_FL_AST_SENT;
620 /* If the enqueuing client said so, tell the AST recipient to
621 * discard dirty data, rather than writing back. */
622 if (new->l_flags & LDLM_FL_AST_DISCARD_DATA)
623 lock->l_flags |= LDLM_FL_DISCARD_DATA;
624 LASSERT(list_empty(&lock->l_bl_ast));
625 list_add(&lock->l_bl_ast, work_list);
627 LASSERT(lock->l_blocking_lock == NULL);
628 lock->l_blocking_lock = LDLM_LOCK_GET(new);
633 * Add a lock to list of just granted locks to send completion AST to.
635 static void ldlm_add_cp_work_item(struct ldlm_lock *lock,
636 struct list_head *work_list)
638 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
639 lock->l_flags |= LDLM_FL_CP_REQD;
640 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
641 LASSERT(list_empty(&lock->l_cp_ast));
642 list_add(&lock->l_cp_ast, work_list);
648 * Aggregator function to add AST work items into a list. Determines
649 * what sort of an AST work needs to be done and calls the proper
651 * Must be called with lr_lock held.
653 static void ldlm_add_ast_work_item(struct ldlm_lock *lock,
654 struct ldlm_lock *new,
655 struct list_head *work_list)
657 check_res_locked(lock->l_resource);
659 ldlm_add_bl_work_item(lock, new, work_list);
661 ldlm_add_cp_work_item(lock, work_list);
665 * Add specified reader/writer reference to LDLM lock with handle \a lockh.
666 * r/w reference type is determined by \a mode
667 * Calls ldlm_lock_addref_internal.
669 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
671 struct ldlm_lock *lock;
673 lock = ldlm_handle2lock(lockh);
674 LASSERT(lock != NULL);
675 ldlm_lock_addref_internal(lock, mode);
678 EXPORT_SYMBOL(ldlm_lock_addref);
682 * Add specified reader/writer reference to LDLM lock \a lock.
683 * r/w reference type is determined by \a mode
684 * Removes lock from LRU if it is there.
685 * Assumes the LDLM lock is already locked.
687 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
689 ldlm_lock_remove_from_lru(lock);
690 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
692 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
694 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
696 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
699 lu_ref_add_atomic(&lock->l_reference, "user", lock);
700 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
704 * Attempts to add reader/writer reference to a lock with handle \a lockh, and
705 * fails if lock is already LDLM_FL_CBPENDING or destroyed.
707 * \retval 0 success, lock was addref-ed
709 * \retval -EAGAIN lock is being canceled.
711 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
713 struct ldlm_lock *lock;
717 lock = ldlm_handle2lock(lockh);
719 lock_res_and_lock(lock);
720 if (lock->l_readers != 0 || lock->l_writers != 0 ||
721 !(lock->l_flags & LDLM_FL_CBPENDING)) {
722 ldlm_lock_addref_internal_nolock(lock, mode);
725 unlock_res_and_lock(lock);
730 EXPORT_SYMBOL(ldlm_lock_addref_try);
733 * Add specified reader/writer reference to LDLM lock \a lock.
734 * Locks LDLM lock and calls ldlm_lock_addref_internal_nolock to do the work.
735 * Only called for local locks.
737 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
739 lock_res_and_lock(lock);
740 ldlm_lock_addref_internal_nolock(lock, mode);
741 unlock_res_and_lock(lock);
745 * Removes reader/writer reference for LDLM lock \a lock.
746 * Assumes LDLM lock is already locked.
747 * only called in ldlm_flock_destroy and for local locks.
748 * Does NOT add lock to LRU if no r/w references left to accommodate flock locks
749 * that cannot be placed in LRU.
751 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
753 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
754 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
755 LASSERT(lock->l_readers > 0);
756 lu_ref_del(&lock->l_reference, "reader", lock);
759 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
760 LASSERT(lock->l_writers > 0);
761 lu_ref_del(&lock->l_reference, "writer", lock);
765 lu_ref_del(&lock->l_reference, "user", lock);
766 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
770 * Removes reader/writer reference for LDLM lock \a lock.
771 * Locks LDLM lock first.
772 * If the lock is determined to be client lock on a client and r/w refcount
773 * drops to zero and the lock is not blocked, the lock is added to LRU lock
775 * For blocked LDLM locks if r/w count drops to zero, blocking_ast is called.
777 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
779 struct ldlm_namespace *ns;
781 lock_res_and_lock(lock);
783 ns = ldlm_lock_to_ns(lock);
785 ldlm_lock_decref_internal_nolock(lock, mode);
787 if (lock->l_flags & LDLM_FL_LOCAL &&
788 !lock->l_readers && !lock->l_writers) {
789 /* If this is a local lock on a server namespace and this was
790 * the last reference, cancel the lock. */
791 CDEBUG(D_INFO, "forcing cancel of local lock\n");
792 lock->l_flags |= LDLM_FL_CBPENDING;
795 if (!lock->l_readers && !lock->l_writers &&
796 (lock->l_flags & LDLM_FL_CBPENDING)) {
797 /* If we received a blocked AST and this was the last reference,
798 * run the callback. */
800 LDLM_DEBUG(lock, "final decref done on cbpending lock");
802 LDLM_LOCK_GET(lock); /* dropped by bl thread */
803 ldlm_lock_remove_from_lru(lock);
804 unlock_res_and_lock(lock);
806 if (lock->l_flags & LDLM_FL_FAIL_LOC)
807 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
809 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
810 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
811 ldlm_handle_bl_callback(ns, NULL, lock);
812 } else if (!lock->l_readers && !lock->l_writers &&
813 !(lock->l_flags & LDLM_FL_NO_LRU) &&
814 !(lock->l_flags & LDLM_FL_BL_AST)) {
816 LDLM_DEBUG(lock, "add lock into lru list");
818 /* If this is a client-side namespace and this was the last
819 * reference, put it on the LRU. */
820 ldlm_lock_add_to_lru(lock);
821 unlock_res_and_lock(lock);
823 if (lock->l_flags & LDLM_FL_FAIL_LOC)
824 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
826 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
827 * are not supported by the server, otherwise, it is done on
829 if (!exp_connect_cancelset(lock->l_conn_export) &&
830 !ns_connect_lru_resize(ns))
831 ldlm_cancel_lru(ns, 0, LCF_ASYNC, 0);
833 LDLM_DEBUG(lock, "do not add lock into lru list");
834 unlock_res_and_lock(lock);
839 * Decrease reader/writer refcount for LDLM lock with handle \a lockh
841 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
843 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
845 LASSERTF(lock != NULL, "Non-existing lock: %#llx\n", lockh->cookie);
846 ldlm_lock_decref_internal(lock, mode);
849 EXPORT_SYMBOL(ldlm_lock_decref);
852 * Decrease reader/writer refcount for LDLM lock with handle
853 * \a lockh and mark it for subsequent cancellation once r/w refcount
854 * drops to zero instead of putting into LRU.
856 * Typical usage is for GROUP locks which we cannot allow to be cached.
858 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
860 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
862 LASSERT(lock != NULL);
864 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
865 lock_res_and_lock(lock);
866 lock->l_flags |= LDLM_FL_CBPENDING;
867 unlock_res_and_lock(lock);
868 ldlm_lock_decref_internal(lock, mode);
871 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
873 struct sl_insert_point {
874 struct list_head *res_link;
875 struct list_head *mode_link;
876 struct list_head *policy_link;
880 * Finds a position to insert the new lock into granted lock list.
882 * Used for locks eligible for skiplist optimization.
885 * queue [input]: the granted list where search acts on;
886 * req [input]: the lock whose position to be located;
887 * prev [output]: positions within 3 lists to insert @req to
891 * - ldlm_grant_lock_with_skiplist
893 static void search_granted_lock(struct list_head *queue,
894 struct ldlm_lock *req,
895 struct sl_insert_point *prev)
897 struct list_head *tmp;
898 struct ldlm_lock *lock, *mode_end, *policy_end;
900 list_for_each(tmp, queue) {
901 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
903 mode_end = list_entry(lock->l_sl_mode.prev,
904 struct ldlm_lock, l_sl_mode);
906 if (lock->l_req_mode != req->l_req_mode) {
907 /* jump to last lock of mode group */
908 tmp = &mode_end->l_res_link;
912 /* suitable mode group is found */
913 if (lock->l_resource->lr_type == LDLM_PLAIN) {
914 /* insert point is last lock of the mode group */
915 prev->res_link = &mode_end->l_res_link;
916 prev->mode_link = &mode_end->l_sl_mode;
917 prev->policy_link = &req->l_sl_policy;
921 if (lock->l_resource->lr_type == LDLM_IBITS) {
924 list_entry(lock->l_sl_policy.prev,
928 if (lock->l_policy_data.l_inodebits.bits ==
929 req->l_policy_data.l_inodebits.bits) {
930 /* insert point is last lock of
931 * the policy group */
933 &policy_end->l_res_link;
935 &policy_end->l_sl_mode;
937 &policy_end->l_sl_policy;
941 if (policy_end == mode_end)
942 /* done with mode group */
945 /* go to next policy group within mode group */
946 tmp = policy_end->l_res_link.next;
947 lock = list_entry(tmp, struct ldlm_lock,
949 } /* loop over policy groups within the mode group */
951 /* insert point is last lock of the mode group,
952 * new policy group is started */
953 prev->res_link = &mode_end->l_res_link;
954 prev->mode_link = &mode_end->l_sl_mode;
955 prev->policy_link = &req->l_sl_policy;
959 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
963 /* insert point is last lock on the queue,
964 * new mode group and new policy group are started */
965 prev->res_link = queue->prev;
966 prev->mode_link = &req->l_sl_mode;
967 prev->policy_link = &req->l_sl_policy;
971 * Add a lock into resource granted list after a position described by
974 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
975 struct sl_insert_point *prev)
977 struct ldlm_resource *res = lock->l_resource;
979 check_res_locked(res);
981 ldlm_resource_dump(D_INFO, res);
982 LDLM_DEBUG(lock, "About to add lock:");
984 if (lock->l_flags & LDLM_FL_DESTROYED) {
985 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
989 LASSERT(list_empty(&lock->l_res_link));
990 LASSERT(list_empty(&lock->l_sl_mode));
991 LASSERT(list_empty(&lock->l_sl_policy));
994 * lock->link == prev->link means lock is first starting the group.
995 * Don't re-add to itself to suppress kernel warnings.
997 if (&lock->l_res_link != prev->res_link)
998 list_add(&lock->l_res_link, prev->res_link);
999 if (&lock->l_sl_mode != prev->mode_link)
1000 list_add(&lock->l_sl_mode, prev->mode_link);
1001 if (&lock->l_sl_policy != prev->policy_link)
1002 list_add(&lock->l_sl_policy, prev->policy_link);
1006 * Add a lock to granted list on a resource maintaining skiplist
1009 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
1011 struct sl_insert_point prev;
1013 LASSERT(lock->l_req_mode == lock->l_granted_mode);
1015 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
1016 ldlm_granted_list_add_lock(lock, &prev);
1020 * Perform lock granting bookkeeping.
1022 * Includes putting the lock into granted list and updating lock mode.
1024 * - ldlm_lock_enqueue
1025 * - ldlm_reprocess_queue
1026 * - ldlm_lock_convert
1028 * must be called with lr_lock held
1030 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
1032 struct ldlm_resource *res = lock->l_resource;
1034 check_res_locked(res);
1036 lock->l_granted_mode = lock->l_req_mode;
1037 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
1038 ldlm_grant_lock_with_skiplist(lock);
1039 else if (res->lr_type == LDLM_EXTENT)
1040 ldlm_extent_add_lock(res, lock);
1042 ldlm_resource_add_lock(res, &res->lr_granted, lock);
1044 if (lock->l_granted_mode < res->lr_most_restr)
1045 res->lr_most_restr = lock->l_granted_mode;
1047 if (work_list && lock->l_completion_ast != NULL)
1048 ldlm_add_ast_work_item(lock, NULL, work_list);
1050 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
1054 * Search for a lock with given properties in a queue.
1056 * \retval a referenced lock or NULL. See the flag descriptions below, in the
1057 * comment above ldlm_lock_match
1059 static struct ldlm_lock *search_queue(struct list_head *queue,
1061 ldlm_policy_data_t *policy,
1062 struct ldlm_lock *old_lock,
1063 __u64 flags, int unref)
1065 struct ldlm_lock *lock;
1066 struct list_head *tmp;
1068 list_for_each(tmp, queue) {
1071 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1073 if (lock == old_lock)
1076 /* Check if this lock can be matched.
1077 * Used by LU-2919(exclusive open) for open lease lock */
1078 if (ldlm_is_excl(lock))
1081 /* llite sometimes wants to match locks that will be
1082 * canceled when their users drop, but we allow it to match
1083 * if it passes in CBPENDING and the lock still has users.
1084 * this is generally only going to be used by children
1085 * whose parents already hold a lock so forward progress
1086 * can still happen. */
1087 if (lock->l_flags & LDLM_FL_CBPENDING &&
1088 !(flags & LDLM_FL_CBPENDING))
1090 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
1091 lock->l_readers == 0 && lock->l_writers == 0)
1094 if (!(lock->l_req_mode & *mode))
1096 match = lock->l_req_mode;
1098 if (lock->l_resource->lr_type == LDLM_EXTENT &&
1099 (lock->l_policy_data.l_extent.start >
1100 policy->l_extent.start ||
1101 lock->l_policy_data.l_extent.end < policy->l_extent.end))
1104 if (unlikely(match == LCK_GROUP) &&
1105 lock->l_resource->lr_type == LDLM_EXTENT &&
1106 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
1109 /* We match if we have existing lock with same or wider set
1111 if (lock->l_resource->lr_type == LDLM_IBITS &&
1112 ((lock->l_policy_data.l_inodebits.bits &
1113 policy->l_inodebits.bits) !=
1114 policy->l_inodebits.bits))
1117 if (!unref && (lock->l_flags & LDLM_FL_GONE_MASK))
1120 if ((flags & LDLM_FL_LOCAL_ONLY) &&
1121 !(lock->l_flags & LDLM_FL_LOCAL))
1124 if (flags & LDLM_FL_TEST_LOCK) {
1125 LDLM_LOCK_GET(lock);
1126 ldlm_lock_touch_in_lru(lock);
1128 ldlm_lock_addref_internal_nolock(lock, match);
1137 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock)
1139 if ((lock->l_flags & LDLM_FL_FAIL_NOTIFIED) == 0) {
1140 lock->l_flags |= LDLM_FL_FAIL_NOTIFIED;
1141 wake_up_all(&lock->l_waitq);
1144 EXPORT_SYMBOL(ldlm_lock_fail_match_locked);
1147 * Mark lock as "matchable" by OST.
1149 * Used to prevent certain races in LOV/OSC where the lock is granted, but LVB
1151 * Assumes LDLM lock is already locked.
1153 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1155 lock->l_flags |= LDLM_FL_LVB_READY;
1156 wake_up_all(&lock->l_waitq);
1158 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
1161 * Mark lock as "matchable" by OST.
1162 * Locks the lock and then \see ldlm_lock_allow_match_locked
1164 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1166 lock_res_and_lock(lock);
1167 ldlm_lock_allow_match_locked(lock);
1168 unlock_res_and_lock(lock);
1170 EXPORT_SYMBOL(ldlm_lock_allow_match);
1173 * Attempt to find a lock with specified properties.
1175 * Typically returns a reference to matched lock unless LDLM_FL_TEST_LOCK is
1178 * Can be called in two ways:
1180 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1181 * for a duplicate of.
1183 * Otherwise, all of the fields must be filled in, to match against.
1185 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1186 * server (ie, connh is NULL)
1187 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1188 * list will be considered
1189 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1190 * to be canceled can still be matched as long as they still have reader
1191 * or writer referneces
1192 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1193 * just tell us if we would have matched.
1195 * \retval 1 if it finds an already-existing lock that is compatible; in this
1196 * case, lockh is filled in with a addref()ed lock
1198 * We also check security context, and if that fails we simply return 0 (to
1199 * keep caller code unchanged), the context failure will be discovered by
1200 * caller sometime later.
1202 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, __u64 flags,
1203 const struct ldlm_res_id *res_id, ldlm_type_t type,
1204 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1205 struct lustre_handle *lockh, int unref)
1207 struct ldlm_resource *res;
1208 struct ldlm_lock *lock, *old_lock = NULL;
1212 old_lock = ldlm_handle2lock(lockh);
1215 ns = ldlm_lock_to_ns(old_lock);
1216 res_id = &old_lock->l_resource->lr_name;
1217 type = old_lock->l_resource->lr_type;
1218 mode = old_lock->l_req_mode;
1221 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1223 LASSERT(old_lock == NULL);
1227 LDLM_RESOURCE_ADDREF(res);
1230 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1236 if (flags & LDLM_FL_BLOCK_GRANTED) {
1240 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1249 LDLM_RESOURCE_DELREF(res);
1250 ldlm_resource_putref(res);
1253 ldlm_lock2handle(lock, lockh);
1254 if ((flags & LDLM_FL_LVB_READY) &&
1255 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1256 __u64 wait_flags = LDLM_FL_LVB_READY |
1257 LDLM_FL_DESTROYED | LDLM_FL_FAIL_NOTIFIED;
1258 struct l_wait_info lwi;
1260 if (lock->l_completion_ast) {
1261 int err = lock->l_completion_ast(lock,
1262 LDLM_FL_WAIT_NOREPROC,
1265 if (flags & LDLM_FL_TEST_LOCK)
1266 LDLM_LOCK_RELEASE(lock);
1268 ldlm_lock_decref_internal(lock,
1275 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1276 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1278 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1279 l_wait_event(lock->l_waitq,
1280 lock->l_flags & wait_flags,
1282 if (!(lock->l_flags & LDLM_FL_LVB_READY)) {
1283 if (flags & LDLM_FL_TEST_LOCK)
1284 LDLM_LOCK_RELEASE(lock);
1286 ldlm_lock_decref_internal(lock, mode);
1293 LDLM_DEBUG(lock, "matched (%llu %llu)",
1294 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1295 res_id->name[2] : policy->l_extent.start,
1296 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1297 res_id->name[3] : policy->l_extent.end);
1299 /* check user's security context */
1300 if (lock->l_conn_export &&
1301 sptlrpc_import_check_ctx(
1302 class_exp2cliimp(lock->l_conn_export))) {
1303 if (!(flags & LDLM_FL_TEST_LOCK))
1304 ldlm_lock_decref_internal(lock, mode);
1308 if (flags & LDLM_FL_TEST_LOCK)
1309 LDLM_LOCK_RELEASE(lock);
1311 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1312 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res %llu/%llu (%llu %llu)",
1313 ns, type, mode, res_id->name[0],
1315 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1316 res_id->name[2] : policy->l_extent.start,
1317 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1318 res_id->name[3] : policy->l_extent.end);
1321 LDLM_LOCK_PUT(old_lock);
1323 return rc ? mode : 0;
1325 EXPORT_SYMBOL(ldlm_lock_match);
1327 ldlm_mode_t ldlm_revalidate_lock_handle(struct lustre_handle *lockh,
1330 struct ldlm_lock *lock;
1331 ldlm_mode_t mode = 0;
1333 lock = ldlm_handle2lock(lockh);
1335 lock_res_and_lock(lock);
1336 if (lock->l_flags & LDLM_FL_GONE_MASK)
1339 if (lock->l_flags & LDLM_FL_CBPENDING &&
1340 lock->l_readers == 0 && lock->l_writers == 0)
1344 *bits = lock->l_policy_data.l_inodebits.bits;
1345 mode = lock->l_granted_mode;
1346 ldlm_lock_addref_internal_nolock(lock, mode);
1351 unlock_res_and_lock(lock);
1352 LDLM_LOCK_PUT(lock);
1356 EXPORT_SYMBOL(ldlm_revalidate_lock_handle);
1358 /** The caller must guarantee that the buffer is large enough. */
1359 int ldlm_fill_lvb(struct ldlm_lock *lock, struct req_capsule *pill,
1360 enum req_location loc, void *data, int size)
1364 LASSERT(data != NULL);
1367 switch (lock->l_lvb_type) {
1369 if (size == sizeof(struct ost_lvb)) {
1370 if (loc == RCL_CLIENT)
1371 lvb = req_capsule_client_swab_get(pill,
1373 lustre_swab_ost_lvb);
1375 lvb = req_capsule_server_swab_get(pill,
1377 lustre_swab_ost_lvb);
1378 if (unlikely(lvb == NULL)) {
1379 LDLM_ERROR(lock, "no LVB");
1383 memcpy(data, lvb, size);
1384 } else if (size == sizeof(struct ost_lvb_v1)) {
1385 struct ost_lvb *olvb = data;
1387 if (loc == RCL_CLIENT)
1388 lvb = req_capsule_client_swab_get(pill,
1390 lustre_swab_ost_lvb_v1);
1392 lvb = req_capsule_server_sized_swab_get(pill,
1394 lustre_swab_ost_lvb_v1);
1395 if (unlikely(lvb == NULL)) {
1396 LDLM_ERROR(lock, "no LVB");
1400 memcpy(data, lvb, size);
1401 olvb->lvb_mtime_ns = 0;
1402 olvb->lvb_atime_ns = 0;
1403 olvb->lvb_ctime_ns = 0;
1405 LDLM_ERROR(lock, "Replied unexpected ost LVB size %d",
1411 if (size == sizeof(struct lquota_lvb)) {
1412 if (loc == RCL_CLIENT)
1413 lvb = req_capsule_client_swab_get(pill,
1415 lustre_swab_lquota_lvb);
1417 lvb = req_capsule_server_swab_get(pill,
1419 lustre_swab_lquota_lvb);
1420 if (unlikely(lvb == NULL)) {
1421 LDLM_ERROR(lock, "no LVB");
1425 memcpy(data, lvb, size);
1428 "Replied unexpected lquota LVB size %d",
1437 if (loc == RCL_CLIENT)
1438 lvb = req_capsule_client_get(pill, &RMF_DLM_LVB);
1440 lvb = req_capsule_server_get(pill, &RMF_DLM_LVB);
1441 if (unlikely(lvb == NULL)) {
1442 LDLM_ERROR(lock, "no LVB");
1446 memcpy(data, lvb, size);
1449 LDLM_ERROR(lock, "Unknown LVB type: %d\n", lock->l_lvb_type);
1458 * Create and fill in new LDLM lock with specified properties.
1459 * Returns a referenced lock
1461 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1462 const struct ldlm_res_id *res_id,
1465 const struct ldlm_callback_suite *cbs,
1466 void *data, __u32 lvb_len,
1467 enum lvb_type lvb_type)
1469 struct ldlm_lock *lock;
1470 struct ldlm_resource *res;
1472 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1476 lock = ldlm_lock_new(res);
1481 lock->l_req_mode = mode;
1482 lock->l_ast_data = data;
1483 lock->l_pid = current_pid();
1485 lock->l_blocking_ast = cbs->lcs_blocking;
1486 lock->l_completion_ast = cbs->lcs_completion;
1487 lock->l_glimpse_ast = cbs->lcs_glimpse;
1490 lock->l_tree_node = NULL;
1491 /* if this is the extent lock, allocate the interval tree node */
1492 if (type == LDLM_EXTENT) {
1493 if (ldlm_interval_alloc(lock) == NULL)
1498 lock->l_lvb_len = lvb_len;
1499 lock->l_lvb_data = kzalloc(lvb_len, GFP_NOFS);
1500 if (!lock->l_lvb_data)
1504 lock->l_lvb_type = lvb_type;
1505 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
1511 ldlm_lock_destroy(lock);
1512 LDLM_LOCK_RELEASE(lock);
1517 * Enqueue (request) a lock.
1518 * On the client this is called from ldlm_cli_enqueue_fini
1519 * after we already got an initial reply from the server with some status.
1521 * Does not block. As a result of enqueue the lock would be put
1522 * into granted or waiting list.
1524 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1525 struct ldlm_lock **lockp,
1526 void *cookie, __u64 *flags)
1528 struct ldlm_lock *lock = *lockp;
1529 struct ldlm_resource *res = lock->l_resource;
1531 lock->l_last_activity = ktime_get_real_seconds();
1533 lock_res_and_lock(lock);
1534 if (lock->l_req_mode == lock->l_granted_mode) {
1535 /* The server returned a blocked lock, but it was granted
1536 * before we got a chance to actually enqueue it. We don't
1537 * need to do anything else. */
1538 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1539 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1543 ldlm_resource_unlink_lock(lock);
1545 /* Cannot happen unless on the server */
1546 if (res->lr_type == LDLM_EXTENT && !lock->l_tree_node)
1549 /* Some flags from the enqueue want to make it into the AST, via the
1550 * lock's l_flags. */
1551 lock->l_flags |= *flags & LDLM_FL_AST_DISCARD_DATA;
1554 * This distinction between local lock trees is very important; a client
1555 * namespace only has information about locks taken by that client, and
1556 * thus doesn't have enough information to decide for itself if it can
1557 * be granted (below). In this case, we do exactly what the server
1558 * tells us to do, as dictated by the 'flags'.
1560 if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1561 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1563 ldlm_grant_lock(lock, NULL);
1566 unlock_res_and_lock(lock);
1571 * Process a call to blocking AST callback for a lock in ast_work list
1574 ldlm_work_bl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1576 struct ldlm_cb_set_arg *arg = opaq;
1577 struct ldlm_lock_desc d;
1579 struct ldlm_lock *lock;
1581 if (list_empty(arg->list))
1584 lock = list_entry(arg->list->next, struct ldlm_lock, l_bl_ast);
1586 /* nobody should touch l_bl_ast */
1587 lock_res_and_lock(lock);
1588 list_del_init(&lock->l_bl_ast);
1590 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1591 LASSERT(lock->l_bl_ast_run == 0);
1592 LASSERT(lock->l_blocking_lock);
1593 lock->l_bl_ast_run++;
1594 unlock_res_and_lock(lock);
1596 ldlm_lock2desc(lock->l_blocking_lock, &d);
1598 rc = lock->l_blocking_ast(lock, &d, (void *)arg, LDLM_CB_BLOCKING);
1599 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1600 lock->l_blocking_lock = NULL;
1601 LDLM_LOCK_RELEASE(lock);
1607 * Process a call to completion AST callback for a lock in ast_work list
1610 ldlm_work_cp_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1612 struct ldlm_cb_set_arg *arg = opaq;
1614 struct ldlm_lock *lock;
1615 ldlm_completion_callback completion_callback;
1617 if (list_empty(arg->list))
1620 lock = list_entry(arg->list->next, struct ldlm_lock, l_cp_ast);
1622 /* It's possible to receive a completion AST before we've set
1623 * the l_completion_ast pointer: either because the AST arrived
1624 * before the reply, or simply because there's a small race
1625 * window between receiving the reply and finishing the local
1626 * enqueue. (bug 842)
1628 * This can't happen with the blocking_ast, however, because we
1629 * will never call the local blocking_ast until we drop our
1630 * reader/writer reference, which we won't do until we get the
1631 * reply and finish enqueueing. */
1633 /* nobody should touch l_cp_ast */
1634 lock_res_and_lock(lock);
1635 list_del_init(&lock->l_cp_ast);
1636 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1637 /* save l_completion_ast since it can be changed by
1638 * mds_intent_policy(), see bug 14225 */
1639 completion_callback = lock->l_completion_ast;
1640 lock->l_flags &= ~LDLM_FL_CP_REQD;
1641 unlock_res_and_lock(lock);
1643 if (completion_callback != NULL)
1644 rc = completion_callback(lock, 0, (void *)arg);
1645 LDLM_LOCK_RELEASE(lock);
1651 * Process a call to revocation AST callback for a lock in ast_work list
1654 ldlm_work_revoke_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1656 struct ldlm_cb_set_arg *arg = opaq;
1657 struct ldlm_lock_desc desc;
1659 struct ldlm_lock *lock;
1661 if (list_empty(arg->list))
1664 lock = list_entry(arg->list->next, struct ldlm_lock, l_rk_ast);
1665 list_del_init(&lock->l_rk_ast);
1667 /* the desc just pretend to exclusive */
1668 ldlm_lock2desc(lock, &desc);
1669 desc.l_req_mode = LCK_EX;
1670 desc.l_granted_mode = 0;
1672 rc = lock->l_blocking_ast(lock, &desc, (void *)arg, LDLM_CB_BLOCKING);
1673 LDLM_LOCK_RELEASE(lock);
1679 * Process a call to glimpse AST callback for a lock in ast_work list
1681 static int ldlm_work_gl_ast_lock(struct ptlrpc_request_set *rqset, void *opaq)
1683 struct ldlm_cb_set_arg *arg = opaq;
1684 struct ldlm_glimpse_work *gl_work;
1685 struct ldlm_lock *lock;
1688 if (list_empty(arg->list))
1691 gl_work = list_entry(arg->list->next, struct ldlm_glimpse_work,
1693 list_del_init(&gl_work->gl_list);
1695 lock = gl_work->gl_lock;
1697 /* transfer the glimpse descriptor to ldlm_cb_set_arg */
1698 arg->gl_desc = gl_work->gl_desc;
1700 /* invoke the actual glimpse callback */
1701 if (lock->l_glimpse_ast(lock, (void *)arg) == 0)
1704 LDLM_LOCK_RELEASE(lock);
1706 if ((gl_work->gl_flags & LDLM_GL_WORK_NOFREE) == 0)
1713 * Process list of locks in need of ASTs being sent.
1715 * Used on server to send multiple ASTs together instead of sending one by
1718 int ldlm_run_ast_work(struct ldlm_namespace *ns, struct list_head *rpc_list,
1719 enum ldlm_desc_ast_t ast_type)
1721 struct ldlm_cb_set_arg *arg;
1722 set_producer_func work_ast_lock;
1725 if (list_empty(rpc_list))
1728 arg = kzalloc(sizeof(*arg), GFP_NOFS);
1732 atomic_set(&arg->restart, 0);
1733 arg->list = rpc_list;
1736 case LDLM_WORK_BL_AST:
1737 arg->type = LDLM_BL_CALLBACK;
1738 work_ast_lock = ldlm_work_bl_ast_lock;
1740 case LDLM_WORK_CP_AST:
1741 arg->type = LDLM_CP_CALLBACK;
1742 work_ast_lock = ldlm_work_cp_ast_lock;
1744 case LDLM_WORK_REVOKE_AST:
1745 arg->type = LDLM_BL_CALLBACK;
1746 work_ast_lock = ldlm_work_revoke_ast_lock;
1748 case LDLM_WORK_GL_AST:
1749 arg->type = LDLM_GL_CALLBACK;
1750 work_ast_lock = ldlm_work_gl_ast_lock;
1756 /* We create a ptlrpc request set with flow control extension.
1757 * This request set will use the work_ast_lock function to produce new
1758 * requests and will send a new request each time one completes in order
1759 * to keep the number of requests in flight to ns_max_parallel_ast */
1760 arg->set = ptlrpc_prep_fcset(ns->ns_max_parallel_ast ? : UINT_MAX,
1761 work_ast_lock, arg);
1762 if (arg->set == NULL) {
1767 ptlrpc_set_wait(arg->set);
1768 ptlrpc_set_destroy(arg->set);
1770 rc = atomic_read(&arg->restart) ? -ERESTART : 0;
1778 * Helper function to call blocking AST for LDLM lock \a lock in a
1779 * "cancelling" mode.
1781 void ldlm_cancel_callback(struct ldlm_lock *lock)
1783 check_res_locked(lock->l_resource);
1784 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1785 lock->l_flags |= LDLM_FL_CANCEL;
1786 if (lock->l_blocking_ast) {
1787 unlock_res_and_lock(lock);
1788 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1790 lock_res_and_lock(lock);
1792 LDLM_DEBUG(lock, "no blocking ast");
1795 lock->l_flags |= LDLM_FL_BL_DONE;
1799 * Remove skiplist-enabled LDLM lock \a req from granted list
1801 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1803 if (req->l_resource->lr_type != LDLM_PLAIN &&
1804 req->l_resource->lr_type != LDLM_IBITS)
1807 list_del_init(&req->l_sl_policy);
1808 list_del_init(&req->l_sl_mode);
1812 * Attempts to cancel LDLM lock \a lock that has no reader/writer references.
1814 void ldlm_lock_cancel(struct ldlm_lock *lock)
1816 struct ldlm_resource *res;
1817 struct ldlm_namespace *ns;
1819 lock_res_and_lock(lock);
1821 res = lock->l_resource;
1822 ns = ldlm_res_to_ns(res);
1824 /* Please do not, no matter how tempting, remove this LBUG without
1825 * talking to me first. -phik */
1826 if (lock->l_readers || lock->l_writers) {
1827 LDLM_ERROR(lock, "lock still has references");
1831 /* Releases cancel callback. */
1832 ldlm_cancel_callback(lock);
1834 ldlm_resource_unlink_lock(lock);
1835 ldlm_lock_destroy_nolock(lock);
1837 if (lock->l_granted_mode == lock->l_req_mode)
1838 ldlm_pool_del(&ns->ns_pool, lock);
1840 /* Make sure we will not be called again for same lock what is possible
1841 * if not to zero out lock->l_granted_mode */
1842 lock->l_granted_mode = LCK_MINMODE;
1843 unlock_res_and_lock(lock);
1845 EXPORT_SYMBOL(ldlm_lock_cancel);
1848 * Set opaque data into the lock that only makes sense to upper layer.
1850 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1852 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1856 if (lock->l_ast_data == NULL)
1857 lock->l_ast_data = data;
1858 if (lock->l_ast_data == data)
1860 LDLM_LOCK_PUT(lock);
1864 EXPORT_SYMBOL(ldlm_lock_set_data);
1866 struct export_cl_data {
1867 struct obd_export *ecl_exp;
1872 * Print lock with lock handle \a lockh description into debug log.
1874 * Used when printing all locks on a resource for debug purposes.
1876 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1878 struct ldlm_lock *lock;
1880 if (!((libcfs_debug | D_ERROR) & level))
1883 lock = ldlm_handle2lock(lockh);
1887 LDLM_DEBUG_LIMIT(level, lock, "###");
1889 LDLM_LOCK_PUT(lock);
1891 EXPORT_SYMBOL(ldlm_lock_dump_handle);
1894 * Print lock information with custom message into debug log.
1897 void _ldlm_lock_debug(struct ldlm_lock *lock,
1898 struct libcfs_debug_msg_data *msgdata,
1899 const char *fmt, ...)
1902 struct obd_export *exp = lock->l_export;
1903 struct ldlm_resource *resource = lock->l_resource;
1904 char *nid = "local";
1906 va_start(args, fmt);
1908 if (exp && exp->exp_connection) {
1909 nid = libcfs_nid2str(exp->exp_connection->c_peer.nid);
1910 } else if (exp && exp->exp_obd != NULL) {
1911 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
1913 nid = libcfs_nid2str(imp->imp_connection->c_peer.nid);
1916 if (resource == NULL) {
1917 libcfs_debug_vmsg2(msgdata, fmt, args,
1918 " 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",
1920 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1921 lock->l_readers, lock->l_writers,
1922 ldlm_lockname[lock->l_granted_mode],
1923 ldlm_lockname[lock->l_req_mode],
1924 lock->l_flags, nid, lock->l_remote_handle.cookie,
1925 exp ? atomic_read(&exp->exp_refcount) : -99,
1926 lock->l_pid, lock->l_callback_timeout, lock->l_lvb_type);
1931 switch (resource->lr_type) {
1933 libcfs_debug_vmsg2(msgdata, fmt, args,
1934 " 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",
1935 ldlm_lock_to_ns_name(lock), lock,
1936 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1937 lock->l_readers, lock->l_writers,
1938 ldlm_lockname[lock->l_granted_mode],
1939 ldlm_lockname[lock->l_req_mode],
1941 atomic_read(&resource->lr_refcount),
1942 ldlm_typename[resource->lr_type],
1943 lock->l_policy_data.l_extent.start,
1944 lock->l_policy_data.l_extent.end,
1945 lock->l_req_extent.start, lock->l_req_extent.end,
1946 lock->l_flags, nid, lock->l_remote_handle.cookie,
1947 exp ? atomic_read(&exp->exp_refcount) : -99,
1948 lock->l_pid, lock->l_callback_timeout,
1953 libcfs_debug_vmsg2(msgdata, fmt, args,
1954 " 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",
1955 ldlm_lock_to_ns_name(lock), lock,
1956 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1957 lock->l_readers, lock->l_writers,
1958 ldlm_lockname[lock->l_granted_mode],
1959 ldlm_lockname[lock->l_req_mode],
1961 atomic_read(&resource->lr_refcount),
1962 ldlm_typename[resource->lr_type],
1963 lock->l_policy_data.l_flock.pid,
1964 lock->l_policy_data.l_flock.start,
1965 lock->l_policy_data.l_flock.end,
1966 lock->l_flags, nid, lock->l_remote_handle.cookie,
1967 exp ? atomic_read(&exp->exp_refcount) : -99,
1968 lock->l_pid, lock->l_callback_timeout);
1972 libcfs_debug_vmsg2(msgdata, fmt, args,
1973 " 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",
1974 ldlm_lock_to_ns_name(lock),
1975 lock, lock->l_handle.h_cookie,
1976 atomic_read(&lock->l_refc),
1977 lock->l_readers, lock->l_writers,
1978 ldlm_lockname[lock->l_granted_mode],
1979 ldlm_lockname[lock->l_req_mode],
1981 lock->l_policy_data.l_inodebits.bits,
1982 atomic_read(&resource->lr_refcount),
1983 ldlm_typename[resource->lr_type],
1984 lock->l_flags, nid, lock->l_remote_handle.cookie,
1985 exp ? atomic_read(&exp->exp_refcount) : -99,
1986 lock->l_pid, lock->l_callback_timeout,
1991 libcfs_debug_vmsg2(msgdata, fmt, args,
1992 " 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",
1993 ldlm_lock_to_ns_name(lock),
1994 lock, lock->l_handle.h_cookie,
1995 atomic_read(&lock->l_refc),
1996 lock->l_readers, lock->l_writers,
1997 ldlm_lockname[lock->l_granted_mode],
1998 ldlm_lockname[lock->l_req_mode],
2000 atomic_read(&resource->lr_refcount),
2001 ldlm_typename[resource->lr_type],
2002 lock->l_flags, nid, lock->l_remote_handle.cookie,
2003 exp ? atomic_read(&exp->exp_refcount) : -99,
2004 lock->l_pid, lock->l_callback_timeout,
2010 EXPORT_SYMBOL(_ldlm_lock_debug);