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 ldlm_resource_putref(res);
163 lock->l_resource = NULL;
164 if (lock->l_export) {
165 class_export_put(lock->l_export);
166 lock->l_export = NULL;
169 if (lock->l_lvb_data != NULL)
170 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
172 ldlm_interval_free(ldlm_interval_detach(lock));
173 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
180 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
183 if (!list_empty(&lock->l_lru)) {
184 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
185 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
186 list_del_init(&lock->l_lru);
188 LASSERT(ns->ns_nr_unused >= 0);
194 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
196 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
199 spin_lock(&ns->ns_unused_lock);
200 rc = ldlm_lock_remove_from_lru_nolock(lock);
201 spin_unlock(&ns->ns_unused_lock);
206 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
208 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
209 lock->l_last_used = cfs_time_current();
210 LASSERT(list_empty(&lock->l_lru));
211 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
212 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
213 LASSERT(ns->ns_nr_unused >= 0);
217 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
219 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
221 spin_lock(&ns->ns_unused_lock);
222 ldlm_lock_add_to_lru_nolock(lock);
223 spin_unlock(&ns->ns_unused_lock);
227 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
229 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
231 spin_lock(&ns->ns_unused_lock);
232 if (!list_empty(&lock->l_lru)) {
233 ldlm_lock_remove_from_lru_nolock(lock);
234 ldlm_lock_add_to_lru_nolock(lock);
236 spin_unlock(&ns->ns_unused_lock);
240 /* This used to have a 'strict' flag, which recovery would use to mark an
241 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
242 * shall explain why it's gone: with the new hash table scheme, once you call
243 * ldlm_lock_destroy, you can never drop your final references on this lock.
244 * Because it's not in the hash table anymore. -phil */
245 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
249 if (lock->l_readers || lock->l_writers) {
250 LDLM_ERROR(lock, "lock still has references");
251 ldlm_lock_dump(D_ERROR, lock, 0);
255 if (!list_empty(&lock->l_res_link)) {
256 LDLM_ERROR(lock, "lock still on resource");
257 ldlm_lock_dump(D_ERROR, lock, 0);
261 if (lock->l_destroyed) {
262 LASSERT(list_empty(&lock->l_lru));
266 lock->l_destroyed = 1;
269 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
270 list_del_init(&lock->l_export_chain);
272 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
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 */
305 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
309 first = ldlm_lock_destroy_internal(lock);
310 /* drop reference from hashtable only for first destroy */
316 /* this is called by portals_handle2object with the handle lock taken */
317 static void lock_handle_addref(void *lock)
319 LDLM_LOCK_GET((struct ldlm_lock *)lock);
323 * usage: pass in a resource on which you have done ldlm_resource_get
324 * pass in a parent lock on which you have done a ldlm_lock_get
325 * after return, ldlm_*_put the resource and parent
326 * returns: lock with refcount 2 - one for current caller and one for remote
328 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
330 struct ldlm_lock *lock;
333 if (resource == NULL)
336 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
340 spin_lock_init(&lock->l_lock);
341 lock->l_resource = ldlm_resource_getref(resource);
343 atomic_set(&lock->l_refc, 2);
344 CFS_INIT_LIST_HEAD(&lock->l_res_link);
345 CFS_INIT_LIST_HEAD(&lock->l_lru);
346 CFS_INIT_LIST_HEAD(&lock->l_export_chain);
347 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
348 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
349 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
350 cfs_waitq_init(&lock->l_waitq);
351 lock->l_blocking_lock = NULL;
352 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
353 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
355 atomic_inc(&resource->lr_namespace->ns_locks);
356 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
357 class_handle_hash(&lock->l_handle, lock_handle_addref);
359 CFS_INIT_LIST_HEAD(&lock->l_extents_list);
360 spin_lock_init(&lock->l_extents_list_lock);
361 CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
366 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
367 const struct ldlm_res_id *new_resid)
369 struct ldlm_resource *oldres = lock->l_resource;
370 struct ldlm_resource *newres;
374 LASSERT(ns_is_client(ns));
376 lock_res_and_lock(lock);
377 if (memcmp(new_resid, &lock->l_resource->lr_name,
378 sizeof(lock->l_resource->lr_name)) == 0) {
380 unlock_res_and_lock(lock);
384 LASSERT(new_resid->name[0] != 0);
386 /* This function assumes that the lock isn't on any lists */
387 LASSERT(list_empty(&lock->l_res_link));
389 type = oldres->lr_type;
390 unlock_res_and_lock(lock);
392 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
396 lock_res_and_lock(lock);
397 LASSERT(memcmp(new_resid, &lock->l_resource->lr_name,
398 sizeof(lock->l_resource->lr_name)) != 0);
400 lock->l_resource = newres;
402 unlock_res_and_lock(lock);
404 /* ...and the flowers are still standing! */
405 ldlm_resource_putref(oldres);
414 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
416 lockh->cookie = lock->l_handle.h_cookie;
419 /* if flags: atomically get the lock and set the flags.
420 * Return NULL if flag already set
423 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
426 struct ldlm_namespace *ns;
427 struct ldlm_lock *lock, *retval = NULL;
432 lock = class_handle2object(handle->cookie);
436 LASSERT(lock->l_resource != NULL);
437 ns = lock->l_resource->lr_namespace;
440 lock_res_and_lock(lock);
442 /* It's unlikely but possible that someone marked the lock as
443 * destroyed after we did handle2object on it */
444 if (lock->l_destroyed) {
445 unlock_res_and_lock(lock);
446 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
451 if (flags && (lock->l_flags & flags)) {
452 unlock_res_and_lock(lock);
458 lock->l_flags |= flags;
460 unlock_res_and_lock(lock);
467 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
468 const struct lustre_handle *handle)
470 struct ldlm_lock *retval = NULL;
471 retval = __ldlm_handle2lock(handle, 0);
475 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
477 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
478 /* INODEBITS_INTEROP: If the other side does not support
479 * inodebits, reply with a plain lock descriptor.
481 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
482 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
483 struct ldlm_resource res = *lock->l_resource;
485 /* Make sure all the right bits are set in this lock we
486 are going to pass to client */
487 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
488 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
489 "Inappropriate inode lock bits during "
490 "conversion " LPU64 "\n",
491 lock->l_policy_data.l_inodebits.bits);
492 res.lr_type = LDLM_PLAIN;
493 ldlm_res2desc(&res, &desc->l_resource);
494 /* Convert "new" lock mode to something old client can
496 if ((lock->l_req_mode == LCK_CR) ||
497 (lock->l_req_mode == LCK_CW))
498 desc->l_req_mode = LCK_PR;
500 desc->l_req_mode = lock->l_req_mode;
501 if ((lock->l_granted_mode == LCK_CR) ||
502 (lock->l_granted_mode == LCK_CW)) {
503 desc->l_granted_mode = LCK_PR;
505 /* We never grant PW/EX locks to clients */
506 LASSERT((lock->l_granted_mode != LCK_PW) &&
507 (lock->l_granted_mode != LCK_EX));
508 desc->l_granted_mode = lock->l_granted_mode;
511 /* We do not copy policy here, because there is no
512 policy for plain locks */
514 ldlm_res2desc(lock->l_resource, &desc->l_resource);
515 desc->l_req_mode = lock->l_req_mode;
516 desc->l_granted_mode = lock->l_granted_mode;
517 desc->l_policy_data = lock->l_policy_data;
521 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
522 struct list_head *work_list)
524 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
525 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
526 lock->l_flags |= LDLM_FL_AST_SENT;
527 /* If the enqueuing client said so, tell the AST recipient to
528 * discard dirty data, rather than writing back. */
529 if (new->l_flags & LDLM_AST_DISCARD_DATA)
530 lock->l_flags |= LDLM_FL_DISCARD_DATA;
531 LASSERT(list_empty(&lock->l_bl_ast));
532 list_add(&lock->l_bl_ast, work_list);
534 LASSERT(lock->l_blocking_lock == NULL);
535 lock->l_blocking_lock = LDLM_LOCK_GET(new);
539 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
541 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
542 lock->l_flags |= LDLM_FL_CP_REQD;
543 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
544 LASSERT(list_empty(&lock->l_cp_ast));
545 list_add(&lock->l_cp_ast, work_list);
550 /* must be called with lr_lock held */
551 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
552 struct list_head *work_list)
555 check_res_locked(lock->l_resource);
557 ldlm_add_bl_work_item(lock, new, work_list);
559 ldlm_add_cp_work_item(lock, work_list);
563 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
565 struct ldlm_lock *lock;
567 lock = ldlm_handle2lock(lockh);
568 LASSERT(lock != NULL);
569 ldlm_lock_addref_internal(lock, mode);
573 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
575 ldlm_lock_remove_from_lru(lock);
576 if (mode & (LCK_NL | LCK_CR | LCK_PR))
578 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
581 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
584 /* only called for local locks */
585 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
587 lock_res_and_lock(lock);
588 ldlm_lock_addref_internal_nolock(lock, mode);
589 unlock_res_and_lock(lock);
592 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
594 struct ldlm_namespace *ns;
597 lock_res_and_lock(lock);
599 ns = lock->l_resource->lr_namespace;
601 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
602 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
603 LASSERT(lock->l_readers > 0);
606 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
607 LASSERT(lock->l_writers > 0);
611 if (lock->l_flags & LDLM_FL_LOCAL &&
612 !lock->l_readers && !lock->l_writers) {
613 /* If this is a local lock on a server namespace and this was
614 * the last reference, cancel the lock. */
615 CDEBUG(D_INFO, "forcing cancel of local lock\n");
616 lock->l_flags |= LDLM_FL_CBPENDING;
619 if (!lock->l_readers && !lock->l_writers &&
620 (lock->l_flags & LDLM_FL_CBPENDING)) {
621 /* If we received a blocked AST and this was the last reference,
622 * run the callback. */
623 if (ns_is_server(ns) && lock->l_export)
624 CERROR("FL_CBPENDING set on non-local lock--just a "
627 LDLM_DEBUG(lock, "final decref done on cbpending lock");
629 LDLM_LOCK_GET(lock); /* dropped by bl thread */
630 ldlm_lock_remove_from_lru(lock);
631 unlock_res_and_lock(lock);
632 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
633 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
634 ldlm_handle_bl_callback(ns, NULL, lock);
635 } else if (ns_is_client(ns) &&
636 !lock->l_readers && !lock->l_writers &&
637 !(lock->l_flags & LDLM_FL_NO_LRU) &&
638 !(lock->l_flags & LDLM_FL_BL_AST)) {
639 /* If this is a client-side namespace and this was the last
640 * reference, put it on the LRU. */
641 ldlm_lock_add_to_lru(lock);
642 unlock_res_and_lock(lock);
643 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
644 * are not supported by the server, otherwise, it is done on
646 if (!exp_connect_cancelset(lock->l_conn_export) &&
647 !ns_connect_lru_resize(ns))
648 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
650 unlock_res_and_lock(lock);
653 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
658 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
660 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
661 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
662 ldlm_lock_decref_internal(lock, mode);
666 /* This will drop a lock reference and mark it for destruction, but will not
667 * necessarily cancel the lock before returning. */
668 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
670 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
673 LASSERT(lock != NULL);
675 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
676 lock_res_and_lock(lock);
677 lock->l_flags |= LDLM_FL_CBPENDING;
678 unlock_res_and_lock(lock);
679 ldlm_lock_decref_internal(lock, mode);
683 struct sl_insert_point {
684 struct list_head *res_link;
685 struct list_head *mode_link;
686 struct list_head *policy_link;
690 * search_granted_lock
693 * Finds a position to insert the new lock.
695 * queue [input]: the granted list where search acts on;
696 * req [input]: the lock whose position to be located;
697 * prev [output]: positions within 3 lists to insert @req to
701 * - ldlm_grant_lock_with_skiplist
703 static void search_granted_lock(struct list_head *queue,
704 struct ldlm_lock *req,
705 struct sl_insert_point *prev)
707 struct list_head *tmp;
708 struct ldlm_lock *lock, *mode_end, *policy_end;
711 list_for_each(tmp, queue) {
712 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
714 mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
717 if (lock->l_req_mode != req->l_req_mode) {
718 /* jump to last lock of mode group */
719 tmp = &mode_end->l_res_link;
723 /* suitable mode group is found */
724 if (lock->l_resource->lr_type == LDLM_PLAIN) {
725 /* insert point is last lock of the mode group */
726 prev->res_link = &mode_end->l_res_link;
727 prev->mode_link = &mode_end->l_sl_mode;
728 prev->policy_link = &req->l_sl_policy;
731 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
733 policy_end = list_entry(lock->l_sl_policy.prev,
737 if (lock->l_policy_data.l_inodebits.bits ==
738 req->l_policy_data.l_inodebits.bits) {
739 /* insert point is last lock of
740 * the policy group */
742 &policy_end->l_res_link;
744 &policy_end->l_sl_mode;
746 &policy_end->l_sl_policy;
751 if (policy_end == mode_end)
752 /* done with mode group */
755 /* jump to next policy group within the mode group */
756 tmp = policy_end->l_res_link.next;
757 lock = list_entry(tmp, struct ldlm_lock,
759 } /* loop over policy groups within the mode group */
761 /* insert point is last lock of the mode group,
762 * new policy group is started */
763 prev->res_link = &mode_end->l_res_link;
764 prev->mode_link = &mode_end->l_sl_mode;
765 prev->policy_link = &req->l_sl_policy;
769 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
774 /* insert point is last lock on the queue,
775 * new mode group and new policy group are started */
776 prev->res_link = queue->prev;
777 prev->mode_link = &req->l_sl_mode;
778 prev->policy_link = &req->l_sl_policy;
783 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
784 struct sl_insert_point *prev)
786 struct ldlm_resource *res = lock->l_resource;
789 check_res_locked(res);
791 ldlm_resource_dump(D_INFO, res);
792 CDEBUG(D_OTHER, "About to add this lock:\n");
793 ldlm_lock_dump(D_OTHER, lock, 0);
795 if (lock->l_destroyed) {
796 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
800 LASSERT(list_empty(&lock->l_res_link));
801 LASSERT(list_empty(&lock->l_sl_mode));
802 LASSERT(list_empty(&lock->l_sl_policy));
804 list_add(&lock->l_res_link, prev->res_link);
805 list_add(&lock->l_sl_mode, prev->mode_link);
806 list_add(&lock->l_sl_policy, prev->policy_link);
811 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
813 struct sl_insert_point prev;
816 LASSERT(lock->l_req_mode == lock->l_granted_mode);
818 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
819 ldlm_granted_list_add_lock(lock, &prev);
824 * - ldlm_lock_enqueue
825 * - ldlm_reprocess_queue
826 * - ldlm_lock_convert
828 * must be called with lr_lock held
830 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
832 struct ldlm_resource *res = lock->l_resource;
835 check_res_locked(res);
837 lock->l_granted_mode = lock->l_req_mode;
838 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
839 ldlm_grant_lock_with_skiplist(lock);
840 else if (res->lr_type == LDLM_EXTENT)
841 ldlm_extent_add_lock(res, lock);
843 ldlm_resource_add_lock(res, &res->lr_granted, lock);
845 if (lock->l_granted_mode < res->lr_most_restr)
846 res->lr_most_restr = lock->l_granted_mode;
848 if (work_list && lock->l_completion_ast != NULL)
849 ldlm_add_ast_work_item(lock, NULL, work_list);
851 ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
855 /* returns a referenced lock or NULL. See the flag descriptions below, in the
856 * comment above ldlm_lock_match */
857 static struct ldlm_lock *search_queue(struct list_head *queue,
859 ldlm_policy_data_t *policy,
860 struct ldlm_lock *old_lock, int flags)
862 struct ldlm_lock *lock;
863 struct list_head *tmp;
865 list_for_each(tmp, queue) {
868 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
870 if (lock == old_lock)
873 /* llite sometimes wants to match locks that will be
874 * canceled when their users drop, but we allow it to match
875 * if it passes in CBPENDING and the lock still has users.
876 * this is generally only going to be used by children
877 * whose parents already hold a lock so forward progress
878 * can still happen. */
879 if (lock->l_flags & LDLM_FL_CBPENDING &&
880 !(flags & LDLM_FL_CBPENDING))
882 if (lock->l_flags & LDLM_FL_CBPENDING &&
883 lock->l_readers == 0 && lock->l_writers == 0)
886 if (!(lock->l_req_mode & *mode))
888 match = lock->l_req_mode;
890 if (lock->l_resource->lr_type == LDLM_EXTENT &&
891 (lock->l_policy_data.l_extent.start >
892 policy->l_extent.start ||
893 lock->l_policy_data.l_extent.end < policy->l_extent.end))
896 if (unlikely(match == LCK_GROUP) &&
897 lock->l_resource->lr_type == LDLM_EXTENT &&
898 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
901 /* We match if we have existing lock with same or wider set
903 if (lock->l_resource->lr_type == LDLM_IBITS &&
904 ((lock->l_policy_data.l_inodebits.bits &
905 policy->l_inodebits.bits) !=
906 policy->l_inodebits.bits))
909 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
912 if ((flags & LDLM_FL_LOCAL_ONLY) &&
913 !(lock->l_flags & LDLM_FL_LOCAL))
916 if (flags & LDLM_FL_TEST_LOCK) {
918 ldlm_lock_touch_in_lru(lock);
920 ldlm_lock_addref_internal_nolock(lock, match);
929 void ldlm_lock_allow_match(struct ldlm_lock *lock)
931 lock_res_and_lock(lock);
932 lock->l_flags |= LDLM_FL_LVB_READY;
933 cfs_waitq_signal(&lock->l_waitq);
934 unlock_res_and_lock(lock);
938 * Checks if requested extent lock is compatible with another owned lock.
940 * Checks if \a lock is compatible with a read or write lock
941 * (specified by \a rw) for an extent [\a start , \a end].
943 * \param lock the already owned lock
944 * \param rw OBD_BRW_READ if requested for reading,
945 * OBD_BRW_WRITE if requested for writing
946 * \param start start of the requested extent
947 * \param end end of the requested extent
948 * \param cookie transparent parameter for passing locking context
950 * \post result == 1, *cookie == context, appropriate lock is referenced
952 * \retval 1 owned lock is reused for the request
953 * \retval 0 no lock reused for the request
955 * \see ldlm_lock_fast_release
957 int ldlm_lock_fast_match(struct ldlm_lock *lock, int rw,
958 obd_off start, obd_off end,
961 LASSERT(rw == OBD_BRW_READ || rw == OBD_BRW_WRITE);
966 lock_res_and_lock(lock);
967 /* check if granted mode is compatible */
968 if (rw == OBD_BRW_WRITE &&
969 !(lock->l_granted_mode & (LCK_PW|LCK_GROUP)))
972 /* does the lock cover the region we would like to access? */
973 if ((lock->l_policy_data.l_extent.start > start) ||
974 (lock->l_policy_data.l_extent.end < end))
977 /* if we received a blocking callback and the lock is no longer
978 * referenced, don't use it */
979 if ((lock->l_flags & LDLM_FL_CBPENDING) &&
980 !lock->l_writers && !lock->l_readers)
983 ldlm_lock_addref_internal_nolock(lock, rw == OBD_BRW_WRITE ? LCK_PW : LCK_PR);
984 unlock_res_and_lock(lock);
985 *cookie = (void *)lock;
986 return 1; /* avoid using rc for stack relief */
989 unlock_res_and_lock(lock);
994 * Releases a reference to a lock taken in a "fast" way.
996 * Releases a read or write (specified by \a rw) lock
997 * referenced by \a cookie.
999 * \param rw OBD_BRW_READ if requested for reading,
1000 * OBD_BRW_WRITE if requested for writing
1001 * \param cookie transparent parameter for passing locking context
1003 * \post appropriate lock is dereferenced
1005 * \see ldlm_lock_fast_lock
1007 void ldlm_lock_fast_release(void *cookie, int rw)
1009 struct ldlm_lock *lock = (struct ldlm_lock *)cookie;
1011 LASSERT(lock != NULL);
1012 LASSERT(rw == OBD_BRW_READ || rw == OBD_BRW_WRITE);
1013 LASSERT(rw == OBD_BRW_READ ||
1014 (lock->l_granted_mode & (LCK_PW | LCK_GROUP)));
1015 ldlm_lock_decref_internal(lock, rw == OBD_BRW_WRITE ? LCK_PW : LCK_PR);
1018 /* Can be called in two ways:
1020 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
1021 * for a duplicate of.
1023 * Otherwise, all of the fields must be filled in, to match against.
1025 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
1026 * server (ie, connh is NULL)
1027 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
1028 * list will be considered
1029 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
1030 * to be canceled can still be matched as long as they still have reader
1031 * or writer refernces
1032 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
1033 * just tell us if we would have matched.
1035 * Returns 1 if it finds an already-existing lock that is compatible; in this
1036 * case, lockh is filled in with a addref()ed lock
1038 * we also check security context, if that failed we simply return 0 (to keep
1039 * caller code unchanged), the context failure will be discovered by caller
1042 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
1043 const struct ldlm_res_id *res_id, ldlm_type_t type,
1044 ldlm_policy_data_t *policy, ldlm_mode_t mode,
1045 struct lustre_handle *lockh)
1047 struct ldlm_resource *res;
1048 struct ldlm_lock *lock, *old_lock = NULL;
1053 old_lock = ldlm_handle2lock(lockh);
1056 ns = old_lock->l_resource->lr_namespace;
1057 res_id = &old_lock->l_resource->lr_name;
1058 type = old_lock->l_resource->lr_type;
1059 mode = old_lock->l_req_mode;
1062 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
1064 LASSERT(old_lock == NULL);
1070 lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
1073 if (flags & LDLM_FL_BLOCK_GRANTED)
1075 lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
1078 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
1085 ldlm_resource_putref(res);
1088 ldlm_lock2handle(lock, lockh);
1089 if ((flags & LDLM_FL_LVB_READY) &&
1090 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
1091 struct l_wait_info lwi;
1092 if (lock->l_completion_ast) {
1093 int err = lock->l_completion_ast(lock,
1094 LDLM_FL_WAIT_NOREPROC,
1097 if (flags & LDLM_FL_TEST_LOCK)
1098 LDLM_LOCK_PUT(lock);
1100 ldlm_lock_decref_internal(lock, mode);
1106 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout), NULL,
1107 LWI_ON_SIGNAL_NOOP, NULL);
1109 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1110 l_wait_event(lock->l_waitq,
1111 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1116 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1117 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1118 res_id->name[2] : policy->l_extent.start,
1119 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1120 res_id->name[3] : policy->l_extent.end);
1122 /* check user's security context */
1123 if (lock->l_conn_export &&
1124 sptlrpc_import_check_ctx(
1125 class_exp2cliimp(lock->l_conn_export))) {
1126 if (!(flags & LDLM_FL_TEST_LOCK))
1127 ldlm_lock_decref_internal(lock, mode);
1131 if (flags & LDLM_FL_TEST_LOCK)
1132 LDLM_LOCK_PUT(lock);
1134 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1135 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1136 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1137 type, mode, res_id->name[0], res_id->name[1],
1138 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1139 res_id->name[2] :policy->l_extent.start,
1140 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1141 res_id->name[3] : policy->l_extent.end);
1144 LDLM_LOCK_PUT(old_lock);
1146 return rc ? mode : 0;
1149 /* Returns a referenced lock */
1150 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1151 const struct ldlm_res_id *res_id,
1154 ldlm_blocking_callback blocking,
1155 ldlm_completion_callback completion,
1156 ldlm_glimpse_callback glimpse,
1157 void *data, __u32 lvb_len)
1159 struct ldlm_lock *lock;
1160 struct ldlm_resource *res;
1163 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1167 lock = ldlm_lock_new(res);
1168 ldlm_resource_putref(res);
1173 lock->l_req_mode = mode;
1174 lock->l_ast_data = data;
1175 lock->l_blocking_ast = blocking;
1176 lock->l_completion_ast = completion;
1177 lock->l_glimpse_ast = glimpse;
1178 lock->l_pid = cfs_curproc_pid();
1180 lock->l_tree_node = NULL;
1181 /* if this is the extent lock, allocate the interval tree node */
1182 if (type == LDLM_EXTENT) {
1183 if (ldlm_interval_alloc(lock) == NULL)
1188 lock->l_lvb_len = lvb_len;
1189 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1190 if (lock->l_lvb_data == NULL)
1197 if (lock->l_lvb_data)
1198 OBD_FREE(lock->l_lvb_data, lvb_len);
1199 ldlm_interval_free(ldlm_interval_detach(lock));
1200 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1204 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1205 struct ldlm_lock **lockp,
1206 void *cookie, int *flags)
1208 struct ldlm_lock *lock = *lockp;
1209 struct ldlm_resource *res = lock->l_resource;
1210 int local = ns_is_client(res->lr_namespace);
1211 ldlm_processing_policy policy;
1212 ldlm_error_t rc = ELDLM_OK;
1213 struct ldlm_interval *node = NULL;
1216 do_gettimeofday(&lock->l_enqueued_time);
1217 /* policies are not executed on the client or during replay */
1218 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1219 && !local && ns->ns_policy) {
1220 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1222 if (rc == ELDLM_LOCK_REPLACED) {
1223 /* The lock that was returned has already been granted,
1224 * and placed into lockp. If it's not the same as the
1225 * one we passed in, then destroy the old one and our
1226 * work here is done. */
1227 if (lock != *lockp) {
1228 ldlm_lock_destroy(lock);
1229 LDLM_LOCK_PUT(lock);
1231 *flags |= LDLM_FL_LOCK_CHANGED;
1233 } else if (rc != ELDLM_OK ||
1234 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1235 ldlm_lock_destroy(lock);
1240 /* For a replaying lock, it might be already in granted list. So
1241 * unlinking the lock will cause the interval node to be freed, we
1242 * have to allocate the interval node early otherwise we can't regrant
1243 * this lock in the future. - jay */
1244 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1245 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO,
1248 lock_res_and_lock(lock);
1249 if (local && lock->l_req_mode == lock->l_granted_mode) {
1250 /* The server returned a blocked lock, but it was granted
1251 * before we got a chance to actually enqueue it. We don't
1252 * need to do anything else. */
1253 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1254 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1255 GOTO(out, ELDLM_OK);
1258 ldlm_resource_unlink_lock(lock);
1259 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1261 ldlm_lock_destroy_nolock(lock);
1262 GOTO(out, rc = -ENOMEM);
1265 CFS_INIT_LIST_HEAD(&node->li_group);
1266 ldlm_interval_attach(node, lock);
1270 /* Some flags from the enqueue want to make it into the AST, via the
1271 * lock's l_flags. */
1272 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1274 /* This distinction between local lock trees is very important; a client
1275 * namespace only has information about locks taken by that client, and
1276 * thus doesn't have enough information to decide for itself if it can
1277 * be granted (below). In this case, we do exactly what the server
1278 * tells us to do, as dictated by the 'flags'.
1280 * We do exactly the same thing during recovery, when the server is
1281 * more or less trusting the clients not to lie.
1283 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1284 * granted/converting queues. */
1286 if (*flags & LDLM_FL_BLOCK_CONV)
1287 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1288 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1289 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1291 ldlm_grant_lock(lock, NULL);
1292 GOTO(out, ELDLM_OK);
1293 } else if (*flags & LDLM_FL_REPLAY) {
1294 if (*flags & LDLM_FL_BLOCK_CONV) {
1295 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1296 GOTO(out, ELDLM_OK);
1297 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1298 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1299 GOTO(out, ELDLM_OK);
1300 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1301 ldlm_grant_lock(lock, NULL);
1302 GOTO(out, ELDLM_OK);
1304 /* If no flags, fall through to normal enqueue path. */
1307 policy = ldlm_processing_policy_table[res->lr_type];
1308 policy(lock, flags, 1, &rc, NULL);
1311 unlock_res_and_lock(lock);
1313 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1317 /* Must be called with namespace taken: queue is waiting or converting. */
1318 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1319 struct list_head *work_list)
1321 struct list_head *tmp, *pos;
1322 ldlm_processing_policy policy;
1324 int rc = LDLM_ITER_CONTINUE;
1328 check_res_locked(res);
1330 policy = ldlm_processing_policy_table[res->lr_type];
1333 list_for_each_safe(tmp, pos, queue) {
1334 struct ldlm_lock *pending;
1335 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1337 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1340 rc = policy(pending, &flags, 0, &err, work_list);
1341 if (rc != LDLM_ITER_CONTINUE)
1348 /* Helper function for ldlm_run_ast_work().
1350 * Send an existing rpc set specified by @arg->set and then
1351 * destroy it. Create new one if @do_create flag is set. */
1353 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1357 ptlrpc_set_wait(arg->set);
1358 if (arg->type == LDLM_BL_CALLBACK)
1359 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1360 ptlrpc_set_destroy(arg->set);
1363 arg->set = ptlrpc_prep_set();
1369 ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1371 struct ldlm_lock_desc d;
1372 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
1375 /* nobody should touch l_bl_ast */
1376 lock_res_and_lock(lock);
1377 list_del_init(&lock->l_bl_ast);
1379 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1380 LASSERT(lock->l_bl_ast_run == 0);
1381 LASSERT(lock->l_blocking_lock);
1382 lock->l_bl_ast_run++;
1383 unlock_res_and_lock(lock);
1385 ldlm_lock2desc(lock->l_blocking_lock, &d);
1387 LDLM_LOCK_PUT(lock->l_blocking_lock);
1388 lock->l_blocking_lock = NULL;
1389 lock->l_blocking_ast(lock, &d, (void *)arg,
1391 LDLM_LOCK_PUT(lock);
1397 ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1399 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
1400 ldlm_completion_callback completion_callback;
1404 /* It's possible to receive a completion AST before we've set
1405 * the l_completion_ast pointer: either because the AST arrived
1406 * before the reply, or simply because there's a small race
1407 * window between receiving the reply and finishing the local
1408 * enqueue. (bug 842)
1410 * This can't happen with the blocking_ast, however, because we
1411 * will never call the local blocking_ast until we drop our
1412 * reader/writer reference, which we won't do until we get the
1413 * reply and finish enqueueing. */
1415 /* nobody should touch l_cp_ast */
1416 lock_res_and_lock(lock);
1417 list_del_init(&lock->l_cp_ast);
1418 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1419 /* save l_completion_ast since it can be changed by
1420 * mds_intent_policy(), see bug 14225 */
1421 completion_callback = lock->l_completion_ast;
1422 lock->l_flags &= ~LDLM_FL_CP_REQD;
1423 unlock_res_and_lock(lock);
1425 if (completion_callback != NULL) {
1426 completion_callback(lock, 0, (void *)arg);
1429 LDLM_LOCK_PUT(lock);
1435 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1437 struct ldlm_lock_desc desc;
1438 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_export_chain);
1441 list_del_init(&lock->l_export_chain);
1443 /* the desc just pretend to exclusive */
1444 ldlm_lock2desc(lock, &desc);
1445 desc.l_req_mode = LCK_EX;
1446 desc.l_granted_mode = 0;
1448 lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1449 LDLM_LOCK_PUT(lock);
1454 int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
1456 struct ldlm_cb_set_arg arg;
1457 struct list_head *tmp, *pos;
1458 int (*work_ast_lock)(struct list_head *tmp, struct ldlm_cb_set_arg *arg);
1462 arg.set = ptlrpc_prep_set();
1463 atomic_set(&arg.restart, 0);
1465 case LDLM_WORK_BL_AST:
1466 arg.type = LDLM_BL_CALLBACK;
1467 work_ast_lock = ldlm_work_bl_ast_lock;
1469 case LDLM_WORK_CP_AST:
1470 arg.type = LDLM_CP_CALLBACK;
1471 work_ast_lock = ldlm_work_cp_ast_lock;
1473 case LDLM_WORK_REVOKE_AST:
1474 arg.type = LDLM_BL_CALLBACK;
1475 work_ast_lock = ldlm_work_revoke_ast_lock;
1482 list_for_each_safe(tmp, pos, rpc_list) {
1483 ast_count += work_ast_lock(tmp, &arg);
1485 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1486 * and create a new set for requests that remained in
1488 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1489 ldlm_send_and_maybe_create_set(&arg, 1);
1495 ldlm_send_and_maybe_create_set(&arg, 0);
1497 /* In case when number of ASTs is multiply of
1498 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1499 * @arg.set must be destroyed here, otherwise we get
1500 * write memory leaking. */
1501 ptlrpc_set_destroy(arg.set);
1503 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1506 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1508 ldlm_reprocess_all(res);
1509 return LDLM_ITER_CONTINUE;
1512 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1514 struct list_head *tmp;
1521 spin_lock(&ns->ns_hash_lock);
1522 for (i = 0; i < RES_HASH_SIZE; i++) {
1523 tmp = ns->ns_hash[i].next;
1524 while (tmp != &(ns->ns_hash[i])) {
1525 struct ldlm_resource *res =
1526 list_entry(tmp, struct ldlm_resource, lr_hash);
1528 ldlm_resource_getref(res);
1529 spin_unlock(&ns->ns_hash_lock);
1531 rc = reprocess_one_queue(res, NULL);
1533 spin_lock(&ns->ns_hash_lock);
1535 ldlm_resource_putref_locked(res);
1537 if (rc == LDLM_ITER_STOP)
1542 spin_unlock(&ns->ns_hash_lock);
1546 void ldlm_reprocess_all(struct ldlm_resource *res)
1548 CFS_LIST_HEAD(rpc_list);
1552 /* Local lock trees don't get reprocessed. */
1553 if (ns_is_client(res->lr_namespace)) {
1560 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1561 if (rc == LDLM_ITER_CONTINUE)
1562 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1565 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1566 if (rc == -ERESTART) {
1567 LASSERT(list_empty(&rpc_list));
1573 void ldlm_cancel_callback(struct ldlm_lock *lock)
1575 check_res_locked(lock->l_resource);
1576 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1577 lock->l_flags |= LDLM_FL_CANCEL;
1578 if (lock->l_blocking_ast) {
1579 // l_check_no_ns_lock(ns);
1580 unlock_res_and_lock(lock);
1581 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1583 lock_res_and_lock(lock);
1585 LDLM_DEBUG(lock, "no blocking ast");
1588 lock->l_flags |= LDLM_FL_BL_DONE;
1591 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1593 if (req->l_resource->lr_type != LDLM_PLAIN &&
1594 req->l_resource->lr_type != LDLM_IBITS)
1597 list_del_init(&req->l_sl_policy);
1598 list_del_init(&req->l_sl_mode);
1601 void ldlm_lock_cancel(struct ldlm_lock *lock)
1603 struct ldlm_resource *res;
1604 struct ldlm_namespace *ns;
1607 lock_res_and_lock(lock);
1609 res = lock->l_resource;
1610 ns = res->lr_namespace;
1612 /* Please do not, no matter how tempting, remove this LBUG without
1613 * talking to me first. -phik */
1614 if (lock->l_readers || lock->l_writers) {
1615 LDLM_ERROR(lock, "lock still has references");
1619 ldlm_del_waiting_lock(lock);
1621 /* Releases cancel callback. */
1622 ldlm_cancel_callback(lock);
1624 /* Yes, second time, just in case it was added again while we were
1625 running with no res lock in ldlm_cancel_callback */
1626 ldlm_del_waiting_lock(lock);
1627 ldlm_resource_unlink_lock(lock);
1628 ldlm_lock_destroy_nolock(lock);
1630 if (lock->l_granted_mode == lock->l_req_mode)
1631 ldlm_pool_del(&ns->ns_pool, lock);
1633 /* Make sure we will not be called again for same lock what is possible
1634 * if not to zero out lock->l_granted_mode */
1635 lock->l_granted_mode = 0;
1636 unlock_res_and_lock(lock);
1641 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1643 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1649 lock->l_ast_data = data;
1650 LDLM_LOCK_PUT(lock);
1654 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1656 struct ldlm_lock *lock;
1657 struct ldlm_resource *res;
1659 spin_lock(&exp->exp_ldlm_data.led_lock);
1660 while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
1661 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1662 struct ldlm_lock, l_export_chain);
1663 res = ldlm_resource_getref(lock->l_resource);
1664 LDLM_LOCK_GET(lock);
1665 spin_unlock(&exp->exp_ldlm_data.led_lock);
1667 LDLM_DEBUG(lock, "export %p", exp);
1668 ldlm_res_lvbo_update(res, NULL, 0, 1);
1670 ldlm_lock_cancel(lock);
1671 ldlm_reprocess_all(res);
1673 ldlm_resource_putref(res);
1674 LDLM_LOCK_PUT(lock);
1675 spin_lock(&exp->exp_ldlm_data.led_lock);
1677 spin_unlock(&exp->exp_ldlm_data.led_lock);
1680 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1683 CFS_LIST_HEAD(rpc_list);
1684 struct ldlm_resource *res;
1685 struct ldlm_namespace *ns;
1688 struct sl_insert_point prev;
1690 struct ldlm_interval *node;
1693 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1694 *flags |= LDLM_FL_BLOCK_GRANTED;
1695 RETURN(lock->l_resource);
1698 /* I can't check the type of lock here because the bitlock of lock
1699 * is not held here, so do the allocation blindly. -jay */
1700 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
1701 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1704 LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1705 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1707 lock_res_and_lock(lock);
1709 res = lock->l_resource;
1710 ns = res->lr_namespace;
1712 old_mode = lock->l_req_mode;
1713 lock->l_req_mode = new_mode;
1714 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1715 /* remember the lock position where the lock might be
1716 * added back to the granted list later and also
1717 * remember the join mode for skiplist fixing. */
1718 prev.res_link = lock->l_res_link.prev;
1719 prev.mode_link = lock->l_sl_mode.prev;
1720 prev.policy_link = lock->l_sl_policy.prev;
1721 ldlm_resource_unlink_lock(lock);
1723 ldlm_resource_unlink_lock(lock);
1724 if (res->lr_type == LDLM_EXTENT) {
1725 /* FIXME: ugly code, I have to attach the lock to a
1726 * interval node again since perhaps it will be granted
1728 CFS_INIT_LIST_HEAD(&node->li_group);
1729 ldlm_interval_attach(node, lock);
1734 /* If this is a local resource, put it on the appropriate list. */
1735 if (ns_is_client(res->lr_namespace)) {
1736 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1737 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1739 /* This should never happen, because of the way the
1740 * server handles conversions. */
1741 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1745 ldlm_grant_lock(lock, &rpc_list);
1747 /* FIXME: completion handling not with ns_lock held ! */
1748 if (lock->l_completion_ast)
1749 lock->l_completion_ast(lock, 0, NULL);
1753 ldlm_processing_policy policy;
1754 policy = ldlm_processing_policy_table[res->lr_type];
1755 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1756 if (rc == LDLM_ITER_STOP) {
1757 lock->l_req_mode = old_mode;
1758 if (res->lr_type == LDLM_EXTENT)
1759 ldlm_extent_add_lock(res, lock);
1761 ldlm_granted_list_add_lock(lock, &prev);
1765 *flags |= LDLM_FL_BLOCK_GRANTED;
1769 unlock_res_and_lock(lock);
1772 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1774 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1778 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1780 struct obd_device *obd = NULL;
1782 if (!((libcfs_debug | D_ERROR) & level))
1786 CDEBUG(level, " NULL LDLM lock\n");
1790 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1791 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1793 if (lock->l_conn_export != NULL)
1794 obd = lock->l_conn_export->exp_obd;
1795 if (lock->l_export && lock->l_export->exp_connection) {
1796 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1797 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1798 lock->l_remote_handle.cookie);
1799 } else if (obd == NULL) {
1800 CDEBUG(level, " Node: local\n");
1802 struct obd_import *imp = obd->u.cli.cl_import;
1803 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1804 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1805 lock->l_remote_handle.cookie);
1807 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1809 lock->l_resource->lr_name.name[0],
1810 lock->l_resource->lr_name.name[1],
1811 lock->l_resource->lr_name.name[2]);
1812 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1813 "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
1814 ldlm_lockname[lock->l_granted_mode],
1815 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1817 if (lock->l_resource->lr_type == LDLM_EXTENT)
1818 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1819 " (req "LPU64"-"LPU64")\n",
1820 lock->l_policy_data.l_extent.start,
1821 lock->l_policy_data.l_extent.end,
1822 lock->l_req_extent.start, lock->l_req_extent.end);
1823 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1824 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1825 lock->l_policy_data.l_flock.pid,
1826 lock->l_policy_data.l_flock.start,
1827 lock->l_policy_data.l_flock.end);
1828 else if (lock->l_resource->lr_type == LDLM_IBITS)
1829 CDEBUG(level, " Bits: "LPX64"\n",
1830 lock->l_policy_data.l_inodebits.bits);
1833 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1835 struct ldlm_lock *lock;
1837 if (!((libcfs_debug | D_ERROR) & level))
1840 lock = ldlm_handle2lock(lockh);
1844 ldlm_lock_dump(D_OTHER, lock, 0);
1846 LDLM_LOCK_PUT(lock);
1849 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1850 struct libcfs_debug_msg_data *data, const char *fmt,
1854 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1856 va_start(args, fmt);
1858 if (lock->l_resource == NULL) {
1859 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1860 data->msg_fn, data->msg_line, fmt, args,
1861 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1862 "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
1863 LPX64" expref: %d pid: %u\n", lock,
1864 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1865 lock->l_readers, lock->l_writers,
1866 ldlm_lockname[lock->l_granted_mode],
1867 ldlm_lockname[lock->l_req_mode],
1868 lock->l_flags, lock->l_remote_handle.cookie,
1870 atomic_read(&lock->l_export->exp_refcount) : -99,
1876 switch (lock->l_resource->lr_type) {
1878 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1879 data->msg_fn, data->msg_line, fmt, args,
1880 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1881 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1882 "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
1883 " expref: %d pid: %u\n",
1884 lock->l_resource->lr_namespace->ns_name, lock,
1885 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1886 lock->l_readers, lock->l_writers,
1887 ldlm_lockname[lock->l_granted_mode],
1888 ldlm_lockname[lock->l_req_mode],
1889 lock->l_resource->lr_name.name[0],
1890 lock->l_resource->lr_name.name[1],
1891 atomic_read(&lock->l_resource->lr_refcount),
1892 ldlm_typename[lock->l_resource->lr_type],
1893 lock->l_policy_data.l_extent.start,
1894 lock->l_policy_data.l_extent.end,
1895 lock->l_req_extent.start, lock->l_req_extent.end,
1896 lock->l_flags, lock->l_remote_handle.cookie,
1898 atomic_read(&lock->l_export->exp_refcount) : -99,
1903 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1904 data->msg_fn, data->msg_line, fmt, args,
1905 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1906 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1907 "["LPU64"->"LPU64"] flags: %x remote: "LPX64
1908 " expref: %d pid: %u\n",
1909 lock->l_resource->lr_namespace->ns_name, lock,
1910 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1911 lock->l_readers, lock->l_writers,
1912 ldlm_lockname[lock->l_granted_mode],
1913 ldlm_lockname[lock->l_req_mode],
1914 lock->l_resource->lr_name.name[0],
1915 lock->l_resource->lr_name.name[1],
1916 atomic_read(&lock->l_resource->lr_refcount),
1917 ldlm_typename[lock->l_resource->lr_type],
1918 lock->l_policy_data.l_flock.pid,
1919 lock->l_policy_data.l_flock.start,
1920 lock->l_policy_data.l_flock.end,
1921 lock->l_flags, lock->l_remote_handle.cookie,
1923 atomic_read(&lock->l_export->exp_refcount) : -99,
1928 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1929 data->msg_fn, data->msg_line, fmt, args,
1930 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1931 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1932 "flags: %x remote: "LPX64" expref: %d "
1934 lock->l_resource->lr_namespace->ns_name,
1935 lock, lock->l_handle.h_cookie,
1936 atomic_read (&lock->l_refc),
1937 lock->l_readers, lock->l_writers,
1938 ldlm_lockname[lock->l_granted_mode],
1939 ldlm_lockname[lock->l_req_mode],
1940 lock->l_resource->lr_name.name[0],
1941 lock->l_resource->lr_name.name[1],
1942 lock->l_policy_data.l_inodebits.bits,
1943 atomic_read(&lock->l_resource->lr_refcount),
1944 ldlm_typename[lock->l_resource->lr_type],
1945 lock->l_flags, lock->l_remote_handle.cookie,
1947 atomic_read(&lock->l_export->exp_refcount) : -99,
1952 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1953 data->msg_fn, data->msg_line, fmt, args,
1954 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1955 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
1956 "remote: "LPX64" expref: %d pid: %u\n",
1957 lock->l_resource->lr_namespace->ns_name,
1958 lock, lock->l_handle.h_cookie,
1959 atomic_read (&lock->l_refc),
1960 lock->l_readers, lock->l_writers,
1961 ldlm_lockname[lock->l_granted_mode],
1962 ldlm_lockname[lock->l_req_mode],
1963 lock->l_resource->lr_name.name[0],
1964 lock->l_resource->lr_name.name[1],
1965 atomic_read(&lock->l_resource->lr_refcount),
1966 ldlm_typename[lock->l_resource->lr_type],
1967 lock->l_flags, lock->l_remote_handle.cookie,
1969 atomic_read(&lock->l_export->exp_refcount) : -99,
1975 EXPORT_SYMBOL(_ldlm_lock_debug);