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>
38 #include <obd_class.h>
39 #include "ldlm_internal.h"
42 char *ldlm_lockname[] = {
53 char *ldlm_typename[] = {
60 char *ldlm_it2str(int it)
67 case (IT_OPEN | IT_CREAT):
80 CERROR("Unknown intent %d\n", it);
85 extern cfs_mem_cache_t *ldlm_lock_slab;
87 static ldlm_processing_policy ldlm_processing_policy_table[] = {
88 [LDLM_PLAIN] ldlm_process_plain_lock,
89 [LDLM_EXTENT] ldlm_process_extent_lock,
91 [LDLM_FLOCK] ldlm_process_flock_lock,
93 [LDLM_IBITS] ldlm_process_inodebits_lock,
96 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
98 return ldlm_processing_policy_table[res->lr_type];
101 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
107 * REFCOUNTED LOCK OBJECTS
112 * Lock refcounts, during creation:
113 * - one special one for allocation, dec'd only once in destroy
114 * - one for being a lock that's in-use
115 * - one for the addref associated with a new lock
117 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
119 atomic_inc(&lock->l_refc);
123 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
125 LASSERT(size == sizeof(*lock));
126 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
129 void ldlm_lock_put(struct ldlm_lock *lock)
133 LASSERT(lock->l_resource != LP_POISON);
134 LASSERT(atomic_read(&lock->l_refc) > 0);
135 if (atomic_dec_and_test(&lock->l_refc)) {
136 struct ldlm_resource *res;
139 "final lock_put on destroyed lock, freeing it.");
141 res = lock->l_resource;
142 LASSERT(lock->l_destroyed);
143 LASSERT(list_empty(&lock->l_res_link));
144 LASSERT(list_empty(&lock->l_pending_chain));
146 atomic_dec(&res->lr_namespace->ns_locks);
147 ldlm_resource_putref(res);
148 lock->l_resource = NULL;
149 if (lock->l_export) {
150 class_export_put(lock->l_export);
151 lock->l_export = NULL;
154 if (lock->l_lvb_data != NULL)
155 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
157 ldlm_interval_free(ldlm_interval_detach(lock));
158 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
165 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
168 if (!list_empty(&lock->l_lru)) {
169 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
170 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
171 list_del_init(&lock->l_lru);
173 LASSERT(ns->ns_nr_unused >= 0);
179 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
181 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
184 spin_lock(&ns->ns_unused_lock);
185 rc = ldlm_lock_remove_from_lru_nolock(lock);
186 spin_unlock(&ns->ns_unused_lock);
191 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
193 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
194 lock->l_last_used = cfs_time_current();
195 LASSERT(list_empty(&lock->l_lru));
196 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
197 LASSERT(ns->ns_nr_unused >= 0);
201 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
203 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
205 spin_lock(&ns->ns_unused_lock);
206 ldlm_lock_add_to_lru_nolock(lock);
207 spin_unlock(&ns->ns_unused_lock);
211 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
213 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
215 spin_lock(&ns->ns_unused_lock);
216 if (!list_empty(&lock->l_lru)) {
217 ldlm_lock_remove_from_lru_nolock(lock);
218 ldlm_lock_add_to_lru_nolock(lock);
220 spin_unlock(&ns->ns_unused_lock);
224 /* This used to have a 'strict' flag, which recovery would use to mark an
225 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
226 * shall explain why it's gone: with the new hash table scheme, once you call
227 * ldlm_lock_destroy, you can never drop your final references on this lock.
228 * Because it's not in the hash table anymore. -phil */
229 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
233 if (lock->l_readers || lock->l_writers) {
234 LDLM_ERROR(lock, "lock still has references");
235 ldlm_lock_dump(D_ERROR, lock, 0);
239 if (!list_empty(&lock->l_res_link)) {
240 LDLM_ERROR(lock, "lock still on resource");
241 ldlm_lock_dump(D_ERROR, lock, 0);
245 if (lock->l_destroyed) {
246 LASSERT(list_empty(&lock->l_lru));
250 lock->l_destroyed = 1;
253 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
254 list_del_init(&lock->l_export_chain);
256 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
258 ldlm_lock_remove_from_lru(lock);
259 class_handle_unhash(&lock->l_handle);
262 /* Wake anyone waiting for this lock */
263 /* FIXME: I should probably add yet another flag, instead of using
264 * l_export to only call this on clients */
266 class_export_put(lock->l_export);
267 lock->l_export = NULL;
268 if (lock->l_export && lock->l_completion_ast)
269 lock->l_completion_ast(lock, 0);
275 void ldlm_lock_destroy(struct ldlm_lock *lock)
279 lock_res_and_lock(lock);
280 first = ldlm_lock_destroy_internal(lock);
281 unlock_res_and_lock(lock);
283 /* drop reference from hashtable only for first destroy */
289 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
293 first = ldlm_lock_destroy_internal(lock);
294 /* drop reference from hashtable only for first destroy */
300 /* this is called by portals_handle2object with the handle lock taken */
301 static void lock_handle_addref(void *lock)
303 LDLM_LOCK_GET((struct ldlm_lock *)lock);
307 * usage: pass in a resource on which you have done ldlm_resource_get
308 * pass in a parent lock on which you have done a ldlm_lock_get
309 * after return, ldlm_*_put the resource and parent
310 * returns: lock with refcount 2 - one for current caller and one for remote
312 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
314 struct ldlm_lock *lock;
317 if (resource == NULL)
320 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
324 spin_lock_init(&lock->l_lock);
325 lock->l_resource = ldlm_resource_getref(resource);
327 atomic_set(&lock->l_refc, 2);
328 CFS_INIT_LIST_HEAD(&lock->l_res_link);
329 CFS_INIT_LIST_HEAD(&lock->l_lru);
330 CFS_INIT_LIST_HEAD(&lock->l_export_chain);
331 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
332 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
333 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
334 cfs_waitq_init(&lock->l_waitq);
335 lock->l_blocking_lock = NULL;
336 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
337 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
339 atomic_inc(&resource->lr_namespace->ns_locks);
340 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
341 class_handle_hash(&lock->l_handle, lock_handle_addref);
343 CFS_INIT_LIST_HEAD(&lock->l_extents_list);
344 spin_lock_init(&lock->l_extents_list_lock);
345 CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
350 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
351 const struct ldlm_res_id *new_resid)
353 struct ldlm_resource *oldres = lock->l_resource;
354 struct ldlm_resource *newres;
358 LASSERT(ns_is_client(ns));
360 lock_res_and_lock(lock);
361 if (memcmp(new_resid, &lock->l_resource->lr_name,
362 sizeof(lock->l_resource->lr_name)) == 0) {
364 unlock_res_and_lock(lock);
368 LASSERT(new_resid->name[0] != 0);
370 /* This function assumes that the lock isn't on any lists */
371 LASSERT(list_empty(&lock->l_res_link));
373 type = oldres->lr_type;
374 unlock_res_and_lock(lock);
376 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
380 lock_res_and_lock(lock);
381 LASSERT(memcmp(new_resid, &lock->l_resource->lr_name,
382 sizeof(lock->l_resource->lr_name)) != 0);
384 lock->l_resource = newres;
386 unlock_res_and_lock(lock);
388 /* ...and the flowers are still standing! */
389 ldlm_resource_putref(oldres);
398 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
400 lockh->cookie = lock->l_handle.h_cookie;
403 /* if flags: atomically get the lock and set the flags.
404 * Return NULL if flag already set
407 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
410 struct ldlm_namespace *ns;
411 struct ldlm_lock *lock, *retval = NULL;
416 lock = class_handle2object(handle->cookie);
420 LASSERT(lock->l_resource != NULL);
421 ns = lock->l_resource->lr_namespace;
424 lock_res_and_lock(lock);
426 /* It's unlikely but possible that someone marked the lock as
427 * destroyed after we did handle2object on it */
428 if (lock->l_destroyed) {
429 unlock_res_and_lock(lock);
430 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
435 if (flags && (lock->l_flags & flags)) {
436 unlock_res_and_lock(lock);
442 lock->l_flags |= flags;
444 unlock_res_and_lock(lock);
451 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
452 const struct lustre_handle *handle)
454 struct ldlm_lock *retval = NULL;
455 retval = __ldlm_handle2lock(handle, 0);
459 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
461 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
462 /* INODEBITS_INTEROP: If the other side does not support
463 * inodebits, reply with a plain lock descriptor.
465 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
466 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
467 struct ldlm_resource res = *lock->l_resource;
469 /* Make sure all the right bits are set in this lock we
470 are going to pass to client */
471 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
472 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
473 "Inappropriate inode lock bits during "
474 "conversion " LPU64 "\n",
475 lock->l_policy_data.l_inodebits.bits);
476 res.lr_type = LDLM_PLAIN;
477 ldlm_res2desc(&res, &desc->l_resource);
478 /* Convert "new" lock mode to something old client can
480 if ((lock->l_req_mode == LCK_CR) ||
481 (lock->l_req_mode == LCK_CW))
482 desc->l_req_mode = LCK_PR;
484 desc->l_req_mode = lock->l_req_mode;
485 if ((lock->l_granted_mode == LCK_CR) ||
486 (lock->l_granted_mode == LCK_CW)) {
487 desc->l_granted_mode = LCK_PR;
489 /* We never grant PW/EX locks to clients */
490 LASSERT((lock->l_granted_mode != LCK_PW) &&
491 (lock->l_granted_mode != LCK_EX));
492 desc->l_granted_mode = lock->l_granted_mode;
495 /* We do not copy policy here, because there is no
496 policy for plain locks */
498 ldlm_res2desc(lock->l_resource, &desc->l_resource);
499 desc->l_req_mode = lock->l_req_mode;
500 desc->l_granted_mode = lock->l_granted_mode;
501 desc->l_policy_data = lock->l_policy_data;
505 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
506 struct list_head *work_list)
508 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
509 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
510 lock->l_flags |= LDLM_FL_AST_SENT;
511 /* If the enqueuing client said so, tell the AST recipient to
512 * discard dirty data, rather than writing back. */
513 if (new->l_flags & LDLM_AST_DISCARD_DATA)
514 lock->l_flags |= LDLM_FL_DISCARD_DATA;
515 LASSERT(list_empty(&lock->l_bl_ast));
516 list_add(&lock->l_bl_ast, work_list);
518 LASSERT(lock->l_blocking_lock == NULL);
519 lock->l_blocking_lock = LDLM_LOCK_GET(new);
523 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
525 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
526 lock->l_flags |= LDLM_FL_CP_REQD;
527 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
528 LASSERT(list_empty(&lock->l_cp_ast));
529 list_add(&lock->l_cp_ast, work_list);
534 /* must be called with lr_lock held */
535 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
536 struct list_head *work_list)
539 check_res_locked(lock->l_resource);
541 ldlm_add_bl_work_item(lock, new, work_list);
543 ldlm_add_cp_work_item(lock, work_list);
547 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
549 struct ldlm_lock *lock;
551 lock = ldlm_handle2lock(lockh);
552 LASSERT(lock != NULL);
553 ldlm_lock_addref_internal(lock, mode);
557 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
559 ldlm_lock_remove_from_lru(lock);
560 if (mode & (LCK_NL | LCK_CR | LCK_PR))
562 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
565 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
568 /* only called for local locks */
569 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
571 lock_res_and_lock(lock);
572 ldlm_lock_addref_internal_nolock(lock, mode);
573 unlock_res_and_lock(lock);
576 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
578 struct ldlm_namespace *ns;
581 lock_res_and_lock(lock);
583 ns = lock->l_resource->lr_namespace;
585 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
586 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
587 LASSERT(lock->l_readers > 0);
590 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
591 LASSERT(lock->l_writers > 0);
595 if (lock->l_flags & LDLM_FL_LOCAL &&
596 !lock->l_readers && !lock->l_writers) {
597 /* If this is a local lock on a server namespace and this was
598 * the last reference, cancel the lock. */
599 CDEBUG(D_INFO, "forcing cancel of local lock\n");
600 lock->l_flags |= LDLM_FL_CBPENDING;
603 if (!lock->l_readers && !lock->l_writers &&
604 (lock->l_flags & LDLM_FL_CBPENDING)) {
605 /* If we received a blocked AST and this was the last reference,
606 * run the callback. */
607 if (ns_is_server(ns) && lock->l_export)
608 CERROR("FL_CBPENDING set on non-local lock--just a "
611 LDLM_DEBUG(lock, "final decref done on cbpending lock");
613 LDLM_LOCK_GET(lock); /* dropped by bl thread */
614 ldlm_lock_remove_from_lru(lock);
615 unlock_res_and_lock(lock);
616 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
617 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
618 ldlm_handle_bl_callback(ns, NULL, lock);
619 } else if (ns_is_client(ns) &&
620 !lock->l_readers && !lock->l_writers &&
621 !(lock->l_flags & LDLM_FL_NO_LRU) &&
622 !(lock->l_flags & LDLM_FL_BL_AST)) {
623 /* If this is a client-side namespace and this was the last
624 * reference, put it on the LRU. */
625 ldlm_lock_add_to_lru(lock);
626 unlock_res_and_lock(lock);
627 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
628 * are not supported by the server, otherwise, it is done on
630 if (!exp_connect_cancelset(lock->l_conn_export) &&
631 !ns_connect_lru_resize(ns))
632 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
634 unlock_res_and_lock(lock);
637 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
642 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
644 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
645 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
646 ldlm_lock_decref_internal(lock, mode);
650 /* This will drop a lock reference and mark it for destruction, but will not
651 * necessarily cancel the lock before returning. */
652 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
654 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
657 LASSERT(lock != NULL);
659 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
660 lock_res_and_lock(lock);
661 lock->l_flags |= LDLM_FL_CBPENDING;
662 unlock_res_and_lock(lock);
663 ldlm_lock_decref_internal(lock, mode);
667 struct sl_insert_point {
668 struct list_head *res_link;
669 struct list_head *mode_link;
670 struct list_head *policy_link;
674 * search_granted_lock
677 * Finds a position to insert the new lock.
679 * queue [input]: the granted list where search acts on;
680 * req [input]: the lock whose position to be located;
681 * prev [output]: positions within 3 lists to insert @req to
685 * - ldlm_grant_lock_with_skiplist
687 static void search_granted_lock(struct list_head *queue,
688 struct ldlm_lock *req,
689 struct sl_insert_point *prev)
691 struct list_head *tmp;
692 struct ldlm_lock *lock, *mode_end, *policy_end;
695 list_for_each(tmp, queue) {
696 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
698 mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
701 if (lock->l_req_mode != req->l_req_mode) {
702 /* jump to last lock of mode group */
703 tmp = &mode_end->l_res_link;
707 /* suitable mode group is found */
708 if (lock->l_resource->lr_type == LDLM_PLAIN) {
709 /* insert point is last lock of the mode group */
710 prev->res_link = &mode_end->l_res_link;
711 prev->mode_link = &mode_end->l_sl_mode;
712 prev->policy_link = &req->l_sl_policy;
715 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
717 policy_end = list_entry(lock->l_sl_policy.prev,
721 if (lock->l_policy_data.l_inodebits.bits ==
722 req->l_policy_data.l_inodebits.bits) {
723 /* insert point is last lock of
724 * the policy group */
726 &policy_end->l_res_link;
728 &policy_end->l_sl_mode;
730 &policy_end->l_sl_policy;
735 if (policy_end == mode_end)
736 /* done with mode group */
739 /* jump to next policy group within the mode group */
740 tmp = policy_end->l_res_link.next;
741 lock = list_entry(tmp, struct ldlm_lock,
743 } /* loop over policy groups within the mode group */
745 /* insert point is last lock of the mode group,
746 * new policy group is started */
747 prev->res_link = &mode_end->l_res_link;
748 prev->mode_link = &mode_end->l_sl_mode;
749 prev->policy_link = &req->l_sl_policy;
753 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
758 /* insert point is last lock on the queue,
759 * new mode group and new policy group are started */
760 prev->res_link = queue->prev;
761 prev->mode_link = &req->l_sl_mode;
762 prev->policy_link = &req->l_sl_policy;
767 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
768 struct sl_insert_point *prev)
770 struct ldlm_resource *res = lock->l_resource;
773 check_res_locked(res);
775 ldlm_resource_dump(D_OTHER, res);
776 CDEBUG(D_OTHER, "About to add this lock:\n");
777 ldlm_lock_dump(D_OTHER, lock, 0);
779 if (lock->l_destroyed) {
780 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
784 LASSERT(list_empty(&lock->l_res_link));
785 LASSERT(list_empty(&lock->l_sl_mode));
786 LASSERT(list_empty(&lock->l_sl_policy));
788 list_add(&lock->l_res_link, prev->res_link);
789 list_add(&lock->l_sl_mode, prev->mode_link);
790 list_add(&lock->l_sl_policy, prev->policy_link);
795 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
797 struct sl_insert_point prev;
800 LASSERT(lock->l_req_mode == lock->l_granted_mode);
802 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
803 ldlm_granted_list_add_lock(lock, &prev);
808 * - ldlm_lock_enqueue
809 * - ldlm_reprocess_queue
810 * - ldlm_lock_convert
812 * must be called with lr_lock held
814 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
816 struct ldlm_resource *res = lock->l_resource;
819 check_res_locked(res);
821 lock->l_granted_mode = lock->l_req_mode;
822 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
823 ldlm_grant_lock_with_skiplist(lock);
824 else if (res->lr_type == LDLM_EXTENT)
825 ldlm_extent_add_lock(res, lock);
827 ldlm_resource_add_lock(res, &res->lr_granted, lock);
829 if (lock->l_granted_mode < res->lr_most_restr)
830 res->lr_most_restr = lock->l_granted_mode;
832 if (work_list && lock->l_completion_ast != NULL)
833 ldlm_add_ast_work_item(lock, NULL, work_list);
835 ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
839 /* returns a referenced lock or NULL. See the flag descriptions below, in the
840 * comment above ldlm_lock_match */
841 static struct ldlm_lock *search_queue(struct list_head *queue,
843 ldlm_policy_data_t *policy,
844 struct ldlm_lock *old_lock, int flags)
846 struct ldlm_lock *lock;
847 struct list_head *tmp;
849 list_for_each(tmp, queue) {
852 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
854 if (lock == old_lock)
857 /* llite sometimes wants to match locks that will be
858 * canceled when their users drop, but we allow it to match
859 * if it passes in CBPENDING and the lock still has users.
860 * this is generally only going to be used by children
861 * whose parents already hold a lock so forward progress
862 * can still happen. */
863 if (lock->l_flags & LDLM_FL_CBPENDING &&
864 !(flags & LDLM_FL_CBPENDING))
866 if (lock->l_flags & LDLM_FL_CBPENDING &&
867 lock->l_readers == 0 && lock->l_writers == 0)
870 if (!(lock->l_req_mode & *mode))
872 match = lock->l_req_mode;
874 if (lock->l_resource->lr_type == LDLM_EXTENT &&
875 (lock->l_policy_data.l_extent.start >
876 policy->l_extent.start ||
877 lock->l_policy_data.l_extent.end < policy->l_extent.end))
880 if (unlikely(match == LCK_GROUP) &&
881 lock->l_resource->lr_type == LDLM_EXTENT &&
882 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
885 /* We match if we have existing lock with same or wider set
887 if (lock->l_resource->lr_type == LDLM_IBITS &&
888 ((lock->l_policy_data.l_inodebits.bits &
889 policy->l_inodebits.bits) !=
890 policy->l_inodebits.bits))
893 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
896 if ((flags & LDLM_FL_LOCAL_ONLY) &&
897 !(lock->l_flags & LDLM_FL_LOCAL))
900 if (flags & LDLM_FL_TEST_LOCK) {
902 ldlm_lock_touch_in_lru(lock);
904 ldlm_lock_addref_internal_nolock(lock, match);
913 void ldlm_lock_allow_match(struct ldlm_lock *lock)
915 lock_res_and_lock(lock);
916 lock->l_flags |= LDLM_FL_LVB_READY;
917 cfs_waitq_signal(&lock->l_waitq);
918 unlock_res_and_lock(lock);
921 int ldlm_lock_fast_match(struct ldlm_lock *lock, int rw,
922 obd_off start, obd_off end,
925 LASSERT(rw == OBD_BRW_READ || rw == OBD_BRW_WRITE);
926 /* should LCK_GROUP be handled in a special way? */
927 if (lock && (rw == OBD_BRW_READ ||
928 (lock->l_granted_mode & (LCK_PW | LCK_GROUP))) &&
929 (lock->l_policy_data.l_extent.start <= start) &&
930 (lock->l_policy_data.l_extent.end >= end)) {
931 ldlm_lock_addref_internal(lock, rw == OBD_BRW_WRITE ? LCK_PW : LCK_PR);
932 *cookie = (void *)lock;
933 return 1; /* avoid using rc for stack relief */
938 void ldlm_lock_fast_release(void *cookie, int rw)
940 struct ldlm_lock *lock = (struct ldlm_lock *)cookie;
942 LASSERT(lock != NULL);
943 LASSERT(rw == OBD_BRW_READ || rw == OBD_BRW_WRITE);
944 LASSERT(rw == OBD_BRW_READ ||
945 (lock->l_granted_mode & (LCK_PW | LCK_GROUP)));
946 ldlm_lock_decref_internal(lock, rw == OBD_BRW_WRITE ? LCK_PW : LCK_PR);
949 /* Can be called in two ways:
951 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
952 * for a duplicate of.
954 * Otherwise, all of the fields must be filled in, to match against.
956 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
957 * server (ie, connh is NULL)
958 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
959 * list will be considered
960 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
961 * to be canceled can still be matched as long as they still have reader
962 * or writer refernces
963 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
964 * just tell us if we would have matched.
966 * Returns 1 if it finds an already-existing lock that is compatible; in this
967 * case, lockh is filled in with a addref()ed lock
969 * we also check security context, if that failed we simply return 0 (to keep
970 * caller code unchanged), the context failure will be discovered by caller
973 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
974 const struct ldlm_res_id *res_id, ldlm_type_t type,
975 ldlm_policy_data_t *policy, ldlm_mode_t mode,
976 struct lustre_handle *lockh)
978 struct ldlm_resource *res;
979 struct ldlm_lock *lock, *old_lock = NULL;
984 old_lock = ldlm_handle2lock(lockh);
987 ns = old_lock->l_resource->lr_namespace;
988 res_id = &old_lock->l_resource->lr_name;
989 type = old_lock->l_resource->lr_type;
990 mode = old_lock->l_req_mode;
993 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
995 LASSERT(old_lock == NULL);
1001 lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
1004 if (flags & LDLM_FL_BLOCK_GRANTED)
1006 lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
1009 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
1016 ldlm_resource_putref(res);
1019 ldlm_lock2handle(lock, lockh);
1020 if ((flags & LDLM_FL_LVB_READY) &&
1021 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1022 struct l_wait_info lwi;
1023 if (lock->l_completion_ast) {
1024 int err = lock->l_completion_ast(lock,
1025 LDLM_FL_WAIT_NOREPROC,
1028 if (flags & LDLM_FL_TEST_LOCK)
1029 LDLM_LOCK_PUT(lock);
1031 ldlm_lock_decref_internal(lock, mode);
1037 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout), NULL,
1038 LWI_ON_SIGNAL_NOOP, NULL);
1040 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1041 l_wait_event(lock->l_waitq,
1042 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1047 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1048 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1049 res_id->name[2] : policy->l_extent.start,
1050 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1051 res_id->name[3] : policy->l_extent.end);
1053 /* check user's security context */
1054 if (lock->l_conn_export &&
1055 sptlrpc_import_check_ctx(
1056 class_exp2cliimp(lock->l_conn_export))) {
1057 if (!(flags & LDLM_FL_TEST_LOCK))
1058 ldlm_lock_decref_internal(lock, mode);
1062 if (flags & LDLM_FL_TEST_LOCK)
1063 LDLM_LOCK_PUT(lock);
1065 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1066 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1067 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1068 type, mode, res_id->name[0], res_id->name[1],
1069 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1070 res_id->name[2] :policy->l_extent.start,
1071 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1072 res_id->name[3] : policy->l_extent.end);
1075 LDLM_LOCK_PUT(old_lock);
1077 return rc ? mode : 0;
1080 /* Returns a referenced lock */
1081 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1082 const struct ldlm_res_id *res_id,
1085 ldlm_blocking_callback blocking,
1086 ldlm_completion_callback completion,
1087 ldlm_glimpse_callback glimpse,
1088 void *data, __u32 lvb_len)
1090 struct ldlm_lock *lock;
1091 struct ldlm_resource *res;
1094 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1098 lock = ldlm_lock_new(res);
1099 ldlm_resource_putref(res);
1104 lock->l_req_mode = mode;
1105 lock->l_ast_data = data;
1106 lock->l_blocking_ast = blocking;
1107 lock->l_completion_ast = completion;
1108 lock->l_glimpse_ast = glimpse;
1109 lock->l_pid = cfs_curproc_pid();
1111 lock->l_tree_node = NULL;
1112 /* if this is the extent lock, allocate the interval tree node */
1113 if (type == LDLM_EXTENT) {
1114 if (ldlm_interval_alloc(lock) == NULL)
1119 lock->l_lvb_len = lvb_len;
1120 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1121 if (lock->l_lvb_data == NULL)
1128 if (lock->l_lvb_data)
1129 OBD_FREE(lock->l_lvb_data, lvb_len);
1130 ldlm_interval_free(ldlm_interval_detach(lock));
1131 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1135 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1136 struct ldlm_lock **lockp,
1137 void *cookie, int *flags)
1139 struct ldlm_lock *lock = *lockp;
1140 struct ldlm_resource *res = lock->l_resource;
1141 int local = ns_is_client(res->lr_namespace);
1142 ldlm_processing_policy policy;
1143 ldlm_error_t rc = ELDLM_OK;
1144 struct ldlm_interval *node = NULL;
1147 do_gettimeofday(&lock->l_enqueued_time);
1148 /* policies are not executed on the client or during replay */
1149 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1150 && !local && ns->ns_policy) {
1151 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1153 if (rc == ELDLM_LOCK_REPLACED) {
1154 /* The lock that was returned has already been granted,
1155 * and placed into lockp. If it's not the same as the
1156 * one we passed in, then destroy the old one and our
1157 * work here is done. */
1158 if (lock != *lockp) {
1159 ldlm_lock_destroy(lock);
1160 LDLM_LOCK_PUT(lock);
1162 *flags |= LDLM_FL_LOCK_CHANGED;
1164 } else if (rc != ELDLM_OK ||
1165 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1166 ldlm_lock_destroy(lock);
1171 /* For a replaying lock, it might be already in granted list. So
1172 * unlinking the lock will cause the interval node to be freed, we
1173 * have to allocate the interval node early otherwise we can't regrant
1174 * this lock in the future. - jay */
1175 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1176 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO,
1179 lock_res_and_lock(lock);
1180 if (local && lock->l_req_mode == lock->l_granted_mode) {
1181 /* The server returned a blocked lock, but it was granted
1182 * before we got a chance to actually enqueue it. We don't
1183 * need to do anything else. */
1184 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1185 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1186 GOTO(out, ELDLM_OK);
1189 ldlm_resource_unlink_lock(lock);
1190 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1192 ldlm_lock_destroy_nolock(lock);
1193 GOTO(out, rc = -ENOMEM);
1196 CFS_INIT_LIST_HEAD(&node->li_group);
1197 ldlm_interval_attach(node, lock);
1201 /* Some flags from the enqueue want to make it into the AST, via the
1202 * lock's l_flags. */
1203 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1205 /* This distinction between local lock trees is very important; a client
1206 * namespace only has information about locks taken by that client, and
1207 * thus doesn't have enough information to decide for itself if it can
1208 * be granted (below). In this case, we do exactly what the server
1209 * tells us to do, as dictated by the 'flags'.
1211 * We do exactly the same thing during recovery, when the server is
1212 * more or less trusting the clients not to lie.
1214 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1215 * granted/converting queues. */
1217 if (*flags & LDLM_FL_BLOCK_CONV)
1218 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1219 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1220 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1222 ldlm_grant_lock(lock, NULL);
1223 GOTO(out, ELDLM_OK);
1224 } else if (*flags & LDLM_FL_REPLAY) {
1225 if (*flags & LDLM_FL_BLOCK_CONV) {
1226 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1227 GOTO(out, ELDLM_OK);
1228 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1229 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1230 GOTO(out, ELDLM_OK);
1231 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1232 ldlm_grant_lock(lock, NULL);
1233 GOTO(out, ELDLM_OK);
1235 /* If no flags, fall through to normal enqueue path. */
1238 policy = ldlm_processing_policy_table[res->lr_type];
1239 policy(lock, flags, 1, &rc, NULL);
1242 unlock_res_and_lock(lock);
1244 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1248 /* Must be called with namespace taken: queue is waiting or converting. */
1249 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1250 struct list_head *work_list)
1252 struct list_head *tmp, *pos;
1253 ldlm_processing_policy policy;
1255 int rc = LDLM_ITER_CONTINUE;
1259 check_res_locked(res);
1261 policy = ldlm_processing_policy_table[res->lr_type];
1264 list_for_each_safe(tmp, pos, queue) {
1265 struct ldlm_lock *pending;
1266 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1268 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1271 rc = policy(pending, &flags, 0, &err, work_list);
1272 if (rc != LDLM_ITER_CONTINUE)
1279 /* Helper function for ldlm_run_ast_work().
1281 * Send an existing rpc set specified by @arg->set and then
1282 * destroy it. Create new one if @do_create flag is set. */
1284 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1288 ptlrpc_set_wait(arg->set);
1289 if (arg->type == LDLM_BL_CALLBACK)
1290 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1291 ptlrpc_set_destroy(arg->set);
1294 arg->set = ptlrpc_prep_set();
1300 ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1302 struct ldlm_lock_desc d;
1303 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
1306 /* nobody should touch l_bl_ast */
1307 lock_res_and_lock(lock);
1308 list_del_init(&lock->l_bl_ast);
1310 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1311 LASSERT(lock->l_bl_ast_run == 0);
1312 LASSERT(lock->l_blocking_lock);
1313 lock->l_bl_ast_run++;
1314 unlock_res_and_lock(lock);
1316 ldlm_lock2desc(lock->l_blocking_lock, &d);
1318 LDLM_LOCK_PUT(lock->l_blocking_lock);
1319 lock->l_blocking_lock = NULL;
1320 lock->l_blocking_ast(lock, &d, (void *)arg,
1322 LDLM_LOCK_PUT(lock);
1328 ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1330 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
1331 ldlm_completion_callback completion_callback;
1335 /* It's possible to receive a completion AST before we've set
1336 * the l_completion_ast pointer: either because the AST arrived
1337 * before the reply, or simply because there's a small race
1338 * window between receiving the reply and finishing the local
1339 * enqueue. (bug 842)
1341 * This can't happen with the blocking_ast, however, because we
1342 * will never call the local blocking_ast until we drop our
1343 * reader/writer reference, which we won't do until we get the
1344 * reply and finish enqueueing. */
1346 /* nobody should touch l_cp_ast */
1347 lock_res_and_lock(lock);
1348 list_del_init(&lock->l_cp_ast);
1349 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1350 /* save l_completion_ast since it can be changed by
1351 * mds_intent_policy(), see bug 14225 */
1352 completion_callback = lock->l_completion_ast;
1353 lock->l_flags &= ~LDLM_FL_CP_REQD;
1354 unlock_res_and_lock(lock);
1356 if (completion_callback != NULL) {
1357 completion_callback(lock, 0, (void *)arg);
1360 LDLM_LOCK_PUT(lock);
1366 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1368 struct ldlm_lock_desc desc;
1369 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_export_chain);
1372 list_del_init(&lock->l_export_chain);
1374 /* the desc just pretend to exclusive */
1375 ldlm_lock2desc(lock, &desc);
1376 desc.l_req_mode = LCK_EX;
1377 desc.l_granted_mode = 0;
1379 lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1380 LDLM_LOCK_PUT(lock);
1385 int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
1387 struct ldlm_cb_set_arg arg;
1388 struct list_head *tmp, *pos;
1389 int (*work_ast_lock)(struct list_head *tmp, struct ldlm_cb_set_arg *arg);
1393 arg.set = ptlrpc_prep_set();
1394 atomic_set(&arg.restart, 0);
1396 case LDLM_WORK_BL_AST:
1397 arg.type = LDLM_BL_CALLBACK;
1398 work_ast_lock = ldlm_work_bl_ast_lock;
1400 case LDLM_WORK_CP_AST:
1401 arg.type = LDLM_CP_CALLBACK;
1402 work_ast_lock = ldlm_work_cp_ast_lock;
1404 case LDLM_WORK_REVOKE_AST:
1405 arg.type = LDLM_BL_CALLBACK;
1406 work_ast_lock = ldlm_work_revoke_ast_lock;
1413 list_for_each_safe(tmp, pos, rpc_list) {
1414 ast_count += work_ast_lock(tmp, &arg);
1416 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1417 * and create a new set for requests that remained in
1419 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1420 ldlm_send_and_maybe_create_set(&arg, 1);
1426 ldlm_send_and_maybe_create_set(&arg, 0);
1428 /* In case when number of ASTs is multiply of
1429 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1430 * @arg.set must be destroyed here, otherwise we get
1431 * write memory leaking. */
1432 ptlrpc_set_destroy(arg.set);
1434 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1437 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1439 ldlm_reprocess_all(res);
1440 return LDLM_ITER_CONTINUE;
1443 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1445 struct list_head *tmp;
1452 spin_lock(&ns->ns_hash_lock);
1453 for (i = 0; i < RES_HASH_SIZE; i++) {
1454 tmp = ns->ns_hash[i].next;
1455 while (tmp != &(ns->ns_hash[i])) {
1456 struct ldlm_resource *res =
1457 list_entry(tmp, struct ldlm_resource, lr_hash);
1459 ldlm_resource_getref(res);
1460 spin_unlock(&ns->ns_hash_lock);
1462 rc = reprocess_one_queue(res, NULL);
1464 spin_lock(&ns->ns_hash_lock);
1466 ldlm_resource_putref_locked(res);
1468 if (rc == LDLM_ITER_STOP)
1473 spin_unlock(&ns->ns_hash_lock);
1477 void ldlm_reprocess_all(struct ldlm_resource *res)
1479 CFS_LIST_HEAD(rpc_list);
1483 /* Local lock trees don't get reprocessed. */
1484 if (ns_is_client(res->lr_namespace)) {
1491 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1492 if (rc == LDLM_ITER_CONTINUE)
1493 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1496 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1497 if (rc == -ERESTART) {
1498 LASSERT(list_empty(&rpc_list));
1504 void ldlm_cancel_callback(struct ldlm_lock *lock)
1506 check_res_locked(lock->l_resource);
1507 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1508 lock->l_flags |= LDLM_FL_CANCEL;
1509 if (lock->l_blocking_ast) {
1510 // l_check_no_ns_lock(ns);
1511 unlock_res_and_lock(lock);
1512 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1514 lock_res_and_lock(lock);
1516 LDLM_DEBUG(lock, "no blocking ast");
1519 lock->l_flags |= LDLM_FL_BL_DONE;
1522 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1524 if (req->l_resource->lr_type != LDLM_PLAIN &&
1525 req->l_resource->lr_type != LDLM_IBITS)
1528 list_del_init(&req->l_sl_policy);
1529 list_del_init(&req->l_sl_mode);
1532 void ldlm_lock_cancel(struct ldlm_lock *lock)
1534 struct ldlm_resource *res;
1535 struct ldlm_namespace *ns;
1538 lock_res_and_lock(lock);
1540 res = lock->l_resource;
1541 ns = res->lr_namespace;
1543 /* Please do not, no matter how tempting, remove this LBUG without
1544 * talking to me first. -phik */
1545 if (lock->l_readers || lock->l_writers) {
1546 LDLM_ERROR(lock, "lock still has references");
1550 ldlm_del_waiting_lock(lock);
1552 /* Releases cancel callback. */
1553 ldlm_cancel_callback(lock);
1555 /* Yes, second time, just in case it was added again while we were
1556 running with no res lock in ldlm_cancel_callback */
1557 ldlm_del_waiting_lock(lock);
1558 ldlm_resource_unlink_lock(lock);
1559 ldlm_lock_destroy_nolock(lock);
1561 if (lock->l_granted_mode == lock->l_req_mode)
1562 ldlm_pool_del(&ns->ns_pool, lock);
1564 /* Make sure we will not be called again for same lock what is possible
1565 * if not to zero out lock->l_granted_mode */
1566 lock->l_granted_mode = 0;
1567 unlock_res_and_lock(lock);
1572 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1574 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1580 lock->l_ast_data = data;
1581 LDLM_LOCK_PUT(lock);
1585 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1587 struct ldlm_lock *lock;
1588 struct ldlm_resource *res;
1590 spin_lock(&exp->exp_ldlm_data.led_lock);
1591 while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
1592 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1593 struct ldlm_lock, l_export_chain);
1594 res = ldlm_resource_getref(lock->l_resource);
1595 LDLM_LOCK_GET(lock);
1596 spin_unlock(&exp->exp_ldlm_data.led_lock);
1598 LDLM_DEBUG(lock, "export %p", exp);
1599 ldlm_res_lvbo_update(res, NULL, 0, 1);
1601 ldlm_lock_cancel(lock);
1602 ldlm_reprocess_all(res);
1604 ldlm_resource_putref(res);
1605 LDLM_LOCK_PUT(lock);
1606 spin_lock(&exp->exp_ldlm_data.led_lock);
1608 spin_unlock(&exp->exp_ldlm_data.led_lock);
1611 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1614 CFS_LIST_HEAD(rpc_list);
1615 struct ldlm_resource *res;
1616 struct ldlm_namespace *ns;
1619 struct sl_insert_point prev;
1621 struct ldlm_interval *node;
1624 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1625 *flags |= LDLM_FL_BLOCK_GRANTED;
1626 RETURN(lock->l_resource);
1629 /* I can't check the type of lock here because the bitlock of lock
1630 * is not held here, so do the allocation blindly. -jay */
1631 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
1632 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1635 LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1636 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1638 lock_res_and_lock(lock);
1640 res = lock->l_resource;
1641 ns = res->lr_namespace;
1643 old_mode = lock->l_req_mode;
1644 lock->l_req_mode = new_mode;
1645 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1646 /* remember the lock position where the lock might be
1647 * added back to the granted list later and also
1648 * remember the join mode for skiplist fixing. */
1649 prev.res_link = lock->l_res_link.prev;
1650 prev.mode_link = lock->l_sl_mode.prev;
1651 prev.policy_link = lock->l_sl_policy.prev;
1652 ldlm_resource_unlink_lock(lock);
1654 ldlm_resource_unlink_lock(lock);
1655 if (res->lr_type == LDLM_EXTENT) {
1656 /* FIXME: ugly code, I have to attach the lock to a
1657 * interval node again since perhaps it will be granted
1659 CFS_INIT_LIST_HEAD(&node->li_group);
1660 ldlm_interval_attach(node, lock);
1665 /* If this is a local resource, put it on the appropriate list. */
1666 if (ns_is_client(res->lr_namespace)) {
1667 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1668 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1670 /* This should never happen, because of the way the
1671 * server handles conversions. */
1672 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1676 ldlm_grant_lock(lock, &rpc_list);
1678 /* FIXME: completion handling not with ns_lock held ! */
1679 if (lock->l_completion_ast)
1680 lock->l_completion_ast(lock, 0, NULL);
1684 ldlm_processing_policy policy;
1685 policy = ldlm_processing_policy_table[res->lr_type];
1686 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1687 if (rc == LDLM_ITER_STOP) {
1688 lock->l_req_mode = old_mode;
1689 if (res->lr_type == LDLM_EXTENT)
1690 ldlm_extent_add_lock(res, lock);
1692 ldlm_granted_list_add_lock(lock, &prev);
1696 *flags |= LDLM_FL_BLOCK_GRANTED;
1700 unlock_res_and_lock(lock);
1703 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1705 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1709 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1711 struct obd_device *obd = NULL;
1713 if (!((libcfs_debug | D_ERROR) & level))
1717 CDEBUG(level, " NULL LDLM lock\n");
1721 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1722 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1724 if (lock->l_conn_export != NULL)
1725 obd = lock->l_conn_export->exp_obd;
1726 if (lock->l_export && lock->l_export->exp_connection) {
1727 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1728 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1729 lock->l_remote_handle.cookie);
1730 } else if (obd == NULL) {
1731 CDEBUG(level, " Node: local\n");
1733 struct obd_import *imp = obd->u.cli.cl_import;
1734 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1735 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1736 lock->l_remote_handle.cookie);
1738 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1740 lock->l_resource->lr_name.name[0],
1741 lock->l_resource->lr_name.name[1],
1742 lock->l_resource->lr_name.name[2]);
1743 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1744 "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
1745 ldlm_lockname[lock->l_granted_mode],
1746 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1748 if (lock->l_resource->lr_type == LDLM_EXTENT)
1749 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1750 " (req "LPU64"-"LPU64")\n",
1751 lock->l_policy_data.l_extent.start,
1752 lock->l_policy_data.l_extent.end,
1753 lock->l_req_extent.start, lock->l_req_extent.end);
1754 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1755 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1756 lock->l_policy_data.l_flock.pid,
1757 lock->l_policy_data.l_flock.start,
1758 lock->l_policy_data.l_flock.end);
1759 else if (lock->l_resource->lr_type == LDLM_IBITS)
1760 CDEBUG(level, " Bits: "LPX64"\n",
1761 lock->l_policy_data.l_inodebits.bits);
1764 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1766 struct ldlm_lock *lock;
1768 if (!((libcfs_debug | D_ERROR) & level))
1771 lock = ldlm_handle2lock(lockh);
1775 ldlm_lock_dump(D_OTHER, lock, 0);
1777 LDLM_LOCK_PUT(lock);
1780 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1781 struct libcfs_debug_msg_data *data, const char *fmt,
1785 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1787 va_start(args, fmt);
1789 if (lock->l_resource == NULL) {
1790 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1791 data->msg_fn, data->msg_line, fmt, args,
1792 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1793 "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
1794 LPX64" expref: %d pid: %u\n", lock,
1795 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1796 lock->l_readers, lock->l_writers,
1797 ldlm_lockname[lock->l_granted_mode],
1798 ldlm_lockname[lock->l_req_mode],
1799 lock->l_flags, lock->l_remote_handle.cookie,
1801 atomic_read(&lock->l_export->exp_refcount) : -99,
1807 switch (lock->l_resource->lr_type) {
1809 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1810 data->msg_fn, data->msg_line, fmt, args,
1811 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1812 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1813 "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
1814 " expref: %d pid: %u\n",
1815 lock->l_resource->lr_namespace->ns_name, lock,
1816 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1817 lock->l_readers, lock->l_writers,
1818 ldlm_lockname[lock->l_granted_mode],
1819 ldlm_lockname[lock->l_req_mode],
1820 lock->l_resource->lr_name.name[0],
1821 lock->l_resource->lr_name.name[1],
1822 atomic_read(&lock->l_resource->lr_refcount),
1823 ldlm_typename[lock->l_resource->lr_type],
1824 lock->l_policy_data.l_extent.start,
1825 lock->l_policy_data.l_extent.end,
1826 lock->l_req_extent.start, lock->l_req_extent.end,
1827 lock->l_flags, lock->l_remote_handle.cookie,
1829 atomic_read(&lock->l_export->exp_refcount) : -99,
1834 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1835 data->msg_fn, data->msg_line, fmt, args,
1836 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1837 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1838 "["LPU64"->"LPU64"] flags: %x remote: "LPX64
1839 " expref: %d pid: %u\n",
1840 lock->l_resource->lr_namespace->ns_name, lock,
1841 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1842 lock->l_readers, lock->l_writers,
1843 ldlm_lockname[lock->l_granted_mode],
1844 ldlm_lockname[lock->l_req_mode],
1845 lock->l_resource->lr_name.name[0],
1846 lock->l_resource->lr_name.name[1],
1847 atomic_read(&lock->l_resource->lr_refcount),
1848 ldlm_typename[lock->l_resource->lr_type],
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 lock->l_flags, lock->l_remote_handle.cookie,
1854 atomic_read(&lock->l_export->exp_refcount) : -99,
1859 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1860 data->msg_fn, data->msg_line, fmt, args,
1861 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1862 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1863 "flags: %x remote: "LPX64" expref: %d "
1865 lock->l_resource->lr_namespace->ns_name,
1866 lock, lock->l_handle.h_cookie,
1867 atomic_read (&lock->l_refc),
1868 lock->l_readers, lock->l_writers,
1869 ldlm_lockname[lock->l_granted_mode],
1870 ldlm_lockname[lock->l_req_mode],
1871 lock->l_resource->lr_name.name[0],
1872 lock->l_resource->lr_name.name[1],
1873 lock->l_policy_data.l_inodebits.bits,
1874 atomic_read(&lock->l_resource->lr_refcount),
1875 ldlm_typename[lock->l_resource->lr_type],
1876 lock->l_flags, lock->l_remote_handle.cookie,
1878 atomic_read(&lock->l_export->exp_refcount) : -99,
1883 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1884 data->msg_fn, data->msg_line, fmt, args,
1885 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1886 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
1887 "remote: "LPX64" expref: %d pid: %u\n",
1888 lock->l_resource->lr_namespace->ns_name,
1889 lock, lock->l_handle.h_cookie,
1890 atomic_read (&lock->l_refc),
1891 lock->l_readers, lock->l_writers,
1892 ldlm_lockname[lock->l_granted_mode],
1893 ldlm_lockname[lock->l_req_mode],
1894 lock->l_resource->lr_name.name[0],
1895 lock->l_resource->lr_name.name[1],
1896 atomic_read(&lock->l_resource->lr_refcount),
1897 ldlm_typename[lock->l_resource->lr_type],
1898 lock->l_flags, lock->l_remote_handle.cookie,
1900 atomic_read(&lock->l_export->exp_refcount) : -99,
1906 EXPORT_SYMBOL(_ldlm_lock_debug);