1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lock.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_LDLM
45 # include <libcfs/libcfs.h>
46 # ifndef HAVE_VFS_INTENT_PATCHES
47 # include <linux/lustre_intent.h>
50 # include <liblustre.h>
53 #include <obd_class.h>
54 #include "ldlm_internal.h"
57 char *ldlm_lockname[] = {
69 char *ldlm_typename[] = {
76 char *ldlm_it2str(int it)
83 case (IT_OPEN | IT_CREAT):
96 CERROR("Unknown intent %d\n", it);
101 extern cfs_mem_cache_t *ldlm_lock_slab;
103 static ldlm_processing_policy ldlm_processing_policy_table[] = {
104 [LDLM_PLAIN] ldlm_process_plain_lock,
105 [LDLM_EXTENT] ldlm_process_extent_lock,
107 [LDLM_FLOCK] ldlm_process_flock_lock,
109 [LDLM_IBITS] ldlm_process_inodebits_lock,
112 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
114 return ldlm_processing_policy_table[res->lr_type];
117 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
123 * REFCOUNTED LOCK OBJECTS
128 * Lock refcounts, during creation:
129 * - one special one for allocation, dec'd only once in destroy
130 * - one for being a lock that's in-use
131 * - one for the addref associated with a new lock
133 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
135 cfs_atomic_inc(&lock->l_refc);
139 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
141 LASSERT(size == sizeof(*lock));
142 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
145 void ldlm_lock_put(struct ldlm_lock *lock)
149 LASSERT(lock->l_resource != LP_POISON);
150 LASSERT(cfs_atomic_read(&lock->l_refc) > 0);
151 if (cfs_atomic_dec_and_test(&lock->l_refc)) {
152 struct ldlm_resource *res;
155 "final lock_put on destroyed lock, freeing it.");
157 res = lock->l_resource;
158 LASSERT(lock->l_destroyed);
159 LASSERT(cfs_list_empty(&lock->l_res_link));
160 LASSERT(cfs_list_empty(&lock->l_pending_chain));
162 cfs_atomic_dec(&ldlm_res_to_ns(res)->ns_locks);
163 lu_ref_del(&res->lr_reference, "lock", lock);
164 ldlm_resource_putref(res);
165 lock->l_resource = NULL;
166 if (lock->l_export) {
167 class_export_lock_put(lock->l_export, lock);
168 lock->l_export = NULL;
171 if (lock->l_lvb_data != NULL)
172 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
174 ldlm_interval_free(ldlm_interval_detach(lock));
175 lu_ref_fini(&lock->l_reference);
176 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
183 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
186 if (!cfs_list_empty(&lock->l_lru)) {
187 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
189 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
190 cfs_list_del_init(&lock->l_lru);
191 if (lock->l_flags & LDLM_FL_SKIPPED)
192 lock->l_flags &= ~LDLM_FL_SKIPPED;
193 LASSERT(ns->ns_nr_unused > 0);
200 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
202 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
206 cfs_spin_lock(&ns->ns_unused_lock);
207 rc = ldlm_lock_remove_from_lru_nolock(lock);
208 cfs_spin_unlock(&ns->ns_unused_lock);
213 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
215 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
217 lock->l_last_used = cfs_time_current();
218 LASSERT(cfs_list_empty(&lock->l_lru));
219 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
220 cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
221 LASSERT(ns->ns_nr_unused >= 0);
225 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
227 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
230 cfs_spin_lock(&ns->ns_unused_lock);
231 ldlm_lock_add_to_lru_nolock(lock);
232 cfs_spin_unlock(&ns->ns_unused_lock);
236 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
238 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
241 cfs_spin_lock(&ns->ns_unused_lock);
242 if (!cfs_list_empty(&lock->l_lru)) {
243 ldlm_lock_remove_from_lru_nolock(lock);
244 ldlm_lock_add_to_lru_nolock(lock);
246 cfs_spin_unlock(&ns->ns_unused_lock);
250 /* This used to have a 'strict' flag, which recovery would use to mark an
251 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
252 * shall explain why it's gone: with the new hash table scheme, once you call
253 * ldlm_lock_destroy, you can never drop your final references on this lock.
254 * Because it's not in the hash table anymore. -phil */
255 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
259 if (lock->l_readers || lock->l_writers) {
260 LDLM_ERROR(lock, "lock still has references");
261 ldlm_lock_dump(D_ERROR, lock, 0);
265 if (!cfs_list_empty(&lock->l_res_link)) {
266 LDLM_ERROR(lock, "lock still on resource");
267 ldlm_lock_dump(D_ERROR, lock, 0);
271 if (lock->l_destroyed) {
272 LASSERT(cfs_list_empty(&lock->l_lru));
276 lock->l_destroyed = 1;
278 if (lock->l_export && lock->l_export->exp_lock_hash &&
279 !cfs_hlist_unhashed(&lock->l_exp_hash))
280 cfs_hash_del(lock->l_export->exp_lock_hash,
281 &lock->l_remote_handle, &lock->l_exp_hash);
283 ldlm_lock_remove_from_lru(lock);
284 class_handle_unhash(&lock->l_handle);
287 /* Wake anyone waiting for this lock */
288 /* FIXME: I should probably add yet another flag, instead of using
289 * l_export to only call this on clients */
291 class_export_put(lock->l_export);
292 lock->l_export = NULL;
293 if (lock->l_export && lock->l_completion_ast)
294 lock->l_completion_ast(lock, 0);
300 void ldlm_lock_destroy(struct ldlm_lock *lock)
304 lock_res_and_lock(lock);
305 first = ldlm_lock_destroy_internal(lock);
306 unlock_res_and_lock(lock);
308 /* drop reference from hashtable only for first destroy */
310 lu_ref_del(&lock->l_reference, "hash", lock);
311 LDLM_LOCK_RELEASE(lock);
316 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
320 first = ldlm_lock_destroy_internal(lock);
321 /* drop reference from hashtable only for first destroy */
323 lu_ref_del(&lock->l_reference, "hash", lock);
324 LDLM_LOCK_RELEASE(lock);
329 /* this is called by portals_handle2object with the handle lock taken */
330 static void lock_handle_addref(void *lock)
332 LDLM_LOCK_GET((struct ldlm_lock *)lock);
336 * usage: pass in a resource on which you have done ldlm_resource_get
337 * pass in a parent lock on which you have done a ldlm_lock_get
338 * after return, ldlm_*_put the resource and parent
339 * returns: lock with refcount 2 - one for current caller and one for remote
341 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
343 struct ldlm_lock *lock;
346 if (resource == NULL)
349 OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
353 cfs_spin_lock_init(&lock->l_lock);
354 lock->l_resource = ldlm_resource_getref(resource);
355 lu_ref_add(&resource->lr_reference, "lock", lock);
357 cfs_atomic_set(&lock->l_refc, 2);
358 CFS_INIT_LIST_HEAD(&lock->l_res_link);
359 CFS_INIT_LIST_HEAD(&lock->l_lru);
360 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
361 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
362 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
363 CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
364 cfs_waitq_init(&lock->l_waitq);
365 lock->l_blocking_lock = NULL;
366 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
367 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
368 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
370 cfs_atomic_inc(&ldlm_res_to_ns(resource)->ns_locks);
371 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
372 class_handle_hash(&lock->l_handle, lock_handle_addref);
374 CFS_INIT_LIST_HEAD(&lock->l_extents_list);
375 cfs_spin_lock_init(&lock->l_extents_list_lock);
376 CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
377 lu_ref_init(&lock->l_reference);
378 lu_ref_add(&lock->l_reference, "hash", lock);
379 lock->l_callback_timeout = 0;
381 #if LUSTRE_TRACKS_LOCK_EXP_REFS
382 CFS_INIT_LIST_HEAD(&lock->l_exp_refs_link);
383 lock->l_exp_refs_nr = 0;
384 lock->l_exp_refs_target = NULL;
390 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
391 const struct ldlm_res_id *new_resid)
393 struct ldlm_resource *oldres = lock->l_resource;
394 struct ldlm_resource *newres;
398 LASSERT(ns_is_client(ns));
400 lock_res_and_lock(lock);
401 if (memcmp(new_resid, &lock->l_resource->lr_name,
402 sizeof(lock->l_resource->lr_name)) == 0) {
404 unlock_res_and_lock(lock);
408 LASSERT(new_resid->name[0] != 0);
410 /* This function assumes that the lock isn't on any lists */
411 LASSERT(cfs_list_empty(&lock->l_res_link));
413 type = oldres->lr_type;
414 unlock_res_and_lock(lock);
416 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
417 lu_ref_add(&newres->lr_reference, "lock", lock);
421 * To flip the lock from the old to the new resource, lock, oldres and
422 * newres have to be locked. Resource spin-locks are nested within
423 * lock->l_lock, and are taken in the memory address order to avoid
426 cfs_spin_lock(&lock->l_lock);
427 oldres = lock->l_resource;
428 if (oldres < newres) {
430 lock_res_nested(newres, LRT_NEW);
433 lock_res_nested(oldres, LRT_NEW);
435 LASSERT(memcmp(new_resid, &oldres->lr_name,
436 sizeof oldres->lr_name) != 0);
437 lock->l_resource = newres;
439 unlock_res_and_lock(lock);
441 /* ...and the flowers are still standing! */
442 lu_ref_del(&oldres->lr_reference, "lock", lock);
443 ldlm_resource_putref(oldres);
452 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
454 lockh->cookie = lock->l_handle.h_cookie;
457 /* if flags: atomically get the lock and set the flags.
458 * Return NULL if flag already set
461 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
464 struct ldlm_namespace *ns;
465 struct ldlm_lock *lock, *retval = NULL;
470 lock = class_handle2object(handle->cookie);
474 LASSERT(lock->l_resource != NULL);
475 ns = ldlm_lock_to_ns(lock);
478 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
479 lock_res_and_lock(lock);
481 /* It's unlikely but possible that someone marked the lock as
482 * destroyed after we did handle2object on it */
483 if (lock->l_destroyed) {
484 unlock_res_and_lock(lock);
485 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
490 if (flags && (lock->l_flags & flags)) {
491 unlock_res_and_lock(lock);
497 lock->l_flags |= flags;
499 unlock_res_and_lock(lock);
506 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
508 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
509 /* INODEBITS_INTEROP: If the other side does not support
510 * inodebits, reply with a plain lock descriptor.
512 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
513 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
514 /* Make sure all the right bits are set in this lock we
515 are going to pass to client */
516 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
517 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
518 "Inappropriate inode lock bits during "
519 "conversion " LPU64 "\n",
520 lock->l_policy_data.l_inodebits.bits);
522 ldlm_res2desc(lock->l_resource, &desc->l_resource);
523 desc->l_resource.lr_type = LDLM_PLAIN;
525 /* Convert "new" lock mode to something old client can
527 if ((lock->l_req_mode == LCK_CR) ||
528 (lock->l_req_mode == LCK_CW))
529 desc->l_req_mode = LCK_PR;
531 desc->l_req_mode = lock->l_req_mode;
532 if ((lock->l_granted_mode == LCK_CR) ||
533 (lock->l_granted_mode == LCK_CW)) {
534 desc->l_granted_mode = LCK_PR;
536 /* We never grant PW/EX locks to clients */
537 LASSERT((lock->l_granted_mode != LCK_PW) &&
538 (lock->l_granted_mode != LCK_EX));
539 desc->l_granted_mode = lock->l_granted_mode;
542 /* We do not copy policy here, because there is no
543 policy for plain locks */
545 ldlm_res2desc(lock->l_resource, &desc->l_resource);
546 desc->l_req_mode = lock->l_req_mode;
547 desc->l_granted_mode = lock->l_granted_mode;
548 desc->l_policy_data = lock->l_policy_data;
552 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
553 cfs_list_t *work_list)
555 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
556 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
557 lock->l_flags |= LDLM_FL_AST_SENT;
558 /* If the enqueuing client said so, tell the AST recipient to
559 * discard dirty data, rather than writing back. */
560 if (new->l_flags & LDLM_AST_DISCARD_DATA)
561 lock->l_flags |= LDLM_FL_DISCARD_DATA;
562 LASSERT(cfs_list_empty(&lock->l_bl_ast));
563 cfs_list_add(&lock->l_bl_ast, work_list);
565 LASSERT(lock->l_blocking_lock == NULL);
566 lock->l_blocking_lock = LDLM_LOCK_GET(new);
570 void ldlm_add_cp_work_item(struct ldlm_lock *lock, cfs_list_t *work_list)
572 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
573 lock->l_flags |= LDLM_FL_CP_REQD;
574 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
575 LASSERT(cfs_list_empty(&lock->l_cp_ast));
576 cfs_list_add(&lock->l_cp_ast, work_list);
581 /* must be called with lr_lock held */
582 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
583 cfs_list_t *work_list)
586 check_res_locked(lock->l_resource);
588 ldlm_add_bl_work_item(lock, new, work_list);
590 ldlm_add_cp_work_item(lock, work_list);
594 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
596 struct ldlm_lock *lock;
598 lock = ldlm_handle2lock(lockh);
599 LASSERT(lock != NULL);
600 ldlm_lock_addref_internal(lock, mode);
604 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
606 ldlm_lock_remove_from_lru(lock);
607 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
609 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
611 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
613 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
616 lu_ref_add_atomic(&lock->l_reference, "user", lock);
617 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
621 * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
624 * \retval 0 success, lock was addref-ed
626 * \retval -EAGAIN lock is being canceled.
628 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
630 struct ldlm_lock *lock;
634 lock = ldlm_handle2lock(lockh);
636 lock_res_and_lock(lock);
637 if (lock->l_readers != 0 || lock->l_writers != 0 ||
638 !(lock->l_flags & LDLM_FL_CBPENDING)) {
639 ldlm_lock_addref_internal_nolock(lock, mode);
642 unlock_res_and_lock(lock);
648 /* only called for local locks */
649 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
651 lock_res_and_lock(lock);
652 ldlm_lock_addref_internal_nolock(lock, mode);
653 unlock_res_and_lock(lock);
656 /* only called in ldlm_flock_destroy and for local locks.
657 * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
658 * * ldlm_lock_remove_from_lru() does nothing, it is safe
659 * * for ldlm_flock_destroy usage by dropping some code */
660 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
662 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
663 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
664 LASSERT(lock->l_readers > 0);
665 lu_ref_del(&lock->l_reference, "reader", lock);
668 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
669 LASSERT(lock->l_writers > 0);
670 lu_ref_del(&lock->l_reference, "writer", lock);
674 lu_ref_del(&lock->l_reference, "user", lock);
675 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
678 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
680 struct ldlm_namespace *ns;
683 lock_res_and_lock(lock);
685 ns = ldlm_lock_to_ns(lock);
687 ldlm_lock_decref_internal_nolock(lock, mode);
689 if (lock->l_flags & LDLM_FL_LOCAL &&
690 !lock->l_readers && !lock->l_writers) {
691 /* If this is a local lock on a server namespace and this was
692 * the last reference, cancel the lock. */
693 CDEBUG(D_INFO, "forcing cancel of local lock\n");
694 lock->l_flags |= LDLM_FL_CBPENDING;
697 if (!lock->l_readers && !lock->l_writers &&
698 (lock->l_flags & LDLM_FL_CBPENDING)) {
699 /* If we received a blocked AST and this was the last reference,
700 * run the callback. */
701 if (ns_is_server(ns) && lock->l_export)
702 CERROR("FL_CBPENDING set on non-local lock--just a "
705 LDLM_DEBUG(lock, "final decref done on cbpending lock");
707 LDLM_LOCK_GET(lock); /* dropped by bl thread */
708 ldlm_lock_remove_from_lru(lock);
709 unlock_res_and_lock(lock);
711 if (lock->l_flags & LDLM_FL_FAIL_LOC)
712 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
714 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
715 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
716 ldlm_handle_bl_callback(ns, NULL, lock);
717 } else if (ns_is_client(ns) &&
718 !lock->l_readers && !lock->l_writers &&
719 !(lock->l_flags & LDLM_FL_BL_AST)) {
720 /* If this is a client-side namespace and this was the last
721 * reference, put it on the LRU. */
722 ldlm_lock_add_to_lru(lock);
723 unlock_res_and_lock(lock);
725 if (lock->l_flags & LDLM_FL_FAIL_LOC)
726 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
728 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
729 * are not supported by the server, otherwise, it is done on
731 if (!exp_connect_cancelset(lock->l_conn_export) &&
732 !ns_connect_lru_resize(ns))
733 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
735 unlock_res_and_lock(lock);
741 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
743 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
744 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
745 ldlm_lock_decref_internal(lock, mode);
749 /* This will drop a lock reference and mark it for destruction, but will not
750 * necessarily cancel the lock before returning. */
751 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
753 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
756 LASSERT(lock != NULL);
758 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
759 lock_res_and_lock(lock);
760 lock->l_flags |= LDLM_FL_CBPENDING;
761 unlock_res_and_lock(lock);
762 ldlm_lock_decref_internal(lock, mode);
766 struct sl_insert_point {
767 cfs_list_t *res_link;
768 cfs_list_t *mode_link;
769 cfs_list_t *policy_link;
773 * search_granted_lock
776 * Finds a position to insert the new lock.
778 * queue [input]: the granted list where search acts on;
779 * req [input]: the lock whose position to be located;
780 * prev [output]: positions within 3 lists to insert @req to
784 * - ldlm_grant_lock_with_skiplist
786 static void search_granted_lock(cfs_list_t *queue,
787 struct ldlm_lock *req,
788 struct sl_insert_point *prev)
791 struct ldlm_lock *lock, *mode_end, *policy_end;
794 cfs_list_for_each(tmp, queue) {
795 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
797 mode_end = cfs_list_entry(lock->l_sl_mode.prev,
798 struct ldlm_lock, l_sl_mode);
800 if (lock->l_req_mode != req->l_req_mode) {
801 /* jump to last lock of mode group */
802 tmp = &mode_end->l_res_link;
806 /* suitable mode group is found */
807 if (lock->l_resource->lr_type == LDLM_PLAIN) {
808 /* insert point is last lock of the mode group */
809 prev->res_link = &mode_end->l_res_link;
810 prev->mode_link = &mode_end->l_sl_mode;
811 prev->policy_link = &req->l_sl_policy;
814 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
817 cfs_list_entry(lock->l_sl_policy.prev,
821 if (lock->l_policy_data.l_inodebits.bits ==
822 req->l_policy_data.l_inodebits.bits) {
823 /* insert point is last lock of
824 * the policy group */
826 &policy_end->l_res_link;
828 &policy_end->l_sl_mode;
830 &policy_end->l_sl_policy;
835 if (policy_end == mode_end)
836 /* done with mode group */
839 /* go to next policy group within mode group */
840 tmp = policy_end->l_res_link.next;
841 lock = cfs_list_entry(tmp, struct ldlm_lock,
843 } /* loop over policy groups within the mode group */
845 /* insert point is last lock of the mode group,
846 * new policy group is started */
847 prev->res_link = &mode_end->l_res_link;
848 prev->mode_link = &mode_end->l_sl_mode;
849 prev->policy_link = &req->l_sl_policy;
853 LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
858 /* insert point is last lock on the queue,
859 * new mode group and new policy group are started */
860 prev->res_link = queue->prev;
861 prev->mode_link = &req->l_sl_mode;
862 prev->policy_link = &req->l_sl_policy;
867 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
868 struct sl_insert_point *prev)
870 struct ldlm_resource *res = lock->l_resource;
873 check_res_locked(res);
875 ldlm_resource_dump(D_INFO, res);
876 CDEBUG(D_OTHER, "About to add this lock:\n");
877 ldlm_lock_dump(D_OTHER, lock, 0);
879 if (lock->l_destroyed) {
880 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
884 LASSERT(cfs_list_empty(&lock->l_res_link));
885 LASSERT(cfs_list_empty(&lock->l_sl_mode));
886 LASSERT(cfs_list_empty(&lock->l_sl_policy));
888 cfs_list_add(&lock->l_res_link, prev->res_link);
889 cfs_list_add(&lock->l_sl_mode, prev->mode_link);
890 cfs_list_add(&lock->l_sl_policy, prev->policy_link);
895 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
897 struct sl_insert_point prev;
900 LASSERT(lock->l_req_mode == lock->l_granted_mode);
902 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
903 ldlm_granted_list_add_lock(lock, &prev);
908 * - ldlm_lock_enqueue
909 * - ldlm_reprocess_queue
910 * - ldlm_lock_convert
912 * must be called with lr_lock held
914 void ldlm_grant_lock(struct ldlm_lock *lock, cfs_list_t *work_list)
916 struct ldlm_resource *res = lock->l_resource;
919 check_res_locked(res);
921 lock->l_granted_mode = lock->l_req_mode;
922 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
923 ldlm_grant_lock_with_skiplist(lock);
924 else if (res->lr_type == LDLM_EXTENT)
925 ldlm_extent_add_lock(res, lock);
927 ldlm_resource_add_lock(res, &res->lr_granted, lock);
929 if (lock->l_granted_mode < res->lr_most_restr)
930 res->lr_most_restr = lock->l_granted_mode;
932 if (work_list && lock->l_completion_ast != NULL)
933 ldlm_add_ast_work_item(lock, NULL, work_list);
935 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
939 /* returns a referenced lock or NULL. See the flag descriptions below, in the
940 * comment above ldlm_lock_match */
941 static struct ldlm_lock *search_queue(cfs_list_t *queue,
943 ldlm_policy_data_t *policy,
944 struct ldlm_lock *old_lock,
945 int flags, int unref)
947 struct ldlm_lock *lock;
950 cfs_list_for_each(tmp, queue) {
953 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
955 if (lock == old_lock)
958 /* llite sometimes wants to match locks that will be
959 * canceled when their users drop, but we allow it to match
960 * if it passes in CBPENDING and the lock still has users.
961 * this is generally only going to be used by children
962 * whose parents already hold a lock so forward progress
963 * can still happen. */
964 if (lock->l_flags & LDLM_FL_CBPENDING &&
965 !(flags & LDLM_FL_CBPENDING))
967 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
968 lock->l_readers == 0 && lock->l_writers == 0)
971 if (!(lock->l_req_mode & *mode))
973 match = lock->l_req_mode;
975 if (lock->l_resource->lr_type == LDLM_EXTENT &&
976 (lock->l_policy_data.l_extent.start >
977 policy->l_extent.start ||
978 lock->l_policy_data.l_extent.end < policy->l_extent.end))
981 if (unlikely(match == LCK_GROUP) &&
982 lock->l_resource->lr_type == LDLM_EXTENT &&
983 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
986 /* We match if we have existing lock with same or wider set
988 if (lock->l_resource->lr_type == LDLM_IBITS &&
989 ((lock->l_policy_data.l_inodebits.bits &
990 policy->l_inodebits.bits) !=
991 policy->l_inodebits.bits))
995 (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED)))
998 if ((flags & LDLM_FL_LOCAL_ONLY) &&
999 !(lock->l_flags & LDLM_FL_LOCAL))
1002 if (flags & LDLM_FL_TEST_LOCK) {
1003 LDLM_LOCK_GET(lock);
1004 ldlm_lock_touch_in_lru(lock);
1006 ldlm_lock_addref_internal_nolock(lock, match);
1015 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1017 lock->l_flags |= LDLM_FL_LVB_READY;
1018 cfs_waitq_signal(&lock->l_waitq);
1021 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1023 lock_res_and_lock(lock);
1024 ldlm_lock_allow_match_locked(lock);
1025 unlock_res_and_lock(lock);
1028 /* Can be called in two ways:
1030 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1031 * for a duplicate of.
1033 * Otherwise, all of the fields must be filled in, to match against.
1035 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1036 * server (ie, connh is NULL)
1037 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1038 * list will be considered
1039 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1040 * to be canceled can still be matched as long as they still have reader
1041 * or writer refernces
1042 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1043 * just tell us if we would have matched.
1045 * Returns 1 if it finds an already-existing lock that is compatible; in this
1046 * case, lockh is filled in with a addref()ed lock
1048 * we also check security context, if that failed we simply return 0 (to keep
1049 * caller code unchanged), the context failure will be discovered by caller
1052 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1053 const struct ldlm_res_id *res_id, ldlm_type_t type,
1054 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1055 struct lustre_handle *lockh, int unref)
1057 struct ldlm_resource *res;
1058 struct ldlm_lock *lock, *old_lock = NULL;
1063 old_lock = ldlm_handle2lock(lockh);
1066 ns = ldlm_lock_to_ns(old_lock);
1067 res_id = &old_lock->l_resource->lr_name;
1068 type = old_lock->l_resource->lr_type;
1069 mode = old_lock->l_req_mode;
1072 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1074 LASSERT(old_lock == NULL);
1078 LDLM_RESOURCE_ADDREF(res);
1081 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1085 if (flags & LDLM_FL_BLOCK_GRANTED)
1087 lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1091 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1099 LDLM_RESOURCE_DELREF(res);
1100 ldlm_resource_putref(res);
1103 ldlm_lock2handle(lock, lockh);
1104 if ((flags & LDLM_FL_LVB_READY) &&
1105 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1106 struct l_wait_info lwi;
1107 if (lock->l_completion_ast) {
1108 int err = lock->l_completion_ast(lock,
1109 LDLM_FL_WAIT_NOREPROC,
1112 if (flags & LDLM_FL_TEST_LOCK)
1113 LDLM_LOCK_RELEASE(lock);
1115 ldlm_lock_decref_internal(lock,
1122 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1123 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1125 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1126 l_wait_event(lock->l_waitq,
1127 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1132 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1133 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1134 res_id->name[2] : policy->l_extent.start,
1135 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1136 res_id->name[3] : policy->l_extent.end);
1138 /* check user's security context */
1139 if (lock->l_conn_export &&
1140 sptlrpc_import_check_ctx(
1141 class_exp2cliimp(lock->l_conn_export))) {
1142 if (!(flags & LDLM_FL_TEST_LOCK))
1143 ldlm_lock_decref_internal(lock, mode);
1147 if (flags & LDLM_FL_TEST_LOCK)
1148 LDLM_LOCK_RELEASE(lock);
1150 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1151 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1152 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1153 type, mode, res_id->name[0], res_id->name[1],
1154 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1155 res_id->name[2] :policy->l_extent.start,
1156 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1157 res_id->name[3] : policy->l_extent.end);
1160 LDLM_LOCK_PUT(old_lock);
1162 return rc ? mode : 0;
1165 /* Returns a referenced lock */
1166 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1167 const struct ldlm_res_id *res_id,
1170 const struct ldlm_callback_suite *cbs,
1171 void *data, __u32 lvb_len)
1173 struct ldlm_lock *lock;
1174 struct ldlm_resource *res;
1177 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1181 lock = ldlm_lock_new(res);
1182 ldlm_resource_putref(res);
1187 lock->l_req_mode = mode;
1188 lock->l_ast_data = data;
1189 lock->l_pid = cfs_curproc_pid();
1191 lock->l_blocking_ast = cbs->lcs_blocking;
1192 lock->l_completion_ast = cbs->lcs_completion;
1193 lock->l_glimpse_ast = cbs->lcs_glimpse;
1194 lock->l_weigh_ast = cbs->lcs_weigh;
1197 lock->l_tree_node = NULL;
1198 /* if this is the extent lock, allocate the interval tree node */
1199 if (type == LDLM_EXTENT) {
1200 if (ldlm_interval_alloc(lock) == NULL)
1205 lock->l_lvb_len = lvb_len;
1206 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1207 if (lock->l_lvb_data == NULL)
1211 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
1217 ldlm_lock_destroy(lock);
1218 LDLM_LOCK_RELEASE(lock);
1222 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1223 struct ldlm_lock **lockp,
1224 void *cookie, int *flags)
1226 struct ldlm_lock *lock = *lockp;
1227 struct ldlm_resource *res = lock->l_resource;
1228 int local = ns_is_client(ldlm_res_to_ns(res));
1229 ldlm_processing_policy policy;
1230 ldlm_error_t rc = ELDLM_OK;
1231 struct ldlm_interval *node = NULL;
1234 lock->l_last_activity = cfs_time_current_sec();
1235 /* policies are not executed on the client or during replay */
1236 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1237 && !local && ns->ns_policy) {
1238 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1240 if (rc == ELDLM_LOCK_REPLACED) {
1241 /* The lock that was returned has already been granted,
1242 * and placed into lockp. If it's not the same as the
1243 * one we passed in, then destroy the old one and our
1244 * work here is done. */
1245 if (lock != *lockp) {
1246 ldlm_lock_destroy(lock);
1247 LDLM_LOCK_RELEASE(lock);
1249 *flags |= LDLM_FL_LOCK_CHANGED;
1251 } else if (rc != ELDLM_OK ||
1252 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1253 ldlm_lock_destroy(lock);
1258 /* For a replaying lock, it might be already in granted list. So
1259 * unlinking the lock will cause the interval node to be freed, we
1260 * have to allocate the interval node early otherwise we can't regrant
1261 * this lock in the future. - jay */
1262 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1263 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1265 lock_res_and_lock(lock);
1266 if (local && lock->l_req_mode == lock->l_granted_mode) {
1267 /* The server returned a blocked lock, but it was granted
1268 * before we got a chance to actually enqueue it. We don't
1269 * need to do anything else. */
1270 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1271 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1272 GOTO(out, ELDLM_OK);
1275 ldlm_resource_unlink_lock(lock);
1276 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1278 ldlm_lock_destroy_nolock(lock);
1279 GOTO(out, rc = -ENOMEM);
1282 CFS_INIT_LIST_HEAD(&node->li_group);
1283 ldlm_interval_attach(node, lock);
1287 /* Some flags from the enqueue want to make it into the AST, via the
1288 * lock's l_flags. */
1289 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1291 /* This distinction between local lock trees is very important; a client
1292 * namespace only has information about locks taken by that client, and
1293 * thus doesn't have enough information to decide for itself if it can
1294 * be granted (below). In this case, we do exactly what the server
1295 * tells us to do, as dictated by the 'flags'.
1297 * We do exactly the same thing during recovery, when the server is
1298 * more or less trusting the clients not to lie.
1300 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1301 * granted/converting queues. */
1303 if (*flags & LDLM_FL_BLOCK_CONV)
1304 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1305 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1306 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1308 ldlm_grant_lock(lock, NULL);
1309 GOTO(out, ELDLM_OK);
1310 } else if (*flags & LDLM_FL_REPLAY) {
1311 if (*flags & LDLM_FL_BLOCK_CONV) {
1312 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1313 GOTO(out, ELDLM_OK);
1314 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1315 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1316 GOTO(out, ELDLM_OK);
1317 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1318 ldlm_grant_lock(lock, NULL);
1319 GOTO(out, ELDLM_OK);
1321 /* If no flags, fall through to normal enqueue path. */
1324 policy = ldlm_processing_policy_table[res->lr_type];
1325 policy(lock, flags, 1, &rc, NULL);
1328 unlock_res_and_lock(lock);
1330 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1334 /* Must be called with namespace taken: queue is waiting or converting. */
1335 int ldlm_reprocess_queue(struct ldlm_resource *res, cfs_list_t *queue,
1336 cfs_list_t *work_list)
1338 cfs_list_t *tmp, *pos;
1339 ldlm_processing_policy policy;
1341 int rc = LDLM_ITER_CONTINUE;
1345 check_res_locked(res);
1347 policy = ldlm_processing_policy_table[res->lr_type];
1350 cfs_list_for_each_safe(tmp, pos, queue) {
1351 struct ldlm_lock *pending;
1352 pending = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1354 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1357 rc = policy(pending, &flags, 0, &err, work_list);
1358 if (rc != LDLM_ITER_CONTINUE)
1365 /* Helper function for ldlm_run_ast_work().
1367 * Send an existing rpc set specified by @arg->set and then
1368 * destroy it. Create new one if @do_create flag is set. */
1370 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1374 ptlrpc_set_wait(arg->set);
1375 if (arg->type == LDLM_BL_CALLBACK)
1376 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1377 ptlrpc_set_destroy(arg->set);
1380 arg->set = ptlrpc_prep_set();
1386 ldlm_work_bl_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1388 struct ldlm_lock_desc d;
1389 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1393 /* nobody should touch l_bl_ast */
1394 lock_res_and_lock(lock);
1395 cfs_list_del_init(&lock->l_bl_ast);
1397 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1398 LASSERT(lock->l_bl_ast_run == 0);
1399 LASSERT(lock->l_blocking_lock);
1400 lock->l_bl_ast_run++;
1401 unlock_res_and_lock(lock);
1403 ldlm_lock2desc(lock->l_blocking_lock, &d);
1405 lock->l_blocking_ast(lock, &d, (void *)arg,
1407 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1408 lock->l_blocking_lock = NULL;
1409 LDLM_LOCK_RELEASE(lock);
1415 ldlm_work_cp_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1417 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock, l_cp_ast);
1418 ldlm_completion_callback completion_callback;
1422 /* It's possible to receive a completion AST before we've set
1423 * the l_completion_ast pointer: either because the AST arrived
1424 * before the reply, or simply because there's a small race
1425 * window between receiving the reply and finishing the local
1426 * enqueue. (bug 842)
1428 * This can't happen with the blocking_ast, however, because we
1429 * will never call the local blocking_ast until we drop our
1430 * reader/writer reference, which we won't do until we get the
1431 * reply and finish enqueueing. */
1433 /* nobody should touch l_cp_ast */
1434 lock_res_and_lock(lock);
1435 cfs_list_del_init(&lock->l_cp_ast);
1436 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1437 /* save l_completion_ast since it can be changed by
1438 * mds_intent_policy(), see bug 14225 */
1439 completion_callback = lock->l_completion_ast;
1440 lock->l_flags &= ~LDLM_FL_CP_REQD;
1441 unlock_res_and_lock(lock);
1443 if (completion_callback != NULL) {
1444 completion_callback(lock, 0, (void *)arg);
1447 LDLM_LOCK_RELEASE(lock);
1453 ldlm_work_revoke_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1455 struct ldlm_lock_desc desc;
1456 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1460 cfs_list_del_init(&lock->l_rk_ast);
1462 /* the desc just pretend to exclusive */
1463 ldlm_lock2desc(lock, &desc);
1464 desc.l_req_mode = LCK_EX;
1465 desc.l_granted_mode = 0;
1467 lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1468 LDLM_LOCK_RELEASE(lock);
1473 int ldlm_run_ast_work(cfs_list_t *rpc_list, ldlm_desc_ast_t ast_type)
1475 struct ldlm_cb_set_arg arg;
1476 cfs_list_t *tmp, *pos;
1477 int (*work_ast_lock)(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg);
1481 if (cfs_list_empty(rpc_list))
1484 arg.set = ptlrpc_prep_set();
1485 if (NULL == arg.set)
1487 cfs_atomic_set(&arg.restart, 0);
1489 case LDLM_WORK_BL_AST:
1490 arg.type = LDLM_BL_CALLBACK;
1491 work_ast_lock = ldlm_work_bl_ast_lock;
1493 case LDLM_WORK_CP_AST:
1494 arg.type = LDLM_CP_CALLBACK;
1495 work_ast_lock = ldlm_work_cp_ast_lock;
1497 case LDLM_WORK_REVOKE_AST:
1498 arg.type = LDLM_BL_CALLBACK;
1499 work_ast_lock = ldlm_work_revoke_ast_lock;
1506 cfs_list_for_each_safe(tmp, pos, rpc_list) {
1507 ast_count += work_ast_lock(tmp, &arg);
1509 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1510 * and create a new set for requests that remained in
1512 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1513 ldlm_send_and_maybe_create_set(&arg, 1);
1519 ldlm_send_and_maybe_create_set(&arg, 0);
1521 /* In case when number of ASTs is multiply of
1522 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1523 * @arg.set must be destroyed here, otherwise we get
1524 * write memory leaking. */
1525 ptlrpc_set_destroy(arg.set);
1527 RETURN(cfs_atomic_read(&arg.restart) ? -ERESTART : 0);
1530 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1532 ldlm_reprocess_all(res);
1533 return LDLM_ITER_CONTINUE;
1536 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1545 cfs_spin_lock(&ns->ns_hash_lock);
1546 for (i = 0; i < RES_HASH_SIZE; i++) {
1547 tmp = ns->ns_hash[i].next;
1548 while (tmp != &(ns->ns_hash[i])) {
1549 struct ldlm_resource *res =
1550 cfs_list_entry(tmp, struct ldlm_resource,
1553 ldlm_resource_getref(res);
1554 cfs_spin_unlock(&ns->ns_hash_lock);
1555 LDLM_RESOURCE_ADDREF(res);
1557 rc = reprocess_one_queue(res, NULL);
1559 LDLM_RESOURCE_DELREF(res);
1560 cfs_spin_lock(&ns->ns_hash_lock);
1562 ldlm_resource_putref_locked(res);
1564 if (rc == LDLM_ITER_STOP)
1569 cfs_spin_unlock(&ns->ns_hash_lock);
1573 void ldlm_reprocess_all(struct ldlm_resource *res)
1575 CFS_LIST_HEAD(rpc_list);
1579 /* Local lock trees don't get reprocessed. */
1580 if (ns_is_client(ldlm_res_to_ns(res))) {
1587 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1588 if (rc == LDLM_ITER_CONTINUE)
1589 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1592 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1593 if (rc == -ERESTART) {
1594 LASSERT(cfs_list_empty(&rpc_list));
1600 void ldlm_cancel_callback(struct ldlm_lock *lock)
1602 check_res_locked(lock->l_resource);
1603 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1604 lock->l_flags |= LDLM_FL_CANCEL;
1605 if (lock->l_blocking_ast) {
1606 // l_check_no_ns_lock(ns);
1607 unlock_res_and_lock(lock);
1608 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1610 lock_res_and_lock(lock);
1612 LDLM_DEBUG(lock, "no blocking ast");
1615 lock->l_flags |= LDLM_FL_BL_DONE;
1618 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1620 if (req->l_resource->lr_type != LDLM_PLAIN &&
1621 req->l_resource->lr_type != LDLM_IBITS)
1624 cfs_list_del_init(&req->l_sl_policy);
1625 cfs_list_del_init(&req->l_sl_mode);
1628 void ldlm_lock_cancel(struct ldlm_lock *lock)
1630 struct ldlm_resource *res;
1631 struct ldlm_namespace *ns;
1634 lock_res_and_lock(lock);
1636 res = lock->l_resource;
1637 ns = ldlm_res_to_ns(res);
1639 /* Please do not, no matter how tempting, remove this LBUG without
1640 * talking to me first. -phik */
1641 if (lock->l_readers || lock->l_writers) {
1642 LDLM_ERROR(lock, "lock still has references");
1646 ldlm_del_waiting_lock(lock);
1648 /* Releases cancel callback. */
1649 ldlm_cancel_callback(lock);
1651 /* Yes, second time, just in case it was added again while we were
1652 running with no res lock in ldlm_cancel_callback */
1653 ldlm_del_waiting_lock(lock);
1654 ldlm_resource_unlink_lock(lock);
1655 ldlm_lock_destroy_nolock(lock);
1657 if (lock->l_granted_mode == lock->l_req_mode)
1658 ldlm_pool_del(&ns->ns_pool, lock);
1660 /* Make sure we will not be called again for same lock what is possible
1661 * if not to zero out lock->l_granted_mode */
1662 lock->l_granted_mode = LCK_MINMODE;
1663 unlock_res_and_lock(lock);
1668 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1670 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1676 lock->l_ast_data = data;
1677 LDLM_LOCK_PUT(lock);
1681 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1682 cfs_hlist_node_t *hnode, void *data)
1685 struct obd_export *exp = data;
1686 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
1687 struct ldlm_resource *res;
1689 res = ldlm_resource_getref(lock->l_resource);
1690 LDLM_LOCK_GET(lock);
1692 LDLM_DEBUG(lock, "export %p", exp);
1693 ldlm_res_lvbo_update(res, NULL, 1);
1694 ldlm_lock_cancel(lock);
1695 ldlm_reprocess_all(res);
1696 ldlm_resource_putref(res);
1697 LDLM_LOCK_RELEASE(lock);
1701 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1703 cfs_hash_for_each_empty(exp->exp_lock_hash,
1704 ldlm_cancel_locks_for_export_cb, exp);
1708 * Downgrade an exclusive lock.
1710 * A fast variant of ldlm_lock_convert for convertion of exclusive
1711 * locks. The convertion is always successful.
1713 * \param lock A lock to convert
1714 * \param new_mode new lock mode
1716 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1718 struct ldlm_namespace *ns;
1721 LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1722 LASSERT(new_mode == LCK_COS);
1724 lock_res_and_lock(lock);
1725 ldlm_resource_unlink_lock(lock);
1727 * Remove the lock from pool as it will be added again in
1728 * ldlm_grant_lock() called below.
1730 ns = ldlm_lock_to_ns(lock);
1731 ldlm_pool_del(&ns->ns_pool, lock);
1733 lock->l_req_mode = new_mode;
1734 ldlm_grant_lock(lock, NULL);
1735 unlock_res_and_lock(lock);
1736 ldlm_reprocess_all(lock->l_resource);
1741 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1744 CFS_LIST_HEAD(rpc_list);
1745 struct ldlm_resource *res;
1746 struct ldlm_namespace *ns;
1749 struct sl_insert_point prev;
1751 struct ldlm_interval *node;
1754 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1755 *flags |= LDLM_FL_BLOCK_GRANTED;
1756 RETURN(lock->l_resource);
1759 /* I can't check the type of lock here because the bitlock of lock
1760 * is not held here, so do the allocation blindly. -jay */
1761 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1762 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1765 LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1766 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1768 lock_res_and_lock(lock);
1770 res = lock->l_resource;
1771 ns = ldlm_res_to_ns(res);
1773 old_mode = lock->l_req_mode;
1774 lock->l_req_mode = new_mode;
1775 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1776 /* remember the lock position where the lock might be
1777 * added back to the granted list later and also
1778 * remember the join mode for skiplist fixing. */
1779 prev.res_link = lock->l_res_link.prev;
1780 prev.mode_link = lock->l_sl_mode.prev;
1781 prev.policy_link = lock->l_sl_policy.prev;
1782 ldlm_resource_unlink_lock(lock);
1784 ldlm_resource_unlink_lock(lock);
1785 if (res->lr_type == LDLM_EXTENT) {
1786 /* FIXME: ugly code, I have to attach the lock to a
1787 * interval node again since perhaps it will be granted
1789 CFS_INIT_LIST_HEAD(&node->li_group);
1790 ldlm_interval_attach(node, lock);
1796 * Remove old lock from the pool before adding the lock with new
1797 * mode below in ->policy()
1799 ldlm_pool_del(&ns->ns_pool, lock);
1801 /* If this is a local resource, put it on the appropriate list. */
1802 if (ns_is_client(ldlm_res_to_ns(res))) {
1803 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1804 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1806 /* This should never happen, because of the way the
1807 * server handles conversions. */
1808 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1812 ldlm_grant_lock(lock, &rpc_list);
1814 /* FIXME: completion handling not with ns_lock held ! */
1815 if (lock->l_completion_ast)
1816 lock->l_completion_ast(lock, 0, NULL);
1820 ldlm_processing_policy policy;
1821 policy = ldlm_processing_policy_table[res->lr_type];
1822 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1823 if (rc == LDLM_ITER_STOP) {
1824 lock->l_req_mode = old_mode;
1825 if (res->lr_type == LDLM_EXTENT)
1826 ldlm_extent_add_lock(res, lock);
1828 ldlm_granted_list_add_lock(lock, &prev);
1832 *flags |= LDLM_FL_BLOCK_GRANTED;
1836 unlock_res_and_lock(lock);
1839 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1841 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1845 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1847 struct obd_device *obd = NULL;
1849 if (!((libcfs_debug | D_ERROR) & level))
1853 CDEBUG(level, " NULL LDLM lock\n");
1857 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1858 lock, lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1860 if (lock->l_conn_export != NULL)
1861 obd = lock->l_conn_export->exp_obd;
1862 if (lock->l_export && lock->l_export->exp_connection) {
1863 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1864 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1865 lock->l_remote_handle.cookie);
1866 } else if (obd == NULL) {
1867 CDEBUG(level, " Node: local\n");
1869 struct obd_import *imp = obd->u.cli.cl_import;
1870 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1871 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1872 lock->l_remote_handle.cookie);
1874 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1876 lock->l_resource->lr_name.name[0],
1877 lock->l_resource->lr_name.name[1],
1878 lock->l_resource->lr_name.name[2]);
1879 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1880 "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1881 ldlm_lockname[lock->l_granted_mode],
1882 cfs_atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1884 if (lock->l_resource->lr_type == LDLM_EXTENT)
1885 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1886 " (req "LPU64"-"LPU64")\n",
1887 lock->l_policy_data.l_extent.start,
1888 lock->l_policy_data.l_extent.end,
1889 lock->l_req_extent.start, lock->l_req_extent.end);
1890 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1891 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1892 lock->l_policy_data.l_flock.pid,
1893 lock->l_policy_data.l_flock.start,
1894 lock->l_policy_data.l_flock.end);
1895 else if (lock->l_resource->lr_type == LDLM_IBITS)
1896 CDEBUG(level, " Bits: "LPX64"\n",
1897 lock->l_policy_data.l_inodebits.bits);
1900 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1902 struct ldlm_lock *lock;
1904 if (!((libcfs_debug | D_ERROR) & level))
1907 lock = ldlm_handle2lock(lockh);
1911 ldlm_lock_dump(D_OTHER, lock, 0);
1913 LDLM_LOCK_PUT(lock);
1916 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1917 struct libcfs_debug_msg_data *data, const char *fmt,
1921 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1923 va_start(args, fmt);
1925 if (lock->l_resource == NULL) {
1926 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1927 data->msg_fn, data->msg_line, fmt, args,
1928 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1929 "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
1930 LPX64" expref: %d pid: %u timeout: %lu\n", lock,
1931 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1932 lock->l_readers, lock->l_writers,
1933 ldlm_lockname[lock->l_granted_mode],
1934 ldlm_lockname[lock->l_req_mode],
1935 lock->l_flags, lock->l_remote_handle.cookie,
1937 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1938 lock->l_pid, lock->l_callback_timeout);
1943 switch (lock->l_resource->lr_type) {
1945 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1946 data->msg_fn, data->msg_line, fmt, args,
1947 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1948 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1949 "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
1950 " expref: %d pid: %u timeout %lu\n",
1951 ldlm_lock_to_ns_name(lock), lock,
1952 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1953 lock->l_readers, lock->l_writers,
1954 ldlm_lockname[lock->l_granted_mode],
1955 ldlm_lockname[lock->l_req_mode],
1956 lock->l_resource->lr_name.name[0],
1957 lock->l_resource->lr_name.name[1],
1958 cfs_atomic_read(&lock->l_resource->lr_refcount),
1959 ldlm_typename[lock->l_resource->lr_type],
1960 lock->l_policy_data.l_extent.start,
1961 lock->l_policy_data.l_extent.end,
1962 lock->l_req_extent.start, lock->l_req_extent.end,
1963 lock->l_flags, lock->l_remote_handle.cookie,
1965 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1966 lock->l_pid, lock->l_callback_timeout);
1970 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1971 data->msg_fn, data->msg_line, fmt, args,
1972 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1973 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1974 "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
1975 " expref: %d pid: %u timeout: %lu\n",
1976 ldlm_lock_to_ns_name(lock), lock,
1977 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1978 lock->l_readers, lock->l_writers,
1979 ldlm_lockname[lock->l_granted_mode],
1980 ldlm_lockname[lock->l_req_mode],
1981 lock->l_resource->lr_name.name[0],
1982 lock->l_resource->lr_name.name[1],
1983 cfs_atomic_read(&lock->l_resource->lr_refcount),
1984 ldlm_typename[lock->l_resource->lr_type],
1985 lock->l_policy_data.l_flock.pid,
1986 lock->l_policy_data.l_flock.start,
1987 lock->l_policy_data.l_flock.end,
1988 lock->l_flags, lock->l_remote_handle.cookie,
1990 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
1991 lock->l_pid, lock->l_callback_timeout);
1995 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
1996 data->msg_fn, data->msg_line, fmt, args,
1997 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1998 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1999 "flags: "LPX64" remote: "LPX64" expref: %d "
2000 "pid: %u timeout: %lu\n",
2001 ldlm_lock_to_ns_name(lock),
2002 lock, lock->l_handle.h_cookie,
2003 cfs_atomic_read (&lock->l_refc),
2004 lock->l_readers, lock->l_writers,
2005 ldlm_lockname[lock->l_granted_mode],
2006 ldlm_lockname[lock->l_req_mode],
2007 lock->l_resource->lr_name.name[0],
2008 lock->l_resource->lr_name.name[1],
2009 lock->l_policy_data.l_inodebits.bits,
2010 cfs_atomic_read(&lock->l_resource->lr_refcount),
2011 ldlm_typename[lock->l_resource->lr_type],
2012 lock->l_flags, lock->l_remote_handle.cookie,
2014 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2015 lock->l_pid, lock->l_callback_timeout);
2019 libcfs_debug_vmsg2(cdls, data->msg_subsys, level,data->msg_file,
2020 data->msg_fn, data->msg_line, fmt, args,
2021 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2022 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2023 "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
2024 ldlm_lock_to_ns_name(lock),
2025 lock, lock->l_handle.h_cookie,
2026 cfs_atomic_read (&lock->l_refc),
2027 lock->l_readers, lock->l_writers,
2028 ldlm_lockname[lock->l_granted_mode],
2029 ldlm_lockname[lock->l_req_mode],
2030 lock->l_resource->lr_name.name[0],
2031 lock->l_resource->lr_name.name[1],
2032 cfs_atomic_read(&lock->l_resource->lr_refcount),
2033 ldlm_typename[lock->l_resource->lr_type],
2034 lock->l_flags, lock->l_remote_handle.cookie,
2036 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2037 lock->l_pid, lock->l_callback_timeout);
2042 EXPORT_SYMBOL(_ldlm_lock_debug);