1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/ldlm/ldlm_lock.c
41 * Author: Peter Braam <braam@clusterfs.com>
42 * Author: Phil Schwan <phil@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_LDLM
48 # include <libcfs/libcfs.h>
49 # include <linux/lustre_intent.h>
51 # include <liblustre.h>
54 #include <obd_class.h>
55 #include "ldlm_internal.h"
58 char *ldlm_lockname[] = {
70 char *ldlm_typename[] = {
77 static ldlm_policy_wire_to_local_t ldlm_policy_wire_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_wire_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 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(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;
114 convert = ldlm_policy_wire_to_local[type - LDLM_MIN_TYPE];
116 convert(wpolicy, lpolicy);
119 char *ldlm_it2str(int it)
126 case (IT_OPEN | IT_CREAT):
139 CERROR("Unknown intent %d\n", it);
144 extern cfs_mem_cache_t *ldlm_lock_slab;
146 static ldlm_processing_policy ldlm_processing_policy_table[] = {
147 [LDLM_PLAIN] ldlm_process_plain_lock,
148 [LDLM_EXTENT] ldlm_process_extent_lock,
150 [LDLM_FLOCK] ldlm_process_flock_lock,
152 [LDLM_IBITS] ldlm_process_inodebits_lock,
155 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
157 return ldlm_processing_policy_table[res->lr_type];
160 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
166 * REFCOUNTED LOCK OBJECTS
171 * Lock refcounts, during creation:
172 * - one special one for allocation, dec'd only once in destroy
173 * - one for being a lock that's in-use
174 * - one for the addref associated with a new lock
176 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
178 cfs_atomic_inc(&lock->l_refc);
182 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
184 LASSERT(size == sizeof(*lock));
185 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
188 void ldlm_lock_put(struct ldlm_lock *lock)
192 LASSERT(lock->l_resource != LP_POISON);
193 LASSERT(cfs_atomic_read(&lock->l_refc) > 0);
194 if (cfs_atomic_dec_and_test(&lock->l_refc)) {
195 struct ldlm_resource *res;
198 "final lock_put on destroyed lock, freeing it.");
200 res = lock->l_resource;
201 LASSERT(lock->l_destroyed);
202 LASSERT(cfs_list_empty(&lock->l_res_link));
203 LASSERT(cfs_list_empty(&lock->l_pending_chain));
205 lprocfs_counter_decr(ldlm_res_to_ns(res)->ns_stats,
207 lu_ref_del(&res->lr_reference, "lock", lock);
208 ldlm_resource_putref(res);
209 lock->l_resource = NULL;
210 if (lock->l_export) {
211 class_export_lock_put(lock->l_export, lock);
212 lock->l_export = NULL;
215 if (lock->l_lvb_data != NULL)
216 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
218 ldlm_interval_free(ldlm_interval_detach(lock));
219 lu_ref_fini(&lock->l_reference);
220 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
227 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
230 if (!cfs_list_empty(&lock->l_lru)) {
231 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
233 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
234 cfs_list_del_init(&lock->l_lru);
235 if (lock->l_flags & LDLM_FL_SKIPPED)
236 lock->l_flags &= ~LDLM_FL_SKIPPED;
237 LASSERT(ns->ns_nr_unused > 0);
244 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
246 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
250 if (lock->l_ns_srv) {
251 LASSERT(cfs_list_empty(&lock->l_lru));
255 cfs_spin_lock(&ns->ns_lock);
256 rc = ldlm_lock_remove_from_lru_nolock(lock);
257 cfs_spin_unlock(&ns->ns_lock);
262 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
264 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
266 lock->l_last_used = cfs_time_current();
267 LASSERT(cfs_list_empty(&lock->l_lru));
268 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
269 cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
270 LASSERT(ns->ns_nr_unused >= 0);
274 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
276 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
279 cfs_spin_lock(&ns->ns_lock);
280 ldlm_lock_add_to_lru_nolock(lock);
281 cfs_spin_unlock(&ns->ns_lock);
285 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
287 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
290 if (lock->l_ns_srv) {
291 LASSERT(cfs_list_empty(&lock->l_lru));
296 cfs_spin_lock(&ns->ns_lock);
297 if (!cfs_list_empty(&lock->l_lru)) {
298 ldlm_lock_remove_from_lru_nolock(lock);
299 ldlm_lock_add_to_lru_nolock(lock);
301 cfs_spin_unlock(&ns->ns_lock);
305 /* This used to have a 'strict' flag, which recovery would use to mark an
306 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
307 * shall explain why it's gone: with the new hash table scheme, once you call
308 * ldlm_lock_destroy, you can never drop your final references on this lock.
309 * Because it's not in the hash table anymore. -phil */
310 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
314 if (lock->l_readers || lock->l_writers) {
315 LDLM_ERROR(lock, "lock still has references");
316 ldlm_lock_dump(D_ERROR, lock, 0);
320 if (!cfs_list_empty(&lock->l_res_link)) {
321 LDLM_ERROR(lock, "lock still on resource");
322 ldlm_lock_dump(D_ERROR, lock, 0);
326 if (lock->l_destroyed) {
327 LASSERT(cfs_list_empty(&lock->l_lru));
331 lock->l_destroyed = 1;
333 if (lock->l_export && lock->l_export->exp_lock_hash &&
334 !cfs_hlist_unhashed(&lock->l_exp_hash))
335 cfs_hash_del(lock->l_export->exp_lock_hash,
336 &lock->l_remote_handle, &lock->l_exp_hash);
338 ldlm_lock_remove_from_lru(lock);
339 class_handle_unhash(&lock->l_handle);
342 /* Wake anyone waiting for this lock */
343 /* FIXME: I should probably add yet another flag, instead of using
344 * l_export to only call this on clients */
346 class_export_put(lock->l_export);
347 lock->l_export = NULL;
348 if (lock->l_export && lock->l_completion_ast)
349 lock->l_completion_ast(lock, 0);
355 void ldlm_lock_destroy(struct ldlm_lock *lock)
359 lock_res_and_lock(lock);
360 first = ldlm_lock_destroy_internal(lock);
361 unlock_res_and_lock(lock);
363 /* drop reference from hashtable only for first destroy */
365 lu_ref_del(&lock->l_reference, "hash", lock);
366 LDLM_LOCK_RELEASE(lock);
371 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
375 first = ldlm_lock_destroy_internal(lock);
376 /* drop reference from hashtable only for first destroy */
378 lu_ref_del(&lock->l_reference, "hash", lock);
379 LDLM_LOCK_RELEASE(lock);
384 /* this is called by portals_handle2object with the handle lock taken */
385 static void lock_handle_addref(void *lock)
387 LDLM_LOCK_GET((struct ldlm_lock *)lock);
391 * usage: pass in a resource on which you have done ldlm_resource_get
392 * new lock will take over the refcount.
393 * returns: lock with refcount 2 - one for current caller and one for remote
395 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
397 struct ldlm_lock *lock;
400 if (resource == NULL)
403 OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
407 cfs_spin_lock_init(&lock->l_lock);
408 lock->l_resource = resource;
409 lu_ref_add(&resource->lr_reference, "lock", lock);
411 cfs_atomic_set(&lock->l_refc, 2);
412 CFS_INIT_LIST_HEAD(&lock->l_res_link);
413 CFS_INIT_LIST_HEAD(&lock->l_lru);
414 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
415 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
416 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
417 CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
418 cfs_waitq_init(&lock->l_waitq);
419 lock->l_blocking_lock = NULL;
420 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
421 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
422 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
424 lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
426 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
427 class_handle_hash(&lock->l_handle, lock_handle_addref);
429 lu_ref_init(&lock->l_reference);
430 lu_ref_add(&lock->l_reference, "hash", lock);
431 lock->l_callback_timeout = 0;
433 #if LUSTRE_TRACKS_LOCK_EXP_REFS
434 CFS_INIT_LIST_HEAD(&lock->l_exp_refs_link);
435 lock->l_exp_refs_nr = 0;
436 lock->l_exp_refs_target = NULL;
442 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
443 const struct ldlm_res_id *new_resid)
445 struct ldlm_resource *oldres = lock->l_resource;
446 struct ldlm_resource *newres;
450 LASSERT(ns_is_client(ns));
452 lock_res_and_lock(lock);
453 if (memcmp(new_resid, &lock->l_resource->lr_name,
454 sizeof(lock->l_resource->lr_name)) == 0) {
456 unlock_res_and_lock(lock);
460 LASSERT(new_resid->name[0] != 0);
462 /* This function assumes that the lock isn't on any lists */
463 LASSERT(cfs_list_empty(&lock->l_res_link));
465 type = oldres->lr_type;
466 unlock_res_and_lock(lock);
468 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
472 lu_ref_add(&newres->lr_reference, "lock", lock);
474 * To flip the lock from the old to the new resource, lock, oldres and
475 * newres have to be locked. Resource spin-locks are nested within
476 * lock->l_lock, and are taken in the memory address order to avoid
479 cfs_spin_lock(&lock->l_lock);
480 oldres = lock->l_resource;
481 if (oldres < newres) {
483 lock_res_nested(newres, LRT_NEW);
486 lock_res_nested(oldres, LRT_NEW);
488 LASSERT(memcmp(new_resid, &oldres->lr_name,
489 sizeof oldres->lr_name) != 0);
490 lock->l_resource = newres;
492 unlock_res_and_lock(lock);
494 /* ...and the flowers are still standing! */
495 lu_ref_del(&oldres->lr_reference, "lock", lock);
496 ldlm_resource_putref(oldres);
505 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
507 lockh->cookie = lock->l_handle.h_cookie;
510 /* if flags: atomically get the lock and set the flags.
511 * Return NULL if flag already set
514 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
517 struct ldlm_lock *lock;
522 lock = class_handle2object(handle->cookie);
526 /* It's unlikely but possible that someone marked the lock as
527 * destroyed after we did handle2object on it */
528 if (flags == 0 && !lock->l_destroyed) {
529 lu_ref_add(&lock->l_reference, "handle", cfs_current());
533 lock_res_and_lock(lock);
535 LASSERT(lock->l_resource != NULL);
537 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
538 if (unlikely(lock->l_destroyed)) {
539 unlock_res_and_lock(lock);
540 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
545 if (flags && (lock->l_flags & flags)) {
546 unlock_res_and_lock(lock);
552 lock->l_flags |= flags;
554 unlock_res_and_lock(lock);
558 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
560 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
561 /* INODEBITS_INTEROP: If the other side does not support
562 * inodebits, reply with a plain lock descriptor.
564 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
565 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
566 /* Make sure all the right bits are set in this lock we
567 are going to pass to client */
568 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
569 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
570 "Inappropriate inode lock bits during "
571 "conversion " LPU64 "\n",
572 lock->l_policy_data.l_inodebits.bits);
574 ldlm_res2desc(lock->l_resource, &desc->l_resource);
575 desc->l_resource.lr_type = LDLM_PLAIN;
577 /* Convert "new" lock mode to something old client can
579 if ((lock->l_req_mode == LCK_CR) ||
580 (lock->l_req_mode == LCK_CW))
581 desc->l_req_mode = LCK_PR;
583 desc->l_req_mode = lock->l_req_mode;
584 if ((lock->l_granted_mode == LCK_CR) ||
585 (lock->l_granted_mode == LCK_CW)) {
586 desc->l_granted_mode = LCK_PR;
588 /* We never grant PW/EX locks to clients */
589 LASSERT((lock->l_granted_mode != LCK_PW) &&
590 (lock->l_granted_mode != LCK_EX));
591 desc->l_granted_mode = lock->l_granted_mode;
594 /* We do not copy policy here, because there is no
595 policy for plain locks */
597 ldlm_res2desc(lock->l_resource, &desc->l_resource);
598 desc->l_req_mode = lock->l_req_mode;
599 desc->l_granted_mode = lock->l_granted_mode;
600 ldlm_convert_policy_to_wire(lock->l_resource->lr_type,
601 &lock->l_policy_data,
602 &desc->l_policy_data);
606 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
607 cfs_list_t *work_list)
609 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
610 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
611 lock->l_flags |= LDLM_FL_AST_SENT;
612 /* If the enqueuing client said so, tell the AST recipient to
613 * discard dirty data, rather than writing back. */
614 if (new->l_flags & LDLM_AST_DISCARD_DATA)
615 lock->l_flags |= LDLM_FL_DISCARD_DATA;
616 LASSERT(cfs_list_empty(&lock->l_bl_ast));
617 cfs_list_add(&lock->l_bl_ast, work_list);
619 LASSERT(lock->l_blocking_lock == NULL);
620 lock->l_blocking_lock = LDLM_LOCK_GET(new);
624 void ldlm_add_cp_work_item(struct ldlm_lock *lock, cfs_list_t *work_list)
626 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
627 lock->l_flags |= LDLM_FL_CP_REQD;
628 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
629 LASSERT(cfs_list_empty(&lock->l_cp_ast));
630 cfs_list_add(&lock->l_cp_ast, work_list);
635 /* must be called with lr_lock held */
636 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
637 cfs_list_t *work_list)
640 check_res_locked(lock->l_resource);
642 ldlm_add_bl_work_item(lock, new, work_list);
644 ldlm_add_cp_work_item(lock, work_list);
648 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
650 struct ldlm_lock *lock;
652 lock = ldlm_handle2lock(lockh);
653 LASSERT(lock != NULL);
654 ldlm_lock_addref_internal(lock, mode);
658 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
660 ldlm_lock_remove_from_lru(lock);
661 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
663 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
665 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
667 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
670 lu_ref_add_atomic(&lock->l_reference, "user", lock);
671 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
675 * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
678 * \retval 0 success, lock was addref-ed
680 * \retval -EAGAIN lock is being canceled.
682 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
684 struct ldlm_lock *lock;
688 lock = ldlm_handle2lock(lockh);
690 lock_res_and_lock(lock);
691 if (lock->l_readers != 0 || lock->l_writers != 0 ||
692 !(lock->l_flags & LDLM_FL_CBPENDING)) {
693 ldlm_lock_addref_internal_nolock(lock, mode);
696 unlock_res_and_lock(lock);
702 /* only called for local locks */
703 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
705 lock_res_and_lock(lock);
706 ldlm_lock_addref_internal_nolock(lock, mode);
707 unlock_res_and_lock(lock);
710 /* only called in ldlm_flock_destroy and for local locks.
711 * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
712 * * ldlm_lock_remove_from_lru() does nothing, it is safe
713 * * for ldlm_flock_destroy usage by dropping some code */
714 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
716 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
717 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
718 LASSERT(lock->l_readers > 0);
719 lu_ref_del(&lock->l_reference, "reader", lock);
722 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
723 LASSERT(lock->l_writers > 0);
724 lu_ref_del(&lock->l_reference, "writer", lock);
728 lu_ref_del(&lock->l_reference, "user", lock);
729 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
732 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
734 struct ldlm_namespace *ns;
737 lock_res_and_lock(lock);
739 ns = ldlm_lock_to_ns(lock);
741 ldlm_lock_decref_internal_nolock(lock, mode);
743 if (lock->l_flags & LDLM_FL_LOCAL &&
744 !lock->l_readers && !lock->l_writers) {
745 /* If this is a local lock on a server namespace and this was
746 * the last reference, cancel the lock. */
747 CDEBUG(D_INFO, "forcing cancel of local lock\n");
748 lock->l_flags |= LDLM_FL_CBPENDING;
751 if (!lock->l_readers && !lock->l_writers &&
752 (lock->l_flags & LDLM_FL_CBPENDING)) {
753 /* If we received a blocked AST and this was the last reference,
754 * run the callback. */
755 if (lock->l_ns_srv && lock->l_export)
756 CERROR("FL_CBPENDING set on non-local lock--just a "
759 LDLM_DEBUG(lock, "final decref done on cbpending lock");
761 LDLM_LOCK_GET(lock); /* dropped by bl thread */
762 ldlm_lock_remove_from_lru(lock);
763 unlock_res_and_lock(lock);
765 if (lock->l_flags & LDLM_FL_FAIL_LOC)
766 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
768 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
769 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
770 ldlm_handle_bl_callback(ns, NULL, lock);
771 } else if (ns_is_client(ns) &&
772 !lock->l_readers && !lock->l_writers &&
773 !(lock->l_flags & LDLM_FL_NO_LRU) &&
774 !(lock->l_flags & LDLM_FL_BL_AST)) {
776 LDLM_DEBUG(lock, "add lock into lru list");
778 /* If this is a client-side namespace and this was the last
779 * reference, put it on the LRU. */
780 ldlm_lock_add_to_lru(lock);
781 unlock_res_and_lock(lock);
783 if (lock->l_flags & LDLM_FL_FAIL_LOC)
784 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
786 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
787 * are not supported by the server, otherwise, it is done on
789 if (!exp_connect_cancelset(lock->l_conn_export) &&
790 !ns_connect_lru_resize(ns))
791 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
793 LDLM_DEBUG(lock, "do not add lock into lru list");
794 unlock_res_and_lock(lock);
800 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
802 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
803 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
804 ldlm_lock_decref_internal(lock, mode);
808 /* This will drop a lock reference and mark it for destruction, but will not
809 * necessarily cancel the lock before returning. */
810 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
812 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
815 LASSERT(lock != NULL);
817 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
818 lock_res_and_lock(lock);
819 lock->l_flags |= LDLM_FL_CBPENDING;
820 unlock_res_and_lock(lock);
821 ldlm_lock_decref_internal(lock, mode);
825 struct sl_insert_point {
826 cfs_list_t *res_link;
827 cfs_list_t *mode_link;
828 cfs_list_t *policy_link;
832 * search_granted_lock
835 * Finds a position to insert the new lock.
837 * queue [input]: the granted list where search acts on;
838 * req [input]: the lock whose position to be located;
839 * prev [output]: positions within 3 lists to insert @req to
843 * - ldlm_grant_lock_with_skiplist
845 static void search_granted_lock(cfs_list_t *queue,
846 struct ldlm_lock *req,
847 struct sl_insert_point *prev)
850 struct ldlm_lock *lock, *mode_end, *policy_end;
853 cfs_list_for_each(tmp, queue) {
854 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
856 mode_end = cfs_list_entry(lock->l_sl_mode.prev,
857 struct ldlm_lock, l_sl_mode);
859 if (lock->l_req_mode != req->l_req_mode) {
860 /* jump to last lock of mode group */
861 tmp = &mode_end->l_res_link;
865 /* suitable mode group is found */
866 if (lock->l_resource->lr_type == LDLM_PLAIN) {
867 /* insert point is last lock of the mode group */
868 prev->res_link = &mode_end->l_res_link;
869 prev->mode_link = &mode_end->l_sl_mode;
870 prev->policy_link = &req->l_sl_policy;
873 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
876 cfs_list_entry(lock->l_sl_policy.prev,
880 if (lock->l_policy_data.l_inodebits.bits ==
881 req->l_policy_data.l_inodebits.bits) {
882 /* insert point is last lock of
883 * the policy group */
885 &policy_end->l_res_link;
887 &policy_end->l_sl_mode;
889 &policy_end->l_sl_policy;
894 if (policy_end == mode_end)
895 /* done with mode group */
898 /* go to next policy group within mode group */
899 tmp = policy_end->l_res_link.next;
900 lock = cfs_list_entry(tmp, struct ldlm_lock,
902 } /* loop over policy groups within the mode group */
904 /* insert point is last lock of the mode group,
905 * new policy group is started */
906 prev->res_link = &mode_end->l_res_link;
907 prev->mode_link = &mode_end->l_sl_mode;
908 prev->policy_link = &req->l_sl_policy;
912 LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
917 /* insert point is last lock on the queue,
918 * new mode group and new policy group are started */
919 prev->res_link = queue->prev;
920 prev->mode_link = &req->l_sl_mode;
921 prev->policy_link = &req->l_sl_policy;
926 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
927 struct sl_insert_point *prev)
929 struct ldlm_resource *res = lock->l_resource;
932 check_res_locked(res);
934 ldlm_resource_dump(D_INFO, res);
935 CDEBUG(D_OTHER, "About to add this lock:\n");
936 ldlm_lock_dump(D_OTHER, lock, 0);
938 if (lock->l_destroyed) {
939 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
943 LASSERT(cfs_list_empty(&lock->l_res_link));
944 LASSERT(cfs_list_empty(&lock->l_sl_mode));
945 LASSERT(cfs_list_empty(&lock->l_sl_policy));
947 cfs_list_add(&lock->l_res_link, prev->res_link);
948 cfs_list_add(&lock->l_sl_mode, prev->mode_link);
949 cfs_list_add(&lock->l_sl_policy, prev->policy_link);
954 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
956 struct sl_insert_point prev;
959 LASSERT(lock->l_req_mode == lock->l_granted_mode);
961 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
962 ldlm_granted_list_add_lock(lock, &prev);
967 * - ldlm_lock_enqueue
968 * - ldlm_reprocess_queue
969 * - ldlm_lock_convert
971 * must be called with lr_lock held
973 void ldlm_grant_lock(struct ldlm_lock *lock, cfs_list_t *work_list)
975 struct ldlm_resource *res = lock->l_resource;
978 check_res_locked(res);
980 lock->l_granted_mode = lock->l_req_mode;
981 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
982 ldlm_grant_lock_with_skiplist(lock);
983 else if (res->lr_type == LDLM_EXTENT)
984 ldlm_extent_add_lock(res, lock);
986 ldlm_resource_add_lock(res, &res->lr_granted, lock);
988 if (lock->l_granted_mode < res->lr_most_restr)
989 res->lr_most_restr = lock->l_granted_mode;
991 if (work_list && lock->l_completion_ast != NULL)
992 ldlm_add_ast_work_item(lock, NULL, work_list);
994 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
998 /* returns a referenced lock or NULL. See the flag descriptions below, in the
999 * comment above ldlm_lock_match */
1000 static struct ldlm_lock *search_queue(cfs_list_t *queue,
1002 ldlm_policy_data_t *policy,
1003 struct ldlm_lock *old_lock,
1004 int flags, int unref)
1006 struct ldlm_lock *lock;
1009 cfs_list_for_each(tmp, queue) {
1012 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1014 if (lock == old_lock)
1017 /* llite sometimes wants to match locks that will be
1018 * canceled when their users drop, but we allow it to match
1019 * if it passes in CBPENDING and the lock still has users.
1020 * this is generally only going to be used by children
1021 * whose parents already hold a lock so forward progress
1022 * can still happen. */
1023 if (lock->l_flags & LDLM_FL_CBPENDING &&
1024 !(flags & LDLM_FL_CBPENDING))
1026 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
1027 lock->l_readers == 0 && lock->l_writers == 0)
1030 if (!(lock->l_req_mode & *mode))
1032 match = lock->l_req_mode;
1034 if (lock->l_resource->lr_type == LDLM_EXTENT &&
1035 (lock->l_policy_data.l_extent.start >
1036 policy->l_extent.start ||
1037 lock->l_policy_data.l_extent.end < policy->l_extent.end))
1040 if (unlikely(match == LCK_GROUP) &&
1041 lock->l_resource->lr_type == LDLM_EXTENT &&
1042 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
1045 /* We match if we have existing lock with same or wider set
1047 if (lock->l_resource->lr_type == LDLM_IBITS &&
1048 ((lock->l_policy_data.l_inodebits.bits &
1049 policy->l_inodebits.bits) !=
1050 policy->l_inodebits.bits))
1054 (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
1055 lock->l_fail_value != 0))
1058 if ((flags & LDLM_FL_LOCAL_ONLY) &&
1059 !(lock->l_flags & LDLM_FL_LOCAL))
1062 if (flags & LDLM_FL_TEST_LOCK) {
1063 LDLM_LOCK_GET(lock);
1064 ldlm_lock_touch_in_lru(lock);
1066 ldlm_lock_addref_internal_nolock(lock, match);
1075 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock, int rc)
1077 if (lock->l_fail_value == 0) {
1078 lock->l_fail_value = rc;
1079 cfs_waitq_signal(&lock->l_waitq);
1082 EXPORT_SYMBOL(ldlm_lock_fail_match_locked);
1084 void ldlm_lock_fail_match(struct ldlm_lock *lock, int rc)
1086 lock_res_and_lock(lock);
1087 ldlm_lock_fail_match_locked(lock, rc);
1088 unlock_res_and_lock(lock);
1090 EXPORT_SYMBOL(ldlm_lock_fail_match);
1092 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1094 lock->l_flags |= LDLM_FL_LVB_READY;
1095 cfs_waitq_signal(&lock->l_waitq);
1098 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1100 lock_res_and_lock(lock);
1101 ldlm_lock_allow_match_locked(lock);
1102 unlock_res_and_lock(lock);
1105 /* Can be called in two ways:
1107 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1108 * for a duplicate of.
1110 * Otherwise, all of the fields must be filled in, to match against.
1112 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1113 * server (ie, connh is NULL)
1114 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1115 * list will be considered
1116 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1117 * to be canceled can still be matched as long as they still have reader
1118 * or writer refernces
1119 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1120 * just tell us if we would have matched.
1122 * Returns 1 if it finds an already-existing lock that is compatible; in this
1123 * case, lockh is filled in with a addref()ed lock
1125 * we also check security context, if that failed we simply return 0 (to keep
1126 * caller code unchanged), the context failure will be discovered by caller
1129 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1130 const struct ldlm_res_id *res_id, ldlm_type_t type,
1131 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1132 struct lustre_handle *lockh, int unref)
1134 struct ldlm_resource *res;
1135 struct ldlm_lock *lock, *old_lock = NULL;
1140 old_lock = ldlm_handle2lock(lockh);
1143 ns = ldlm_lock_to_ns(old_lock);
1144 res_id = &old_lock->l_resource->lr_name;
1145 type = old_lock->l_resource->lr_type;
1146 mode = old_lock->l_req_mode;
1149 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1151 LASSERT(old_lock == NULL);
1155 LDLM_RESOURCE_ADDREF(res);
1158 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1162 if (flags & LDLM_FL_BLOCK_GRANTED)
1164 lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1168 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1176 LDLM_RESOURCE_DELREF(res);
1177 ldlm_resource_putref(res);
1180 ldlm_lock2handle(lock, lockh);
1181 if ((flags & LDLM_FL_LVB_READY) &&
1182 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1183 struct l_wait_info lwi;
1184 if (lock->l_completion_ast) {
1185 int err = lock->l_completion_ast(lock,
1186 LDLM_FL_WAIT_NOREPROC,
1189 if (flags & LDLM_FL_TEST_LOCK)
1190 LDLM_LOCK_RELEASE(lock);
1192 ldlm_lock_decref_internal(lock,
1199 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1200 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1202 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1203 l_wait_event(lock->l_waitq,
1204 lock->l_flags & LDLM_FL_LVB_READY ||
1205 lock->l_fail_value != 0,
1207 if (!(lock->l_flags & LDLM_FL_LVB_READY)) {
1208 if (flags & LDLM_FL_TEST_LOCK)
1209 LDLM_LOCK_RELEASE(lock);
1211 ldlm_lock_decref_internal(lock, mode);
1218 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1219 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1220 res_id->name[2] : policy->l_extent.start,
1221 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1222 res_id->name[3] : policy->l_extent.end);
1224 /* check user's security context */
1225 if (lock->l_conn_export &&
1226 sptlrpc_import_check_ctx(
1227 class_exp2cliimp(lock->l_conn_export))) {
1228 if (!(flags & LDLM_FL_TEST_LOCK))
1229 ldlm_lock_decref_internal(lock, mode);
1233 if (flags & LDLM_FL_TEST_LOCK)
1234 LDLM_LOCK_RELEASE(lock);
1236 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1237 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1238 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1239 type, mode, res_id->name[0], res_id->name[1],
1240 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1241 res_id->name[2] :policy->l_extent.start,
1242 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1243 res_id->name[3] : policy->l_extent.end);
1246 LDLM_LOCK_PUT(old_lock);
1248 return rc ? mode : 0;
1251 ldlm_mode_t ldlm_revalidate_lock_handle(struct lustre_handle *lockh,
1254 struct ldlm_lock *lock;
1255 ldlm_mode_t mode = 0;
1258 lock = ldlm_handle2lock(lockh);
1260 lock_res_and_lock(lock);
1261 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
1262 lock->l_fail_value != 0)
1265 if (lock->l_flags & LDLM_FL_CBPENDING &&
1266 lock->l_readers == 0 && lock->l_writers == 0)
1270 *bits = lock->l_policy_data.l_inodebits.bits;
1271 mode = lock->l_granted_mode;
1272 ldlm_lock_addref_internal_nolock(lock, mode);
1279 unlock_res_and_lock(lock);
1280 LDLM_LOCK_PUT(lock);
1284 EXPORT_SYMBOL(ldlm_revalidate_lock_handle);
1286 /* Returns a referenced lock */
1287 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1288 const struct ldlm_res_id *res_id,
1291 const struct ldlm_callback_suite *cbs,
1292 void *data, __u32 lvb_len)
1294 struct ldlm_lock *lock;
1295 struct ldlm_resource *res;
1298 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1302 lock = ldlm_lock_new(res);
1307 lock->l_req_mode = mode;
1308 lock->l_ast_data = data;
1309 lock->l_pid = cfs_curproc_pid();
1310 lock->l_ns_srv = ns_is_server(ns);
1312 lock->l_blocking_ast = cbs->lcs_blocking;
1313 lock->l_completion_ast = cbs->lcs_completion;
1314 lock->l_glimpse_ast = cbs->lcs_glimpse;
1315 lock->l_weigh_ast = cbs->lcs_weigh;
1318 lock->l_tree_node = NULL;
1319 /* if this is the extent lock, allocate the interval tree node */
1320 if (type == LDLM_EXTENT) {
1321 if (ldlm_interval_alloc(lock) == NULL)
1326 lock->l_lvb_len = lvb_len;
1327 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1328 if (lock->l_lvb_data == NULL)
1332 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
1338 ldlm_lock_destroy(lock);
1339 LDLM_LOCK_RELEASE(lock);
1343 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1344 struct ldlm_lock **lockp,
1345 void *cookie, int *flags)
1347 struct ldlm_lock *lock = *lockp;
1348 struct ldlm_resource *res = lock->l_resource;
1349 int local = ns_is_client(ldlm_res_to_ns(res));
1350 ldlm_processing_policy policy;
1351 ldlm_error_t rc = ELDLM_OK;
1352 struct ldlm_interval *node = NULL;
1355 lock->l_last_activity = cfs_time_current_sec();
1356 /* policies are not executed on the client or during replay */
1357 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1358 && !local && ns->ns_policy) {
1359 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1361 if (rc == ELDLM_LOCK_REPLACED) {
1362 /* The lock that was returned has already been granted,
1363 * and placed into lockp. If it's not the same as the
1364 * one we passed in, then destroy the old one and our
1365 * work here is done. */
1366 if (lock != *lockp) {
1367 ldlm_lock_destroy(lock);
1368 LDLM_LOCK_RELEASE(lock);
1370 *flags |= LDLM_FL_LOCK_CHANGED;
1372 } else if (rc != ELDLM_OK ||
1373 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1374 ldlm_lock_destroy(lock);
1379 /* For a replaying lock, it might be already in granted list. So
1380 * unlinking the lock will cause the interval node to be freed, we
1381 * have to allocate the interval node early otherwise we can't regrant
1382 * this lock in the future. - jay */
1383 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1384 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1386 lock_res_and_lock(lock);
1387 if (local && lock->l_req_mode == lock->l_granted_mode) {
1388 /* The server returned a blocked lock, but it was granted
1389 * before we got a chance to actually enqueue it. We don't
1390 * need to do anything else. */
1391 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1392 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1393 GOTO(out, ELDLM_OK);
1396 ldlm_resource_unlink_lock(lock);
1397 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1399 ldlm_lock_destroy_nolock(lock);
1400 GOTO(out, rc = -ENOMEM);
1403 CFS_INIT_LIST_HEAD(&node->li_group);
1404 ldlm_interval_attach(node, lock);
1408 /* Some flags from the enqueue want to make it into the AST, via the
1409 * lock's l_flags. */
1410 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1412 /* This distinction between local lock trees is very important; a client
1413 * namespace only has information about locks taken by that client, and
1414 * thus doesn't have enough information to decide for itself if it can
1415 * be granted (below). In this case, we do exactly what the server
1416 * tells us to do, as dictated by the 'flags'.
1418 * We do exactly the same thing during recovery, when the server is
1419 * more or less trusting the clients not to lie.
1421 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1422 * granted/converting queues. */
1424 if (*flags & LDLM_FL_BLOCK_CONV)
1425 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1426 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1427 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1429 ldlm_grant_lock(lock, NULL);
1430 GOTO(out, ELDLM_OK);
1431 } else if (*flags & LDLM_FL_REPLAY) {
1432 if (*flags & LDLM_FL_BLOCK_CONV) {
1433 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1434 GOTO(out, ELDLM_OK);
1435 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1436 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1437 GOTO(out, ELDLM_OK);
1438 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1439 ldlm_grant_lock(lock, NULL);
1440 GOTO(out, ELDLM_OK);
1442 /* If no flags, fall through to normal enqueue path. */
1445 policy = ldlm_processing_policy_table[res->lr_type];
1446 policy(lock, flags, 1, &rc, NULL);
1449 unlock_res_and_lock(lock);
1451 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1455 /* Must be called with namespace taken: queue is waiting or converting. */
1456 int ldlm_reprocess_queue(struct ldlm_resource *res, cfs_list_t *queue,
1457 cfs_list_t *work_list)
1459 cfs_list_t *tmp, *pos;
1460 ldlm_processing_policy policy;
1462 int rc = LDLM_ITER_CONTINUE;
1466 check_res_locked(res);
1468 policy = ldlm_processing_policy_table[res->lr_type];
1471 cfs_list_for_each_safe(tmp, pos, queue) {
1472 struct ldlm_lock *pending;
1473 pending = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1475 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1478 rc = policy(pending, &flags, 0, &err, work_list);
1479 if (rc != LDLM_ITER_CONTINUE)
1487 ldlm_work_bl_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1489 struct ldlm_lock_desc d;
1490 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1495 /* nobody should touch l_bl_ast */
1496 lock_res_and_lock(lock);
1497 cfs_list_del_init(&lock->l_bl_ast);
1499 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1500 LASSERT(lock->l_bl_ast_run == 0);
1501 LASSERT(lock->l_blocking_lock);
1502 lock->l_bl_ast_run++;
1503 unlock_res_and_lock(lock);
1505 ldlm_lock2desc(lock->l_blocking_lock, &d);
1507 rc = lock->l_blocking_ast(lock, &d, (void *)arg,
1509 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1510 lock->l_blocking_lock = NULL;
1511 LDLM_LOCK_RELEASE(lock);
1517 ldlm_work_cp_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1519 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock, l_cp_ast);
1520 ldlm_completion_callback completion_callback;
1524 /* It's possible to receive a completion AST before we've set
1525 * the l_completion_ast pointer: either because the AST arrived
1526 * before the reply, or simply because there's a small race
1527 * window between receiving the reply and finishing the local
1528 * enqueue. (bug 842)
1530 * This can't happen with the blocking_ast, however, because we
1531 * will never call the local blocking_ast until we drop our
1532 * reader/writer reference, which we won't do until we get the
1533 * reply and finish enqueueing. */
1535 /* nobody should touch l_cp_ast */
1536 lock_res_and_lock(lock);
1537 cfs_list_del_init(&lock->l_cp_ast);
1538 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1539 /* save l_completion_ast since it can be changed by
1540 * mds_intent_policy(), see bug 14225 */
1541 completion_callback = lock->l_completion_ast;
1542 lock->l_flags &= ~LDLM_FL_CP_REQD;
1543 unlock_res_and_lock(lock);
1545 if (completion_callback != NULL)
1546 rc = completion_callback(lock, 0, (void *)arg);
1547 LDLM_LOCK_RELEASE(lock);
1553 ldlm_work_revoke_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1555 struct ldlm_lock_desc desc;
1556 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1561 cfs_list_del_init(&lock->l_rk_ast);
1563 /* the desc just pretend to exclusive */
1564 ldlm_lock2desc(lock, &desc);
1565 desc.l_req_mode = LCK_EX;
1566 desc.l_granted_mode = 0;
1568 rc = lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1569 LDLM_LOCK_RELEASE(lock);
1574 int ldlm_run_ast_work(struct ldlm_namespace *ns, cfs_list_t *rpc_list,
1575 ldlm_desc_ast_t ast_type)
1577 struct ldlm_cb_set_arg arg = { 0 };
1578 struct l_wait_info lwi = { 0 };
1579 cfs_list_t *tmp, *pos;
1580 int (*work_ast_lock)(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg);
1581 unsigned int max_ast_count;
1584 if (cfs_list_empty(rpc_list))
1587 cfs_atomic_set(&arg.restart, 0);
1588 cfs_atomic_set(&arg.rpcs, 0);
1589 cfs_waitq_init(&arg.waitq);
1592 case LDLM_WORK_BL_AST:
1593 arg.type = LDLM_BL_CALLBACK;
1594 work_ast_lock = ldlm_work_bl_ast_lock;
1596 case LDLM_WORK_CP_AST:
1597 arg.type = LDLM_CP_CALLBACK;
1598 work_ast_lock = ldlm_work_cp_ast_lock;
1600 case LDLM_WORK_REVOKE_AST:
1601 arg.type = LDLM_BL_CALLBACK;
1602 work_ast_lock = ldlm_work_revoke_ast_lock;
1608 max_ast_count = ns->ns_max_parallel_ast ? : UINT_MAX;
1609 arg.threshold = max_ast_count;
1611 cfs_list_for_each_safe(tmp, pos, rpc_list) {
1612 (void)work_ast_lock(tmp, &arg);
1613 if (cfs_atomic_read(&arg.rpcs) < max_ast_count)
1616 l_wait_event(arg.waitq,
1617 cfs_atomic_read(&arg.rpcs) < arg.threshold,
1622 l_wait_event(arg.waitq, cfs_atomic_read(&arg.rpcs) == 0, &lwi);
1624 RETURN(cfs_atomic_read(&arg.restart) ? -ERESTART : 0);
1627 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1629 ldlm_reprocess_all(res);
1630 return LDLM_ITER_CONTINUE;
1633 static int ldlm_reprocess_res(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1634 cfs_hlist_node_t *hnode, void *arg)
1636 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1639 rc = reprocess_one_queue(res, arg);
1641 return rc == LDLM_ITER_STOP;
1644 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1649 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1650 ldlm_reprocess_res, NULL);
1655 void ldlm_reprocess_all(struct ldlm_resource *res)
1657 CFS_LIST_HEAD(rpc_list);
1661 /* Local lock trees don't get reprocessed. */
1662 if (ns_is_client(ldlm_res_to_ns(res))) {
1669 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1670 if (rc == LDLM_ITER_CONTINUE)
1671 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1674 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
1676 if (rc == -ERESTART) {
1677 LASSERT(cfs_list_empty(&rpc_list));
1683 void ldlm_cancel_callback(struct ldlm_lock *lock)
1685 check_res_locked(lock->l_resource);
1686 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1687 lock->l_flags |= LDLM_FL_CANCEL;
1688 if (lock->l_blocking_ast) {
1689 // l_check_no_ns_lock(ns);
1690 unlock_res_and_lock(lock);
1691 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1693 lock_res_and_lock(lock);
1695 LDLM_DEBUG(lock, "no blocking ast");
1698 lock->l_flags |= LDLM_FL_BL_DONE;
1701 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1703 if (req->l_resource->lr_type != LDLM_PLAIN &&
1704 req->l_resource->lr_type != LDLM_IBITS)
1707 cfs_list_del_init(&req->l_sl_policy);
1708 cfs_list_del_init(&req->l_sl_mode);
1711 void ldlm_lock_cancel(struct ldlm_lock *lock)
1713 struct ldlm_resource *res;
1714 struct ldlm_namespace *ns;
1717 lock_res_and_lock(lock);
1719 res = lock->l_resource;
1720 ns = ldlm_res_to_ns(res);
1722 /* Please do not, no matter how tempting, remove this LBUG without
1723 * talking to me first. -phik */
1724 if (lock->l_readers || lock->l_writers) {
1725 LDLM_ERROR(lock, "lock still has references");
1729 ldlm_del_waiting_lock(lock);
1731 /* Releases cancel callback. */
1732 ldlm_cancel_callback(lock);
1734 /* Yes, second time, just in case it was added again while we were
1735 running with no res lock in ldlm_cancel_callback */
1736 ldlm_del_waiting_lock(lock);
1737 ldlm_resource_unlink_lock(lock);
1738 ldlm_lock_destroy_nolock(lock);
1740 if (lock->l_granted_mode == lock->l_req_mode)
1741 ldlm_pool_del(&ns->ns_pool, lock);
1743 /* Make sure we will not be called again for same lock what is possible
1744 * if not to zero out lock->l_granted_mode */
1745 lock->l_granted_mode = LCK_MINMODE;
1746 unlock_res_and_lock(lock);
1751 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1753 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1758 if (lock->l_ast_data == NULL)
1759 lock->l_ast_data = data;
1760 if (lock->l_ast_data == data)
1762 LDLM_LOCK_PUT(lock);
1766 EXPORT_SYMBOL(ldlm_lock_set_data);
1768 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1769 cfs_hlist_node_t *hnode, void *data)
1772 struct obd_export *exp = data;
1773 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
1774 struct ldlm_resource *res;
1776 res = ldlm_resource_getref(lock->l_resource);
1777 LDLM_LOCK_GET(lock);
1779 LDLM_DEBUG(lock, "export %p", exp);
1780 ldlm_res_lvbo_update(res, NULL, 1);
1781 ldlm_lock_cancel(lock);
1782 ldlm_reprocess_all(res);
1783 ldlm_resource_putref(res);
1784 LDLM_LOCK_RELEASE(lock);
1788 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1790 cfs_hash_for_each_empty(exp->exp_lock_hash,
1791 ldlm_cancel_locks_for_export_cb, exp);
1795 * Downgrade an exclusive lock.
1797 * A fast variant of ldlm_lock_convert for convertion of exclusive
1798 * locks. The convertion is always successful.
1800 * \param lock A lock to convert
1801 * \param new_mode new lock mode
1803 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1807 LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1808 LASSERT(new_mode == LCK_COS);
1810 lock_res_and_lock(lock);
1811 ldlm_resource_unlink_lock(lock);
1813 * Remove the lock from pool as it will be added again in
1814 * ldlm_grant_lock() called below.
1816 ldlm_pool_del(&ldlm_lock_to_ns(lock)->ns_pool, lock);
1818 lock->l_req_mode = new_mode;
1819 ldlm_grant_lock(lock, NULL);
1820 unlock_res_and_lock(lock);
1821 ldlm_reprocess_all(lock->l_resource);
1826 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1829 CFS_LIST_HEAD(rpc_list);
1830 struct ldlm_resource *res;
1831 struct ldlm_namespace *ns;
1834 struct sl_insert_point prev;
1836 struct ldlm_interval *node;
1839 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1840 *flags |= LDLM_FL_BLOCK_GRANTED;
1841 RETURN(lock->l_resource);
1844 /* I can't check the type of lock here because the bitlock of lock
1845 * is not held here, so do the allocation blindly. -jay */
1846 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1847 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1850 LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1851 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1853 lock_res_and_lock(lock);
1855 res = lock->l_resource;
1856 ns = ldlm_res_to_ns(res);
1858 old_mode = lock->l_req_mode;
1859 lock->l_req_mode = new_mode;
1860 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1861 /* remember the lock position where the lock might be
1862 * added back to the granted list later and also
1863 * remember the join mode for skiplist fixing. */
1864 prev.res_link = lock->l_res_link.prev;
1865 prev.mode_link = lock->l_sl_mode.prev;
1866 prev.policy_link = lock->l_sl_policy.prev;
1867 ldlm_resource_unlink_lock(lock);
1869 ldlm_resource_unlink_lock(lock);
1870 if (res->lr_type == LDLM_EXTENT) {
1871 /* FIXME: ugly code, I have to attach the lock to a
1872 * interval node again since perhaps it will be granted
1874 CFS_INIT_LIST_HEAD(&node->li_group);
1875 ldlm_interval_attach(node, lock);
1881 * Remove old lock from the pool before adding the lock with new
1882 * mode below in ->policy()
1884 ldlm_pool_del(&ns->ns_pool, lock);
1886 /* If this is a local resource, put it on the appropriate list. */
1887 if (ns_is_client(ldlm_res_to_ns(res))) {
1888 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1889 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1891 /* This should never happen, because of the way the
1892 * server handles conversions. */
1893 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1897 ldlm_grant_lock(lock, &rpc_list);
1899 /* FIXME: completion handling not with lr_lock held ! */
1900 if (lock->l_completion_ast)
1901 lock->l_completion_ast(lock, 0, NULL);
1905 ldlm_processing_policy policy;
1906 policy = ldlm_processing_policy_table[res->lr_type];
1907 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1908 if (rc == LDLM_ITER_STOP) {
1909 lock->l_req_mode = old_mode;
1910 if (res->lr_type == LDLM_EXTENT)
1911 ldlm_extent_add_lock(res, lock);
1913 ldlm_granted_list_add_lock(lock, &prev);
1917 *flags |= LDLM_FL_BLOCK_GRANTED;
1921 unlock_res_and_lock(lock);
1924 ldlm_run_ast_work(ns, &rpc_list, LDLM_WORK_CP_AST);
1926 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1930 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1932 struct obd_device *obd = NULL;
1934 if (!((libcfs_debug | D_ERROR) & level))
1938 CDEBUG(level, " NULL LDLM lock\n");
1942 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1943 lock, lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1945 if (lock->l_conn_export != NULL)
1946 obd = lock->l_conn_export->exp_obd;
1947 if (lock->l_export && lock->l_export->exp_connection) {
1948 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1949 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1950 lock->l_remote_handle.cookie);
1951 } else if (obd == NULL) {
1952 CDEBUG(level, " Node: local\n");
1954 struct obd_import *imp = obd->u.cli.cl_import;
1955 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1956 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1957 lock->l_remote_handle.cookie);
1959 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1961 lock->l_resource->lr_name.name[0],
1962 lock->l_resource->lr_name.name[1],
1963 lock->l_resource->lr_name.name[2]);
1964 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1965 "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1966 ldlm_lockname[lock->l_granted_mode],
1967 cfs_atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1969 if (lock->l_resource->lr_type == LDLM_EXTENT)
1970 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1971 " (req "LPU64"-"LPU64")\n",
1972 lock->l_policy_data.l_extent.start,
1973 lock->l_policy_data.l_extent.end,
1974 lock->l_req_extent.start, lock->l_req_extent.end);
1975 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1976 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1977 lock->l_policy_data.l_flock.pid,
1978 lock->l_policy_data.l_flock.start,
1979 lock->l_policy_data.l_flock.end);
1980 else if (lock->l_resource->lr_type == LDLM_IBITS)
1981 CDEBUG(level, " Bits: "LPX64"\n",
1982 lock->l_policy_data.l_inodebits.bits);
1985 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1987 struct ldlm_lock *lock;
1989 if (!((libcfs_debug | D_ERROR) & level))
1992 lock = ldlm_handle2lock(lockh);
1996 ldlm_lock_dump(D_OTHER, lock, 0);
1998 LDLM_LOCK_PUT(lock);
2001 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
2002 struct libcfs_debug_msg_data *data, const char *fmt,
2006 cfs_debug_limit_state_t *cdls = data->msg_cdls;
2008 va_start(args, fmt);
2010 if (lock->l_resource == NULL) {
2011 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2012 data->msg_fn, data->msg_line, fmt, args,
2013 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2014 "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
2015 LPX64" expref: %d pid: %u timeout: %lu\n", lock,
2016 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2017 lock->l_readers, lock->l_writers,
2018 ldlm_lockname[lock->l_granted_mode],
2019 ldlm_lockname[lock->l_req_mode],
2020 lock->l_flags, lock->l_remote_handle.cookie,
2022 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2023 lock->l_pid, lock->l_callback_timeout);
2028 switch (lock->l_resource->lr_type) {
2030 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2031 data->msg_fn, data->msg_line, fmt, args,
2032 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2033 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
2034 "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
2035 " expref: %d pid: %u timeout %lu\n",
2036 ldlm_lock_to_ns_name(lock), lock,
2037 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2038 lock->l_readers, lock->l_writers,
2039 ldlm_lockname[lock->l_granted_mode],
2040 ldlm_lockname[lock->l_req_mode],
2041 lock->l_resource->lr_name.name[0],
2042 lock->l_resource->lr_name.name[1],
2043 cfs_atomic_read(&lock->l_resource->lr_refcount),
2044 ldlm_typename[lock->l_resource->lr_type],
2045 lock->l_policy_data.l_extent.start,
2046 lock->l_policy_data.l_extent.end,
2047 lock->l_req_extent.start, lock->l_req_extent.end,
2048 lock->l_flags, lock->l_remote_handle.cookie,
2050 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2051 lock->l_pid, lock->l_callback_timeout);
2055 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2056 data->msg_fn, data->msg_line, fmt, args,
2057 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2058 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
2059 "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
2060 " expref: %d pid: %u timeout: %lu\n",
2061 ldlm_lock_to_ns_name(lock), lock,
2062 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2063 lock->l_readers, lock->l_writers,
2064 ldlm_lockname[lock->l_granted_mode],
2065 ldlm_lockname[lock->l_req_mode],
2066 lock->l_resource->lr_name.name[0],
2067 lock->l_resource->lr_name.name[1],
2068 cfs_atomic_read(&lock->l_resource->lr_refcount),
2069 ldlm_typename[lock->l_resource->lr_type],
2070 lock->l_policy_data.l_flock.pid,
2071 lock->l_policy_data.l_flock.start,
2072 lock->l_policy_data.l_flock.end,
2073 lock->l_flags, lock->l_remote_handle.cookie,
2075 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2076 lock->l_pid, lock->l_callback_timeout);
2080 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2081 data->msg_fn, data->msg_line, 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" 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 lock->l_resource->lr_name.name[0],
2093 lock->l_resource->lr_name.name[1],
2094 lock->l_policy_data.l_inodebits.bits,
2095 cfs_atomic_read(&lock->l_resource->lr_refcount),
2096 ldlm_typename[lock->l_resource->lr_type],
2097 lock->l_flags, lock->l_remote_handle.cookie,
2099 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2100 lock->l_pid, lock->l_callback_timeout);
2104 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2105 data->msg_fn, data->msg_line, fmt, args,
2106 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2107 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2108 "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
2109 ldlm_lock_to_ns_name(lock),
2110 lock, lock->l_handle.h_cookie,
2111 cfs_atomic_read (&lock->l_refc),
2112 lock->l_readers, lock->l_writers,
2113 ldlm_lockname[lock->l_granted_mode],
2114 ldlm_lockname[lock->l_req_mode],
2115 lock->l_resource->lr_name.name[0],
2116 lock->l_resource->lr_name.name[1],
2117 cfs_atomic_read(&lock->l_resource->lr_refcount),
2118 ldlm_typename[lock->l_resource->lr_type],
2119 lock->l_flags, lock->l_remote_handle.cookie,
2121 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2122 lock->l_pid, lock->l_callback_timeout);
2127 EXPORT_SYMBOL(_ldlm_lock_debug);