1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lock.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_LDLM
45 # include <libcfs/libcfs.h>
46 # ifndef HAVE_VFS_INTENT_PATCHES
47 # include <linux/lustre_intent.h>
50 # include <liblustre.h>
53 #include <obd_class.h>
54 #include "ldlm_internal.h"
57 char *ldlm_lockname[] = {
68 char *ldlm_typename[] = {
75 char *ldlm_it2str(int it)
82 case (IT_OPEN | IT_CREAT):
95 CERROR("Unknown intent %d\n", it);
100 extern cfs_mem_cache_t *ldlm_lock_slab;
102 static ldlm_processing_policy ldlm_processing_policy_table[] = {
103 [LDLM_PLAIN] ldlm_process_plain_lock,
104 [LDLM_EXTENT] ldlm_process_extent_lock,
106 [LDLM_FLOCK] ldlm_process_flock_lock,
108 [LDLM_IBITS] ldlm_process_inodebits_lock,
111 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
113 return ldlm_processing_policy_table[res->lr_type];
116 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
122 * REFCOUNTED LOCK OBJECTS
127 * Lock refcounts, during creation:
128 * - one special one for allocation, dec'd only once in destroy
129 * - one for being a lock that's in-use
130 * - one for the addref associated with a new lock
132 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
134 atomic_inc(&lock->l_refc);
138 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
140 LASSERT(size == sizeof(*lock));
141 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
144 void ldlm_lock_put(struct ldlm_lock *lock)
148 LASSERT(lock->l_resource != LP_POISON);
149 LASSERT(atomic_read(&lock->l_refc) > 0);
150 if (atomic_dec_and_test(&lock->l_refc)) {
151 struct ldlm_resource *res;
154 "final lock_put on destroyed lock, freeing it.");
156 res = lock->l_resource;
157 LASSERT(lock->l_destroyed);
158 LASSERT(list_empty(&lock->l_res_link));
159 LASSERT(list_empty(&lock->l_pending_chain));
161 atomic_dec(&res->lr_namespace->ns_locks);
162 lu_ref_del(&res->lr_reference, "lock", lock);
163 ldlm_resource_putref(res);
164 lock->l_resource = NULL;
165 if (lock->l_export) {
166 class_export_put(lock->l_export);
167 lock->l_export = NULL;
170 if (lock->l_lvb_data != NULL)
171 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
173 ldlm_interval_free(ldlm_interval_detach(lock));
174 lu_ref_fini(&lock->l_reference);
175 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
182 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
185 if (!list_empty(&lock->l_lru)) {
186 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
187 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
188 list_del_init(&lock->l_lru);
190 LASSERT(ns->ns_nr_unused >= 0);
196 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
198 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
201 spin_lock(&ns->ns_unused_lock);
202 rc = ldlm_lock_remove_from_lru_nolock(lock);
203 spin_unlock(&ns->ns_unused_lock);
208 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
210 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
211 lock->l_last_used = cfs_time_current();
212 LASSERT(list_empty(&lock->l_lru));
213 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
214 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
215 LASSERT(ns->ns_nr_unused >= 0);
219 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
221 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
223 spin_lock(&ns->ns_unused_lock);
224 ldlm_lock_add_to_lru_nolock(lock);
225 spin_unlock(&ns->ns_unused_lock);
229 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
231 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
233 spin_lock(&ns->ns_unused_lock);
234 if (!list_empty(&lock->l_lru)) {
235 ldlm_lock_remove_from_lru_nolock(lock);
236 ldlm_lock_add_to_lru_nolock(lock);
238 spin_unlock(&ns->ns_unused_lock);
242 /* This used to have a 'strict' flag, which recovery would use to mark an
243 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
244 * shall explain why it's gone: with the new hash table scheme, once you call
245 * ldlm_lock_destroy, you can never drop your final references on this lock.
246 * Because it's not in the hash table anymore. -phil */
247 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
251 if (lock->l_readers || lock->l_writers) {
252 LDLM_ERROR(lock, "lock still has references");
253 ldlm_lock_dump(D_ERROR, lock, 0);
257 if (!list_empty(&lock->l_res_link)) {
258 LDLM_ERROR(lock, "lock still on resource");
259 ldlm_lock_dump(D_ERROR, lock, 0);
263 if (lock->l_destroyed) {
264 LASSERT(list_empty(&lock->l_lru));
268 lock->l_destroyed = 1;
270 if (lock->l_export && lock->l_export->exp_lock_hash)
271 lustre_hash_del(lock->l_export->exp_lock_hash,
272 &lock->l_remote_handle, &lock->l_exp_hash);
274 ldlm_lock_remove_from_lru(lock);
275 class_handle_unhash(&lock->l_handle);
278 /* Wake anyone waiting for this lock */
279 /* FIXME: I should probably add yet another flag, instead of using
280 * l_export to only call this on clients */
282 class_export_put(lock->l_export);
283 lock->l_export = NULL;
284 if (lock->l_export && lock->l_completion_ast)
285 lock->l_completion_ast(lock, 0);
291 void ldlm_lock_destroy(struct ldlm_lock *lock)
295 lock_res_and_lock(lock);
296 first = ldlm_lock_destroy_internal(lock);
297 unlock_res_and_lock(lock);
299 /* drop reference from hashtable only for first destroy */
301 lu_ref_del(&lock->l_reference, "hash", lock);
302 LDLM_LOCK_RELEASE(lock);
307 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
311 first = ldlm_lock_destroy_internal(lock);
312 /* drop reference from hashtable only for first destroy */
314 lu_ref_del(&lock->l_reference, "hash", lock);
315 LDLM_LOCK_RELEASE(lock);
320 /* this is called by portals_handle2object with the handle lock taken */
321 static void lock_handle_addref(void *lock)
323 LDLM_LOCK_GET((struct ldlm_lock *)lock);
327 * usage: pass in a resource on which you have done ldlm_resource_get
328 * pass in a parent lock on which you have done a ldlm_lock_get
329 * after return, ldlm_*_put the resource and parent
330 * returns: lock with refcount 2 - one for current caller and one for remote
332 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
334 struct ldlm_lock *lock;
337 if (resource == NULL)
340 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
344 spin_lock_init(&lock->l_lock);
345 lock->l_resource = ldlm_resource_getref(resource);
346 lu_ref_add(&resource->lr_reference, "lock", lock);
348 atomic_set(&lock->l_refc, 2);
349 CFS_INIT_LIST_HEAD(&lock->l_res_link);
350 CFS_INIT_LIST_HEAD(&lock->l_lru);
351 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
352 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
353 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
354 CFS_INIT_LIST_HEAD(&lock->l_rk_ast);
355 cfs_waitq_init(&lock->l_waitq);
356 lock->l_blocking_lock = NULL;
357 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
358 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
359 CFS_INIT_HLIST_NODE(&lock->l_exp_hash);
361 atomic_inc(&resource->lr_namespace->ns_locks);
362 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
363 class_handle_hash(&lock->l_handle, lock_handle_addref);
365 CFS_INIT_LIST_HEAD(&lock->l_extents_list);
366 spin_lock_init(&lock->l_extents_list_lock);
367 CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
368 lu_ref_init(&lock->l_reference);
369 lu_ref_add(&lock->l_reference, "hash", lock);
374 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
375 const struct ldlm_res_id *new_resid)
377 struct ldlm_resource *oldres = lock->l_resource;
378 struct ldlm_resource *newres;
382 LASSERT(ns_is_client(ns));
384 lock_res_and_lock(lock);
385 if (memcmp(new_resid, &lock->l_resource->lr_name,
386 sizeof(lock->l_resource->lr_name)) == 0) {
388 unlock_res_and_lock(lock);
392 LASSERT(new_resid->name[0] != 0);
394 /* This function assumes that the lock isn't on any lists */
395 LASSERT(list_empty(&lock->l_res_link));
397 type = oldres->lr_type;
398 unlock_res_and_lock(lock);
400 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
401 lu_ref_add(&newres->lr_reference, "lock", lock);
405 * To flip the lock from the old to the new resource, lock, oldres and
406 * newres have to be locked. Resource spin-locks are nested within
407 * lock->l_lock, and are taken in the memory address order to avoid
410 spin_lock(&lock->l_lock);
411 oldres = lock->l_resource;
412 if (oldres < newres) {
414 lock_res_nested(newres, LRT_NEW);
417 lock_res_nested(oldres, LRT_NEW);
419 LASSERT(memcmp(new_resid, &oldres->lr_name,
420 sizeof oldres->lr_name) != 0);
421 lock->l_resource = newres;
423 unlock_res_and_lock(lock);
425 /* ...and the flowers are still standing! */
426 lu_ref_del(&oldres->lr_reference, "lock", lock);
427 ldlm_resource_putref(oldres);
436 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
438 lockh->cookie = lock->l_handle.h_cookie;
441 /* if flags: atomically get the lock and set the flags.
442 * Return NULL if flag already set
445 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
448 struct ldlm_namespace *ns;
449 struct ldlm_lock *lock, *retval = NULL;
454 lock = class_handle2object(handle->cookie);
458 LASSERT(lock->l_resource != NULL);
459 ns = lock->l_resource->lr_namespace;
462 lu_ref_add_atomic(&lock->l_reference, "handle", cfs_current());
463 lock_res_and_lock(lock);
465 /* It's unlikely but possible that someone marked the lock as
466 * destroyed after we did handle2object on it */
467 if (lock->l_destroyed) {
468 unlock_res_and_lock(lock);
469 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
474 if (flags && (lock->l_flags & flags)) {
475 unlock_res_and_lock(lock);
481 lock->l_flags |= flags;
483 unlock_res_and_lock(lock);
490 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
492 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
493 /* INODEBITS_INTEROP: If the other side does not support
494 * inodebits, reply with a plain lock descriptor.
496 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
497 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
498 struct ldlm_resource res = *lock->l_resource;
500 /* Make sure all the right bits are set in this lock we
501 are going to pass to client */
502 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
503 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
504 "Inappropriate inode lock bits during "
505 "conversion " LPU64 "\n",
506 lock->l_policy_data.l_inodebits.bits);
507 res.lr_type = LDLM_PLAIN;
508 ldlm_res2desc(&res, &desc->l_resource);
509 /* Convert "new" lock mode to something old client can
511 if ((lock->l_req_mode == LCK_CR) ||
512 (lock->l_req_mode == LCK_CW))
513 desc->l_req_mode = LCK_PR;
515 desc->l_req_mode = lock->l_req_mode;
516 if ((lock->l_granted_mode == LCK_CR) ||
517 (lock->l_granted_mode == LCK_CW)) {
518 desc->l_granted_mode = LCK_PR;
520 /* We never grant PW/EX locks to clients */
521 LASSERT((lock->l_granted_mode != LCK_PW) &&
522 (lock->l_granted_mode != LCK_EX));
523 desc->l_granted_mode = lock->l_granted_mode;
526 /* We do not copy policy here, because there is no
527 policy for plain locks */
529 ldlm_res2desc(lock->l_resource, &desc->l_resource);
530 desc->l_req_mode = lock->l_req_mode;
531 desc->l_granted_mode = lock->l_granted_mode;
532 desc->l_policy_data = lock->l_policy_data;
536 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
537 struct list_head *work_list)
539 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
540 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
541 lock->l_flags |= LDLM_FL_AST_SENT;
542 /* If the enqueuing client said so, tell the AST recipient to
543 * discard dirty data, rather than writing back. */
544 if (new->l_flags & LDLM_AST_DISCARD_DATA)
545 lock->l_flags |= LDLM_FL_DISCARD_DATA;
546 LASSERT(list_empty(&lock->l_bl_ast));
547 list_add(&lock->l_bl_ast, work_list);
549 LASSERT(lock->l_blocking_lock == NULL);
550 lock->l_blocking_lock = LDLM_LOCK_GET(new);
554 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
556 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
557 lock->l_flags |= LDLM_FL_CP_REQD;
558 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
559 LASSERT(list_empty(&lock->l_cp_ast));
560 list_add(&lock->l_cp_ast, work_list);
565 /* must be called with lr_lock held */
566 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
567 struct list_head *work_list)
570 check_res_locked(lock->l_resource);
572 ldlm_add_bl_work_item(lock, new, work_list);
574 ldlm_add_cp_work_item(lock, work_list);
578 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
580 struct ldlm_lock *lock;
582 lock = ldlm_handle2lock(lockh);
583 LASSERT(lock != NULL);
584 ldlm_lock_addref_internal(lock, mode);
588 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
590 ldlm_lock_remove_from_lru(lock);
591 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
593 lu_ref_add_atomic(&lock->l_reference, "reader", lock);
595 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
597 lu_ref_add_atomic(&lock->l_reference, "writer", lock);
600 lu_ref_add_atomic(&lock->l_reference, "user", lock);
601 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
604 /* only called for local locks */
605 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
607 lock_res_and_lock(lock);
608 ldlm_lock_addref_internal_nolock(lock, mode);
609 unlock_res_and_lock(lock);
612 /* only called in ldlm_flock_destroy and for local locks.
613 * * for LDLM_FLOCK type locks, l_blocking_ast is null, and
614 * * ldlm_lock_remove_from_lru() does nothing, it is safe
615 * * for ldlm_flock_destroy usage by dropping some code */
616 void ldlm_lock_decref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
618 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
619 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
620 LASSERT(lock->l_readers > 0);
621 lu_ref_del(&lock->l_reference, "reader", lock);
624 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
625 LASSERT(lock->l_writers > 0);
626 lu_ref_del(&lock->l_reference, "writer", lock);
630 lu_ref_del(&lock->l_reference, "user", lock);
631 LDLM_LOCK_RELEASE(lock); /* matches the LDLM_LOCK_GET() in addref */
634 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
636 struct ldlm_namespace *ns;
639 lock_res_and_lock(lock);
641 ns = lock->l_resource->lr_namespace;
643 ldlm_lock_decref_internal_nolock(lock, mode);
645 if (lock->l_flags & LDLM_FL_LOCAL &&
646 !lock->l_readers && !lock->l_writers) {
647 /* If this is a local lock on a server namespace and this was
648 * the last reference, cancel the lock. */
649 CDEBUG(D_INFO, "forcing cancel of local lock\n");
650 lock->l_flags |= LDLM_FL_CBPENDING;
653 if (!lock->l_readers && !lock->l_writers &&
654 (lock->l_flags & LDLM_FL_CBPENDING)) {
655 /* If we received a blocked AST and this was the last reference,
656 * run the callback. */
657 if (ns_is_server(ns) && lock->l_export)
658 CERROR("FL_CBPENDING set on non-local lock--just a "
661 LDLM_DEBUG(lock, "final decref done on cbpending lock");
663 LDLM_LOCK_GET(lock); /* dropped by bl thread */
664 ldlm_lock_remove_from_lru(lock);
665 unlock_res_and_lock(lock);
666 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
667 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
668 ldlm_handle_bl_callback(ns, NULL, lock);
669 } else if (ns_is_client(ns) &&
670 !lock->l_readers && !lock->l_writers &&
671 !(lock->l_flags & LDLM_FL_BL_AST)) {
672 /* If this is a client-side namespace and this was the last
673 * reference, put it on the LRU. */
674 ldlm_lock_add_to_lru(lock);
675 unlock_res_and_lock(lock);
676 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
677 * are not supported by the server, otherwise, it is done on
679 if (!exp_connect_cancelset(lock->l_conn_export) &&
680 !ns_connect_lru_resize(ns))
681 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
683 unlock_res_and_lock(lock);
689 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
691 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
692 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
693 ldlm_lock_decref_internal(lock, mode);
697 /* This will drop a lock reference and mark it for destruction, but will not
698 * necessarily cancel the lock before returning. */
699 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
701 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
704 LASSERT(lock != NULL);
706 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
707 lock_res_and_lock(lock);
708 lock->l_flags |= LDLM_FL_CBPENDING;
709 unlock_res_and_lock(lock);
710 ldlm_lock_decref_internal(lock, mode);
714 struct sl_insert_point {
715 struct list_head *res_link;
716 struct list_head *mode_link;
717 struct list_head *policy_link;
721 * search_granted_lock
724 * Finds a position to insert the new lock.
726 * queue [input]: the granted list where search acts on;
727 * req [input]: the lock whose position to be located;
728 * prev [output]: positions within 3 lists to insert @req to
732 * - ldlm_grant_lock_with_skiplist
734 static void search_granted_lock(struct list_head *queue,
735 struct ldlm_lock *req,
736 struct sl_insert_point *prev)
738 struct list_head *tmp;
739 struct ldlm_lock *lock, *mode_end, *policy_end;
742 list_for_each(tmp, queue) {
743 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
745 mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
748 if (lock->l_req_mode != req->l_req_mode) {
749 /* jump to last lock of mode group */
750 tmp = &mode_end->l_res_link;
754 /* suitable mode group is found */
755 if (lock->l_resource->lr_type == LDLM_PLAIN) {
756 /* insert point is last lock of the mode group */
757 prev->res_link = &mode_end->l_res_link;
758 prev->mode_link = &mode_end->l_sl_mode;
759 prev->policy_link = &req->l_sl_policy;
762 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
764 policy_end = list_entry(lock->l_sl_policy.prev,
768 if (lock->l_policy_data.l_inodebits.bits ==
769 req->l_policy_data.l_inodebits.bits) {
770 /* insert point is last lock of
771 * the policy group */
773 &policy_end->l_res_link;
775 &policy_end->l_sl_mode;
777 &policy_end->l_sl_policy;
782 if (policy_end == mode_end)
783 /* done with mode group */
786 /* jump to next policy group within the mode group */
787 tmp = policy_end->l_res_link.next;
788 lock = list_entry(tmp, struct ldlm_lock,
790 } /* loop over policy groups within the mode group */
792 /* insert point is last lock of the mode group,
793 * new policy group is started */
794 prev->res_link = &mode_end->l_res_link;
795 prev->mode_link = &mode_end->l_sl_mode;
796 prev->policy_link = &req->l_sl_policy;
800 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
805 /* insert point is last lock on the queue,
806 * new mode group and new policy group are started */
807 prev->res_link = queue->prev;
808 prev->mode_link = &req->l_sl_mode;
809 prev->policy_link = &req->l_sl_policy;
814 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
815 struct sl_insert_point *prev)
817 struct ldlm_resource *res = lock->l_resource;
820 check_res_locked(res);
822 ldlm_resource_dump(D_INFO, res);
823 CDEBUG(D_OTHER, "About to add this lock:\n");
824 ldlm_lock_dump(D_OTHER, lock, 0);
826 if (lock->l_destroyed) {
827 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
831 LASSERT(list_empty(&lock->l_res_link));
832 LASSERT(list_empty(&lock->l_sl_mode));
833 LASSERT(list_empty(&lock->l_sl_policy));
835 list_add(&lock->l_res_link, prev->res_link);
836 list_add(&lock->l_sl_mode, prev->mode_link);
837 list_add(&lock->l_sl_policy, prev->policy_link);
842 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
844 struct sl_insert_point prev;
847 LASSERT(lock->l_req_mode == lock->l_granted_mode);
849 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
850 ldlm_granted_list_add_lock(lock, &prev);
855 * - ldlm_lock_enqueue
856 * - ldlm_reprocess_queue
857 * - ldlm_lock_convert
859 * must be called with lr_lock held
861 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
863 struct ldlm_resource *res = lock->l_resource;
866 check_res_locked(res);
868 lock->l_granted_mode = lock->l_req_mode;
869 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
870 ldlm_grant_lock_with_skiplist(lock);
871 else if (res->lr_type == LDLM_EXTENT)
872 ldlm_extent_add_lock(res, lock);
874 ldlm_resource_add_lock(res, &res->lr_granted, lock);
876 if (lock->l_granted_mode < res->lr_most_restr)
877 res->lr_most_restr = lock->l_granted_mode;
879 if (work_list && lock->l_completion_ast != NULL)
880 ldlm_add_ast_work_item(lock, NULL, work_list);
882 ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
886 /* returns a referenced lock or NULL. See the flag descriptions below, in the
887 * comment above ldlm_lock_match */
888 static struct ldlm_lock *search_queue(struct list_head *queue,
890 ldlm_policy_data_t *policy,
891 struct ldlm_lock *old_lock, int flags)
893 struct ldlm_lock *lock;
894 struct list_head *tmp;
896 list_for_each(tmp, queue) {
899 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
901 if (lock == old_lock)
904 /* llite sometimes wants to match locks that will be
905 * canceled when their users drop, but we allow it to match
906 * if it passes in CBPENDING and the lock still has users.
907 * this is generally only going to be used by children
908 * whose parents already hold a lock so forward progress
909 * can still happen. */
910 if (lock->l_flags & LDLM_FL_CBPENDING &&
911 !(flags & LDLM_FL_CBPENDING))
913 if (lock->l_flags & LDLM_FL_CBPENDING &&
914 lock->l_readers == 0 && lock->l_writers == 0)
917 if (!(lock->l_req_mode & *mode))
919 match = lock->l_req_mode;
921 if (lock->l_resource->lr_type == LDLM_EXTENT &&
922 (lock->l_policy_data.l_extent.start >
923 policy->l_extent.start ||
924 lock->l_policy_data.l_extent.end < policy->l_extent.end))
927 if (unlikely(match == LCK_GROUP) &&
928 lock->l_resource->lr_type == LDLM_EXTENT &&
929 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
932 /* We match if we have existing lock with same or wider set
934 if (lock->l_resource->lr_type == LDLM_IBITS &&
935 ((lock->l_policy_data.l_inodebits.bits &
936 policy->l_inodebits.bits) !=
937 policy->l_inodebits.bits))
940 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
943 if ((flags & LDLM_FL_LOCAL_ONLY) &&
944 !(lock->l_flags & LDLM_FL_LOCAL))
947 if (flags & LDLM_FL_TEST_LOCK) {
949 ldlm_lock_touch_in_lru(lock);
951 ldlm_lock_addref_internal_nolock(lock, match);
960 void ldlm_lock_allow_match(struct ldlm_lock *lock)
962 lock_res_and_lock(lock);
963 lock->l_flags |= LDLM_FL_LVB_READY;
964 cfs_waitq_signal(&lock->l_waitq);
965 unlock_res_and_lock(lock);
969 * Checks if requested extent lock is compatible with another owned lock.
971 * Checks if \a lock is compatible with a read or write lock
972 * (specified by \a rw) for an extent [\a start , \a end].
974 * \param lock the already owned lock
975 * \param rw OBD_BRW_READ if requested for reading,
976 * OBD_BRW_WRITE if requested for writing
977 * \param start start of the requested extent
978 * \param end end of the requested extent
979 * \param cookie transparent parameter for passing locking context
981 * \post result == 1, *cookie == context, appropriate lock is referenced
983 * \retval 1 owned lock is reused for the request
984 * \retval 0 no lock reused for the request
986 * \see ldlm_lock_fast_release
988 int ldlm_lock_fast_match(struct ldlm_lock *lock, int rw,
989 obd_off start, obd_off end,
992 LASSERT(rw == OBD_BRW_READ || rw == OBD_BRW_WRITE);
997 lock_res_and_lock(lock);
998 /* check if granted mode is compatible */
999 if (rw == OBD_BRW_WRITE &&
1000 !(lock->l_granted_mode & (LCK_PW|LCK_GROUP)))
1003 /* does the lock cover the region we would like to access? */
1004 if ((lock->l_policy_data.l_extent.start > start) ||
1005 (lock->l_policy_data.l_extent.end < end))
1008 /* if we received a blocking callback and the lock is no longer
1009 * referenced, don't use it */
1010 if ((lock->l_flags & LDLM_FL_CBPENDING) &&
1011 !lock->l_writers && !lock->l_readers)
1014 ldlm_lock_addref_internal_nolock(lock, rw == OBD_BRW_WRITE ? LCK_PW : LCK_PR);
1015 unlock_res_and_lock(lock);
1016 *cookie = (void *)lock;
1017 return 1; /* avoid using rc for stack relief */
1020 unlock_res_and_lock(lock);
1025 * Releases a reference to a lock taken in a "fast" way.
1027 * Releases a read or write (specified by \a rw) lock
1028 * referenced by \a cookie.
1030 * \param rw OBD_BRW_READ if requested for reading,
1031 * OBD_BRW_WRITE if requested for writing
1032 * \param cookie transparent parameter for passing locking context
1034 * \post appropriate lock is dereferenced
1036 * \see ldlm_lock_fast_lock
1038 void ldlm_lock_fast_release(void *cookie, int rw)
1040 struct ldlm_lock *lock = (struct ldlm_lock *)cookie;
1042 LASSERT(lock != NULL);
1043 LASSERT(rw == OBD_BRW_READ || rw == OBD_BRW_WRITE);
1044 LASSERT(rw == OBD_BRW_READ ||
1045 (lock->l_granted_mode & (LCK_PW | LCK_GROUP)));
1046 ldlm_lock_decref_internal(lock, rw == OBD_BRW_WRITE ? LCK_PW : LCK_PR);
1049 /* Can be called in two ways:
1051 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1052 * for a duplicate of.
1054 * Otherwise, all of the fields must be filled in, to match against.
1056 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1057 * server (ie, connh is NULL)
1058 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1059 * list will be considered
1060 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1061 * to be canceled can still be matched as long as they still have reader
1062 * or writer refernces
1063 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1064 * just tell us if we would have matched.
1066 * Returns 1 if it finds an already-existing lock that is compatible; in this
1067 * case, lockh is filled in with a addref()ed lock
1069 * we also check security context, if that failed we simply return 0 (to keep
1070 * caller code unchanged), the context failure will be discovered by caller
1073 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1074 const struct ldlm_res_id *res_id, ldlm_type_t type,
1075 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1076 struct lustre_handle *lockh)
1078 struct ldlm_resource *res;
1079 struct ldlm_lock *lock, *old_lock = NULL;
1084 old_lock = ldlm_handle2lock(lockh);
1087 ns = old_lock->l_resource->lr_namespace;
1088 res_id = &old_lock->l_resource->lr_name;
1089 type = old_lock->l_resource->lr_type;
1090 mode = old_lock->l_req_mode;
1093 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1095 LASSERT(old_lock == NULL);
1099 LDLM_RESOURCE_ADDREF(res);
1102 lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
1105 if (flags & LDLM_FL_BLOCK_GRANTED)
1107 lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
1110 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
1117 LDLM_RESOURCE_DELREF(res);
1118 ldlm_resource_putref(res);
1121 ldlm_lock2handle(lock, lockh);
1122 if ((flags & LDLM_FL_LVB_READY) &&
1123 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1124 struct l_wait_info lwi;
1125 if (lock->l_completion_ast) {
1126 int err = lock->l_completion_ast(lock,
1127 LDLM_FL_WAIT_NOREPROC,
1130 if (flags & LDLM_FL_TEST_LOCK)
1131 LDLM_LOCK_RELEASE(lock);
1133 ldlm_lock_decref_internal(lock, mode);
1139 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout), NULL,
1140 LWI_ON_SIGNAL_NOOP, NULL);
1142 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1143 l_wait_event(lock->l_waitq,
1144 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1149 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1150 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1151 res_id->name[2] : policy->l_extent.start,
1152 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1153 res_id->name[3] : policy->l_extent.end);
1155 /* check user's security context */
1156 if (lock->l_conn_export &&
1157 sptlrpc_import_check_ctx(
1158 class_exp2cliimp(lock->l_conn_export))) {
1159 if (!(flags & LDLM_FL_TEST_LOCK))
1160 ldlm_lock_decref_internal(lock, mode);
1164 if (flags & LDLM_FL_TEST_LOCK)
1165 LDLM_LOCK_RELEASE(lock);
1167 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1168 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1169 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1170 type, mode, res_id->name[0], res_id->name[1],
1171 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1172 res_id->name[2] :policy->l_extent.start,
1173 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1174 res_id->name[3] : policy->l_extent.end);
1177 LDLM_LOCK_PUT(old_lock);
1179 return rc ? mode : 0;
1182 /* Returns a referenced lock */
1183 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1184 const struct ldlm_res_id *res_id,
1187 const struct ldlm_callback_suite *cbs,
1188 void *data, __u32 lvb_len)
1190 struct ldlm_lock *lock;
1191 struct ldlm_resource *res;
1194 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1198 lock = ldlm_lock_new(res);
1199 ldlm_resource_putref(res);
1204 lock->l_req_mode = mode;
1205 lock->l_ast_data = data;
1206 lock->l_pid = cfs_curproc_pid();
1208 lock->l_blocking_ast = cbs->lcs_blocking;
1209 lock->l_completion_ast = cbs->lcs_completion;
1210 lock->l_glimpse_ast = cbs->lcs_glimpse;
1211 lock->l_weigh_ast = cbs->lcs_weigh;
1214 lock->l_tree_node = NULL;
1215 /* if this is the extent lock, allocate the interval tree node */
1216 if (type == LDLM_EXTENT) {
1217 if (ldlm_interval_alloc(lock) == NULL)
1222 lock->l_lvb_len = lvb_len;
1223 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1224 if (lock->l_lvb_data == NULL)
1231 if (lock->l_lvb_data)
1232 OBD_FREE(lock->l_lvb_data, lvb_len);
1233 ldlm_interval_free(ldlm_interval_detach(lock));
1234 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1238 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1239 struct ldlm_lock **lockp,
1240 void *cookie, int *flags)
1242 struct ldlm_lock *lock = *lockp;
1243 struct ldlm_resource *res = lock->l_resource;
1244 int local = ns_is_client(res->lr_namespace);
1245 ldlm_processing_policy policy;
1246 ldlm_error_t rc = ELDLM_OK;
1247 struct ldlm_interval *node = NULL;
1250 do_gettimeofday(&lock->l_enqueued_time);
1251 /* policies are not executed on the client or during replay */
1252 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1253 && !local && ns->ns_policy) {
1254 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1256 if (rc == ELDLM_LOCK_REPLACED) {
1257 /* The lock that was returned has already been granted,
1258 * and placed into lockp. If it's not the same as the
1259 * one we passed in, then destroy the old one and our
1260 * work here is done. */
1261 if (lock != *lockp) {
1262 ldlm_lock_destroy(lock);
1263 LDLM_LOCK_RELEASE(lock);
1265 *flags |= LDLM_FL_LOCK_CHANGED;
1267 } else if (rc != ELDLM_OK ||
1268 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1269 ldlm_lock_destroy(lock);
1274 /* For a replaying lock, it might be already in granted list. So
1275 * unlinking the lock will cause the interval node to be freed, we
1276 * have to allocate the interval node early otherwise we can't regrant
1277 * this lock in the future. - jay */
1278 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1279 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO,
1282 lock_res_and_lock(lock);
1283 if (local && lock->l_req_mode == lock->l_granted_mode) {
1284 /* The server returned a blocked lock, but it was granted
1285 * before we got a chance to actually enqueue it. We don't
1286 * need to do anything else. */
1287 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1288 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1289 GOTO(out, ELDLM_OK);
1292 ldlm_resource_unlink_lock(lock);
1293 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1295 ldlm_lock_destroy_nolock(lock);
1296 GOTO(out, rc = -ENOMEM);
1299 CFS_INIT_LIST_HEAD(&node->li_group);
1300 ldlm_interval_attach(node, lock);
1304 /* Some flags from the enqueue want to make it into the AST, via the
1305 * lock's l_flags. */
1306 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1308 /* This distinction between local lock trees is very important; a client
1309 * namespace only has information about locks taken by that client, and
1310 * thus doesn't have enough information to decide for itself if it can
1311 * be granted (below). In this case, we do exactly what the server
1312 * tells us to do, as dictated by the 'flags'.
1314 * We do exactly the same thing during recovery, when the server is
1315 * more or less trusting the clients not to lie.
1317 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1318 * granted/converting queues. */
1320 if (*flags & LDLM_FL_BLOCK_CONV)
1321 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1322 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1323 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1325 ldlm_grant_lock(lock, NULL);
1326 GOTO(out, ELDLM_OK);
1327 } else if (*flags & LDLM_FL_REPLAY) {
1328 if (*flags & LDLM_FL_BLOCK_CONV) {
1329 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1330 GOTO(out, ELDLM_OK);
1331 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1332 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1333 GOTO(out, ELDLM_OK);
1334 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1335 ldlm_grant_lock(lock, NULL);
1336 GOTO(out, ELDLM_OK);
1338 /* If no flags, fall through to normal enqueue path. */
1341 policy = ldlm_processing_policy_table[res->lr_type];
1342 policy(lock, flags, 1, &rc, NULL);
1345 unlock_res_and_lock(lock);
1347 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1351 /* Must be called with namespace taken: queue is waiting or converting. */
1352 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1353 struct list_head *work_list)
1355 struct list_head *tmp, *pos;
1356 ldlm_processing_policy policy;
1358 int rc = LDLM_ITER_CONTINUE;
1362 check_res_locked(res);
1364 policy = ldlm_processing_policy_table[res->lr_type];
1367 list_for_each_safe(tmp, pos, queue) {
1368 struct ldlm_lock *pending;
1369 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1371 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1374 rc = policy(pending, &flags, 0, &err, work_list);
1375 if (rc != LDLM_ITER_CONTINUE)
1382 /* Helper function for ldlm_run_ast_work().
1384 * Send an existing rpc set specified by @arg->set and then
1385 * destroy it. Create new one if @do_create flag is set. */
1387 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1391 ptlrpc_set_wait(arg->set);
1392 if (arg->type == LDLM_BL_CALLBACK)
1393 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1394 ptlrpc_set_destroy(arg->set);
1397 arg->set = ptlrpc_prep_set();
1403 ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1405 struct ldlm_lock_desc d;
1406 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
1409 /* nobody should touch l_bl_ast */
1410 lock_res_and_lock(lock);
1411 list_del_init(&lock->l_bl_ast);
1413 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1414 LASSERT(lock->l_bl_ast_run == 0);
1415 LASSERT(lock->l_blocking_lock);
1416 lock->l_bl_ast_run++;
1417 unlock_res_and_lock(lock);
1419 ldlm_lock2desc(lock->l_blocking_lock, &d);
1421 LDLM_LOCK_RELEASE(lock->l_blocking_lock);
1422 lock->l_blocking_lock = NULL;
1423 lock->l_blocking_ast(lock, &d, (void *)arg,
1425 LDLM_LOCK_RELEASE(lock);
1431 ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1433 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
1434 ldlm_completion_callback completion_callback;
1438 /* It's possible to receive a completion AST before we've set
1439 * the l_completion_ast pointer: either because the AST arrived
1440 * before the reply, or simply because there's a small race
1441 * window between receiving the reply and finishing the local
1442 * enqueue. (bug 842)
1444 * This can't happen with the blocking_ast, however, because we
1445 * will never call the local blocking_ast until we drop our
1446 * reader/writer reference, which we won't do until we get the
1447 * reply and finish enqueueing. */
1449 /* nobody should touch l_cp_ast */
1450 lock_res_and_lock(lock);
1451 list_del_init(&lock->l_cp_ast);
1452 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1453 /* save l_completion_ast since it can be changed by
1454 * mds_intent_policy(), see bug 14225 */
1455 completion_callback = lock->l_completion_ast;
1456 lock->l_flags &= ~LDLM_FL_CP_REQD;
1457 unlock_res_and_lock(lock);
1459 if (completion_callback != NULL) {
1460 completion_callback(lock, 0, (void *)arg);
1463 LDLM_LOCK_RELEASE(lock);
1469 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1471 struct ldlm_lock_desc desc;
1472 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_rk_ast);
1475 list_del_init(&lock->l_rk_ast);
1477 /* the desc just pretend to exclusive */
1478 ldlm_lock2desc(lock, &desc);
1479 desc.l_req_mode = LCK_EX;
1480 desc.l_granted_mode = 0;
1482 lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1483 LDLM_LOCK_RELEASE(lock);
1488 int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
1490 struct ldlm_cb_set_arg arg;
1491 struct list_head *tmp, *pos;
1492 int (*work_ast_lock)(struct list_head *tmp, struct ldlm_cb_set_arg *arg);
1496 arg.set = ptlrpc_prep_set();
1497 atomic_set(&arg.restart, 0);
1499 case LDLM_WORK_BL_AST:
1500 arg.type = LDLM_BL_CALLBACK;
1501 work_ast_lock = ldlm_work_bl_ast_lock;
1503 case LDLM_WORK_CP_AST:
1504 arg.type = LDLM_CP_CALLBACK;
1505 work_ast_lock = ldlm_work_cp_ast_lock;
1507 case LDLM_WORK_REVOKE_AST:
1508 arg.type = LDLM_BL_CALLBACK;
1509 work_ast_lock = ldlm_work_revoke_ast_lock;
1516 list_for_each_safe(tmp, pos, rpc_list) {
1517 ast_count += work_ast_lock(tmp, &arg);
1519 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1520 * and create a new set for requests that remained in
1522 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1523 ldlm_send_and_maybe_create_set(&arg, 1);
1529 ldlm_send_and_maybe_create_set(&arg, 0);
1531 /* In case when number of ASTs is multiply of
1532 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1533 * @arg.set must be destroyed here, otherwise we get
1534 * write memory leaking. */
1535 ptlrpc_set_destroy(arg.set);
1537 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1540 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1542 ldlm_reprocess_all(res);
1543 return LDLM_ITER_CONTINUE;
1546 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1548 struct list_head *tmp;
1555 spin_lock(&ns->ns_hash_lock);
1556 for (i = 0; i < RES_HASH_SIZE; i++) {
1557 tmp = ns->ns_hash[i].next;
1558 while (tmp != &(ns->ns_hash[i])) {
1559 struct ldlm_resource *res =
1560 list_entry(tmp, struct ldlm_resource, lr_hash);
1562 ldlm_resource_getref(res);
1563 spin_unlock(&ns->ns_hash_lock);
1564 LDLM_RESOURCE_ADDREF(res);
1566 rc = reprocess_one_queue(res, NULL);
1568 LDLM_RESOURCE_DELREF(res);
1569 spin_lock(&ns->ns_hash_lock);
1571 ldlm_resource_putref_locked(res);
1573 if (rc == LDLM_ITER_STOP)
1578 spin_unlock(&ns->ns_hash_lock);
1582 void ldlm_reprocess_all(struct ldlm_resource *res)
1584 CFS_LIST_HEAD(rpc_list);
1588 /* Local lock trees don't get reprocessed. */
1589 if (ns_is_client(res->lr_namespace)) {
1596 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1597 if (rc == LDLM_ITER_CONTINUE)
1598 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1601 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1602 if (rc == -ERESTART) {
1603 LASSERT(list_empty(&rpc_list));
1609 void ldlm_cancel_callback(struct ldlm_lock *lock)
1611 check_res_locked(lock->l_resource);
1612 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1613 lock->l_flags |= LDLM_FL_CANCEL;
1614 if (lock->l_blocking_ast) {
1615 // l_check_no_ns_lock(ns);
1616 unlock_res_and_lock(lock);
1617 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1619 lock_res_and_lock(lock);
1621 LDLM_DEBUG(lock, "no blocking ast");
1624 lock->l_flags |= LDLM_FL_BL_DONE;
1627 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1629 if (req->l_resource->lr_type != LDLM_PLAIN &&
1630 req->l_resource->lr_type != LDLM_IBITS)
1633 list_del_init(&req->l_sl_policy);
1634 list_del_init(&req->l_sl_mode);
1637 void ldlm_lock_cancel(struct ldlm_lock *lock)
1639 struct ldlm_resource *res;
1640 struct ldlm_namespace *ns;
1643 lock_res_and_lock(lock);
1645 res = lock->l_resource;
1646 ns = res->lr_namespace;
1648 /* Please do not, no matter how tempting, remove this LBUG without
1649 * talking to me first. -phik */
1650 if (lock->l_readers || lock->l_writers) {
1651 LDLM_ERROR(lock, "lock still has references");
1655 ldlm_del_waiting_lock(lock);
1657 /* Releases cancel callback. */
1658 ldlm_cancel_callback(lock);
1660 /* Yes, second time, just in case it was added again while we were
1661 running with no res lock in ldlm_cancel_callback */
1662 ldlm_del_waiting_lock(lock);
1663 ldlm_resource_unlink_lock(lock);
1664 ldlm_lock_destroy_nolock(lock);
1666 if (lock->l_granted_mode == lock->l_req_mode)
1667 ldlm_pool_del(&ns->ns_pool, lock);
1669 /* Make sure we will not be called again for same lock what is possible
1670 * if not to zero out lock->l_granted_mode */
1671 lock->l_granted_mode = LCK_MINMODE;
1672 unlock_res_and_lock(lock);
1677 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1679 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1685 lock->l_ast_data = data;
1686 LDLM_LOCK_PUT(lock);
1690 void ldlm_cancel_locks_for_export_cb(void *obj, void *data)
1692 struct obd_export *exp = data;
1693 struct ldlm_lock *lock = obj;
1694 struct ldlm_resource *res;
1696 res = ldlm_resource_getref(lock->l_resource);
1697 LDLM_LOCK_GET(lock);
1699 LDLM_DEBUG(lock, "export %p", exp);
1700 ldlm_res_lvbo_update(res, NULL, 0, 1);
1701 ldlm_lock_cancel(lock);
1702 ldlm_reprocess_all(res);
1703 ldlm_resource_putref(res);
1704 LDLM_LOCK_RELEASE(lock);
1707 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1709 lustre_hash_for_each_empty(exp->exp_lock_hash,
1710 ldlm_cancel_locks_for_export_cb, exp);
1713 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1716 CFS_LIST_HEAD(rpc_list);
1717 struct ldlm_resource *res;
1718 struct ldlm_namespace *ns;
1721 struct sl_insert_point prev;
1723 struct ldlm_interval *node;
1726 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1727 *flags |= LDLM_FL_BLOCK_GRANTED;
1728 RETURN(lock->l_resource);
1731 /* I can't check the type of lock here because the bitlock of lock
1732 * is not held here, so do the allocation blindly. -jay */
1733 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
1734 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1737 LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1738 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1740 lock_res_and_lock(lock);
1742 res = lock->l_resource;
1743 ns = res->lr_namespace;
1745 old_mode = lock->l_req_mode;
1746 lock->l_req_mode = new_mode;
1747 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1748 /* remember the lock position where the lock might be
1749 * added back to the granted list later and also
1750 * remember the join mode for skiplist fixing. */
1751 prev.res_link = lock->l_res_link.prev;
1752 prev.mode_link = lock->l_sl_mode.prev;
1753 prev.policy_link = lock->l_sl_policy.prev;
1754 ldlm_resource_unlink_lock(lock);
1756 ldlm_resource_unlink_lock(lock);
1757 if (res->lr_type == LDLM_EXTENT) {
1758 /* FIXME: ugly code, I have to attach the lock to a
1759 * interval node again since perhaps it will be granted
1761 CFS_INIT_LIST_HEAD(&node->li_group);
1762 ldlm_interval_attach(node, lock);
1767 /* If this is a local resource, put it on the appropriate list. */
1768 if (ns_is_client(res->lr_namespace)) {
1769 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1770 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1772 /* This should never happen, because of the way the
1773 * server handles conversions. */
1774 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1778 ldlm_grant_lock(lock, &rpc_list);
1780 /* FIXME: completion handling not with ns_lock held ! */
1781 if (lock->l_completion_ast)
1782 lock->l_completion_ast(lock, 0, NULL);
1786 ldlm_processing_policy policy;
1787 policy = ldlm_processing_policy_table[res->lr_type];
1788 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1789 if (rc == LDLM_ITER_STOP) {
1790 lock->l_req_mode = old_mode;
1791 if (res->lr_type == LDLM_EXTENT)
1792 ldlm_extent_add_lock(res, lock);
1794 ldlm_granted_list_add_lock(lock, &prev);
1798 *flags |= LDLM_FL_BLOCK_GRANTED;
1802 unlock_res_and_lock(lock);
1805 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1807 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1811 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1813 struct obd_device *obd = NULL;
1815 if (!((libcfs_debug | D_ERROR) & level))
1819 CDEBUG(level, " NULL LDLM lock\n");
1823 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1824 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1826 if (lock->l_conn_export != NULL)
1827 obd = lock->l_conn_export->exp_obd;
1828 if (lock->l_export && lock->l_export->exp_connection) {
1829 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1830 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1831 lock->l_remote_handle.cookie);
1832 } else if (obd == NULL) {
1833 CDEBUG(level, " Node: local\n");
1835 struct obd_import *imp = obd->u.cli.cl_import;
1836 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1837 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1838 lock->l_remote_handle.cookie);
1840 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1842 lock->l_resource->lr_name.name[0],
1843 lock->l_resource->lr_name.name[1],
1844 lock->l_resource->lr_name.name[2]);
1845 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1846 "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
1847 ldlm_lockname[lock->l_granted_mode],
1848 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1850 if (lock->l_resource->lr_type == LDLM_EXTENT)
1851 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1852 " (req "LPU64"-"LPU64")\n",
1853 lock->l_policy_data.l_extent.start,
1854 lock->l_policy_data.l_extent.end,
1855 lock->l_req_extent.start, lock->l_req_extent.end);
1856 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1857 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1858 lock->l_policy_data.l_flock.pid,
1859 lock->l_policy_data.l_flock.start,
1860 lock->l_policy_data.l_flock.end);
1861 else if (lock->l_resource->lr_type == LDLM_IBITS)
1862 CDEBUG(level, " Bits: "LPX64"\n",
1863 lock->l_policy_data.l_inodebits.bits);
1866 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1868 struct ldlm_lock *lock;
1870 if (!((libcfs_debug | D_ERROR) & level))
1873 lock = ldlm_handle2lock(lockh);
1877 ldlm_lock_dump(D_OTHER, lock, 0);
1879 LDLM_LOCK_PUT(lock);
1882 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1883 struct libcfs_debug_msg_data *data, const char *fmt,
1887 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1889 va_start(args, fmt);
1891 if (lock->l_resource == NULL) {
1892 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1893 data->msg_fn, data->msg_line, fmt, args,
1894 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1895 "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
1896 LPX64" expref: %d pid: %u\n", lock,
1897 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1898 lock->l_readers, lock->l_writers,
1899 ldlm_lockname[lock->l_granted_mode],
1900 ldlm_lockname[lock->l_req_mode],
1901 lock->l_flags, lock->l_remote_handle.cookie,
1903 atomic_read(&lock->l_export->exp_refcount) : -99,
1909 switch (lock->l_resource->lr_type) {
1911 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1912 data->msg_fn, data->msg_line, fmt, args,
1913 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1914 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1915 "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
1916 " expref: %d pid: %u\n",
1917 lock->l_resource->lr_namespace->ns_name, lock,
1918 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1919 lock->l_readers, lock->l_writers,
1920 ldlm_lockname[lock->l_granted_mode],
1921 ldlm_lockname[lock->l_req_mode],
1922 lock->l_resource->lr_name.name[0],
1923 lock->l_resource->lr_name.name[1],
1924 atomic_read(&lock->l_resource->lr_refcount),
1925 ldlm_typename[lock->l_resource->lr_type],
1926 lock->l_policy_data.l_extent.start,
1927 lock->l_policy_data.l_extent.end,
1928 lock->l_req_extent.start, lock->l_req_extent.end,
1929 lock->l_flags, lock->l_remote_handle.cookie,
1931 atomic_read(&lock->l_export->exp_refcount) : -99,
1936 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1937 data->msg_fn, data->msg_line, fmt, args,
1938 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1939 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1940 "["LPU64"->"LPU64"] flags: %x remote: "LPX64
1941 " expref: %d pid: %u\n",
1942 lock->l_resource->lr_namespace->ns_name, lock,
1943 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1944 lock->l_readers, lock->l_writers,
1945 ldlm_lockname[lock->l_granted_mode],
1946 ldlm_lockname[lock->l_req_mode],
1947 lock->l_resource->lr_name.name[0],
1948 lock->l_resource->lr_name.name[1],
1949 atomic_read(&lock->l_resource->lr_refcount),
1950 ldlm_typename[lock->l_resource->lr_type],
1951 lock->l_policy_data.l_flock.pid,
1952 lock->l_policy_data.l_flock.start,
1953 lock->l_policy_data.l_flock.end,
1954 lock->l_flags, lock->l_remote_handle.cookie,
1956 atomic_read(&lock->l_export->exp_refcount) : -99,
1961 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1962 data->msg_fn, data->msg_line, fmt, args,
1963 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1964 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1965 "flags: %x remote: "LPX64" expref: %d "
1967 lock->l_resource->lr_namespace->ns_name,
1968 lock, lock->l_handle.h_cookie,
1969 atomic_read (&lock->l_refc),
1970 lock->l_readers, lock->l_writers,
1971 ldlm_lockname[lock->l_granted_mode],
1972 ldlm_lockname[lock->l_req_mode],
1973 lock->l_resource->lr_name.name[0],
1974 lock->l_resource->lr_name.name[1],
1975 lock->l_policy_data.l_inodebits.bits,
1976 atomic_read(&lock->l_resource->lr_refcount),
1977 ldlm_typename[lock->l_resource->lr_type],
1978 lock->l_flags, lock->l_remote_handle.cookie,
1980 atomic_read(&lock->l_export->exp_refcount) : -99,
1985 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1986 data->msg_fn, data->msg_line, fmt, args,
1987 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1988 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
1989 "remote: "LPX64" expref: %d pid: %u\n",
1990 lock->l_resource->lr_namespace->ns_name,
1991 lock, lock->l_handle.h_cookie,
1992 atomic_read (&lock->l_refc),
1993 lock->l_readers, lock->l_writers,
1994 ldlm_lockname[lock->l_granted_mode],
1995 ldlm_lockname[lock->l_req_mode],
1996 lock->l_resource->lr_name.name[0],
1997 lock->l_resource->lr_name.name[1],
1998 atomic_read(&lock->l_resource->lr_refcount),
1999 ldlm_typename[lock->l_resource->lr_type],
2000 lock->l_flags, lock->l_remote_handle.cookie,
2002 atomic_read(&lock->l_export->exp_refcount) : -99,
2008 EXPORT_SYMBOL(_ldlm_lock_debug);