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 # ifndef HAVE_VFS_INTENT_PATCHES
32 # include <linux/lustre_intent.h>
35 # include <liblustre.h>
36 # include <libcfs/kp30.h>
39 #include <obd_class.h>
40 #include "ldlm_internal.h"
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;
147 "final lock_put on destroyed lock, freeing it.");
149 res = lock->l_resource;
150 LASSERT(lock->l_destroyed);
151 LASSERT(list_empty(&lock->l_res_link));
152 LASSERT(list_empty(&lock->l_pending_chain));
154 atomic_dec(&res->lr_namespace->ns_locks);
155 ldlm_resource_putref(res);
156 lock->l_resource = NULL;
157 if (lock->l_export) {
158 class_export_put(lock->l_export);
159 lock->l_export = NULL;
162 if (lock->l_lvb_data != NULL)
163 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
165 ldlm_interval_free(ldlm_interval_detach(lock));
166 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
173 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
176 if (!list_empty(&lock->l_lru)) {
177 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
178 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
179 list_del_init(&lock->l_lru);
181 LASSERT(ns->ns_nr_unused >= 0);
187 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
189 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
192 spin_lock(&ns->ns_unused_lock);
193 rc = ldlm_lock_remove_from_lru_nolock(lock);
194 spin_unlock(&ns->ns_unused_lock);
199 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
201 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
202 lock->l_last_used = cfs_time_current();
203 LASSERT(list_empty(&lock->l_lru));
204 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
205 LASSERT(ns->ns_nr_unused >= 0);
209 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
211 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
213 spin_lock(&ns->ns_unused_lock);
214 ldlm_lock_add_to_lru_nolock(lock);
215 spin_unlock(&ns->ns_unused_lock);
219 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
221 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
223 spin_lock(&ns->ns_unused_lock);
224 if (!list_empty(&lock->l_lru)) {
225 ldlm_lock_remove_from_lru_nolock(lock);
226 ldlm_lock_add_to_lru_nolock(lock);
228 spin_unlock(&ns->ns_unused_lock);
232 /* This used to have a 'strict' flag, which recovery would use to mark an
233 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
234 * shall explain why it's gone: with the new hash table scheme, once you call
235 * ldlm_lock_destroy, you can never drop your final references on this lock.
236 * Because it's not in the hash table anymore. -phil */
237 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
241 if (lock->l_readers || lock->l_writers) {
242 LDLM_ERROR(lock, "lock still has references");
243 ldlm_lock_dump(D_ERROR, lock, 0);
247 if (!list_empty(&lock->l_res_link)) {
248 LDLM_ERROR(lock, "lock still on resource");
249 ldlm_lock_dump(D_ERROR, lock, 0);
253 if (lock->l_destroyed) {
254 LASSERT(list_empty(&lock->l_lru));
258 lock->l_destroyed = 1;
261 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
262 list_del_init(&lock->l_export_chain);
264 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
266 ldlm_lock_remove_from_lru(lock);
267 class_handle_unhash(&lock->l_handle);
270 /* Wake anyone waiting for this lock */
271 /* FIXME: I should probably add yet another flag, instead of using
272 * l_export to only call this on clients */
274 class_export_put(lock->l_export);
275 lock->l_export = NULL;
276 if (lock->l_export && lock->l_completion_ast)
277 lock->l_completion_ast(lock, 0);
283 void ldlm_lock_destroy(struct ldlm_lock *lock)
287 lock_res_and_lock(lock);
288 first = ldlm_lock_destroy_internal(lock);
289 unlock_res_and_lock(lock);
291 /* drop reference from hashtable only for first destroy */
297 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
301 first = ldlm_lock_destroy_internal(lock);
302 /* drop reference from hashtable only for first destroy */
308 /* this is called by portals_handle2object with the handle lock taken */
309 static void lock_handle_addref(void *lock)
311 LDLM_LOCK_GET((struct ldlm_lock *)lock);
315 * usage: pass in a resource on which you have done ldlm_resource_get
316 * pass in a parent lock on which you have done a ldlm_lock_get
317 * after return, ldlm_*_put the resource and parent
318 * returns: lock with refcount 2 - one for current caller and one for remote
320 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
322 struct ldlm_lock *lock;
325 if (resource == NULL)
328 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
332 spin_lock_init(&lock->l_lock);
333 lock->l_resource = ldlm_resource_getref(resource);
335 atomic_set(&lock->l_refc, 2);
336 CFS_INIT_LIST_HEAD(&lock->l_res_link);
337 CFS_INIT_LIST_HEAD(&lock->l_lru);
338 CFS_INIT_LIST_HEAD(&lock->l_export_chain);
339 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
340 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
341 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
342 cfs_waitq_init(&lock->l_waitq);
343 lock->l_blocking_lock = NULL;
344 lock->l_sl_mode.prev = NULL;
345 lock->l_sl_mode.next = NULL;
346 lock->l_sl_policy.prev = NULL;
347 lock->l_sl_policy.next = NULL;
349 atomic_inc(&resource->lr_namespace->ns_locks);
350 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
351 class_handle_hash(&lock->l_handle, lock_handle_addref);
356 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
357 const struct ldlm_res_id *new_resid)
359 struct ldlm_resource *oldres = lock->l_resource;
360 struct ldlm_resource *newres;
364 LASSERT(ns_is_client(ns));
366 lock_res_and_lock(lock);
367 if (memcmp(new_resid, &lock->l_resource->lr_name,
368 sizeof(lock->l_resource->lr_name)) == 0) {
370 unlock_res_and_lock(lock);
374 LASSERT(new_resid->name[0] != 0);
376 /* This function assumes that the lock isn't on any lists */
377 LASSERT(list_empty(&lock->l_res_link));
379 type = oldres->lr_type;
380 unlock_res_and_lock(lock);
382 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
383 if (newres == NULL) {
388 lock_res_and_lock(lock);
389 LASSERT(memcmp(new_resid, &lock->l_resource->lr_name,
390 sizeof(lock->l_resource->lr_name)) != 0);
392 lock->l_resource = newres;
394 unlock_res_and_lock(lock);
396 /* ...and the flowers are still standing! */
397 ldlm_resource_putref(oldres);
406 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
408 lockh->cookie = lock->l_handle.h_cookie;
411 /* if flags: atomically get the lock and set the flags.
412 * Return NULL if flag already set
415 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
418 struct ldlm_namespace *ns;
419 struct ldlm_lock *lock, *retval = NULL;
424 lock = class_handle2object(handle->cookie);
428 LASSERT(lock->l_resource != NULL);
429 ns = lock->l_resource->lr_namespace;
432 lock_res_and_lock(lock);
434 /* It's unlikely but possible that someone marked the lock as
435 * destroyed after we did handle2object on it */
436 if (lock->l_destroyed) {
437 unlock_res_and_lock(lock);
438 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
443 if (flags && (lock->l_flags & flags)) {
444 unlock_res_and_lock(lock);
450 lock->l_flags |= flags;
452 unlock_res_and_lock(lock);
459 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
460 const struct lustre_handle *handle)
462 struct ldlm_lock *retval = NULL;
463 retval = __ldlm_handle2lock(handle, 0);
467 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
469 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
470 /* INODEBITS_INTEROP: If the other side does not support
471 * inodebits, reply with a plain lock descriptor.
473 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
474 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
475 struct ldlm_resource res = *lock->l_resource;
477 /* Make sure all the right bits are set in this lock we
478 are going to pass to client */
479 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
480 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
481 "Inappropriate inode lock bits during "
482 "conversion " LPU64 "\n",
483 lock->l_policy_data.l_inodebits.bits);
484 res.lr_type = LDLM_PLAIN;
485 ldlm_res2desc(&res, &desc->l_resource);
486 /* Convert "new" lock mode to something old client can
488 if ((lock->l_req_mode == LCK_CR) ||
489 (lock->l_req_mode == LCK_CW))
490 desc->l_req_mode = LCK_PR;
492 desc->l_req_mode = lock->l_req_mode;
493 if ((lock->l_granted_mode == LCK_CR) ||
494 (lock->l_granted_mode == LCK_CW)) {
495 desc->l_granted_mode = LCK_PR;
497 /* We never grant PW/EX locks to clients */
498 LASSERT((lock->l_granted_mode != LCK_PW) &&
499 (lock->l_granted_mode != LCK_EX));
500 desc->l_granted_mode = lock->l_granted_mode;
503 /* We do not copy policy here, because there is no
504 policy for plain locks */
506 ldlm_res2desc(lock->l_resource, &desc->l_resource);
507 desc->l_req_mode = lock->l_req_mode;
508 desc->l_granted_mode = lock->l_granted_mode;
509 desc->l_policy_data = lock->l_policy_data;
513 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
514 struct list_head *work_list)
516 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
517 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
518 lock->l_flags |= LDLM_FL_AST_SENT;
519 /* If the enqueuing client said so, tell the AST recipient to
520 * discard dirty data, rather than writing back. */
521 if (new->l_flags & LDLM_AST_DISCARD_DATA)
522 lock->l_flags |= LDLM_FL_DISCARD_DATA;
523 LASSERT(list_empty(&lock->l_bl_ast));
524 list_add(&lock->l_bl_ast, work_list);
526 LASSERT(lock->l_blocking_lock == NULL);
527 lock->l_blocking_lock = LDLM_LOCK_GET(new);
531 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
533 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
534 lock->l_flags |= LDLM_FL_CP_REQD;
535 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
536 LASSERT(list_empty(&lock->l_cp_ast));
537 list_add(&lock->l_cp_ast, work_list);
542 /* must be called with lr_lock held */
543 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
544 struct list_head *work_list)
547 check_res_locked(lock->l_resource);
549 ldlm_add_bl_work_item(lock, new, work_list);
551 ldlm_add_cp_work_item(lock, work_list);
555 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
557 struct ldlm_lock *lock;
559 lock = ldlm_handle2lock(lockh);
560 LASSERT(lock != NULL);
561 ldlm_lock_addref_internal(lock, mode);
565 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
567 ldlm_lock_remove_from_lru(lock);
568 if (mode & (LCK_NL | LCK_CR | LCK_PR))
570 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
573 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
576 /* only called for local locks */
577 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
579 lock_res_and_lock(lock);
580 ldlm_lock_addref_internal_nolock(lock, mode);
581 unlock_res_and_lock(lock);
584 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
586 struct ldlm_namespace *ns;
589 lock_res_and_lock(lock);
591 ns = lock->l_resource->lr_namespace;
593 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
594 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
595 LASSERT(lock->l_readers > 0);
598 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
599 LASSERT(lock->l_writers > 0);
603 if (lock->l_flags & LDLM_FL_LOCAL &&
604 !lock->l_readers && !lock->l_writers) {
605 /* If this is a local lock on a server namespace and this was
606 * the last reference, cancel the lock. */
607 CDEBUG(D_INFO, "forcing cancel of local lock\n");
608 lock->l_flags |= LDLM_FL_CBPENDING;
611 if (!lock->l_readers && !lock->l_writers &&
612 (lock->l_flags & LDLM_FL_CBPENDING)) {
613 /* If we received a blocked AST and this was the last reference,
614 * run the callback. */
615 if (ns_is_server(ns) && lock->l_export)
616 CERROR("FL_CBPENDING set on non-local lock--just a "
619 LDLM_DEBUG(lock, "final decref done on cbpending lock");
621 LDLM_LOCK_GET(lock); /* dropped by bl thread */
622 ldlm_lock_remove_from_lru(lock);
623 unlock_res_and_lock(lock);
624 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
625 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
626 ldlm_handle_bl_callback(ns, NULL, lock);
627 } else if (ns_is_client(ns) &&
628 !lock->l_readers && !lock->l_writers &&
629 !(lock->l_flags & LDLM_FL_NO_LRU) &&
630 !(lock->l_flags & LDLM_FL_BL_AST)) {
631 /* If this is a client-side namespace and this was the last
632 * reference, put it on the LRU. */
633 ldlm_lock_add_to_lru(lock);
634 unlock_res_and_lock(lock);
635 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
636 * are not supported by the server, otherwise, it is done on
638 if (!exp_connect_cancelset(lock->l_conn_export) &&
639 !ns_connect_lru_resize(ns))
640 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
642 unlock_res_and_lock(lock);
645 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
650 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
652 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
653 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
654 ldlm_lock_decref_internal(lock, mode);
658 /* This will drop a lock reference and mark it for destruction, but will not
659 * necessarily cancel the lock before returning. */
660 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
662 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
665 LASSERT(lock != NULL);
667 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
668 lock_res_and_lock(lock);
669 lock->l_flags |= LDLM_FL_CBPENDING;
670 unlock_res_and_lock(lock);
671 ldlm_lock_decref_internal(lock, mode);
676 * search_granted_lock
679 * Finds a position to insert the new lock.
681 * queue [input]: the granted list where search acts on;
682 * req [input]: the lock whose position to be located;
683 * lockp [output]: the position where the lock should be inserted before, or
684 * NULL indicating @req should be appended to @queue.
686 * Bit-masks combination of following values indicating in which way the
687 * lock need to be inserted.
688 * - LDLM_JOIN_NONE: noting about skip list needs to be fixed;
689 * - LDLM_MODE_JOIN_RIGHT: @req needs join right becoming the head of a
691 * - LDLM_POLICY_JOIN_RIGHT: @req needs join right becoming the head of
694 * - ldlm_grant_lock_with_skiplist
696 static int search_granted_lock(struct list_head *queue,
697 struct ldlm_lock *req,
698 struct ldlm_lock **lockp)
700 struct list_head *tmp, *tmp_tail;
701 struct ldlm_lock *lock, *mode_head_lock;
702 int rc = LDLM_JOIN_NONE;
705 list_for_each(tmp, queue) {
706 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
708 if (lock->l_req_mode != req->l_req_mode) {
709 if (LDLM_SL_HEAD(&lock->l_sl_mode))
710 tmp = &list_entry(lock->l_sl_mode.next,
712 l_sl_mode)->l_res_link;
716 /* found the same mode group */
717 if (lock->l_resource->lr_type == LDLM_PLAIN) {
719 rc = LDLM_MODE_JOIN_RIGHT;
721 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
723 if (LDLM_SL_HEAD(&lock->l_sl_mode))
724 tmp_tail = &list_entry(lock->l_sl_mode.next,
726 l_sl_mode)->l_res_link;
727 mode_head_lock = lock;
729 if (lock->l_policy_data.l_inodebits.bits ==
730 req->l_policy_data.l_inodebits.bits) {
731 /* matched policy lock is found */
733 rc |= LDLM_POLICY_JOIN_RIGHT;
735 /* if the policy group head is also a
736 * mode group head or a single mode
738 if (LDLM_SL_HEAD(&lock->l_sl_mode) ||
740 LDLM_SL_EMPTY(&lock->l_sl_mode)))
741 rc |= LDLM_MODE_JOIN_RIGHT;
745 if (LDLM_SL_HEAD(&lock->l_sl_policy))
746 tmp = &list_entry(lock->l_sl_policy.next,
748 l_sl_policy)->l_res_link;
754 lock = list_entry(tmp, struct ldlm_lock,
756 } /* for all locks in the matched mode group */
758 /* no matched policy group is found, insert before
759 * the mode group head lock */
760 *lockp = mode_head_lock;
761 rc = LDLM_MODE_JOIN_RIGHT;
764 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
769 /* no matched mode group is found, append to the end */
777 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
778 struct ldlm_lock *lockp,
781 struct ldlm_resource *res = lock->l_resource;
784 LASSERT(lockp || join == LDLM_JOIN_NONE);
786 check_res_locked(res);
788 ldlm_resource_dump(D_OTHER, res);
789 CDEBUG(D_OTHER, "About to add this lock:\n");
790 ldlm_lock_dump(D_OTHER, lock, 0);
792 if (lock->l_destroyed) {
793 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
797 LASSERT(list_empty(&lock->l_res_link));
800 list_add_tail(&lock->l_res_link, &lock->l_resource->lr_granted);
801 else if ((join & LDLM_MODE_JOIN_LEFT) || (join & LDLM_POLICY_JOIN_LEFT))
802 list_add(&lock->l_res_link, &lockp->l_res_link);
804 list_add_tail(&lock->l_res_link, &lockp->l_res_link);
807 if (join & LDLM_MODE_JOIN_RIGHT) {
808 LASSERT(! LDLM_SL_TAIL(&lockp->l_sl_mode));
809 if (LDLM_SL_EMPTY(&lockp->l_sl_mode)) {
810 lock->l_sl_mode.next = &lockp->l_sl_mode;
811 lockp->l_sl_mode.prev = &lock->l_sl_mode;
812 } else if (LDLM_SL_HEAD(&lockp->l_sl_mode)) {
813 lock->l_sl_mode.next = lockp->l_sl_mode.next;
814 lockp->l_sl_mode.next = NULL;
815 lock->l_sl_mode.next->prev = &lock->l_sl_mode;
817 } else if (join & LDLM_MODE_JOIN_LEFT) {
818 LASSERT(! LDLM_SL_HEAD(&lockp->l_sl_mode));
819 if (LDLM_SL_EMPTY(&lockp->l_sl_mode)) {
820 lock->l_sl_mode.prev = &lockp->l_sl_mode;
821 lockp->l_sl_mode.next = &lock->l_sl_mode;
822 } else if (LDLM_SL_TAIL(&lockp->l_sl_mode)) {
823 lock->l_sl_mode.prev = lockp->l_sl_mode.prev;
824 lockp->l_sl_mode.prev = NULL;
825 lock->l_sl_mode.prev->next = &lock->l_sl_mode;
829 if (join & LDLM_POLICY_JOIN_RIGHT) {
830 LASSERT(! LDLM_SL_TAIL(&lockp->l_sl_policy));
831 if (LDLM_SL_EMPTY(&lockp->l_sl_policy)) {
832 lock->l_sl_policy.next = &lockp->l_sl_policy;
833 lockp->l_sl_policy.prev = &lock->l_sl_policy;
834 } else if (LDLM_SL_HEAD(&lockp->l_sl_policy)) {
835 lock->l_sl_policy.next = lockp->l_sl_policy.next;
836 lockp->l_sl_policy.next = NULL;
837 lock->l_sl_policy.next->prev = &lock->l_sl_policy;
839 } else if (join & LDLM_POLICY_JOIN_LEFT) {
840 LASSERT(! LDLM_SL_HEAD(&lockp->l_sl_policy));
841 if (LDLM_SL_EMPTY(&lockp->l_sl_policy)) {
842 lock->l_sl_policy.prev = &lockp->l_sl_policy;
843 lockp->l_sl_policy.next = &lock->l_sl_policy;
844 } else if (LDLM_SL_TAIL(&lockp->l_sl_policy)) {
845 lock->l_sl_policy.prev = lockp->l_sl_policy.prev;
846 lockp->l_sl_policy.prev = NULL;
847 lock->l_sl_policy.prev->next = &lock->l_sl_policy;
854 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
856 int join = LDLM_JOIN_NONE;
857 struct ldlm_lock *lockp = NULL;
860 LASSERT(lock->l_req_mode == lock->l_granted_mode);
862 join = search_granted_lock(&lock->l_resource->lr_granted, lock, &lockp);
863 ldlm_granted_list_add_lock(lock, lockp, join);
868 * - ldlm_lock_enqueue
869 * - ldlm_reprocess_queue
870 * - ldlm_lock_convert
872 * must be called with lr_lock held
874 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
876 struct ldlm_resource *res = lock->l_resource;
879 check_res_locked(res);
881 lock->l_granted_mode = lock->l_req_mode;
882 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
883 ldlm_grant_lock_with_skiplist(lock);
884 else if (res->lr_type == LDLM_EXTENT)
885 ldlm_extent_add_lock(res, lock);
887 ldlm_resource_add_lock(res, &res->lr_granted, lock);
889 if (lock->l_granted_mode < res->lr_most_restr)
890 res->lr_most_restr = lock->l_granted_mode;
892 if (work_list && lock->l_completion_ast != NULL)
893 ldlm_add_ast_work_item(lock, NULL, work_list);
895 ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
899 /* returns a referenced lock or NULL. See the flag descriptions below, in the
900 * comment above ldlm_lock_match */
901 static struct ldlm_lock *search_queue(struct list_head *queue,
903 ldlm_policy_data_t *policy,
904 struct ldlm_lock *old_lock, int flags)
906 struct ldlm_lock *lock;
907 struct list_head *tmp;
909 list_for_each(tmp, queue) {
912 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
914 if (lock == old_lock)
917 /* llite sometimes wants to match locks that will be
918 * canceled when their users drop, but we allow it to match
919 * if it passes in CBPENDING and the lock still has users.
920 * this is generally only going to be used by children
921 * whose parents already hold a lock so forward progress
922 * can still happen. */
923 if (lock->l_flags & LDLM_FL_CBPENDING &&
924 !(flags & LDLM_FL_CBPENDING))
926 if (lock->l_flags & LDLM_FL_CBPENDING &&
927 lock->l_readers == 0 && lock->l_writers == 0)
930 if (!(lock->l_req_mode & *mode))
932 match = lock->l_req_mode;
934 if (lock->l_resource->lr_type == LDLM_EXTENT &&
935 (lock->l_policy_data.l_extent.start >
936 policy->l_extent.start ||
937 lock->l_policy_data.l_extent.end < policy->l_extent.end))
940 if (unlikely(match == LCK_GROUP) &&
941 lock->l_resource->lr_type == LDLM_EXTENT &&
942 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
945 /* We match if we have existing lock with same or wider set
947 if (lock->l_resource->lr_type == LDLM_IBITS &&
948 ((lock->l_policy_data.l_inodebits.bits &
949 policy->l_inodebits.bits) !=
950 policy->l_inodebits.bits))
953 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
956 if ((flags & LDLM_FL_LOCAL_ONLY) &&
957 !(lock->l_flags & LDLM_FL_LOCAL))
960 if (flags & LDLM_FL_TEST_LOCK) {
962 ldlm_lock_touch_in_lru(lock);
964 ldlm_lock_addref_internal_nolock(lock, match);
973 void ldlm_lock_allow_match(struct ldlm_lock *lock)
975 lock_res_and_lock(lock);
976 lock->l_flags |= LDLM_FL_LVB_READY;
977 cfs_waitq_signal(&lock->l_waitq);
978 unlock_res_and_lock(lock);
981 /* Can be called in two ways:
983 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
984 * for a duplicate of.
986 * Otherwise, all of the fields must be filled in, to match against.
988 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
989 * server (ie, connh is NULL)
990 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
991 * list will be considered
992 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
993 * to be canceled can still be matched as long as they still have reader
994 * or writer refernces
995 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
996 * just tell us if we would have matched.
998 * Returns 1 if it finds an already-existing lock that is compatible; in this
999 * case, lockh is filled in with a addref()ed lock
1001 * we also check security context, if that failed we simply return 0 (to keep
1002 * caller code unchanged), the context failure will be discovered by caller
1005 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1006 const struct ldlm_res_id *res_id, ldlm_type_t type,
1007 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1008 struct lustre_handle *lockh)
1010 struct ldlm_resource *res;
1011 struct ldlm_lock *lock, *old_lock = NULL;
1016 old_lock = ldlm_handle2lock(lockh);
1019 ns = old_lock->l_resource->lr_namespace;
1020 res_id = &old_lock->l_resource->lr_name;
1021 type = old_lock->l_resource->lr_type;
1022 mode = old_lock->l_req_mode;
1025 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1027 LASSERT(old_lock == NULL);
1033 lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
1036 if (flags & LDLM_FL_BLOCK_GRANTED)
1038 lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
1041 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
1048 ldlm_resource_putref(res);
1051 ldlm_lock2handle(lock, lockh);
1052 if ((flags & LDLM_FL_LVB_READY) &&
1053 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1054 struct l_wait_info lwi;
1055 if (lock->l_completion_ast) {
1056 int err = lock->l_completion_ast(lock,
1057 LDLM_FL_WAIT_NOREPROC,
1060 if (flags & LDLM_FL_TEST_LOCK)
1061 LDLM_LOCK_PUT(lock);
1063 ldlm_lock_decref_internal(lock, mode);
1069 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout), NULL,
1070 LWI_ON_SIGNAL_NOOP, NULL);
1072 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1073 l_wait_event(lock->l_waitq,
1074 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1079 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1080 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1081 res_id->name[2] : policy->l_extent.start,
1082 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1083 res_id->name[3] : policy->l_extent.end);
1085 /* check user's security context */
1086 if (lock->l_conn_export &&
1087 sptlrpc_import_check_ctx(
1088 class_exp2cliimp(lock->l_conn_export))) {
1089 if (!(flags & LDLM_FL_TEST_LOCK))
1090 ldlm_lock_decref_internal(lock, mode);
1094 if (flags & LDLM_FL_TEST_LOCK)
1095 LDLM_LOCK_PUT(lock);
1097 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1098 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1099 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1100 type, mode, res_id->name[0], res_id->name[1],
1101 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1102 res_id->name[2] :policy->l_extent.start,
1103 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1104 res_id->name[3] : policy->l_extent.end);
1107 LDLM_LOCK_PUT(old_lock);
1109 return rc ? mode : 0;
1112 /* Returns a referenced lock */
1113 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1114 const struct ldlm_res_id *res_id,
1117 ldlm_blocking_callback blocking,
1118 ldlm_completion_callback completion,
1119 ldlm_glimpse_callback glimpse,
1120 void *data, __u32 lvb_len)
1122 struct ldlm_lock *lock;
1123 struct ldlm_resource *res;
1126 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1130 lock = ldlm_lock_new(res);
1131 ldlm_resource_putref(res);
1136 lock->l_req_mode = mode;
1137 lock->l_ast_data = data;
1138 lock->l_blocking_ast = blocking;
1139 lock->l_completion_ast = completion;
1140 lock->l_glimpse_ast = glimpse;
1141 lock->l_pid = cfs_curproc_pid();
1143 lock->l_tree_node = NULL;
1144 /* if this is the extent lock, allocate the interval tree node */
1145 if (type == LDLM_EXTENT) {
1146 if (ldlm_interval_alloc(lock) == NULL)
1151 lock->l_lvb_len = lvb_len;
1152 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1153 if (lock->l_lvb_data == NULL)
1160 if (lock->l_lvb_data)
1161 OBD_FREE(lock->l_lvb_data, lvb_len);
1162 ldlm_interval_free(ldlm_interval_detach(lock));
1163 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1167 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1168 struct ldlm_lock **lockp,
1169 void *cookie, int *flags)
1171 struct ldlm_lock *lock = *lockp;
1172 struct ldlm_resource *res = lock->l_resource;
1173 int local = ns_is_client(res->lr_namespace);
1174 ldlm_processing_policy policy;
1175 ldlm_error_t rc = ELDLM_OK;
1176 struct ldlm_interval *node = NULL;
1179 do_gettimeofday(&lock->l_enqueued_time);
1180 /* policies are not executed on the client or during replay */
1181 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1182 && !local && ns->ns_policy) {
1183 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1185 if (rc == ELDLM_LOCK_REPLACED) {
1186 /* The lock that was returned has already been granted,
1187 * and placed into lockp. If it's not the same as the
1188 * one we passed in, then destroy the old one and our
1189 * work here is done. */
1190 if (lock != *lockp) {
1191 ldlm_lock_destroy(lock);
1192 LDLM_LOCK_PUT(lock);
1194 *flags |= LDLM_FL_LOCK_CHANGED;
1196 } else if (rc != ELDLM_OK ||
1197 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1198 ldlm_lock_destroy(lock);
1203 /* For a replaying lock, it might be already in granted list. So
1204 * unlinking the lock will cause the interval node to be freed, we
1205 * have to allocate the interval node early otherwise we can't regrant
1206 * this lock in the future. - jay */
1207 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1208 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO,
1211 lock_res_and_lock(lock);
1212 if (local && lock->l_req_mode == lock->l_granted_mode) {
1213 /* The server returned a blocked lock, but it was granted
1214 * before we got a chance to actually enqueue it. We don't
1215 * need to do anything else. */
1216 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1217 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1218 GOTO(out, ELDLM_OK);
1221 ldlm_resource_unlink_lock(lock);
1222 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1224 ldlm_lock_destroy_nolock(lock);
1225 GOTO(out, rc = -ENOMEM);
1228 CFS_INIT_LIST_HEAD(&node->li_group);
1229 ldlm_interval_attach(node, lock);
1233 /* Some flags from the enqueue want to make it into the AST, via the
1234 * lock's l_flags. */
1235 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1237 /* This distinction between local lock trees is very important; a client
1238 * namespace only has information about locks taken by that client, and
1239 * thus doesn't have enough information to decide for itself if it can
1240 * be granted (below). In this case, we do exactly what the server
1241 * tells us to do, as dictated by the 'flags'.
1243 * We do exactly the same thing during recovery, when the server is
1244 * more or less trusting the clients not to lie.
1246 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1247 * granted/converting queues. */
1249 if (*flags & LDLM_FL_BLOCK_CONV)
1250 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1251 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1252 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1254 ldlm_grant_lock(lock, NULL);
1255 GOTO(out, ELDLM_OK);
1256 } else if (*flags & LDLM_FL_REPLAY) {
1257 if (*flags & LDLM_FL_BLOCK_CONV) {
1258 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1259 GOTO(out, ELDLM_OK);
1260 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1261 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1262 GOTO(out, ELDLM_OK);
1263 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1264 ldlm_grant_lock(lock, NULL);
1265 GOTO(out, ELDLM_OK);
1267 /* If no flags, fall through to normal enqueue path. */
1270 policy = ldlm_processing_policy_table[res->lr_type];
1271 policy(lock, flags, 1, &rc, NULL);
1274 unlock_res_and_lock(lock);
1276 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1280 /* Must be called with namespace taken: queue is waiting or converting. */
1281 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1282 struct list_head *work_list)
1284 struct list_head *tmp, *pos;
1285 ldlm_processing_policy policy;
1287 int rc = LDLM_ITER_CONTINUE;
1291 check_res_locked(res);
1293 policy = ldlm_processing_policy_table[res->lr_type];
1296 list_for_each_safe(tmp, pos, queue) {
1297 struct ldlm_lock *pending;
1298 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1300 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1303 rc = policy(pending, &flags, 0, &err, work_list);
1304 if (rc != LDLM_ITER_CONTINUE)
1311 /* Helper function for ldlm_run_ast_work().
1313 * Send an existing rpc set specified by @arg->set and then
1314 * destroy it. Create new one if @do_create flag is set. */
1316 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1320 ptlrpc_set_wait(arg->set);
1321 if (arg->type == LDLM_BL_CALLBACK)
1322 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1323 ptlrpc_set_destroy(arg->set);
1326 arg->set = ptlrpc_prep_set();
1332 ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1334 struct ldlm_lock_desc d;
1335 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
1338 /* nobody should touch l_bl_ast */
1339 lock_res_and_lock(lock);
1340 list_del_init(&lock->l_bl_ast);
1342 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1343 LASSERT(lock->l_bl_ast_run == 0);
1344 LASSERT(lock->l_blocking_lock);
1345 lock->l_bl_ast_run++;
1346 unlock_res_and_lock(lock);
1348 ldlm_lock2desc(lock->l_blocking_lock, &d);
1350 LDLM_LOCK_PUT(lock->l_blocking_lock);
1351 lock->l_blocking_lock = NULL;
1352 lock->l_blocking_ast(lock, &d, (void *)arg,
1354 LDLM_LOCK_PUT(lock);
1360 ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1362 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
1363 ldlm_completion_callback completion_callback;
1367 /* It's possible to receive a completion AST before we've set
1368 * the l_completion_ast pointer: either because the AST arrived
1369 * before the reply, or simply because there's a small race
1370 * window between receiving the reply and finishing the local
1371 * enqueue. (bug 842)
1373 * This can't happen with the blocking_ast, however, because we
1374 * will never call the local blocking_ast until we drop our
1375 * reader/writer reference, which we won't do until we get the
1376 * reply and finish enqueueing. */
1378 /* nobody should touch l_cp_ast */
1379 lock_res_and_lock(lock);
1380 list_del_init(&lock->l_cp_ast);
1381 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1382 /* save l_completion_ast since it can be changed by
1383 * mds_intent_policy(), see bug 14225 */
1384 completion_callback = lock->l_completion_ast;
1385 lock->l_flags &= ~LDLM_FL_CP_REQD;
1386 unlock_res_and_lock(lock);
1388 if (completion_callback != NULL) {
1389 completion_callback(lock, 0, (void *)arg);
1392 LDLM_LOCK_PUT(lock);
1398 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1400 struct ldlm_lock_desc desc;
1401 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_export_chain);
1404 list_del_init(&lock->l_export_chain);
1406 /* the desc just pretend to exclusive */
1407 ldlm_lock2desc(lock, &desc);
1408 desc.l_req_mode = LCK_EX;
1409 desc.l_granted_mode = 0;
1411 lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1412 LDLM_LOCK_PUT(lock);
1417 int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
1419 struct ldlm_cb_set_arg arg;
1420 struct list_head *tmp, *pos;
1421 int (*work_ast_lock)(struct list_head *tmp, struct ldlm_cb_set_arg *arg);
1425 arg.set = ptlrpc_prep_set();
1426 atomic_set(&arg.restart, 0);
1428 case LDLM_WORK_BL_AST:
1429 arg.type = LDLM_BL_CALLBACK;
1430 work_ast_lock = ldlm_work_bl_ast_lock;
1432 case LDLM_WORK_CP_AST:
1433 arg.type = LDLM_CP_CALLBACK;
1434 work_ast_lock = ldlm_work_cp_ast_lock;
1436 case LDLM_WORK_REVOKE_AST:
1437 arg.type = LDLM_BL_CALLBACK;
1438 work_ast_lock = ldlm_work_revoke_ast_lock;
1445 list_for_each_safe(tmp, pos, rpc_list) {
1446 ast_count += work_ast_lock(tmp, &arg);
1448 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1449 * and create a new set for requests that remained in
1451 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1452 ldlm_send_and_maybe_create_set(&arg, 1);
1458 ldlm_send_and_maybe_create_set(&arg, 0);
1460 /* In case when number of ASTs is multiply of
1461 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1462 * @arg.set must be destroyed here, otherwise we get
1463 * write memory leaking. */
1464 ptlrpc_set_destroy(arg.set);
1466 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1469 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1471 ldlm_reprocess_all(res);
1472 return LDLM_ITER_CONTINUE;
1475 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1477 struct list_head *tmp;
1484 spin_lock(&ns->ns_hash_lock);
1485 for (i = 0; i < RES_HASH_SIZE; i++) {
1486 tmp = ns->ns_hash[i].next;
1487 while (tmp != &(ns->ns_hash[i])) {
1488 struct ldlm_resource *res =
1489 list_entry(tmp, struct ldlm_resource, lr_hash);
1491 ldlm_resource_getref(res);
1492 spin_unlock(&ns->ns_hash_lock);
1494 rc = reprocess_one_queue(res, NULL);
1496 spin_lock(&ns->ns_hash_lock);
1498 ldlm_resource_putref_locked(res);
1500 if (rc == LDLM_ITER_STOP)
1505 spin_unlock(&ns->ns_hash_lock);
1509 void ldlm_reprocess_all(struct ldlm_resource *res)
1511 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1515 /* Local lock trees don't get reprocessed. */
1516 if (ns_is_client(res->lr_namespace)) {
1523 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1524 if (rc == LDLM_ITER_CONTINUE)
1525 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1528 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1529 if (rc == -ERESTART) {
1530 LASSERT(list_empty(&rpc_list));
1536 void ldlm_cancel_callback(struct ldlm_lock *lock)
1538 check_res_locked(lock->l_resource);
1539 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1540 lock->l_flags |= LDLM_FL_CANCEL;
1541 if (lock->l_blocking_ast) {
1542 // l_check_no_ns_lock(ns);
1543 unlock_res_and_lock(lock);
1544 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1546 lock_res_and_lock(lock);
1548 LDLM_DEBUG(lock, "no blocking ast");
1551 lock->l_flags |= LDLM_FL_BL_DONE;
1554 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1556 struct ldlm_lock *lock;
1558 if (req->l_resource->lr_type != LDLM_PLAIN &&
1559 req->l_resource->lr_type != LDLM_IBITS)
1562 if (LDLM_SL_HEAD(&req->l_sl_mode)) {
1563 lock = list_entry(req->l_res_link.next, struct ldlm_lock,
1565 if (req->l_sl_mode.next == &lock->l_sl_mode) {
1566 lock->l_sl_mode.prev = NULL;
1568 lock->l_sl_mode.next = req->l_sl_mode.next;
1569 lock->l_sl_mode.next->prev = &lock->l_sl_mode;
1571 req->l_sl_mode.next = NULL;
1572 } else if (LDLM_SL_TAIL(&req->l_sl_mode)) {
1573 lock = list_entry(req->l_res_link.prev, struct ldlm_lock,
1575 if (req->l_sl_mode.prev == &lock->l_sl_mode) {
1576 lock->l_sl_mode.next = NULL;
1578 lock->l_sl_mode.prev = req->l_sl_mode.prev;
1579 lock->l_sl_mode.prev->next = &lock->l_sl_mode;
1581 req->l_sl_mode.prev = NULL;
1584 if (LDLM_SL_HEAD(&req->l_sl_policy)) {
1585 lock = list_entry(req->l_res_link.next, struct ldlm_lock,
1587 if (req->l_sl_policy.next == &lock->l_sl_policy) {
1588 lock->l_sl_policy.prev = NULL;
1590 lock->l_sl_policy.next = req->l_sl_policy.next;
1591 lock->l_sl_policy.next->prev = &lock->l_sl_policy;
1593 req->l_sl_policy.next = NULL;
1594 } else if (LDLM_SL_TAIL(&req->l_sl_policy)) {
1595 lock = list_entry(req->l_res_link.prev, struct ldlm_lock,
1597 if (req->l_sl_policy.prev == &lock->l_sl_policy) {
1598 lock->l_sl_policy.next = NULL;
1600 lock->l_sl_policy.prev = req->l_sl_policy.prev;
1601 lock->l_sl_policy.prev->next = &lock->l_sl_policy;
1603 req->l_sl_policy.prev = NULL;
1607 void ldlm_lock_cancel(struct ldlm_lock *lock)
1609 struct ldlm_resource *res;
1610 struct ldlm_namespace *ns;
1613 lock_res_and_lock(lock);
1615 res = lock->l_resource;
1616 ns = res->lr_namespace;
1618 /* Please do not, no matter how tempting, remove this LBUG without
1619 * talking to me first. -phik */
1620 if (lock->l_readers || lock->l_writers) {
1621 LDLM_ERROR(lock, "lock still has references");
1625 ldlm_del_waiting_lock(lock);
1627 /* Releases cancel callback. */
1628 ldlm_cancel_callback(lock);
1630 /* Yes, second time, just in case it was added again while we were
1631 running with no res lock in ldlm_cancel_callback */
1632 ldlm_del_waiting_lock(lock);
1633 ldlm_resource_unlink_lock(lock);
1634 ldlm_lock_destroy_nolock(lock);
1636 if (lock->l_granted_mode == lock->l_req_mode)
1637 ldlm_pool_del(&ns->ns_pool, lock);
1639 /* Make sure we will not be called again for same lock what is possible
1640 * if not to zero out lock->l_granted_mode */
1641 lock->l_granted_mode = 0;
1642 unlock_res_and_lock(lock);
1647 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1649 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1655 lock->l_ast_data = data;
1656 LDLM_LOCK_PUT(lock);
1660 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1662 struct ldlm_lock *lock;
1663 struct ldlm_resource *res;
1665 spin_lock(&exp->exp_ldlm_data.led_lock);
1666 while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
1667 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1668 struct ldlm_lock, l_export_chain);
1669 res = ldlm_resource_getref(lock->l_resource);
1670 LDLM_LOCK_GET(lock);
1671 spin_unlock(&exp->exp_ldlm_data.led_lock);
1673 LDLM_DEBUG(lock, "export %p", exp);
1674 ldlm_res_lvbo_update(res, NULL, 0, 1);
1676 ldlm_lock_cancel(lock);
1677 ldlm_reprocess_all(res);
1679 ldlm_resource_putref(res);
1680 LDLM_LOCK_PUT(lock);
1681 spin_lock(&exp->exp_ldlm_data.led_lock);
1683 spin_unlock(&exp->exp_ldlm_data.led_lock);
1686 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1689 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
1690 struct ldlm_resource *res;
1691 struct ldlm_namespace *ns;
1694 struct ldlm_lock *mark_lock = NULL;
1695 int join = LDLM_JOIN_NONE;
1697 struct ldlm_interval *node;
1700 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1701 *flags |= LDLM_FL_BLOCK_GRANTED;
1702 RETURN(lock->l_resource);
1705 /* I can't check the type of lock here because the bitlock of lock
1706 * is not held here, so do the allocation blindly. -jay */
1707 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
1708 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1711 LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1712 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1714 lock_res_and_lock(lock);
1716 res = lock->l_resource;
1717 ns = res->lr_namespace;
1719 old_mode = lock->l_req_mode;
1720 lock->l_req_mode = new_mode;
1721 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1722 /* remember the lock position where the lock might be
1723 * added back to the granted list later and also
1724 * remember the join mode for skiplist fixing. */
1725 if (LDLM_SL_HEAD(&lock->l_sl_mode))
1726 join = LDLM_MODE_JOIN_RIGHT;
1727 else if (LDLM_SL_TAIL(&lock->l_sl_mode))
1728 join = LDLM_MODE_JOIN_LEFT;
1729 if (LDLM_SL_HEAD(&lock->l_sl_policy))
1730 join |= LDLM_POLICY_JOIN_RIGHT;
1731 else if (LDLM_SL_TAIL(&lock->l_sl_policy))
1732 join |= LDLM_POLICY_JOIN_LEFT;
1734 LASSERT(!((join & LDLM_MODE_JOIN_RIGHT) &&
1735 (join & LDLM_POLICY_JOIN_LEFT)));
1736 LASSERT(!((join & LDLM_MODE_JOIN_LEFT) &&
1737 (join & LDLM_POLICY_JOIN_RIGHT)));
1739 if ((join & LDLM_MODE_JOIN_LEFT) ||
1740 (join & LDLM_POLICY_JOIN_LEFT))
1741 mark_lock = list_entry(lock->l_res_link.prev,
1742 struct ldlm_lock, l_res_link);
1743 else if (lock->l_res_link.next != &res->lr_granted)
1744 mark_lock = list_entry(lock->l_res_link.next,
1745 struct ldlm_lock, l_res_link);
1747 ldlm_resource_unlink_lock(lock);
1748 if (res->lr_type == LDLM_EXTENT) {
1749 /* FIXME: ugly code, I have to attach the lock to a
1750 * interval node again since perhaps it will be granted
1752 CFS_INIT_LIST_HEAD(&node->li_group);
1753 ldlm_interval_attach(node, lock);
1758 /* If this is a local resource, put it on the appropriate list. */
1759 if (ns_is_client(res->lr_namespace)) {
1760 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1761 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1763 /* This should never happen, because of the way the
1764 * server handles conversions. */
1765 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1769 ldlm_grant_lock(lock, &rpc_list);
1771 /* FIXME: completion handling not with ns_lock held ! */
1772 if (lock->l_completion_ast)
1773 lock->l_completion_ast(lock, 0, NULL);
1777 ldlm_processing_policy policy;
1778 policy = ldlm_processing_policy_table[res->lr_type];
1779 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1780 if (rc == LDLM_ITER_STOP) {
1781 lock->l_req_mode = old_mode;
1782 if (res->lr_type == LDLM_EXTENT)
1783 ldlm_extent_add_lock(res, lock);
1785 ldlm_granted_list_add_lock(lock, mark_lock,
1789 *flags |= LDLM_FL_BLOCK_GRANTED;
1793 unlock_res_and_lock(lock);
1796 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1798 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1802 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1804 struct obd_device *obd = NULL;
1806 if (!((libcfs_debug | D_ERROR) & level))
1810 CDEBUG(level, " NULL LDLM lock\n");
1814 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1815 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1817 if (lock->l_conn_export != NULL)
1818 obd = lock->l_conn_export->exp_obd;
1819 if (lock->l_export && lock->l_export->exp_connection) {
1820 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1821 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1822 lock->l_remote_handle.cookie);
1823 } else if (obd == NULL) {
1824 CDEBUG(level, " Node: local\n");
1826 struct obd_import *imp = obd->u.cli.cl_import;
1827 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1828 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1829 lock->l_remote_handle.cookie);
1831 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1833 lock->l_resource->lr_name.name[0],
1834 lock->l_resource->lr_name.name[1],
1835 lock->l_resource->lr_name.name[2]);
1836 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1837 "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
1838 ldlm_lockname[lock->l_granted_mode],
1839 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1841 if (lock->l_resource->lr_type == LDLM_EXTENT)
1842 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1843 " (req "LPU64"-"LPU64")\n",
1844 lock->l_policy_data.l_extent.start,
1845 lock->l_policy_data.l_extent.end,
1846 lock->l_req_extent.start, lock->l_req_extent.end);
1847 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1848 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1849 lock->l_policy_data.l_flock.pid,
1850 lock->l_policy_data.l_flock.start,
1851 lock->l_policy_data.l_flock.end);
1852 else if (lock->l_resource->lr_type == LDLM_IBITS)
1853 CDEBUG(level, " Bits: "LPX64"\n",
1854 lock->l_policy_data.l_inodebits.bits);
1857 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1859 struct ldlm_lock *lock;
1861 if (!((libcfs_debug | D_ERROR) & level))
1864 lock = ldlm_handle2lock(lockh);
1868 ldlm_lock_dump(D_OTHER, lock, 0);
1870 LDLM_LOCK_PUT(lock);
1873 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1874 struct libcfs_debug_msg_data *data, const char *fmt,
1878 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1880 va_start(args, fmt);
1882 if (lock->l_resource == NULL) {
1883 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1884 data->msg_fn, data->msg_line, fmt, args,
1885 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1886 "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
1887 LPX64" expref: %d pid: %u\n", lock,
1888 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1889 lock->l_readers, lock->l_writers,
1890 ldlm_lockname[lock->l_granted_mode],
1891 ldlm_lockname[lock->l_req_mode],
1892 lock->l_flags, lock->l_remote_handle.cookie,
1894 atomic_read(&lock->l_export->exp_refcount) : -99,
1900 switch (lock->l_resource->lr_type) {
1902 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1903 data->msg_fn, data->msg_line, fmt, args,
1904 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1905 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1906 "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
1907 " expref: %d pid: %u\n",
1908 lock->l_resource->lr_namespace->ns_name, lock,
1909 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1910 lock->l_readers, lock->l_writers,
1911 ldlm_lockname[lock->l_granted_mode],
1912 ldlm_lockname[lock->l_req_mode],
1913 lock->l_resource->lr_name.name[0],
1914 lock->l_resource->lr_name.name[1],
1915 atomic_read(&lock->l_resource->lr_refcount),
1916 ldlm_typename[lock->l_resource->lr_type],
1917 lock->l_policy_data.l_extent.start,
1918 lock->l_policy_data.l_extent.end,
1919 lock->l_req_extent.start, lock->l_req_extent.end,
1920 lock->l_flags, lock->l_remote_handle.cookie,
1922 atomic_read(&lock->l_export->exp_refcount) : -99,
1927 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1928 data->msg_fn, data->msg_line, fmt, args,
1929 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1930 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1931 "["LPU64"->"LPU64"] flags: %x remote: "LPX64
1932 " expref: %d pid: %u\n",
1933 lock->l_resource->lr_namespace->ns_name, lock,
1934 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1935 lock->l_readers, lock->l_writers,
1936 ldlm_lockname[lock->l_granted_mode],
1937 ldlm_lockname[lock->l_req_mode],
1938 lock->l_resource->lr_name.name[0],
1939 lock->l_resource->lr_name.name[1],
1940 atomic_read(&lock->l_resource->lr_refcount),
1941 ldlm_typename[lock->l_resource->lr_type],
1942 lock->l_policy_data.l_flock.pid,
1943 lock->l_policy_data.l_flock.start,
1944 lock->l_policy_data.l_flock.end,
1945 lock->l_flags, lock->l_remote_handle.cookie,
1947 atomic_read(&lock->l_export->exp_refcount) : -99,
1952 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1953 data->msg_fn, data->msg_line, fmt, args,
1954 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1955 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1956 "flags: %x remote: "LPX64" expref: %d "
1958 lock->l_resource->lr_namespace->ns_name,
1959 lock, lock->l_handle.h_cookie,
1960 atomic_read (&lock->l_refc),
1961 lock->l_readers, lock->l_writers,
1962 ldlm_lockname[lock->l_granted_mode],
1963 ldlm_lockname[lock->l_req_mode],
1964 lock->l_resource->lr_name.name[0],
1965 lock->l_resource->lr_name.name[1],
1966 lock->l_policy_data.l_inodebits.bits,
1967 atomic_read(&lock->l_resource->lr_refcount),
1968 ldlm_typename[lock->l_resource->lr_type],
1969 lock->l_flags, lock->l_remote_handle.cookie,
1971 atomic_read(&lock->l_export->exp_refcount) : -99,
1976 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1977 data->msg_fn, data->msg_line, fmt, args,
1978 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1979 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
1980 "remote: "LPX64" expref: %d pid: %u\n",
1981 lock->l_resource->lr_namespace->ns_name,
1982 lock, lock->l_handle.h_cookie,
1983 atomic_read (&lock->l_refc),
1984 lock->l_readers, lock->l_writers,
1985 ldlm_lockname[lock->l_granted_mode],
1986 ldlm_lockname[lock->l_req_mode],
1987 lock->l_resource->lr_name.name[0],
1988 lock->l_resource->lr_name.name[1],
1989 atomic_read(&lock->l_resource->lr_refcount),
1990 ldlm_typename[lock->l_resource->lr_type],
1991 lock->l_flags, lock->l_remote_handle.cookie,
1993 atomic_read(&lock->l_export->exp_refcount) : -99,
1999 EXPORT_SYMBOL(_ldlm_lock_debug);