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 # ifndef HAVE_VFS_INTENT_PATCHES
47 # include <linux/lustre_intent.h>
50 # include <liblustre.h>
53 #include <obd_class.h>
54 #include "ldlm_internal.h"
57 char *ldlm_lockname[] = {
69 char *ldlm_typename[] = {
76 char *ldlm_it2str(int it)
83 case (IT_OPEN | IT_CREAT):
96 CERROR("Unknown intent %d\n", it);
101 extern cfs_mem_cache_t *ldlm_lock_slab;
103 static ldlm_processing_policy ldlm_processing_policy_table[] = {
104 [LDLM_PLAIN] ldlm_process_plain_lock,
105 [LDLM_EXTENT] ldlm_process_extent_lock,
107 [LDLM_FLOCK] ldlm_process_flock_lock,
109 [LDLM_IBITS] ldlm_process_inodebits_lock,
112 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
114 return ldlm_processing_policy_table[res->lr_type];
117 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
123 * REFCOUNTED LOCK OBJECTS
128 * Lock refcounts, during creation:
129 * - one special one for allocation, dec'd only once in destroy
130 * - one for being a lock that's in-use
131 * - one for the addref associated with a new lock
133 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
135 cfs_atomic_inc(&lock->l_refc);
139 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
141 LASSERT(size == sizeof(*lock));
142 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
145 void ldlm_lock_put(struct ldlm_lock *lock)
149 LASSERT(lock->l_resource != LP_POISON);
150 LASSERT(cfs_atomic_read(&lock->l_refc) > 0);
151 if (cfs_atomic_dec_and_test(&lock->l_refc)) {
152 struct ldlm_resource *res;
155 "final lock_put on destroyed lock, freeing it.");
157 res = lock->l_resource;
158 LASSERT(lock->l_destroyed);
159 LASSERT(cfs_list_empty(&lock->l_res_link));
160 LASSERT(cfs_list_empty(&lock->l_pending_chain));
162 cfs_atomic_dec(&ldlm_res_to_ns(res)->ns_locks);
163 lu_ref_del(&res->lr_reference, "lock", lock);
164 ldlm_resource_putref(res);
165 lock->l_resource = NULL;
166 if (lock->l_export) {
167 class_export_lock_put(lock->l_export, lock);
168 lock->l_export = NULL;
171 if (lock->l_lvb_data != NULL)
172 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
174 ldlm_interval_free(ldlm_interval_detach(lock));
175 lu_ref_fini(&lock->l_reference);
176 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
183 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
186 if (!cfs_list_empty(&lock->l_lru)) {
187 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
189 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
190 cfs_list_del_init(&lock->l_lru);
191 if (lock->l_flags & LDLM_FL_SKIPPED)
192 lock->l_flags &= ~LDLM_FL_SKIPPED;
193 LASSERT(ns->ns_nr_unused > 0);
200 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
202 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
206 if (lock->l_ns_srv) {
207 LASSERT(cfs_list_empty(&lock->l_lru));
211 cfs_spin_lock(&ns->ns_unused_lock);
212 rc = ldlm_lock_remove_from_lru_nolock(lock);
213 cfs_spin_unlock(&ns->ns_unused_lock);
218 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
220 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
222 lock->l_last_used = cfs_time_current();
223 LASSERT(cfs_list_empty(&lock->l_lru));
224 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
225 cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
226 LASSERT(ns->ns_nr_unused >= 0);
230 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
232 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
235 cfs_spin_lock(&ns->ns_unused_lock);
236 ldlm_lock_add_to_lru_nolock(lock);
237 cfs_spin_unlock(&ns->ns_unused_lock);
241 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
243 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
246 if (lock->l_ns_srv) {
247 LASSERT(cfs_list_empty(&lock->l_lru));
252 cfs_spin_lock(&ns->ns_unused_lock);
253 if (!cfs_list_empty(&lock->l_lru)) {
254 ldlm_lock_remove_from_lru_nolock(lock);
255 ldlm_lock_add_to_lru_nolock(lock);
257 cfs_spin_unlock(&ns->ns_unused_lock);
261 /* This used to have a 'strict' flag, which recovery would use to mark an
262 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
263 * shall explain why it's gone: with the new hash table scheme, once you call
264 * ldlm_lock_destroy, you can never drop your final references on this lock.
265 * Because it's not in the hash table anymore. -phil */
266 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
270 if (lock->l_readers || lock->l_writers) {
271 LDLM_ERROR(lock, "lock still has references");
272 ldlm_lock_dump(D_ERROR, lock, 0);
276 if (!cfs_list_empty(&lock->l_res_link)) {
277 LDLM_ERROR(lock, "lock still on resource");
278 ldlm_lock_dump(D_ERROR, lock, 0);
282 if (lock->l_destroyed) {
283 LASSERT(cfs_list_empty(&lock->l_lru));
287 lock->l_destroyed = 1;
289 if (lock->l_export && lock->l_export->exp_lock_hash &&
290 !cfs_hlist_unhashed(&lock->l_exp_hash))
291 cfs_hash_del(lock->l_export->exp_lock_hash,
292 &lock->l_remote_handle, &lock->l_exp_hash);
294 ldlm_lock_remove_from_lru(lock);
295 class_handle_unhash(&lock->l_handle);
298 /* Wake anyone waiting for this lock */
299 /* FIXME: I should probably add yet another flag, instead of using
300 * l_export to only call this on clients */
302 class_export_put(lock->l_export);
303 lock->l_export = NULL;
304 if (lock->l_export && lock->l_completion_ast)
305 lock->l_completion_ast(lock, 0);
311 void ldlm_lock_destroy(struct ldlm_lock *lock)
315 lock_res_and_lock(lock);
316 first = ldlm_lock_destroy_internal(lock);
317 unlock_res_and_lock(lock);
319 /* drop reference from hashtable only for first destroy */
321 lu_ref_del(&lock->l_reference, "hash", lock);
322 LDLM_LOCK_RELEASE(lock);
327 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
331 first = ldlm_lock_destroy_internal(lock);
332 /* drop reference from hashtable only for first destroy */
334 lu_ref_del(&lock->l_reference, "hash", lock);
335 LDLM_LOCK_RELEASE(lock);
340 /* this is called by portals_handle2object with the handle lock taken */
341 static void lock_handle_addref(void *lock)
343 LDLM_LOCK_GET((struct ldlm_lock *)lock);
347 * usage: pass in a resource on which you have done ldlm_resource_get
348 * new lock will take over the refcount.
349 * returns: lock with refcount 2 - one for current caller and one for remote
351 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
353 struct ldlm_lock *lock;
356 if (resource == NULL)
359 OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
363 cfs_spin_lock_init(&lock->l_lock);
364 lock->l_resource = resource;
365 lu_ref_add(&resource->lr_reference, "lock", lock);
367 cfs_atomic_set(&lock->l_refc, 2);
368 CFS_INIT_LIST_HEAD(&lock->l_res_link);
369 CFS_INIT_LIST_HEAD(&lock->l_lru);
370 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
371 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
372 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
373 CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
374 cfs_waitq_init(&lock->l_waitq);
375 lock->l_blocking_lock = NULL;
376 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
377 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
378 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
380 cfs_atomic_inc(&ldlm_res_to_ns(resource)->ns_locks);
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);
424 lu_ref_add(&newres->lr_reference, "lock", lock);
428 * To flip the lock from the old to the new resource, lock, oldres and
429 * newres have to be locked. Resource spin-locks are nested within
430 * lock->l_lock, and are taken in the memory address order to avoid
433 cfs_spin_lock(&lock->l_lock);
434 oldres = lock->l_resource;
435 if (oldres < newres) {
437 lock_res_nested(newres, LRT_NEW);
440 lock_res_nested(oldres, LRT_NEW);
442 LASSERT(memcmp(new_resid, &oldres->lr_name,
443 sizeof oldres->lr_name) != 0);
444 lock->l_resource = newres;
446 unlock_res_and_lock(lock);
448 /* ...and the flowers are still standing! */
449 lu_ref_del(&oldres->lr_reference, "lock", lock);
450 ldlm_resource_putref(oldres);
459 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
461 lockh->cookie = lock->l_handle.h_cookie;
464 /* if flags: atomically get the lock and set the flags.
465 * Return NULL if flag already set
468 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
471 struct ldlm_namespace *ns;
472 struct ldlm_lock *lock, *retval = NULL;
477 lock = class_handle2object(handle->cookie);
481 LASSERT(lock->l_resource != NULL);
482 ns = ldlm_lock_to_ns(lock);
485 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
486 lock_res_and_lock(lock);
488 /* It's unlikely but possible that someone marked the lock as
489 * destroyed after we did handle2object on it */
490 if (lock->l_destroyed) {
491 unlock_res_and_lock(lock);
492 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
497 if (flags && (lock->l_flags & flags)) {
498 unlock_res_and_lock(lock);
504 lock->l_flags |= flags;
506 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 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1552 cfs_spin_lock(&ns->ns_hash_lock);
1553 for (i = 0; i < RES_HASH_SIZE; i++) {
1554 tmp = ns->ns_hash[i].next;
1555 while (tmp != &(ns->ns_hash[i])) {
1556 struct ldlm_resource *res =
1557 cfs_list_entry(tmp, struct ldlm_resource,
1560 ldlm_resource_getref(res);
1561 cfs_spin_unlock(&ns->ns_hash_lock);
1562 LDLM_RESOURCE_ADDREF(res);
1564 rc = reprocess_one_queue(res, NULL);
1566 LDLM_RESOURCE_DELREF(res);
1567 cfs_spin_lock(&ns->ns_hash_lock);
1569 ldlm_resource_putref_locked(res);
1571 if (rc == LDLM_ITER_STOP)
1576 cfs_spin_unlock(&ns->ns_hash_lock);
1580 void ldlm_reprocess_all(struct ldlm_resource *res)
1582 CFS_LIST_HEAD(rpc_list);
1586 /* Local lock trees don't get reprocessed. */
1587 if (ns_is_client(ldlm_res_to_ns(res))) {
1594 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1595 if (rc == LDLM_ITER_CONTINUE)
1596 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1599 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1600 if (rc == -ERESTART) {
1601 LASSERT(cfs_list_empty(&rpc_list));
1607 void ldlm_cancel_callback(struct ldlm_lock *lock)
1609 check_res_locked(lock->l_resource);
1610 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1611 lock->l_flags |= LDLM_FL_CANCEL;
1612 if (lock->l_blocking_ast) {
1613 // l_check_no_ns_lock(ns);
1614 unlock_res_and_lock(lock);
1615 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1617 lock_res_and_lock(lock);
1619 LDLM_DEBUG(lock, "no blocking ast");
1622 lock->l_flags |= LDLM_FL_BL_DONE;
1625 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1627 if (req->l_resource->lr_type != LDLM_PLAIN &&
1628 req->l_resource->lr_type != LDLM_IBITS)
1631 cfs_list_del_init(&req->l_sl_policy);
1632 cfs_list_del_init(&req->l_sl_mode);
1635 void ldlm_lock_cancel(struct ldlm_lock *lock)
1637 struct ldlm_resource *res;
1638 struct ldlm_namespace *ns;
1641 lock_res_and_lock(lock);
1643 res = lock->l_resource;
1644 ns = ldlm_res_to_ns(res);
1646 /* Please do not, no matter how tempting, remove this LBUG without
1647 * talking to me first. -phik */
1648 if (lock->l_readers || lock->l_writers) {
1649 LDLM_ERROR(lock, "lock still has references");
1653 ldlm_del_waiting_lock(lock);
1655 /* Releases cancel callback. */
1656 ldlm_cancel_callback(lock);
1658 /* Yes, second time, just in case it was added again while we were
1659 running with no res lock in ldlm_cancel_callback */
1660 ldlm_del_waiting_lock(lock);
1661 ldlm_resource_unlink_lock(lock);
1662 ldlm_lock_destroy_nolock(lock);
1664 if (lock->l_granted_mode == lock->l_req_mode)
1665 ldlm_pool_del(&ns->ns_pool, lock);
1667 /* Make sure we will not be called again for same lock what is possible
1668 * if not to zero out lock->l_granted_mode */
1669 lock->l_granted_mode = LCK_MINMODE;
1670 unlock_res_and_lock(lock);
1675 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1677 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1683 lock->l_ast_data = data;
1684 LDLM_LOCK_PUT(lock);
1688 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1689 cfs_hlist_node_t *hnode, void *data)
1692 struct obd_export *exp = data;
1693 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
1694 struct ldlm_resource *res;
1696 res = ldlm_resource_getref(lock->l_resource);
1697 LDLM_LOCK_GET(lock);
1699 LDLM_DEBUG(lock, "export %p", exp);
1700 ldlm_res_lvbo_update(res, NULL, 1);
1701 ldlm_lock_cancel(lock);
1702 ldlm_reprocess_all(res);
1703 ldlm_resource_putref(res);
1704 LDLM_LOCK_RELEASE(lock);
1708 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1710 cfs_hash_for_each_empty(exp->exp_lock_hash,
1711 ldlm_cancel_locks_for_export_cb, exp);
1715 * Downgrade an exclusive lock.
1717 * A fast variant of ldlm_lock_convert for convertion of exclusive
1718 * locks. The convertion is always successful.
1720 * \param lock A lock to convert
1721 * \param new_mode new lock mode
1723 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1725 struct ldlm_namespace *ns;
1728 LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1729 LASSERT(new_mode == LCK_COS);
1731 lock_res_and_lock(lock);
1732 ldlm_resource_unlink_lock(lock);
1734 * Remove the lock from pool as it will be added again in
1735 * ldlm_grant_lock() called below.
1737 ns = ldlm_lock_to_ns(lock);
1738 ldlm_pool_del(&ns->ns_pool, lock);
1740 lock->l_req_mode = new_mode;
1741 ldlm_grant_lock(lock, NULL);
1742 unlock_res_and_lock(lock);
1743 ldlm_reprocess_all(lock->l_resource);
1748 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1751 CFS_LIST_HEAD(rpc_list);
1752 struct ldlm_resource *res;
1753 struct ldlm_namespace *ns;
1756 struct sl_insert_point prev;
1758 struct ldlm_interval *node;
1761 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1762 *flags |= LDLM_FL_BLOCK_GRANTED;
1763 RETURN(lock->l_resource);
1766 /* I can't check the type of lock here because the bitlock of lock
1767 * is not held here, so do the allocation blindly. -jay */
1768 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1769 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1772 LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1773 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1775 lock_res_and_lock(lock);
1777 res = lock->l_resource;
1778 ns = ldlm_res_to_ns(res);
1780 old_mode = lock->l_req_mode;
1781 lock->l_req_mode = new_mode;
1782 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1783 /* remember the lock position where the lock might be
1784 * added back to the granted list later and also
1785 * remember the join mode for skiplist fixing. */
1786 prev.res_link = lock->l_res_link.prev;
1787 prev.mode_link = lock->l_sl_mode.prev;
1788 prev.policy_link = lock->l_sl_policy.prev;
1789 ldlm_resource_unlink_lock(lock);
1791 ldlm_resource_unlink_lock(lock);
1792 if (res->lr_type == LDLM_EXTENT) {
1793 /* FIXME: ugly code, I have to attach the lock to a
1794 * interval node again since perhaps it will be granted
1796 CFS_INIT_LIST_HEAD(&node->li_group);
1797 ldlm_interval_attach(node, lock);
1803 * Remove old lock from the pool before adding the lock with new
1804 * mode below in ->policy()
1806 ldlm_pool_del(&ns->ns_pool, lock);
1808 /* If this is a local resource, put it on the appropriate list. */
1809 if (ns_is_client(ldlm_res_to_ns(res))) {
1810 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1811 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1813 /* This should never happen, because of the way the
1814 * server handles conversions. */
1815 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1819 ldlm_grant_lock(lock, &rpc_list);
1821 /* FIXME: completion handling not with ns_lock held ! */
1822 if (lock->l_completion_ast)
1823 lock->l_completion_ast(lock, 0, NULL);
1827 ldlm_processing_policy policy;
1828 policy = ldlm_processing_policy_table[res->lr_type];
1829 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1830 if (rc == LDLM_ITER_STOP) {
1831 lock->l_req_mode = old_mode;
1832 if (res->lr_type == LDLM_EXTENT)
1833 ldlm_extent_add_lock(res, lock);
1835 ldlm_granted_list_add_lock(lock, &prev);
1839 *flags |= LDLM_FL_BLOCK_GRANTED;
1843 unlock_res_and_lock(lock);
1846 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1848 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1852 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1854 struct obd_device *obd = NULL;
1856 if (!((libcfs_debug | D_ERROR) & level))
1860 CDEBUG(level, " NULL LDLM lock\n");
1864 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1865 lock, lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1867 if (lock->l_conn_export != NULL)
1868 obd = lock->l_conn_export->exp_obd;
1869 if (lock->l_export && lock->l_export->exp_connection) {
1870 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1871 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1872 lock->l_remote_handle.cookie);
1873 } else if (obd == NULL) {
1874 CDEBUG(level, " Node: local\n");
1876 struct obd_import *imp = obd->u.cli.cl_import;
1877 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1878 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1879 lock->l_remote_handle.cookie);
1881 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1883 lock->l_resource->lr_name.name[0],
1884 lock->l_resource->lr_name.name[1],
1885 lock->l_resource->lr_name.name[2]);
1886 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1887 "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1888 ldlm_lockname[lock->l_granted_mode],
1889 cfs_atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1891 if (lock->l_resource->lr_type == LDLM_EXTENT)
1892 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1893 " (req "LPU64"-"LPU64")\n",
1894 lock->l_policy_data.l_extent.start,
1895 lock->l_policy_data.l_extent.end,
1896 lock->l_req_extent.start, lock->l_req_extent.end);
1897 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1898 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1899 lock->l_policy_data.l_flock.pid,
1900 lock->l_policy_data.l_flock.start,
1901 lock->l_policy_data.l_flock.end);
1902 else if (lock->l_resource->lr_type == LDLM_IBITS)
1903 CDEBUG(level, " Bits: "LPX64"\n",
1904 lock->l_policy_data.l_inodebits.bits);
1907 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1909 struct ldlm_lock *lock;
1911 if (!((libcfs_debug | D_ERROR) & level))
1914 lock = ldlm_handle2lock(lockh);
1918 ldlm_lock_dump(D_OTHER, lock, 0);
1920 LDLM_LOCK_PUT(lock);
1923 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1924 struct libcfs_debug_msg_data *data, const char *fmt,
1928 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1930 va_start(args, fmt);
1932 if (lock->l_resource == NULL) {
1933 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1934 data->msg_fn, data->msg_line, fmt, args,
1935 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1936 "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
1937 LPX64" expref: %d pid: %u timeout: %lu\n", lock,
1938 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1939 lock->l_readers, lock->l_writers,
1940 ldlm_lockname[lock->l_granted_mode],
1941 ldlm_lockname[lock->l_req_mode],
1942 lock->l_flags, lock->l_remote_handle.cookie,
1944 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1945 lock->l_pid, lock->l_callback_timeout);
1950 switch (lock->l_resource->lr_type) {
1952 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1953 data->msg_fn, data->msg_line, fmt, args,
1954 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1955 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1956 "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
1957 " expref: %d pid: %u timeout %lu\n",
1958 ldlm_lock_to_ns_name(lock), lock,
1959 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1960 lock->l_readers, lock->l_writers,
1961 ldlm_lockname[lock->l_granted_mode],
1962 ldlm_lockname[lock->l_req_mode],
1963 lock->l_resource->lr_name.name[0],
1964 lock->l_resource->lr_name.name[1],
1965 cfs_atomic_read(&lock->l_resource->lr_refcount),
1966 ldlm_typename[lock->l_resource->lr_type],
1967 lock->l_policy_data.l_extent.start,
1968 lock->l_policy_data.l_extent.end,
1969 lock->l_req_extent.start, lock->l_req_extent.end,
1970 lock->l_flags, lock->l_remote_handle.cookie,
1972 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1973 lock->l_pid, lock->l_callback_timeout);
1977 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1978 data->msg_fn, data->msg_line, fmt, args,
1979 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1980 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1981 "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
1982 " expref: %d pid: %u timeout: %lu\n",
1983 ldlm_lock_to_ns_name(lock), lock,
1984 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1985 lock->l_readers, lock->l_writers,
1986 ldlm_lockname[lock->l_granted_mode],
1987 ldlm_lockname[lock->l_req_mode],
1988 lock->l_resource->lr_name.name[0],
1989 lock->l_resource->lr_name.name[1],
1990 cfs_atomic_read(&lock->l_resource->lr_refcount),
1991 ldlm_typename[lock->l_resource->lr_type],
1992 lock->l_policy_data.l_flock.pid,
1993 lock->l_policy_data.l_flock.start,
1994 lock->l_policy_data.l_flock.end,
1995 lock->l_flags, lock->l_remote_handle.cookie,
1997 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1998 lock->l_pid, lock->l_callback_timeout);
2002 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2003 data->msg_fn, data->msg_line, fmt, args,
2004 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2005 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
2006 "flags: "LPX64" remote: "LPX64" expref: %d "
2007 "pid: %u timeout: %lu\n",
2008 ldlm_lock_to_ns_name(lock),
2009 lock, lock->l_handle.h_cookie,
2010 cfs_atomic_read (&lock->l_refc),
2011 lock->l_readers, lock->l_writers,
2012 ldlm_lockname[lock->l_granted_mode],
2013 ldlm_lockname[lock->l_req_mode],
2014 lock->l_resource->lr_name.name[0],
2015 lock->l_resource->lr_name.name[1],
2016 lock->l_policy_data.l_inodebits.bits,
2017 cfs_atomic_read(&lock->l_resource->lr_refcount),
2018 ldlm_typename[lock->l_resource->lr_type],
2019 lock->l_flags, lock->l_remote_handle.cookie,
2021 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2022 lock->l_pid, lock->l_callback_timeout);
2026 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2027 data->msg_fn, data->msg_line, fmt, args,
2028 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2029 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2030 "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
2031 ldlm_lock_to_ns_name(lock),
2032 lock, lock->l_handle.h_cookie,
2033 cfs_atomic_read (&lock->l_refc),
2034 lock->l_readers, lock->l_writers,
2035 ldlm_lockname[lock->l_granted_mode],
2036 ldlm_lockname[lock->l_req_mode],
2037 lock->l_resource->lr_name.name[0],
2038 lock->l_resource->lr_name.name[1],
2039 cfs_atomic_read(&lock->l_resource->lr_refcount),
2040 ldlm_typename[lock->l_resource->lr_type],
2041 lock->l_flags, lock->l_remote_handle.cookie,
2043 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2044 lock->l_pid, lock->l_callback_timeout);
2049 EXPORT_SYMBOL(_ldlm_lock_debug);