4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lock.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_LDLM
45 # include <libcfs/libcfs.h>
46 # include <linux/lustre_intent.h>
48 # include <liblustre.h>
51 #include <obd_class.h>
52 #include "ldlm_internal.h"
55 char *ldlm_lockname[] = {
67 char *ldlm_typename[] = {
74 static ldlm_policy_wire_to_local_t ldlm_policy_wire18_to_local[] = {
75 [LDLM_PLAIN - LDLM_MIN_TYPE] ldlm_plain_policy_wire_to_local,
76 [LDLM_EXTENT - LDLM_MIN_TYPE] ldlm_extent_policy_wire_to_local,
77 [LDLM_FLOCK - LDLM_MIN_TYPE] ldlm_flock_policy_wire18_to_local,
78 [LDLM_IBITS - LDLM_MIN_TYPE] ldlm_ibits_policy_wire_to_local,
81 static ldlm_policy_wire_to_local_t ldlm_policy_wire21_to_local[] = {
82 [LDLM_PLAIN - LDLM_MIN_TYPE] ldlm_plain_policy_wire_to_local,
83 [LDLM_EXTENT - LDLM_MIN_TYPE] ldlm_extent_policy_wire_to_local,
84 [LDLM_FLOCK - LDLM_MIN_TYPE] ldlm_flock_policy_wire21_to_local,
85 [LDLM_IBITS - LDLM_MIN_TYPE] ldlm_ibits_policy_wire_to_local,
88 static ldlm_policy_local_to_wire_t ldlm_policy_local_to_wire[] = {
89 [LDLM_PLAIN - LDLM_MIN_TYPE] ldlm_plain_policy_local_to_wire,
90 [LDLM_EXTENT - LDLM_MIN_TYPE] ldlm_extent_policy_local_to_wire,
91 [LDLM_FLOCK - LDLM_MIN_TYPE] ldlm_flock_policy_local_to_wire,
92 [LDLM_IBITS - LDLM_MIN_TYPE] ldlm_ibits_policy_local_to_wire,
96 * Converts lock policy from local format to on the wire lock_desc format
98 void ldlm_convert_policy_to_wire(ldlm_type_t type,
99 const ldlm_policy_data_t *lpolicy,
100 ldlm_wire_policy_data_t *wpolicy)
102 ldlm_policy_local_to_wire_t convert;
104 convert = ldlm_policy_local_to_wire[type - LDLM_MIN_TYPE];
106 convert(lpolicy, wpolicy);
110 * Converts lock policy from on the wire lock_desc format to local format
112 void ldlm_convert_policy_to_local(struct obd_export *exp, ldlm_type_t type,
113 const ldlm_wire_policy_data_t *wpolicy,
114 ldlm_policy_data_t *lpolicy)
116 ldlm_policy_wire_to_local_t convert;
119 /** some badnes for 2.0.0 clients, but 2.0.0 isn't supported */
120 new_client = (exp->exp_connect_flags & OBD_CONNECT_FULL20) != 0;
122 convert = ldlm_policy_wire21_to_local[type - LDLM_MIN_TYPE];
124 convert = ldlm_policy_wire18_to_local[type - LDLM_MIN_TYPE];
126 convert(wpolicy, lpolicy);
129 char *ldlm_it2str(int it)
136 case (IT_OPEN | IT_CREAT):
151 CERROR("Unknown intent %d\n", it);
156 extern cfs_mem_cache_t *ldlm_lock_slab;
158 #ifdef HAVE_SERVER_SUPPORT
159 static ldlm_processing_policy ldlm_processing_policy_table[] = {
160 [LDLM_PLAIN] ldlm_process_plain_lock,
161 [LDLM_EXTENT] ldlm_process_extent_lock,
163 [LDLM_FLOCK] ldlm_process_flock_lock,
165 [LDLM_IBITS] ldlm_process_inodebits_lock,
168 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
170 return ldlm_processing_policy_table[res->lr_type];
172 #endif /* HAVE_SERVER_SUPPORT */
174 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
180 * REFCOUNTED LOCK OBJECTS
185 * Lock refcounts, during creation:
186 * - one special one for allocation, dec'd only once in destroy
187 * - one for being a lock that's in-use
188 * - one for the addref associated with a new lock
190 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
192 cfs_atomic_inc(&lock->l_refc);
196 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
198 LASSERT(size == sizeof(*lock));
199 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
202 void ldlm_lock_put(struct ldlm_lock *lock)
206 LASSERT(lock->l_resource != LP_POISON);
207 LASSERT(cfs_atomic_read(&lock->l_refc) > 0);
208 if (cfs_atomic_dec_and_test(&lock->l_refc)) {
209 struct ldlm_resource *res;
212 "final lock_put on destroyed lock, freeing it.");
214 res = lock->l_resource;
215 LASSERT(lock->l_destroyed);
216 LASSERT(cfs_list_empty(&lock->l_res_link));
217 LASSERT(cfs_list_empty(&lock->l_pending_chain));
219 lprocfs_counter_decr(ldlm_res_to_ns(res)->ns_stats,
221 lu_ref_del(&res->lr_reference, "lock", lock);
222 ldlm_resource_putref(res);
223 lock->l_resource = NULL;
224 if (lock->l_export) {
225 class_export_lock_put(lock->l_export, lock);
226 lock->l_export = NULL;
229 if (lock->l_lvb_data != NULL)
230 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
232 ldlm_interval_free(ldlm_interval_detach(lock));
233 lu_ref_fini(&lock->l_reference);
234 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
241 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
244 if (!cfs_list_empty(&lock->l_lru)) {
245 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
247 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
248 cfs_list_del_init(&lock->l_lru);
249 if (lock->l_flags & LDLM_FL_SKIPPED)
250 lock->l_flags &= ~LDLM_FL_SKIPPED;
251 LASSERT(ns->ns_nr_unused > 0);
258 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
260 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
264 if (lock->l_ns_srv) {
265 LASSERT(cfs_list_empty(&lock->l_lru));
269 cfs_spin_lock(&ns->ns_lock);
270 rc = ldlm_lock_remove_from_lru_nolock(lock);
271 cfs_spin_unlock(&ns->ns_lock);
276 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
278 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
280 lock->l_last_used = cfs_time_current();
281 LASSERT(cfs_list_empty(&lock->l_lru));
282 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
283 cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
284 LASSERT(ns->ns_nr_unused >= 0);
288 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
290 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
293 cfs_spin_lock(&ns->ns_lock);
294 ldlm_lock_add_to_lru_nolock(lock);
295 cfs_spin_unlock(&ns->ns_lock);
299 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
301 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
304 if (lock->l_ns_srv) {
305 LASSERT(cfs_list_empty(&lock->l_lru));
310 cfs_spin_lock(&ns->ns_lock);
311 if (!cfs_list_empty(&lock->l_lru)) {
312 ldlm_lock_remove_from_lru_nolock(lock);
313 ldlm_lock_add_to_lru_nolock(lock);
315 cfs_spin_unlock(&ns->ns_lock);
319 /* This used to have a 'strict' flag, which recovery would use to mark an
320 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
321 * shall explain why it's gone: with the new hash table scheme, once you call
322 * ldlm_lock_destroy, you can never drop your final references on this lock.
323 * Because it's not in the hash table anymore. -phil */
324 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
328 if (lock->l_readers || lock->l_writers) {
329 LDLM_ERROR(lock, "lock still has references");
333 if (!cfs_list_empty(&lock->l_res_link)) {
334 LDLM_ERROR(lock, "lock still on resource");
338 if (lock->l_destroyed) {
339 LASSERT(cfs_list_empty(&lock->l_lru));
343 lock->l_destroyed = 1;
345 if (lock->l_export && lock->l_export->exp_lock_hash &&
346 !cfs_hlist_unhashed(&lock->l_exp_hash))
347 cfs_hash_del(lock->l_export->exp_lock_hash,
348 &lock->l_remote_handle, &lock->l_exp_hash);
350 ldlm_lock_remove_from_lru(lock);
351 class_handle_unhash(&lock->l_handle);
354 /* Wake anyone waiting for this lock */
355 /* FIXME: I should probably add yet another flag, instead of using
356 * l_export to only call this on clients */
358 class_export_put(lock->l_export);
359 lock->l_export = NULL;
360 if (lock->l_export && lock->l_completion_ast)
361 lock->l_completion_ast(lock, 0);
367 void ldlm_lock_destroy(struct ldlm_lock *lock)
371 lock_res_and_lock(lock);
372 first = ldlm_lock_destroy_internal(lock);
373 unlock_res_and_lock(lock);
375 /* drop reference from hashtable only for first destroy */
377 lu_ref_del(&lock->l_reference, "hash", lock);
378 LDLM_LOCK_RELEASE(lock);
383 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
387 first = ldlm_lock_destroy_internal(lock);
388 /* drop reference from hashtable only for first destroy */
390 lu_ref_del(&lock->l_reference, "hash", lock);
391 LDLM_LOCK_RELEASE(lock);
396 /* this is called by portals_handle2object with the handle lock taken */
397 static void lock_handle_addref(void *lock)
399 LDLM_LOCK_GET((struct ldlm_lock *)lock);
403 * usage: pass in a resource on which you have done ldlm_resource_get
404 * new lock will take over the refcount.
405 * returns: lock with refcount 2 - one for current caller and one for remote
407 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
409 struct ldlm_lock *lock;
412 if (resource == NULL)
415 OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
419 cfs_spin_lock_init(&lock->l_lock);
420 lock->l_resource = resource;
421 lu_ref_add(&resource->lr_reference, "lock", lock);
423 cfs_atomic_set(&lock->l_refc, 2);
424 CFS_INIT_LIST_HEAD(&lock->l_res_link);
425 CFS_INIT_LIST_HEAD(&lock->l_lru);
426 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
427 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
428 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
429 CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
430 cfs_waitq_init(&lock->l_waitq);
431 lock->l_blocking_lock = NULL;
432 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
433 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
434 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
436 lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
438 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
439 class_handle_hash(&lock->l_handle, lock_handle_addref);
441 lu_ref_init(&lock->l_reference);
442 lu_ref_add(&lock->l_reference, "hash", lock);
443 lock->l_callback_timeout = 0;
445 #if LUSTRE_TRACKS_LOCK_EXP_REFS
446 CFS_INIT_LIST_HEAD(&lock->l_exp_refs_link);
447 lock->l_exp_refs_nr = 0;
448 lock->l_exp_refs_target = NULL;
450 CFS_INIT_LIST_HEAD(&lock->l_exp_list);
455 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
456 const struct ldlm_res_id *new_resid)
458 struct ldlm_resource *oldres = lock->l_resource;
459 struct ldlm_resource *newres;
463 LASSERT(ns_is_client(ns));
465 lock_res_and_lock(lock);
466 if (memcmp(new_resid, &lock->l_resource->lr_name,
467 sizeof(lock->l_resource->lr_name)) == 0) {
469 unlock_res_and_lock(lock);
473 LASSERT(new_resid->name[0] != 0);
475 /* This function assumes that the lock isn't on any lists */
476 LASSERT(cfs_list_empty(&lock->l_res_link));
478 type = oldres->lr_type;
479 unlock_res_and_lock(lock);
481 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
485 lu_ref_add(&newres->lr_reference, "lock", lock);
487 * To flip the lock from the old to the new resource, lock, oldres and
488 * newres have to be locked. Resource spin-locks are nested within
489 * lock->l_lock, and are taken in the memory address order to avoid
492 cfs_spin_lock(&lock->l_lock);
493 oldres = lock->l_resource;
494 if (oldres < newres) {
496 lock_res_nested(newres, LRT_NEW);
499 lock_res_nested(oldres, LRT_NEW);
501 LASSERT(memcmp(new_resid, &oldres->lr_name,
502 sizeof oldres->lr_name) != 0);
503 lock->l_resource = newres;
505 unlock_res_and_lock(lock);
507 /* ...and the flowers are still standing! */
508 lu_ref_del(&oldres->lr_reference, "lock", lock);
509 ldlm_resource_putref(oldres);
518 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
520 lockh->cookie = lock->l_handle.h_cookie;
523 /* if flags: atomically get the lock and set the flags.
524 * Return NULL if flag already set
527 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
530 struct ldlm_lock *lock;
535 lock = class_handle2object(handle->cookie);
539 /* It's unlikely but possible that someone marked the lock as
540 * destroyed after we did handle2object on it */
541 if (flags == 0 && !lock->l_destroyed) {
542 lu_ref_add(&lock->l_reference, "handle", cfs_current());
546 lock_res_and_lock(lock);
548 LASSERT(lock->l_resource != NULL);
550 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
551 if (unlikely(lock->l_destroyed)) {
552 unlock_res_and_lock(lock);
553 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
558 if (flags && (lock->l_flags & flags)) {
559 unlock_res_and_lock(lock);
565 lock->l_flags |= flags;
567 unlock_res_and_lock(lock);
571 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
573 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
574 /* INODEBITS_INTEROP: If the other side does not support
575 * inodebits, reply with a plain lock descriptor.
577 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
578 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
579 /* Make sure all the right bits are set in this lock we
580 are going to pass to client */
581 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
582 (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
583 MDS_INODELOCK_LAYOUT),
584 "Inappropriate inode lock bits during "
585 "conversion " LPU64 "\n",
586 lock->l_policy_data.l_inodebits.bits);
588 ldlm_res2desc(lock->l_resource, &desc->l_resource);
589 desc->l_resource.lr_type = LDLM_PLAIN;
591 /* Convert "new" lock mode to something old client can
593 if ((lock->l_req_mode == LCK_CR) ||
594 (lock->l_req_mode == LCK_CW))
595 desc->l_req_mode = LCK_PR;
597 desc->l_req_mode = lock->l_req_mode;
598 if ((lock->l_granted_mode == LCK_CR) ||
599 (lock->l_granted_mode == LCK_CW)) {
600 desc->l_granted_mode = LCK_PR;
602 /* We never grant PW/EX locks to clients */
603 LASSERT((lock->l_granted_mode != LCK_PW) &&
604 (lock->l_granted_mode != LCK_EX));
605 desc->l_granted_mode = lock->l_granted_mode;
608 /* We do not copy policy here, because there is no
609 policy for plain locks */
611 ldlm_res2desc(lock->l_resource, &desc->l_resource);
612 desc->l_req_mode = lock->l_req_mode;
613 desc->l_granted_mode = lock->l_granted_mode;
614 ldlm_convert_policy_to_wire(lock->l_resource->lr_type,
615 &lock->l_policy_data,
616 &desc->l_policy_data);
620 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
621 cfs_list_t *work_list)
623 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
624 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
625 lock->l_flags |= LDLM_FL_AST_SENT;
626 /* If the enqueuing client said so, tell the AST recipient to
627 * discard dirty data, rather than writing back. */
628 if (new->l_flags & LDLM_AST_DISCARD_DATA)
629 lock->l_flags |= LDLM_FL_DISCARD_DATA;
630 LASSERT(cfs_list_empty(&lock->l_bl_ast));
631 cfs_list_add(&lock->l_bl_ast, work_list);
633 LASSERT(lock->l_blocking_lock == NULL);
634 lock->l_blocking_lock = LDLM_LOCK_GET(new);
638 void ldlm_add_cp_work_item(struct ldlm_lock *lock, cfs_list_t *work_list)
640 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
641 lock->l_flags |= LDLM_FL_CP_REQD;
642 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
643 LASSERT(cfs_list_empty(&lock->l_cp_ast));
644 cfs_list_add(&lock->l_cp_ast, work_list);
649 /* must be called with lr_lock held */
650 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
651 cfs_list_t *work_list)
654 check_res_locked(lock->l_resource);
656 ldlm_add_bl_work_item(lock, new, work_list);
658 ldlm_add_cp_work_item(lock, work_list);
662 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
664 struct ldlm_lock *lock;
666 lock = ldlm_handle2lock(lockh);
667 LASSERT(lock != NULL);
668 ldlm_lock_addref_internal(lock, mode);
672 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
674 ldlm_lock_remove_from_lru(lock);
675 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
677 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
679 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
681 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
684 lu_ref_add_atomic(&lock->l_reference, "user", lock);
685 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
689 * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
692 * \retval 0 success, lock was addref-ed
694 * \retval -EAGAIN lock is being canceled.
696 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
698 struct ldlm_lock *lock;
702 lock = ldlm_handle2lock(lockh);
704 lock_res_and_lock(lock);
705 if (lock->l_readers != 0 || lock->l_writers != 0 ||
706 !(lock->l_flags & LDLM_FL_CBPENDING)) {
707 ldlm_lock_addref_internal_nolock(lock, mode);
710 unlock_res_and_lock(lock);
716 /* only called for local locks */
717 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
719 lock_res_and_lock(lock);
720 ldlm_lock_addref_internal_nolock(lock, mode);
721 unlock_res_and_lock(lock);
724 /* only called in ldlm_flock_destroy and for local locks.
725 * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
726 * * ldlm_lock_remove_from_lru() does nothing, it is safe
727 * * for ldlm_flock_destroy usage by dropping some code */
728 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
730 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
731 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
732 LASSERT(lock->l_readers > 0);
733 lu_ref_del(&lock->l_reference, "reader", lock);
736 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
737 LASSERT(lock->l_writers > 0);
738 lu_ref_del(&lock->l_reference, "writer", lock);
742 lu_ref_del(&lock->l_reference, "user", lock);
743 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
746 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
748 struct ldlm_namespace *ns;
751 lock_res_and_lock(lock);
753 ns = ldlm_lock_to_ns(lock);
755 ldlm_lock_decref_internal_nolock(lock, mode);
757 if (lock->l_flags & LDLM_FL_LOCAL &&
758 !lock->l_readers && !lock->l_writers) {
759 /* If this is a local lock on a server namespace and this was
760 * the last reference, cancel the lock. */
761 CDEBUG(D_INFO, "forcing cancel of local lock\n");
762 lock->l_flags |= LDLM_FL_CBPENDING;
765 if (!lock->l_readers && !lock->l_writers &&
766 (lock->l_flags & LDLM_FL_CBPENDING)) {
767 /* If we received a blocked AST and this was the last reference,
768 * run the callback. */
769 if (lock->l_ns_srv && lock->l_export)
770 CERROR("FL_CBPENDING set on non-local lock--just a "
773 LDLM_DEBUG(lock, "final decref done on cbpending lock");
775 LDLM_LOCK_GET(lock); /* dropped by bl thread */
776 ldlm_lock_remove_from_lru(lock);
777 unlock_res_and_lock(lock);
779 if (lock->l_flags & LDLM_FL_FAIL_LOC)
780 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
782 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
783 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
784 ldlm_handle_bl_callback(ns, NULL, lock);
785 } else if (ns_is_client(ns) &&
786 !lock->l_readers && !lock->l_writers &&
787 !(lock->l_flags & LDLM_FL_NO_LRU) &&
788 !(lock->l_flags & LDLM_FL_BL_AST)) {
790 LDLM_DEBUG(lock, "add lock into lru list");
792 /* If this is a client-side namespace and this was the last
793 * reference, put it on the LRU. */
794 ldlm_lock_add_to_lru(lock);
795 unlock_res_and_lock(lock);
797 if (lock->l_flags & LDLM_FL_FAIL_LOC)
798 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
800 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
801 * are not supported by the server, otherwise, it is done on
803 if (!exp_connect_cancelset(lock->l_conn_export) &&
804 !ns_connect_lru_resize(ns))
805 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
807 LDLM_DEBUG(lock, "do not add lock into lru list");
808 unlock_res_and_lock(lock);
814 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
816 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
817 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
818 ldlm_lock_decref_internal(lock, mode);
822 /* This will drop a lock reference and mark it for destruction, but will not
823 * necessarily cancel the lock before returning. */
824 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
826 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
829 LASSERT(lock != NULL);
831 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
832 lock_res_and_lock(lock);
833 lock->l_flags |= LDLM_FL_CBPENDING;
834 unlock_res_and_lock(lock);
835 ldlm_lock_decref_internal(lock, mode);
839 struct sl_insert_point {
840 cfs_list_t *res_link;
841 cfs_list_t *mode_link;
842 cfs_list_t *policy_link;
846 * search_granted_lock
849 * Finds a position to insert the new lock.
851 * queue [input]: the granted list where search acts on;
852 * req [input]: the lock whose position to be located;
853 * prev [output]: positions within 3 lists to insert @req to
857 * - ldlm_grant_lock_with_skiplist
859 static void search_granted_lock(cfs_list_t *queue,
860 struct ldlm_lock *req,
861 struct sl_insert_point *prev)
864 struct ldlm_lock *lock, *mode_end, *policy_end;
867 cfs_list_for_each(tmp, queue) {
868 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
870 mode_end = cfs_list_entry(lock->l_sl_mode.prev,
871 struct ldlm_lock, l_sl_mode);
873 if (lock->l_req_mode != req->l_req_mode) {
874 /* jump to last lock of mode group */
875 tmp = &mode_end->l_res_link;
879 /* suitable mode group is found */
880 if (lock->l_resource->lr_type == LDLM_PLAIN) {
881 /* insert point is last lock of the mode group */
882 prev->res_link = &mode_end->l_res_link;
883 prev->mode_link = &mode_end->l_sl_mode;
884 prev->policy_link = &req->l_sl_policy;
887 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
890 cfs_list_entry(lock->l_sl_policy.prev,
894 if (lock->l_policy_data.l_inodebits.bits ==
895 req->l_policy_data.l_inodebits.bits) {
896 /* insert point is last lock of
897 * the policy group */
899 &policy_end->l_res_link;
901 &policy_end->l_sl_mode;
903 &policy_end->l_sl_policy;
908 if (policy_end == mode_end)
909 /* done with mode group */
912 /* go to next policy group within mode group */
913 tmp = policy_end->l_res_link.next;
914 lock = cfs_list_entry(tmp, struct ldlm_lock,
916 } /* loop over policy groups within the mode group */
918 /* insert point is last lock of the mode group,
919 * new policy group is started */
920 prev->res_link = &mode_end->l_res_link;
921 prev->mode_link = &mode_end->l_sl_mode;
922 prev->policy_link = &req->l_sl_policy;
926 LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
931 /* insert point is last lock on the queue,
932 * new mode group and new policy group are started */
933 prev->res_link = queue->prev;
934 prev->mode_link = &req->l_sl_mode;
935 prev->policy_link = &req->l_sl_policy;
940 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
941 struct sl_insert_point *prev)
943 struct ldlm_resource *res = lock->l_resource;
946 check_res_locked(res);
948 ldlm_resource_dump(D_INFO, res);
949 LDLM_DEBUG(lock, "About to add lock:");
951 if (lock->l_destroyed) {
952 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
956 LASSERT(cfs_list_empty(&lock->l_res_link));
957 LASSERT(cfs_list_empty(&lock->l_sl_mode));
958 LASSERT(cfs_list_empty(&lock->l_sl_policy));
960 cfs_list_add(&lock->l_res_link, prev->res_link);
961 cfs_list_add(&lock->l_sl_mode, prev->mode_link);
962 cfs_list_add(&lock->l_sl_policy, prev->policy_link);
967 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
969 struct sl_insert_point prev;
972 LASSERT(lock->l_req_mode == lock->l_granted_mode);
974 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
975 ldlm_granted_list_add_lock(lock, &prev);
980 * - ldlm_lock_enqueue
981 * - ldlm_reprocess_queue
982 * - ldlm_lock_convert
984 * must be called with lr_lock held
986 void ldlm_grant_lock(struct ldlm_lock *lock, cfs_list_t *work_list)
988 struct ldlm_resource *res = lock->l_resource;
991 check_res_locked(res);
993 lock->l_granted_mode = lock->l_req_mode;
994 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
995 ldlm_grant_lock_with_skiplist(lock);
996 else if (res->lr_type == LDLM_EXTENT)
997 ldlm_extent_add_lock(res, lock);
999 ldlm_resource_add_lock(res, &res->lr_granted, lock);
1001 if (lock->l_granted_mode < res->lr_most_restr)
1002 res->lr_most_restr = lock->l_granted_mode;
1004 if (work_list && lock->l_completion_ast != NULL)
1005 ldlm_add_ast_work_item(lock, NULL, work_list);
1007 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
1011 /* returns a referenced lock or NULL. See the flag descriptions below, in the
1012 * comment above ldlm_lock_match */
1013 static struct ldlm_lock *search_queue(cfs_list_t *queue,
1015 ldlm_policy_data_t *policy,
1016 struct ldlm_lock *old_lock,
1017 int flags, int unref)
1019 struct ldlm_lock *lock;
1022 cfs_list_for_each(tmp, queue) {
1025 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1027 if (lock == old_lock)
1030 /* llite sometimes wants to match locks that will be
1031 * canceled when their users drop, but we allow it to match
1032 * if it passes in CBPENDING and the lock still has users.
1033 * this is generally only going to be used by children
1034 * whose parents already hold a lock so forward progress
1035 * can still happen. */
1036 if (lock->l_flags & LDLM_FL_CBPENDING &&
1037 !(flags & LDLM_FL_CBPENDING))
1039 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
1040 lock->l_readers == 0 && lock->l_writers == 0)
1043 if (!(lock->l_req_mode & *mode))
1045 match = lock->l_req_mode;
1047 if (lock->l_resource->lr_type == LDLM_EXTENT &&
1048 (lock->l_policy_data.l_extent.start >
1049 policy->l_extent.start ||
1050 lock->l_policy_data.l_extent.end < policy->l_extent.end))
1053 if (unlikely(match == LCK_GROUP) &&
1054 lock->l_resource->lr_type == LDLM_EXTENT &&
1055 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
1058 /* We match if we have existing lock with same or wider set
1060 if (lock->l_resource->lr_type == LDLM_IBITS &&
1061 ((lock->l_policy_data.l_inodebits.bits &
1062 policy->l_inodebits.bits) !=
1063 policy->l_inodebits.bits))
1067 (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
1071 if ((flags & LDLM_FL_LOCAL_ONLY) &&
1072 !(lock->l_flags & LDLM_FL_LOCAL))
1075 if (flags & LDLM_FL_TEST_LOCK) {
1076 LDLM_LOCK_GET(lock);
1077 ldlm_lock_touch_in_lru(lock);
1079 ldlm_lock_addref_internal_nolock(lock, match);
1088 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock)
1090 if (!lock->l_failed) {
1092 cfs_waitq_broadcast(&lock->l_waitq);
1095 EXPORT_SYMBOL(ldlm_lock_fail_match_locked);
1097 void ldlm_lock_fail_match(struct ldlm_lock *lock)
1099 lock_res_and_lock(lock);
1100 ldlm_lock_fail_match_locked(lock);
1101 unlock_res_and_lock(lock);
1103 EXPORT_SYMBOL(ldlm_lock_fail_match);
1105 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1107 lock->l_flags |= LDLM_FL_LVB_READY;
1108 cfs_waitq_broadcast(&lock->l_waitq);
1111 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1113 lock_res_and_lock(lock);
1114 ldlm_lock_allow_match_locked(lock);
1115 unlock_res_and_lock(lock);
1118 /* Can be called in two ways:
1120 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1121 * for a duplicate of.
1123 * Otherwise, all of the fields must be filled in, to match against.
1125 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1126 * server (ie, connh is NULL)
1127 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1128 * list will be considered
1129 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1130 * to be canceled can still be matched as long as they still have reader
1131 * or writer refernces
1132 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1133 * just tell us if we would have matched.
1135 * Returns 1 if it finds an already-existing lock that is compatible; in this
1136 * case, lockh is filled in with a addref()ed lock
1138 * we also check security context, if that failed we simply return 0 (to keep
1139 * caller code unchanged), the context failure will be discovered by caller
1142 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1143 const struct ldlm_res_id *res_id, ldlm_type_t type,
1144 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1145 struct lustre_handle *lockh, int unref)
1147 struct ldlm_resource *res;
1148 struct ldlm_lock *lock, *old_lock = NULL;
1153 old_lock = ldlm_handle2lock(lockh);
1156 ns = ldlm_lock_to_ns(old_lock);
1157 res_id = &old_lock->l_resource->lr_name;
1158 type = old_lock->l_resource->lr_type;
1159 mode = old_lock->l_req_mode;
1162 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1164 LASSERT(old_lock == NULL);
1168 LDLM_RESOURCE_ADDREF(res);
1171 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1175 if (flags & LDLM_FL_BLOCK_GRANTED)
1177 lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1181 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1189 LDLM_RESOURCE_DELREF(res);
1190 ldlm_resource_putref(res);
1193 ldlm_lock2handle(lock, lockh);
1194 if ((flags & LDLM_FL_LVB_READY) &&
1195 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1196 struct l_wait_info lwi;
1197 if (lock->l_completion_ast) {
1198 int err = lock->l_completion_ast(lock,
1199 LDLM_FL_WAIT_NOREPROC,
1202 if (flags & LDLM_FL_TEST_LOCK)
1203 LDLM_LOCK_RELEASE(lock);
1205 ldlm_lock_decref_internal(lock,
1212 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1213 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1215 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1216 l_wait_event(lock->l_waitq,
1217 lock->l_flags & LDLM_FL_LVB_READY ||
1220 if (!(lock->l_flags & LDLM_FL_LVB_READY)) {
1221 if (flags & LDLM_FL_TEST_LOCK)
1222 LDLM_LOCK_RELEASE(lock);
1224 ldlm_lock_decref_internal(lock, mode);
1231 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1232 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1233 res_id->name[2] : policy->l_extent.start,
1234 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1235 res_id->name[3] : policy->l_extent.end);
1237 /* check user's security context */
1238 if (lock->l_conn_export &&
1239 sptlrpc_import_check_ctx(
1240 class_exp2cliimp(lock->l_conn_export))) {
1241 if (!(flags & LDLM_FL_TEST_LOCK))
1242 ldlm_lock_decref_internal(lock, mode);
1246 if (flags & LDLM_FL_TEST_LOCK)
1247 LDLM_LOCK_RELEASE(lock);
1249 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1250 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1251 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1252 type, mode, res_id->name[0], res_id->name[1],
1253 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1254 res_id->name[2] :policy->l_extent.start,
1255 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1256 res_id->name[3] : policy->l_extent.end);
1259 LDLM_LOCK_PUT(old_lock);
1261 return rc ? mode : 0;
1264 ldlm_mode_t ldlm_revalidate_lock_handle(struct lustre_handle *lockh,
1267 struct ldlm_lock *lock;
1268 ldlm_mode_t mode = 0;
1271 lock = ldlm_handle2lock(lockh);
1273 lock_res_and_lock(lock);
1274 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
1278 if (lock->l_flags & LDLM_FL_CBPENDING &&
1279 lock->l_readers == 0 && lock->l_writers == 0)
1283 *bits = lock->l_policy_data.l_inodebits.bits;
1284 mode = lock->l_granted_mode;
1285 ldlm_lock_addref_internal_nolock(lock, mode);
1292 unlock_res_and_lock(lock);
1293 LDLM_LOCK_PUT(lock);
1297 EXPORT_SYMBOL(ldlm_revalidate_lock_handle);
1299 /* Returns a referenced lock */
1300 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1301 const struct ldlm_res_id *res_id,
1304 const struct ldlm_callback_suite *cbs,
1305 void *data, __u32 lvb_len)
1307 struct ldlm_lock *lock;
1308 struct ldlm_resource *res;
1311 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1315 lock = ldlm_lock_new(res);
1320 lock->l_req_mode = mode;
1321 lock->l_ast_data = data;
1322 lock->l_pid = cfs_curproc_pid();
1323 lock->l_ns_srv = !!ns_is_server(ns);
1325 lock->l_blocking_ast = cbs->lcs_blocking;
1326 lock->l_completion_ast = cbs->lcs_completion;
1327 lock->l_glimpse_ast = cbs->lcs_glimpse;
1328 lock->l_weigh_ast = cbs->lcs_weigh;
1331 lock->l_tree_node = NULL;
1332 /* if this is the extent lock, allocate the interval tree node */
1333 if (type == LDLM_EXTENT) {
1334 if (ldlm_interval_alloc(lock) == NULL)
1339 lock->l_lvb_len = lvb_len;
1340 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1341 if (lock->l_lvb_data == NULL)
1345 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
1351 ldlm_lock_destroy(lock);
1352 LDLM_LOCK_RELEASE(lock);
1356 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1357 struct ldlm_lock **lockp,
1358 void *cookie, int *flags)
1360 struct ldlm_lock *lock = *lockp;
1361 struct ldlm_resource *res = lock->l_resource;
1362 int local = ns_is_client(ldlm_res_to_ns(res));
1363 #ifdef HAVE_SERVER_SUPPORT
1364 ldlm_processing_policy policy;
1366 ldlm_error_t rc = ELDLM_OK;
1367 struct ldlm_interval *node = NULL;
1370 lock->l_last_activity = cfs_time_current_sec();
1371 /* policies are not executed on the client or during replay */
1372 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1373 && !local && ns->ns_policy) {
1374 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1376 if (rc == ELDLM_LOCK_REPLACED) {
1377 /* The lock that was returned has already been granted,
1378 * and placed into lockp. If it's not the same as the
1379 * one we passed in, then destroy the old one and our
1380 * work here is done. */
1381 if (lock != *lockp) {
1382 ldlm_lock_destroy(lock);
1383 LDLM_LOCK_RELEASE(lock);
1385 *flags |= LDLM_FL_LOCK_CHANGED;
1387 } else if (rc != ELDLM_OK ||
1388 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1389 ldlm_lock_destroy(lock);
1394 /* For a replaying lock, it might be already in granted list. So
1395 * unlinking the lock will cause the interval node to be freed, we
1396 * have to allocate the interval node early otherwise we can't regrant
1397 * this lock in the future. - jay */
1398 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1399 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1401 lock_res_and_lock(lock);
1402 if (local && lock->l_req_mode == lock->l_granted_mode) {
1403 /* The server returned a blocked lock, but it was granted
1404 * before we got a chance to actually enqueue it. We don't
1405 * need to do anything else. */
1406 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1407 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1408 GOTO(out, ELDLM_OK);
1411 ldlm_resource_unlink_lock(lock);
1412 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1414 ldlm_lock_destroy_nolock(lock);
1415 GOTO(out, rc = -ENOMEM);
1418 CFS_INIT_LIST_HEAD(&node->li_group);
1419 ldlm_interval_attach(node, lock);
1423 /* Some flags from the enqueue want to make it into the AST, via the
1424 * lock's l_flags. */
1425 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1427 /* This distinction between local lock trees is very important; a client
1428 * namespace only has information about locks taken by that client, and
1429 * thus doesn't have enough information to decide for itself if it can
1430 * be granted (below). In this case, we do exactly what the server
1431 * tells us to do, as dictated by the 'flags'.
1433 * We do exactly the same thing during recovery, when the server is
1434 * more or less trusting the clients not to lie.
1436 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1437 * granted/converting queues. */
1439 if (*flags & LDLM_FL_BLOCK_CONV)
1440 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1441 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1442 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1444 ldlm_grant_lock(lock, NULL);
1445 GOTO(out, ELDLM_OK);
1446 #ifdef HAVE_SERVER_SUPPORT
1447 } else if (*flags & LDLM_FL_REPLAY) {
1448 if (*flags & LDLM_FL_BLOCK_CONV) {
1449 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1450 GOTO(out, ELDLM_OK);
1451 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1452 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1453 GOTO(out, ELDLM_OK);
1454 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1455 ldlm_grant_lock(lock, NULL);
1456 GOTO(out, ELDLM_OK);
1458 /* If no flags, fall through to normal enqueue path. */
1461 policy = ldlm_processing_policy_table[res->lr_type];
1462 policy(lock, flags, 1, &rc, NULL);
1466 CERROR("This is client-side-only module, cannot handle "
1467 "LDLM_NAMESPACE_SERVER resource type lock.\n");
1473 unlock_res_and_lock(lock);
1475 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1479 #ifdef HAVE_SERVER_SUPPORT
1480 /* Must be called with namespace taken: queue is waiting or converting. */
1481 int ldlm_reprocess_queue(struct ldlm_resource *res, cfs_list_t *queue,
1482 cfs_list_t *work_list)
1484 cfs_list_t *tmp, *pos;
1485 ldlm_processing_policy policy;
1487 int rc = LDLM_ITER_CONTINUE;
1491 check_res_locked(res);
1493 policy = ldlm_processing_policy_table[res->lr_type];
1496 cfs_list_for_each_safe(tmp, pos, queue) {
1497 struct ldlm_lock *pending;
1498 pending = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1500 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1503 rc = policy(pending, &flags, 0, &err, work_list);
1504 if (rc != LDLM_ITER_CONTINUE)
1513 ldlm_work_bl_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1515 struct ldlm_lock_desc d;
1516 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1521 /* nobody should touch l_bl_ast */
1522 lock_res_and_lock(lock);
1523 cfs_list_del_init(&lock->l_bl_ast);
1525 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1526 LASSERT(lock->l_bl_ast_run == 0);
1527 LASSERT(lock->l_blocking_lock);
1528 lock->l_bl_ast_run++;
1529 unlock_res_and_lock(lock);
1531 ldlm_lock2desc(lock->l_blocking_lock, &d);
1533 rc = lock->l_blocking_ast(lock, &d, (void *)arg,
1535 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1536 lock->l_blocking_lock = NULL;
1537 LDLM_LOCK_RELEASE(lock);
1543 ldlm_work_cp_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1545 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock, l_cp_ast);
1546 ldlm_completion_callback completion_callback;
1550 /* It's possible to receive a completion AST before we've set
1551 * the l_completion_ast pointer: either because the AST arrived
1552 * before the reply, or simply because there's a small race
1553 * window between receiving the reply and finishing the local
1554 * enqueue. (bug 842)
1556 * This can't happen with the blocking_ast, however, because we
1557 * will never call the local blocking_ast until we drop our
1558 * reader/writer reference, which we won't do until we get the
1559 * reply and finish enqueueing. */
1561 /* nobody should touch l_cp_ast */
1562 lock_res_and_lock(lock);
1563 cfs_list_del_init(&lock->l_cp_ast);
1564 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1565 /* save l_completion_ast since it can be changed by
1566 * mds_intent_policy(), see bug 14225 */
1567 completion_callback = lock->l_completion_ast;
1568 lock->l_flags &= ~LDLM_FL_CP_REQD;
1569 unlock_res_and_lock(lock);
1571 if (completion_callback != NULL)
1572 rc = completion_callback(lock, 0, (void *)arg);
1573 LDLM_LOCK_RELEASE(lock);
1579 ldlm_work_revoke_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1581 struct ldlm_lock_desc desc;
1582 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1587 cfs_list_del_init(&lock->l_rk_ast);
1589 /* the desc just pretend to exclusive */
1590 ldlm_lock2desc(lock, &desc);
1591 desc.l_req_mode = LCK_EX;
1592 desc.l_granted_mode = 0;
1594 rc = lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1595 LDLM_LOCK_RELEASE(lock);
1600 int ldlm_run_ast_work(struct ldlm_namespace *ns, cfs_list_t *rpc_list,
1601 ldlm_desc_ast_t ast_type)
1603 struct l_wait_info lwi = { 0 };
1604 struct ldlm_cb_set_arg *arg;
1605 cfs_list_t *tmp, *pos;
1606 int (*work_ast_lock)(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg);
1607 unsigned int max_ast_count;
1611 if (cfs_list_empty(rpc_list))
1618 cfs_atomic_set(&arg->restart, 0);
1619 cfs_atomic_set(&arg->rpcs, 0);
1620 cfs_atomic_set(&arg->refcount, 1);
1621 cfs_waitq_init(&arg->waitq);
1624 case LDLM_WORK_BL_AST:
1625 arg->type = LDLM_BL_CALLBACK;
1626 work_ast_lock = ldlm_work_bl_ast_lock;
1628 case LDLM_WORK_CP_AST:
1629 arg->type = LDLM_CP_CALLBACK;
1630 work_ast_lock = ldlm_work_cp_ast_lock;
1632 case LDLM_WORK_REVOKE_AST:
1633 arg->type = LDLM_BL_CALLBACK;
1634 work_ast_lock = ldlm_work_revoke_ast_lock;
1640 max_ast_count = ns->ns_max_parallel_ast ? : UINT_MAX;
1641 arg->threshold = max_ast_count;
1643 cfs_list_for_each_safe(tmp, pos, rpc_list) {
1644 (void)work_ast_lock(tmp, arg);
1645 if (cfs_atomic_read(&arg->rpcs) < max_ast_count)
1648 l_wait_event(arg->waitq,
1649 cfs_atomic_read(&arg->rpcs) < arg->threshold,
1654 l_wait_event(arg->waitq, cfs_atomic_read(&arg->rpcs) == 0, &lwi);
1656 rc = cfs_atomic_read(&arg->restart) ? -ERESTART : 0;
1661 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1663 ldlm_reprocess_all(res);
1664 return LDLM_ITER_CONTINUE;
1667 static int ldlm_reprocess_res(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1668 cfs_hlist_node_t *hnode, void *arg)
1670 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1673 rc = reprocess_one_queue(res, arg);
1675 return rc == LDLM_ITER_STOP;
1678 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1683 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1684 ldlm_reprocess_res, NULL);
1689 void ldlm_reprocess_all(struct ldlm_resource *res)
1691 CFS_LIST_HEAD(rpc_list);
1693 #ifdef HAVE_SERVER_SUPPORT
1696 /* Local lock trees don't get reprocessed. */
1697 if (ns_is_client(ldlm_res_to_ns(res))) {
1704 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1705 if (rc == LDLM_ITER_CONTINUE)
1706 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1709 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
1711 if (rc == -ERESTART) {
1712 LASSERT(cfs_list_empty(&rpc_list));
1717 if (!ns_is_client(ldlm_res_to_ns(res))) {
1718 CERROR("This is client-side-only module, cannot handle "
1719 "LDLM_NAMESPACE_SERVER resource type lock.\n");
1726 void ldlm_cancel_callback(struct ldlm_lock *lock)
1728 check_res_locked(lock->l_resource);
1729 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1730 lock->l_flags |= LDLM_FL_CANCEL;
1731 if (lock->l_blocking_ast) {
1732 // l_check_no_ns_lock(ns);
1733 unlock_res_and_lock(lock);
1734 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1736 lock_res_and_lock(lock);
1738 LDLM_DEBUG(lock, "no blocking ast");
1741 lock->l_flags |= LDLM_FL_BL_DONE;
1744 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1746 if (req->l_resource->lr_type != LDLM_PLAIN &&
1747 req->l_resource->lr_type != LDLM_IBITS)
1750 cfs_list_del_init(&req->l_sl_policy);
1751 cfs_list_del_init(&req->l_sl_mode);
1754 void ldlm_lock_cancel(struct ldlm_lock *lock)
1756 struct ldlm_resource *res;
1757 struct ldlm_namespace *ns;
1760 lock_res_and_lock(lock);
1762 res = lock->l_resource;
1763 ns = ldlm_res_to_ns(res);
1765 /* Please do not, no matter how tempting, remove this LBUG without
1766 * talking to me first. -phik */
1767 if (lock->l_readers || lock->l_writers) {
1768 LDLM_ERROR(lock, "lock still has references");
1772 ldlm_del_waiting_lock(lock);
1774 /* Releases cancel callback. */
1775 ldlm_cancel_callback(lock);
1777 /* Yes, second time, just in case it was added again while we were
1778 running with no res lock in ldlm_cancel_callback */
1779 ldlm_del_waiting_lock(lock);
1780 ldlm_resource_unlink_lock(lock);
1781 ldlm_lock_destroy_nolock(lock);
1783 if (lock->l_granted_mode == lock->l_req_mode)
1784 ldlm_pool_del(&ns->ns_pool, lock);
1786 /* Make sure we will not be called again for same lock what is possible
1787 * if not to zero out lock->l_granted_mode */
1788 lock->l_granted_mode = LCK_MINMODE;
1789 unlock_res_and_lock(lock);
1794 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1796 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1801 if (lock->l_ast_data == NULL)
1802 lock->l_ast_data = data;
1803 if (lock->l_ast_data == data)
1805 LDLM_LOCK_PUT(lock);
1809 EXPORT_SYMBOL(ldlm_lock_set_data);
1811 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1812 cfs_hlist_node_t *hnode, void *data)
1815 struct obd_export *exp = data;
1816 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
1817 struct ldlm_resource *res;
1819 res = ldlm_resource_getref(lock->l_resource);
1820 LDLM_LOCK_GET(lock);
1822 LDLM_DEBUG(lock, "export %p", exp);
1823 ldlm_res_lvbo_update(res, NULL, 1);
1824 ldlm_lock_cancel(lock);
1825 ldlm_reprocess_all(res);
1826 ldlm_resource_putref(res);
1827 LDLM_LOCK_RELEASE(lock);
1831 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1833 cfs_hash_for_each_empty(exp->exp_lock_hash,
1834 ldlm_cancel_locks_for_export_cb, exp);
1838 * Downgrade an exclusive lock.
1840 * A fast variant of ldlm_lock_convert for convertion of exclusive
1841 * locks. The convertion is always successful.
1843 * \param lock A lock to convert
1844 * \param new_mode new lock mode
1846 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1850 LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1851 LASSERT(new_mode == LCK_COS);
1853 lock_res_and_lock(lock);
1854 ldlm_resource_unlink_lock(lock);
1856 * Remove the lock from pool as it will be added again in
1857 * ldlm_grant_lock() called below.
1859 ldlm_pool_del(&ldlm_lock_to_ns(lock)->ns_pool, lock);
1861 lock->l_req_mode = new_mode;
1862 ldlm_grant_lock(lock, NULL);
1863 unlock_res_and_lock(lock);
1864 ldlm_reprocess_all(lock->l_resource);
1869 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1872 CFS_LIST_HEAD(rpc_list);
1873 struct ldlm_resource *res;
1874 struct ldlm_namespace *ns;
1877 struct sl_insert_point prev;
1878 struct ldlm_interval *node;
1881 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1882 *flags |= LDLM_FL_BLOCK_GRANTED;
1883 RETURN(lock->l_resource);
1886 /* I can't check the type of lock here because the bitlock of lock
1887 * is not held here, so do the allocation blindly. -jay */
1888 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1889 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1892 LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1893 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1895 lock_res_and_lock(lock);
1897 res = lock->l_resource;
1898 ns = ldlm_res_to_ns(res);
1900 old_mode = lock->l_req_mode;
1901 lock->l_req_mode = new_mode;
1902 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1903 /* remember the lock position where the lock might be
1904 * added back to the granted list later and also
1905 * remember the join mode for skiplist fixing. */
1906 prev.res_link = lock->l_res_link.prev;
1907 prev.mode_link = lock->l_sl_mode.prev;
1908 prev.policy_link = lock->l_sl_policy.prev;
1909 ldlm_resource_unlink_lock(lock);
1911 ldlm_resource_unlink_lock(lock);
1912 if (res->lr_type == LDLM_EXTENT) {
1913 /* FIXME: ugly code, I have to attach the lock to a
1914 * interval node again since perhaps it will be granted
1916 CFS_INIT_LIST_HEAD(&node->li_group);
1917 ldlm_interval_attach(node, lock);
1923 * Remove old lock from the pool before adding the lock with new
1924 * mode below in ->policy()
1926 ldlm_pool_del(&ns->ns_pool, lock);
1928 /* If this is a local resource, put it on the appropriate list. */
1929 if (ns_is_client(ldlm_res_to_ns(res))) {
1930 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1931 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1933 /* This should never happen, because of the way the
1934 * server handles conversions. */
1935 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1939 ldlm_grant_lock(lock, &rpc_list);
1941 /* FIXME: completion handling not with lr_lock held ! */
1942 if (lock->l_completion_ast)
1943 lock->l_completion_ast(lock, 0, NULL);
1945 #ifdef HAVE_SERVER_SUPPORT
1950 ldlm_processing_policy policy;
1951 policy = ldlm_processing_policy_table[res->lr_type];
1952 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1953 if (rc == LDLM_ITER_STOP) {
1954 lock->l_req_mode = old_mode;
1955 if (res->lr_type == LDLM_EXTENT)
1956 ldlm_extent_add_lock(res, lock);
1958 ldlm_granted_list_add_lock(lock, &prev);
1962 *flags |= LDLM_FL_BLOCK_GRANTED;
1968 CERROR("This is client-side-only module, cannot handle "
1969 "LDLM_NAMESPACE_SERVER resource type lock.\n");
1973 unlock_res_and_lock(lock);
1976 ldlm_run_ast_work(ns, &rpc_list, LDLM_WORK_CP_AST);
1978 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1982 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1984 struct ldlm_lock *lock;
1986 if (!((libcfs_debug | D_ERROR) & level))
1989 lock = ldlm_handle2lock(lockh);
1993 LDLM_DEBUG_LIMIT(level, lock, "###");
1995 LDLM_LOCK_PUT(lock);
1998 void _ldlm_lock_debug(struct ldlm_lock *lock,
1999 struct libcfs_debug_msg_data *msgdata,
2000 const char *fmt, ...)
2003 struct obd_export *exp = lock->l_export;
2004 struct ldlm_resource *resource = lock->l_resource;
2005 char *nid = "local";
2007 va_start(args, fmt);
2009 if (exp && exp->exp_connection) {
2010 nid = libcfs_nid2str(exp->exp_connection->c_peer.nid);
2011 } else if (exp && exp->exp_obd != NULL) {
2012 struct obd_import *imp = exp->exp_obd->u.cli.cl_import;
2013 nid = libcfs_nid2str(imp->imp_connection->c_peer.nid);
2016 if (resource == NULL) {
2017 libcfs_debug_vmsg2(msgdata, fmt, args,
2018 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2019 "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" nid: %s "
2020 "remote: "LPX64" expref: %d pid: %u timeout: %lu\n",
2022 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2023 lock->l_readers, lock->l_writers,
2024 ldlm_lockname[lock->l_granted_mode],
2025 ldlm_lockname[lock->l_req_mode],
2026 lock->l_flags, nid, lock->l_remote_handle.cookie,
2027 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2028 lock->l_pid, lock->l_callback_timeout);
2033 switch (resource->lr_type) {
2035 libcfs_debug_vmsg2(msgdata, fmt, args,
2036 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2037 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
2038 "] (req "LPU64"->"LPU64") flags: "LPX64" nid: %s remote:"
2039 " "LPX64" expref: %d pid: %u timeout %lu\n",
2040 ldlm_lock_to_ns_name(lock), lock,
2041 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2042 lock->l_readers, lock->l_writers,
2043 ldlm_lockname[lock->l_granted_mode],
2044 ldlm_lockname[lock->l_req_mode],
2045 resource->lr_name.name[0],
2046 resource->lr_name.name[1],
2047 cfs_atomic_read(&resource->lr_refcount),
2048 ldlm_typename[resource->lr_type],
2049 lock->l_policy_data.l_extent.start,
2050 lock->l_policy_data.l_extent.end,
2051 lock->l_req_extent.start, lock->l_req_extent.end,
2052 lock->l_flags, nid, lock->l_remote_handle.cookie,
2053 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2054 lock->l_pid, lock->l_callback_timeout);
2058 libcfs_debug_vmsg2(msgdata, fmt, args,
2059 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2060 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
2061 "["LPU64"->"LPU64"] flags: "LPX64" nid: %s remote: "LPX64
2062 " expref: %d pid: %u timeout: %lu\n",
2063 ldlm_lock_to_ns_name(lock), lock,
2064 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2065 lock->l_readers, lock->l_writers,
2066 ldlm_lockname[lock->l_granted_mode],
2067 ldlm_lockname[lock->l_req_mode],
2068 resource->lr_name.name[0],
2069 resource->lr_name.name[1],
2070 cfs_atomic_read(&resource->lr_refcount),
2071 ldlm_typename[resource->lr_type],
2072 lock->l_policy_data.l_flock.pid,
2073 lock->l_policy_data.l_flock.start,
2074 lock->l_policy_data.l_flock.end,
2075 lock->l_flags, nid, lock->l_remote_handle.cookie,
2076 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2077 lock->l_pid, lock->l_callback_timeout);
2081 libcfs_debug_vmsg2(msgdata, fmt, args,
2082 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2083 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
2084 "flags: "LPX64" nid: %s remote: "LPX64" expref: %d "
2085 "pid: %u timeout: %lu\n",
2086 ldlm_lock_to_ns_name(lock),
2087 lock, lock->l_handle.h_cookie,
2088 cfs_atomic_read (&lock->l_refc),
2089 lock->l_readers, lock->l_writers,
2090 ldlm_lockname[lock->l_granted_mode],
2091 ldlm_lockname[lock->l_req_mode],
2092 resource->lr_name.name[0],
2093 resource->lr_name.name[1],
2094 lock->l_policy_data.l_inodebits.bits,
2095 cfs_atomic_read(&resource->lr_refcount),
2096 ldlm_typename[resource->lr_type],
2097 lock->l_flags, nid, lock->l_remote_handle.cookie,
2098 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2099 lock->l_pid, lock->l_callback_timeout);
2103 libcfs_debug_vmsg2(msgdata, fmt, args,
2104 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2105 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2106 "nid: %s remote: "LPX64" expref: %d pid: %u timeout %lu"
2108 ldlm_lock_to_ns_name(lock),
2109 lock, lock->l_handle.h_cookie,
2110 cfs_atomic_read (&lock->l_refc),
2111 lock->l_readers, lock->l_writers,
2112 ldlm_lockname[lock->l_granted_mode],
2113 ldlm_lockname[lock->l_req_mode],
2114 resource->lr_name.name[0],
2115 resource->lr_name.name[1],
2116 cfs_atomic_read(&resource->lr_refcount),
2117 ldlm_typename[resource->lr_type],
2118 lock->l_flags, nid, lock->l_remote_handle.cookie,
2119 exp ? cfs_atomic_read(&exp->exp_refcount) : -99,
2120 lock->l_pid, lock->l_callback_timeout);
2125 EXPORT_SYMBOL(_ldlm_lock_debug);