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 2008 Sun Microsystems, Inc. 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 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(atomic_read(&lock->l_refc) > 0);
151 if (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(list_empty(&lock->l_res_link));
160 LASSERT(list_empty(&lock->l_pending_chain));
162 atomic_dec(&res->lr_namespace->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 (!list_empty(&lock->l_lru)) {
187 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
188 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
189 list_del_init(&lock->l_lru);
190 LASSERT(ns->ns_nr_unused > 0);
197 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
199 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
202 spin_lock(&ns->ns_unused_lock);
203 rc = ldlm_lock_remove_from_lru_nolock(lock);
204 spin_unlock(&ns->ns_unused_lock);
209 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
211 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
212 lock->l_last_used = cfs_time_current();
213 LASSERT(list_empty(&lock->l_lru));
214 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
215 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
216 LASSERT(ns->ns_nr_unused >= 0);
220 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
222 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
224 spin_lock(&ns->ns_unused_lock);
225 ldlm_lock_add_to_lru_nolock(lock);
226 spin_unlock(&ns->ns_unused_lock);
230 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
232 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
234 spin_lock(&ns->ns_unused_lock);
235 if (!list_empty(&lock->l_lru)) {
236 ldlm_lock_remove_from_lru_nolock(lock);
237 ldlm_lock_add_to_lru_nolock(lock);
239 spin_unlock(&ns->ns_unused_lock);
243 /* This used to have a 'strict' flag, which recovery would use to mark an
244 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
245 * shall explain why it's gone: with the new hash table scheme, once you call
246 * ldlm_lock_destroy, you can never drop your final references on this lock.
247 * Because it's not in the hash table anymore. -phil */
248 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
252 if (lock->l_readers || lock->l_writers) {
253 LDLM_ERROR(lock, "lock still has references");
254 ldlm_lock_dump(D_ERROR, lock, 0);
258 if (!list_empty(&lock->l_res_link)) {
259 LDLM_ERROR(lock, "lock still on resource");
260 ldlm_lock_dump(D_ERROR, lock, 0);
264 if (lock->l_destroyed) {
265 LASSERT(list_empty(&lock->l_lru));
269 lock->l_destroyed = 1;
271 if (lock->l_export && lock->l_export->exp_lock_hash &&
272 !hlist_unhashed(&lock->l_exp_hash))
273 cfs_hash_del(lock->l_export->exp_lock_hash,
274 &lock->l_remote_handle, &lock->l_exp_hash);
276 ldlm_lock_remove_from_lru(lock);
277 class_handle_unhash(&lock->l_handle);
280 /* Wake anyone waiting for this lock */
281 /* FIXME: I should probably add yet another flag, instead of using
282 * l_export to only call this on clients */
284 class_export_put(lock->l_export);
285 lock->l_export = NULL;
286 if (lock->l_export && lock->l_completion_ast)
287 lock->l_completion_ast(lock, 0);
293 void ldlm_lock_destroy(struct ldlm_lock *lock)
297 lock_res_and_lock(lock);
298 first = ldlm_lock_destroy_internal(lock);
299 unlock_res_and_lock(lock);
301 /* drop reference from hashtable only for first destroy */
303 lu_ref_del(&lock->l_reference, "hash", lock);
304 LDLM_LOCK_RELEASE(lock);
309 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
313 first = ldlm_lock_destroy_internal(lock);
314 /* drop reference from hashtable only for first destroy */
316 lu_ref_del(&lock->l_reference, "hash", lock);
317 LDLM_LOCK_RELEASE(lock);
322 /* this is called by portals_handle2object with the handle lock taken */
323 static void lock_handle_addref(void *lock)
325 LDLM_LOCK_GET((struct ldlm_lock *)lock);
329 * usage: pass in a resource on which you have done ldlm_resource_get
330 * pass in a parent lock on which you have done a ldlm_lock_get
331 * after return, ldlm_*_put the resource and parent
332 * returns: lock with refcount 2 - one for current caller and one for remote
334 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
336 struct ldlm_lock *lock;
339 if (resource == NULL)
342 OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
346 spin_lock_init(&lock->l_lock);
347 lock->l_resource = ldlm_resource_getref(resource);
348 lu_ref_add(&resource->lr_reference, "lock", lock);
350 atomic_set(&lock->l_refc, 2);
351 CFS_INIT_LIST_HEAD(&lock->l_res_link);
352 CFS_INIT_LIST_HEAD(&lock->l_lru);
353 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
354 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
355 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
356 CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
357 cfs_waitq_init(&lock->l_waitq);
358 lock->l_blocking_lock = NULL;
359 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
360 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
361 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
363 atomic_inc(&resource->lr_namespace->ns_locks);
364 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
365 class_handle_hash(&lock->l_handle, lock_handle_addref);
367 CFS_INIT_LIST_HEAD(&lock->l_extents_list);
368 spin_lock_init(&lock->l_extents_list_lock);
369 CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
370 lu_ref_init(&lock->l_reference);
371 lu_ref_add(&lock->l_reference, "hash", lock);
372 lock->l_callback_timeout = 0;
374 #if LUSTRE_TRACKS_LOCK_EXP_REFS
375 CFS_INIT_LIST_HEAD(&lock->l_exp_refs_link);
376 lock->l_exp_refs_nr = 0;
377 lock->l_exp_refs_target = NULL;
383 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
384 const struct ldlm_res_id *new_resid)
386 struct ldlm_resource *oldres = lock->l_resource;
387 struct ldlm_resource *newres;
391 LASSERT(ns_is_client(ns));
393 lock_res_and_lock(lock);
394 if (memcmp(new_resid, &lock->l_resource->lr_name,
395 sizeof(lock->l_resource->lr_name)) == 0) {
397 unlock_res_and_lock(lock);
401 LASSERT(new_resid->name[0] != 0);
403 /* This function assumes that the lock isn't on any lists */
404 LASSERT(list_empty(&lock->l_res_link));
406 type = oldres->lr_type;
407 unlock_res_and_lock(lock);
409 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
410 lu_ref_add(&newres->lr_reference, "lock", lock);
414 * To flip the lock from the old to the new resource, lock, oldres and
415 * newres have to be locked. Resource spin-locks are nested within
416 * lock->l_lock, and are taken in the memory address order to avoid
419 spin_lock(&lock->l_lock);
420 oldres = lock->l_resource;
421 if (oldres < newres) {
423 lock_res_nested(newres, LRT_NEW);
426 lock_res_nested(oldres, LRT_NEW);
428 LASSERT(memcmp(new_resid, &oldres->lr_name,
429 sizeof oldres->lr_name) != 0);
430 lock->l_resource = newres;
432 unlock_res_and_lock(lock);
434 /* ...and the flowers are still standing! */
435 lu_ref_del(&oldres->lr_reference, "lock", lock);
436 ldlm_resource_putref(oldres);
445 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
447 lockh->cookie = lock->l_handle.h_cookie;
450 /* if flags: atomically get the lock and set the flags.
451 * Return NULL if flag already set
454 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
457 struct ldlm_namespace *ns;
458 struct ldlm_lock *lock, *retval = NULL;
463 lock = class_handle2object(handle->cookie);
467 LASSERT(lock->l_resource != NULL);
468 ns = lock->l_resource->lr_namespace;
471 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
472 lock_res_and_lock(lock);
474 /* It's unlikely but possible that someone marked the lock as
475 * destroyed after we did handle2object on it */
476 if (lock->l_destroyed) {
477 unlock_res_and_lock(lock);
478 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
483 if (flags && (lock->l_flags & flags)) {
484 unlock_res_and_lock(lock);
490 lock->l_flags |= flags;
492 unlock_res_and_lock(lock);
499 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
501 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
502 /* INODEBITS_INTEROP: If the other side does not support
503 * inodebits, reply with a plain lock descriptor.
505 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
506 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
507 /* Make sure all the right bits are set in this lock we
508 are going to pass to client */
509 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
510 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
511 "Inappropriate inode lock bits during "
512 "conversion " LPU64 "\n",
513 lock->l_policy_data.l_inodebits.bits);
515 ldlm_res2desc(lock->l_resource, &desc->l_resource);
516 desc->l_resource.lr_type = LDLM_PLAIN;
518 /* Convert "new" lock mode to something old client can
520 if ((lock->l_req_mode == LCK_CR) ||
521 (lock->l_req_mode == LCK_CW))
522 desc->l_req_mode = LCK_PR;
524 desc->l_req_mode = lock->l_req_mode;
525 if ((lock->l_granted_mode == LCK_CR) ||
526 (lock->l_granted_mode == LCK_CW)) {
527 desc->l_granted_mode = LCK_PR;
529 /* We never grant PW/EX locks to clients */
530 LASSERT((lock->l_granted_mode != LCK_PW) &&
531 (lock->l_granted_mode != LCK_EX));
532 desc->l_granted_mode = lock->l_granted_mode;
535 /* We do not copy policy here, because there is no
536 policy for plain locks */
538 ldlm_res2desc(lock->l_resource, &desc->l_resource);
539 desc->l_req_mode = lock->l_req_mode;
540 desc->l_granted_mode = lock->l_granted_mode;
541 desc->l_policy_data = lock->l_policy_data;
545 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
546 struct list_head *work_list)
548 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
549 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
550 lock->l_flags |= LDLM_FL_AST_SENT;
551 /* If the enqueuing client said so, tell the AST recipient to
552 * discard dirty data, rather than writing back. */
553 if (new->l_flags & LDLM_AST_DISCARD_DATA)
554 lock->l_flags |= LDLM_FL_DISCARD_DATA;
555 LASSERT(list_empty(&lock->l_bl_ast));
556 list_add(&lock->l_bl_ast, work_list);
558 LASSERT(lock->l_blocking_lock == NULL);
559 lock->l_blocking_lock = LDLM_LOCK_GET(new);
563 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
565 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
566 lock->l_flags |= LDLM_FL_CP_REQD;
567 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
568 LASSERT(list_empty(&lock->l_cp_ast));
569 list_add(&lock->l_cp_ast, work_list);
574 /* must be called with lr_lock held */
575 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
576 struct list_head *work_list)
579 check_res_locked(lock->l_resource);
581 ldlm_add_bl_work_item(lock, new, work_list);
583 ldlm_add_cp_work_item(lock, work_list);
587 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
589 struct ldlm_lock *lock;
591 lock = ldlm_handle2lock(lockh);
592 LASSERT(lock != NULL);
593 ldlm_lock_addref_internal(lock, mode);
597 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
599 ldlm_lock_remove_from_lru(lock);
600 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
602 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
604 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
606 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
609 lu_ref_add_atomic(&lock->l_reference, "user", lock);
610 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
614 * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
617 * \retval 0 success, lock was addref-ed
619 * \retval -EAGAIN lock is being canceled.
621 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
623 struct ldlm_lock *lock;
627 lock = ldlm_handle2lock(lockh);
629 lock_res_and_lock(lock);
630 if (lock->l_readers != 0 || lock->l_writers != 0 ||
631 !(lock->l_flags & LDLM_FL_CBPENDING)) {
632 ldlm_lock_addref_internal_nolock(lock, mode);
635 unlock_res_and_lock(lock);
641 /* only called for local locks */
642 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
644 lock_res_and_lock(lock);
645 ldlm_lock_addref_internal_nolock(lock, mode);
646 unlock_res_and_lock(lock);
649 /* only called in ldlm_flock_destroy and for local locks.
650 * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
651 * * ldlm_lock_remove_from_lru() does nothing, it is safe
652 * * for ldlm_flock_destroy usage by dropping some code */
653 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
655 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
656 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
657 LASSERT(lock->l_readers > 0);
658 lu_ref_del(&lock->l_reference, "reader", lock);
661 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
662 LASSERT(lock->l_writers > 0);
663 lu_ref_del(&lock->l_reference, "writer", lock);
667 lu_ref_del(&lock->l_reference, "user", lock);
668 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
671 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
673 struct ldlm_namespace *ns;
676 lock_res_and_lock(lock);
678 ns = lock->l_resource->lr_namespace;
680 ldlm_lock_decref_internal_nolock(lock, mode);
682 if (lock->l_flags & LDLM_FL_LOCAL &&
683 !lock->l_readers && !lock->l_writers) {
684 /* If this is a local lock on a server namespace and this was
685 * the last reference, cancel the lock. */
686 CDEBUG(D_INFO, "forcing cancel of local lock\n");
687 lock->l_flags |= LDLM_FL_CBPENDING;
690 if (!lock->l_readers && !lock->l_writers &&
691 (lock->l_flags & LDLM_FL_CBPENDING)) {
692 /* If we received a blocked AST and this was the last reference,
693 * run the callback. */
694 if (ns_is_server(ns) && lock->l_export)
695 CERROR("FL_CBPENDING set on non-local lock--just a "
698 LDLM_DEBUG(lock, "final decref done on cbpending lock");
700 LDLM_LOCK_GET(lock); /* dropped by bl thread */
701 ldlm_lock_remove_from_lru(lock);
702 unlock_res_and_lock(lock);
704 if (lock->l_flags & LDLM_FL_FAIL_LOC)
705 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
707 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
708 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
709 ldlm_handle_bl_callback(ns, NULL, lock);
710 } else if (ns_is_client(ns) &&
711 !lock->l_readers && !lock->l_writers &&
712 !(lock->l_flags & LDLM_FL_BL_AST)) {
713 /* If this is a client-side namespace and this was the last
714 * reference, put it on the LRU. */
715 ldlm_lock_add_to_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 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
722 * are not supported by the server, otherwise, it is done on
724 if (!exp_connect_cancelset(lock->l_conn_export) &&
725 !ns_connect_lru_resize(ns))
726 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
728 unlock_res_and_lock(lock);
734 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
736 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
737 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
738 ldlm_lock_decref_internal(lock, mode);
742 /* This will drop a lock reference and mark it for destruction, but will not
743 * necessarily cancel the lock before returning. */
744 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
746 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
749 LASSERT(lock != NULL);
751 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
752 lock_res_and_lock(lock);
753 lock->l_flags |= LDLM_FL_CBPENDING;
754 unlock_res_and_lock(lock);
755 ldlm_lock_decref_internal(lock, mode);
759 struct sl_insert_point {
760 struct list_head *res_link;
761 struct list_head *mode_link;
762 struct list_head *policy_link;
766 * search_granted_lock
769 * Finds a position to insert the new lock.
771 * queue [input]: the granted list where search acts on;
772 * req [input]: the lock whose position to be located;
773 * prev [output]: positions within 3 lists to insert @req to
777 * - ldlm_grant_lock_with_skiplist
779 static void search_granted_lock(struct list_head *queue,
780 struct ldlm_lock *req,
781 struct sl_insert_point *prev)
783 struct list_head *tmp;
784 struct ldlm_lock *lock, *mode_end, *policy_end;
787 list_for_each(tmp, queue) {
788 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
790 mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
793 if (lock->l_req_mode != req->l_req_mode) {
794 /* jump to last lock of mode group */
795 tmp = &mode_end->l_res_link;
799 /* suitable mode group is found */
800 if (lock->l_resource->lr_type == LDLM_PLAIN) {
801 /* insert point is last lock of the mode group */
802 prev->res_link = &mode_end->l_res_link;
803 prev->mode_link = &mode_end->l_sl_mode;
804 prev->policy_link = &req->l_sl_policy;
807 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
809 policy_end = list_entry(lock->l_sl_policy.prev,
813 if (lock->l_policy_data.l_inodebits.bits ==
814 req->l_policy_data.l_inodebits.bits) {
815 /* insert point is last lock of
816 * the policy group */
818 &policy_end->l_res_link;
820 &policy_end->l_sl_mode;
822 &policy_end->l_sl_policy;
827 if (policy_end == mode_end)
828 /* done with mode group */
831 /* go to next policy group within mode group */
832 tmp = policy_end->l_res_link.next;
833 lock = list_entry(tmp, struct ldlm_lock,
835 } /* loop over policy groups within the mode group */
837 /* insert point is last lock of the mode group,
838 * new policy group is started */
839 prev->res_link = &mode_end->l_res_link;
840 prev->mode_link = &mode_end->l_sl_mode;
841 prev->policy_link = &req->l_sl_policy;
845 LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
850 /* insert point is last lock on the queue,
851 * new mode group and new policy group are started */
852 prev->res_link = queue->prev;
853 prev->mode_link = &req->l_sl_mode;
854 prev->policy_link = &req->l_sl_policy;
859 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
860 struct sl_insert_point *prev)
862 struct ldlm_resource *res = lock->l_resource;
865 check_res_locked(res);
867 ldlm_resource_dump(D_INFO, res);
868 CDEBUG(D_OTHER, "About to add this lock:\n");
869 ldlm_lock_dump(D_OTHER, lock, 0);
871 if (lock->l_destroyed) {
872 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
876 LASSERT(list_empty(&lock->l_res_link));
877 LASSERT(list_empty(&lock->l_sl_mode));
878 LASSERT(list_empty(&lock->l_sl_policy));
880 list_add(&lock->l_res_link, prev->res_link);
881 list_add(&lock->l_sl_mode, prev->mode_link);
882 list_add(&lock->l_sl_policy, prev->policy_link);
887 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
889 struct sl_insert_point prev;
892 LASSERT(lock->l_req_mode == lock->l_granted_mode);
894 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
895 ldlm_granted_list_add_lock(lock, &prev);
900 * - ldlm_lock_enqueue
901 * - ldlm_reprocess_queue
902 * - ldlm_lock_convert
904 * must be called with lr_lock held
906 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
908 struct ldlm_resource *res = lock->l_resource;
911 check_res_locked(res);
913 lock->l_granted_mode = lock->l_req_mode;
914 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
915 ldlm_grant_lock_with_skiplist(lock);
916 else if (res->lr_type == LDLM_EXTENT)
917 ldlm_extent_add_lock(res, lock);
919 ldlm_resource_add_lock(res, &res->lr_granted, lock);
921 if (lock->l_granted_mode < res->lr_most_restr)
922 res->lr_most_restr = lock->l_granted_mode;
924 if (work_list && lock->l_completion_ast != NULL)
925 ldlm_add_ast_work_item(lock, NULL, work_list);
927 ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
931 /* returns a referenced lock or NULL. See the flag descriptions below, in the
932 * comment above ldlm_lock_match */
933 static struct ldlm_lock *search_queue(struct list_head *queue,
935 ldlm_policy_data_t *policy,
936 struct ldlm_lock *old_lock,
937 int flags, int unref)
939 struct ldlm_lock *lock;
940 struct list_head *tmp;
942 list_for_each(tmp, queue) {
945 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
947 if (lock == old_lock)
950 /* llite sometimes wants to match locks that will be
951 * canceled when their users drop, but we allow it to match
952 * if it passes in CBPENDING and the lock still has users.
953 * this is generally only going to be used by children
954 * whose parents already hold a lock so forward progress
955 * can still happen. */
956 if (lock->l_flags & LDLM_FL_CBPENDING &&
957 !(flags & LDLM_FL_CBPENDING))
959 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
960 lock->l_readers == 0 && lock->l_writers == 0)
963 if (!(lock->l_req_mode & *mode))
965 match = lock->l_req_mode;
967 if (lock->l_resource->lr_type == LDLM_EXTENT &&
968 (lock->l_policy_data.l_extent.start >
969 policy->l_extent.start ||
970 lock->l_policy_data.l_extent.end < policy->l_extent.end))
973 if (unlikely(match == LCK_GROUP) &&
974 lock->l_resource->lr_type == LDLM_EXTENT &&
975 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
978 /* We match if we have existing lock with same or wider set
980 if (lock->l_resource->lr_type == LDLM_IBITS &&
981 ((lock->l_policy_data.l_inodebits.bits &
982 policy->l_inodebits.bits) !=
983 policy->l_inodebits.bits))
987 (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED)))
990 if ((flags & LDLM_FL_LOCAL_ONLY) &&
991 !(lock->l_flags & LDLM_FL_LOCAL))
994 if (flags & LDLM_FL_TEST_LOCK) {
996 ldlm_lock_touch_in_lru(lock);
998 ldlm_lock_addref_internal_nolock(lock, match);
1007 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1009 lock->l_flags |= LDLM_FL_LVB_READY;
1010 cfs_waitq_signal(&lock->l_waitq);
1013 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1015 lock_res_and_lock(lock);
1016 ldlm_lock_allow_match_locked(lock);
1017 unlock_res_and_lock(lock);
1020 /* Can be called in two ways:
1022 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1023 * for a duplicate of.
1025 * Otherwise, all of the fields must be filled in, to match against.
1027 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1028 * server (ie, connh is NULL)
1029 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1030 * list will be considered
1031 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1032 * to be canceled can still be matched as long as they still have reader
1033 * or writer refernces
1034 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1035 * just tell us if we would have matched.
1037 * Returns 1 if it finds an already-existing lock that is compatible; in this
1038 * case, lockh is filled in with a addref()ed lock
1040 * we also check security context, if that failed we simply return 0 (to keep
1041 * caller code unchanged), the context failure will be discovered by caller
1044 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1045 const struct ldlm_res_id *res_id, ldlm_type_t type,
1046 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1047 struct lustre_handle *lockh, int unref)
1049 struct ldlm_resource *res;
1050 struct ldlm_lock *lock, *old_lock = NULL;
1055 old_lock = ldlm_handle2lock(lockh);
1058 ns = old_lock->l_resource->lr_namespace;
1059 res_id = &old_lock->l_resource->lr_name;
1060 type = old_lock->l_resource->lr_type;
1061 mode = old_lock->l_req_mode;
1064 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1066 LASSERT(old_lock == NULL);
1070 LDLM_RESOURCE_ADDREF(res);
1073 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1077 if (flags & LDLM_FL_BLOCK_GRANTED)
1079 lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1083 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1091 LDLM_RESOURCE_DELREF(res);
1092 ldlm_resource_putref(res);
1095 ldlm_lock2handle(lock, lockh);
1096 if ((flags & LDLM_FL_LVB_READY) &&
1097 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1098 struct l_wait_info lwi;
1099 if (lock->l_completion_ast) {
1100 int err = lock->l_completion_ast(lock,
1101 LDLM_FL_WAIT_NOREPROC,
1104 if (flags & LDLM_FL_TEST_LOCK)
1105 LDLM_LOCK_RELEASE(lock);
1107 ldlm_lock_decref_internal(lock,
1114 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1115 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1117 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1118 l_wait_event(lock->l_waitq,
1119 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1124 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1125 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1126 res_id->name[2] : policy->l_extent.start,
1127 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1128 res_id->name[3] : policy->l_extent.end);
1130 /* check user's security context */
1131 if (lock->l_conn_export &&
1132 sptlrpc_import_check_ctx(
1133 class_exp2cliimp(lock->l_conn_export))) {
1134 if (!(flags & LDLM_FL_TEST_LOCK))
1135 ldlm_lock_decref_internal(lock, mode);
1139 if (flags & LDLM_FL_TEST_LOCK)
1140 LDLM_LOCK_RELEASE(lock);
1142 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1143 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1144 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1145 type, mode, res_id->name[0], res_id->name[1],
1146 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1147 res_id->name[2] :policy->l_extent.start,
1148 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1149 res_id->name[3] : policy->l_extent.end);
1152 LDLM_LOCK_PUT(old_lock);
1154 return rc ? mode : 0;
1157 /* Returns a referenced lock */
1158 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1159 const struct ldlm_res_id *res_id,
1162 const struct ldlm_callback_suite *cbs,
1163 void *data, __u32 lvb_len)
1165 struct ldlm_lock *lock;
1166 struct ldlm_resource *res;
1169 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1173 lock = ldlm_lock_new(res);
1174 ldlm_resource_putref(res);
1179 lock->l_req_mode = mode;
1180 lock->l_ast_data = data;
1181 lock->l_pid = cfs_curproc_pid();
1183 lock->l_blocking_ast = cbs->lcs_blocking;
1184 lock->l_completion_ast = cbs->lcs_completion;
1185 lock->l_glimpse_ast = cbs->lcs_glimpse;
1186 lock->l_weigh_ast = cbs->lcs_weigh;
1189 lock->l_tree_node = NULL;
1190 /* if this is the extent lock, allocate the interval tree node */
1191 if (type == LDLM_EXTENT) {
1192 if (ldlm_interval_alloc(lock) == NULL)
1197 lock->l_lvb_len = lvb_len;
1198 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1199 if (lock->l_lvb_data == NULL)
1206 if (lock->l_lvb_data)
1207 OBD_FREE(lock->l_lvb_data, lvb_len);
1208 ldlm_interval_free(ldlm_interval_detach(lock));
1209 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1213 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1214 struct ldlm_lock **lockp,
1215 void *cookie, int *flags)
1217 struct ldlm_lock *lock = *lockp;
1218 struct ldlm_resource *res = lock->l_resource;
1219 int local = ns_is_client(res->lr_namespace);
1220 ldlm_processing_policy policy;
1221 ldlm_error_t rc = ELDLM_OK;
1222 struct ldlm_interval *node = NULL;
1225 lock->l_last_activity = cfs_time_current_sec();
1226 /* policies are not executed on the client or during replay */
1227 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1228 && !local && ns->ns_policy) {
1229 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1231 if (rc == ELDLM_LOCK_REPLACED) {
1232 /* The lock that was returned has already been granted,
1233 * and placed into lockp. If it's not the same as the
1234 * one we passed in, then destroy the old one and our
1235 * work here is done. */
1236 if (lock != *lockp) {
1237 ldlm_lock_destroy(lock);
1238 LDLM_LOCK_RELEASE(lock);
1240 *flags |= LDLM_FL_LOCK_CHANGED;
1242 } else if (rc != ELDLM_OK ||
1243 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1244 ldlm_lock_destroy(lock);
1249 /* For a replaying lock, it might be already in granted list. So
1250 * unlinking the lock will cause the interval node to be freed, we
1251 * have to allocate the interval node early otherwise we can't regrant
1252 * this lock in the future. - jay */
1253 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1254 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1256 lock_res_and_lock(lock);
1257 if (local && lock->l_req_mode == lock->l_granted_mode) {
1258 /* The server returned a blocked lock, but it was granted
1259 * before we got a chance to actually enqueue it. We don't
1260 * need to do anything else. */
1261 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1262 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1263 GOTO(out, ELDLM_OK);
1266 ldlm_resource_unlink_lock(lock);
1267 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1269 ldlm_lock_destroy_nolock(lock);
1270 GOTO(out, rc = -ENOMEM);
1273 CFS_INIT_LIST_HEAD(&node->li_group);
1274 ldlm_interval_attach(node, lock);
1278 /* Some flags from the enqueue want to make it into the AST, via the
1279 * lock's l_flags. */
1280 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1282 /* This distinction between local lock trees is very important; a client
1283 * namespace only has information about locks taken by that client, and
1284 * thus doesn't have enough information to decide for itself if it can
1285 * be granted (below). In this case, we do exactly what the server
1286 * tells us to do, as dictated by the 'flags'.
1288 * We do exactly the same thing during recovery, when the server is
1289 * more or less trusting the clients not to lie.
1291 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1292 * granted/converting queues. */
1294 if (*flags & LDLM_FL_BLOCK_CONV)
1295 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1296 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1297 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1299 ldlm_grant_lock(lock, NULL);
1300 GOTO(out, ELDLM_OK);
1301 } else if (*flags & LDLM_FL_REPLAY) {
1302 if (*flags & LDLM_FL_BLOCK_CONV) {
1303 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1304 GOTO(out, ELDLM_OK);
1305 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1306 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1307 GOTO(out, ELDLM_OK);
1308 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1309 ldlm_grant_lock(lock, NULL);
1310 GOTO(out, ELDLM_OK);
1312 /* If no flags, fall through to normal enqueue path. */
1315 policy = ldlm_processing_policy_table[res->lr_type];
1316 policy(lock, flags, 1, &rc, NULL);
1319 unlock_res_and_lock(lock);
1321 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1325 /* Must be called with namespace taken: queue is waiting or converting. */
1326 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1327 struct list_head *work_list)
1329 struct list_head *tmp, *pos;
1330 ldlm_processing_policy policy;
1332 int rc = LDLM_ITER_CONTINUE;
1336 check_res_locked(res);
1338 policy = ldlm_processing_policy_table[res->lr_type];
1341 list_for_each_safe(tmp, pos, queue) {
1342 struct ldlm_lock *pending;
1343 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1345 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1348 rc = policy(pending, &flags, 0, &err, work_list);
1349 if (rc != LDLM_ITER_CONTINUE)
1356 /* Helper function for ldlm_run_ast_work().
1358 * Send an existing rpc set specified by @arg->set and then
1359 * destroy it. Create new one if @do_create flag is set. */
1361 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1365 ptlrpc_set_wait(arg->set);
1366 if (arg->type == LDLM_BL_CALLBACK)
1367 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1368 ptlrpc_set_destroy(arg->set);
1371 arg->set = ptlrpc_prep_set();
1377 ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1379 struct ldlm_lock_desc d;
1380 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
1383 /* nobody should touch l_bl_ast */
1384 lock_res_and_lock(lock);
1385 list_del_init(&lock->l_bl_ast);
1387 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1388 LASSERT(lock->l_bl_ast_run == 0);
1389 LASSERT(lock->l_blocking_lock);
1390 lock->l_bl_ast_run++;
1391 unlock_res_and_lock(lock);
1393 ldlm_lock2desc(lock->l_blocking_lock, &d);
1395 lock->l_blocking_ast(lock, &d, (void *)arg,
1397 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1398 lock->l_blocking_lock = NULL;
1399 LDLM_LOCK_RELEASE(lock);
1405 ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1407 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
1408 ldlm_completion_callback completion_callback;
1412 /* It's possible to receive a completion AST before we've set
1413 * the l_completion_ast pointer: either because the AST arrived
1414 * before the reply, or simply because there's a small race
1415 * window between receiving the reply and finishing the local
1416 * enqueue. (bug 842)
1418 * This can't happen with the blocking_ast, however, because we
1419 * will never call the local blocking_ast until we drop our
1420 * reader/writer reference, which we won't do until we get the
1421 * reply and finish enqueueing. */
1423 /* nobody should touch l_cp_ast */
1424 lock_res_and_lock(lock);
1425 list_del_init(&lock->l_cp_ast);
1426 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1427 /* save l_completion_ast since it can be changed by
1428 * mds_intent_policy(), see bug 14225 */
1429 completion_callback = lock->l_completion_ast;
1430 lock->l_flags &= ~LDLM_FL_CP_REQD;
1431 unlock_res_and_lock(lock);
1433 if (completion_callback != NULL) {
1434 completion_callback(lock, 0, (void *)arg);
1437 LDLM_LOCK_RELEASE(lock);
1443 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1445 struct ldlm_lock_desc desc;
1446 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_rk_ast);
1449 list_del_init(&lock->l_rk_ast);
1451 /* the desc just pretend to exclusive */
1452 ldlm_lock2desc(lock, &desc);
1453 desc.l_req_mode = LCK_EX;
1454 desc.l_granted_mode = 0;
1456 lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1457 LDLM_LOCK_RELEASE(lock);
1462 int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
1464 struct ldlm_cb_set_arg arg;
1465 struct list_head *tmp, *pos;
1466 int (*work_ast_lock)(struct list_head *tmp,struct ldlm_cb_set_arg *arg);
1470 if (list_empty(rpc_list))
1473 arg.set = ptlrpc_prep_set();
1474 if (NULL == arg.set)
1476 atomic_set(&arg.restart, 0);
1478 case LDLM_WORK_BL_AST:
1479 arg.type = LDLM_BL_CALLBACK;
1480 work_ast_lock = ldlm_work_bl_ast_lock;
1482 case LDLM_WORK_CP_AST:
1483 arg.type = LDLM_CP_CALLBACK;
1484 work_ast_lock = ldlm_work_cp_ast_lock;
1486 case LDLM_WORK_REVOKE_AST:
1487 arg.type = LDLM_BL_CALLBACK;
1488 work_ast_lock = ldlm_work_revoke_ast_lock;
1495 list_for_each_safe(tmp, pos, rpc_list) {
1496 ast_count += work_ast_lock(tmp, &arg);
1498 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1499 * and create a new set for requests that remained in
1501 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1502 ldlm_send_and_maybe_create_set(&arg, 1);
1508 ldlm_send_and_maybe_create_set(&arg, 0);
1510 /* In case when number of ASTs is multiply of
1511 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1512 * @arg.set must be destroyed here, otherwise we get
1513 * write memory leaking. */
1514 ptlrpc_set_destroy(arg.set);
1516 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1519 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1521 ldlm_reprocess_all(res);
1522 return LDLM_ITER_CONTINUE;
1525 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1527 struct list_head *tmp;
1534 spin_lock(&ns->ns_hash_lock);
1535 for (i = 0; i < RES_HASH_SIZE; i++) {
1536 tmp = ns->ns_hash[i].next;
1537 while (tmp != &(ns->ns_hash[i])) {
1538 struct ldlm_resource *res =
1539 list_entry(tmp, struct ldlm_resource, lr_hash);
1541 ldlm_resource_getref(res);
1542 spin_unlock(&ns->ns_hash_lock);
1543 LDLM_RESOURCE_ADDREF(res);
1545 rc = reprocess_one_queue(res, NULL);
1547 LDLM_RESOURCE_DELREF(res);
1548 spin_lock(&ns->ns_hash_lock);
1550 ldlm_resource_putref_locked(res);
1552 if (rc == LDLM_ITER_STOP)
1557 spin_unlock(&ns->ns_hash_lock);
1561 void ldlm_reprocess_all(struct ldlm_resource *res)
1563 CFS_LIST_HEAD(rpc_list);
1567 /* Local lock trees don't get reprocessed. */
1568 if (ns_is_client(res->lr_namespace)) {
1575 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1576 if (rc == LDLM_ITER_CONTINUE)
1577 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1580 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1581 if (rc == -ERESTART) {
1582 LASSERT(list_empty(&rpc_list));
1588 void ldlm_cancel_callback(struct ldlm_lock *lock)
1590 check_res_locked(lock->l_resource);
1591 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1592 lock->l_flags |= LDLM_FL_CANCEL;
1593 if (lock->l_blocking_ast) {
1594 // l_check_no_ns_lock(ns);
1595 unlock_res_and_lock(lock);
1596 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1598 lock_res_and_lock(lock);
1600 LDLM_DEBUG(lock, "no blocking ast");
1603 lock->l_flags |= LDLM_FL_BL_DONE;
1606 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1608 if (req->l_resource->lr_type != LDLM_PLAIN &&
1609 req->l_resource->lr_type != LDLM_IBITS)
1612 list_del_init(&req->l_sl_policy);
1613 list_del_init(&req->l_sl_mode);
1616 void ldlm_lock_cancel(struct ldlm_lock *lock)
1618 struct ldlm_resource *res;
1619 struct ldlm_namespace *ns;
1622 lock_res_and_lock(lock);
1624 res = lock->l_resource;
1625 ns = res->lr_namespace;
1627 /* Please do not, no matter how tempting, remove this LBUG without
1628 * talking to me first. -phik */
1629 if (lock->l_readers || lock->l_writers) {
1630 LDLM_ERROR(lock, "lock still has references");
1634 ldlm_del_waiting_lock(lock);
1636 /* Releases cancel callback. */
1637 ldlm_cancel_callback(lock);
1639 /* Yes, second time, just in case it was added again while we were
1640 running with no res lock in ldlm_cancel_callback */
1641 ldlm_del_waiting_lock(lock);
1642 ldlm_resource_unlink_lock(lock);
1643 ldlm_lock_destroy_nolock(lock);
1645 if (lock->l_granted_mode == lock->l_req_mode)
1646 ldlm_pool_del(&ns->ns_pool, lock);
1648 /* Make sure we will not be called again for same lock what is possible
1649 * if not to zero out lock->l_granted_mode */
1650 lock->l_granted_mode = LCK_MINMODE;
1651 unlock_res_and_lock(lock);
1656 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1658 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1664 lock->l_ast_data = data;
1665 LDLM_LOCK_PUT(lock);
1669 void ldlm_cancel_locks_for_export_cb(void *obj, void *data)
1671 struct obd_export *exp = data;
1672 struct ldlm_lock *lock = obj;
1673 struct ldlm_resource *res;
1675 res = ldlm_resource_getref(lock->l_resource);
1676 LDLM_LOCK_GET(lock);
1678 LDLM_DEBUG(lock, "export %p", exp);
1679 ldlm_res_lvbo_update(res, NULL, 1);
1680 ldlm_lock_cancel(lock);
1681 ldlm_reprocess_all(res);
1682 ldlm_resource_putref(res);
1683 LDLM_LOCK_RELEASE(lock);
1686 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1688 cfs_hash_for_each_empty(exp->exp_lock_hash,
1689 ldlm_cancel_locks_for_export_cb, exp);
1693 * Downgrade an exclusive lock.
1695 * A fast variant of ldlm_lock_convert for convertion of exclusive
1696 * locks. The convertion is always successful.
1698 * \param lock A lock to convert
1699 * \param new_mode new lock mode
1701 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1703 struct ldlm_namespace *ns;
1706 LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1707 LASSERT(new_mode == LCK_COS);
1709 lock_res_and_lock(lock);
1710 ldlm_resource_unlink_lock(lock);
1712 * Remove the lock from pool as it will be added again in
1713 * ldlm_grant_lock() called below.
1715 ns = lock->l_resource->lr_namespace;
1716 ldlm_pool_del(&ns->ns_pool, lock);
1718 lock->l_req_mode = new_mode;
1719 ldlm_grant_lock(lock, NULL);
1720 unlock_res_and_lock(lock);
1721 ldlm_reprocess_all(lock->l_resource);
1726 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1729 CFS_LIST_HEAD(rpc_list);
1730 struct ldlm_resource *res;
1731 struct ldlm_namespace *ns;
1734 struct sl_insert_point prev;
1736 struct ldlm_interval *node;
1739 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1740 *flags |= LDLM_FL_BLOCK_GRANTED;
1741 RETURN(lock->l_resource);
1744 /* I can't check the type of lock here because the bitlock of lock
1745 * is not held here, so do the allocation blindly. -jay */
1746 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1747 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1750 LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1751 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1753 lock_res_and_lock(lock);
1755 res = lock->l_resource;
1756 ns = res->lr_namespace;
1758 old_mode = lock->l_req_mode;
1759 lock->l_req_mode = new_mode;
1760 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1761 /* remember the lock position where the lock might be
1762 * added back to the granted list later and also
1763 * remember the join mode for skiplist fixing. */
1764 prev.res_link = lock->l_res_link.prev;
1765 prev.mode_link = lock->l_sl_mode.prev;
1766 prev.policy_link = lock->l_sl_policy.prev;
1767 ldlm_resource_unlink_lock(lock);
1769 ldlm_resource_unlink_lock(lock);
1770 if (res->lr_type == LDLM_EXTENT) {
1771 /* FIXME: ugly code, I have to attach the lock to a
1772 * interval node again since perhaps it will be granted
1774 CFS_INIT_LIST_HEAD(&node->li_group);
1775 ldlm_interval_attach(node, lock);
1781 * Remove old lock from the pool before adding the lock with new
1782 * mode below in ->policy()
1784 ldlm_pool_del(&ns->ns_pool, lock);
1786 /* If this is a local resource, put it on the appropriate list. */
1787 if (ns_is_client(res->lr_namespace)) {
1788 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1789 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1791 /* This should never happen, because of the way the
1792 * server handles conversions. */
1793 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1797 ldlm_grant_lock(lock, &rpc_list);
1799 /* FIXME: completion handling not with ns_lock held ! */
1800 if (lock->l_completion_ast)
1801 lock->l_completion_ast(lock, 0, NULL);
1805 ldlm_processing_policy policy;
1806 policy = ldlm_processing_policy_table[res->lr_type];
1807 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1808 if (rc == LDLM_ITER_STOP) {
1809 lock->l_req_mode = old_mode;
1810 if (res->lr_type == LDLM_EXTENT)
1811 ldlm_extent_add_lock(res, lock);
1813 ldlm_granted_list_add_lock(lock, &prev);
1817 *flags |= LDLM_FL_BLOCK_GRANTED;
1821 unlock_res_and_lock(lock);
1824 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1826 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1830 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1832 struct obd_device *obd = NULL;
1834 if (!((libcfs_debug | D_ERROR) & level))
1838 CDEBUG(level, " NULL LDLM lock\n");
1842 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1843 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1845 if (lock->l_conn_export != NULL)
1846 obd = lock->l_conn_export->exp_obd;
1847 if (lock->l_export && lock->l_export->exp_connection) {
1848 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1849 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1850 lock->l_remote_handle.cookie);
1851 } else if (obd == NULL) {
1852 CDEBUG(level, " Node: local\n");
1854 struct obd_import *imp = obd->u.cli.cl_import;
1855 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1856 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1857 lock->l_remote_handle.cookie);
1859 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1861 lock->l_resource->lr_name.name[0],
1862 lock->l_resource->lr_name.name[1],
1863 lock->l_resource->lr_name.name[2]);
1864 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1865 "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1866 ldlm_lockname[lock->l_granted_mode],
1867 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1869 if (lock->l_resource->lr_type == LDLM_EXTENT)
1870 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1871 " (req "LPU64"-"LPU64")\n",
1872 lock->l_policy_data.l_extent.start,
1873 lock->l_policy_data.l_extent.end,
1874 lock->l_req_extent.start, lock->l_req_extent.end);
1875 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1876 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1877 lock->l_policy_data.l_flock.pid,
1878 lock->l_policy_data.l_flock.start,
1879 lock->l_policy_data.l_flock.end);
1880 else if (lock->l_resource->lr_type == LDLM_IBITS)
1881 CDEBUG(level, " Bits: "LPX64"\n",
1882 lock->l_policy_data.l_inodebits.bits);
1885 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1887 struct ldlm_lock *lock;
1889 if (!((libcfs_debug | D_ERROR) & level))
1892 lock = ldlm_handle2lock(lockh);
1896 ldlm_lock_dump(D_OTHER, lock, 0);
1898 LDLM_LOCK_PUT(lock);
1901 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1902 struct libcfs_debug_msg_data *data, const char *fmt,
1906 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1908 va_start(args, fmt);
1910 if (lock->l_resource == NULL) {
1911 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1912 data->msg_fn, data->msg_line, fmt, args,
1913 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1914 "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
1915 LPX64" expref: %d pid: %u timeout: %lu\n", lock,
1916 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1917 lock->l_readers, lock->l_writers,
1918 ldlm_lockname[lock->l_granted_mode],
1919 ldlm_lockname[lock->l_req_mode],
1920 lock->l_flags, lock->l_remote_handle.cookie,
1922 atomic_read(&lock->l_export->exp_refcount) : -99,
1923 lock->l_pid, lock->l_callback_timeout);
1928 switch (lock->l_resource->lr_type) {
1930 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1931 data->msg_fn, data->msg_line, fmt, args,
1932 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1933 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1934 "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
1935 " expref: %d pid: %u timeout %lu\n",
1936 lock->l_resource->lr_namespace->ns_name, lock,
1937 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1938 lock->l_readers, lock->l_writers,
1939 ldlm_lockname[lock->l_granted_mode],
1940 ldlm_lockname[lock->l_req_mode],
1941 lock->l_resource->lr_name.name[0],
1942 lock->l_resource->lr_name.name[1],
1943 atomic_read(&lock->l_resource->lr_refcount),
1944 ldlm_typename[lock->l_resource->lr_type],
1945 lock->l_policy_data.l_extent.start,
1946 lock->l_policy_data.l_extent.end,
1947 lock->l_req_extent.start, lock->l_req_extent.end,
1948 lock->l_flags, lock->l_remote_handle.cookie,
1950 atomic_read(&lock->l_export->exp_refcount) : -99,
1951 lock->l_pid, lock->l_callback_timeout);
1955 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1956 data->msg_fn, data->msg_line, fmt, args,
1957 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1958 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1959 "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
1960 " expref: %d pid: %u timeout: %lu\n",
1961 lock->l_resource->lr_namespace->ns_name, lock,
1962 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1963 lock->l_readers, lock->l_writers,
1964 ldlm_lockname[lock->l_granted_mode],
1965 ldlm_lockname[lock->l_req_mode],
1966 lock->l_resource->lr_name.name[0],
1967 lock->l_resource->lr_name.name[1],
1968 atomic_read(&lock->l_resource->lr_refcount),
1969 ldlm_typename[lock->l_resource->lr_type],
1970 lock->l_policy_data.l_flock.pid,
1971 lock->l_policy_data.l_flock.start,
1972 lock->l_policy_data.l_flock.end,
1973 lock->l_flags, lock->l_remote_handle.cookie,
1975 atomic_read(&lock->l_export->exp_refcount) : -99,
1976 lock->l_pid, lock->l_callback_timeout);
1980 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1981 data->msg_fn, data->msg_line, fmt, args,
1982 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1983 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1984 "flags: "LPX64" remote: "LPX64" expref: %d "
1985 "pid: %u timeout: %lu\n",
1986 lock->l_resource->lr_namespace->ns_name,
1987 lock, lock->l_handle.h_cookie,
1988 atomic_read (&lock->l_refc),
1989 lock->l_readers, lock->l_writers,
1990 ldlm_lockname[lock->l_granted_mode],
1991 ldlm_lockname[lock->l_req_mode],
1992 lock->l_resource->lr_name.name[0],
1993 lock->l_resource->lr_name.name[1],
1994 lock->l_policy_data.l_inodebits.bits,
1995 atomic_read(&lock->l_resource->lr_refcount),
1996 ldlm_typename[lock->l_resource->lr_type],
1997 lock->l_flags, lock->l_remote_handle.cookie,
1999 atomic_read(&lock->l_export->exp_refcount) : -99,
2000 lock->l_pid, lock->l_callback_timeout);
2004 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2005 data->msg_fn, data->msg_line, fmt, args,
2006 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2007 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2008 "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
2009 lock->l_resource->lr_namespace->ns_name,
2010 lock, lock->l_handle.h_cookie,
2011 atomic_read (&lock->l_refc),
2012 lock->l_readers, lock->l_writers,
2013 ldlm_lockname[lock->l_granted_mode],
2014 ldlm_lockname[lock->l_req_mode],
2015 lock->l_resource->lr_name.name[0],
2016 lock->l_resource->lr_name.name[1],
2017 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 atomic_read(&lock->l_export->exp_refcount) : -99,
2022 lock->l_pid, lock->l_callback_timeout);
2027 EXPORT_SYMBOL(_ldlm_lock_debug);