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.
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 char *ldlm_it2str(int it)
81 case (IT_OPEN | IT_CREAT):
94 CERROR("Unknown intent %d\n", it);
99 extern cfs_mem_cache_t *ldlm_lock_slab;
101 static ldlm_processing_policy ldlm_processing_policy_table[] = {
102 [LDLM_PLAIN] ldlm_process_plain_lock,
103 [LDLM_EXTENT] ldlm_process_extent_lock,
105 [LDLM_FLOCK] ldlm_process_flock_lock,
107 [LDLM_IBITS] ldlm_process_inodebits_lock,
110 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
112 return ldlm_processing_policy_table[res->lr_type];
115 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
121 * REFCOUNTED LOCK OBJECTS
126 * Lock refcounts, during creation:
127 * - one special one for allocation, dec'd only once in destroy
128 * - one for being a lock that's in-use
129 * - one for the addref associated with a new lock
131 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
133 cfs_atomic_inc(&lock->l_refc);
137 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
139 LASSERT(size == sizeof(*lock));
140 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
143 void ldlm_lock_put(struct ldlm_lock *lock)
147 LASSERT(lock->l_resource != LP_POISON);
148 LASSERT(cfs_atomic_read(&lock->l_refc) > 0);
149 if (cfs_atomic_dec_and_test(&lock->l_refc)) {
150 struct ldlm_resource *res;
153 "final lock_put on destroyed lock, freeing it.");
155 res = lock->l_resource;
156 LASSERT(lock->l_destroyed);
157 LASSERT(cfs_list_empty(&lock->l_res_link));
158 LASSERT(cfs_list_empty(&lock->l_pending_chain));
160 lprocfs_counter_decr(ldlm_res_to_ns(res)->ns_stats,
162 lu_ref_del(&res->lr_reference, "lock", lock);
163 ldlm_resource_putref(res);
164 lock->l_resource = NULL;
165 if (lock->l_export) {
166 class_export_lock_put(lock->l_export, lock);
167 lock->l_export = NULL;
170 if (lock->l_lvb_data != NULL)
171 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
173 ldlm_interval_free(ldlm_interval_detach(lock));
174 lu_ref_fini(&lock->l_reference);
175 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
182 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
185 if (!cfs_list_empty(&lock->l_lru)) {
186 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
188 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
189 cfs_list_del_init(&lock->l_lru);
190 if (lock->l_flags & LDLM_FL_SKIPPED)
191 lock->l_flags &= ~LDLM_FL_SKIPPED;
192 LASSERT(ns->ns_nr_unused > 0);
199 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
201 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
205 if (lock->l_ns_srv) {
206 LASSERT(cfs_list_empty(&lock->l_lru));
210 cfs_spin_lock(&ns->ns_lock);
211 rc = ldlm_lock_remove_from_lru_nolock(lock);
212 cfs_spin_unlock(&ns->ns_lock);
217 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
219 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
221 lock->l_last_used = cfs_time_current();
222 LASSERT(cfs_list_empty(&lock->l_lru));
223 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
224 cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
225 LASSERT(ns->ns_nr_unused >= 0);
229 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
231 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
234 cfs_spin_lock(&ns->ns_lock);
235 ldlm_lock_add_to_lru_nolock(lock);
236 cfs_spin_unlock(&ns->ns_lock);
240 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
242 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
245 if (lock->l_ns_srv) {
246 LASSERT(cfs_list_empty(&lock->l_lru));
251 cfs_spin_lock(&ns->ns_lock);
252 if (!cfs_list_empty(&lock->l_lru)) {
253 ldlm_lock_remove_from_lru_nolock(lock);
254 ldlm_lock_add_to_lru_nolock(lock);
256 cfs_spin_unlock(&ns->ns_lock);
260 /* This used to have a 'strict' flag, which recovery would use to mark an
261 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
262 * shall explain why it's gone: with the new hash table scheme, once you call
263 * ldlm_lock_destroy, you can never drop your final references on this lock.
264 * Because it's not in the hash table anymore. -phil */
265 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
269 if (lock->l_readers || lock->l_writers) {
270 LDLM_ERROR(lock, "lock still has references");
271 ldlm_lock_dump(D_ERROR, lock, 0);
275 if (!cfs_list_empty(&lock->l_res_link)) {
276 LDLM_ERROR(lock, "lock still on resource");
277 ldlm_lock_dump(D_ERROR, lock, 0);
281 if (lock->l_destroyed) {
282 LASSERT(cfs_list_empty(&lock->l_lru));
286 lock->l_destroyed = 1;
288 if (lock->l_export && lock->l_export->exp_lock_hash &&
289 !cfs_hlist_unhashed(&lock->l_exp_hash))
290 cfs_hash_del(lock->l_export->exp_lock_hash,
291 &lock->l_remote_handle, &lock->l_exp_hash);
293 ldlm_lock_remove_from_lru(lock);
294 class_handle_unhash(&lock->l_handle);
297 /* Wake anyone waiting for this lock */
298 /* FIXME: I should probably add yet another flag, instead of using
299 * l_export to only call this on clients */
301 class_export_put(lock->l_export);
302 lock->l_export = NULL;
303 if (lock->l_export && lock->l_completion_ast)
304 lock->l_completion_ast(lock, 0);
310 void ldlm_lock_destroy(struct ldlm_lock *lock)
314 lock_res_and_lock(lock);
315 first = ldlm_lock_destroy_internal(lock);
316 unlock_res_and_lock(lock);
318 /* drop reference from hashtable only for first destroy */
320 lu_ref_del(&lock->l_reference, "hash", lock);
321 LDLM_LOCK_RELEASE(lock);
326 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
330 first = ldlm_lock_destroy_internal(lock);
331 /* drop reference from hashtable only for first destroy */
333 lu_ref_del(&lock->l_reference, "hash", lock);
334 LDLM_LOCK_RELEASE(lock);
339 /* this is called by portals_handle2object with the handle lock taken */
340 static void lock_handle_addref(void *lock)
342 LDLM_LOCK_GET((struct ldlm_lock *)lock);
346 * usage: pass in a resource on which you have done ldlm_resource_get
347 * new lock will take over the refcount.
348 * returns: lock with refcount 2 - one for current caller and one for remote
350 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
352 struct ldlm_lock *lock;
355 if (resource == NULL)
358 OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
362 cfs_spin_lock_init(&lock->l_lock);
363 lock->l_resource = resource;
364 lu_ref_add(&resource->lr_reference, "lock", lock);
366 cfs_atomic_set(&lock->l_refc, 2);
367 CFS_INIT_LIST_HEAD(&lock->l_res_link);
368 CFS_INIT_LIST_HEAD(&lock->l_lru);
369 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
370 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
371 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
372 CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
373 cfs_waitq_init(&lock->l_waitq);
374 lock->l_blocking_lock = NULL;
375 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
376 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
377 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
379 lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
381 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
382 class_handle_hash(&lock->l_handle, lock_handle_addref);
384 lu_ref_init(&lock->l_reference);
385 lu_ref_add(&lock->l_reference, "hash", lock);
386 lock->l_callback_timeout = 0;
388 #if LUSTRE_TRACKS_LOCK_EXP_REFS
389 CFS_INIT_LIST_HEAD(&lock->l_exp_refs_link);
390 lock->l_exp_refs_nr = 0;
391 lock->l_exp_refs_target = NULL;
397 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
398 const struct ldlm_res_id *new_resid)
400 struct ldlm_resource *oldres = lock->l_resource;
401 struct ldlm_resource *newres;
405 LASSERT(ns_is_client(ns));
407 lock_res_and_lock(lock);
408 if (memcmp(new_resid, &lock->l_resource->lr_name,
409 sizeof(lock->l_resource->lr_name)) == 0) {
411 unlock_res_and_lock(lock);
415 LASSERT(new_resid->name[0] != 0);
417 /* This function assumes that the lock isn't on any lists */
418 LASSERT(cfs_list_empty(&lock->l_res_link));
420 type = oldres->lr_type;
421 unlock_res_and_lock(lock);
423 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
427 lu_ref_add(&newres->lr_reference, "lock", lock);
429 * To flip the lock from the old to the new resource, lock, oldres and
430 * newres have to be locked. Resource spin-locks are nested within
431 * lock->l_lock, and are taken in the memory address order to avoid
434 cfs_spin_lock(&lock->l_lock);
435 oldres = lock->l_resource;
436 if (oldres < newres) {
438 lock_res_nested(newres, LRT_NEW);
441 lock_res_nested(oldres, LRT_NEW);
443 LASSERT(memcmp(new_resid, &oldres->lr_name,
444 sizeof oldres->lr_name) != 0);
445 lock->l_resource = newres;
447 unlock_res_and_lock(lock);
449 /* ...and the flowers are still standing! */
450 lu_ref_del(&oldres->lr_reference, "lock", lock);
451 ldlm_resource_putref(oldres);
460 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
462 lockh->cookie = lock->l_handle.h_cookie;
465 /* if flags: atomically get the lock and set the flags.
466 * Return NULL if flag already set
469 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
472 struct ldlm_lock *lock;
477 lock = class_handle2object(handle->cookie);
481 /* It's unlikely but possible that someone marked the lock as
482 * destroyed after we did handle2object on it */
483 if (flags == 0 && !lock->l_destroyed) {
484 lu_ref_add(&lock->l_reference, "handle", cfs_current());
488 lock_res_and_lock(lock);
490 LASSERT(lock->l_resource != NULL);
492 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
493 if (unlikely(lock->l_destroyed)) {
494 unlock_res_and_lock(lock);
495 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
500 if (flags && (lock->l_flags & flags)) {
501 unlock_res_and_lock(lock);
507 lock->l_flags |= flags;
509 unlock_res_and_lock(lock);
513 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
515 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
516 /* INODEBITS_INTEROP: If the other side does not support
517 * inodebits, reply with a plain lock descriptor.
519 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
520 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
521 /* Make sure all the right bits are set in this lock we
522 are going to pass to client */
523 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
524 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
525 "Inappropriate inode lock bits during "
526 "conversion " LPU64 "\n",
527 lock->l_policy_data.l_inodebits.bits);
529 ldlm_res2desc(lock->l_resource, &desc->l_resource);
530 desc->l_resource.lr_type = LDLM_PLAIN;
532 /* Convert "new" lock mode to something old client can
534 if ((lock->l_req_mode == LCK_CR) ||
535 (lock->l_req_mode == LCK_CW))
536 desc->l_req_mode = LCK_PR;
538 desc->l_req_mode = lock->l_req_mode;
539 if ((lock->l_granted_mode == LCK_CR) ||
540 (lock->l_granted_mode == LCK_CW)) {
541 desc->l_granted_mode = LCK_PR;
543 /* We never grant PW/EX locks to clients */
544 LASSERT((lock->l_granted_mode != LCK_PW) &&
545 (lock->l_granted_mode != LCK_EX));
546 desc->l_granted_mode = lock->l_granted_mode;
549 /* We do not copy policy here, because there is no
550 policy for plain locks */
552 ldlm_res2desc(lock->l_resource, &desc->l_resource);
553 desc->l_req_mode = lock->l_req_mode;
554 desc->l_granted_mode = lock->l_granted_mode;
555 desc->l_policy_data = lock->l_policy_data;
559 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
560 cfs_list_t *work_list)
562 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
563 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
564 lock->l_flags |= LDLM_FL_AST_SENT;
565 /* If the enqueuing client said so, tell the AST recipient to
566 * discard dirty data, rather than writing back. */
567 if (new->l_flags & LDLM_AST_DISCARD_DATA)
568 lock->l_flags |= LDLM_FL_DISCARD_DATA;
569 LASSERT(cfs_list_empty(&lock->l_bl_ast));
570 cfs_list_add(&lock->l_bl_ast, work_list);
572 LASSERT(lock->l_blocking_lock == NULL);
573 lock->l_blocking_lock = LDLM_LOCK_GET(new);
577 void ldlm_add_cp_work_item(struct ldlm_lock *lock, cfs_list_t *work_list)
579 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
580 lock->l_flags |= LDLM_FL_CP_REQD;
581 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
582 LASSERT(cfs_list_empty(&lock->l_cp_ast));
583 cfs_list_add(&lock->l_cp_ast, work_list);
588 /* must be called with lr_lock held */
589 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
590 cfs_list_t *work_list)
593 check_res_locked(lock->l_resource);
595 ldlm_add_bl_work_item(lock, new, work_list);
597 ldlm_add_cp_work_item(lock, work_list);
601 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
603 struct ldlm_lock *lock;
605 lock = ldlm_handle2lock(lockh);
606 LASSERT(lock != NULL);
607 ldlm_lock_addref_internal(lock, mode);
611 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
613 ldlm_lock_remove_from_lru(lock);
614 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
616 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
618 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
620 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
623 lu_ref_add_atomic(&lock->l_reference, "user", lock);
624 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
628 * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
631 * \retval 0 success, lock was addref-ed
633 * \retval -EAGAIN lock is being canceled.
635 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
637 struct ldlm_lock *lock;
641 lock = ldlm_handle2lock(lockh);
643 lock_res_and_lock(lock);
644 if (lock->l_readers != 0 || lock->l_writers != 0 ||
645 !(lock->l_flags & LDLM_FL_CBPENDING)) {
646 ldlm_lock_addref_internal_nolock(lock, mode);
649 unlock_res_and_lock(lock);
655 /* only called for local locks */
656 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
658 lock_res_and_lock(lock);
659 ldlm_lock_addref_internal_nolock(lock, mode);
660 unlock_res_and_lock(lock);
663 /* only called in ldlm_flock_destroy and for local locks.
664 * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
665 * * ldlm_lock_remove_from_lru() does nothing, it is safe
666 * * for ldlm_flock_destroy usage by dropping some code */
667 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
669 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
670 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
671 LASSERT(lock->l_readers > 0);
672 lu_ref_del(&lock->l_reference, "reader", lock);
675 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
676 LASSERT(lock->l_writers > 0);
677 lu_ref_del(&lock->l_reference, "writer", lock);
681 lu_ref_del(&lock->l_reference, "user", lock);
682 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
685 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
687 struct ldlm_namespace *ns;
690 lock_res_and_lock(lock);
692 ns = ldlm_lock_to_ns(lock);
694 ldlm_lock_decref_internal_nolock(lock, mode);
696 if (lock->l_flags & LDLM_FL_LOCAL &&
697 !lock->l_readers && !lock->l_writers) {
698 /* If this is a local lock on a server namespace and this was
699 * the last reference, cancel the lock. */
700 CDEBUG(D_INFO, "forcing cancel of local lock\n");
701 lock->l_flags |= LDLM_FL_CBPENDING;
704 if (!lock->l_readers && !lock->l_writers &&
705 (lock->l_flags & LDLM_FL_CBPENDING)) {
706 /* If we received a blocked AST and this was the last reference,
707 * run the callback. */
708 if (lock->l_ns_srv && lock->l_export)
709 CERROR("FL_CBPENDING set on non-local lock--just a "
712 LDLM_DEBUG(lock, "final decref done on cbpending lock");
714 LDLM_LOCK_GET(lock); /* dropped by bl thread */
715 ldlm_lock_remove_from_lru(lock);
716 unlock_res_and_lock(lock);
718 if (lock->l_flags & LDLM_FL_FAIL_LOC)
719 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
721 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
722 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
723 ldlm_handle_bl_callback(ns, NULL, lock);
724 } else if (ns_is_client(ns) &&
725 !lock->l_readers && !lock->l_writers &&
726 !(lock->l_flags & LDLM_FL_BL_AST)) {
727 /* If this is a client-side namespace and this was the last
728 * reference, put it on the LRU. */
729 ldlm_lock_add_to_lru(lock);
730 unlock_res_and_lock(lock);
732 if (lock->l_flags & LDLM_FL_FAIL_LOC)
733 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
735 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
736 * are not supported by the server, otherwise, it is done on
738 if (!exp_connect_cancelset(lock->l_conn_export) &&
739 !ns_connect_lru_resize(ns))
740 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
742 unlock_res_and_lock(lock);
748 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
750 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
751 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
752 ldlm_lock_decref_internal(lock, mode);
756 /* This will drop a lock reference and mark it for destruction, but will not
757 * necessarily cancel the lock before returning. */
758 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
760 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
763 LASSERT(lock != NULL);
765 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
766 lock_res_and_lock(lock);
767 lock->l_flags |= LDLM_FL_CBPENDING;
768 unlock_res_and_lock(lock);
769 ldlm_lock_decref_internal(lock, mode);
773 struct sl_insert_point {
774 cfs_list_t *res_link;
775 cfs_list_t *mode_link;
776 cfs_list_t *policy_link;
780 * search_granted_lock
783 * Finds a position to insert the new lock.
785 * queue [input]: the granted list where search acts on;
786 * req [input]: the lock whose position to be located;
787 * prev [output]: positions within 3 lists to insert @req to
791 * - ldlm_grant_lock_with_skiplist
793 static void search_granted_lock(cfs_list_t *queue,
794 struct ldlm_lock *req,
795 struct sl_insert_point *prev)
798 struct ldlm_lock *lock, *mode_end, *policy_end;
801 cfs_list_for_each(tmp, queue) {
802 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
804 mode_end = cfs_list_entry(lock->l_sl_mode.prev,
805 struct ldlm_lock, l_sl_mode);
807 if (lock->l_req_mode != req->l_req_mode) {
808 /* jump to last lock of mode group */
809 tmp = &mode_end->l_res_link;
813 /* suitable mode group is found */
814 if (lock->l_resource->lr_type == LDLM_PLAIN) {
815 /* insert point is last lock of the mode group */
816 prev->res_link = &mode_end->l_res_link;
817 prev->mode_link = &mode_end->l_sl_mode;
818 prev->policy_link = &req->l_sl_policy;
821 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
824 cfs_list_entry(lock->l_sl_policy.prev,
828 if (lock->l_policy_data.l_inodebits.bits ==
829 req->l_policy_data.l_inodebits.bits) {
830 /* insert point is last lock of
831 * the policy group */
833 &policy_end->l_res_link;
835 &policy_end->l_sl_mode;
837 &policy_end->l_sl_policy;
842 if (policy_end == mode_end)
843 /* done with mode group */
846 /* go to next policy group within mode group */
847 tmp = policy_end->l_res_link.next;
848 lock = cfs_list_entry(tmp, struct ldlm_lock,
850 } /* loop over policy groups within the mode group */
852 /* insert point is last lock of the mode group,
853 * new policy group is started */
854 prev->res_link = &mode_end->l_res_link;
855 prev->mode_link = &mode_end->l_sl_mode;
856 prev->policy_link = &req->l_sl_policy;
860 LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
865 /* insert point is last lock on the queue,
866 * new mode group and new policy group are started */
867 prev->res_link = queue->prev;
868 prev->mode_link = &req->l_sl_mode;
869 prev->policy_link = &req->l_sl_policy;
874 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
875 struct sl_insert_point *prev)
877 struct ldlm_resource *res = lock->l_resource;
880 check_res_locked(res);
882 ldlm_resource_dump(D_INFO, res);
883 CDEBUG(D_OTHER, "About to add this lock:\n");
884 ldlm_lock_dump(D_OTHER, lock, 0);
886 if (lock->l_destroyed) {
887 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
891 LASSERT(cfs_list_empty(&lock->l_res_link));
892 LASSERT(cfs_list_empty(&lock->l_sl_mode));
893 LASSERT(cfs_list_empty(&lock->l_sl_policy));
895 cfs_list_add(&lock->l_res_link, prev->res_link);
896 cfs_list_add(&lock->l_sl_mode, prev->mode_link);
897 cfs_list_add(&lock->l_sl_policy, prev->policy_link);
902 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
904 struct sl_insert_point prev;
907 LASSERT(lock->l_req_mode == lock->l_granted_mode);
909 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
910 ldlm_granted_list_add_lock(lock, &prev);
915 * - ldlm_lock_enqueue
916 * - ldlm_reprocess_queue
917 * - ldlm_lock_convert
919 * must be called with lr_lock held
921 void ldlm_grant_lock(struct ldlm_lock *lock, cfs_list_t *work_list)
923 struct ldlm_resource *res = lock->l_resource;
926 check_res_locked(res);
928 lock->l_granted_mode = lock->l_req_mode;
929 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
930 ldlm_grant_lock_with_skiplist(lock);
931 else if (res->lr_type == LDLM_EXTENT)
932 ldlm_extent_add_lock(res, lock);
934 ldlm_resource_add_lock(res, &res->lr_granted, lock);
936 if (lock->l_granted_mode < res->lr_most_restr)
937 res->lr_most_restr = lock->l_granted_mode;
939 if (work_list && lock->l_completion_ast != NULL)
940 ldlm_add_ast_work_item(lock, NULL, work_list);
942 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
946 /* returns a referenced lock or NULL. See the flag descriptions below, in the
947 * comment above ldlm_lock_match */
948 static struct ldlm_lock *search_queue(cfs_list_t *queue,
950 ldlm_policy_data_t *policy,
951 struct ldlm_lock *old_lock,
952 int flags, int unref)
954 struct ldlm_lock *lock;
957 cfs_list_for_each(tmp, queue) {
960 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
962 if (lock == old_lock)
965 /* llite sometimes wants to match locks that will be
966 * canceled when their users drop, but we allow it to match
967 * if it passes in CBPENDING and the lock still has users.
968 * this is generally only going to be used by children
969 * whose parents already hold a lock so forward progress
970 * can still happen. */
971 if (lock->l_flags & LDLM_FL_CBPENDING &&
972 !(flags & LDLM_FL_CBPENDING))
974 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
975 lock->l_readers == 0 && lock->l_writers == 0)
978 if (!(lock->l_req_mode & *mode))
980 match = lock->l_req_mode;
982 if (lock->l_resource->lr_type == LDLM_EXTENT &&
983 (lock->l_policy_data.l_extent.start >
984 policy->l_extent.start ||
985 lock->l_policy_data.l_extent.end < policy->l_extent.end))
988 if (unlikely(match == LCK_GROUP) &&
989 lock->l_resource->lr_type == LDLM_EXTENT &&
990 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
993 /* We match if we have existing lock with same or wider set
995 if (lock->l_resource->lr_type == LDLM_IBITS &&
996 ((lock->l_policy_data.l_inodebits.bits &
997 policy->l_inodebits.bits) !=
998 policy->l_inodebits.bits))
1002 (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED)))
1005 if ((flags & LDLM_FL_LOCAL_ONLY) &&
1006 !(lock->l_flags & LDLM_FL_LOCAL))
1009 if (flags & LDLM_FL_TEST_LOCK) {
1010 LDLM_LOCK_GET(lock);
1011 ldlm_lock_touch_in_lru(lock);
1013 ldlm_lock_addref_internal_nolock(lock, match);
1022 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1024 lock->l_flags |= LDLM_FL_LVB_READY;
1025 cfs_waitq_signal(&lock->l_waitq);
1028 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1030 lock_res_and_lock(lock);
1031 ldlm_lock_allow_match_locked(lock);
1032 unlock_res_and_lock(lock);
1035 /* Can be called in two ways:
1037 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1038 * for a duplicate of.
1040 * Otherwise, all of the fields must be filled in, to match against.
1042 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1043 * server (ie, connh is NULL)
1044 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1045 * list will be considered
1046 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1047 * to be canceled can still be matched as long as they still have reader
1048 * or writer refernces
1049 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1050 * just tell us if we would have matched.
1052 * Returns 1 if it finds an already-existing lock that is compatible; in this
1053 * case, lockh is filled in with a addref()ed lock
1055 * we also check security context, if that failed we simply return 0 (to keep
1056 * caller code unchanged), the context failure will be discovered by caller
1059 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1060 const struct ldlm_res_id *res_id, ldlm_type_t type,
1061 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1062 struct lustre_handle *lockh, int unref)
1064 struct ldlm_resource *res;
1065 struct ldlm_lock *lock, *old_lock = NULL;
1070 old_lock = ldlm_handle2lock(lockh);
1073 ns = ldlm_lock_to_ns(old_lock);
1074 res_id = &old_lock->l_resource->lr_name;
1075 type = old_lock->l_resource->lr_type;
1076 mode = old_lock->l_req_mode;
1079 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1081 LASSERT(old_lock == NULL);
1085 LDLM_RESOURCE_ADDREF(res);
1088 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1092 if (flags & LDLM_FL_BLOCK_GRANTED)
1094 lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1098 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1106 LDLM_RESOURCE_DELREF(res);
1107 ldlm_resource_putref(res);
1110 ldlm_lock2handle(lock, lockh);
1111 if ((flags & LDLM_FL_LVB_READY) &&
1112 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1113 struct l_wait_info lwi;
1114 if (lock->l_completion_ast) {
1115 int err = lock->l_completion_ast(lock,
1116 LDLM_FL_WAIT_NOREPROC,
1119 if (flags & LDLM_FL_TEST_LOCK)
1120 LDLM_LOCK_RELEASE(lock);
1122 ldlm_lock_decref_internal(lock,
1129 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1130 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1132 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1133 l_wait_event(lock->l_waitq,
1134 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1139 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1140 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1141 res_id->name[2] : policy->l_extent.start,
1142 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1143 res_id->name[3] : policy->l_extent.end);
1145 /* check user's security context */
1146 if (lock->l_conn_export &&
1147 sptlrpc_import_check_ctx(
1148 class_exp2cliimp(lock->l_conn_export))) {
1149 if (!(flags & LDLM_FL_TEST_LOCK))
1150 ldlm_lock_decref_internal(lock, mode);
1154 if (flags & LDLM_FL_TEST_LOCK)
1155 LDLM_LOCK_RELEASE(lock);
1157 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1158 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1159 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1160 type, mode, res_id->name[0], res_id->name[1],
1161 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1162 res_id->name[2] :policy->l_extent.start,
1163 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1164 res_id->name[3] : policy->l_extent.end);
1167 LDLM_LOCK_PUT(old_lock);
1169 return rc ? mode : 0;
1172 /* Returns a referenced lock */
1173 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1174 const struct ldlm_res_id *res_id,
1177 const struct ldlm_callback_suite *cbs,
1178 void *data, __u32 lvb_len)
1180 struct ldlm_lock *lock;
1181 struct ldlm_resource *res;
1184 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1188 lock = ldlm_lock_new(res);
1193 lock->l_req_mode = mode;
1194 lock->l_ast_data = data;
1195 lock->l_pid = cfs_curproc_pid();
1196 lock->l_ns_srv = ns_is_server(ns);
1198 lock->l_blocking_ast = cbs->lcs_blocking;
1199 lock->l_completion_ast = cbs->lcs_completion;
1200 lock->l_glimpse_ast = cbs->lcs_glimpse;
1201 lock->l_weigh_ast = cbs->lcs_weigh;
1204 lock->l_tree_node = NULL;
1205 /* if this is the extent lock, allocate the interval tree node */
1206 if (type == LDLM_EXTENT) {
1207 if (ldlm_interval_alloc(lock) == NULL)
1212 lock->l_lvb_len = lvb_len;
1213 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1214 if (lock->l_lvb_data == NULL)
1218 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
1224 ldlm_lock_destroy(lock);
1225 LDLM_LOCK_RELEASE(lock);
1229 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1230 struct ldlm_lock **lockp,
1231 void *cookie, int *flags)
1233 struct ldlm_lock *lock = *lockp;
1234 struct ldlm_resource *res = lock->l_resource;
1235 int local = ns_is_client(ldlm_res_to_ns(res));
1236 ldlm_processing_policy policy;
1237 ldlm_error_t rc = ELDLM_OK;
1238 struct ldlm_interval *node = NULL;
1241 lock->l_last_activity = cfs_time_current_sec();
1242 /* policies are not executed on the client or during replay */
1243 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1244 && !local && ns->ns_policy) {
1245 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1247 if (rc == ELDLM_LOCK_REPLACED) {
1248 /* The lock that was returned has already been granted,
1249 * and placed into lockp. If it's not the same as the
1250 * one we passed in, then destroy the old one and our
1251 * work here is done. */
1252 if (lock != *lockp) {
1253 ldlm_lock_destroy(lock);
1254 LDLM_LOCK_RELEASE(lock);
1256 *flags |= LDLM_FL_LOCK_CHANGED;
1258 } else if (rc != ELDLM_OK ||
1259 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1260 ldlm_lock_destroy(lock);
1265 /* For a replaying lock, it might be already in granted list. So
1266 * unlinking the lock will cause the interval node to be freed, we
1267 * have to allocate the interval node early otherwise we can't regrant
1268 * this lock in the future. - jay */
1269 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1270 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1272 lock_res_and_lock(lock);
1273 if (local && lock->l_req_mode == lock->l_granted_mode) {
1274 /* The server returned a blocked lock, but it was granted
1275 * before we got a chance to actually enqueue it. We don't
1276 * need to do anything else. */
1277 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1278 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1279 GOTO(out, ELDLM_OK);
1282 ldlm_resource_unlink_lock(lock);
1283 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1285 ldlm_lock_destroy_nolock(lock);
1286 GOTO(out, rc = -ENOMEM);
1289 CFS_INIT_LIST_HEAD(&node->li_group);
1290 ldlm_interval_attach(node, lock);
1294 /* Some flags from the enqueue want to make it into the AST, via the
1295 * lock's l_flags. */
1296 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1298 /* This distinction between local lock trees is very important; a client
1299 * namespace only has information about locks taken by that client, and
1300 * thus doesn't have enough information to decide for itself if it can
1301 * be granted (below). In this case, we do exactly what the server
1302 * tells us to do, as dictated by the 'flags'.
1304 * We do exactly the same thing during recovery, when the server is
1305 * more or less trusting the clients not to lie.
1307 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1308 * granted/converting queues. */
1310 if (*flags & LDLM_FL_BLOCK_CONV)
1311 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1312 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1313 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1315 ldlm_grant_lock(lock, NULL);
1316 GOTO(out, ELDLM_OK);
1317 } else if (*flags & LDLM_FL_REPLAY) {
1318 if (*flags & LDLM_FL_BLOCK_CONV) {
1319 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1320 GOTO(out, ELDLM_OK);
1321 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1322 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1323 GOTO(out, ELDLM_OK);
1324 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1325 ldlm_grant_lock(lock, NULL);
1326 GOTO(out, ELDLM_OK);
1328 /* If no flags, fall through to normal enqueue path. */
1331 policy = ldlm_processing_policy_table[res->lr_type];
1332 policy(lock, flags, 1, &rc, NULL);
1335 unlock_res_and_lock(lock);
1337 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1341 /* Must be called with namespace taken: queue is waiting or converting. */
1342 int ldlm_reprocess_queue(struct ldlm_resource *res, cfs_list_t *queue,
1343 cfs_list_t *work_list)
1345 cfs_list_t *tmp, *pos;
1346 ldlm_processing_policy policy;
1348 int rc = LDLM_ITER_CONTINUE;
1352 check_res_locked(res);
1354 policy = ldlm_processing_policy_table[res->lr_type];
1357 cfs_list_for_each_safe(tmp, pos, queue) {
1358 struct ldlm_lock *pending;
1359 pending = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1361 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1364 rc = policy(pending, &flags, 0, &err, work_list);
1365 if (rc != LDLM_ITER_CONTINUE)
1372 /* Helper function for ldlm_run_ast_work().
1374 * Send an existing rpc set specified by @arg->set and then
1375 * destroy it. Create new one if @do_create flag is set. */
1377 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1381 ptlrpc_set_wait(arg->set);
1382 if (arg->type == LDLM_BL_CALLBACK)
1383 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1384 ptlrpc_set_destroy(arg->set);
1387 arg->set = ptlrpc_prep_set();
1393 ldlm_work_bl_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1395 struct ldlm_lock_desc d;
1396 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1400 /* nobody should touch l_bl_ast */
1401 lock_res_and_lock(lock);
1402 cfs_list_del_init(&lock->l_bl_ast);
1404 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1405 LASSERT(lock->l_bl_ast_run == 0);
1406 LASSERT(lock->l_blocking_lock);
1407 lock->l_bl_ast_run++;
1408 unlock_res_and_lock(lock);
1410 ldlm_lock2desc(lock->l_blocking_lock, &d);
1412 lock->l_blocking_ast(lock, &d, (void *)arg,
1414 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1415 lock->l_blocking_lock = NULL;
1416 LDLM_LOCK_RELEASE(lock);
1422 ldlm_work_cp_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1424 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock, l_cp_ast);
1425 ldlm_completion_callback completion_callback;
1429 /* It's possible to receive a completion AST before we've set
1430 * the l_completion_ast pointer: either because the AST arrived
1431 * before the reply, or simply because there's a small race
1432 * window between receiving the reply and finishing the local
1433 * enqueue. (bug 842)
1435 * This can't happen with the blocking_ast, however, because we
1436 * will never call the local blocking_ast until we drop our
1437 * reader/writer reference, which we won't do until we get the
1438 * reply and finish enqueueing. */
1440 /* nobody should touch l_cp_ast */
1441 lock_res_and_lock(lock);
1442 cfs_list_del_init(&lock->l_cp_ast);
1443 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1444 /* save l_completion_ast since it can be changed by
1445 * mds_intent_policy(), see bug 14225 */
1446 completion_callback = lock->l_completion_ast;
1447 lock->l_flags &= ~LDLM_FL_CP_REQD;
1448 unlock_res_and_lock(lock);
1450 if (completion_callback != NULL) {
1451 completion_callback(lock, 0, (void *)arg);
1454 LDLM_LOCK_RELEASE(lock);
1460 ldlm_work_revoke_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1462 struct ldlm_lock_desc desc;
1463 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1467 cfs_list_del_init(&lock->l_rk_ast);
1469 /* the desc just pretend to exclusive */
1470 ldlm_lock2desc(lock, &desc);
1471 desc.l_req_mode = LCK_EX;
1472 desc.l_granted_mode = 0;
1474 lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1475 LDLM_LOCK_RELEASE(lock);
1480 int ldlm_run_ast_work(cfs_list_t *rpc_list, ldlm_desc_ast_t ast_type)
1482 struct ldlm_cb_set_arg arg;
1483 cfs_list_t *tmp, *pos;
1484 int (*work_ast_lock)(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg);
1488 if (cfs_list_empty(rpc_list))
1491 arg.set = ptlrpc_prep_set();
1492 if (NULL == arg.set)
1494 cfs_atomic_set(&arg.restart, 0);
1496 case LDLM_WORK_BL_AST:
1497 arg.type = LDLM_BL_CALLBACK;
1498 work_ast_lock = ldlm_work_bl_ast_lock;
1500 case LDLM_WORK_CP_AST:
1501 arg.type = LDLM_CP_CALLBACK;
1502 work_ast_lock = ldlm_work_cp_ast_lock;
1504 case LDLM_WORK_REVOKE_AST:
1505 arg.type = LDLM_BL_CALLBACK;
1506 work_ast_lock = ldlm_work_revoke_ast_lock;
1513 cfs_list_for_each_safe(tmp, pos, rpc_list) {
1514 ast_count += work_ast_lock(tmp, &arg);
1516 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1517 * and create a new set for requests that remained in
1519 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1520 ldlm_send_and_maybe_create_set(&arg, 1);
1526 ldlm_send_and_maybe_create_set(&arg, 0);
1528 /* In case when number of ASTs is multiply of
1529 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1530 * @arg.set must be destroyed here, otherwise we get
1531 * write memory leaking. */
1532 ptlrpc_set_destroy(arg.set);
1534 RETURN(cfs_atomic_read(&arg.restart) ? -ERESTART : 0);
1537 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1539 ldlm_reprocess_all(res);
1540 return LDLM_ITER_CONTINUE;
1543 static int ldlm_reprocess_res(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1544 cfs_hlist_node_t *hnode, void *arg)
1546 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1549 rc = reprocess_one_queue(res, arg);
1551 return rc == LDLM_ITER_STOP;
1554 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1559 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1560 ldlm_reprocess_res, NULL);
1565 void ldlm_reprocess_all(struct ldlm_resource *res)
1567 CFS_LIST_HEAD(rpc_list);
1571 /* Local lock trees don't get reprocessed. */
1572 if (ns_is_client(ldlm_res_to_ns(res))) {
1579 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1580 if (rc == LDLM_ITER_CONTINUE)
1581 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1584 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1585 if (rc == -ERESTART) {
1586 LASSERT(cfs_list_empty(&rpc_list));
1592 void ldlm_cancel_callback(struct ldlm_lock *lock)
1594 check_res_locked(lock->l_resource);
1595 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1596 lock->l_flags |= LDLM_FL_CANCEL;
1597 if (lock->l_blocking_ast) {
1598 // l_check_no_ns_lock(ns);
1599 unlock_res_and_lock(lock);
1600 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1602 lock_res_and_lock(lock);
1604 LDLM_DEBUG(lock, "no blocking ast");
1607 lock->l_flags |= LDLM_FL_BL_DONE;
1610 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1612 if (req->l_resource->lr_type != LDLM_PLAIN &&
1613 req->l_resource->lr_type != LDLM_IBITS)
1616 cfs_list_del_init(&req->l_sl_policy);
1617 cfs_list_del_init(&req->l_sl_mode);
1620 void ldlm_lock_cancel(struct ldlm_lock *lock)
1622 struct ldlm_resource *res;
1623 struct ldlm_namespace *ns;
1626 lock_res_and_lock(lock);
1628 res = lock->l_resource;
1629 ns = ldlm_res_to_ns(res);
1631 /* Please do not, no matter how tempting, remove this LBUG without
1632 * talking to me first. -phik */
1633 if (lock->l_readers || lock->l_writers) {
1634 LDLM_ERROR(lock, "lock still has references");
1638 ldlm_del_waiting_lock(lock);
1640 /* Releases cancel callback. */
1641 ldlm_cancel_callback(lock);
1643 /* Yes, second time, just in case it was added again while we were
1644 running with no res lock in ldlm_cancel_callback */
1645 ldlm_del_waiting_lock(lock);
1646 ldlm_resource_unlink_lock(lock);
1647 ldlm_lock_destroy_nolock(lock);
1649 if (lock->l_granted_mode == lock->l_req_mode)
1650 ldlm_pool_del(&ns->ns_pool, lock);
1652 /* Make sure we will not be called again for same lock what is possible
1653 * if not to zero out lock->l_granted_mode */
1654 lock->l_granted_mode = LCK_MINMODE;
1655 unlock_res_and_lock(lock);
1660 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1662 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1668 lock->l_ast_data = data;
1669 LDLM_LOCK_PUT(lock);
1673 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1674 cfs_hlist_node_t *hnode, void *data)
1677 struct obd_export *exp = data;
1678 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
1679 struct ldlm_resource *res;
1681 res = ldlm_resource_getref(lock->l_resource);
1682 LDLM_LOCK_GET(lock);
1684 LDLM_DEBUG(lock, "export %p", exp);
1685 ldlm_res_lvbo_update(res, NULL, 1);
1686 ldlm_lock_cancel(lock);
1687 ldlm_reprocess_all(res);
1688 ldlm_resource_putref(res);
1689 LDLM_LOCK_RELEASE(lock);
1693 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1695 cfs_hash_for_each_empty(exp->exp_lock_hash,
1696 ldlm_cancel_locks_for_export_cb, exp);
1700 * Downgrade an exclusive lock.
1702 * A fast variant of ldlm_lock_convert for convertion of exclusive
1703 * locks. The convertion is always successful.
1705 * \param lock A lock to convert
1706 * \param new_mode new lock mode
1708 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1712 LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1713 LASSERT(new_mode == LCK_COS);
1715 lock_res_and_lock(lock);
1716 ldlm_resource_unlink_lock(lock);
1718 * Remove the lock from pool as it will be added again in
1719 * ldlm_grant_lock() called below.
1721 ldlm_pool_del(&ldlm_lock_to_ns(lock)->ns_pool, lock);
1723 lock->l_req_mode = new_mode;
1724 ldlm_grant_lock(lock, NULL);
1725 unlock_res_and_lock(lock);
1726 ldlm_reprocess_all(lock->l_resource);
1731 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1734 CFS_LIST_HEAD(rpc_list);
1735 struct ldlm_resource *res;
1736 struct ldlm_namespace *ns;
1739 struct sl_insert_point prev;
1741 struct ldlm_interval *node;
1744 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1745 *flags |= LDLM_FL_BLOCK_GRANTED;
1746 RETURN(lock->l_resource);
1749 /* I can't check the type of lock here because the bitlock of lock
1750 * is not held here, so do the allocation blindly. -jay */
1751 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1752 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1755 LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1756 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1758 lock_res_and_lock(lock);
1760 res = lock->l_resource;
1761 ns = ldlm_res_to_ns(res);
1763 old_mode = lock->l_req_mode;
1764 lock->l_req_mode = new_mode;
1765 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1766 /* remember the lock position where the lock might be
1767 * added back to the granted list later and also
1768 * remember the join mode for skiplist fixing. */
1769 prev.res_link = lock->l_res_link.prev;
1770 prev.mode_link = lock->l_sl_mode.prev;
1771 prev.policy_link = lock->l_sl_policy.prev;
1772 ldlm_resource_unlink_lock(lock);
1774 ldlm_resource_unlink_lock(lock);
1775 if (res->lr_type == LDLM_EXTENT) {
1776 /* FIXME: ugly code, I have to attach the lock to a
1777 * interval node again since perhaps it will be granted
1779 CFS_INIT_LIST_HEAD(&node->li_group);
1780 ldlm_interval_attach(node, lock);
1786 * Remove old lock from the pool before adding the lock with new
1787 * mode below in ->policy()
1789 ldlm_pool_del(&ns->ns_pool, lock);
1791 /* If this is a local resource, put it on the appropriate list. */
1792 if (ns_is_client(ldlm_res_to_ns(res))) {
1793 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1794 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1796 /* This should never happen, because of the way the
1797 * server handles conversions. */
1798 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1802 ldlm_grant_lock(lock, &rpc_list);
1804 /* FIXME: completion handling not with lr_lock held ! */
1805 if (lock->l_completion_ast)
1806 lock->l_completion_ast(lock, 0, NULL);
1810 ldlm_processing_policy policy;
1811 policy = ldlm_processing_policy_table[res->lr_type];
1812 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1813 if (rc == LDLM_ITER_STOP) {
1814 lock->l_req_mode = old_mode;
1815 if (res->lr_type == LDLM_EXTENT)
1816 ldlm_extent_add_lock(res, lock);
1818 ldlm_granted_list_add_lock(lock, &prev);
1822 *flags |= LDLM_FL_BLOCK_GRANTED;
1826 unlock_res_and_lock(lock);
1829 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1831 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1835 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1837 struct obd_device *obd = NULL;
1839 if (!((libcfs_debug | D_ERROR) & level))
1843 CDEBUG(level, " NULL LDLM lock\n");
1847 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1848 lock, lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1850 if (lock->l_conn_export != NULL)
1851 obd = lock->l_conn_export->exp_obd;
1852 if (lock->l_export && lock->l_export->exp_connection) {
1853 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1854 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1855 lock->l_remote_handle.cookie);
1856 } else if (obd == NULL) {
1857 CDEBUG(level, " Node: local\n");
1859 struct obd_import *imp = obd->u.cli.cl_import;
1860 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1861 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1862 lock->l_remote_handle.cookie);
1864 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1866 lock->l_resource->lr_name.name[0],
1867 lock->l_resource->lr_name.name[1],
1868 lock->l_resource->lr_name.name[2]);
1869 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1870 "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1871 ldlm_lockname[lock->l_granted_mode],
1872 cfs_atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1874 if (lock->l_resource->lr_type == LDLM_EXTENT)
1875 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1876 " (req "LPU64"-"LPU64")\n",
1877 lock->l_policy_data.l_extent.start,
1878 lock->l_policy_data.l_extent.end,
1879 lock->l_req_extent.start, lock->l_req_extent.end);
1880 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1881 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1882 lock->l_policy_data.l_flock.pid,
1883 lock->l_policy_data.l_flock.start,
1884 lock->l_policy_data.l_flock.end);
1885 else if (lock->l_resource->lr_type == LDLM_IBITS)
1886 CDEBUG(level, " Bits: "LPX64"\n",
1887 lock->l_policy_data.l_inodebits.bits);
1890 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1892 struct ldlm_lock *lock;
1894 if (!((libcfs_debug | D_ERROR) & level))
1897 lock = ldlm_handle2lock(lockh);
1901 ldlm_lock_dump(D_OTHER, lock, 0);
1903 LDLM_LOCK_PUT(lock);
1906 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1907 struct libcfs_debug_msg_data *data, const char *fmt,
1911 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1913 va_start(args, fmt);
1915 if (lock->l_resource == NULL) {
1916 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1917 data->msg_fn, data->msg_line, fmt, args,
1918 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1919 "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
1920 LPX64" expref: %d pid: %u timeout: %lu\n", lock,
1921 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1922 lock->l_readers, lock->l_writers,
1923 ldlm_lockname[lock->l_granted_mode],
1924 ldlm_lockname[lock->l_req_mode],
1925 lock->l_flags, lock->l_remote_handle.cookie,
1927 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1928 lock->l_pid, lock->l_callback_timeout);
1933 switch (lock->l_resource->lr_type) {
1935 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1936 data->msg_fn, data->msg_line, fmt, args,
1937 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1938 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1939 "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
1940 " expref: %d pid: %u timeout %lu\n",
1941 ldlm_lock_to_ns_name(lock), lock,
1942 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1943 lock->l_readers, lock->l_writers,
1944 ldlm_lockname[lock->l_granted_mode],
1945 ldlm_lockname[lock->l_req_mode],
1946 lock->l_resource->lr_name.name[0],
1947 lock->l_resource->lr_name.name[1],
1948 cfs_atomic_read(&lock->l_resource->lr_refcount),
1949 ldlm_typename[lock->l_resource->lr_type],
1950 lock->l_policy_data.l_extent.start,
1951 lock->l_policy_data.l_extent.end,
1952 lock->l_req_extent.start, lock->l_req_extent.end,
1953 lock->l_flags, lock->l_remote_handle.cookie,
1955 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1956 lock->l_pid, lock->l_callback_timeout);
1960 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1961 data->msg_fn, data->msg_line, fmt, args,
1962 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1963 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1964 "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
1965 " expref: %d pid: %u timeout: %lu\n",
1966 ldlm_lock_to_ns_name(lock), lock,
1967 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1968 lock->l_readers, lock->l_writers,
1969 ldlm_lockname[lock->l_granted_mode],
1970 ldlm_lockname[lock->l_req_mode],
1971 lock->l_resource->lr_name.name[0],
1972 lock->l_resource->lr_name.name[1],
1973 cfs_atomic_read(&lock->l_resource->lr_refcount),
1974 ldlm_typename[lock->l_resource->lr_type],
1975 lock->l_policy_data.l_flock.pid,
1976 lock->l_policy_data.l_flock.start,
1977 lock->l_policy_data.l_flock.end,
1978 lock->l_flags, lock->l_remote_handle.cookie,
1980 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1981 lock->l_pid, lock->l_callback_timeout);
1985 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1986 data->msg_fn, data->msg_line, fmt, args,
1987 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1988 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1989 "flags: "LPX64" remote: "LPX64" expref: %d "
1990 "pid: %u timeout: %lu\n",
1991 ldlm_lock_to_ns_name(lock),
1992 lock, lock->l_handle.h_cookie,
1993 cfs_atomic_read (&lock->l_refc),
1994 lock->l_readers, lock->l_writers,
1995 ldlm_lockname[lock->l_granted_mode],
1996 ldlm_lockname[lock->l_req_mode],
1997 lock->l_resource->lr_name.name[0],
1998 lock->l_resource->lr_name.name[1],
1999 lock->l_policy_data.l_inodebits.bits,
2000 cfs_atomic_read(&lock->l_resource->lr_refcount),
2001 ldlm_typename[lock->l_resource->lr_type],
2002 lock->l_flags, lock->l_remote_handle.cookie,
2004 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2005 lock->l_pid, lock->l_callback_timeout);
2009 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2010 data->msg_fn, data->msg_line, fmt, args,
2011 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2012 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2013 "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
2014 ldlm_lock_to_ns_name(lock),
2015 lock, lock->l_handle.h_cookie,
2016 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_resource->lr_name.name[0],
2021 lock->l_resource->lr_name.name[1],
2022 cfs_atomic_read(&lock->l_resource->lr_refcount),
2023 ldlm_typename[lock->l_resource->lr_type],
2024 lock->l_flags, lock->l_remote_handle.cookie,
2026 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2027 lock->l_pid, lock->l_callback_timeout);
2032 EXPORT_SYMBOL(_ldlm_lock_debug);