1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5 * Author: Peter Braam <braam@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of the Lustre file system, http://www.lustre.org
9 * Lustre is a trademark of Cluster File Systems, Inc.
11 * You may have signed or agreed to another license before downloading
12 * this software. If so, you are bound by the terms and conditions
13 * of that agreement, and the following does not apply to you. See the
14 * LICENSE file included with this distribution for more information.
16 * If you did not agree to a different license, then this copy of Lustre
17 * is open source software; you can redistribute it and/or modify it
18 * under the terms of version 2 of the GNU General Public License as
19 * published by the Free Software Foundation.
21 * In either case, Lustre is distributed in the hope that it will be
22 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * license text for more details.
27 #define DEBUG_SUBSYSTEM S_LDLM
30 # include <libcfs/libcfs.h>
31 # ifndef HAVE_VFS_INTENT_PATCHES
32 # include <linux/lustre_intent.h>
35 # include <liblustre.h>
36 # include <libcfs/kp30.h>
39 #include <obd_class.h>
40 #include "ldlm_internal.h"
43 char *ldlm_lockname[] = {
54 char *ldlm_typename[] = {
61 char *ldlm_it2str(int it)
68 case (IT_OPEN | IT_CREAT):
81 CERROR("Unknown intent %d\n", it);
86 extern cfs_mem_cache_t *ldlm_lock_slab;
88 static ldlm_processing_policy ldlm_processing_policy_table[] = {
89 [LDLM_PLAIN] ldlm_process_plain_lock,
90 [LDLM_EXTENT] ldlm_process_extent_lock,
92 [LDLM_FLOCK] ldlm_process_flock_lock,
94 [LDLM_IBITS] ldlm_process_inodebits_lock,
97 ldlm_processing_policy ldlm_get_processing_policy(struct ldlm_resource *res)
99 return ldlm_processing_policy_table[res->lr_type];
102 void ldlm_register_intent(struct ldlm_namespace *ns, ldlm_res_policy arg)
108 * REFCOUNTED LOCK OBJECTS
113 * Lock refcounts, during creation:
114 * - one special one for allocation, dec'd only once in destroy
115 * - one for being a lock that's in-use
116 * - one for the addref associated with a new lock
118 struct ldlm_lock *ldlm_lock_get(struct ldlm_lock *lock)
120 atomic_inc(&lock->l_refc);
124 static void ldlm_lock_free(struct ldlm_lock *lock, size_t size)
126 LASSERT(size == sizeof(*lock));
127 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
130 void ldlm_lock_put(struct ldlm_lock *lock)
134 LASSERT(lock->l_resource != LP_POISON);
135 LASSERT(atomic_read(&lock->l_refc) > 0);
136 if (atomic_dec_and_test(&lock->l_refc)) {
137 struct ldlm_resource *res;
140 "final lock_put on destroyed lock, freeing it.");
142 res = lock->l_resource;
143 LASSERT(lock->l_destroyed);
144 LASSERT(list_empty(&lock->l_res_link));
145 LASSERT(list_empty(&lock->l_pending_chain));
147 atomic_dec(&res->lr_namespace->ns_locks);
148 ldlm_resource_putref(res);
149 lock->l_resource = NULL;
150 if (lock->l_export) {
151 class_export_put(lock->l_export);
152 lock->l_export = NULL;
155 if (lock->l_lvb_data != NULL)
156 OBD_FREE(lock->l_lvb_data, lock->l_lvb_len);
158 ldlm_interval_free(ldlm_interval_detach(lock));
159 OBD_FREE_RCU_CB(lock, sizeof(*lock), &lock->l_handle,
166 int ldlm_lock_remove_from_lru_nolock(struct ldlm_lock *lock)
169 if (!list_empty(&lock->l_lru)) {
170 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
171 LASSERT(lock->l_resource->lr_type != LDLM_FLOCK);
172 list_del_init(&lock->l_lru);
174 LASSERT(ns->ns_nr_unused >= 0);
180 int ldlm_lock_remove_from_lru(struct ldlm_lock *lock)
182 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
185 spin_lock(&ns->ns_unused_lock);
186 rc = ldlm_lock_remove_from_lru_nolock(lock);
187 spin_unlock(&ns->ns_unused_lock);
192 void ldlm_lock_add_to_lru_nolock(struct ldlm_lock *lock)
194 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
195 lock->l_last_used = cfs_time_current();
196 LASSERT(list_empty(&lock->l_lru));
197 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
198 LASSERT(ns->ns_nr_unused >= 0);
202 void ldlm_lock_add_to_lru(struct ldlm_lock *lock)
204 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
206 spin_lock(&ns->ns_unused_lock);
207 ldlm_lock_add_to_lru_nolock(lock);
208 spin_unlock(&ns->ns_unused_lock);
212 void ldlm_lock_touch_in_lru(struct ldlm_lock *lock)
214 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
216 spin_lock(&ns->ns_unused_lock);
217 if (!list_empty(&lock->l_lru)) {
218 ldlm_lock_remove_from_lru_nolock(lock);
219 ldlm_lock_add_to_lru_nolock(lock);
221 spin_unlock(&ns->ns_unused_lock);
225 /* This used to have a 'strict' flag, which recovery would use to mark an
226 * in-use lock as needing-to-die. Lest I am ever tempted to put it back, I
227 * shall explain why it's gone: with the new hash table scheme, once you call
228 * ldlm_lock_destroy, you can never drop your final references on this lock.
229 * Because it's not in the hash table anymore. -phil */
230 int ldlm_lock_destroy_internal(struct ldlm_lock *lock)
234 if (lock->l_readers || lock->l_writers) {
235 LDLM_ERROR(lock, "lock still has references");
236 ldlm_lock_dump(D_ERROR, lock, 0);
240 if (!list_empty(&lock->l_res_link)) {
241 LDLM_ERROR(lock, "lock still on resource");
242 ldlm_lock_dump(D_ERROR, lock, 0);
246 if (lock->l_destroyed) {
247 LASSERT(list_empty(&lock->l_lru));
251 lock->l_destroyed = 1;
254 spin_lock(&lock->l_export->exp_ldlm_data.led_lock);
255 list_del_init(&lock->l_export_chain);
257 spin_unlock(&lock->l_export->exp_ldlm_data.led_lock);
259 ldlm_lock_remove_from_lru(lock);
260 class_handle_unhash(&lock->l_handle);
263 /* Wake anyone waiting for this lock */
264 /* FIXME: I should probably add yet another flag, instead of using
265 * l_export to only call this on clients */
267 class_export_put(lock->l_export);
268 lock->l_export = NULL;
269 if (lock->l_export && lock->l_completion_ast)
270 lock->l_completion_ast(lock, 0);
276 void ldlm_lock_destroy(struct ldlm_lock *lock)
280 lock_res_and_lock(lock);
281 first = ldlm_lock_destroy_internal(lock);
282 unlock_res_and_lock(lock);
284 /* drop reference from hashtable only for first destroy */
290 void ldlm_lock_destroy_nolock(struct ldlm_lock *lock)
294 first = ldlm_lock_destroy_internal(lock);
295 /* drop reference from hashtable only for first destroy */
301 /* this is called by portals_handle2object with the handle lock taken */
302 static void lock_handle_addref(void *lock)
304 LDLM_LOCK_GET((struct ldlm_lock *)lock);
308 * usage: pass in a resource on which you have done ldlm_resource_get
309 * pass in a parent lock on which you have done a ldlm_lock_get
310 * after return, ldlm_*_put the resource and parent
311 * returns: lock with refcount 2 - one for current caller and one for remote
313 static struct ldlm_lock *ldlm_lock_new(struct ldlm_resource *resource)
315 struct ldlm_lock *lock;
318 if (resource == NULL)
321 OBD_SLAB_ALLOC(lock, ldlm_lock_slab, CFS_ALLOC_IO, sizeof(*lock));
325 spin_lock_init(&lock->l_lock);
326 lock->l_resource = ldlm_resource_getref(resource);
328 atomic_set(&lock->l_refc, 2);
329 CFS_INIT_LIST_HEAD(&lock->l_res_link);
330 CFS_INIT_LIST_HEAD(&lock->l_lru);
331 CFS_INIT_LIST_HEAD(&lock->l_export_chain);
332 CFS_INIT_LIST_HEAD(&lock->l_pending_chain);
333 CFS_INIT_LIST_HEAD(&lock->l_bl_ast);
334 CFS_INIT_LIST_HEAD(&lock->l_cp_ast);
335 cfs_waitq_init(&lock->l_waitq);
336 lock->l_blocking_lock = NULL;
337 CFS_INIT_LIST_HEAD(&lock->l_sl_mode);
338 CFS_INIT_LIST_HEAD(&lock->l_sl_policy);
340 atomic_inc(&resource->lr_namespace->ns_locks);
341 CFS_INIT_LIST_HEAD(&lock->l_handle.h_link);
342 class_handle_hash(&lock->l_handle, lock_handle_addref);
344 CFS_INIT_LIST_HEAD(&lock->l_extents_list);
345 spin_lock_init(&lock->l_extents_list_lock);
346 CFS_INIT_LIST_HEAD(&lock->l_cache_locks_list);
351 int ldlm_lock_change_resource(struct ldlm_namespace *ns, struct ldlm_lock *lock,
352 const struct ldlm_res_id *new_resid)
354 struct ldlm_resource *oldres = lock->l_resource;
355 struct ldlm_resource *newres;
359 LASSERT(ns_is_client(ns));
361 lock_res_and_lock(lock);
362 if (memcmp(new_resid, &lock->l_resource->lr_name,
363 sizeof(lock->l_resource->lr_name)) == 0) {
365 unlock_res_and_lock(lock);
369 LASSERT(new_resid->name[0] != 0);
371 /* This function assumes that the lock isn't on any lists */
372 LASSERT(list_empty(&lock->l_res_link));
374 type = oldres->lr_type;
375 unlock_res_and_lock(lock);
377 newres = ldlm_resource_get(ns, NULL, new_resid, type, 1);
381 lock_res_and_lock(lock);
382 LASSERT(memcmp(new_resid, &lock->l_resource->lr_name,
383 sizeof(lock->l_resource->lr_name)) != 0);
385 lock->l_resource = newres;
387 unlock_res_and_lock(lock);
389 /* ...and the flowers are still standing! */
390 ldlm_resource_putref(oldres);
399 void ldlm_lock2handle(const struct ldlm_lock *lock, struct lustre_handle *lockh)
401 lockh->cookie = lock->l_handle.h_cookie;
404 /* if flags: atomically get the lock and set the flags.
405 * Return NULL if flag already set
408 struct ldlm_lock *__ldlm_handle2lock(const struct lustre_handle *handle,
411 struct ldlm_namespace *ns;
412 struct ldlm_lock *lock, *retval = NULL;
417 lock = class_handle2object(handle->cookie);
421 LASSERT(lock->l_resource != NULL);
422 ns = lock->l_resource->lr_namespace;
425 lock_res_and_lock(lock);
427 /* It's unlikely but possible that someone marked the lock as
428 * destroyed after we did handle2object on it */
429 if (lock->l_destroyed) {
430 unlock_res_and_lock(lock);
431 CDEBUG(D_INFO, "lock already destroyed: lock %p\n", lock);
436 if (flags && (lock->l_flags & flags)) {
437 unlock_res_and_lock(lock);
443 lock->l_flags |= flags;
445 unlock_res_and_lock(lock);
452 struct ldlm_lock *ldlm_handle2lock_ns(struct ldlm_namespace *ns,
453 const struct lustre_handle *handle)
455 struct ldlm_lock *retval = NULL;
456 retval = __ldlm_handle2lock(handle, 0);
460 void ldlm_lock2desc(struct ldlm_lock *lock, struct ldlm_lock_desc *desc)
462 struct obd_export *exp = lock->l_export?:lock->l_conn_export;
463 /* INODEBITS_INTEROP: If the other side does not support
464 * inodebits, reply with a plain lock descriptor.
466 if ((lock->l_resource->lr_type == LDLM_IBITS) &&
467 (exp && !(exp->exp_connect_flags & OBD_CONNECT_IBITS))) {
468 struct ldlm_resource res = *lock->l_resource;
470 /* Make sure all the right bits are set in this lock we
471 are going to pass to client */
472 LASSERTF(lock->l_policy_data.l_inodebits.bits ==
473 (MDS_INODELOCK_LOOKUP|MDS_INODELOCK_UPDATE),
474 "Inappropriate inode lock bits during "
475 "conversion " LPU64 "\n",
476 lock->l_policy_data.l_inodebits.bits);
477 res.lr_type = LDLM_PLAIN;
478 ldlm_res2desc(&res, &desc->l_resource);
479 /* Convert "new" lock mode to something old client can
481 if ((lock->l_req_mode == LCK_CR) ||
482 (lock->l_req_mode == LCK_CW))
483 desc->l_req_mode = LCK_PR;
485 desc->l_req_mode = lock->l_req_mode;
486 if ((lock->l_granted_mode == LCK_CR) ||
487 (lock->l_granted_mode == LCK_CW)) {
488 desc->l_granted_mode = LCK_PR;
490 /* We never grant PW/EX locks to clients */
491 LASSERT((lock->l_granted_mode != LCK_PW) &&
492 (lock->l_granted_mode != LCK_EX));
493 desc->l_granted_mode = lock->l_granted_mode;
496 /* We do not copy policy here, because there is no
497 policy for plain locks */
499 ldlm_res2desc(lock->l_resource, &desc->l_resource);
500 desc->l_req_mode = lock->l_req_mode;
501 desc->l_granted_mode = lock->l_granted_mode;
502 desc->l_policy_data = lock->l_policy_data;
506 void ldlm_add_bl_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
507 struct list_head *work_list)
509 if ((lock->l_flags & LDLM_FL_AST_SENT) == 0) {
510 LDLM_DEBUG(lock, "lock incompatible; sending blocking AST.");
511 lock->l_flags |= LDLM_FL_AST_SENT;
512 /* If the enqueuing client said so, tell the AST recipient to
513 * discard dirty data, rather than writing back. */
514 if (new->l_flags & LDLM_AST_DISCARD_DATA)
515 lock->l_flags |= LDLM_FL_DISCARD_DATA;
516 LASSERT(list_empty(&lock->l_bl_ast));
517 list_add(&lock->l_bl_ast, work_list);
519 LASSERT(lock->l_blocking_lock == NULL);
520 lock->l_blocking_lock = LDLM_LOCK_GET(new);
524 void ldlm_add_cp_work_item(struct ldlm_lock *lock, struct list_head *work_list)
526 if ((lock->l_flags & LDLM_FL_CP_REQD) == 0) {
527 lock->l_flags |= LDLM_FL_CP_REQD;
528 LDLM_DEBUG(lock, "lock granted; sending completion AST.");
529 LASSERT(list_empty(&lock->l_cp_ast));
530 list_add(&lock->l_cp_ast, work_list);
535 /* must be called with lr_lock held */
536 void ldlm_add_ast_work_item(struct ldlm_lock *lock, struct ldlm_lock *new,
537 struct list_head *work_list)
540 check_res_locked(lock->l_resource);
542 ldlm_add_bl_work_item(lock, new, work_list);
544 ldlm_add_cp_work_item(lock, work_list);
548 void ldlm_lock_addref(struct lustre_handle *lockh, __u32 mode)
550 struct ldlm_lock *lock;
552 lock = ldlm_handle2lock(lockh);
553 LASSERT(lock != NULL);
554 ldlm_lock_addref_internal(lock, mode);
558 void ldlm_lock_addref_internal_nolock(struct ldlm_lock *lock, __u32 mode)
560 ldlm_lock_remove_from_lru(lock);
561 if (mode & (LCK_NL | LCK_CR | LCK_PR))
563 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP))
566 LDLM_DEBUG(lock, "ldlm_lock_addref(%s)", ldlm_lockname[mode]);
569 /* only called for local locks */
570 void ldlm_lock_addref_internal(struct ldlm_lock *lock, __u32 mode)
572 lock_res_and_lock(lock);
573 ldlm_lock_addref_internal_nolock(lock, mode);
574 unlock_res_and_lock(lock);
577 void ldlm_lock_decref_internal(struct ldlm_lock *lock, __u32 mode)
579 struct ldlm_namespace *ns;
582 lock_res_and_lock(lock);
584 ns = lock->l_resource->lr_namespace;
586 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
587 if (mode & (LCK_NL | LCK_CR | LCK_PR)) {
588 LASSERT(lock->l_readers > 0);
591 if (mode & (LCK_EX | LCK_CW | LCK_PW | LCK_GROUP)) {
592 LASSERT(lock->l_writers > 0);
596 if (lock->l_flags & LDLM_FL_LOCAL &&
597 !lock->l_readers && !lock->l_writers) {
598 /* If this is a local lock on a server namespace and this was
599 * the last reference, cancel the lock. */
600 CDEBUG(D_INFO, "forcing cancel of local lock\n");
601 lock->l_flags |= LDLM_FL_CBPENDING;
604 if (!lock->l_readers && !lock->l_writers &&
605 (lock->l_flags & LDLM_FL_CBPENDING)) {
606 /* If we received a blocked AST and this was the last reference,
607 * run the callback. */
608 if (ns_is_server(ns) && lock->l_export)
609 CERROR("FL_CBPENDING set on non-local lock--just a "
612 LDLM_DEBUG(lock, "final decref done on cbpending lock");
614 LDLM_LOCK_GET(lock); /* dropped by bl thread */
615 ldlm_lock_remove_from_lru(lock);
616 unlock_res_and_lock(lock);
617 if ((lock->l_flags & LDLM_FL_ATOMIC_CB) ||
618 ldlm_bl_to_thread_lock(ns, NULL, lock) != 0)
619 ldlm_handle_bl_callback(ns, NULL, lock);
620 } else if (ns_is_client(ns) &&
621 !lock->l_readers && !lock->l_writers &&
622 !(lock->l_flags & LDLM_FL_NO_LRU) &&
623 !(lock->l_flags & LDLM_FL_BL_AST)) {
624 /* If this is a client-side namespace and this was the last
625 * reference, put it on the LRU. */
626 ldlm_lock_add_to_lru(lock);
627 unlock_res_and_lock(lock);
628 /* Call ldlm_cancel_lru() only if EARLY_CANCEL and LRU RESIZE
629 * are not supported by the server, otherwise, it is done on
631 if (!exp_connect_cancelset(lock->l_conn_export) &&
632 !ns_connect_lru_resize(ns))
633 ldlm_cancel_lru(ns, 0, LDLM_ASYNC, 0);
635 unlock_res_and_lock(lock);
638 LDLM_LOCK_PUT(lock); /* matches the ldlm_lock_get in addref */
643 void ldlm_lock_decref(struct lustre_handle *lockh, __u32 mode)
645 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
646 LASSERTF(lock != NULL, "Non-existing lock: "LPX64"\n", lockh->cookie);
647 ldlm_lock_decref_internal(lock, mode);
651 /* This will drop a lock reference and mark it for destruction, but will not
652 * necessarily cancel the lock before returning. */
653 void ldlm_lock_decref_and_cancel(struct lustre_handle *lockh, __u32 mode)
655 struct ldlm_lock *lock = __ldlm_handle2lock(lockh, 0);
658 LASSERT(lock != NULL);
660 LDLM_DEBUG(lock, "ldlm_lock_decref(%s)", ldlm_lockname[mode]);
661 lock_res_and_lock(lock);
662 lock->l_flags |= LDLM_FL_CBPENDING;
663 unlock_res_and_lock(lock);
664 ldlm_lock_decref_internal(lock, mode);
668 struct sl_insert_point {
669 struct list_head *res_link;
670 struct list_head *mode_link;
671 struct list_head *policy_link;
675 * search_granted_lock
678 * Finds a position to insert the new lock.
680 * queue [input]: the granted list where search acts on;
681 * req [input]: the lock whose position to be located;
682 * prev [output]: positions within 3 lists to insert @req to
686 * - ldlm_grant_lock_with_skiplist
688 static void search_granted_lock(struct list_head *queue,
689 struct ldlm_lock *req,
690 struct sl_insert_point *prev)
692 struct list_head *tmp;
693 struct ldlm_lock *lock, *mode_end, *policy_end;
696 list_for_each(tmp, queue) {
697 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
699 mode_end = list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
702 if (lock->l_req_mode != req->l_req_mode) {
703 /* jump to last lock of mode group */
704 tmp = &mode_end->l_res_link;
708 /* suitable mode group is found */
709 if (lock->l_resource->lr_type == LDLM_PLAIN) {
710 /* insert point is last lock of the mode group */
711 prev->res_link = &mode_end->l_res_link;
712 prev->mode_link = &mode_end->l_sl_mode;
713 prev->policy_link = &req->l_sl_policy;
716 } else if (lock->l_resource->lr_type == LDLM_IBITS) {
718 policy_end = list_entry(lock->l_sl_policy.prev,
722 if (lock->l_policy_data.l_inodebits.bits ==
723 req->l_policy_data.l_inodebits.bits) {
724 /* insert point is last lock of
725 * the policy group */
727 &policy_end->l_res_link;
729 &policy_end->l_sl_mode;
731 &policy_end->l_sl_policy;
736 if (policy_end == mode_end)
737 /* done with mode group */
740 /* jump to next policy group within the mode group */
741 tmp = policy_end->l_res_link.next;
742 lock = list_entry(tmp, struct ldlm_lock,
744 } /* loop over policy groups within the mode group */
746 /* insert point is last lock of the mode group,
747 * new policy group is started */
748 prev->res_link = &mode_end->l_res_link;
749 prev->mode_link = &mode_end->l_sl_mode;
750 prev->policy_link = &req->l_sl_policy;
754 LDLM_ERROR(lock, "is not LDLM_PLAIN or LDLM_IBITS lock");
759 /* insert point is last lock on the queue,
760 * new mode group and new policy group are started */
761 prev->res_link = queue->prev;
762 prev->mode_link = &req->l_sl_mode;
763 prev->policy_link = &req->l_sl_policy;
768 static void ldlm_granted_list_add_lock(struct ldlm_lock *lock,
769 struct sl_insert_point *prev)
771 struct ldlm_resource *res = lock->l_resource;
774 check_res_locked(res);
776 ldlm_resource_dump(D_OTHER, res);
777 CDEBUG(D_OTHER, "About to add this lock:\n");
778 ldlm_lock_dump(D_OTHER, lock, 0);
780 if (lock->l_destroyed) {
781 CDEBUG(D_OTHER, "Lock destroyed, not adding to resource\n");
785 LASSERT(list_empty(&lock->l_res_link));
786 LASSERT(list_empty(&lock->l_sl_mode));
787 LASSERT(list_empty(&lock->l_sl_policy));
789 list_add(&lock->l_res_link, prev->res_link);
790 list_add(&lock->l_sl_mode, prev->mode_link);
791 list_add(&lock->l_sl_policy, prev->policy_link);
796 static void ldlm_grant_lock_with_skiplist(struct ldlm_lock *lock)
798 struct sl_insert_point prev;
801 LASSERT(lock->l_req_mode == lock->l_granted_mode);
803 search_granted_lock(&lock->l_resource->lr_granted, lock, &prev);
804 ldlm_granted_list_add_lock(lock, &prev);
809 * - ldlm_lock_enqueue
810 * - ldlm_reprocess_queue
811 * - ldlm_lock_convert
813 * must be called with lr_lock held
815 void ldlm_grant_lock(struct ldlm_lock *lock, struct list_head *work_list)
817 struct ldlm_resource *res = lock->l_resource;
820 check_res_locked(res);
822 lock->l_granted_mode = lock->l_req_mode;
823 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS)
824 ldlm_grant_lock_with_skiplist(lock);
825 else if (res->lr_type == LDLM_EXTENT)
826 ldlm_extent_add_lock(res, lock);
828 ldlm_resource_add_lock(res, &res->lr_granted, lock);
830 if (lock->l_granted_mode < res->lr_most_restr)
831 res->lr_most_restr = lock->l_granted_mode;
833 if (work_list && lock->l_completion_ast != NULL)
834 ldlm_add_ast_work_item(lock, NULL, work_list);
836 ldlm_pool_add(&res->lr_namespace->ns_pool, lock);
840 /* returns a referenced lock or NULL. See the flag descriptions below, in the
841 * comment above ldlm_lock_match */
842 static struct ldlm_lock *search_queue(struct list_head *queue,
844 ldlm_policy_data_t *policy,
845 struct ldlm_lock *old_lock, int flags)
847 struct ldlm_lock *lock;
848 struct list_head *tmp;
850 list_for_each(tmp, queue) {
853 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
855 if (lock == old_lock)
858 /* llite sometimes wants to match locks that will be
859 * canceled when their users drop, but we allow it to match
860 * if it passes in CBPENDING and the lock still has users.
861 * this is generally only going to be used by children
862 * whose parents already hold a lock so forward progress
863 * can still happen. */
864 if (lock->l_flags & LDLM_FL_CBPENDING &&
865 !(flags & LDLM_FL_CBPENDING))
867 if (lock->l_flags & LDLM_FL_CBPENDING &&
868 lock->l_readers == 0 && lock->l_writers == 0)
871 if (!(lock->l_req_mode & *mode))
873 match = lock->l_req_mode;
875 if (lock->l_resource->lr_type == LDLM_EXTENT &&
876 (lock->l_policy_data.l_extent.start >
877 policy->l_extent.start ||
878 lock->l_policy_data.l_extent.end < policy->l_extent.end))
881 if (unlikely(match == LCK_GROUP) &&
882 lock->l_resource->lr_type == LDLM_EXTENT &&
883 lock->l_policy_data.l_extent.gid != policy->l_extent.gid)
886 /* We match if we have existing lock with same or wider set
888 if (lock->l_resource->lr_type == LDLM_IBITS &&
889 ((lock->l_policy_data.l_inodebits.bits &
890 policy->l_inodebits.bits) !=
891 policy->l_inodebits.bits))
894 if (lock->l_destroyed || (lock->l_flags & LDLM_FL_FAILED))
897 if ((flags & LDLM_FL_LOCAL_ONLY) &&
898 !(lock->l_flags & LDLM_FL_LOCAL))
901 if (flags & LDLM_FL_TEST_LOCK) {
903 ldlm_lock_touch_in_lru(lock);
905 ldlm_lock_addref_internal_nolock(lock, match);
914 void ldlm_lock_allow_match(struct ldlm_lock *lock)
916 lock_res_and_lock(lock);
917 lock->l_flags |= LDLM_FL_LVB_READY;
918 cfs_waitq_signal(&lock->l_waitq);
919 unlock_res_and_lock(lock);
922 /* Can be called in two ways:
924 * If 'ns' is NULL, then lockh describes an existing lock that we want to look
925 * for a duplicate of.
927 * Otherwise, all of the fields must be filled in, to match against.
929 * If 'flags' contains LDLM_FL_LOCAL_ONLY, then only match local locks on the
930 * server (ie, connh is NULL)
931 * If 'flags' contains LDLM_FL_BLOCK_GRANTED, then only locks on the granted
932 * list will be considered
933 * If 'flags' contains LDLM_FL_CBPENDING, then locks that have been marked
934 * to be canceled can still be matched as long as they still have reader
935 * or writer refernces
936 * If 'flags' contains LDLM_FL_TEST_LOCK, then don't actually reference a lock,
937 * just tell us if we would have matched.
939 * Returns 1 if it finds an already-existing lock that is compatible; in this
940 * case, lockh is filled in with a addref()ed lock
942 * we also check security context, if that failed we simply return 0 (to keep
943 * caller code unchanged), the context failure will be discovered by caller
946 ldlm_mode_t ldlm_lock_match(struct ldlm_namespace *ns, int flags,
947 const struct ldlm_res_id *res_id, ldlm_type_t type,
948 ldlm_policy_data_t *policy, ldlm_mode_t mode,
949 struct lustre_handle *lockh)
951 struct ldlm_resource *res;
952 struct ldlm_lock *lock, *old_lock = NULL;
957 old_lock = ldlm_handle2lock(lockh);
960 ns = old_lock->l_resource->lr_namespace;
961 res_id = &old_lock->l_resource->lr_name;
962 type = old_lock->l_resource->lr_type;
963 mode = old_lock->l_req_mode;
966 res = ldlm_resource_get(ns, NULL, res_id, type, 0);
968 LASSERT(old_lock == NULL);
974 lock = search_queue(&res->lr_granted, &mode, policy, old_lock, flags);
977 if (flags & LDLM_FL_BLOCK_GRANTED)
979 lock = search_queue(&res->lr_converting, &mode, policy, old_lock, flags);
982 lock = search_queue(&res->lr_waiting, &mode, policy, old_lock, flags);
989 ldlm_resource_putref(res);
992 ldlm_lock2handle(lock, lockh);
993 if ((flags & LDLM_FL_LVB_READY) &&
994 (!(lock->l_flags & LDLM_FL_LVB_READY))) {
995 struct l_wait_info lwi;
996 if (lock->l_completion_ast) {
997 int err = lock->l_completion_ast(lock,
998 LDLM_FL_WAIT_NOREPROC,
1001 if (flags & LDLM_FL_TEST_LOCK)
1002 LDLM_LOCK_PUT(lock);
1004 ldlm_lock_decref_internal(lock, mode);
1010 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout), NULL,
1011 LWI_ON_SIGNAL_NOOP, NULL);
1013 /* XXX FIXME see comment on CAN_MATCH in lustre_dlm.h */
1014 l_wait_event(lock->l_waitq,
1015 (lock->l_flags & LDLM_FL_LVB_READY), &lwi);
1020 LDLM_DEBUG(lock, "matched ("LPU64" "LPU64")",
1021 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1022 res_id->name[2] : policy->l_extent.start,
1023 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1024 res_id->name[3] : policy->l_extent.end);
1026 /* check user's security context */
1027 if (lock->l_conn_export &&
1028 sptlrpc_import_check_ctx(
1029 class_exp2cliimp(lock->l_conn_export))) {
1030 if (!(flags & LDLM_FL_TEST_LOCK))
1031 ldlm_lock_decref_internal(lock, mode);
1035 if (flags & LDLM_FL_TEST_LOCK)
1036 LDLM_LOCK_PUT(lock);
1038 } else if (!(flags & LDLM_FL_TEST_LOCK)) {/*less verbose for test-only*/
1039 LDLM_DEBUG_NOLOCK("not matched ns %p type %u mode %u res "
1040 LPU64"/"LPU64" ("LPU64" "LPU64")", ns,
1041 type, mode, res_id->name[0], res_id->name[1],
1042 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1043 res_id->name[2] :policy->l_extent.start,
1044 (type == LDLM_PLAIN || type == LDLM_IBITS) ?
1045 res_id->name[3] : policy->l_extent.end);
1048 LDLM_LOCK_PUT(old_lock);
1050 return rc ? mode : 0;
1053 /* Returns a referenced lock */
1054 struct ldlm_lock *ldlm_lock_create(struct ldlm_namespace *ns,
1055 const struct ldlm_res_id *res_id,
1058 ldlm_blocking_callback blocking,
1059 ldlm_completion_callback completion,
1060 ldlm_glimpse_callback glimpse,
1061 void *data, __u32 lvb_len)
1063 struct ldlm_lock *lock;
1064 struct ldlm_resource *res;
1067 res = ldlm_resource_get(ns, NULL, res_id, type, 1);
1071 lock = ldlm_lock_new(res);
1072 ldlm_resource_putref(res);
1077 lock->l_req_mode = mode;
1078 lock->l_ast_data = data;
1079 lock->l_blocking_ast = blocking;
1080 lock->l_completion_ast = completion;
1081 lock->l_glimpse_ast = glimpse;
1082 lock->l_pid = cfs_curproc_pid();
1084 lock->l_tree_node = NULL;
1085 /* if this is the extent lock, allocate the interval tree node */
1086 if (type == LDLM_EXTENT) {
1087 if (ldlm_interval_alloc(lock) == NULL)
1092 lock->l_lvb_len = lvb_len;
1093 OBD_ALLOC(lock->l_lvb_data, lvb_len);
1094 if (lock->l_lvb_data == NULL)
1101 if (lock->l_lvb_data)
1102 OBD_FREE(lock->l_lvb_data, lvb_len);
1103 ldlm_interval_free(ldlm_interval_detach(lock));
1104 OBD_SLAB_FREE(lock, ldlm_lock_slab, sizeof(*lock));
1108 ldlm_error_t ldlm_lock_enqueue(struct ldlm_namespace *ns,
1109 struct ldlm_lock **lockp,
1110 void *cookie, int *flags)
1112 struct ldlm_lock *lock = *lockp;
1113 struct ldlm_resource *res = lock->l_resource;
1114 int local = ns_is_client(res->lr_namespace);
1115 ldlm_processing_policy policy;
1116 ldlm_error_t rc = ELDLM_OK;
1117 struct ldlm_interval *node = NULL;
1120 do_gettimeofday(&lock->l_enqueued_time);
1121 /* policies are not executed on the client or during replay */
1122 if ((*flags & (LDLM_FL_HAS_INTENT|LDLM_FL_REPLAY)) == LDLM_FL_HAS_INTENT
1123 && !local && ns->ns_policy) {
1124 rc = ns->ns_policy(ns, lockp, cookie, lock->l_req_mode, *flags,
1126 if (rc == ELDLM_LOCK_REPLACED) {
1127 /* The lock that was returned has already been granted,
1128 * and placed into lockp. If it's not the same as the
1129 * one we passed in, then destroy the old one and our
1130 * work here is done. */
1131 if (lock != *lockp) {
1132 ldlm_lock_destroy(lock);
1133 LDLM_LOCK_PUT(lock);
1135 *flags |= LDLM_FL_LOCK_CHANGED;
1137 } else if (rc != ELDLM_OK ||
1138 (rc == ELDLM_OK && (*flags & LDLM_FL_INTENT_ONLY))) {
1139 ldlm_lock_destroy(lock);
1144 /* For a replaying lock, it might be already in granted list. So
1145 * unlinking the lock will cause the interval node to be freed, we
1146 * have to allocate the interval node early otherwise we can't regrant
1147 * this lock in the future. - jay */
1148 if (!local && (*flags & LDLM_FL_REPLAY) && res->lr_type == LDLM_EXTENT)
1149 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO,
1152 lock_res_and_lock(lock);
1153 if (local && lock->l_req_mode == lock->l_granted_mode) {
1154 /* The server returned a blocked lock, but it was granted
1155 * before we got a chance to actually enqueue it. We don't
1156 * need to do anything else. */
1157 *flags &= ~(LDLM_FL_BLOCK_GRANTED |
1158 LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_WAIT);
1159 GOTO(out, ELDLM_OK);
1162 ldlm_resource_unlink_lock(lock);
1163 if (res->lr_type == LDLM_EXTENT && lock->l_tree_node == NULL) {
1165 ldlm_lock_destroy_nolock(lock);
1166 GOTO(out, rc = -ENOMEM);
1169 CFS_INIT_LIST_HEAD(&node->li_group);
1170 ldlm_interval_attach(node, lock);
1174 /* Some flags from the enqueue want to make it into the AST, via the
1175 * lock's l_flags. */
1176 lock->l_flags |= *flags & LDLM_AST_DISCARD_DATA;
1178 /* This distinction between local lock trees is very important; a client
1179 * namespace only has information about locks taken by that client, and
1180 * thus doesn't have enough information to decide for itself if it can
1181 * be granted (below). In this case, we do exactly what the server
1182 * tells us to do, as dictated by the 'flags'.
1184 * We do exactly the same thing during recovery, when the server is
1185 * more or less trusting the clients not to lie.
1187 * FIXME (bug 268): Detect obvious lies by checking compatibility in
1188 * granted/converting queues. */
1190 if (*flags & LDLM_FL_BLOCK_CONV)
1191 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1192 else if (*flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED))
1193 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1195 ldlm_grant_lock(lock, NULL);
1196 GOTO(out, ELDLM_OK);
1197 } else if (*flags & LDLM_FL_REPLAY) {
1198 if (*flags & LDLM_FL_BLOCK_CONV) {
1199 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1200 GOTO(out, ELDLM_OK);
1201 } else if (*flags & LDLM_FL_BLOCK_WAIT) {
1202 ldlm_resource_add_lock(res, &res->lr_waiting, lock);
1203 GOTO(out, ELDLM_OK);
1204 } else if (*flags & LDLM_FL_BLOCK_GRANTED) {
1205 ldlm_grant_lock(lock, NULL);
1206 GOTO(out, ELDLM_OK);
1208 /* If no flags, fall through to normal enqueue path. */
1211 policy = ldlm_processing_policy_table[res->lr_type];
1212 policy(lock, flags, 1, &rc, NULL);
1215 unlock_res_and_lock(lock);
1217 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1221 /* Must be called with namespace taken: queue is waiting or converting. */
1222 int ldlm_reprocess_queue(struct ldlm_resource *res, struct list_head *queue,
1223 struct list_head *work_list)
1225 struct list_head *tmp, *pos;
1226 ldlm_processing_policy policy;
1228 int rc = LDLM_ITER_CONTINUE;
1232 check_res_locked(res);
1234 policy = ldlm_processing_policy_table[res->lr_type];
1237 list_for_each_safe(tmp, pos, queue) {
1238 struct ldlm_lock *pending;
1239 pending = list_entry(tmp, struct ldlm_lock, l_res_link);
1241 CDEBUG(D_INFO, "Reprocessing lock %p\n", pending);
1244 rc = policy(pending, &flags, 0, &err, work_list);
1245 if (rc != LDLM_ITER_CONTINUE)
1252 /* Helper function for ldlm_run_ast_work().
1254 * Send an existing rpc set specified by @arg->set and then
1255 * destroy it. Create new one if @do_create flag is set. */
1257 ldlm_send_and_maybe_create_set(struct ldlm_cb_set_arg *arg, int do_create)
1261 ptlrpc_set_wait(arg->set);
1262 if (arg->type == LDLM_BL_CALLBACK)
1263 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
1264 ptlrpc_set_destroy(arg->set);
1267 arg->set = ptlrpc_prep_set();
1273 ldlm_work_bl_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1275 struct ldlm_lock_desc d;
1276 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
1279 /* nobody should touch l_bl_ast */
1280 lock_res_and_lock(lock);
1281 list_del_init(&lock->l_bl_ast);
1283 LASSERT(lock->l_flags & LDLM_FL_AST_SENT);
1284 LASSERT(lock->l_bl_ast_run == 0);
1285 LASSERT(lock->l_blocking_lock);
1286 lock->l_bl_ast_run++;
1287 unlock_res_and_lock(lock);
1289 ldlm_lock2desc(lock->l_blocking_lock, &d);
1291 LDLM_LOCK_PUT(lock->l_blocking_lock);
1292 lock->l_blocking_lock = NULL;
1293 lock->l_blocking_ast(lock, &d, (void *)arg,
1295 LDLM_LOCK_PUT(lock);
1301 ldlm_work_cp_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1303 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_cp_ast);
1304 ldlm_completion_callback completion_callback;
1308 /* It's possible to receive a completion AST before we've set
1309 * the l_completion_ast pointer: either because the AST arrived
1310 * before the reply, or simply because there's a small race
1311 * window between receiving the reply and finishing the local
1312 * enqueue. (bug 842)
1314 * This can't happen with the blocking_ast, however, because we
1315 * will never call the local blocking_ast until we drop our
1316 * reader/writer reference, which we won't do until we get the
1317 * reply and finish enqueueing. */
1319 /* nobody should touch l_cp_ast */
1320 lock_res_and_lock(lock);
1321 list_del_init(&lock->l_cp_ast);
1322 LASSERT(lock->l_flags & LDLM_FL_CP_REQD);
1323 /* save l_completion_ast since it can be changed by
1324 * mds_intent_policy(), see bug 14225 */
1325 completion_callback = lock->l_completion_ast;
1326 lock->l_flags &= ~LDLM_FL_CP_REQD;
1327 unlock_res_and_lock(lock);
1329 if (completion_callback != NULL) {
1330 completion_callback(lock, 0, (void *)arg);
1333 LDLM_LOCK_PUT(lock);
1339 ldlm_work_revoke_ast_lock(struct list_head *tmp, struct ldlm_cb_set_arg *arg)
1341 struct ldlm_lock_desc desc;
1342 struct ldlm_lock *lock = list_entry(tmp, struct ldlm_lock, l_export_chain);
1345 list_del_init(&lock->l_export_chain);
1347 /* the desc just pretend to exclusive */
1348 ldlm_lock2desc(lock, &desc);
1349 desc.l_req_mode = LCK_EX;
1350 desc.l_granted_mode = 0;
1352 lock->l_blocking_ast(lock, &desc, (void*)arg, LDLM_CB_BLOCKING);
1353 LDLM_LOCK_PUT(lock);
1358 int ldlm_run_ast_work(struct list_head *rpc_list, ldlm_desc_ast_t ast_type)
1360 struct ldlm_cb_set_arg arg;
1361 struct list_head *tmp, *pos;
1362 int (*work_ast_lock)(struct list_head *tmp, struct ldlm_cb_set_arg *arg);
1366 arg.set = ptlrpc_prep_set();
1367 atomic_set(&arg.restart, 0);
1369 case LDLM_WORK_BL_AST:
1370 arg.type = LDLM_BL_CALLBACK;
1371 work_ast_lock = ldlm_work_bl_ast_lock;
1373 case LDLM_WORK_CP_AST:
1374 arg.type = LDLM_CP_CALLBACK;
1375 work_ast_lock = ldlm_work_cp_ast_lock;
1377 case LDLM_WORK_REVOKE_AST:
1378 arg.type = LDLM_BL_CALLBACK;
1379 work_ast_lock = ldlm_work_revoke_ast_lock;
1386 list_for_each_safe(tmp, pos, rpc_list) {
1387 ast_count += work_ast_lock(tmp, &arg);
1389 /* Send the request set if it exceeds the PARALLEL_AST_LIMIT,
1390 * and create a new set for requests that remained in
1392 if (unlikely(ast_count == PARALLEL_AST_LIMIT)) {
1393 ldlm_send_and_maybe_create_set(&arg, 1);
1399 ldlm_send_and_maybe_create_set(&arg, 0);
1401 /* In case when number of ASTs is multiply of
1402 * PARALLEL_AST_LIMIT or @rpc_list was initially empty,
1403 * @arg.set must be destroyed here, otherwise we get
1404 * write memory leaking. */
1405 ptlrpc_set_destroy(arg.set);
1407 RETURN(atomic_read(&arg.restart) ? -ERESTART : 0);
1410 static int reprocess_one_queue(struct ldlm_resource *res, void *closure)
1412 ldlm_reprocess_all(res);
1413 return LDLM_ITER_CONTINUE;
1416 void ldlm_reprocess_all_ns(struct ldlm_namespace *ns)
1418 struct list_head *tmp;
1425 spin_lock(&ns->ns_hash_lock);
1426 for (i = 0; i < RES_HASH_SIZE; i++) {
1427 tmp = ns->ns_hash[i].next;
1428 while (tmp != &(ns->ns_hash[i])) {
1429 struct ldlm_resource *res =
1430 list_entry(tmp, struct ldlm_resource, lr_hash);
1432 ldlm_resource_getref(res);
1433 spin_unlock(&ns->ns_hash_lock);
1435 rc = reprocess_one_queue(res, NULL);
1437 spin_lock(&ns->ns_hash_lock);
1439 ldlm_resource_putref_locked(res);
1441 if (rc == LDLM_ITER_STOP)
1446 spin_unlock(&ns->ns_hash_lock);
1450 void ldlm_reprocess_all(struct ldlm_resource *res)
1452 CFS_LIST_HEAD(rpc_list);
1456 /* Local lock trees don't get reprocessed. */
1457 if (ns_is_client(res->lr_namespace)) {
1464 rc = ldlm_reprocess_queue(res, &res->lr_converting, &rpc_list);
1465 if (rc == LDLM_ITER_CONTINUE)
1466 ldlm_reprocess_queue(res, &res->lr_waiting, &rpc_list);
1469 rc = ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1470 if (rc == -ERESTART) {
1471 LASSERT(list_empty(&rpc_list));
1477 void ldlm_cancel_callback(struct ldlm_lock *lock)
1479 check_res_locked(lock->l_resource);
1480 if (!(lock->l_flags & LDLM_FL_CANCEL)) {
1481 lock->l_flags |= LDLM_FL_CANCEL;
1482 if (lock->l_blocking_ast) {
1483 // l_check_no_ns_lock(ns);
1484 unlock_res_and_lock(lock);
1485 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
1487 lock_res_and_lock(lock);
1489 LDLM_DEBUG(lock, "no blocking ast");
1492 lock->l_flags |= LDLM_FL_BL_DONE;
1495 void ldlm_unlink_lock_skiplist(struct ldlm_lock *req)
1497 if (req->l_resource->lr_type != LDLM_PLAIN &&
1498 req->l_resource->lr_type != LDLM_IBITS)
1501 list_del_init(&req->l_sl_policy);
1502 list_del_init(&req->l_sl_mode);
1505 void ldlm_lock_cancel(struct ldlm_lock *lock)
1507 struct ldlm_resource *res;
1508 struct ldlm_namespace *ns;
1511 lock_res_and_lock(lock);
1513 res = lock->l_resource;
1514 ns = res->lr_namespace;
1516 /* Please do not, no matter how tempting, remove this LBUG without
1517 * talking to me first. -phik */
1518 if (lock->l_readers || lock->l_writers) {
1519 LDLM_ERROR(lock, "lock still has references");
1523 ldlm_del_waiting_lock(lock);
1525 /* Releases cancel callback. */
1526 ldlm_cancel_callback(lock);
1528 /* Yes, second time, just in case it was added again while we were
1529 running with no res lock in ldlm_cancel_callback */
1530 ldlm_del_waiting_lock(lock);
1531 ldlm_resource_unlink_lock(lock);
1532 ldlm_lock_destroy_nolock(lock);
1534 if (lock->l_granted_mode == lock->l_req_mode)
1535 ldlm_pool_del(&ns->ns_pool, lock);
1537 /* Make sure we will not be called again for same lock what is possible
1538 * if not to zero out lock->l_granted_mode */
1539 lock->l_granted_mode = 0;
1540 unlock_res_and_lock(lock);
1545 int ldlm_lock_set_data(struct lustre_handle *lockh, void *data)
1547 struct ldlm_lock *lock = ldlm_handle2lock(lockh);
1553 lock->l_ast_data = data;
1554 LDLM_LOCK_PUT(lock);
1558 void ldlm_cancel_locks_for_export(struct obd_export *exp)
1560 struct ldlm_lock *lock;
1561 struct ldlm_resource *res;
1563 spin_lock(&exp->exp_ldlm_data.led_lock);
1564 while(!list_empty(&exp->exp_ldlm_data.led_held_locks)) {
1565 lock = list_entry(exp->exp_ldlm_data.led_held_locks.next,
1566 struct ldlm_lock, l_export_chain);
1567 res = ldlm_resource_getref(lock->l_resource);
1568 LDLM_LOCK_GET(lock);
1569 spin_unlock(&exp->exp_ldlm_data.led_lock);
1571 LDLM_DEBUG(lock, "export %p", exp);
1572 ldlm_res_lvbo_update(res, NULL, 0, 1);
1574 ldlm_lock_cancel(lock);
1575 ldlm_reprocess_all(res);
1577 ldlm_resource_putref(res);
1578 LDLM_LOCK_PUT(lock);
1579 spin_lock(&exp->exp_ldlm_data.led_lock);
1581 spin_unlock(&exp->exp_ldlm_data.led_lock);
1584 struct ldlm_resource *ldlm_lock_convert(struct ldlm_lock *lock, int new_mode,
1587 CFS_LIST_HEAD(rpc_list);
1588 struct ldlm_resource *res;
1589 struct ldlm_namespace *ns;
1592 struct sl_insert_point prev;
1594 struct ldlm_interval *node;
1597 if (new_mode == lock->l_granted_mode) { // No changes? Just return.
1598 *flags |= LDLM_FL_BLOCK_GRANTED;
1599 RETURN(lock->l_resource);
1602 /* I can't check the type of lock here because the bitlock of lock
1603 * is not held here, so do the allocation blindly. -jay */
1604 OBD_SLAB_ALLOC(node, ldlm_interval_slab, CFS_ALLOC_IO, sizeof(*node));
1605 if (node == NULL) /* Actually, this causes EDEADLOCK to be returned */
1608 LASSERTF(new_mode == LCK_PW && lock->l_granted_mode == LCK_PR,
1609 "new_mode %u, granted %u\n", new_mode, lock->l_granted_mode);
1611 lock_res_and_lock(lock);
1613 res = lock->l_resource;
1614 ns = res->lr_namespace;
1616 old_mode = lock->l_req_mode;
1617 lock->l_req_mode = new_mode;
1618 if (res->lr_type == LDLM_PLAIN || res->lr_type == LDLM_IBITS) {
1619 /* remember the lock position where the lock might be
1620 * added back to the granted list later and also
1621 * remember the join mode for skiplist fixing. */
1622 prev.res_link = lock->l_res_link.prev;
1623 prev.mode_link = lock->l_sl_mode.prev;
1624 prev.policy_link = lock->l_sl_policy.prev;
1625 ldlm_resource_unlink_lock(lock);
1627 ldlm_resource_unlink_lock(lock);
1628 if (res->lr_type == LDLM_EXTENT) {
1629 /* FIXME: ugly code, I have to attach the lock to a
1630 * interval node again since perhaps it will be granted
1632 CFS_INIT_LIST_HEAD(&node->li_group);
1633 ldlm_interval_attach(node, lock);
1638 /* If this is a local resource, put it on the appropriate list. */
1639 if (ns_is_client(res->lr_namespace)) {
1640 if (*flags & (LDLM_FL_BLOCK_CONV | LDLM_FL_BLOCK_GRANTED)) {
1641 ldlm_resource_add_lock(res, &res->lr_converting, lock);
1643 /* This should never happen, because of the way the
1644 * server handles conversions. */
1645 LDLM_ERROR(lock, "Erroneous flags %d on local lock\n",
1649 ldlm_grant_lock(lock, &rpc_list);
1651 /* FIXME: completion handling not with ns_lock held ! */
1652 if (lock->l_completion_ast)
1653 lock->l_completion_ast(lock, 0, NULL);
1657 ldlm_processing_policy policy;
1658 policy = ldlm_processing_policy_table[res->lr_type];
1659 rc = policy(lock, &pflags, 0, &err, &rpc_list);
1660 if (rc == LDLM_ITER_STOP) {
1661 lock->l_req_mode = old_mode;
1662 if (res->lr_type == LDLM_EXTENT)
1663 ldlm_extent_add_lock(res, lock);
1665 ldlm_granted_list_add_lock(lock, &prev);
1669 *flags |= LDLM_FL_BLOCK_GRANTED;
1673 unlock_res_and_lock(lock);
1676 ldlm_run_ast_work(&rpc_list, LDLM_WORK_CP_AST);
1678 OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
1682 void ldlm_lock_dump(int level, struct ldlm_lock *lock, int pos)
1684 struct obd_device *obd = NULL;
1686 if (!((libcfs_debug | D_ERROR) & level))
1690 CDEBUG(level, " NULL LDLM lock\n");
1694 CDEBUG(level," -- Lock dump: %p/"LPX64" (rc: %d) (pos: %d) (pid: %d)\n",
1695 lock, lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1697 if (lock->l_conn_export != NULL)
1698 obd = lock->l_conn_export->exp_obd;
1699 if (lock->l_export && lock->l_export->exp_connection) {
1700 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1701 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid),
1702 lock->l_remote_handle.cookie);
1703 } else if (obd == NULL) {
1704 CDEBUG(level, " Node: local\n");
1706 struct obd_import *imp = obd->u.cli.cl_import;
1707 CDEBUG(level, " Node: NID %s (rhandle: "LPX64")\n",
1708 libcfs_nid2str(imp->imp_connection->c_peer.nid),
1709 lock->l_remote_handle.cookie);
1711 CDEBUG(level, " Resource: %p ("LPU64"/"LPU64"/"LPU64")\n",
1713 lock->l_resource->lr_name.name[0],
1714 lock->l_resource->lr_name.name[1],
1715 lock->l_resource->lr_name.name[2]);
1716 CDEBUG(level, " Req mode: %s, grant mode: %s, rc: %u, read: %d, "
1717 "write: %d flags: %#x\n", ldlm_lockname[lock->l_req_mode],
1718 ldlm_lockname[lock->l_granted_mode],
1719 atomic_read(&lock->l_refc), lock->l_readers, lock->l_writers,
1721 if (lock->l_resource->lr_type == LDLM_EXTENT)
1722 CDEBUG(level, " Extent: "LPU64" -> "LPU64
1723 " (req "LPU64"-"LPU64")\n",
1724 lock->l_policy_data.l_extent.start,
1725 lock->l_policy_data.l_extent.end,
1726 lock->l_req_extent.start, lock->l_req_extent.end);
1727 else if (lock->l_resource->lr_type == LDLM_FLOCK)
1728 CDEBUG(level, " Pid: %d Extent: "LPU64" -> "LPU64"\n",
1729 lock->l_policy_data.l_flock.pid,
1730 lock->l_policy_data.l_flock.start,
1731 lock->l_policy_data.l_flock.end);
1732 else if (lock->l_resource->lr_type == LDLM_IBITS)
1733 CDEBUG(level, " Bits: "LPX64"\n",
1734 lock->l_policy_data.l_inodebits.bits);
1737 void ldlm_lock_dump_handle(int level, struct lustre_handle *lockh)
1739 struct ldlm_lock *lock;
1741 if (!((libcfs_debug | D_ERROR) & level))
1744 lock = ldlm_handle2lock(lockh);
1748 ldlm_lock_dump(D_OTHER, lock, 0);
1750 LDLM_LOCK_PUT(lock);
1753 void _ldlm_lock_debug(struct ldlm_lock *lock, __u32 level,
1754 struct libcfs_debug_msg_data *data, const char *fmt,
1758 cfs_debug_limit_state_t *cdls = data->msg_cdls;
1760 va_start(args, fmt);
1762 if (lock->l_resource == NULL) {
1763 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1764 data->msg_fn, data->msg_line, fmt, args,
1765 " ns: \?\? lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1766 "res: \?\? rrc=\?\? type: \?\?\? flags: %x remote: "
1767 LPX64" expref: %d pid: %u\n", lock,
1768 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1769 lock->l_readers, lock->l_writers,
1770 ldlm_lockname[lock->l_granted_mode],
1771 ldlm_lockname[lock->l_req_mode],
1772 lock->l_flags, lock->l_remote_handle.cookie,
1774 atomic_read(&lock->l_export->exp_refcount) : -99,
1780 switch (lock->l_resource->lr_type) {
1782 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1783 data->msg_fn, data->msg_line, fmt, args,
1784 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1785 "res: "LPU64"/"LPU64" rrc: %d type: %s ["LPU64"->"LPU64
1786 "] (req "LPU64"->"LPU64") flags: %x remote: "LPX64
1787 " expref: %d pid: %u\n",
1788 lock->l_resource->lr_namespace->ns_name, lock,
1789 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1790 lock->l_readers, lock->l_writers,
1791 ldlm_lockname[lock->l_granted_mode],
1792 ldlm_lockname[lock->l_req_mode],
1793 lock->l_resource->lr_name.name[0],
1794 lock->l_resource->lr_name.name[1],
1795 atomic_read(&lock->l_resource->lr_refcount),
1796 ldlm_typename[lock->l_resource->lr_type],
1797 lock->l_policy_data.l_extent.start,
1798 lock->l_policy_data.l_extent.end,
1799 lock->l_req_extent.start, lock->l_req_extent.end,
1800 lock->l_flags, lock->l_remote_handle.cookie,
1802 atomic_read(&lock->l_export->exp_refcount) : -99,
1807 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1808 data->msg_fn, data->msg_line, fmt, args,
1809 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1810 "res: "LPU64"/"LPU64" rrc: %d type: %s pid: %d "
1811 "["LPU64"->"LPU64"] flags: %x remote: "LPX64
1812 " expref: %d pid: %u\n",
1813 lock->l_resource->lr_namespace->ns_name, lock,
1814 lock->l_handle.h_cookie, atomic_read(&lock->l_refc),
1815 lock->l_readers, lock->l_writers,
1816 ldlm_lockname[lock->l_granted_mode],
1817 ldlm_lockname[lock->l_req_mode],
1818 lock->l_resource->lr_name.name[0],
1819 lock->l_resource->lr_name.name[1],
1820 atomic_read(&lock->l_resource->lr_refcount),
1821 ldlm_typename[lock->l_resource->lr_type],
1822 lock->l_policy_data.l_flock.pid,
1823 lock->l_policy_data.l_flock.start,
1824 lock->l_policy_data.l_flock.end,
1825 lock->l_flags, lock->l_remote_handle.cookie,
1827 atomic_read(&lock->l_export->exp_refcount) : -99,
1832 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1833 data->msg_fn, data->msg_line, fmt, args,
1834 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1835 "res: "LPU64"/"LPU64" bits "LPX64" rrc: %d type: %s "
1836 "flags: %x remote: "LPX64" expref: %d "
1838 lock->l_resource->lr_namespace->ns_name,
1839 lock, lock->l_handle.h_cookie,
1840 atomic_read (&lock->l_refc),
1841 lock->l_readers, lock->l_writers,
1842 ldlm_lockname[lock->l_granted_mode],
1843 ldlm_lockname[lock->l_req_mode],
1844 lock->l_resource->lr_name.name[0],
1845 lock->l_resource->lr_name.name[1],
1846 lock->l_policy_data.l_inodebits.bits,
1847 atomic_read(&lock->l_resource->lr_refcount),
1848 ldlm_typename[lock->l_resource->lr_type],
1849 lock->l_flags, lock->l_remote_handle.cookie,
1851 atomic_read(&lock->l_export->exp_refcount) : -99,
1856 libcfs_debug_vmsg2(cdls, data->msg_subsys, level, data->msg_file,
1857 data->msg_fn, data->msg_line, fmt, args,
1858 " ns: %s lock: %p/"LPX64" lrc: %d/%d,%d mode: %s/%s "
1859 "res: "LPU64"/"LPU64" rrc: %d type: %s flags: %x "
1860 "remote: "LPX64" expref: %d pid: %u\n",
1861 lock->l_resource->lr_namespace->ns_name,
1862 lock, lock->l_handle.h_cookie,
1863 atomic_read (&lock->l_refc),
1864 lock->l_readers, lock->l_writers,
1865 ldlm_lockname[lock->l_granted_mode],
1866 ldlm_lockname[lock->l_req_mode],
1867 lock->l_resource->lr_name.name[0],
1868 lock->l_resource->lr_name.name[1],
1869 atomic_read(&lock->l_resource->lr_refcount),
1870 ldlm_typename[lock->l_resource->lr_type],
1871 lock->l_flags, lock->l_remote_handle.cookie,
1873 atomic_read(&lock->l_export->exp_refcount) : -99,
1879 EXPORT_SYMBOL(_ldlm_lock_debug);