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_put(lock->l_export);
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);
191 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 lustre_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(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
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;
377 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
378 const struct ldlm_res_id *new_resid)
380 struct ldlm_resource *oldres = lock->l_resource;
381 struct ldlm_resource *newres;
385 LASSERT(ns_is_client(ns));
387 lock_res_and_lock(lock);
388 if (memcmp(new_resid, &lock->l_resource->lr_name,
389 sizeof(lock->l_resource->lr_name)) == 0) {
391 unlock_res_and_lock(lock);
395 LASSERT(new_resid->name[0] != 0);
397 /* This function assumes that the lock isn't on any lists */
398 LASSERT(list_empty(&lock->l_res_link));
400 type = oldres->lr_type;
401 unlock_res_and_lock(lock);
403 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
404 lu_ref_add(&newres->lr_reference, "lock", lock);
408 * To flip the lock from the old to the new resource, lock, oldres and
409 * newres have to be locked. Resource spin-locks are nested within
410 * lock->l_lock, and are taken in the memory address order to avoid
413 spin_lock(&lock->l_lock);
414 oldres = lock->l_resource;
415 if (oldres < newres) {
417 lock_res_nested(newres, LRT_NEW);
420 lock_res_nested(oldres, LRT_NEW);
422 LASSERT(memcmp(new_resid, &oldres->lr_name,
423 sizeof oldres->lr_name) != 0);
424 lock->l_resource = newres;
426 unlock_res_and_lock(lock);
428 /* ...and the flowers are still standing! */
429 lu_ref_del(&oldres->lr_reference, "lock", lock);
430 ldlm_resource_putref(oldres);
439 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
441 lockh->cookie = lock->l_handle.h_cookie;
444 /* if flags: atomically get the lock and set the flags.
445 * Return NULL if flag already set
448 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
451 struct ldlm_namespace *ns;
452 struct ldlm_lock *lock, *retval = NULL;
457 lock = class_handle2object(handle->cookie);
461 LASSERT(lock->l_resource != NULL);
462 ns = lock->l_resource->lr_namespace;
465 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
466 lock_res_and_lock(lock);
468 /* It's unlikely but possible that someone marked the lock as
469 * destroyed after we did handle2object on it */
470 if (lock->l_destroyed) {
471 unlock_res_and_lock(lock);
472 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
477 if (flags && (lock->l_flags & flags)) {
478 unlock_res_and_lock(lock);
484 lock->l_flags |= flags;
486 unlock_res_and_lock(lock);
493 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
495 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
496 /* INODEBITS_INTEROP: If the other side does not support
497 * inodebits, reply with a plain lock descriptor.
499 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
500 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
501 struct ldlm_resource res = *lock->l_resource;
503 /* Make sure all the right bits are set in this lock we
504 are going to pass to client */
505 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
506 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
507 "Inappropriate inode lock bits during "
508 "conversion " LPU64 "\n",
509 lock->l_policy_data.l_inodebits.bits);
510 res.lr_type = LDLM_PLAIN;
511 ldlm_res2desc(&res, &desc->l_resource);
512 /* Convert "new" lock mode to something old client can
514 if ((lock->l_req_mode == LCK_CR) ||
515 (lock->l_req_mode == LCK_CW))
516 desc->l_req_mode = LCK_PR;
518 desc->l_req_mode = lock->l_req_mode;
519 if ((lock->l_granted_mode == LCK_CR) ||
520 (lock->l_granted_mode == LCK_CW)) {
521 desc->l_granted_mode = LCK_PR;
523 /* We never grant PW/EX locks to clients */
524 LASSERT((lock->l_granted_mode != LCK_PW) &&
525 (lock->l_granted_mode != LCK_EX));
526 desc->l_granted_mode = lock->l_granted_mode;
529 /* We do not copy policy here, because there is no
530 policy for plain locks */
532 ldlm_res2desc(lock->l_resource, &desc->l_resource);
533 desc->l_req_mode = lock->l_req_mode;
534 desc->l_granted_mode = lock->l_granted_mode;
535 desc->l_policy_data = lock->l_policy_data;
539 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
540 struct list_head *work_list)
542 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
543 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
544 lock->l_flags |= LDLM_FL_AST_SENT;
545 /* If the enqueuing client said so, tell the AST recipient to
546 * discard dirty data, rather than writing back. */
547 if (new->l_flags & LDLM_AST_DISCARD_DATA)
548 lock->l_flags |= LDLM_FL_DISCARD_DATA;
549 LASSERT(list_empty(&lock->l_bl_ast));
550 list_add(&lock->l_bl_ast, work_list);
552 LASSERT(lock->l_blocking_lock == NULL);
553 lock->l_blocking_lock = LDLM_LOCK_GET(new);
557 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
559 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
560 lock->l_flags |= LDLM_FL_CP_REQD;
561 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
562 LASSERT(list_empty(&lock->l_cp_ast));
563 list_add(&lock->l_cp_ast, work_list);
568 /* must be called with lr_lock held */
569 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
570 struct list_head *work_list)
573 check_res_locked(lock->l_resource);
575 ldlm_add_bl_work_item(lock, new, work_list);
577 ldlm_add_cp_work_item(lock, work_list);
581 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
583 struct ldlm_lock *lock;
585 lock = ldlm_handle2lock(lockh);
586 LASSERT(lock != NULL);
587 ldlm_lock_addref_internal(lock, mode);
591 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
593 ldlm_lock_remove_from_lru(lock);
594 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
596 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
598 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
600 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
603 lu_ref_add_atomic(&lock->l_reference, "user", lock);
604 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
608 * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
611 * \retval 0 success, lock was addref-ed
613 * \retval -EAGAIN lock is being canceled.
615 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
617 struct ldlm_lock *lock;
621 lock = ldlm_handle2lock(lockh);
623 lock_res_and_lock(lock);
624 if (lock->l_readers != 0 || lock->l_writers != 0 ||
625 !(lock->l_flags & LDLM_FL_CBPENDING)) {
626 ldlm_lock_addref_internal_nolock(lock, mode);
629 unlock_res_and_lock(lock);
635 /* only called for local locks */
636 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
638 lock_res_and_lock(lock);
639 ldlm_lock_addref_internal_nolock(lock, mode);
640 unlock_res_and_lock(lock);
643 /* only called in ldlm_flock_destroy and for local locks.
644 * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
645 * * ldlm_lock_remove_from_lru() does nothing, it is safe
646 * * for ldlm_flock_destroy usage by dropping some code */
647 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
649 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
650 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
651 LASSERT(lock->l_readers > 0);
652 lu_ref_del(&lock->l_reference, "reader", lock);
655 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
656 LASSERT(lock->l_writers > 0);
657 lu_ref_del(&lock->l_reference, "writer", lock);
661 lu_ref_del(&lock->l_reference, "user", lock);
662 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
665 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
667 struct ldlm_namespace *ns;
670 lock_res_and_lock(lock);
672 ns = lock->l_resource->lr_namespace;
674 ldlm_lock_decref_internal_nolock(lock, mode);
676 if (lock->l_flags & LDLM_FL_LOCAL &&
677 !lock->l_readers && !lock->l_writers) {
678 /* If this is a local lock on a server namespace and this was
679 * the last reference, cancel the lock. */
680 CDEBUG(D_INFO, "forcing cancel of local lock\n");
681 lock->l_flags |= LDLM_FL_CBPENDING;
684 if (!lock->l_readers && !lock->l_writers &&
685 (lock->l_flags & LDLM_FL_CBPENDING)) {
686 /* If we received a blocked AST and this was the last reference,
687 * run the callback. */
688 if (ns_is_server(ns) && lock->l_export)
689 CERROR("FL_CBPENDING set on non-local lock--just a "
692 LDLM_DEBUG(lock, "final decref done on cbpending lock");
694 LDLM_LOCK_GET(lock); /* dropped by bl thread */
695 ldlm_lock_remove_from_lru(lock);
696 unlock_res_and_lock(lock);
697 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
698 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
699 ldlm_handle_bl_callback(ns, NULL, lock);
700 } else if (ns_is_client(ns) &&
701 !lock->l_readers && !lock->l_writers &&
702 !(lock->l_flags & LDLM_FL_BL_AST)) {
703 /* If this is a client-side namespace and this was the last
704 * reference, put it on the LRU. */
705 ldlm_lock_add_to_lru(lock);
706 unlock_res_and_lock(lock);
707 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
708 * are not supported by the server, otherwise, it is done on
710 if (!exp_connect_cancelset(lock->l_conn_export) &&
711 !ns_connect_lru_resize(ns))
712 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
714 unlock_res_and_lock(lock);
720 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
722 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
723 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
724 ldlm_lock_decref_internal(lock, mode);
728 /* This will drop a lock reference and mark it for destruction, but will not
729 * necessarily cancel the lock before returning. */
730 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
732 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
735 LASSERT(lock != NULL);
737 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
738 lock_res_and_lock(lock);
739 lock->l_flags |= LDLM_FL_CBPENDING;
740 unlock_res_and_lock(lock);
741 ldlm_lock_decref_internal(lock, mode);
745 struct sl_insert_point {
746 struct list_head *res_link;
747 struct list_head *mode_link;
748 struct list_head *policy_link;
752 * search_granted_lock
755 * Finds a position to insert the new lock.
757 * queue [input]: the granted list where search acts on;
758 * req [input]: the lock whose position to be located;
759 * prev [output]: positions within 3 lists to insert @req to
763 * - ldlm_grant_lock_with_skiplist
765 static void search_granted_lock(struct list_head *queue,
766 struct ldlm_lock *req,
767 struct sl_insert_point *prev)
769 struct list_head *tmp;
770 struct ldlm_lock *lock, *mode_end, *policy_end;
773 list_for_each(tmp, queue) {
774 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
776 mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
779 if (lock->l_req_mode != req->l_req_mode) {
780 /* jump to last lock of mode group */
781 tmp = &mode_end->l_res_link;
785 /* suitable mode group is found */
786 if (lock->l_resource->lr_type == LDLM_PLAIN) {
787 /* insert point is last lock of the mode group */
788 prev->res_link = &mode_end->l_res_link;
789 prev->mode_link = &mode_end->l_sl_mode;
790 prev->policy_link = &req->l_sl_policy;
793 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
795 policy_end = list_entry(lock->l_sl_policy.prev,
799 if (lock->l_policy_data.l_inodebits.bits ==
800 req->l_policy_data.l_inodebits.bits) {
801 /* insert point is last lock of
802 * the policy group */
804 &policy_end->l_res_link;
806 &policy_end->l_sl_mode;
808 &policy_end->l_sl_policy;
813 if (policy_end == mode_end)
814 /* done with mode group */
817 /* go to next policy group within mode group */
818 tmp = policy_end->l_res_link.next;
819 lock = list_entry(tmp, struct ldlm_lock,
821 } /* loop over policy groups within the mode group */
823 /* insert point is last lock of the mode group,
824 * new policy group is started */
825 prev->res_link = &mode_end->l_res_link;
826 prev->mode_link = &mode_end->l_sl_mode;
827 prev->policy_link = &req->l_sl_policy;
831 LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
836 /* insert point is last lock on the queue,
837 * new mode group and new policy group are started */
838 prev->res_link = queue->prev;
839 prev->mode_link = &req->l_sl_mode;
840 prev->policy_link = &req->l_sl_policy;
845 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
846 struct sl_insert_point *prev)
848 struct ldlm_resource *res = lock->l_resource;
851 check_res_locked(res);
853 ldlm_resource_dump(D_INFO, res);
854 CDEBUG(D_OTHER, "About to add this lock:\n");
855 ldlm_lock_dump(D_OTHER, lock, 0);
857 if (lock->l_destroyed) {
858 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
862 LASSERT(list_empty(&lock->l_res_link));
863 LASSERT(list_empty(&lock->l_sl_mode));
864 LASSERT(list_empty(&lock->l_sl_policy));
866 list_add(&lock->l_res_link, prev->res_link);
867 list_add(&lock->l_sl_mode, prev->mode_link);
868 list_add(&lock->l_sl_policy, prev->policy_link);
873 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
875 struct sl_insert_point prev;
878 LASSERT(lock->l_req_mode == lock->l_granted_mode);
880 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
881 ldlm_granted_list_add_lock(lock, &prev);
886 * - ldlm_lock_enqueue
887 * - ldlm_reprocess_queue
888 * - ldlm_lock_convert
890 * must be called with lr_lock held
892 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
894 struct ldlm_resource *res = lock->l_resource;
897 check_res_locked(res);
899 lock->l_granted_mode = lock->l_req_mode;
900 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
901 ldlm_grant_lock_with_skiplist(lock);
902 else if (res->lr_type == LDLM_EXTENT)
903 ldlm_extent_add_lock(res, lock);
905 ldlm_resource_add_lock(res, &res->lr_granted, lock);
907 if (lock->l_granted_mode < res->lr_most_restr)
908 res->lr_most_restr = lock->l_granted_mode;
910 if (work_list && lock->l_completion_ast != NULL)
911 ldlm_add_ast_work_item(lock, NULL, work_list);
913 ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
917 /* returns a referenced lock or NULL. See the flag descriptions below, in the
918 * comment above ldlm_lock_match */
919 static struct ldlm_lock *search_queue(struct list_head *queue,
921 ldlm_policy_data_t *policy,
922 struct ldlm_lock *old_lock,
923 int flags, int unref)
925 struct ldlm_lock *lock;
926 struct list_head *tmp;
928 list_for_each(tmp, queue) {
931 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
933 if (lock == old_lock)
936 /* llite sometimes wants to match locks that will be
937 * canceled when their users drop, but we allow it to match
938 * if it passes in CBPENDING and the lock still has users.
939 * this is generally only going to be used by children
940 * whose parents already hold a lock so forward progress
941 * can still happen. */
942 if (lock->l_flags & LDLM_FL_CBPENDING &&
943 !(flags & LDLM_FL_CBPENDING))
945 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
946 lock->l_readers == 0 && lock->l_writers == 0)
949 if (!(lock->l_req_mode & *mode))
951 match = lock->l_req_mode;
953 if (lock->l_resource->lr_type == LDLM_EXTENT &&
954 (lock->l_policy_data.l_extent.start >
955 policy->l_extent.start ||
956 lock->l_policy_data.l_extent.end < policy->l_extent.end))
959 if (unlikely(match == LCK_GROUP) &&
960 lock->l_resource->lr_type == LDLM_EXTENT &&
961 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
964 /* We match if we have existing lock with same or wider set
966 if (lock->l_resource->lr_type == LDLM_IBITS &&
967 ((lock->l_policy_data.l_inodebits.bits &
968 policy->l_inodebits.bits) !=
969 policy->l_inodebits.bits))
973 (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED)))
976 if ((flags & LDLM_FL_LOCAL_ONLY) &&
977 !(lock->l_flags & LDLM_FL_LOCAL))
980 if (flags & LDLM_FL_TEST_LOCK) {
982 ldlm_lock_touch_in_lru(lock);
984 ldlm_lock_addref_internal_nolock(lock, match);
993 void ldlm_lock_allow_match(struct ldlm_lock *lock)
995 lock_res_and_lock(lock);
996 lock->l_flags |= LDLM_FL_LVB_READY;
997 cfs_waitq_signal(&lock->l_waitq);
998 unlock_res_and_lock(lock);
1001 /* Can be called in two ways:
1003 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1004 * for a duplicate of.
1006 * Otherwise, all of the fields must be filled in, to match against.
1008 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1009 * server (ie, connh is NULL)
1010 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1011 * list will be considered
1012 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1013 * to be canceled can still be matched as long as they still have reader
1014 * or writer refernces
1015 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1016 * just tell us if we would have matched.
1018 * Returns 1 if it finds an already-existing lock that is compatible; in this
1019 * case, lockh is filled in with a addref()ed lock
1021 * we also check security context, if that failed we simply return 0 (to keep
1022 * caller code unchanged), the context failure will be discovered by caller
1025 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1026 const struct ldlm_res_id *res_id, ldlm_type_t type,
1027 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1028 struct lustre_handle *lockh, int unref)
1030 struct ldlm_resource *res;
1031 struct ldlm_lock *lock, *old_lock = NULL;
1036 old_lock = ldlm_handle2lock(lockh);
1039 ns = old_lock->l_resource->lr_namespace;
1040 res_id = &old_lock->l_resource->lr_name;
1041 type = old_lock->l_resource->lr_type;
1042 mode = old_lock->l_req_mode;
1045 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1047 LASSERT(old_lock == NULL);
1051 LDLM_RESOURCE_ADDREF(res);
1054 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1058 if (flags & LDLM_FL_BLOCK_GRANTED)
1060 lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1064 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1072 LDLM_RESOURCE_DELREF(res);
1073 ldlm_resource_putref(res);
1076 ldlm_lock2handle(lock, lockh);
1077 if ((flags & LDLM_FL_LVB_READY) &&
1078 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1079 struct l_wait_info lwi;
1080 if (lock->l_completion_ast) {
1081 int err = lock->l_completion_ast(lock,
1082 LDLM_FL_WAIT_NOREPROC,
1085 if (flags & LDLM_FL_TEST_LOCK)
1086 LDLM_LOCK_RELEASE(lock);
1088 ldlm_lock_decref_internal(lock,
1095 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1096 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1098 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1099 l_wait_event(lock->l_waitq,
1100 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1105 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1106 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1107 res_id->name[2] : policy->l_extent.start,
1108 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1109 res_id->name[3] : policy->l_extent.end);
1111 /* check user's security context */
1112 if (lock->l_conn_export &&
1113 sptlrpc_import_check_ctx(
1114 class_exp2cliimp(lock->l_conn_export))) {
1115 if (!(flags & LDLM_FL_TEST_LOCK))
1116 ldlm_lock_decref_internal(lock, mode);
1120 if (flags & LDLM_FL_TEST_LOCK)
1121 LDLM_LOCK_RELEASE(lock);
1123 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1124 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1125 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1126 type, mode, res_id->name[0], res_id->name[1],
1127 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1128 res_id->name[2] :policy->l_extent.start,
1129 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1130 res_id->name[3] : policy->l_extent.end);
1133 LDLM_LOCK_PUT(old_lock);
1135 return rc ? mode : 0;
1138 /* Returns a referenced lock */
1139 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1140 const struct ldlm_res_id *res_id,
1143 const struct ldlm_callback_suite *cbs,
1144 void *data, __u32 lvb_len)
1146 struct ldlm_lock *lock;
1147 struct ldlm_resource *res;
1150 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1154 lock = ldlm_lock_new(res);
1155 ldlm_resource_putref(res);
1160 lock->l_req_mode = mode;
1161 lock->l_ast_data = data;
1162 lock->l_pid = cfs_curproc_pid();
1164 lock->l_blocking_ast = cbs->lcs_blocking;
1165 lock->l_completion_ast = cbs->lcs_completion;
1166 lock->l_glimpse_ast = cbs->lcs_glimpse;
1167 lock->l_weigh_ast = cbs->lcs_weigh;
1170 lock->l_tree_node = NULL;
1171 /* if this is the extent lock, allocate the interval tree node */
1172 if (type == LDLM_EXTENT) {
1173 if (ldlm_interval_alloc(lock) == NULL)
1178 lock->l_lvb_len = lvb_len;
1179 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1180 if (lock->l_lvb_data == NULL)
1187 if (lock->l_lvb_data)
1188 OBD_FREE(lock->l_lvb_data, lvb_len);
1189 ldlm_interval_free(ldlm_interval_detach(lock));
1190 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1194 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1195 struct ldlm_lock **lockp,
1196 void *cookie, int *flags)
1198 struct ldlm_lock *lock = *lockp;
1199 struct ldlm_resource *res = lock->l_resource;
1200 int local = ns_is_client(res->lr_namespace);
1201 ldlm_processing_policy policy;
1202 ldlm_error_t rc = ELDLM_OK;
1203 struct ldlm_interval *node = NULL;
1206 do_gettimeofday(&lock->l_enqueued_time);
1207 /* policies are not executed on the client or during replay */
1208 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1209 && !local && ns->ns_policy) {
1210 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1212 if (rc == ELDLM_LOCK_REPLACED) {
1213 /* The lock that was returned has already been granted,
1214 * and placed into lockp. If it's not the same as the
1215 * one we passed in, then destroy the old one and our
1216 * work here is done. */
1217 if (lock != *lockp) {
1218 ldlm_lock_destroy(lock);
1219 LDLM_LOCK_RELEASE(lock);
1221 *flags |= LDLM_FL_LOCK_CHANGED;
1223 } else if (rc != ELDLM_OK ||
1224 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1225 ldlm_lock_destroy(lock);
1230 /* For a replaying lock, it might be already in granted list. So
1231 * unlinking the lock will cause the interval node to be freed, we
1232 * have to allocate the interval node early otherwise we can't regrant
1233 * this lock in the future. - jay */
1234 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1235 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO,
1238 lock_res_and_lock(lock);
1239 if (local && lock->l_req_mode == lock->l_granted_mode) {
1240 /* The server returned a blocked lock, but it was granted
1241 * before we got a chance to actually enqueue it. We don't
1242 * need to do anything else. */
1243 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1244 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1245 GOTO(out, ELDLM_OK);
1248 ldlm_resource_unlink_lock(lock);
1249 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1251 ldlm_lock_destroy_nolock(lock);
1252 GOTO(out, rc = -ENOMEM);
1255 CFS_INIT_LIST_HEAD(&node->li_group);
1256 ldlm_interval_attach(node, lock);
1260 /* Some flags from the enqueue want to make it into the AST, via the
1261 * lock's l_flags. */
1262 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1264 /* This distinction between local lock trees is very important; a client
1265 * namespace only has information about locks taken by that client, and
1266 * thus doesn't have enough information to decide for itself if it can
1267 * be granted (below). In this case, we do exactly what the server
1268 * tells us to do, as dictated by the 'flags'.
1270 * We do exactly the same thing during recovery, when the server is
1271 * more or less trusting the clients not to lie.
1273 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1274 * granted/converting queues. */
1276 if (*flags & LDLM_FL_BLOCK_CONV)
1277 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1278 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1279 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1281 ldlm_grant_lock(lock, NULL);
1282 GOTO(out, ELDLM_OK);
1283 } else if (*flags & LDLM_FL_REPLAY) {
1284 if (*flags & LDLM_FL_BLOCK_CONV) {
1285 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1286 GOTO(out, ELDLM_OK);
1287 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1288 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1289 GOTO(out, ELDLM_OK);
1290 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1291 ldlm_grant_lock(lock, NULL);
1292 GOTO(out, ELDLM_OK);
1294 /* If no flags, fall through to normal enqueue path. */
1297 policy = ldlm_processing_policy_table[res->lr_type];
1298 policy(lock, flags, 1, &rc, NULL);
1301 unlock_res_and_lock(lock);
1303 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1307 /* Must be called with namespace taken: queue is waiting or converting. */
1308 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1309 struct list_head *work_list)
1311 struct list_head *tmp, *pos;
1312 ldlm_processing_policy policy;
1314 int rc = LDLM_ITER_CONTINUE;
1318 check_res_locked(res);
1320 policy = ldlm_processing_policy_table[res->lr_type];
1323 list_for_each_safe(tmp, pos, queue) {
1324 struct ldlm_lock *pending;
1325 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1327 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1330 rc = policy(pending, &flags, 0, &err, work_list);
1331 if (rc != LDLM_ITER_CONTINUE)
1338 /* Helper function for ldlm_run_ast_work().
1340 * Send an existing rpc set specified by @arg->set and then
1341 * destroy it. Create new one if @do_create flag is set. */
1343 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1347 ptlrpc_set_wait(arg->set);
1348 if (arg->type == LDLM_BL_CALLBACK)
1349 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1350 ptlrpc_set_destroy(arg->set);
1353 arg->set = ptlrpc_prep_set();
1359 ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1361 struct ldlm_lock_desc d;
1362 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
1365 /* nobody should touch l_bl_ast */
1366 lock_res_and_lock(lock);
1367 list_del_init(&lock->l_bl_ast);
1369 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1370 LASSERT(lock->l_bl_ast_run == 0);
1371 LASSERT(lock->l_blocking_lock);
1372 lock->l_bl_ast_run++;
1373 unlock_res_and_lock(lock);
1375 ldlm_lock2desc(lock->l_blocking_lock, &d);
1377 lock->l_blocking_ast(lock, &d, (void *)arg,
1379 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1380 lock->l_blocking_lock = NULL;
1381 LDLM_LOCK_RELEASE(lock);
1387 ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1389 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
1390 ldlm_completion_callback completion_callback;
1394 /* It's possible to receive a completion AST before we've set
1395 * the l_completion_ast pointer: either because the AST arrived
1396 * before the reply, or simply because there's a small race
1397 * window between receiving the reply and finishing the local
1398 * enqueue. (bug 842)
1400 * This can't happen with the blocking_ast, however, because we
1401 * will never call the local blocking_ast until we drop our
1402 * reader/writer reference, which we won't do until we get the
1403 * reply and finish enqueueing. */
1405 /* nobody should touch l_cp_ast */
1406 lock_res_and_lock(lock);
1407 list_del_init(&lock->l_cp_ast);
1408 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1409 /* save l_completion_ast since it can be changed by
1410 * mds_intent_policy(), see bug 14225 */
1411 completion_callback = lock->l_completion_ast;
1412 lock->l_flags &= ~LDLM_FL_CP_REQD;
1413 unlock_res_and_lock(lock);
1415 if (completion_callback != NULL) {
1416 completion_callback(lock, 0, (void *)arg);
1419 LDLM_LOCK_RELEASE(lock);
1425 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1427 struct ldlm_lock_desc desc;
1428 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_rk_ast);
1431 list_del_init(&lock->l_rk_ast);
1433 /* the desc just pretend to exclusive */
1434 ldlm_lock2desc(lock, &desc);
1435 desc.l_req_mode = LCK_EX;
1436 desc.l_granted_mode = 0;
1438 lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1439 LDLM_LOCK_RELEASE(lock);
1444 int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
1446 struct ldlm_cb_set_arg arg;
1447 struct list_head *tmp, *pos;
1448 int (*work_ast_lock)(struct list_head *tmp,struct ldlm_cb_set_arg *arg);
1452 arg.set = ptlrpc_prep_set();
1453 atomic_set(&arg.restart, 0);
1455 case LDLM_WORK_BL_AST:
1456 arg.type = LDLM_BL_CALLBACK;
1457 work_ast_lock = ldlm_work_bl_ast_lock;
1459 case LDLM_WORK_CP_AST:
1460 arg.type = LDLM_CP_CALLBACK;
1461 work_ast_lock = ldlm_work_cp_ast_lock;
1463 case LDLM_WORK_REVOKE_AST:
1464 arg.type = LDLM_BL_CALLBACK;
1465 work_ast_lock = ldlm_work_revoke_ast_lock;
1472 list_for_each_safe(tmp, pos, rpc_list) {
1473 ast_count += work_ast_lock(tmp, &arg);
1475 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1476 * and create a new set for requests that remained in
1478 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1479 ldlm_send_and_maybe_create_set(&arg, 1);
1485 ldlm_send_and_maybe_create_set(&arg, 0);
1487 /* In case when number of ASTs is multiply of
1488 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1489 * @arg.set must be destroyed here, otherwise we get
1490 * write memory leaking. */
1491 ptlrpc_set_destroy(arg.set);
1493 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1496 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1498 ldlm_reprocess_all(res);
1499 return LDLM_ITER_CONTINUE;
1502 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1504 struct list_head *tmp;
1511 spin_lock(&ns->ns_hash_lock);
1512 for (i = 0; i < RES_HASH_SIZE; i++) {
1513 tmp = ns->ns_hash[i].next;
1514 while (tmp != &(ns->ns_hash[i])) {
1515 struct ldlm_resource *res =
1516 list_entry(tmp, struct ldlm_resource, lr_hash);
1518 ldlm_resource_getref(res);
1519 spin_unlock(&ns->ns_hash_lock);
1520 LDLM_RESOURCE_ADDREF(res);
1522 rc = reprocess_one_queue(res, NULL);
1524 LDLM_RESOURCE_DELREF(res);
1525 spin_lock(&ns->ns_hash_lock);
1527 ldlm_resource_putref_locked(res);
1529 if (rc == LDLM_ITER_STOP)
1534 spin_unlock(&ns->ns_hash_lock);
1538 void ldlm_reprocess_all(struct ldlm_resource *res)
1540 CFS_LIST_HEAD(rpc_list);
1544 /* Local lock trees don't get reprocessed. */
1545 if (ns_is_client(res->lr_namespace)) {
1552 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1553 if (rc == LDLM_ITER_CONTINUE)
1554 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1557 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1558 if (rc == -ERESTART) {
1559 LASSERT(list_empty(&rpc_list));
1565 void ldlm_cancel_callback(struct ldlm_lock *lock)
1567 check_res_locked(lock->l_resource);
1568 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1569 lock->l_flags |= LDLM_FL_CANCEL;
1570 if (lock->l_blocking_ast) {
1571 // l_check_no_ns_lock(ns);
1572 unlock_res_and_lock(lock);
1573 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1575 lock_res_and_lock(lock);
1577 LDLM_DEBUG(lock, "no blocking ast");
1580 lock->l_flags |= LDLM_FL_BL_DONE;
1583 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1585 if (req->l_resource->lr_type != LDLM_PLAIN &&
1586 req->l_resource->lr_type != LDLM_IBITS)
1589 list_del_init(&req->l_sl_policy);
1590 list_del_init(&req->l_sl_mode);
1593 void ldlm_lock_cancel(struct ldlm_lock *lock)
1595 struct ldlm_resource *res;
1596 struct ldlm_namespace *ns;
1599 lock_res_and_lock(lock);
1601 res = lock->l_resource;
1602 ns = res->lr_namespace;
1604 /* Please do not, no matter how tempting, remove this LBUG without
1605 * talking to me first. -phik */
1606 if (lock->l_readers || lock->l_writers) {
1607 LDLM_ERROR(lock, "lock still has references");
1611 ldlm_del_waiting_lock(lock);
1613 /* Releases cancel callback. */
1614 ldlm_cancel_callback(lock);
1616 /* Yes, second time, just in case it was added again while we were
1617 running with no res lock in ldlm_cancel_callback */
1618 ldlm_del_waiting_lock(lock);
1619 ldlm_resource_unlink_lock(lock);
1620 ldlm_lock_destroy_nolock(lock);
1622 if (lock->l_granted_mode == lock->l_req_mode)
1623 ldlm_pool_del(&ns->ns_pool, lock);
1625 /* Make sure we will not be called again for same lock what is possible
1626 * if not to zero out lock->l_granted_mode */
1627 lock->l_granted_mode = LCK_MINMODE;
1628 unlock_res_and_lock(lock);
1633 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1635 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1641 lock->l_ast_data = data;
1642 LDLM_LOCK_PUT(lock);
1646 void ldlm_cancel_locks_for_export_cb(void *obj, void *data)
1648 struct obd_export *exp = data;
1649 struct ldlm_lock *lock = obj;
1650 struct ldlm_resource *res;
1652 res = ldlm_resource_getref(lock->l_resource);
1653 LDLM_LOCK_GET(lock);
1655 LDLM_DEBUG(lock, "export %p", exp);
1656 ldlm_res_lvbo_update(res, NULL, 0, 1);
1657 ldlm_lock_cancel(lock);
1658 ldlm_reprocess_all(res);
1659 ldlm_resource_putref(res);
1660 LDLM_LOCK_RELEASE(lock);
1663 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1665 lustre_hash_for_each_empty(exp->exp_lock_hash,
1666 ldlm_cancel_locks_for_export_cb, exp);
1670 * Downgrade an exclusive lock.
1672 * A fast variant of ldlm_lock_convert for convertion of exclusive
1673 * locks. The convertion is always successful.
1675 * \param lock A lock to convert
1676 * \param new_mode new lock mode
1678 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1680 struct ldlm_namespace *ns;
1683 LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1684 LASSERT(new_mode == LCK_COS);
1686 lock_res_and_lock(lock);
1687 ldlm_resource_unlink_lock(lock);
1689 * Remove the lock from pool as it will be added again in
1690 * ldlm_grant_lock() called below.
1692 ns = lock->l_resource->lr_namespace;
1693 ldlm_pool_del(&ns->ns_pool, lock);
1695 lock->l_req_mode = new_mode;
1696 ldlm_grant_lock(lock, NULL);
1697 unlock_res_and_lock(lock);
1698 ldlm_reprocess_all(lock->l_resource);
1703 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1706 CFS_LIST_HEAD(rpc_list);
1707 struct ldlm_resource *res;
1708 struct ldlm_namespace *ns;
1711 struct sl_insert_point prev;
1713 struct ldlm_interval *node;
1716 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1717 *flags |= LDLM_FL_BLOCK_GRANTED;
1718 RETURN(lock->l_resource);
1721 /* I can't check the type of lock here because the bitlock of lock
1722 * is not held here, so do the allocation blindly. -jay */
1723 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
1724 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1727 LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1728 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1730 lock_res_and_lock(lock);
1732 res = lock->l_resource;
1733 ns = res->lr_namespace;
1735 old_mode = lock->l_req_mode;
1736 lock->l_req_mode = new_mode;
1737 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1738 /* remember the lock position where the lock might be
1739 * added back to the granted list later and also
1740 * remember the join mode for skiplist fixing. */
1741 prev.res_link = lock->l_res_link.prev;
1742 prev.mode_link = lock->l_sl_mode.prev;
1743 prev.policy_link = lock->l_sl_policy.prev;
1744 ldlm_resource_unlink_lock(lock);
1746 ldlm_resource_unlink_lock(lock);
1747 if (res->lr_type == LDLM_EXTENT) {
1748 /* FIXME: ugly code, I have to attach the lock to a
1749 * interval node again since perhaps it will be granted
1751 CFS_INIT_LIST_HEAD(&node->li_group);
1752 ldlm_interval_attach(node, lock);
1758 * Remove old lock from the pool before adding the lock with new
1759 * mode below in ->policy()
1761 ldlm_pool_del(&ns->ns_pool, lock);
1763 /* If this is a local resource, put it on the appropriate list. */
1764 if (ns_is_client(res->lr_namespace)) {
1765 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1766 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1768 /* This should never happen, because of the way the
1769 * server handles conversions. */
1770 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1774 ldlm_grant_lock(lock, &rpc_list);
1776 /* FIXME: completion handling not with ns_lock held ! */
1777 if (lock->l_completion_ast)
1778 lock->l_completion_ast(lock, 0, NULL);
1782 ldlm_processing_policy policy;
1783 policy = ldlm_processing_policy_table[res->lr_type];
1784 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1785 if (rc == LDLM_ITER_STOP) {
1786 lock->l_req_mode = old_mode;
1787 if (res->lr_type == LDLM_EXTENT)
1788 ldlm_extent_add_lock(res, lock);
1790 ldlm_granted_list_add_lock(lock, &prev);
1794 *flags |= LDLM_FL_BLOCK_GRANTED;
1798 unlock_res_and_lock(lock);
1801 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1803 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1807 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1809 struct obd_device *obd = NULL;
1811 if (!((libcfs_debug | D_ERROR) & level))
1815 CDEBUG(level, " NULL LDLM lock\n");
1819 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1820 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1822 if (lock->l_conn_export != NULL)
1823 obd = lock->l_conn_export->exp_obd;
1824 if (lock->l_export && lock->l_export->exp_connection) {
1825 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1826 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1827 lock->l_remote_handle.cookie);
1828 } else if (obd == NULL) {
1829 CDEBUG(level, " Node: local\n");
1831 struct obd_import *imp = obd->u.cli.cl_import;
1832 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1833 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1834 lock->l_remote_handle.cookie);
1836 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1838 lock->l_resource->lr_name.name[0],
1839 lock->l_resource->lr_name.name[1],
1840 lock->l_resource->lr_name.name[2]);
1841 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1842 "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
1843 ldlm_lockname[lock->l_granted_mode],
1844 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1846 if (lock->l_resource->lr_type == LDLM_EXTENT)
1847 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1848 " (req "LPU64"-"LPU64")\n",
1849 lock->l_policy_data.l_extent.start,
1850 lock->l_policy_data.l_extent.end,
1851 lock->l_req_extent.start, lock->l_req_extent.end);
1852 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1853 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1854 lock->l_policy_data.l_flock.pid,
1855 lock->l_policy_data.l_flock.start,
1856 lock->l_policy_data.l_flock.end);
1857 else if (lock->l_resource->lr_type == LDLM_IBITS)
1858 CDEBUG(level, " Bits: "LPX64"\n",
1859 lock->l_policy_data.l_inodebits.bits);
1862 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1864 struct ldlm_lock *lock;
1866 if (!((libcfs_debug | D_ERROR) & level))
1869 lock = ldlm_handle2lock(lockh);
1873 ldlm_lock_dump(D_OTHER, lock, 0);
1875 LDLM_LOCK_PUT(lock);
1878 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1879 struct libcfs_debug_msg_data *data, const char *fmt,
1883 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1885 va_start(args, fmt);
1887 if (lock->l_resource == NULL) {
1888 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1889 data->msg_fn, data->msg_line, fmt, args,
1890 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1891 "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
1892 LPX64" expref: %d pid: %u timeout: %lu\n", lock,
1893 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1894 lock->l_readers, lock->l_writers,
1895 ldlm_lockname[lock->l_granted_mode],
1896 ldlm_lockname[lock->l_req_mode],
1897 lock->l_flags, lock->l_remote_handle.cookie,
1899 atomic_read(&lock->l_export->exp_refcount) : -99,
1900 lock->l_pid, lock->l_callback_timeout);
1905 switch (lock->l_resource->lr_type) {
1907 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1908 data->msg_fn, data->msg_line, fmt, args,
1909 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1910 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1911 "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
1912 " expref: %d pid: %u timeout %lu\n",
1913 lock->l_resource->lr_namespace->ns_name, lock,
1914 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1915 lock->l_readers, lock->l_writers,
1916 ldlm_lockname[lock->l_granted_mode],
1917 ldlm_lockname[lock->l_req_mode],
1918 lock->l_resource->lr_name.name[0],
1919 lock->l_resource->lr_name.name[1],
1920 atomic_read(&lock->l_resource->lr_refcount),
1921 ldlm_typename[lock->l_resource->lr_type],
1922 lock->l_policy_data.l_extent.start,
1923 lock->l_policy_data.l_extent.end,
1924 lock->l_req_extent.start, lock->l_req_extent.end,
1925 lock->l_flags, lock->l_remote_handle.cookie,
1927 atomic_read(&lock->l_export->exp_refcount) : -99,
1928 lock->l_pid, lock->l_callback_timeout);
1932 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1933 data->msg_fn, data->msg_line, fmt, args,
1934 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1935 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1936 "["LPU64"->"LPU64"] flags: %x remote: "LPX64
1937 " expref: %d pid: %u timeout: %lu\n",
1938 lock->l_resource->lr_namespace->ns_name, lock,
1939 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1940 lock->l_readers, lock->l_writers,
1941 ldlm_lockname[lock->l_granted_mode],
1942 ldlm_lockname[lock->l_req_mode],
1943 lock->l_resource->lr_name.name[0],
1944 lock->l_resource->lr_name.name[1],
1945 atomic_read(&lock->l_resource->lr_refcount),
1946 ldlm_typename[lock->l_resource->lr_type],
1947 lock->l_policy_data.l_flock.pid,
1948 lock->l_policy_data.l_flock.start,
1949 lock->l_policy_data.l_flock.end,
1950 lock->l_flags, lock->l_remote_handle.cookie,
1952 atomic_read(&lock->l_export->exp_refcount) : -99,
1953 lock->l_pid, lock->l_callback_timeout);
1957 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1958 data->msg_fn, data->msg_line, fmt, args,
1959 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1960 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1961 "flags: %x remote: "LPX64" expref: %d "
1962 "pid: %u timeout: %lu\n",
1963 lock->l_resource->lr_namespace->ns_name,
1964 lock, lock->l_handle.h_cookie,
1965 atomic_read (&lock->l_refc),
1966 lock->l_readers, lock->l_writers,
1967 ldlm_lockname[lock->l_granted_mode],
1968 ldlm_lockname[lock->l_req_mode],
1969 lock->l_resource->lr_name.name[0],
1970 lock->l_resource->lr_name.name[1],
1971 lock->l_policy_data.l_inodebits.bits,
1972 atomic_read(&lock->l_resource->lr_refcount),
1973 ldlm_typename[lock->l_resource->lr_type],
1974 lock->l_flags, lock->l_remote_handle.cookie,
1976 atomic_read(&lock->l_export->exp_refcount) : -99,
1977 lock->l_pid, lock->l_callback_timeout);
1981 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1982 data->msg_fn, data->msg_line, fmt, args,
1983 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1984 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
1985 "remote: "LPX64" expref: %d 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 atomic_read(&lock->l_resource->lr_refcount),
1995 ldlm_typename[lock->l_resource->lr_type],
1996 lock->l_flags, lock->l_remote_handle.cookie,
1998 atomic_read(&lock->l_export->exp_refcount) : -99,
1999 lock->l_pid, lock->l_callback_timeout);
2004 EXPORT_SYMBOL(_ldlm_lock_debug);