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.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/ldlm/ldlm_lock.c
40 * Author: Peter Braam <braam@clusterfs.com>
41 * Author: Phil Schwan <phil@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_LDLM
47 # include <libcfs/libcfs.h>
48 # 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 static ldlm_policy_wire_to_local_t ldlm_policy_wire18_to_local[] = {
77 [LDLM_PLAIN - LDLM_MIN_TYPE] ldlm_plain_policy_wire_to_local,
78 [LDLM_EXTENT - LDLM_MIN_TYPE] ldlm_extent_policy_wire_to_local,
79 [LDLM_FLOCK - LDLM_MIN_TYPE] ldlm_flock_policy_wire18_to_local,
80 [LDLM_IBITS - LDLM_MIN_TYPE] ldlm_ibits_policy_wire_to_local,
83 static ldlm_policy_wire_to_local_t ldlm_policy_wire21_to_local[] = {
84 [LDLM_PLAIN - LDLM_MIN_TYPE] ldlm_plain_policy_wire_to_local,
85 [LDLM_EXTENT - LDLM_MIN_TYPE] ldlm_extent_policy_wire_to_local,
86 [LDLM_FLOCK - LDLM_MIN_TYPE] ldlm_flock_policy_wire21_to_local,
87 [LDLM_IBITS - LDLM_MIN_TYPE] ldlm_ibits_policy_wire_to_local,
90 static ldlm_policy_local_to_wire_t ldlm_policy_local_to_wire[] = {
91 [LDLM_PLAIN - LDLM_MIN_TYPE] ldlm_plain_policy_local_to_wire,
92 [LDLM_EXTENT - LDLM_MIN_TYPE] ldlm_extent_policy_local_to_wire,
93 [LDLM_FLOCK - LDLM_MIN_TYPE] ldlm_flock_policy_local_to_wire,
94 [LDLM_IBITS - LDLM_MIN_TYPE] ldlm_ibits_policy_local_to_wire,
98 * Converts lock policy from local format to on the wire lock_desc format
100 void ldlm_convert_policy_to_wire(ldlm_type_t type,
101 const ldlm_policy_data_t *lpolicy,
102 ldlm_wire_policy_data_t *wpolicy)
104 ldlm_policy_local_to_wire_t convert;
106 convert = ldlm_policy_local_to_wire[type - LDLM_MIN_TYPE];
108 convert(lpolicy, wpolicy);
112 * Converts lock policy from on the wire lock_desc format to local format
114 void ldlm_convert_policy_to_local(struct obd_export *exp, ldlm_type_t type,
115 const ldlm_wire_policy_data_t *wpolicy,
116 ldlm_policy_data_t *lpolicy)
118 ldlm_policy_wire_to_local_t convert;
121 /** some badnes for 2.0.0 clients, but 2.0.0 isn't supported */
122 new_client = (exp->exp_connect_flags & OBD_CONNECT_FULL20) != 0;
124 convert = ldlm_policy_wire21_to_local[type - LDLM_MIN_TYPE];
126 convert = ldlm_policy_wire18_to_local[type - LDLM_MIN_TYPE];
128 convert(wpolicy, lpolicy);
131 char *ldlm_it2str(int it)
138 case (IT_OPEN | IT_CREAT):
153 CERROR("Unknown intent %d\n", it);
158 extern cfs_mem_cache_t *ldlm_lock_slab;
160 static ldlm_processing_policy ldlm_processing_policy_table[] = {
161 [LDLM_PLAIN] ldlm_process_plain_lock,
162 [LDLM_EXTENT] ldlm_process_extent_lock,
164 [LDLM_FLOCK] ldlm_process_flock_lock,
166 [LDLM_IBITS] ldlm_process_inodebits_lock,
169 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
171 return ldlm_processing_policy_table[res->lr_type];
174 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
180 * REFCOUNTED LOCK OBJECTS
185 * Lock refcounts, during creation:
186 * - one special one for allocation, dec'd only once in destroy
187 * - one for being a lock that's in-use
188 * - one for the addref associated with a new lock
190 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
192 cfs_atomic_inc(&lock->l_refc);
196 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
198 LASSERT(size == sizeof(*lock));
199 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
202 void ldlm_lock_put(struct ldlm_lock *lock)
206 LASSERT(lock->l_resource != LP_POISON);
207 LASSERT(cfs_atomic_read(&lock->l_refc) > 0);
208 if (cfs_atomic_dec_and_test(&lock->l_refc)) {
209 struct ldlm_resource *res;
212 "final lock_put on destroyed lock, freeing it.");
214 res = lock->l_resource;
215 LASSERT(lock->l_destroyed);
216 LASSERT(cfs_list_empty(&lock->l_res_link));
217 LASSERT(cfs_list_empty(&lock->l_pending_chain));
219 lprocfs_counter_decr(ldlm_res_to_ns(res)->ns_stats,
221 lu_ref_del(&res->lr_reference, "lock", lock);
222 ldlm_resource_putref(res);
223 lock->l_resource = NULL;
224 if (lock->l_export) {
225 class_export_lock_put(lock->l_export, lock);
226 lock->l_export = NULL;
229 if (lock->l_lvb_data != NULL)
230 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
232 ldlm_interval_free(ldlm_interval_detach(lock));
233 lu_ref_fini(&lock->l_reference);
234 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
241 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
244 if (!cfs_list_empty(&lock->l_lru)) {
245 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
247 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
248 cfs_list_del_init(&lock->l_lru);
249 if (lock->l_flags & LDLM_FL_SKIPPED)
250 lock->l_flags &= ~LDLM_FL_SKIPPED;
251 LASSERT(ns->ns_nr_unused > 0);
258 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
260 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
264 if (lock->l_ns_srv) {
265 LASSERT(cfs_list_empty(&lock->l_lru));
269 cfs_spin_lock(&ns->ns_lock);
270 rc = ldlm_lock_remove_from_lru_nolock(lock);
271 cfs_spin_unlock(&ns->ns_lock);
276 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
278 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
280 lock->l_last_used = cfs_time_current();
281 LASSERT(cfs_list_empty(&lock->l_lru));
282 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
283 cfs_list_add_tail(&lock->l_lru, &ns->ns_unused_list);
284 LASSERT(ns->ns_nr_unused >= 0);
288 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
290 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
293 cfs_spin_lock(&ns->ns_lock);
294 ldlm_lock_add_to_lru_nolock(lock);
295 cfs_spin_unlock(&ns->ns_lock);
299 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
301 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
304 if (lock->l_ns_srv) {
305 LASSERT(cfs_list_empty(&lock->l_lru));
310 cfs_spin_lock(&ns->ns_lock);
311 if (!cfs_list_empty(&lock->l_lru)) {
312 ldlm_lock_remove_from_lru_nolock(lock);
313 ldlm_lock_add_to_lru_nolock(lock);
315 cfs_spin_unlock(&ns->ns_lock);
319 /* This used to have a 'strict' flag, which recovery would use to mark an
320 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
321 * shall explain why it's gone: with the new hash table scheme, once you call
322 * ldlm_lock_destroy, you can never drop your final references on this lock.
323 * Because it's not in the hash table anymore. -phil */
324 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
328 if (lock->l_readers || lock->l_writers) {
329 LDLM_ERROR(lock, "lock still has references");
330 ldlm_lock_dump(D_ERROR, lock, 0);
334 if (!cfs_list_empty(&lock->l_res_link)) {
335 LDLM_ERROR(lock, "lock still on resource");
336 ldlm_lock_dump(D_ERROR, lock, 0);
340 if (lock->l_destroyed) {
341 LASSERT(cfs_list_empty(&lock->l_lru));
345 lock->l_destroyed = 1;
347 if (lock->l_export && lock->l_export->exp_lock_hash &&
348 !cfs_hlist_unhashed(&lock->l_exp_hash))
349 cfs_hash_del(lock->l_export->exp_lock_hash,
350 &lock->l_remote_handle, &lock->l_exp_hash);
352 ldlm_lock_remove_from_lru(lock);
353 class_handle_unhash(&lock->l_handle);
356 /* Wake anyone waiting for this lock */
357 /* FIXME: I should probably add yet another flag, instead of using
358 * l_export to only call this on clients */
360 class_export_put(lock->l_export);
361 lock->l_export = NULL;
362 if (lock->l_export && lock->l_completion_ast)
363 lock->l_completion_ast(lock, 0);
369 void ldlm_lock_destroy(struct ldlm_lock *lock)
373 lock_res_and_lock(lock);
374 first = ldlm_lock_destroy_internal(lock);
375 unlock_res_and_lock(lock);
377 /* drop reference from hashtable only for first destroy */
379 lu_ref_del(&lock->l_reference, "hash", lock);
380 LDLM_LOCK_RELEASE(lock);
385 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
389 first = ldlm_lock_destroy_internal(lock);
390 /* drop reference from hashtable only for first destroy */
392 lu_ref_del(&lock->l_reference, "hash", lock);
393 LDLM_LOCK_RELEASE(lock);
398 /* this is called by portals_handle2object with the handle lock taken */
399 static void lock_handle_addref(void *lock)
401 LDLM_LOCK_GET((struct ldlm_lock *)lock);
405 * usage: pass in a resource on which you have done ldlm_resource_get
406 * new lock will take over the refcount.
407 * returns: lock with refcount 2 - one for current caller and one for remote
409 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
411 struct ldlm_lock *lock;
414 if (resource == NULL)
417 OBD_SLAB_ALLOC_PTR_GFP(lock, ldlm_lock_slab, CFS_ALLOC_IO);
421 cfs_spin_lock_init(&lock->l_lock);
422 lock->l_resource = resource;
423 lu_ref_add(&resource->lr_reference, "lock", lock);
425 cfs_atomic_set(&lock->l_refc, 2);
426 CFS_INIT_LIST_HEAD(&lock->l_res_link);
427 CFS_INIT_LIST_HEAD(&lock->l_lru);
428 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
429 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
430 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
431 CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
432 cfs_waitq_init(&lock->l_waitq);
433 lock->l_blocking_lock = NULL;
434 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
435 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
436 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
438 lprocfs_counter_incr(ldlm_res_to_ns(resource)->ns_stats,
440 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
441 class_handle_hash(&lock->l_handle, lock_handle_addref);
443 lu_ref_init(&lock->l_reference);
444 lu_ref_add(&lock->l_reference, "hash", lock);
445 lock->l_callback_timeout = 0;
447 #if LUSTRE_TRACKS_LOCK_EXP_REFS
448 CFS_INIT_LIST_HEAD(&lock->l_exp_refs_link);
449 lock->l_exp_refs_nr = 0;
450 lock->l_exp_refs_target = NULL;
452 CFS_INIT_LIST_HEAD(&lock->l_exp_list);
457 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
458 const struct ldlm_res_id *new_resid)
460 struct ldlm_resource *oldres = lock->l_resource;
461 struct ldlm_resource *newres;
465 LASSERT(ns_is_client(ns));
467 lock_res_and_lock(lock);
468 if (memcmp(new_resid, &lock->l_resource->lr_name,
469 sizeof(lock->l_resource->lr_name)) == 0) {
471 unlock_res_and_lock(lock);
475 LASSERT(new_resid->name[0] != 0);
477 /* This function assumes that the lock isn't on any lists */
478 LASSERT(cfs_list_empty(&lock->l_res_link));
480 type = oldres->lr_type;
481 unlock_res_and_lock(lock);
483 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
487 lu_ref_add(&newres->lr_reference, "lock", lock);
489 * To flip the lock from the old to the new resource, lock, oldres and
490 * newres have to be locked. Resource spin-locks are nested within
491 * lock->l_lock, and are taken in the memory address order to avoid
494 cfs_spin_lock(&lock->l_lock);
495 oldres = lock->l_resource;
496 if (oldres < newres) {
498 lock_res_nested(newres, LRT_NEW);
501 lock_res_nested(oldres, LRT_NEW);
503 LASSERT(memcmp(new_resid, &oldres->lr_name,
504 sizeof oldres->lr_name) != 0);
505 lock->l_resource = newres;
507 unlock_res_and_lock(lock);
509 /* ...and the flowers are still standing! */
510 lu_ref_del(&oldres->lr_reference, "lock", lock);
511 ldlm_resource_putref(oldres);
520 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
522 lockh->cookie = lock->l_handle.h_cookie;
525 /* if flags: atomically get the lock and set the flags.
526 * Return NULL if flag already set
529 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
532 struct ldlm_lock *lock;
537 lock = class_handle2object(handle->cookie);
541 /* It's unlikely but possible that someone marked the lock as
542 * destroyed after we did handle2object on it */
543 if (flags == 0 && !lock->l_destroyed) {
544 lu_ref_add(&lock->l_reference, "handle", cfs_current());
548 lock_res_and_lock(lock);
550 LASSERT(lock->l_resource != NULL);
552 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
553 if (unlikely(lock->l_destroyed)) {
554 unlock_res_and_lock(lock);
555 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
560 if (flags && (lock->l_flags & flags)) {
561 unlock_res_and_lock(lock);
567 lock->l_flags |= flags;
569 unlock_res_and_lock(lock);
573 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
575 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
576 /* INODEBITS_INTEROP: If the other side does not support
577 * inodebits, reply with a plain lock descriptor.
579 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
580 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
581 /* Make sure all the right bits are set in this lock we
582 are going to pass to client */
583 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
584 (MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
585 MDS_INODELOCK_LAYOUT),
586 "Inappropriate inode lock bits during "
587 "conversion " LPU64 "\n",
588 lock->l_policy_data.l_inodebits.bits);
590 ldlm_res2desc(lock->l_resource, &desc->l_resource);
591 desc->l_resource.lr_type = LDLM_PLAIN;
593 /* Convert "new" lock mode to something old client can
595 if ((lock->l_req_mode == LCK_CR) ||
596 (lock->l_req_mode == LCK_CW))
597 desc->l_req_mode = LCK_PR;
599 desc->l_req_mode = lock->l_req_mode;
600 if ((lock->l_granted_mode == LCK_CR) ||
601 (lock->l_granted_mode == LCK_CW)) {
602 desc->l_granted_mode = LCK_PR;
604 /* We never grant PW/EX locks to clients */
605 LASSERT((lock->l_granted_mode != LCK_PW) &&
606 (lock->l_granted_mode != LCK_EX));
607 desc->l_granted_mode = lock->l_granted_mode;
610 /* We do not copy policy here, because there is no
611 policy for plain locks */
613 ldlm_res2desc(lock->l_resource, &desc->l_resource);
614 desc->l_req_mode = lock->l_req_mode;
615 desc->l_granted_mode = lock->l_granted_mode;
616 ldlm_convert_policy_to_wire(lock->l_resource->lr_type,
617 &lock->l_policy_data,
618 &desc->l_policy_data);
622 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
623 cfs_list_t *work_list)
625 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
626 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
627 lock->l_flags |= LDLM_FL_AST_SENT;
628 /* If the enqueuing client said so, tell the AST recipient to
629 * discard dirty data, rather than writing back. */
630 if (new->l_flags & LDLM_AST_DISCARD_DATA)
631 lock->l_flags |= LDLM_FL_DISCARD_DATA;
632 LASSERT(cfs_list_empty(&lock->l_bl_ast));
633 cfs_list_add(&lock->l_bl_ast, work_list);
635 LASSERT(lock->l_blocking_lock == NULL);
636 lock->l_blocking_lock = LDLM_LOCK_GET(new);
640 void ldlm_add_cp_work_item(struct ldlm_lock *lock, cfs_list_t *work_list)
642 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
643 lock->l_flags |= LDLM_FL_CP_REQD;
644 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
645 LASSERT(cfs_list_empty(&lock->l_cp_ast));
646 cfs_list_add(&lock->l_cp_ast, work_list);
651 /* must be called with lr_lock held */
652 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
653 cfs_list_t *work_list)
656 check_res_locked(lock->l_resource);
658 ldlm_add_bl_work_item(lock, new, work_list);
660 ldlm_add_cp_work_item(lock, work_list);
664 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
666 struct ldlm_lock *lock;
668 lock = ldlm_handle2lock(lockh);
669 LASSERT(lock != NULL);
670 ldlm_lock_addref_internal(lock, mode);
674 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
676 ldlm_lock_remove_from_lru(lock);
677 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
679 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
681 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
683 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
686 lu_ref_add_atomic(&lock->l_reference, "user", lock);
687 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
691 * Attempts to addref a lock, and fails if lock is already LDLM_FL_CBPENDING
694 * \retval 0 success, lock was addref-ed
696 * \retval -EAGAIN lock is being canceled.
698 int ldlm_lock_addref_try(struct lustre_handle *lockh, __u32 mode)
700 struct ldlm_lock *lock;
704 lock = ldlm_handle2lock(lockh);
706 lock_res_and_lock(lock);
707 if (lock->l_readers != 0 || lock->l_writers != 0 ||
708 !(lock->l_flags & LDLM_FL_CBPENDING)) {
709 ldlm_lock_addref_internal_nolock(lock, mode);
712 unlock_res_and_lock(lock);
718 /* only called for local locks */
719 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
721 lock_res_and_lock(lock);
722 ldlm_lock_addref_internal_nolock(lock, mode);
723 unlock_res_and_lock(lock);
726 /* only called in ldlm_flock_destroy and for local locks.
727 * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
728 * * ldlm_lock_remove_from_lru() does nothing, it is safe
729 * * for ldlm_flock_destroy usage by dropping some code */
730 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
732 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
733 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
734 LASSERT(lock->l_readers > 0);
735 lu_ref_del(&lock->l_reference, "reader", lock);
738 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP | LCK_COS)) {
739 LASSERT(lock->l_writers > 0);
740 lu_ref_del(&lock->l_reference, "writer", lock);
744 lu_ref_del(&lock->l_reference, "user", lock);
745 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
748 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
750 struct ldlm_namespace *ns;
753 lock_res_and_lock(lock);
755 ns = ldlm_lock_to_ns(lock);
757 ldlm_lock_decref_internal_nolock(lock, mode);
759 if (lock->l_flags & LDLM_FL_LOCAL &&
760 !lock->l_readers && !lock->l_writers) {
761 /* If this is a local lock on a server namespace and this was
762 * the last reference, cancel the lock. */
763 CDEBUG(D_INFO, "forcing cancel of local lock\n");
764 lock->l_flags |= LDLM_FL_CBPENDING;
767 if (!lock->l_readers && !lock->l_writers &&
768 (lock->l_flags & LDLM_FL_CBPENDING)) {
769 /* If we received a blocked AST and this was the last reference,
770 * run the callback. */
771 if (lock->l_ns_srv && lock->l_export)
772 CERROR("FL_CBPENDING set on non-local lock--just a "
775 LDLM_DEBUG(lock, "final decref done on cbpending lock");
777 LDLM_LOCK_GET(lock); /* dropped by bl thread */
778 ldlm_lock_remove_from_lru(lock);
779 unlock_res_and_lock(lock);
781 if (lock->l_flags & LDLM_FL_FAIL_LOC)
782 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
784 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
785 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
786 ldlm_handle_bl_callback(ns, NULL, lock);
787 } else if (ns_is_client(ns) &&
788 !lock->l_readers && !lock->l_writers &&
789 !(lock->l_flags & LDLM_FL_NO_LRU) &&
790 !(lock->l_flags & LDLM_FL_BL_AST)) {
792 LDLM_DEBUG(lock, "add lock into lru list");
794 /* If this is a client-side namespace and this was the last
795 * reference, put it on the LRU. */
796 ldlm_lock_add_to_lru(lock);
797 unlock_res_and_lock(lock);
799 if (lock->l_flags & LDLM_FL_FAIL_LOC)
800 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
802 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
803 * are not supported by the server, otherwise, it is done on
805 if (!exp_connect_cancelset(lock->l_conn_export) &&
806 !ns_connect_lru_resize(ns))
807 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
809 LDLM_DEBUG(lock, "do not add lock into lru list");
810 unlock_res_and_lock(lock);
816 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
818 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
819 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
820 ldlm_lock_decref_internal(lock, mode);
824 /* This will drop a lock reference and mark it for destruction, but will not
825 * necessarily cancel the lock before returning. */
826 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
828 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
831 LASSERT(lock != NULL);
833 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
834 lock_res_and_lock(lock);
835 lock->l_flags |= LDLM_FL_CBPENDING;
836 unlock_res_and_lock(lock);
837 ldlm_lock_decref_internal(lock, mode);
841 struct sl_insert_point {
842 cfs_list_t *res_link;
843 cfs_list_t *mode_link;
844 cfs_list_t *policy_link;
848 * search_granted_lock
851 * Finds a position to insert the new lock.
853 * queue [input]: the granted list where search acts on;
854 * req [input]: the lock whose position to be located;
855 * prev [output]: positions within 3 lists to insert @req to
859 * - ldlm_grant_lock_with_skiplist
861 static void search_granted_lock(cfs_list_t *queue,
862 struct ldlm_lock *req,
863 struct sl_insert_point *prev)
866 struct ldlm_lock *lock, *mode_end, *policy_end;
869 cfs_list_for_each(tmp, queue) {
870 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
872 mode_end = cfs_list_entry(lock->l_sl_mode.prev,
873 struct ldlm_lock, l_sl_mode);
875 if (lock->l_req_mode != req->l_req_mode) {
876 /* jump to last lock of mode group */
877 tmp = &mode_end->l_res_link;
881 /* suitable mode group is found */
882 if (lock->l_resource->lr_type == LDLM_PLAIN) {
883 /* insert point is last lock of the mode group */
884 prev->res_link = &mode_end->l_res_link;
885 prev->mode_link = &mode_end->l_sl_mode;
886 prev->policy_link = &req->l_sl_policy;
889 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
892 cfs_list_entry(lock->l_sl_policy.prev,
896 if (lock->l_policy_data.l_inodebits.bits ==
897 req->l_policy_data.l_inodebits.bits) {
898 /* insert point is last lock of
899 * the policy group */
901 &policy_end->l_res_link;
903 &policy_end->l_sl_mode;
905 &policy_end->l_sl_policy;
910 if (policy_end == mode_end)
911 /* done with mode group */
914 /* go to next policy group within mode group */
915 tmp = policy_end->l_res_link.next;
916 lock = cfs_list_entry(tmp, struct ldlm_lock,
918 } /* loop over policy groups within the mode group */
920 /* insert point is last lock of the mode group,
921 * new policy group is started */
922 prev->res_link = &mode_end->l_res_link;
923 prev->mode_link = &mode_end->l_sl_mode;
924 prev->policy_link = &req->l_sl_policy;
928 LDLM_ERROR(lock,"is not LDLM_PLAIN or LDLM_IBITS lock");
933 /* insert point is last lock on the queue,
934 * new mode group and new policy group are started */
935 prev->res_link = queue->prev;
936 prev->mode_link = &req->l_sl_mode;
937 prev->policy_link = &req->l_sl_policy;
942 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
943 struct sl_insert_point *prev)
945 struct ldlm_resource *res = lock->l_resource;
948 check_res_locked(res);
950 ldlm_resource_dump(D_INFO, res);
951 CDEBUG(D_OTHER, "About to add this lock:\n");
952 ldlm_lock_dump(D_OTHER, lock, 0);
954 if (lock->l_destroyed) {
955 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
959 LASSERT(cfs_list_empty(&lock->l_res_link));
960 LASSERT(cfs_list_empty(&lock->l_sl_mode));
961 LASSERT(cfs_list_empty(&lock->l_sl_policy));
963 cfs_list_add(&lock->l_res_link, prev->res_link);
964 cfs_list_add(&lock->l_sl_mode, prev->mode_link);
965 cfs_list_add(&lock->l_sl_policy, prev->policy_link);
970 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
972 struct sl_insert_point prev;
975 LASSERT(lock->l_req_mode == lock->l_granted_mode);
977 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
978 ldlm_granted_list_add_lock(lock, &prev);
983 * - ldlm_lock_enqueue
984 * - ldlm_reprocess_queue
985 * - ldlm_lock_convert
987 * must be called with lr_lock held
989 void ldlm_grant_lock(struct ldlm_lock *lock, cfs_list_t *work_list)
991 struct ldlm_resource *res = lock->l_resource;
994 check_res_locked(res);
996 lock->l_granted_mode = lock->l_req_mode;
997 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
998 ldlm_grant_lock_with_skiplist(lock);
999 else if (res->lr_type == LDLM_EXTENT)
1000 ldlm_extent_add_lock(res, lock);
1002 ldlm_resource_add_lock(res, &res->lr_granted, lock);
1004 if (lock->l_granted_mode < res->lr_most_restr)
1005 res->lr_most_restr = lock->l_granted_mode;
1007 if (work_list && lock->l_completion_ast != NULL)
1008 ldlm_add_ast_work_item(lock, NULL, work_list);
1010 ldlm_pool_add(&ldlm_res_to_ns(res)->ns_pool, lock);
1014 /* returns a referenced lock or NULL. See the flag descriptions below, in the
1015 * comment above ldlm_lock_match */
1016 static struct ldlm_lock *search_queue(cfs_list_t *queue,
1018 ldlm_policy_data_t *policy,
1019 struct ldlm_lock *old_lock,
1020 int flags, int unref)
1022 struct ldlm_lock *lock;
1025 cfs_list_for_each(tmp, queue) {
1028 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1030 if (lock == old_lock)
1033 /* llite sometimes wants to match locks that will be
1034 * canceled when their users drop, but we allow it to match
1035 * if it passes in CBPENDING and the lock still has users.
1036 * this is generally only going to be used by children
1037 * whose parents already hold a lock so forward progress
1038 * can still happen. */
1039 if (lock->l_flags & LDLM_FL_CBPENDING &&
1040 !(flags & LDLM_FL_CBPENDING))
1042 if (!unref && lock->l_flags & LDLM_FL_CBPENDING &&
1043 lock->l_readers == 0 && lock->l_writers == 0)
1046 if (!(lock->l_req_mode & *mode))
1048 match = lock->l_req_mode;
1050 if (lock->l_resource->lr_type == LDLM_EXTENT &&
1051 (lock->l_policy_data.l_extent.start >
1052 policy->l_extent.start ||
1053 lock->l_policy_data.l_extent.end < policy->l_extent.end))
1056 if (unlikely(match == LCK_GROUP) &&
1057 lock->l_resource->lr_type == LDLM_EXTENT &&
1058 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
1061 /* We match if we have existing lock with same or wider set
1063 if (lock->l_resource->lr_type == LDLM_IBITS &&
1064 ((lock->l_policy_data.l_inodebits.bits &
1065 policy->l_inodebits.bits) !=
1066 policy->l_inodebits.bits))
1070 (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
1074 if ((flags & LDLM_FL_LOCAL_ONLY) &&
1075 !(lock->l_flags & LDLM_FL_LOCAL))
1078 if (flags & LDLM_FL_TEST_LOCK) {
1079 LDLM_LOCK_GET(lock);
1080 ldlm_lock_touch_in_lru(lock);
1082 ldlm_lock_addref_internal_nolock(lock, match);
1091 void ldlm_lock_fail_match_locked(struct ldlm_lock *lock)
1093 if (!lock->l_failed) {
1095 cfs_waitq_broadcast(&lock->l_waitq);
1098 EXPORT_SYMBOL(ldlm_lock_fail_match_locked);
1100 void ldlm_lock_fail_match(struct ldlm_lock *lock)
1102 lock_res_and_lock(lock);
1103 ldlm_lock_fail_match_locked(lock);
1104 unlock_res_and_lock(lock);
1106 EXPORT_SYMBOL(ldlm_lock_fail_match);
1108 void ldlm_lock_allow_match_locked(struct ldlm_lock *lock)
1110 lock->l_flags |= LDLM_FL_LVB_READY;
1111 cfs_waitq_broadcast(&lock->l_waitq);
1114 void ldlm_lock_allow_match(struct ldlm_lock *lock)
1116 lock_res_and_lock(lock);
1117 ldlm_lock_allow_match_locked(lock);
1118 unlock_res_and_lock(lock);
1121 /* Can be called in two ways:
1123 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1124 * for a duplicate of.
1126 * Otherwise, all of the fields must be filled in, to match against.
1128 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1129 * server (ie, connh is NULL)
1130 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1131 * list will be considered
1132 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1133 * to be canceled can still be matched as long as they still have reader
1134 * or writer refernces
1135 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1136 * just tell us if we would have matched.
1138 * Returns 1 if it finds an already-existing lock that is compatible; in this
1139 * case, lockh is filled in with a addref()ed lock
1141 * we also check security context, if that failed we simply return 0 (to keep
1142 * caller code unchanged), the context failure will be discovered by caller
1145 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1146 const struct ldlm_res_id *res_id, ldlm_type_t type,
1147 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1148 struct lustre_handle *lockh, int unref)
1150 struct ldlm_resource *res;
1151 struct ldlm_lock *lock, *old_lock = NULL;
1156 old_lock = ldlm_handle2lock(lockh);
1159 ns = ldlm_lock_to_ns(old_lock);
1160 res_id = &old_lock->l_resource->lr_name;
1161 type = old_lock->l_resource->lr_type;
1162 mode = old_lock->l_req_mode;
1165 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1167 LASSERT(old_lock == NULL);
1171 LDLM_RESOURCE_ADDREF(res);
1174 lock = search_queue(&res->lr_granted, &mode, policy, old_lock,
1178 if (flags & LDLM_FL_BLOCK_GRANTED)
1180 lock = search_queue(&res->lr_converting, &mode, policy, old_lock,
1184 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock,
1192 LDLM_RESOURCE_DELREF(res);
1193 ldlm_resource_putref(res);
1196 ldlm_lock2handle(lock, lockh);
1197 if ((flags & LDLM_FL_LVB_READY) &&
1198 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1199 struct l_wait_info lwi;
1200 if (lock->l_completion_ast) {
1201 int err = lock->l_completion_ast(lock,
1202 LDLM_FL_WAIT_NOREPROC,
1205 if (flags & LDLM_FL_TEST_LOCK)
1206 LDLM_LOCK_RELEASE(lock);
1208 ldlm_lock_decref_internal(lock,
1215 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
1216 NULL, LWI_ON_SIGNAL_NOOP, NULL);
1218 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1219 l_wait_event(lock->l_waitq,
1220 lock->l_flags & LDLM_FL_LVB_READY ||
1223 if (!(lock->l_flags & LDLM_FL_LVB_READY)) {
1224 if (flags & LDLM_FL_TEST_LOCK)
1225 LDLM_LOCK_RELEASE(lock);
1227 ldlm_lock_decref_internal(lock, mode);
1234 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1235 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1236 res_id->name[2] : policy->l_extent.start,
1237 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1238 res_id->name[3] : policy->l_extent.end);
1240 /* check user's security context */
1241 if (lock->l_conn_export &&
1242 sptlrpc_import_check_ctx(
1243 class_exp2cliimp(lock->l_conn_export))) {
1244 if (!(flags & LDLM_FL_TEST_LOCK))
1245 ldlm_lock_decref_internal(lock, mode);
1249 if (flags & LDLM_FL_TEST_LOCK)
1250 LDLM_LOCK_RELEASE(lock);
1252 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1253 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1254 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1255 type, mode, res_id->name[0], res_id->name[1],
1256 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1257 res_id->name[2] :policy->l_extent.start,
1258 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1259 res_id->name[3] : policy->l_extent.end);
1262 LDLM_LOCK_PUT(old_lock);
1264 return rc ? mode : 0;
1267 ldlm_mode_t ldlm_revalidate_lock_handle(struct lustre_handle *lockh,
1270 struct ldlm_lock *lock;
1271 ldlm_mode_t mode = 0;
1274 lock = ldlm_handle2lock(lockh);
1276 lock_res_and_lock(lock);
1277 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED ||
1281 if (lock->l_flags & LDLM_FL_CBPENDING &&
1282 lock->l_readers == 0 && lock->l_writers == 0)
1286 *bits = lock->l_policy_data.l_inodebits.bits;
1287 mode = lock->l_granted_mode;
1288 ldlm_lock_addref_internal_nolock(lock, mode);
1295 unlock_res_and_lock(lock);
1296 LDLM_LOCK_PUT(lock);
1300 EXPORT_SYMBOL(ldlm_revalidate_lock_handle);
1302 /* Returns a referenced lock */
1303 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1304 const struct ldlm_res_id *res_id,
1307 const struct ldlm_callback_suite *cbs,
1308 void *data, __u32 lvb_len)
1310 struct ldlm_lock *lock;
1311 struct ldlm_resource *res;
1314 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1318 lock = ldlm_lock_new(res);
1323 lock->l_req_mode = mode;
1324 lock->l_ast_data = data;
1325 lock->l_pid = cfs_curproc_pid();
1326 lock->l_ns_srv = !!ns_is_server(ns);
1328 lock->l_blocking_ast = cbs->lcs_blocking;
1329 lock->l_completion_ast = cbs->lcs_completion;
1330 lock->l_glimpse_ast = cbs->lcs_glimpse;
1331 lock->l_weigh_ast = cbs->lcs_weigh;
1334 lock->l_tree_node = NULL;
1335 /* if this is the extent lock, allocate the interval tree node */
1336 if (type == LDLM_EXTENT) {
1337 if (ldlm_interval_alloc(lock) == NULL)
1342 lock->l_lvb_len = lvb_len;
1343 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1344 if (lock->l_lvb_data == NULL)
1348 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_NEW_LOCK))
1354 ldlm_lock_destroy(lock);
1355 LDLM_LOCK_RELEASE(lock);
1359 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1360 struct ldlm_lock **lockp,
1361 void *cookie, int *flags)
1363 struct ldlm_lock *lock = *lockp;
1364 struct ldlm_resource *res = lock->l_resource;
1365 int local = ns_is_client(ldlm_res_to_ns(res));
1366 ldlm_processing_policy policy;
1367 ldlm_error_t rc = ELDLM_OK;
1368 struct ldlm_interval *node = NULL;
1371 lock->l_last_activity = cfs_time_current_sec();
1372 /* policies are not executed on the client or during replay */
1373 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1374 && !local && ns->ns_policy) {
1375 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1377 if (rc == ELDLM_LOCK_REPLACED) {
1378 /* The lock that was returned has already been granted,
1379 * and placed into lockp. If it's not the same as the
1380 * one we passed in, then destroy the old one and our
1381 * work here is done. */
1382 if (lock != *lockp) {
1383 ldlm_lock_destroy(lock);
1384 LDLM_LOCK_RELEASE(lock);
1386 *flags |= LDLM_FL_LOCK_CHANGED;
1388 } else if (rc != ELDLM_OK ||
1389 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1390 ldlm_lock_destroy(lock);
1395 /* For a replaying lock, it might be already in granted list. So
1396 * unlinking the lock will cause the interval node to be freed, we
1397 * have to allocate the interval node early otherwise we can't regrant
1398 * this lock in the future. - jay */
1399 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1400 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1402 lock_res_and_lock(lock);
1403 if (local && lock->l_req_mode == lock->l_granted_mode) {
1404 /* The server returned a blocked lock, but it was granted
1405 * before we got a chance to actually enqueue it. We don't
1406 * need to do anything else. */
1407 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1408 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1409 GOTO(out, ELDLM_OK);
1412 ldlm_resource_unlink_lock(lock);
1413 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1415 ldlm_lock_destroy_nolock(lock);
1416 GOTO(out, rc = -ENOMEM);
1419 CFS_INIT_LIST_HEAD(&node->li_group);
1420 ldlm_interval_attach(node, lock);
1424 /* Some flags from the enqueue want to make it into the AST, via the
1425 * lock's l_flags. */
1426 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1428 /* This distinction between local lock trees is very important; a client
1429 * namespace only has information about locks taken by that client, and
1430 * thus doesn't have enough information to decide for itself if it can
1431 * be granted (below). In this case, we do exactly what the server
1432 * tells us to do, as dictated by the 'flags'.
1434 * We do exactly the same thing during recovery, when the server is
1435 * more or less trusting the clients not to lie.
1437 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1438 * granted/converting queues. */
1440 if (*flags & LDLM_FL_BLOCK_CONV)
1441 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1442 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1443 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1445 ldlm_grant_lock(lock, NULL);
1446 GOTO(out, ELDLM_OK);
1447 } else if (*flags & LDLM_FL_REPLAY) {
1448 if (*flags & LDLM_FL_BLOCK_CONV) {
1449 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1450 GOTO(out, ELDLM_OK);
1451 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1452 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1453 GOTO(out, ELDLM_OK);
1454 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1455 ldlm_grant_lock(lock, NULL);
1456 GOTO(out, ELDLM_OK);
1458 /* If no flags, fall through to normal enqueue path. */
1461 policy = ldlm_processing_policy_table[res->lr_type];
1462 policy(lock, flags, 1, &rc, NULL);
1465 unlock_res_and_lock(lock);
1467 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1471 /* Must be called with namespace taken: queue is waiting or converting. */
1472 int ldlm_reprocess_queue(struct ldlm_resource *res, cfs_list_t *queue,
1473 cfs_list_t *work_list)
1475 cfs_list_t *tmp, *pos;
1476 ldlm_processing_policy policy;
1478 int rc = LDLM_ITER_CONTINUE;
1482 check_res_locked(res);
1484 policy = ldlm_processing_policy_table[res->lr_type];
1487 cfs_list_for_each_safe(tmp, pos, queue) {
1488 struct ldlm_lock *pending;
1489 pending = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1491 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1494 rc = policy(pending, &flags, 0, &err, work_list);
1495 if (rc != LDLM_ITER_CONTINUE)
1503 ldlm_work_bl_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1505 struct ldlm_lock_desc d;
1506 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1511 /* nobody should touch l_bl_ast */
1512 lock_res_and_lock(lock);
1513 cfs_list_del_init(&lock->l_bl_ast);
1515 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1516 LASSERT(lock->l_bl_ast_run == 0);
1517 LASSERT(lock->l_blocking_lock);
1518 lock->l_bl_ast_run++;
1519 unlock_res_and_lock(lock);
1521 ldlm_lock2desc(lock->l_blocking_lock, &d);
1523 rc = lock->l_blocking_ast(lock, &d, (void *)arg,
1525 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1526 lock->l_blocking_lock = NULL;
1527 LDLM_LOCK_RELEASE(lock);
1533 ldlm_work_cp_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1535 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock, l_cp_ast);
1536 ldlm_completion_callback completion_callback;
1540 /* It's possible to receive a completion AST before we've set
1541 * the l_completion_ast pointer: either because the AST arrived
1542 * before the reply, or simply because there's a small race
1543 * window between receiving the reply and finishing the local
1544 * enqueue. (bug 842)
1546 * This can't happen with the blocking_ast, however, because we
1547 * will never call the local blocking_ast until we drop our
1548 * reader/writer reference, which we won't do until we get the
1549 * reply and finish enqueueing. */
1551 /* nobody should touch l_cp_ast */
1552 lock_res_and_lock(lock);
1553 cfs_list_del_init(&lock->l_cp_ast);
1554 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1555 /* save l_completion_ast since it can be changed by
1556 * mds_intent_policy(), see bug 14225 */
1557 completion_callback = lock->l_completion_ast;
1558 lock->l_flags &= ~LDLM_FL_CP_REQD;
1559 unlock_res_and_lock(lock);
1561 if (completion_callback != NULL)
1562 rc = completion_callback(lock, 0, (void *)arg);
1563 LDLM_LOCK_RELEASE(lock);
1569 ldlm_work_revoke_ast_lock(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg)
1571 struct ldlm_lock_desc desc;
1572 struct ldlm_lock *lock = cfs_list_entry(tmp, struct ldlm_lock,
1577 cfs_list_del_init(&lock->l_rk_ast);
1579 /* the desc just pretend to exclusive */
1580 ldlm_lock2desc(lock, &desc);
1581 desc.l_req_mode = LCK_EX;
1582 desc.l_granted_mode = 0;
1584 rc = lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1585 LDLM_LOCK_RELEASE(lock);
1590 int ldlm_run_ast_work(struct ldlm_namespace *ns, cfs_list_t *rpc_list,
1591 ldlm_desc_ast_t ast_type)
1593 struct l_wait_info lwi = { 0 };
1594 struct ldlm_cb_set_arg *arg;
1595 cfs_list_t *tmp, *pos;
1596 int (*work_ast_lock)(cfs_list_t *tmp, struct ldlm_cb_set_arg *arg);
1597 unsigned int max_ast_count;
1601 if (cfs_list_empty(rpc_list))
1608 cfs_atomic_set(&arg->restart, 0);
1609 cfs_atomic_set(&arg->rpcs, 0);
1610 cfs_atomic_set(&arg->refcount, 1);
1611 cfs_waitq_init(&arg->waitq);
1614 case LDLM_WORK_BL_AST:
1615 arg->type = LDLM_BL_CALLBACK;
1616 work_ast_lock = ldlm_work_bl_ast_lock;
1618 case LDLM_WORK_CP_AST:
1619 arg->type = LDLM_CP_CALLBACK;
1620 work_ast_lock = ldlm_work_cp_ast_lock;
1622 case LDLM_WORK_REVOKE_AST:
1623 arg->type = LDLM_BL_CALLBACK;
1624 work_ast_lock = ldlm_work_revoke_ast_lock;
1630 max_ast_count = ns->ns_max_parallel_ast ? : UINT_MAX;
1631 arg->threshold = max_ast_count;
1633 cfs_list_for_each_safe(tmp, pos, rpc_list) {
1634 (void)work_ast_lock(tmp, arg);
1635 if (cfs_atomic_read(&arg->rpcs) < max_ast_count)
1638 l_wait_event(arg->waitq,
1639 cfs_atomic_read(&arg->rpcs) < arg->threshold,
1644 l_wait_event(arg->waitq, cfs_atomic_read(&arg->rpcs) == 0, &lwi);
1646 rc = cfs_atomic_read(&arg->restart) ? -ERESTART : 0;
1651 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1653 ldlm_reprocess_all(res);
1654 return LDLM_ITER_CONTINUE;
1657 static int ldlm_reprocess_res(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1658 cfs_hlist_node_t *hnode, void *arg)
1660 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1663 rc = reprocess_one_queue(res, arg);
1665 return rc == LDLM_ITER_STOP;
1668 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1673 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1674 ldlm_reprocess_res, NULL);
1679 void ldlm_reprocess_all(struct ldlm_resource *res)
1681 CFS_LIST_HEAD(rpc_list);
1685 /* Local lock trees don't get reprocessed. */
1686 if (ns_is_client(ldlm_res_to_ns(res))) {
1693 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1694 if (rc == LDLM_ITER_CONTINUE)
1695 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1698 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
1700 if (rc == -ERESTART) {
1701 LASSERT(cfs_list_empty(&rpc_list));
1707 void ldlm_cancel_callback(struct ldlm_lock *lock)
1709 check_res_locked(lock->l_resource);
1710 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1711 lock->l_flags |= LDLM_FL_CANCEL;
1712 if (lock->l_blocking_ast) {
1713 // l_check_no_ns_lock(ns);
1714 unlock_res_and_lock(lock);
1715 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1717 lock_res_and_lock(lock);
1719 LDLM_DEBUG(lock, "no blocking ast");
1722 lock->l_flags |= LDLM_FL_BL_DONE;
1725 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1727 if (req->l_resource->lr_type != LDLM_PLAIN &&
1728 req->l_resource->lr_type != LDLM_IBITS)
1731 cfs_list_del_init(&req->l_sl_policy);
1732 cfs_list_del_init(&req->l_sl_mode);
1735 void ldlm_lock_cancel(struct ldlm_lock *lock)
1737 struct ldlm_resource *res;
1738 struct ldlm_namespace *ns;
1741 lock_res_and_lock(lock);
1743 res = lock->l_resource;
1744 ns = ldlm_res_to_ns(res);
1746 /* Please do not, no matter how tempting, remove this LBUG without
1747 * talking to me first. -phik */
1748 if (lock->l_readers || lock->l_writers) {
1749 LDLM_ERROR(lock, "lock still has references");
1753 ldlm_del_waiting_lock(lock);
1755 /* Releases cancel callback. */
1756 ldlm_cancel_callback(lock);
1758 /* Yes, second time, just in case it was added again while we were
1759 running with no res lock in ldlm_cancel_callback */
1760 ldlm_del_waiting_lock(lock);
1761 ldlm_resource_unlink_lock(lock);
1762 ldlm_lock_destroy_nolock(lock);
1764 if (lock->l_granted_mode == lock->l_req_mode)
1765 ldlm_pool_del(&ns->ns_pool, lock);
1767 /* Make sure we will not be called again for same lock what is possible
1768 * if not to zero out lock->l_granted_mode */
1769 lock->l_granted_mode = LCK_MINMODE;
1770 unlock_res_and_lock(lock);
1775 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1777 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1782 if (lock->l_ast_data == NULL)
1783 lock->l_ast_data = data;
1784 if (lock->l_ast_data == data)
1786 LDLM_LOCK_PUT(lock);
1790 EXPORT_SYMBOL(ldlm_lock_set_data);
1792 int ldlm_cancel_locks_for_export_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1793 cfs_hlist_node_t *hnode, void *data)
1796 struct obd_export *exp = data;
1797 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
1798 struct ldlm_resource *res;
1800 res = ldlm_resource_getref(lock->l_resource);
1801 LDLM_LOCK_GET(lock);
1803 LDLM_DEBUG(lock, "export %p", exp);
1804 ldlm_res_lvbo_update(res, NULL, 1);
1805 ldlm_lock_cancel(lock);
1806 ldlm_reprocess_all(res);
1807 ldlm_resource_putref(res);
1808 LDLM_LOCK_RELEASE(lock);
1812 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1814 cfs_hash_for_each_empty(exp->exp_lock_hash,
1815 ldlm_cancel_locks_for_export_cb, exp);
1819 * Downgrade an exclusive lock.
1821 * A fast variant of ldlm_lock_convert for convertion of exclusive
1822 * locks. The convertion is always successful.
1824 * \param lock A lock to convert
1825 * \param new_mode new lock mode
1827 void ldlm_lock_downgrade(struct ldlm_lock *lock, int new_mode)
1831 LASSERT(lock->l_granted_mode & (LCK_PW | LCK_EX));
1832 LASSERT(new_mode == LCK_COS);
1834 lock_res_and_lock(lock);
1835 ldlm_resource_unlink_lock(lock);
1837 * Remove the lock from pool as it will be added again in
1838 * ldlm_grant_lock() called below.
1840 ldlm_pool_del(&ldlm_lock_to_ns(lock)->ns_pool, lock);
1842 lock->l_req_mode = new_mode;
1843 ldlm_grant_lock(lock, NULL);
1844 unlock_res_and_lock(lock);
1845 ldlm_reprocess_all(lock->l_resource);
1850 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1853 CFS_LIST_HEAD(rpc_list);
1854 struct ldlm_resource *res;
1855 struct ldlm_namespace *ns;
1858 struct sl_insert_point prev;
1860 struct ldlm_interval *node;
1863 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1864 *flags |= LDLM_FL_BLOCK_GRANTED;
1865 RETURN(lock->l_resource);
1868 /* I can't check the type of lock here because the bitlock of lock
1869 * is not held here, so do the allocation blindly. -jay */
1870 OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, CFS_ALLOC_IO);
1871 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1874 LASSERTF((new_mode == LCK_PW && lock->l_granted_mode == LCK_PR),
1875 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1877 lock_res_and_lock(lock);
1879 res = lock->l_resource;
1880 ns = ldlm_res_to_ns(res);
1882 old_mode = lock->l_req_mode;
1883 lock->l_req_mode = new_mode;
1884 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1885 /* remember the lock position where the lock might be
1886 * added back to the granted list later and also
1887 * remember the join mode for skiplist fixing. */
1888 prev.res_link = lock->l_res_link.prev;
1889 prev.mode_link = lock->l_sl_mode.prev;
1890 prev.policy_link = lock->l_sl_policy.prev;
1891 ldlm_resource_unlink_lock(lock);
1893 ldlm_resource_unlink_lock(lock);
1894 if (res->lr_type == LDLM_EXTENT) {
1895 /* FIXME: ugly code, I have to attach the lock to a
1896 * interval node again since perhaps it will be granted
1898 CFS_INIT_LIST_HEAD(&node->li_group);
1899 ldlm_interval_attach(node, lock);
1905 * Remove old lock from the pool before adding the lock with new
1906 * mode below in ->policy()
1908 ldlm_pool_del(&ns->ns_pool, lock);
1910 /* If this is a local resource, put it on the appropriate list. */
1911 if (ns_is_client(ldlm_res_to_ns(res))) {
1912 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1913 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1915 /* This should never happen, because of the way the
1916 * server handles conversions. */
1917 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1921 ldlm_grant_lock(lock, &rpc_list);
1923 /* FIXME: completion handling not with lr_lock held ! */
1924 if (lock->l_completion_ast)
1925 lock->l_completion_ast(lock, 0, NULL);
1929 ldlm_processing_policy policy;
1930 policy = ldlm_processing_policy_table[res->lr_type];
1931 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1932 if (rc == LDLM_ITER_STOP) {
1933 lock->l_req_mode = old_mode;
1934 if (res->lr_type == LDLM_EXTENT)
1935 ldlm_extent_add_lock(res, lock);
1937 ldlm_granted_list_add_lock(lock, &prev);
1941 *flags |= LDLM_FL_BLOCK_GRANTED;
1945 unlock_res_and_lock(lock);
1948 ldlm_run_ast_work(ns, &rpc_list, LDLM_WORK_CP_AST);
1950 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1954 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1956 struct obd_device *obd = NULL;
1958 if (!((libcfs_debug | D_ERROR) & level))
1962 CDEBUG(level, " NULL LDLM lock\n");
1966 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1967 lock, lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
1969 if (lock->l_conn_export != NULL)
1970 obd = lock->l_conn_export->exp_obd;
1971 if (lock->l_export && lock->l_export->exp_connection) {
1972 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1973 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1974 lock->l_remote_handle.cookie);
1975 } else if (obd == NULL) {
1976 CDEBUG(level, " Node: local\n");
1978 struct obd_import *imp = obd->u.cli.cl_import;
1979 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1980 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1981 lock->l_remote_handle.cookie);
1983 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1985 lock->l_resource->lr_name.name[0],
1986 lock->l_resource->lr_name.name[1],
1987 lock->l_resource->lr_name.name[2]);
1988 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1989 "write: %d flags: "LPX64"\n", ldlm_lockname[lock->l_req_mode],
1990 ldlm_lockname[lock->l_granted_mode],
1991 cfs_atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1993 if (lock->l_resource->lr_type == LDLM_EXTENT)
1994 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1995 " (req "LPU64"-"LPU64")\n",
1996 lock->l_policy_data.l_extent.start,
1997 lock->l_policy_data.l_extent.end,
1998 lock->l_req_extent.start, lock->l_req_extent.end);
1999 else if (lock->l_resource->lr_type == LDLM_FLOCK)
2000 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
2001 lock->l_policy_data.l_flock.pid,
2002 lock->l_policy_data.l_flock.start,
2003 lock->l_policy_data.l_flock.end);
2004 else if (lock->l_resource->lr_type == LDLM_IBITS)
2005 CDEBUG(level, " Bits: "LPX64"\n",
2006 lock->l_policy_data.l_inodebits.bits);
2009 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
2011 struct ldlm_lock *lock;
2013 if (!((libcfs_debug | D_ERROR) & level))
2016 lock = ldlm_handle2lock(lockh);
2020 ldlm_lock_dump(D_OTHER, lock, 0);
2022 LDLM_LOCK_PUT(lock);
2025 void _ldlm_lock_debug(struct ldlm_lock *lock,
2026 struct libcfs_debug_msg_data *msgdata,
2027 const char *fmt, ...)
2031 va_start(args, fmt);
2033 if (lock->l_resource == NULL) {
2034 libcfs_debug_vmsg2(msgdata, fmt, args,
2035 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2036 "res: \?\? rrc=\?\? type: \?\?\? flags: "LPX64" remote: "
2037 LPX64" expref: %d pid: %u timeout: %lu\n", lock,
2038 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2039 lock->l_readers, lock->l_writers,
2040 ldlm_lockname[lock->l_granted_mode],
2041 ldlm_lockname[lock->l_req_mode],
2042 lock->l_flags, lock->l_remote_handle.cookie,
2044 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2045 lock->l_pid, lock->l_callback_timeout);
2050 switch (lock->l_resource->lr_type) {
2052 libcfs_debug_vmsg2(msgdata, fmt, args,
2053 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2054 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
2055 "] (req "LPU64"->"LPU64") flags: "LPX64" remote: "LPX64
2056 " expref: %d pid: %u timeout %lu\n",
2057 ldlm_lock_to_ns_name(lock), lock,
2058 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2059 lock->l_readers, lock->l_writers,
2060 ldlm_lockname[lock->l_granted_mode],
2061 ldlm_lockname[lock->l_req_mode],
2062 lock->l_resource->lr_name.name[0],
2063 lock->l_resource->lr_name.name[1],
2064 cfs_atomic_read(&lock->l_resource->lr_refcount),
2065 ldlm_typename[lock->l_resource->lr_type],
2066 lock->l_policy_data.l_extent.start,
2067 lock->l_policy_data.l_extent.end,
2068 lock->l_req_extent.start, lock->l_req_extent.end,
2069 lock->l_flags, lock->l_remote_handle.cookie,
2071 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2072 lock->l_pid, lock->l_callback_timeout);
2076 libcfs_debug_vmsg2(msgdata, fmt, args,
2077 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2078 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
2079 "["LPU64"->"LPU64"] flags: "LPX64" remote: "LPX64
2080 " expref: %d pid: %u timeout: %lu\n",
2081 ldlm_lock_to_ns_name(lock), lock,
2082 lock->l_handle.h_cookie, cfs_atomic_read(&lock->l_refc),
2083 lock->l_readers, lock->l_writers,
2084 ldlm_lockname[lock->l_granted_mode],
2085 ldlm_lockname[lock->l_req_mode],
2086 lock->l_resource->lr_name.name[0],
2087 lock->l_resource->lr_name.name[1],
2088 cfs_atomic_read(&lock->l_resource->lr_refcount),
2089 ldlm_typename[lock->l_resource->lr_type],
2090 lock->l_policy_data.l_flock.pid,
2091 lock->l_policy_data.l_flock.start,
2092 lock->l_policy_data.l_flock.end,
2093 lock->l_flags, lock->l_remote_handle.cookie,
2095 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2096 lock->l_pid, lock->l_callback_timeout);
2100 libcfs_debug_vmsg2(msgdata, fmt, args,
2101 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2102 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
2103 "flags: "LPX64" remote: "LPX64" expref: %d "
2104 "pid: %u timeout: %lu\n",
2105 ldlm_lock_to_ns_name(lock),
2106 lock, lock->l_handle.h_cookie,
2107 cfs_atomic_read (&lock->l_refc),
2108 lock->l_readers, lock->l_writers,
2109 ldlm_lockname[lock->l_granted_mode],
2110 ldlm_lockname[lock->l_req_mode],
2111 lock->l_resource->lr_name.name[0],
2112 lock->l_resource->lr_name.name[1],
2113 lock->l_policy_data.l_inodebits.bits,
2114 cfs_atomic_read(&lock->l_resource->lr_refcount),
2115 ldlm_typename[lock->l_resource->lr_type],
2116 lock->l_flags, lock->l_remote_handle.cookie,
2118 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2119 lock->l_pid, lock->l_callback_timeout);
2123 libcfs_debug_vmsg2(msgdata, fmt, args,
2124 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
2125 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: "LPX64" "
2126 "remote: "LPX64" expref: %d pid: %u timeout %lu\n",
2127 ldlm_lock_to_ns_name(lock),
2128 lock, lock->l_handle.h_cookie,
2129 cfs_atomic_read (&lock->l_refc),
2130 lock->l_readers, lock->l_writers,
2131 ldlm_lockname[lock->l_granted_mode],
2132 ldlm_lockname[lock->l_req_mode],
2133 lock->l_resource->lr_name.name[0],
2134 lock->l_resource->lr_name.name[1],
2135 cfs_atomic_read(&lock->l_resource->lr_refcount),
2136 ldlm_typename[lock->l_resource->lr_type],
2137 lock->l_flags, lock->l_remote_handle.cookie,
2139 cfs_atomic_read(&lock->l_export->exp_refcount) : -99,
2140 lock->l_pid, lock->l_callback_timeout);
2145 EXPORT_SYMBOL(_ldlm_lock_debug);