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 # include <linux/lustre_intent.h>
48 # include <liblustre.h>
49 # include <libcfs/kp30.h>
52 #include <obd_class.h>
53 #include "ldlm_internal.h"
55 //struct lustre_lock ldlm_everything_lock;
58 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;
154 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing it.");
156 res = lock->l_resource;
157 LASSERT(lock->l_destroyed);
158 LASSERT(list_empty(&lock->l_res_link));
159 LASSERT(list_empty(&lock->l_pending_chain));
161 atomic_dec(&res->lr_namespace->ns_locks);
162 ldlm_resource_putref(res);
163 lock->l_resource = NULL;
164 if (lock->l_export) {
165 class_export_put(lock->l_export);
166 lock->l_export = NULL;
169 if (lock->l_lvb_data != NULL)
170 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
172 ldlm_interval_free(ldlm_interval_detach(lock));
173 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
180 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
183 if (!list_empty(&lock->l_lru)) {
184 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
185 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
186 list_del_init(&lock->l_lru);
187 LASSERT(ns->ns_nr_unused > 0);
194 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
196 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
199 spin_lock(&ns->ns_unused_lock);
200 rc = ldlm_lock_remove_from_lru_nolock(lock);
201 spin_unlock(&ns->ns_unused_lock);
206 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
208 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
209 lock->l_last_used = cfs_time_current();
210 LASSERT(list_empty(&lock->l_lru));
211 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
212 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
213 LASSERT(ns->ns_nr_unused >= 0);
217 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
219 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
221 spin_lock(&ns->ns_unused_lock);
222 ldlm_lock_add_to_lru_nolock(lock);
223 spin_unlock(&ns->ns_unused_lock);
227 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
229 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
231 spin_lock(&ns->ns_unused_lock);
232 if (!list_empty(&lock->l_lru)) {
233 ldlm_lock_remove_from_lru_nolock(lock);
234 ldlm_lock_add_to_lru_nolock(lock);
236 spin_unlock(&ns->ns_unused_lock);
240 /* This used to have a 'strict' flag, which recovery would use to mark an
241 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
242 * shall explain why it's gone: with the new hash table scheme, once you call
243 * ldlm_lock_destroy, you can never drop your final references on this lock.
244 * Because it's not in the hash table anymore. -phil */
245 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
249 if (lock->l_readers || lock->l_writers) {
250 LDLM_ERROR(lock, "lock still has references");
251 ldlm_lock_dump(D_ERROR, lock, 0);
255 if (!list_empty(&lock->l_res_link)) {
256 LDLM_ERROR(lock, "lock still on resource");
257 ldlm_lock_dump(D_ERROR, lock, 0);
261 if (lock->l_destroyed) {
262 LASSERT(list_empty(&lock->l_lru));
266 lock->l_destroyed = 1;
268 if (lock->l_export && lock->l_export->exp_lock_hash)
269 lustre_hash_del(lock->l_export->exp_lock_hash,
270 &lock->l_remote_handle, &lock->l_exp_hash);
272 ldlm_lock_remove_from_lru(lock);
273 class_handle_unhash(&lock->l_handle);
276 /* Wake anyone waiting for this lock */
277 /* FIXME: I should probably add yet another flag, instead of using
278 * l_export to only call this on clients */
280 class_export_put(lock->l_export);
281 lock->l_export = NULL;
282 if (lock->l_export && lock->l_completion_ast)
283 lock->l_completion_ast(lock, 0);
289 void ldlm_lock_destroy(struct ldlm_lock *lock)
293 lock_res_and_lock(lock);
294 first = ldlm_lock_destroy_internal(lock);
295 unlock_res_and_lock(lock);
297 /* drop reference from hashtable only for first destroy */
303 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
307 first = ldlm_lock_destroy_internal(lock);
308 /* drop reference from hashtable only for first destroy */
314 /* this is called by portals_handle2object with the handle lock taken */
315 static void lock_handle_addref(void *lock)
317 LDLM_LOCK_GET((struct ldlm_lock *)lock);
321 * usage: pass in a resource on which you have done ldlm_resource_get
322 * pass in a parent lock on which you have done a ldlm_lock_get
323 * after return, ldlm_*_put the resource and parent
324 * returns: lock with refcount 2 - one for current caller and one for remote
326 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
328 struct ldlm_lock *lock;
331 if (resource == NULL)
334 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
338 spin_lock_init(&lock->l_lock);
339 lock->l_resource = ldlm_resource_getref(resource);
341 atomic_set(&lock->l_refc, 2);
342 CFS_INIT_LIST_HEAD(&lock->l_res_link);
343 CFS_INIT_LIST_HEAD(&lock->l_lru);
344 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
345 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
346 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
347 cfs_waitq_init(&lock->l_waitq);
348 lock->l_blocking_lock = NULL;
349 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
350 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
351 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
353 atomic_inc(&resource->lr_namespace->ns_locks);
354 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
355 class_handle_hash(&lock->l_handle, lock_handle_addref);
357 CFS_INIT_LIST_HEAD(&lock->l_extents_list);
358 spin_lock_init(&lock->l_extents_list_lock);
359 CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
360 lock->l_callback_timeout = 0;
365 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
366 struct ldlm_res_id new_resid)
368 struct ldlm_resource *oldres = lock->l_resource;
369 struct ldlm_resource *newres;
373 LASSERT(ns_is_client(ns));
375 lock_res_and_lock(lock);
376 if (memcmp(&new_resid, &lock->l_resource->lr_name,
377 sizeof(lock->l_resource->lr_name)) == 0) {
379 unlock_res_and_lock(lock);
383 LASSERT(new_resid.name[0] != 0);
385 /* This function assumes that the lock isn't on any lists */
386 LASSERT(list_empty(&lock->l_res_link));
388 type = oldres->lr_type;
389 unlock_res_and_lock(lock);
391 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
395 lock_res_and_lock(lock);
396 LASSERT(memcmp(&new_resid, &lock->l_resource->lr_name,
397 sizeof(lock->l_resource->lr_name)) != 0);
399 lock->l_resource = newres;
401 unlock_res_and_lock(lock);
403 /* ...and the flowers are still standing! */
404 ldlm_resource_putref(oldres);
413 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
415 lockh->cookie = lock->l_handle.h_cookie;
418 /* if flags: atomically get the lock and set the flags.
419 * Return NULL if flag already set
422 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
424 struct ldlm_namespace *ns;
425 struct ldlm_lock *lock = NULL, *retval = NULL;
430 lock = class_handle2object(handle->cookie);
434 LASSERT(lock->l_resource != NULL);
435 ns = lock->l_resource->lr_namespace;
438 lock_res_and_lock(lock);
440 /* It's unlikely but possible that someone marked the lock as
441 * destroyed after we did handle2object on it */
442 if (lock->l_destroyed) {
443 unlock_res_and_lock(lock);
444 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
449 if (flags && (lock->l_flags & flags)) {
450 unlock_res_and_lock(lock);
456 lock->l_flags |= flags;
458 unlock_res_and_lock(lock);
465 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
466 struct lustre_handle *handle)
468 struct ldlm_lock *retval = NULL;
469 retval = __ldlm_handle2lock(handle, 0);
473 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
475 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
476 /* INODEBITS_INTEROP: If the other side does not support
477 * inodebits, reply with a plain lock descriptor.
479 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
480 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
482 /* Make sure all the right bits are set in this lock we
483 are going to pass to client */
484 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
485 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
486 "Inappropriate inode lock bits during "
487 "conversion " LPU64 "\n",
488 lock->l_policy_data.l_inodebits.bits);
489 ldlm_res2desc(lock->l_resource, &desc->l_resource);
490 desc->l_resource.lr_type = LDLM_PLAIN;
491 /* Convert "new" lock mode to something old client can
493 if ((lock->l_req_mode == LCK_CR) ||
494 (lock->l_req_mode == LCK_CW))
495 desc->l_req_mode = LCK_PR;
497 desc->l_req_mode = lock->l_req_mode;
498 if ((lock->l_granted_mode == LCK_CR) ||
499 (lock->l_granted_mode == LCK_CW)) {
500 desc->l_granted_mode = LCK_PR;
502 /* We never grant PW/EX locks to clients */
503 LASSERT((lock->l_granted_mode != LCK_PW) &&
504 (lock->l_granted_mode != LCK_EX));
505 desc->l_granted_mode = lock->l_granted_mode;
508 /* We do not copy policy here, because there is no
509 policy for plain locks */
511 ldlm_res2desc(lock->l_resource, &desc->l_resource);
512 desc->l_req_mode = lock->l_req_mode;
513 desc->l_granted_mode = lock->l_granted_mode;
514 desc->l_policy_data = lock->l_policy_data;
518 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
519 struct list_head *work_list)
521 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
522 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
523 lock->l_flags |= LDLM_FL_AST_SENT;
524 /* If the enqueuing client said so, tell the AST recipient to
525 * discard dirty data, rather than writing back. */
526 if (new->l_flags & LDLM_AST_DISCARD_DATA)
527 lock->l_flags |= LDLM_FL_DISCARD_DATA;
528 LASSERT(list_empty(&lock->l_bl_ast));
529 list_add(&lock->l_bl_ast, work_list);
531 LASSERT(lock->l_blocking_lock == NULL);
532 lock->l_blocking_lock = LDLM_LOCK_GET(new);
536 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
538 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
539 lock->l_flags |= LDLM_FL_CP_REQD;
540 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
541 LASSERT(list_empty(&lock->l_cp_ast));
542 list_add(&lock->l_cp_ast, work_list);
547 /* must be called with lr_lock held */
548 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
549 struct list_head *work_list)
552 check_res_locked(lock->l_resource);
554 ldlm_add_bl_work_item(lock, new, work_list);
556 ldlm_add_cp_work_item(lock, work_list);
560 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
562 struct ldlm_lock *lock;
564 lock = ldlm_handle2lock(lockh);
565 LASSERT(lock != NULL);
566 ldlm_lock_addref_internal(lock, mode);
570 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
572 ldlm_lock_remove_from_lru(lock);
573 if (mode & (LCK_NL | LCK_CR | LCK_PR))
575 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
578 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
581 /* only called for local locks */
582 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
584 lock_res_and_lock(lock);
585 ldlm_lock_addref_internal_nolock(lock, mode);
586 unlock_res_and_lock(lock);
589 /* only called in ldlm_flock_destroy and for local locks.
590 * for LDLM_FLOCK type locks, l_blocking_ast is null, and
591 * ldlm_lock_remove_from_lru() does nothing, it is safe
592 * for ldlm_flock_destroy usage by dropping some code */
593 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
595 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
596 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
597 LASSERT(lock->l_readers > 0);
600 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
601 LASSERT(lock->l_writers > 0);
605 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
608 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
610 struct ldlm_namespace *ns;
613 lock_res_and_lock(lock);
615 ns = lock->l_resource->lr_namespace;
617 ldlm_lock_decref_internal_nolock(lock, mode);
619 if (lock->l_flags & LDLM_FL_LOCAL &&
620 !lock->l_readers && !lock->l_writers) {
621 /* If this is a local lock on a server namespace and this was
622 * the last reference, cancel the lock. */
623 CDEBUG(D_INFO, "forcing cancel of local lock\n");
624 lock->l_flags |= LDLM_FL_CBPENDING;
627 if (!lock->l_readers && !lock->l_writers &&
628 (lock->l_flags & LDLM_FL_CBPENDING)) {
629 /* If we received a blocked AST and this was the last reference,
630 * run the callback. */
631 if (ns_is_server(ns) && lock->l_export)
632 CERROR("FL_CBPENDING set on non-local lock--just a "
635 LDLM_DEBUG(lock, "final decref done on cbpending lock");
637 LDLM_LOCK_GET(lock); /* dropped by bl thread */
638 ldlm_lock_remove_from_lru(lock);
639 unlock_res_and_lock(lock);
641 if (lock->l_flags & LDLM_FL_FAIL_LOC)
642 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
644 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
645 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
646 ldlm_handle_bl_callback(ns, NULL, lock);
647 } else if (ns_is_client(ns) &&
648 !lock->l_readers && !lock->l_writers &&
649 !(lock->l_flags & LDLM_FL_NO_LRU) &&
650 !(lock->l_flags & LDLM_FL_BL_AST)) {
651 /* If this is a client-side namespace and this was the last
652 * reference, put it on the LRU. */
653 ldlm_lock_add_to_lru(lock);
654 unlock_res_and_lock(lock);
656 if (lock->l_flags & LDLM_FL_FAIL_LOC)
657 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
659 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
660 * are not supported by the server, otherwise, it is done on
662 if (!exp_connect_cancelset(lock->l_conn_export) &&
663 !ns_connect_lru_resize(ns))
664 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
666 unlock_res_and_lock(lock);
672 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
674 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
675 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
676 ldlm_lock_decref_internal(lock, mode);
680 /* This will drop a lock reference and mark it for destruction, but will not
681 * necessarily cancel the lock before returning. */
682 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
684 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
687 LASSERT(lock != NULL);
689 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
690 lock_res_and_lock(lock);
691 lock->l_flags |= LDLM_FL_CBPENDING;
692 unlock_res_and_lock(lock);
693 ldlm_lock_decref_internal(lock, mode);
697 struct sl_insert_point {
698 struct list_head *res_link;
699 struct list_head *mode_link;
700 struct list_head *policy_link;
704 * search_granted_lock
707 * Finds a position to insert the new lock.
709 * queue [input]: the granted list where search acts on;
710 * req [input]: the lock whose position to be located;
711 * prev [output]: positions within 3 lists to insert @req to
715 * - ldlm_grant_lock_with_skiplist
717 static void search_granted_lock(struct list_head *queue,
718 struct ldlm_lock *req,
719 struct sl_insert_point *prev)
721 struct list_head *tmp;
722 struct ldlm_lock *lock, *mode_end, *policy_end;
725 list_for_each(tmp, queue) {
726 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
728 mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
731 if (lock->l_req_mode != req->l_req_mode) {
732 /* jump to last lock of mode group */
733 tmp = &mode_end->l_res_link;
737 /* suitable mode group is found */
738 if (lock->l_resource->lr_type == LDLM_PLAIN) {
739 /* insert point is last lock of the mode group */
740 prev->res_link = &mode_end->l_res_link;
741 prev->mode_link = &mode_end->l_sl_mode;
742 prev->policy_link = &req->l_sl_policy;
745 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
747 policy_end = list_entry(lock->l_sl_policy.prev,
751 if (lock->l_policy_data.l_inodebits.bits ==
752 req->l_policy_data.l_inodebits.bits) {
753 /* insert point is last lock of
754 * the policy group */
756 &policy_end->l_res_link;
758 &policy_end->l_sl_mode;
760 &policy_end->l_sl_policy;
765 if (policy_end == mode_end)
766 /* done with mode group */
769 /* jump to next policy group within the mode group */
770 tmp = policy_end->l_res_link.next;
771 lock = list_entry(tmp, struct ldlm_lock,
773 } /* loop over policy groups within the mode group */
775 /* insert point is last lock of the mode group,
776 * new policy group is started */
777 prev->res_link = &mode_end->l_res_link;
778 prev->mode_link = &mode_end->l_sl_mode;
779 prev->policy_link = &req->l_sl_policy;
783 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
788 /* insert point is last lock on the queue,
789 * new mode group and new policy group are started */
790 prev->res_link = queue->prev;
791 prev->mode_link = &req->l_sl_mode;
792 prev->policy_link = &req->l_sl_policy;
797 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
798 struct sl_insert_point *prev)
800 struct ldlm_resource *res = lock->l_resource;
803 check_res_locked(res);
805 ldlm_resource_dump(D_INFO, res);
806 CDEBUG(D_OTHER, "About to add this lock:\n");
807 ldlm_lock_dump(D_OTHER, lock, 0);
809 if (lock->l_destroyed) {
810 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
814 LASSERT(list_empty(&lock->l_res_link));
815 LASSERT(list_empty(&lock->l_sl_mode));
816 LASSERT(list_empty(&lock->l_sl_policy));
818 list_add(&lock->l_res_link, prev->res_link);
819 list_add(&lock->l_sl_mode, prev->mode_link);
820 list_add(&lock->l_sl_policy, prev->policy_link);
825 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
827 struct sl_insert_point prev;
830 LASSERT(lock->l_req_mode == lock->l_granted_mode);
832 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
833 ldlm_granted_list_add_lock(lock, &prev);
838 * - ldlm_lock_enqueue
839 * - ldlm_reprocess_queue
840 * - ldlm_lock_convert
842 * must be called with lr_lock held
844 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
846 struct ldlm_resource *res = lock->l_resource;
849 check_res_locked(res);
851 lock->l_granted_mode = lock->l_req_mode;
852 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
853 ldlm_grant_lock_with_skiplist(lock);
854 else if (res->lr_type == LDLM_EXTENT)
855 ldlm_extent_add_lock(res, lock);
857 ldlm_resource_add_lock(res, &res->lr_granted, lock);
859 if (lock->l_granted_mode < res->lr_most_restr)
860 res->lr_most_restr = lock->l_granted_mode;
862 if (work_list && lock->l_completion_ast != NULL)
863 ldlm_add_ast_work_item(lock, NULL, work_list);
865 ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
869 /* returns a referenced lock or NULL. See the flag descriptions below, in the
870 * comment above ldlm_lock_match */
871 static struct ldlm_lock *search_queue(struct list_head *queue,
873 ldlm_policy_data_t *policy,
874 struct ldlm_lock *old_lock, int flags)
876 struct ldlm_lock *lock;
877 struct list_head *tmp;
879 list_for_each(tmp, queue) {
882 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
884 if (lock == old_lock)
887 /* llite sometimes wants to match locks that will be
888 * canceled when their users drop, but we allow it to match
889 * if it passes in CBPENDING and the lock still has users.
890 * this is generally only going to be used by children
891 * whose parents already hold a lock so forward progress
892 * can still happen. */
893 if (lock->l_flags & LDLM_FL_CBPENDING &&
894 !(flags & LDLM_FL_CBPENDING))
896 if (lock->l_flags & LDLM_FL_CBPENDING &&
897 lock->l_readers == 0 && lock->l_writers == 0)
900 if (!(lock->l_req_mode & *mode))
903 match = lock->l_req_mode;
904 if (lock->l_resource->lr_type == LDLM_EXTENT &&
905 (lock->l_policy_data.l_extent.start >
906 policy->l_extent.start ||
907 lock->l_policy_data.l_extent.end < policy->l_extent.end))
910 if (unlikely(match == LCK_GROUP) &&
911 lock->l_resource->lr_type == LDLM_EXTENT &&
912 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
915 /* We match if we have existing lock with same or wider set
917 if (lock->l_resource->lr_type == LDLM_IBITS &&
918 ((lock->l_policy_data.l_inodebits.bits &
919 policy->l_inodebits.bits) !=
920 policy->l_inodebits.bits))
923 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
926 if ((flags & LDLM_FL_LOCAL_ONLY) &&
927 !(lock->l_flags & LDLM_FL_LOCAL))
930 if (flags & LDLM_FL_TEST_LOCK) {
932 ldlm_lock_touch_in_lru(lock);
934 ldlm_lock_addref_internal_nolock(lock, match);
943 void ldlm_lock_allow_match(struct ldlm_lock *lock)
945 lock_res_and_lock(lock);
946 lock->l_flags |= LDLM_FL_LVB_READY;
947 cfs_waitq_signal(&lock->l_waitq);
948 unlock_res_and_lock(lock);
951 int ldlm_lock_fast_match(struct ldlm_lock *lock, int rw,
952 obd_off start, obd_off end,
953 struct lustre_handle *lockh)
955 LASSERT(rw == OBD_BRW_READ || rw == OBD_BRW_WRITE);
956 LASSERT(lockh != NULL);
961 lock_res_and_lock(lock);
962 /* check if granted mode is compatible */
963 if (rw == OBD_BRW_WRITE &&
964 !(lock->l_granted_mode & (LCK_PW|LCK_GROUP)))
967 /* does the lock cover the region we would like to access? */
968 if ((lock->l_policy_data.l_extent.start > start) ||
969 (lock->l_policy_data.l_extent.end < end))
972 /* if we received a blocking callback and the lock is no longer
973 * referenced, don't use it */
974 if ((lock->l_flags & LDLM_FL_CBPENDING) &&
975 !lock->l_writers && !lock->l_readers)
978 ldlm_lock_addref_internal_nolock(lock,
979 rw == OBD_BRW_WRITE ? LCK_PW : LCK_PR);
980 unlock_res_and_lock(lock);
981 ldlm_lock2handle(lock, lockh);
982 return 1; /* avoid using rc for stack relief */
985 unlock_res_and_lock(lock);
989 /* Can be called in two ways:
991 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
992 * for a duplicate of.
994 * Otherwise, all of the fields must be filled in, to match against.
996 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
997 * server (ie, connh is NULL)
998 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
999 * list will be considered
1000 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1001 * to be canceled can still be matched as long as they still have reader
1002 * or writer refernces
1003 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1004 * just tell us if we would have matched.
1006 * Returns 1 if it finds an already-existing lock that is compatible; in this
1007 * case, lockh is filled in with a addref()ed lock
1009 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1010 struct ldlm_res_id *res_id, ldlm_type_t type,
1011 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1012 struct lustre_handle *lockh)
1014 struct ldlm_resource *res;
1015 struct ldlm_lock *lock, *old_lock = NULL;
1020 old_lock = ldlm_handle2lock(lockh);
1023 ns = old_lock->l_resource->lr_namespace;
1024 res_id = &old_lock->l_resource->lr_name;
1025 type = old_lock->l_resource->lr_type;
1026 mode = old_lock->l_req_mode;
1029 res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
1031 LASSERT(old_lock == NULL);
1037 lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
1040 if (flags & LDLM_FL_BLOCK_GRANTED)
1042 lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
1045 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
1052 ldlm_resource_putref(res);
1055 ldlm_lock2handle(lock, lockh);
1056 if ((flags & LDLM_FL_LVB_READY) && (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1057 struct l_wait_info lwi;
1058 if (lock->l_completion_ast) {
1059 int err = lock->l_completion_ast(lock,
1060 LDLM_FL_WAIT_NOREPROC,
1063 if (flags & LDLM_FL_TEST_LOCK)
1064 LDLM_LOCK_PUT(lock);
1066 ldlm_lock_decref_internal(lock, mode);
1072 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout), NULL,
1073 LWI_ON_SIGNAL_NOOP, NULL);
1075 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1076 l_wait_event(lock->l_waitq,
1077 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1082 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1083 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1084 res_id->name[2] : policy->l_extent.start,
1085 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1086 res_id->name[3] : policy->l_extent.end);
1087 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1088 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1089 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1090 type, mode, res_id->name[0], res_id->name[1],
1091 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1092 res_id->name[2] :policy->l_extent.start,
1093 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1094 res_id->name[3] : policy->l_extent.end);
1097 LDLM_LOCK_PUT(old_lock);
1098 if (flags & LDLM_FL_TEST_LOCK && rc)
1099 LDLM_LOCK_PUT(lock);
1101 return rc ? mode : 0;
1104 /* Returns a referenced lock */
1105 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1106 struct ldlm_res_id res_id, ldlm_type_t type,
1108 ldlm_blocking_callback blocking,
1109 ldlm_completion_callback completion,
1110 ldlm_glimpse_callback glimpse,
1111 void *data, __u32 lvb_len)
1113 struct ldlm_lock *lock;
1114 struct ldlm_resource *res;
1117 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1121 lock = ldlm_lock_new(res);
1122 ldlm_resource_putref(res);
1127 lock->l_req_mode = mode;
1128 lock->l_ast_data = data;
1129 lock->l_blocking_ast = blocking;
1130 lock->l_completion_ast = completion;
1131 lock->l_glimpse_ast = glimpse;
1132 lock->l_pid = cfs_curproc_pid();
1134 lock->l_tree_node = NULL;
1135 /* if this is the extent lock, allocate the interval tree node */
1136 if (type == LDLM_EXTENT) {
1137 if (ldlm_interval_alloc(lock) == NULL)
1142 lock->l_lvb_len = lvb_len;
1143 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1144 if (lock->l_lvb_data == NULL)
1151 if (lock->l_lvb_data)
1152 OBD_FREE(lock->l_lvb_data, lvb_len);
1153 ldlm_interval_free(ldlm_interval_detach(lock));
1154 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1158 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1159 struct ldlm_lock **lockp,
1160 void *cookie, int *flags)
1162 struct ldlm_lock *lock = *lockp;
1163 struct ldlm_resource *res = lock->l_resource;
1164 int local = ns_is_client(res->lr_namespace);
1165 ldlm_processing_policy policy;
1166 ldlm_error_t rc = ELDLM_OK;
1167 struct ldlm_interval *node = NULL;
1170 lock->l_last_activity = cfs_time_current_sec();
1171 /* policies are not executed on the client or during replay */
1172 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1173 && !local && ns->ns_policy) {
1174 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1176 if (rc == ELDLM_LOCK_REPLACED) {
1177 /* The lock that was returned has already been granted,
1178 * and placed into lockp. If it's not the same as the
1179 * one we passed in, then destroy the old one and our
1180 * work here is done. */
1181 if (lock != *lockp) {
1182 ldlm_lock_destroy(lock);
1183 LDLM_LOCK_PUT(lock);
1185 *flags |= LDLM_FL_LOCK_CHANGED;
1187 } else if (rc != ELDLM_OK ||
1188 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1189 ldlm_lock_destroy(lock);
1194 /* For a replaying lock, it might be already in granted list. So
1195 * unlinking the lock will cause the interval node to be freed, we
1196 * have to allocate the interval node early otherwise we can't regrant
1197 * this lock in the future. - jay */
1198 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1199 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO,
1201 if(res->lr_type == LDLM_EXTENT)
1202 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_LOCAL, 30);
1204 lock_res_and_lock(lock);
1205 if (lock->l_destroyed)
1206 GOTO(out, rc = -EAGAIN);
1208 if (local && lock->l_req_mode == lock->l_granted_mode) {
1209 /* The server returned a blocked lock, but it was granted
1210 * before we got a chance to actually enqueue it. We don't
1211 * need to do anything else. */
1212 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1213 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1214 GOTO(out, ELDLM_OK);
1217 ldlm_resource_unlink_lock(lock);
1218 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1219 LASSERT(!local && (*flags & LDLM_FL_REPLAY));
1221 ldlm_lock_destroy_nolock(lock);
1222 GOTO(out, rc = -ENOMEM);
1225 CFS_INIT_LIST_HEAD(&node->li_group);
1226 ldlm_interval_attach(node, lock);
1230 /* Some flags from the enqueue want to make it into the AST, via the
1231 * lock's l_flags. */
1232 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1234 /* This distinction between local lock trees is very important; a client
1235 * namespace only has information about locks taken by that client, and
1236 * thus doesn't have enough information to decide for itself if it can
1237 * be granted (below). In this case, we do exactly what the server
1238 * tells us to do, as dictated by the 'flags'.
1240 * We do exactly the same thing during recovery, when the server is
1241 * more or less trusting the clients not to lie.
1243 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1244 * granted/converting queues. */
1246 if (*flags & LDLM_FL_BLOCK_CONV)
1247 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1248 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1249 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1251 ldlm_grant_lock(lock, NULL);
1252 GOTO(out, ELDLM_OK);
1253 } else if (*flags & LDLM_FL_REPLAY) {
1254 if (*flags & LDLM_FL_BLOCK_CONV) {
1255 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1256 GOTO(out, ELDLM_OK);
1257 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1258 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1259 GOTO(out, ELDLM_OK);
1260 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1261 ldlm_grant_lock(lock, NULL);
1262 GOTO(out, ELDLM_OK);
1264 /* If no flags, fall through to normal enqueue path. */
1267 policy = ldlm_processing_policy_table[res->lr_type];
1268 policy(lock, flags, 1, &rc, NULL);
1271 unlock_res_and_lock(lock);
1273 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1277 /* Must be called with namespace taken: queue is waiting or converting. */
1278 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1279 struct list_head *work_list)
1281 struct list_head *tmp, *pos;
1282 ldlm_processing_policy policy;
1284 int rc = LDLM_ITER_CONTINUE;
1288 check_res_locked(res);
1290 policy = ldlm_processing_policy_table[res->lr_type];
1293 list_for_each_safe(tmp, pos, queue) {
1294 struct ldlm_lock *pending;
1295 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1297 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1300 rc = policy(pending, &flags, 0, &err, work_list);
1301 if (rc != LDLM_ITER_CONTINUE)
1308 /* Helper function for pair ldlm_run_{bl,cp}_ast_work().
1310 * Send an existing rpc set specified by @arg->set and then
1311 * destroy it. Create new one if @do_create flag is set. */
1313 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1317 rc = ptlrpc_set_wait(arg->set);
1318 if (arg->type == LDLM_BL_CALLBACK)
1319 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1320 ptlrpc_set_destroy(arg->set);
1323 arg->set = ptlrpc_prep_set();
1326 int ldlm_run_bl_ast_work(struct list_head *rpc_list)
1328 struct ldlm_cb_set_arg arg;
1329 struct list_head *tmp, *pos;
1330 struct ldlm_lock_desc d;
1335 arg.set = ptlrpc_prep_set();
1336 if (NULL == arg.set)
1338 atomic_set(&arg.restart, 0);
1339 arg.type = LDLM_BL_CALLBACK;
1342 list_for_each_safe(tmp, pos, rpc_list) {
1343 struct ldlm_lock *lock =
1344 list_entry(tmp, struct ldlm_lock, l_bl_ast);
1346 /* nobody should touch l_bl_ast */
1347 lock_res_and_lock(lock);
1348 list_del_init(&lock->l_bl_ast);
1350 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1351 LASSERT(lock->l_bl_ast_run == 0);
1352 LASSERT(lock->l_blocking_lock);
1353 lock->l_bl_ast_run++;
1354 unlock_res_and_lock(lock);
1356 ldlm_lock2desc(lock->l_blocking_lock, &d);
1358 LDLM_LOCK_PUT(lock->l_blocking_lock);
1359 lock->l_blocking_lock = NULL;
1360 rc = lock->l_blocking_ast(lock, &d, (void *)&arg,
1362 LDLM_LOCK_PUT(lock);
1365 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1366 * and create a new set for requests that remained in
1368 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1369 ldlm_send_and_maybe_create_set(&arg, 1);
1375 ldlm_send_and_maybe_create_set(&arg, 0);
1377 /* In case when number of ASTs is multiply of
1378 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1379 * @arg.set must be destroyed here, otherwise we get
1380 * write memory leaking. */
1381 ptlrpc_set_destroy(arg.set);
1383 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1386 int ldlm_run_cp_ast_work(struct list_head *rpc_list)
1388 struct ldlm_cb_set_arg arg;
1389 struct list_head *tmp, *pos;
1394 arg.set = ptlrpc_prep_set();
1395 if (NULL == arg.set)
1397 atomic_set(&arg.restart, 0);
1398 arg.type = LDLM_CP_CALLBACK;
1400 /* It's possible to receive a completion AST before we've set
1401 * the l_completion_ast pointer: either because the AST arrived
1402 * before the reply, or simply because there's a small race
1403 * window between receiving the reply and finishing the local
1404 * enqueue. (bug 842)
1406 * This can't happen with the blocking_ast, however, because we
1407 * will never call the local blocking_ast until we drop our
1408 * reader/writer reference, which we won't do until we get the
1409 * reply and finish enqueueing. */
1412 list_for_each_safe(tmp, pos, rpc_list) {
1413 struct ldlm_lock *lock =
1414 list_entry(tmp, struct ldlm_lock, l_cp_ast);
1415 ldlm_completion_callback completion_callback;
1417 /* nobody should touch l_cp_ast */
1418 lock_res_and_lock(lock);
1419 list_del_init(&lock->l_cp_ast);
1420 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1421 /* save l_completion_ast since it can be changed by
1422 * mds_intent_policy(), see bug 14225 */
1423 completion_callback = lock->l_completion_ast;
1424 lock->l_flags &= ~LDLM_FL_CP_REQD;
1425 unlock_res_and_lock(lock);
1427 if (completion_callback != NULL) {
1428 rc = completion_callback(lock, 0, (void *)&arg);
1431 LDLM_LOCK_PUT(lock);
1433 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1434 * and create a new set for requests that remained in
1436 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1437 ldlm_send_and_maybe_create_set(&arg, 1);
1443 ldlm_send_and_maybe_create_set(&arg, 0);
1445 /* In case when number of ASTs is multiply of
1446 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1447 * @arg.set must be destroyed here, otherwise we get
1448 * write memory leaking. */
1449 ptlrpc_set_destroy(arg.set);
1451 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1454 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1456 ldlm_reprocess_all(res);
1457 return LDLM_ITER_CONTINUE;
1460 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1462 struct list_head *tmp;
1466 spin_lock(&ns->ns_hash_lock);
1467 for (i = 0; i < RES_HASH_SIZE; i++) {
1468 tmp = ns->ns_hash[i].next;
1469 while (tmp != &(ns->ns_hash[i])) {
1470 struct ldlm_resource *res =
1471 list_entry(tmp, struct ldlm_resource, lr_hash);
1473 ldlm_resource_getref(res);
1474 spin_unlock(&ns->ns_hash_lock);
1476 rc = reprocess_one_queue(res, NULL);
1478 spin_lock(&ns->ns_hash_lock);
1480 ldlm_resource_putref_locked(res);
1482 if (rc == LDLM_ITER_STOP)
1487 spin_unlock(&ns->ns_hash_lock);
1491 void ldlm_reprocess_all(struct ldlm_resource *res)
1493 CFS_LIST_HEAD(rpc_list);
1497 /* Local lock trees don't get reprocessed. */
1498 if (ns_is_client(res->lr_namespace)) {
1505 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1506 if (rc == LDLM_ITER_CONTINUE)
1507 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1510 rc = ldlm_run_cp_ast_work(&rpc_list);
1511 if (rc == -ERESTART) {
1512 LASSERT(list_empty(&rpc_list));
1518 void ldlm_cancel_callback(struct ldlm_lock *lock)
1520 check_res_locked(lock->l_resource);
1521 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1522 lock->l_flags |= LDLM_FL_CANCEL;
1523 if (lock->l_blocking_ast) {
1524 // l_check_no_ns_lock(ns);
1525 unlock_res_and_lock(lock);
1526 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1528 lock_res_and_lock(lock);
1530 LDLM_DEBUG(lock, "no blocking ast");
1533 lock->l_flags |= LDLM_FL_BL_DONE;
1536 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1538 if (req->l_resource->lr_type != LDLM_PLAIN &&
1539 req->l_resource->lr_type != LDLM_IBITS)
1542 list_del_init(&req->l_sl_policy);
1543 list_del_init(&req->l_sl_mode);
1546 void ldlm_lock_cancel(struct ldlm_lock *lock)
1548 struct ldlm_resource *res;
1549 struct ldlm_namespace *ns;
1552 lock_res_and_lock(lock);
1554 res = lock->l_resource;
1555 ns = res->lr_namespace;
1557 /* Please do not, no matter how tempting, remove this LBUG without
1558 * talking to me first. -phik */
1559 if (lock->l_readers || lock->l_writers) {
1560 LDLM_ERROR(lock, "lock still has references");
1564 ldlm_del_waiting_lock(lock);
1566 /* Releases res lock */
1567 ldlm_cancel_callback(lock);
1569 /* Yes, second time, just in case it was added again while we were
1570 running with no res lock in ldlm_cancel_callback */
1571 ldlm_del_waiting_lock(lock);
1572 ldlm_resource_unlink_lock(lock);
1573 ldlm_lock_destroy_nolock(lock);
1575 if (lock->l_granted_mode == lock->l_req_mode)
1576 ldlm_pool_del(&ns->ns_pool, lock);
1578 /* Make sure we will not be called again for same lock what is possible
1579 * if not to zero out lock->l_granted_mode */
1580 lock->l_granted_mode = 0;
1581 unlock_res_and_lock(lock);
1586 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1588 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1594 lock->l_ast_data = data;
1595 LDLM_LOCK_PUT(lock);
1599 void ldlm_cancel_locks_for_export_cb(void *obj, void *data)
1601 struct obd_export *exp = data;
1602 struct ldlm_lock *lock = obj;
1603 struct ldlm_resource *res;
1605 res = ldlm_resource_getref(lock->l_resource);
1606 LDLM_LOCK_GET(lock);
1608 LDLM_DEBUG(lock, "export %p", exp);
1609 ldlm_res_lvbo_update(res, NULL, 0, 1);
1611 ldlm_lock_cancel(lock);
1612 ldlm_reprocess_all(res);
1614 ldlm_resource_putref(res);
1615 LDLM_LOCK_PUT(lock);
1618 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1620 lustre_hash_for_each_empty(exp->exp_lock_hash,
1621 ldlm_cancel_locks_for_export_cb, exp);
1624 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1627 CFS_LIST_HEAD(rpc_list);
1628 struct ldlm_resource *res;
1629 struct ldlm_namespace *ns;
1632 struct sl_insert_point prev;
1634 struct ldlm_interval *node;
1637 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1638 *flags |= LDLM_FL_BLOCK_GRANTED;
1639 RETURN(lock->l_resource);
1642 /* I can't check the type of lock here because the bitlock of lock
1643 * is not held here, so do the allocation blindly. -jay */
1644 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
1645 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1648 LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1649 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1651 lock_res_and_lock(lock);
1652 if (unlikely(lock->l_destroyed != 0)) {
1653 unlock_res_and_lock(lock);
1657 res = lock->l_resource;
1658 ns = res->lr_namespace;
1660 old_mode = lock->l_req_mode;
1661 lock->l_req_mode = new_mode;
1662 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1663 /* remember the lock position where the lock might be
1664 * added back to the granted list later and also
1665 * remember the join mode for skiplist fixing. */
1666 prev.res_link = lock->l_res_link.prev;
1667 prev.mode_link = lock->l_sl_mode.prev;
1668 prev.policy_link = lock->l_sl_policy.prev;
1669 ldlm_resource_unlink_lock(lock);
1671 ldlm_resource_unlink_lock(lock);
1672 if (res->lr_type == LDLM_EXTENT) {
1673 /* FIXME: ugly code, I have to attach the lock to a
1674 * interval node again since perhaps it will be granted
1676 CFS_INIT_LIST_HEAD(&node->li_group);
1677 ldlm_interval_attach(node, lock);
1682 /* If this is a local resource, put it on the appropriate list. */
1683 if (ns_is_client(res->lr_namespace)) {
1684 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1685 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1687 /* This should never happen, because of the way the
1688 * server handles conversions. */
1689 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1693 ldlm_grant_lock(lock, &rpc_list);
1695 /* FIXME: completion handling not with ns_lock held ! */
1696 if (lock->l_completion_ast)
1697 lock->l_completion_ast(lock, 0, NULL);
1701 ldlm_processing_policy policy;
1702 policy = ldlm_processing_policy_table[res->lr_type];
1703 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1704 if (rc == LDLM_ITER_STOP) {
1705 lock->l_req_mode = old_mode;
1706 if (res->lr_type == LDLM_EXTENT)
1707 ldlm_extent_add_lock(res, lock);
1709 ldlm_granted_list_add_lock(lock, &prev);
1713 *flags |= LDLM_FL_BLOCK_GRANTED;
1717 unlock_res_and_lock(lock);
1720 ldlm_run_cp_ast_work(&rpc_list);
1722 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1726 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1728 struct obd_device *obd = NULL;
1730 if (!((libcfs_debug | D_ERROR) & level))
1734 CDEBUG(level, " NULL LDLM lock\n");
1738 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1739 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1741 if (lock->l_conn_export != NULL)
1742 obd = lock->l_conn_export->exp_obd;
1743 if (lock->l_export && lock->l_export->exp_connection) {
1744 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1745 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1746 lock->l_remote_handle.cookie);
1747 } else if (obd == NULL) {
1748 CDEBUG(level, " Node: local\n");
1750 struct obd_import *imp = obd->u.cli.cl_import;
1751 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1752 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1753 lock->l_remote_handle.cookie);
1755 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64")\n", lock->l_resource,
1756 lock->l_resource->lr_name.name[0],
1757 lock->l_resource->lr_name.name[1]);
1758 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1759 "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1760 ldlm_lockname[lock->l_granted_mode],
1761 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1763 if (lock->l_resource->lr_type == LDLM_EXTENT)
1764 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1765 " (req "LPU64"-"LPU64")\n",
1766 lock->l_policy_data.l_extent.start,
1767 lock->l_policy_data.l_extent.end,
1768 lock->l_req_extent.start, lock->l_req_extent.end);
1769 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1770 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1771 lock->l_policy_data.l_flock.pid,
1772 lock->l_policy_data.l_flock.start,
1773 lock->l_policy_data.l_flock.end);
1774 else if (lock->l_resource->lr_type == LDLM_IBITS)
1775 CDEBUG(level, " Bits: "LPX64"\n",
1776 lock->l_policy_data.l_inodebits.bits);
1779 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1781 struct ldlm_lock *lock;
1783 if (!((libcfs_debug | D_ERROR) & level))
1786 lock = ldlm_handle2lock(lockh);
1790 ldlm_lock_dump(D_OTHER, lock, 0);
1792 LDLM_LOCK_PUT(lock);
1795 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1796 struct libcfs_debug_msg_data *data, const char *fmt,
1800 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1802 va_start(args, fmt);
1803 if (lock->l_resource == NULL) {
1804 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1805 data->msg_fn, data->msg_line, fmt, args,
1806 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1807 "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
1808 LPX64" expref: %d pid: %u timeout: %lu\n", lock,
1809 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1810 lock->l_readers, lock->l_writers,
1811 ldlm_lockname[lock->l_granted_mode],
1812 ldlm_lockname[lock->l_req_mode],
1813 lock->l_flags, lock->l_remote_handle.cookie,
1815 atomic_read(&lock->l_export->exp_refcount) : -99,
1816 lock->l_pid, lock->l_callback_timeout);
1821 switch (lock->l_resource->lr_type) {
1823 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1824 data->msg_fn, data->msg_line, fmt, args,
1825 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1826 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1827 "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
1828 " expref: %d pid: %u timeout %lu\n",
1829 lock->l_resource->lr_namespace->ns_name, lock,
1830 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1831 lock->l_readers, lock->l_writers,
1832 ldlm_lockname[lock->l_granted_mode],
1833 ldlm_lockname[lock->l_req_mode],
1834 lock->l_resource->lr_name.name[0],
1835 lock->l_resource->lr_name.name[1],
1836 atomic_read(&lock->l_resource->lr_refcount),
1837 ldlm_typename[lock->l_resource->lr_type],
1838 lock->l_policy_data.l_extent.start,
1839 lock->l_policy_data.l_extent.end,
1840 lock->l_req_extent.start, lock->l_req_extent.end,
1841 lock->l_flags, lock->l_remote_handle.cookie,
1843 atomic_read(&lock->l_export->exp_refcount) : -99,
1844 lock->l_pid, lock->l_callback_timeout);
1847 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1848 data->msg_fn, data->msg_line, fmt, args,
1849 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1850 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1851 "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
1852 " expref: %d pid: %u timeout: %lu\n",
1853 lock->l_resource->lr_namespace->ns_name, lock,
1854 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1855 lock->l_readers, lock->l_writers,
1856 ldlm_lockname[lock->l_granted_mode],
1857 ldlm_lockname[lock->l_req_mode],
1858 lock->l_resource->lr_name.name[0],
1859 lock->l_resource->lr_name.name[1],
1860 atomic_read(&lock->l_resource->lr_refcount),
1861 ldlm_typename[lock->l_resource->lr_type],
1862 lock->l_policy_data.l_flock.pid,
1863 lock->l_policy_data.l_flock.start,
1864 lock->l_policy_data.l_flock.end,
1865 lock->l_flags, lock->l_remote_handle.cookie,
1867 atomic_read(&lock->l_export->exp_refcount) : -99,
1868 lock->l_pid, lock->l_callback_timeout);
1871 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1872 data->msg_fn, data->msg_line, fmt, args,
1873 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1874 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1875 "flags: "LPX64" remote: "LPX64" expref: %d "
1876 "pid: %u timeout: %lu\n",
1877 lock->l_resource->lr_namespace->ns_name,
1878 lock, lock->l_handle.h_cookie,
1879 atomic_read (&lock->l_refc),
1880 lock->l_readers, lock->l_writers,
1881 ldlm_lockname[lock->l_granted_mode],
1882 ldlm_lockname[lock->l_req_mode],
1883 lock->l_resource->lr_name.name[0],
1884 lock->l_resource->lr_name.name[1],
1885 lock->l_policy_data.l_inodebits.bits,
1886 atomic_read(&lock->l_resource->lr_refcount),
1887 ldlm_typename[lock->l_resource->lr_type],
1888 lock->l_flags, lock->l_remote_handle.cookie,
1890 atomic_read(&lock->l_export->exp_refcount) : -99,
1891 lock->l_pid, lock->l_callback_timeout);
1894 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1895 data->msg_fn, data->msg_line, fmt, args,
1896 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1897 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
1898 "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
1899 lock->l_resource->lr_namespace->ns_name,
1900 lock, lock->l_handle.h_cookie,
1901 atomic_read (&lock->l_refc),
1902 lock->l_readers, lock->l_writers,
1903 ldlm_lockname[lock->l_granted_mode],
1904 ldlm_lockname[lock->l_req_mode],
1905 lock->l_resource->lr_name.name[0],
1906 lock->l_resource->lr_name.name[1],
1907 atomic_read(&lock->l_resource->lr_refcount),
1908 ldlm_typename[lock->l_resource->lr_type],
1909 lock->l_flags, lock->l_remote_handle.cookie,
1911 atomic_read(&lock->l_export->exp_refcount) : -99,
1912 lock->l_pid, lock->l_callback_timeout);
1917 EXPORT_SYMBOL(_ldlm_lock_debug);