1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * You may have signed or agreed to another license before downloading
12 * this software. If so, you are bound by the terms and conditions
13 * of that agreement, and the following does not apply to you. See the
14 * LICENSE file included with this distribution for more information.
16 * If you did not agree to a different license, then this copy of Lustre
17 * is open source software; you can redistribute it and/or modify it
18 * under the terms of version 2 of the GNU General Public License as
19 * published by the Free Software Foundation.
21 * In either case, Lustre is distributed in the hope that it will be
22 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * license text for more details.
27 #define DEBUG_SUBSYSTEM S_LDLM
30 # include <libcfs/libcfs.h>
31 # include <linux/lustre_intent.h>
33 # include <liblustre.h>
34 # include <libcfs/kp30.h>
37 #include <obd_class.h>
38 #include "ldlm_internal.h"
40 //struct lustre_lock ldlm_everything_lock;
42 /* lock's skip list pointers fix mode */
43 #define LDLM_JOIN_NONE 0
44 #define LDLM_MODE_JOIN_RIGHT 1
45 #define LDLM_MODE_JOIN_LEFT (1 << 1)
46 #define LDLM_POLICY_JOIN_RIGHT (1 << 2)
47 #define LDLM_POLICY_JOIN_LEFT (1 << 3)
50 char *ldlm_lockname[] = {
61 char *ldlm_typename[] = {
68 char *ldlm_it2str(int it)
75 case (IT_OPEN | IT_CREAT):
88 CERROR("Unknown intent %d\n", it);
93 extern cfs_mem_cache_t *ldlm_lock_slab;
95 static ldlm_processing_policy ldlm_processing_policy_table[] = {
96 [LDLM_PLAIN] ldlm_process_plain_lock,
97 [LDLM_EXTENT] ldlm_process_extent_lock,
99 [LDLM_FLOCK] ldlm_process_flock_lock,
101 [LDLM_IBITS] ldlm_process_inodebits_lock,
104 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
106 return ldlm_processing_policy_table[res->lr_type];
109 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
115 * REFCOUNTED LOCK OBJECTS
120 * Lock refcounts, during creation:
121 * - one special one for allocation, dec'd only once in destroy
122 * - one for being a lock that's in-use
123 * - one for the addref associated with a new lock
125 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
127 atomic_inc(&lock->l_refc);
131 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
133 LASSERT(size == sizeof(*lock));
134 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
137 void ldlm_lock_put(struct ldlm_lock *lock)
141 LASSERT(lock->l_resource != LP_POISON);
142 LASSERT(atomic_read(&lock->l_refc) > 0);
143 if (atomic_dec_and_test(&lock->l_refc)) {
144 struct ldlm_resource *res;
146 LDLM_DEBUG(lock, "final lock_put on destroyed lock, freeing it.");
148 res = lock->l_resource;
149 LASSERT(lock->l_destroyed);
150 LASSERT(list_empty(&lock->l_res_link));
151 LASSERT(list_empty(&lock->l_pending_chain));
153 atomic_dec(&res->lr_namespace->ns_locks);
154 ldlm_resource_putref(res);
155 lock->l_resource = NULL;
156 if (lock->l_export) {
157 class_export_put(lock->l_export);
158 lock->l_export = NULL;
161 if (lock->l_lvb_data != NULL)
162 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
164 ldlm_interval_free(ldlm_interval_detach(lock));
165 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
172 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
175 if (!list_empty(&lock->l_lru)) {
176 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
177 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
178 list_del_init(&lock->l_lru);
180 LASSERT(ns->ns_nr_unused >= 0);
186 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
188 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
191 spin_lock(&ns->ns_unused_lock);
192 rc = ldlm_lock_remove_from_lru_nolock(lock);
193 spin_unlock(&ns->ns_unused_lock);
198 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
200 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
201 lock->l_last_used = cfs_time_current();
202 LASSERT(list_empty(&lock->l_lru));
203 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
204 LASSERT(ns->ns_nr_unused >= 0);
208 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
210 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
212 spin_lock(&ns->ns_unused_lock);
213 ldlm_lock_add_to_lru_nolock(lock);
214 spin_unlock(&ns->ns_unused_lock);
218 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
220 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
222 spin_lock(&ns->ns_unused_lock);
223 if (!list_empty(&lock->l_lru)) {
224 ldlm_lock_remove_from_lru_nolock(lock);
225 ldlm_lock_add_to_lru_nolock(lock);
227 spin_unlock(&ns->ns_unused_lock);
231 /* This used to have a 'strict' flag, which recovery would use to mark an
232 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
233 * shall explain why it's gone: with the new hash table scheme, once you call
234 * ldlm_lock_destroy, you can never drop your final references on this lock.
235 * Because it's not in the hash table anymore. -phil */
236 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
240 if (lock->l_readers || lock->l_writers) {
241 LDLM_ERROR(lock, "lock still has references");
242 ldlm_lock_dump(D_ERROR, lock, 0);
246 if (!list_empty(&lock->l_res_link)) {
247 LDLM_ERROR(lock, "lock still on resource");
248 ldlm_lock_dump(D_ERROR, lock, 0);
252 if (lock->l_destroyed) {
253 LASSERT(list_empty(&lock->l_lru));
257 lock->l_destroyed = 1;
260 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
261 list_del_init(&lock->l_export_chain);
263 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
265 ldlm_lock_remove_from_lru(lock);
266 class_handle_unhash(&lock->l_handle);
269 /* Wake anyone waiting for this lock */
270 /* FIXME: I should probably add yet another flag, instead of using
271 * l_export to only call this on clients */
273 class_export_put(lock->l_export);
274 lock->l_export = NULL;
275 if (lock->l_export && lock->l_completion_ast)
276 lock->l_completion_ast(lock, 0);
282 void ldlm_lock_destroy(struct ldlm_lock *lock)
286 lock_res_and_lock(lock);
287 first = ldlm_lock_destroy_internal(lock);
288 unlock_res_and_lock(lock);
290 /* drop reference from hashtable only for first destroy */
296 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
300 first = ldlm_lock_destroy_internal(lock);
301 /* drop reference from hashtable only for first destroy */
307 /* this is called by portals_handle2object with the handle lock taken */
308 static void lock_handle_addref(void *lock)
310 LDLM_LOCK_GET((struct ldlm_lock *)lock);
314 * usage: pass in a resource on which you have done ldlm_resource_get
315 * pass in a parent lock on which you have done a ldlm_lock_get
316 * after return, ldlm_*_put the resource and parent
317 * returns: lock with refcount 2 - one for current caller and one for remote
319 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
321 struct ldlm_lock *lock;
324 if (resource == NULL)
327 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
331 spin_lock_init(&lock->l_lock);
332 lock->l_resource = ldlm_resource_getref(resource);
334 atomic_set(&lock->l_refc, 2);
335 CFS_INIT_LIST_HEAD(&lock->l_res_link);
336 CFS_INIT_LIST_HEAD(&lock->l_lru);
337 CFS_INIT_LIST_HEAD(&lock->l_export_chain);
338 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
339 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
340 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
341 cfs_waitq_init(&lock->l_waitq);
342 lock->l_blocking_lock = NULL;
343 lock->l_sl_mode.prev = NULL;
344 lock->l_sl_mode.next = NULL;
345 lock->l_sl_policy.prev = NULL;
346 lock->l_sl_policy.next = NULL;
348 atomic_inc(&resource->lr_namespace->ns_locks);
349 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
350 class_handle_hash(&lock->l_handle, lock_handle_addref);
352 CFS_INIT_LIST_HEAD(&lock->l_extents_list);
353 spin_lock_init(&lock->l_extents_list_lock);
354 CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
359 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
360 struct ldlm_res_id new_resid)
362 struct ldlm_resource *oldres = lock->l_resource;
363 struct ldlm_resource *newres;
367 LASSERT(ns_is_client(ns));
369 lock_res_and_lock(lock);
370 if (memcmp(&new_resid, &lock->l_resource->lr_name,
371 sizeof(lock->l_resource->lr_name)) == 0) {
373 unlock_res_and_lock(lock);
377 LASSERT(new_resid.name[0] != 0);
379 /* This function assumes that the lock isn't on any lists */
380 LASSERT(list_empty(&lock->l_res_link));
382 type = oldres->lr_type;
383 unlock_res_and_lock(lock);
385 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
386 if (newres == NULL) {
391 lock_res_and_lock(lock);
392 LASSERT(memcmp(&new_resid, &lock->l_resource->lr_name,
393 sizeof(lock->l_resource->lr_name)) != 0);
395 lock->l_resource = newres;
397 unlock_res_and_lock(lock);
399 /* ...and the flowers are still standing! */
400 ldlm_resource_putref(oldres);
409 void ldlm_lock2handle(struct ldlm_lock *lock, struct lustre_handle *lockh)
411 lockh->cookie = lock->l_handle.h_cookie;
414 /* if flags: atomically get the lock and set the flags.
415 * Return NULL if flag already set
418 struct ldlm_lock *__ldlm_handle2lock(struct lustre_handle *handle, int flags)
420 struct ldlm_namespace *ns;
421 struct ldlm_lock *lock = NULL, *retval = NULL;
426 lock = class_handle2object(handle->cookie);
430 LASSERT(lock->l_resource != NULL);
431 ns = lock->l_resource->lr_namespace;
434 lock_res_and_lock(lock);
436 /* It's unlikely but possible that someone marked the lock as
437 * destroyed after we did handle2object on it */
438 if (lock->l_destroyed) {
439 unlock_res_and_lock(lock);
440 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
445 if (flags && (lock->l_flags & flags)) {
446 unlock_res_and_lock(lock);
452 lock->l_flags |= flags;
454 unlock_res_and_lock(lock);
461 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
462 struct lustre_handle *handle)
464 struct ldlm_lock *retval = NULL;
465 retval = __ldlm_handle2lock(handle, 0);
469 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
471 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
472 /* INODEBITS_INTEROP: If the other side does not support
473 * inodebits, reply with a plain lock descriptor.
475 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
476 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
477 struct ldlm_resource res = *lock->l_resource;
479 /* Make sure all the right bits are set in this lock we
480 are going to pass to client */
481 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
482 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
483 "Inappropriate inode lock bits during "
484 "conversion " LPU64 "\n",
485 lock->l_policy_data.l_inodebits.bits);
486 res.lr_type = LDLM_PLAIN;
487 ldlm_res2desc(&res, &desc->l_resource);
488 /* Convert "new" lock mode to something old client can
490 if ((lock->l_req_mode == LCK_CR) ||
491 (lock->l_req_mode == LCK_CW))
492 desc->l_req_mode = LCK_PR;
494 desc->l_req_mode = lock->l_req_mode;
495 if ((lock->l_granted_mode == LCK_CR) ||
496 (lock->l_granted_mode == LCK_CW)) {
497 desc->l_granted_mode = LCK_PR;
499 /* We never grant PW/EX locks to clients */
500 LASSERT((lock->l_granted_mode != LCK_PW) &&
501 (lock->l_granted_mode != LCK_EX));
502 desc->l_granted_mode = lock->l_granted_mode;
505 /* We do not copy policy here, because there is no
506 policy for plain locks */
508 ldlm_res2desc(lock->l_resource, &desc->l_resource);
509 desc->l_req_mode = lock->l_req_mode;
510 desc->l_granted_mode = lock->l_granted_mode;
511 desc->l_policy_data = lock->l_policy_data;
515 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
516 struct list_head *work_list)
518 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
519 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
520 lock->l_flags |= LDLM_FL_AST_SENT;
521 /* If the enqueuing client said so, tell the AST recipient to
522 * discard dirty data, rather than writing back. */
523 if (new->l_flags & LDLM_AST_DISCARD_DATA)
524 lock->l_flags |= LDLM_FL_DISCARD_DATA;
525 LASSERT(list_empty(&lock->l_bl_ast));
526 list_add(&lock->l_bl_ast, work_list);
528 LASSERT(lock->l_blocking_lock == NULL);
529 lock->l_blocking_lock = LDLM_LOCK_GET(new);
533 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
535 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
536 lock->l_flags |= LDLM_FL_CP_REQD;
537 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
538 LASSERT(list_empty(&lock->l_cp_ast));
539 list_add(&lock->l_cp_ast, work_list);
544 /* must be called with lr_lock held */
545 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
546 struct list_head *work_list)
549 check_res_locked(lock->l_resource);
551 ldlm_add_bl_work_item(lock, new, work_list);
553 ldlm_add_cp_work_item(lock, work_list);
557 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
559 struct ldlm_lock *lock;
561 lock = ldlm_handle2lock(lockh);
562 LASSERT(lock != NULL);
563 ldlm_lock_addref_internal(lock, mode);
567 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
569 ldlm_lock_remove_from_lru(lock);
570 if (mode & (LCK_NL | LCK_CR | LCK_PR))
572 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
575 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
578 /* only called for local locks */
579 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
581 lock_res_and_lock(lock);
582 ldlm_lock_addref_internal_nolock(lock, mode);
583 unlock_res_and_lock(lock);
586 /* only called in ldlm_flock_destroy and for local locks.
587 * for LDLM_FLOCK type locks, l_blocking_ast is null, and
588 * ldlm_lock_remove_from_lru() does nothing, it is safe
589 * for ldlm_flock_destroy usage by dropping some code */
590 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
592 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
593 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
594 LASSERT(lock->l_readers > 0);
597 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
598 LASSERT(lock->l_writers > 0);
602 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
605 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
607 struct ldlm_namespace *ns;
610 lock_res_and_lock(lock);
612 ns = lock->l_resource->lr_namespace;
614 ldlm_lock_decref_internal_nolock(lock, mode);
616 if (lock->l_flags & LDLM_FL_LOCAL &&
617 !lock->l_readers && !lock->l_writers) {
618 /* If this is a local lock on a server namespace and this was
619 * the last reference, cancel the lock. */
620 CDEBUG(D_INFO, "forcing cancel of local lock\n");
621 lock->l_flags |= LDLM_FL_CBPENDING;
624 if (!lock->l_readers && !lock->l_writers &&
625 (lock->l_flags & LDLM_FL_CBPENDING)) {
626 /* If we received a blocked AST and this was the last reference,
627 * run the callback. */
628 if (ns_is_server(ns) && lock->l_export)
629 CERROR("FL_CBPENDING set on non-local lock--just a "
632 LDLM_DEBUG(lock, "final decref done on cbpending lock");
634 LDLM_LOCK_GET(lock); /* dropped by bl thread */
635 ldlm_lock_remove_from_lru(lock);
636 unlock_res_and_lock(lock);
637 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
638 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
639 ldlm_handle_bl_callback(ns, NULL, lock);
640 } else if (ns_is_client(ns) &&
641 !lock->l_readers && !lock->l_writers &&
642 !(lock->l_flags & LDLM_FL_NO_LRU)) {
643 /* If this is a client-side namespace and this was the last
644 * reference, put it on the LRU. */
645 ldlm_lock_add_to_lru(lock);
646 unlock_res_and_lock(lock);
647 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
648 * are not supported by the server, otherwise, it is done on
650 if (!exp_connect_cancelset(lock->l_conn_export) &&
651 !ns_connect_lru_resize(ns))
652 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
654 unlock_res_and_lock(lock);
660 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
662 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
663 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
664 ldlm_lock_decref_internal(lock, mode);
668 /* This will drop a lock reference and mark it for destruction, but will not
669 * necessarily cancel the lock before returning. */
670 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
672 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
675 LASSERT(lock != NULL);
677 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
678 lock_res_and_lock(lock);
679 lock->l_flags |= LDLM_FL_CBPENDING;
680 unlock_res_and_lock(lock);
681 ldlm_lock_decref_internal(lock, mode);
686 * search_granted_lock
689 * Finds a position to insert the new lock.
691 * queue [input]: the granted list where search acts on;
692 * req [input]: the lock whose position to be located;
693 * lockp [output]: the position where the lock should be inserted before, or
694 * NULL indicating @req should be appended to @queue.
696 * Bit-masks combination of following values indicating in which way the
697 * lock need to be inserted.
698 * - LDLM_JOIN_NONE: noting about skip list needs to be fixed;
699 * - LDLM_MODE_JOIN_RIGHT: @req needs join right becoming the head of a
701 * - LDLM_POLICY_JOIN_RIGHT: @req needs join right becoming the head of
704 * - ldlm_grant_lock_with_skiplist
706 static int search_granted_lock(struct list_head *queue,
707 struct ldlm_lock *req,
708 struct ldlm_lock **lockp)
710 struct list_head *tmp, *tmp_tail;
711 struct ldlm_lock *lock, *mode_head_lock;
712 int rc = LDLM_JOIN_NONE;
715 list_for_each(tmp, queue) {
716 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
718 if (lock->l_req_mode != req->l_req_mode) {
719 if (LDLM_SL_HEAD(&lock->l_sl_mode))
720 tmp = &list_entry(lock->l_sl_mode.next,
722 l_sl_mode)->l_res_link;
726 /* found the same mode group */
727 if (lock->l_resource->lr_type == LDLM_PLAIN) {
729 rc = LDLM_MODE_JOIN_RIGHT;
731 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
733 if (LDLM_SL_HEAD(&lock->l_sl_mode))
734 tmp_tail = &list_entry(lock->l_sl_mode.next,
736 l_sl_mode)->l_res_link;
737 mode_head_lock = lock;
739 if (lock->l_policy_data.l_inodebits.bits ==
740 req->l_policy_data.l_inodebits.bits) {
741 /* matched policy lock is found */
743 rc |= LDLM_POLICY_JOIN_RIGHT;
745 /* if the policy group head is also a
746 * mode group head or a single mode
748 if (LDLM_SL_HEAD(&lock->l_sl_mode) ||
750 LDLM_SL_EMPTY(&lock->l_sl_mode)))
751 rc |= LDLM_MODE_JOIN_RIGHT;
755 if (LDLM_SL_HEAD(&lock->l_sl_policy))
756 tmp = &list_entry(lock->l_sl_policy.next,
758 l_sl_policy)->l_res_link;
764 lock = list_entry(tmp, struct ldlm_lock,
766 } /* for all locks in the matched mode group */
768 /* no matched policy group is found, insert before
769 * the mode group head lock */
770 *lockp = mode_head_lock;
771 rc = LDLM_MODE_JOIN_RIGHT;
774 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
779 /* no matched mode group is found, append to the end */
787 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
788 struct ldlm_lock *lockp,
791 struct ldlm_resource *res = lock->l_resource;
794 LASSERT(lockp || join == LDLM_JOIN_NONE);
796 check_res_locked(res);
798 ldlm_resource_dump(D_OTHER, res);
799 CDEBUG(D_OTHER, "About to add this lock:\n");
800 ldlm_lock_dump(D_OTHER, lock, 0);
802 if (lock->l_destroyed) {
803 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
807 LASSERT(list_empty(&lock->l_res_link));
810 list_add_tail(&lock->l_res_link, &lock->l_resource->lr_granted);
811 else if ((join & LDLM_MODE_JOIN_LEFT) || (join & LDLM_POLICY_JOIN_LEFT))
812 list_add(&lock->l_res_link, &lockp->l_res_link);
814 list_add_tail(&lock->l_res_link, &lockp->l_res_link);
817 if (join & LDLM_MODE_JOIN_RIGHT) {
818 LASSERT(! LDLM_SL_TAIL(&lockp->l_sl_mode));
819 if (LDLM_SL_EMPTY(&lockp->l_sl_mode)) {
820 lock->l_sl_mode.next = &lockp->l_sl_mode;
821 lockp->l_sl_mode.prev = &lock->l_sl_mode;
822 } else if (LDLM_SL_HEAD(&lockp->l_sl_mode)) {
823 lock->l_sl_mode.next = lockp->l_sl_mode.next;
824 lockp->l_sl_mode.next = NULL;
825 lock->l_sl_mode.next->prev = &lock->l_sl_mode;
827 } else if (join & LDLM_MODE_JOIN_LEFT) {
828 LASSERT(! LDLM_SL_HEAD(&lockp->l_sl_mode));
829 if (LDLM_SL_EMPTY(&lockp->l_sl_mode)) {
830 lock->l_sl_mode.prev = &lockp->l_sl_mode;
831 lockp->l_sl_mode.next = &lock->l_sl_mode;
832 } else if (LDLM_SL_TAIL(&lockp->l_sl_mode)) {
833 lock->l_sl_mode.prev = lockp->l_sl_mode.prev;
834 lockp->l_sl_mode.prev = NULL;
835 lock->l_sl_mode.prev->next = &lock->l_sl_mode;
839 if (join & LDLM_POLICY_JOIN_RIGHT) {
840 LASSERT(! LDLM_SL_TAIL(&lockp->l_sl_policy));
841 if (LDLM_SL_EMPTY(&lockp->l_sl_policy)) {
842 lock->l_sl_policy.next = &lockp->l_sl_policy;
843 lockp->l_sl_policy.prev = &lock->l_sl_policy;
844 } else if (LDLM_SL_HEAD(&lockp->l_sl_policy)) {
845 lock->l_sl_policy.next = lockp->l_sl_policy.next;
846 lockp->l_sl_policy.next = NULL;
847 lock->l_sl_policy.next->prev = &lock->l_sl_policy;
849 } else if (join & LDLM_POLICY_JOIN_LEFT) {
850 LASSERT(! LDLM_SL_HEAD(&lockp->l_sl_policy));
851 if (LDLM_SL_EMPTY(&lockp->l_sl_policy)) {
852 lock->l_sl_policy.prev = &lockp->l_sl_policy;
853 lockp->l_sl_policy.next = &lock->l_sl_policy;
854 } else if (LDLM_SL_TAIL(&lockp->l_sl_policy)) {
855 lock->l_sl_policy.prev = lockp->l_sl_policy.prev;
856 lockp->l_sl_policy.prev = NULL;
857 lock->l_sl_policy.prev->next = &lock->l_sl_policy;
864 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
866 int join = LDLM_JOIN_NONE;
867 struct ldlm_lock *lockp = NULL;
870 LASSERT(lock->l_req_mode == lock->l_granted_mode);
872 join = search_granted_lock(&lock->l_resource->lr_granted, lock, &lockp);
873 ldlm_granted_list_add_lock(lock, lockp, join);
878 * - ldlm_lock_enqueue
879 * - ldlm_reprocess_queue
880 * - ldlm_lock_convert
882 * must be called with lr_lock held
884 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
886 struct ldlm_resource *res = lock->l_resource;
889 check_res_locked(res);
891 lock->l_granted_mode = lock->l_req_mode;
892 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
893 ldlm_grant_lock_with_skiplist(lock);
894 else if (res->lr_type == LDLM_EXTENT)
895 ldlm_extent_add_lock(res, lock);
897 ldlm_resource_add_lock(res, &res->lr_granted, lock);
899 if (lock->l_granted_mode < res->lr_most_restr)
900 res->lr_most_restr = lock->l_granted_mode;
902 if (work_list && lock->l_completion_ast != NULL)
903 ldlm_add_ast_work_item(lock, NULL, work_list);
905 ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
909 /* returns a referenced lock or NULL. See the flag descriptions below, in the
910 * comment above ldlm_lock_match */
911 static struct ldlm_lock *search_queue(struct list_head *queue,
913 ldlm_policy_data_t *policy,
914 struct ldlm_lock *old_lock, int flags)
916 struct ldlm_lock *lock;
917 struct list_head *tmp;
919 list_for_each(tmp, queue) {
922 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
924 if (lock == old_lock)
927 /* llite sometimes wants to match locks that will be
928 * canceled when their users drop, but we allow it to match
929 * if it passes in CBPENDING and the lock still has users.
930 * this is generally only going to be used by children
931 * whose parents already hold a lock so forward progress
932 * can still happen. */
933 if (lock->l_flags & LDLM_FL_CBPENDING &&
934 !(flags & LDLM_FL_CBPENDING))
936 if (lock->l_flags & LDLM_FL_CBPENDING &&
937 lock->l_readers == 0 && lock->l_writers == 0)
940 if (!(lock->l_req_mode & *mode))
943 match = lock->l_req_mode;
944 if (lock->l_resource->lr_type == LDLM_EXTENT &&
945 (lock->l_policy_data.l_extent.start >
946 policy->l_extent.start ||
947 lock->l_policy_data.l_extent.end < policy->l_extent.end))
950 if (unlikely(match == LCK_GROUP) &&
951 lock->l_resource->lr_type == LDLM_EXTENT &&
952 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
955 /* We match if we have existing lock with same or wider set
957 if (lock->l_resource->lr_type == LDLM_IBITS &&
958 ((lock->l_policy_data.l_inodebits.bits &
959 policy->l_inodebits.bits) !=
960 policy->l_inodebits.bits))
963 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
966 if ((flags & LDLM_FL_LOCAL_ONLY) &&
967 !(lock->l_flags & LDLM_FL_LOCAL))
970 if (flags & LDLM_FL_TEST_LOCK) {
972 ldlm_lock_touch_in_lru(lock);
974 ldlm_lock_addref_internal_nolock(lock, match);
983 void ldlm_lock_allow_match(struct ldlm_lock *lock)
985 lock_res_and_lock(lock);
986 lock->l_flags |= LDLM_FL_LVB_READY;
987 cfs_waitq_signal(&lock->l_waitq);
988 unlock_res_and_lock(lock);
991 /* Can be called in two ways:
993 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
994 * for a duplicate of.
996 * Otherwise, all of the fields must be filled in, to match against.
998 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
999 * server (ie, connh is NULL)
1000 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1001 * list will be considered
1002 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1003 * to be canceled can still be matched as long as they still have reader
1004 * or writer refernces
1005 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1006 * just tell us if we would have matched.
1008 * Returns 1 if it finds an already-existing lock that is compatible; in this
1009 * case, lockh is filled in with a addref()ed lock
1011 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1012 struct ldlm_res_id *res_id, ldlm_type_t type,
1013 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1014 struct lustre_handle *lockh)
1016 struct ldlm_resource *res;
1017 struct ldlm_lock *lock, *old_lock = NULL;
1022 old_lock = ldlm_handle2lock(lockh);
1025 ns = old_lock->l_resource->lr_namespace;
1026 res_id = &old_lock->l_resource->lr_name;
1027 type = old_lock->l_resource->lr_type;
1028 mode = old_lock->l_req_mode;
1031 res = ldlm_resource_get(ns, NULL, *res_id, type, 0);
1033 LASSERT(old_lock == NULL);
1039 lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
1042 if (flags & LDLM_FL_BLOCK_GRANTED)
1044 lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
1047 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
1054 ldlm_resource_putref(res);
1057 ldlm_lock2handle(lock, lockh);
1058 if ((flags & LDLM_FL_LVB_READY) && (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1059 struct l_wait_info lwi;
1060 if (lock->l_completion_ast) {
1061 int err = lock->l_completion_ast(lock,
1062 LDLM_FL_WAIT_NOREPROC,
1065 if (flags & LDLM_FL_TEST_LOCK)
1066 LDLM_LOCK_PUT(lock);
1068 ldlm_lock_decref_internal(lock, mode);
1074 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout), NULL,
1075 LWI_ON_SIGNAL_NOOP, NULL);
1077 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1078 l_wait_event(lock->l_waitq,
1079 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1084 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1085 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1086 res_id->name[2] : policy->l_extent.start,
1087 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1088 res_id->name[3] : policy->l_extent.end);
1089 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1090 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1091 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1092 type, mode, res_id->name[0], res_id->name[1],
1093 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1094 res_id->name[2] :policy->l_extent.start,
1095 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1096 res_id->name[3] : policy->l_extent.end);
1099 LDLM_LOCK_PUT(old_lock);
1100 if (flags & LDLM_FL_TEST_LOCK && rc)
1101 LDLM_LOCK_PUT(lock);
1103 return rc ? mode : 0;
1106 /* Returns a referenced lock */
1107 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1108 struct ldlm_res_id res_id, ldlm_type_t type,
1110 ldlm_blocking_callback blocking,
1111 ldlm_completion_callback completion,
1112 ldlm_glimpse_callback glimpse,
1113 void *data, __u32 lvb_len)
1115 struct ldlm_lock *lock;
1116 struct ldlm_resource *res;
1119 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1123 lock = ldlm_lock_new(res);
1124 ldlm_resource_putref(res);
1129 lock->l_req_mode = mode;
1130 lock->l_ast_data = data;
1131 lock->l_blocking_ast = blocking;
1132 lock->l_completion_ast = completion;
1133 lock->l_glimpse_ast = glimpse;
1134 lock->l_pid = cfs_curproc_pid();
1136 lock->l_tree_node = NULL;
1137 /* if this is the extent lock, allocate the interval tree node */
1138 if (type == LDLM_EXTENT) {
1139 if (ldlm_interval_alloc(lock) == NULL)
1144 lock->l_lvb_len = lvb_len;
1145 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1146 if (lock->l_lvb_data == NULL)
1153 if (lock->l_lvb_data)
1154 OBD_FREE(lock->l_lvb_data, lvb_len);
1155 ldlm_interval_free(ldlm_interval_detach(lock));
1156 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1160 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1161 struct ldlm_lock **lockp,
1162 void *cookie, int *flags)
1164 struct ldlm_lock *lock = *lockp;
1165 struct ldlm_resource *res = lock->l_resource;
1166 int local = ns_is_client(res->lr_namespace);
1167 ldlm_processing_policy policy;
1168 ldlm_error_t rc = ELDLM_OK;
1171 do_gettimeofday(&lock->l_enqueued_time);
1172 /* policies are not executed on the client or during replay */
1173 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1174 && !local && ns->ns_policy) {
1175 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1177 if (rc == ELDLM_LOCK_REPLACED) {
1178 /* The lock that was returned has already been granted,
1179 * and placed into lockp. If it's not the same as the
1180 * one we passed in, then destroy the old one and our
1181 * work here is done. */
1182 if (lock != *lockp) {
1183 ldlm_lock_destroy(lock);
1184 LDLM_LOCK_PUT(lock);
1186 *flags |= LDLM_FL_LOCK_CHANGED;
1188 } else if (rc != ELDLM_OK ||
1189 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1190 ldlm_lock_destroy(lock);
1195 lock_res_and_lock(lock);
1196 if (local && lock->l_req_mode == lock->l_granted_mode) {
1197 /* The server returned a blocked lock, but it was granted before
1198 * we got a chance to actually enqueue it. We don't need to do
1200 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1201 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1202 GOTO(out, ELDLM_OK);
1205 /* Some flags from the enqueue want to make it into the AST, via the
1206 * lock's l_flags. */
1207 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1209 /* This distinction between local lock trees is very important; a client
1210 * namespace only has information about locks taken by that client, and
1211 * thus doesn't have enough information to decide for itself if it can
1212 * be granted (below). In this case, we do exactly what the server
1213 * tells us to do, as dictated by the 'flags'.
1215 * We do exactly the same thing during recovery, when the server is
1216 * more or less trusting the clients not to lie.
1218 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1219 * granted/converting queues. */
1220 ldlm_resource_unlink_lock(lock);
1222 if (*flags & LDLM_FL_BLOCK_CONV)
1223 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1224 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1225 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1227 ldlm_grant_lock(lock, NULL);
1228 GOTO(out, ELDLM_OK);
1229 } else if (*flags & LDLM_FL_REPLAY) {
1230 if (*flags & LDLM_FL_BLOCK_CONV) {
1231 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1232 GOTO(out, ELDLM_OK);
1233 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1234 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1235 GOTO(out, ELDLM_OK);
1236 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1237 ldlm_grant_lock(lock, NULL);
1238 GOTO(out, ELDLM_OK);
1240 /* If no flags, fall through to normal enqueue path. */
1243 policy = ldlm_processing_policy_table[res->lr_type];
1244 policy(lock, flags, 1, &rc, NULL);
1247 unlock_res_and_lock(lock);
1251 /* Must be called with namespace taken: queue is waiting or converting. */
1252 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1253 struct list_head *work_list)
1255 struct list_head *tmp, *pos;
1256 ldlm_processing_policy policy;
1258 int rc = LDLM_ITER_CONTINUE;
1262 check_res_locked(res);
1264 policy = ldlm_processing_policy_table[res->lr_type];
1267 list_for_each_safe(tmp, pos, queue) {
1268 struct ldlm_lock *pending;
1269 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1271 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1274 rc = policy(pending, &flags, 0, &err, work_list);
1275 if (rc != LDLM_ITER_CONTINUE)
1282 /* Helper function for pair ldlm_run_{bl,cp}_ast_work().
1284 * Send an existing rpc set specified by @arg->set and then
1285 * destroy it. Create new one if @do_create flag is set. */
1287 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1291 rc = ptlrpc_set_wait(arg->set);
1292 if (arg->type == LDLM_BL_CALLBACK)
1293 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1294 ptlrpc_set_destroy(arg->set);
1297 arg->set = ptlrpc_prep_set();
1300 int ldlm_run_bl_ast_work(struct list_head *rpc_list)
1302 struct ldlm_cb_set_arg arg;
1303 struct list_head *tmp, *pos;
1304 struct ldlm_lock_desc d;
1309 arg.set = ptlrpc_prep_set();
1310 atomic_set(&arg.restart, 0);
1311 arg.type = LDLM_BL_CALLBACK;
1314 list_for_each_safe(tmp, pos, rpc_list) {
1315 struct ldlm_lock *lock =
1316 list_entry(tmp, struct ldlm_lock, l_bl_ast);
1318 /* nobody should touch l_bl_ast */
1319 lock_res_and_lock(lock);
1320 list_del_init(&lock->l_bl_ast);
1322 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1323 LASSERT(lock->l_bl_ast_run == 0);
1324 LASSERT(lock->l_blocking_lock);
1325 lock->l_bl_ast_run++;
1326 unlock_res_and_lock(lock);
1328 ldlm_lock2desc(lock->l_blocking_lock, &d);
1330 LDLM_LOCK_PUT(lock->l_blocking_lock);
1331 lock->l_blocking_lock = NULL;
1332 rc = lock->l_blocking_ast(lock, &d, (void *)&arg,
1334 LDLM_LOCK_PUT(lock);
1337 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1338 * and create a new set for requests that remained in
1340 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1341 ldlm_send_and_maybe_create_set(&arg, 1);
1347 ldlm_send_and_maybe_create_set(&arg, 0);
1349 /* In case when number of ASTs is multiply of
1350 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1351 * @arg.set must be destroyed here, otherwise we get
1352 * write memory leaking. */
1353 ptlrpc_set_destroy(arg.set);
1355 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1358 int ldlm_run_cp_ast_work(struct list_head *rpc_list)
1360 struct ldlm_cb_set_arg arg;
1361 struct list_head *tmp, *pos;
1366 arg.set = ptlrpc_prep_set();
1367 atomic_set(&arg.restart, 0);
1368 arg.type = LDLM_CP_CALLBACK;
1370 /* It's possible to receive a completion AST before we've set
1371 * the l_completion_ast pointer: either because the AST arrived
1372 * before the reply, or simply because there's a small race
1373 * window between receiving the reply and finishing the local
1374 * enqueue. (bug 842)
1376 * This can't happen with the blocking_ast, however, because we
1377 * will never call the local blocking_ast until we drop our
1378 * reader/writer reference, which we won't do until we get the
1379 * reply and finish enqueueing. */
1382 list_for_each_safe(tmp, pos, rpc_list) {
1383 struct ldlm_lock *lock =
1384 list_entry(tmp, struct ldlm_lock, l_cp_ast);
1385 ldlm_completion_callback completion_callback;
1387 /* nobody should touch l_cp_ast */
1388 lock_res_and_lock(lock);
1389 list_del_init(&lock->l_cp_ast);
1390 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1391 /* save l_completion_ast since it can be changed by
1392 * mds_intent_policy(), see bug 14225 */
1393 completion_callback = lock->l_completion_ast;
1394 lock->l_flags &= ~LDLM_FL_CP_REQD;
1395 unlock_res_and_lock(lock);
1397 if (completion_callback != NULL) {
1398 rc = completion_callback(lock, 0, (void *)&arg);
1401 LDLM_LOCK_PUT(lock);
1403 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1404 * and create a new set for requests that remained in
1406 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1407 ldlm_send_and_maybe_create_set(&arg, 1);
1413 ldlm_send_and_maybe_create_set(&arg, 0);
1415 /* In case when number of ASTs is multiply of
1416 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1417 * @arg.set must be destroyed here, otherwise we get
1418 * write memory leaking. */
1419 ptlrpc_set_destroy(arg.set);
1421 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1424 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1426 ldlm_reprocess_all(res);
1427 return LDLM_ITER_CONTINUE;
1430 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1432 struct list_head *tmp;
1436 spin_lock(&ns->ns_hash_lock);
1437 for (i = 0; i < RES_HASH_SIZE; i++) {
1438 tmp = ns->ns_hash[i].next;
1439 while (tmp != &(ns->ns_hash[i])) {
1440 struct ldlm_resource *res =
1441 list_entry(tmp, struct ldlm_resource, lr_hash);
1443 ldlm_resource_getref(res);
1444 spin_unlock(&ns->ns_hash_lock);
1446 rc = reprocess_one_queue(res, NULL);
1448 spin_lock(&ns->ns_hash_lock);
1450 ldlm_resource_putref_locked(res);
1452 if (rc == LDLM_ITER_STOP)
1457 spin_unlock(&ns->ns_hash_lock);
1461 void ldlm_reprocess_all(struct ldlm_resource *res)
1463 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1467 /* Local lock trees don't get reprocessed. */
1468 if (ns_is_client(res->lr_namespace)) {
1475 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1476 if (rc == LDLM_ITER_CONTINUE)
1477 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1480 rc = ldlm_run_cp_ast_work(&rpc_list);
1481 if (rc == -ERESTART) {
1482 LASSERT(list_empty(&rpc_list));
1488 void ldlm_cancel_callback(struct ldlm_lock *lock)
1490 check_res_locked(lock->l_resource);
1491 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1492 lock->l_flags |= LDLM_FL_CANCEL;
1493 if (lock->l_blocking_ast) {
1494 // l_check_no_ns_lock(ns);
1495 unlock_res_and_lock(lock);
1496 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1498 lock_res_and_lock(lock);
1500 LDLM_DEBUG(lock, "no blocking ast");
1503 lock->l_flags |= LDLM_FL_BL_DONE;
1506 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1508 struct ldlm_lock *lock;
1510 if (req->l_resource->lr_type != LDLM_PLAIN &&
1511 req->l_resource->lr_type != LDLM_IBITS)
1514 if (LDLM_SL_HEAD(&req->l_sl_mode)) {
1515 lock = list_entry(req->l_res_link.next, struct ldlm_lock,
1517 if (req->l_sl_mode.next == &lock->l_sl_mode) {
1518 lock->l_sl_mode.prev = NULL;
1520 lock->l_sl_mode.next = req->l_sl_mode.next;
1521 lock->l_sl_mode.next->prev = &lock->l_sl_mode;
1523 req->l_sl_mode.next = NULL;
1524 } else if (LDLM_SL_TAIL(&req->l_sl_mode)) {
1525 lock = list_entry(req->l_res_link.prev, struct ldlm_lock,
1527 if (req->l_sl_mode.prev == &lock->l_sl_mode) {
1528 lock->l_sl_mode.next = NULL;
1530 lock->l_sl_mode.prev = req->l_sl_mode.prev;
1531 lock->l_sl_mode.prev->next = &lock->l_sl_mode;
1533 req->l_sl_mode.prev = NULL;
1536 if (LDLM_SL_HEAD(&req->l_sl_policy)) {
1537 lock = list_entry(req->l_res_link.next, struct ldlm_lock,
1539 if (req->l_sl_policy.next == &lock->l_sl_policy) {
1540 lock->l_sl_policy.prev = NULL;
1542 lock->l_sl_policy.next = req->l_sl_policy.next;
1543 lock->l_sl_policy.next->prev = &lock->l_sl_policy;
1545 req->l_sl_policy.next = NULL;
1546 } else if (LDLM_SL_TAIL(&req->l_sl_policy)) {
1547 lock = list_entry(req->l_res_link.prev, struct ldlm_lock,
1549 if (req->l_sl_policy.prev == &lock->l_sl_policy) {
1550 lock->l_sl_policy.next = NULL;
1552 lock->l_sl_policy.prev = req->l_sl_policy.prev;
1553 lock->l_sl_policy.prev->next = &lock->l_sl_policy;
1555 req->l_sl_policy.prev = NULL;
1559 void ldlm_lock_cancel(struct ldlm_lock *lock)
1561 struct ldlm_resource *res;
1562 struct ldlm_namespace *ns;
1565 lock_res_and_lock(lock);
1567 res = lock->l_resource;
1568 ns = res->lr_namespace;
1570 /* Please do not, no matter how tempting, remove this LBUG without
1571 * talking to me first. -phik */
1572 if (lock->l_readers || lock->l_writers) {
1573 LDLM_ERROR(lock, "lock still has references");
1577 ldlm_del_waiting_lock(lock);
1579 /* Releases res lock */
1580 ldlm_cancel_callback(lock);
1582 /* Yes, second time, just in case it was added again while we were
1583 running with no res lock in ldlm_cancel_callback */
1584 ldlm_del_waiting_lock(lock);
1585 ldlm_resource_unlink_lock(lock);
1586 ldlm_lock_destroy_nolock(lock);
1588 if (lock->l_granted_mode == lock->l_req_mode)
1589 ldlm_pool_del(&ns->ns_pool, lock);
1591 /* Make sure we will not be called again for same lock what is possible
1592 * if not to zero out lock->l_granted_mode */
1593 lock->l_granted_mode = 0;
1594 unlock_res_and_lock(lock);
1599 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1601 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1607 lock->l_ast_data = data;
1608 LDLM_LOCK_PUT(lock);
1612 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1614 struct ldlm_lock *lock;
1615 struct ldlm_resource *res;
1617 spin_lock(&exp->exp_ldlm_data.led_lock);
1618 while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
1619 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1620 struct ldlm_lock, l_export_chain);
1621 res = ldlm_resource_getref(lock->l_resource);
1622 LDLM_LOCK_GET(lock);
1623 spin_unlock(&exp->exp_ldlm_data.led_lock);
1625 LDLM_DEBUG(lock, "export %p", exp);
1626 ldlm_res_lvbo_update(res, NULL, 0, 1);
1628 ldlm_lock_cancel(lock);
1629 ldlm_reprocess_all(res);
1631 ldlm_resource_putref(res);
1632 LDLM_LOCK_PUT(lock);
1633 spin_lock(&exp->exp_ldlm_data.led_lock);
1635 spin_unlock(&exp->exp_ldlm_data.led_lock);
1638 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1641 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1642 struct ldlm_resource *res;
1643 struct ldlm_namespace *ns;
1646 struct ldlm_lock *mark_lock = NULL;
1647 int join= LDLM_JOIN_NONE;
1649 struct ldlm_interval *node;
1652 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1653 *flags |= LDLM_FL_BLOCK_GRANTED;
1654 RETURN(lock->l_resource);
1657 /* I can't check the type of lock here because the bitlock of lock
1658 * is not held here, so do the allocation blindly. -jay */
1659 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
1660 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1663 LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1664 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1666 lock_res_and_lock(lock);
1668 res = lock->l_resource;
1669 ns = res->lr_namespace;
1671 old_mode = lock->l_req_mode;
1672 lock->l_req_mode = new_mode;
1673 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1674 /* remember the lock position where the lock might be
1675 * added back to the granted list later and also
1676 * remember the join mode for skiplist fixing. */
1677 if (LDLM_SL_HEAD(&lock->l_sl_mode))
1678 join = LDLM_MODE_JOIN_RIGHT;
1679 else if (LDLM_SL_TAIL(&lock->l_sl_mode))
1680 join = LDLM_MODE_JOIN_LEFT;
1681 if (LDLM_SL_HEAD(&lock->l_sl_policy))
1682 join |= LDLM_POLICY_JOIN_RIGHT;
1683 else if (LDLM_SL_TAIL(&lock->l_sl_policy))
1684 join |= LDLM_POLICY_JOIN_LEFT;
1686 LASSERT(!((join & LDLM_MODE_JOIN_RIGHT) &&
1687 (join & LDLM_POLICY_JOIN_LEFT)));
1688 LASSERT(!((join & LDLM_MODE_JOIN_LEFT) &&
1689 (join & LDLM_POLICY_JOIN_RIGHT)));
1691 if ((join & LDLM_MODE_JOIN_LEFT) ||
1692 (join & LDLM_POLICY_JOIN_LEFT))
1693 mark_lock = list_entry(lock->l_res_link.prev,
1694 struct ldlm_lock, l_res_link);
1695 else if (lock->l_res_link.next != &res->lr_granted)
1696 mark_lock = list_entry(lock->l_res_link.next,
1697 struct ldlm_lock, l_res_link);
1699 ldlm_resource_unlink_lock(lock);
1700 if (res->lr_type == LDLM_EXTENT) {
1701 /* FIXME: ugly code, I have to attach the lock to a
1702 * interval node again since perhaps it will be granted
1704 CFS_INIT_LIST_HEAD(&node->li_group);
1705 ldlm_interval_attach(node, lock);
1710 /* If this is a local resource, put it on the appropriate list. */
1711 if (ns_is_client(res->lr_namespace)) {
1712 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1713 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1715 /* This should never happen, because of the way the
1716 * server handles conversions. */
1717 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1721 ldlm_grant_lock(lock, &rpc_list);
1723 /* FIXME: completion handling not with ns_lock held ! */
1724 if (lock->l_completion_ast)
1725 lock->l_completion_ast(lock, 0, NULL);
1729 ldlm_processing_policy policy;
1730 policy = ldlm_processing_policy_table[res->lr_type];
1731 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1732 if (rc == LDLM_ITER_STOP) {
1733 lock->l_req_mode = old_mode;
1734 if (res->lr_type == LDLM_EXTENT)
1735 ldlm_extent_add_lock(res, lock);
1737 ldlm_granted_list_add_lock(lock, mark_lock,
1741 *flags |= LDLM_FL_BLOCK_GRANTED;
1745 unlock_res_and_lock(lock);
1748 ldlm_run_cp_ast_work(&rpc_list);
1750 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1754 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1756 struct obd_device *obd = NULL;
1758 if (!((libcfs_debug | D_ERROR) & level))
1762 CDEBUG(level, " NULL LDLM lock\n");
1766 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1767 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1769 if (lock->l_conn_export != NULL)
1770 obd = lock->l_conn_export->exp_obd;
1771 if (lock->l_export && lock->l_export->exp_connection) {
1772 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1773 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1774 lock->l_remote_handle.cookie);
1775 } else if (obd == NULL) {
1776 CDEBUG(level, " Node: local\n");
1778 struct obd_import *imp = obd->u.cli.cl_import;
1779 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1780 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1781 lock->l_remote_handle.cookie);
1783 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64")\n", lock->l_resource,
1784 lock->l_resource->lr_name.name[0],
1785 lock->l_resource->lr_name.name[1]);
1786 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1787 "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
1788 ldlm_lockname[lock->l_granted_mode],
1789 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1791 if (lock->l_resource->lr_type == LDLM_EXTENT)
1792 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1793 " (req "LPU64"-"LPU64")\n",
1794 lock->l_policy_data.l_extent.start,
1795 lock->l_policy_data.l_extent.end,
1796 lock->l_req_extent.start, lock->l_req_extent.end);
1797 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1798 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1799 lock->l_policy_data.l_flock.pid,
1800 lock->l_policy_data.l_flock.start,
1801 lock->l_policy_data.l_flock.end);
1802 else if (lock->l_resource->lr_type == LDLM_IBITS)
1803 CDEBUG(level, " Bits: "LPX64"\n",
1804 lock->l_policy_data.l_inodebits.bits);
1807 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1809 struct ldlm_lock *lock;
1811 if (!((libcfs_debug | D_ERROR) & level))
1814 lock = ldlm_handle2lock(lockh);
1818 ldlm_lock_dump(D_OTHER, lock, 0);
1820 LDLM_LOCK_PUT(lock);
1823 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1824 struct libcfs_debug_msg_data *data, const char *fmt,
1828 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1830 va_start(args, fmt);
1831 if (lock->l_resource == NULL) {
1832 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1833 data->msg_fn, data->msg_line, fmt, args,
1834 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1835 "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
1836 LPX64" expref: %d pid: %u\n", lock,
1837 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1838 lock->l_readers, lock->l_writers,
1839 ldlm_lockname[lock->l_granted_mode],
1840 ldlm_lockname[lock->l_req_mode],
1841 lock->l_flags, lock->l_remote_handle.cookie,
1843 atomic_read(&lock->l_export->exp_refcount) : -99,
1849 switch (lock->l_resource->lr_type) {
1851 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1852 data->msg_fn, data->msg_line, fmt, args,
1853 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1854 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1855 "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
1856 " expref: %d pid: %u\n",
1857 lock->l_resource->lr_namespace->ns_name, lock,
1858 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1859 lock->l_readers, lock->l_writers,
1860 ldlm_lockname[lock->l_granted_mode],
1861 ldlm_lockname[lock->l_req_mode],
1862 lock->l_resource->lr_name.name[0],
1863 lock->l_resource->lr_name.name[1],
1864 atomic_read(&lock->l_resource->lr_refcount),
1865 ldlm_typename[lock->l_resource->lr_type],
1866 lock->l_policy_data.l_extent.start,
1867 lock->l_policy_data.l_extent.end,
1868 lock->l_req_extent.start, lock->l_req_extent.end,
1869 lock->l_flags, lock->l_remote_handle.cookie,
1871 atomic_read(&lock->l_export->exp_refcount) : -99,
1875 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1876 data->msg_fn, data->msg_line, fmt, args,
1877 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1878 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1879 "["LPU64"->"LPU64"] flags: %x remote: "LPX64
1880 " expref: %d pid: %u\n",
1881 lock->l_resource->lr_namespace->ns_name, lock,
1882 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1883 lock->l_readers, lock->l_writers,
1884 ldlm_lockname[lock->l_granted_mode],
1885 ldlm_lockname[lock->l_req_mode],
1886 lock->l_resource->lr_name.name[0],
1887 lock->l_resource->lr_name.name[1],
1888 atomic_read(&lock->l_resource->lr_refcount),
1889 ldlm_typename[lock->l_resource->lr_type],
1890 lock->l_policy_data.l_flock.pid,
1891 lock->l_policy_data.l_flock.start,
1892 lock->l_policy_data.l_flock.end,
1893 lock->l_flags, lock->l_remote_handle.cookie,
1895 atomic_read(&lock->l_export->exp_refcount) : -99,
1899 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1900 data->msg_fn, data->msg_line, fmt, args,
1901 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1902 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1903 "flags: %x remote: "LPX64" expref: %d "
1905 lock->l_resource->lr_namespace->ns_name,
1906 lock, lock->l_handle.h_cookie,
1907 atomic_read (&lock->l_refc),
1908 lock->l_readers, lock->l_writers,
1909 ldlm_lockname[lock->l_granted_mode],
1910 ldlm_lockname[lock->l_req_mode],
1911 lock->l_resource->lr_name.name[0],
1912 lock->l_resource->lr_name.name[1],
1913 lock->l_policy_data.l_inodebits.bits,
1914 atomic_read(&lock->l_resource->lr_refcount),
1915 ldlm_typename[lock->l_resource->lr_type],
1916 lock->l_flags, lock->l_remote_handle.cookie,
1918 atomic_read(&lock->l_export->exp_refcount) : -99,
1922 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1923 data->msg_fn, data->msg_line, fmt, args,
1924 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1925 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
1926 "remote: "LPX64" expref: %d pid: %u\n",
1927 lock->l_resource->lr_namespace->ns_name,
1928 lock, lock->l_handle.h_cookie,
1929 atomic_read (&lock->l_refc),
1930 lock->l_readers, lock->l_writers,
1931 ldlm_lockname[lock->l_granted_mode],
1932 ldlm_lockname[lock->l_req_mode],
1933 lock->l_resource->lr_name.name[0],
1934 lock->l_resource->lr_name.name[1],
1935 atomic_read(&lock->l_resource->lr_refcount),
1936 ldlm_typename[lock->l_resource->lr_type],
1937 lock->l_flags, lock->l_remote_handle.cookie,
1939 atomic_read(&lock->l_export->exp_refcount) : -99,
1945 EXPORT_SYMBOL(_ldlm_lock_debug);