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.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 # define EXPORT_SYMTAB
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lustre_fid.h>
49 #include <libcfs/list.h>
50 /* lu_time_global_{init,fini}() */
53 #include <cl_object.h>
54 #include "cl_internal.h"
56 /** Lock class of cl_lock::cll_guard */
57 static cfs_lock_class_key_t cl_lock_guard_class;
58 static cfs_mem_cache_t *cl_lock_kmem;
60 static struct lu_kmem_descr cl_lock_caches[] = {
62 .ckd_cache = &cl_lock_kmem,
63 .ckd_name = "cl_lock_kmem",
64 .ckd_size = sizeof (struct cl_lock)
72 * Basic lock invariant that is maintained at all times. Caller either has a
73 * reference to \a lock, or somehow assures that \a lock cannot be freed.
75 * \see cl_lock_invariant()
77 static int cl_lock_invariant_trusted(const struct lu_env *env,
78 const struct cl_lock *lock)
82 ergo(lock->cll_state == CLS_FREEING, lock->cll_holds == 0) &&
83 cfs_atomic_read(&lock->cll_ref) >= lock->cll_holds &&
84 lock->cll_holds >= lock->cll_users &&
85 lock->cll_holds >= 0 &&
86 lock->cll_users >= 0 &&
91 * Stronger lock invariant, checking that caller has a reference on a lock.
93 * \see cl_lock_invariant_trusted()
95 static int cl_lock_invariant(const struct lu_env *env,
96 const struct cl_lock *lock)
100 result = cfs_atomic_read(&lock->cll_ref) > 0 &&
101 cl_lock_invariant_trusted(env, lock);
102 if (!result && env != NULL)
103 CL_LOCK_DEBUG(D_ERROR, env, lock, "invariant broken");
108 * Returns lock "nesting": 0 for a top-lock and 1 for a sub-lock.
110 static enum clt_nesting_level cl_lock_nesting(const struct cl_lock *lock)
112 return cl_object_header(lock->cll_descr.cld_obj)->coh_nesting;
116 * Returns a set of counters for this lock, depending on a lock nesting.
118 static struct cl_thread_counters *cl_lock_counters(const struct lu_env *env,
119 const struct cl_lock *lock)
121 struct cl_thread_info *info;
122 enum clt_nesting_level nesting;
124 info = cl_env_info(env);
125 nesting = cl_lock_nesting(lock);
126 LASSERT(nesting < ARRAY_SIZE(info->clt_counters));
127 return &info->clt_counters[nesting];
130 static void cl_lock_trace0(int level, const struct lu_env *env,
131 const char *prefix, const struct cl_lock *lock,
132 const char *func, const int line)
134 struct cl_object_header *h = cl_object_header(lock->cll_descr.cld_obj);
135 CDEBUG(level, "%s: %p@(%i %p %i %d %d %d %d %lx)"
136 "(%p/%d/%i) at %s():%d\n",
137 prefix, lock, cfs_atomic_read(&lock->cll_ref),
138 lock->cll_guarder, lock->cll_depth,
139 lock->cll_state, lock->cll_error, lock->cll_holds,
140 lock->cll_users, lock->cll_flags,
141 env, h->coh_nesting, cl_lock_nr_mutexed(env),
144 #define cl_lock_trace(level, env, prefix, lock) \
145 cl_lock_trace0(level, env, prefix, lock, __FUNCTION__, __LINE__)
147 #define RETIP ((unsigned long)__builtin_return_address(0))
149 #ifdef CONFIG_LOCKDEP
150 static cfs_lock_class_key_t cl_lock_key;
152 static void cl_lock_lockdep_init(struct cl_lock *lock)
154 lockdep_set_class_and_name(lock, &cl_lock_key, "EXT");
157 static void cl_lock_lockdep_acquire(const struct lu_env *env,
158 struct cl_lock *lock, __u32 enqflags)
160 cl_lock_counters(env, lock)->ctc_nr_locks_acquired++;
161 lock_acquire(&lock->dep_map, !!(enqflags & CEF_ASYNC),
162 /* try: */ 0, lock->cll_descr.cld_mode <= CLM_READ,
163 /* check: */ 2, RETIP);
166 static void cl_lock_lockdep_release(const struct lu_env *env,
167 struct cl_lock *lock)
169 cl_lock_counters(env, lock)->ctc_nr_locks_acquired--;
170 lock_release(&lock->dep_map, 0, RETIP);
173 #else /* !CONFIG_LOCKDEP */
175 static void cl_lock_lockdep_init(struct cl_lock *lock)
177 static void cl_lock_lockdep_acquire(const struct lu_env *env,
178 struct cl_lock *lock, __u32 enqflags)
180 static void cl_lock_lockdep_release(const struct lu_env *env,
181 struct cl_lock *lock)
184 #endif /* !CONFIG_LOCKDEP */
187 * Adds lock slice to the compound lock.
189 * This is called by cl_object_operations::coo_lock_init() methods to add a
190 * per-layer state to the lock. New state is added at the end of
191 * cl_lock::cll_layers list, that is, it is at the bottom of the stack.
193 * \see cl_req_slice_add(), cl_page_slice_add(), cl_io_slice_add()
195 void cl_lock_slice_add(struct cl_lock *lock, struct cl_lock_slice *slice,
196 struct cl_object *obj,
197 const struct cl_lock_operations *ops)
200 slice->cls_lock = lock;
201 cfs_list_add_tail(&slice->cls_linkage, &lock->cll_layers);
202 slice->cls_obj = obj;
203 slice->cls_ops = ops;
206 EXPORT_SYMBOL(cl_lock_slice_add);
209 * Returns true iff a lock with the mode \a has provides at least the same
210 * guarantees as a lock with the mode \a need.
212 int cl_lock_mode_match(enum cl_lock_mode has, enum cl_lock_mode need)
214 LINVRNT(need == CLM_READ || need == CLM_WRITE ||
215 need == CLM_PHANTOM || need == CLM_GROUP);
216 LINVRNT(has == CLM_READ || has == CLM_WRITE ||
217 has == CLM_PHANTOM || has == CLM_GROUP);
218 CLASSERT(CLM_PHANTOM < CLM_READ);
219 CLASSERT(CLM_READ < CLM_WRITE);
220 CLASSERT(CLM_WRITE < CLM_GROUP);
222 if (has != CLM_GROUP)
227 EXPORT_SYMBOL(cl_lock_mode_match);
230 * Returns true iff extent portions of lock descriptions match.
232 int cl_lock_ext_match(const struct cl_lock_descr *has,
233 const struct cl_lock_descr *need)
236 has->cld_start <= need->cld_start &&
237 has->cld_end >= need->cld_end &&
238 cl_lock_mode_match(has->cld_mode, need->cld_mode) &&
239 (has->cld_mode != CLM_GROUP || has->cld_gid == need->cld_gid);
241 EXPORT_SYMBOL(cl_lock_ext_match);
244 * Returns true iff a lock with the description \a has provides at least the
245 * same guarantees as a lock with the description \a need.
247 int cl_lock_descr_match(const struct cl_lock_descr *has,
248 const struct cl_lock_descr *need)
251 cl_object_same(has->cld_obj, need->cld_obj) &&
252 cl_lock_ext_match(has, need);
254 EXPORT_SYMBOL(cl_lock_descr_match);
256 static void cl_lock_free(const struct lu_env *env, struct cl_lock *lock)
258 struct cl_object *obj = lock->cll_descr.cld_obj;
260 LASSERT(cl_is_lock(lock));
261 LINVRNT(!cl_lock_is_mutexed(lock));
264 cl_lock_trace(D_DLMTRACE, env, "free lock", lock);
266 while (!cfs_list_empty(&lock->cll_layers)) {
267 struct cl_lock_slice *slice;
269 slice = cfs_list_entry(lock->cll_layers.next,
270 struct cl_lock_slice, cls_linkage);
271 cfs_list_del_init(lock->cll_layers.next);
272 slice->cls_ops->clo_fini(env, slice);
274 cfs_atomic_dec(&cl_object_site(obj)->cs_locks.cs_total);
275 cfs_atomic_dec(&cl_object_site(obj)->cs_locks_state[lock->cll_state]);
276 lu_object_ref_del_at(&obj->co_lu, lock->cll_obj_ref, "cl_lock", lock);
277 cl_object_put(env, obj);
278 lu_ref_fini(&lock->cll_reference);
279 lu_ref_fini(&lock->cll_holders);
280 cfs_mutex_destroy(&lock->cll_guard);
281 OBD_SLAB_FREE_PTR(lock, cl_lock_kmem);
286 * Releases a reference on a lock.
288 * When last reference is released, lock is returned to the cache, unless it
289 * is in cl_lock_state::CLS_FREEING state, in which case it is destroyed
292 * \see cl_object_put(), cl_page_put()
294 void cl_lock_put(const struct lu_env *env, struct cl_lock *lock)
296 struct cl_object *obj;
297 struct cl_object_header *head;
298 struct cl_site *site;
300 LINVRNT(cl_lock_invariant(env, lock));
302 obj = lock->cll_descr.cld_obj;
303 LINVRNT(obj != NULL);
304 head = cl_object_header(obj);
305 site = cl_object_site(obj);
307 CDEBUG(D_TRACE, "releasing reference: %d %p %lu\n",
308 cfs_atomic_read(&lock->cll_ref), lock, RETIP);
310 if (cfs_atomic_dec_and_test(&lock->cll_ref)) {
311 if (lock->cll_state == CLS_FREEING) {
312 LASSERT(cfs_list_empty(&lock->cll_linkage));
313 cl_lock_free(env, lock);
315 cfs_atomic_dec(&site->cs_locks.cs_busy);
319 EXPORT_SYMBOL(cl_lock_put);
322 * Acquires an additional reference to a lock.
324 * This can be called only by caller already possessing a reference to \a
327 * \see cl_object_get(), cl_page_get()
329 void cl_lock_get(struct cl_lock *lock)
331 LINVRNT(cl_lock_invariant(NULL, lock));
332 CDEBUG(D_TRACE, "acquiring reference: %d %p %lu\n",
333 cfs_atomic_read(&lock->cll_ref), lock, RETIP);
334 cfs_atomic_inc(&lock->cll_ref);
336 EXPORT_SYMBOL(cl_lock_get);
339 * Acquires a reference to a lock.
341 * This is much like cl_lock_get(), except that this function can be used to
342 * acquire initial reference to the cached lock. Caller has to deal with all
343 * possible races. Use with care!
345 * \see cl_page_get_trust()
347 void cl_lock_get_trust(struct cl_lock *lock)
349 struct cl_site *site = cl_object_site(lock->cll_descr.cld_obj);
351 LASSERT(cl_is_lock(lock));
352 CDEBUG(D_TRACE, "acquiring trusted reference: %d %p %lu\n",
353 cfs_atomic_read(&lock->cll_ref), lock, RETIP);
354 if (cfs_atomic_inc_return(&lock->cll_ref) == 1)
355 cfs_atomic_inc(&site->cs_locks.cs_busy);
357 EXPORT_SYMBOL(cl_lock_get_trust);
360 * Helper function destroying the lock that wasn't completely initialized.
362 * Other threads can acquire references to the top-lock through its
363 * sub-locks. Hence, it cannot be cl_lock_free()-ed immediately.
365 static void cl_lock_finish(const struct lu_env *env, struct cl_lock *lock)
367 cl_lock_mutex_get(env, lock);
368 cl_lock_cancel(env, lock);
369 cl_lock_delete(env, lock);
370 cl_lock_mutex_put(env, lock);
371 cl_lock_put(env, lock);
374 static struct cl_lock *cl_lock_alloc(const struct lu_env *env,
375 struct cl_object *obj,
376 const struct cl_io *io,
377 const struct cl_lock_descr *descr)
379 struct cl_lock *lock;
380 struct lu_object_header *head;
381 struct cl_site *site = cl_object_site(obj);
384 OBD_SLAB_ALLOC_PTR_GFP(lock, cl_lock_kmem, CFS_ALLOC_IO);
386 cfs_atomic_set(&lock->cll_ref, 1);
387 lock->cll_descr = *descr;
388 lock->cll_state = CLS_NEW;
390 lock->cll_obj_ref = lu_object_ref_add(&obj->co_lu,
392 CFS_INIT_LIST_HEAD(&lock->cll_layers);
393 CFS_INIT_LIST_HEAD(&lock->cll_linkage);
394 CFS_INIT_LIST_HEAD(&lock->cll_inclosure);
395 lu_ref_init(&lock->cll_reference);
396 lu_ref_init(&lock->cll_holders);
397 cfs_mutex_init(&lock->cll_guard);
398 cfs_lockdep_set_class(&lock->cll_guard, &cl_lock_guard_class);
399 cfs_waitq_init(&lock->cll_wq);
400 head = obj->co_lu.lo_header;
401 cfs_atomic_inc(&site->cs_locks_state[CLS_NEW]);
402 cfs_atomic_inc(&site->cs_locks.cs_total);
403 cfs_atomic_inc(&site->cs_locks.cs_created);
404 cl_lock_lockdep_init(lock);
405 cfs_list_for_each_entry(obj, &head->loh_layers,
409 err = obj->co_ops->coo_lock_init(env, obj, lock, io);
411 cl_lock_finish(env, lock);
417 lock = ERR_PTR(-ENOMEM);
422 * Transfer the lock into INTRANSIT state and return the original state.
424 * \pre state: CLS_CACHED, CLS_HELD or CLS_ENQUEUED
425 * \post state: CLS_INTRANSIT
428 enum cl_lock_state cl_lock_intransit(const struct lu_env *env,
429 struct cl_lock *lock)
431 enum cl_lock_state state = lock->cll_state;
433 LASSERT(cl_lock_is_mutexed(lock));
434 LASSERT(state != CLS_INTRANSIT);
435 LASSERTF(state >= CLS_ENQUEUED && state <= CLS_CACHED,
436 "Malformed lock state %d.\n", state);
438 cl_lock_state_set(env, lock, CLS_INTRANSIT);
439 lock->cll_intransit_owner = cfs_current();
440 cl_lock_hold_add(env, lock, "intransit", cfs_current());
443 EXPORT_SYMBOL(cl_lock_intransit);
446 * Exit the intransit state and restore the lock state to the original state
448 void cl_lock_extransit(const struct lu_env *env, struct cl_lock *lock,
449 enum cl_lock_state state)
451 LASSERT(cl_lock_is_mutexed(lock));
452 LASSERT(lock->cll_state == CLS_INTRANSIT);
453 LASSERT(state != CLS_INTRANSIT);
454 LASSERT(lock->cll_intransit_owner == cfs_current());
456 lock->cll_intransit_owner = NULL;
457 cl_lock_state_set(env, lock, state);
458 cl_lock_unhold(env, lock, "intransit", cfs_current());
460 EXPORT_SYMBOL(cl_lock_extransit);
463 * Checking whether the lock is intransit state
465 int cl_lock_is_intransit(struct cl_lock *lock)
467 LASSERT(cl_lock_is_mutexed(lock));
468 return lock->cll_state == CLS_INTRANSIT &&
469 lock->cll_intransit_owner != cfs_current();
471 EXPORT_SYMBOL(cl_lock_is_intransit);
473 * Returns true iff lock is "suitable" for given io. E.g., locks acquired by
474 * truncate and O_APPEND cannot be reused for read/non-append-write, as they
475 * cover multiple stripes and can trigger cascading timeouts.
477 static int cl_lock_fits_into(const struct lu_env *env,
478 const struct cl_lock *lock,
479 const struct cl_lock_descr *need,
480 const struct cl_io *io)
482 const struct cl_lock_slice *slice;
484 LINVRNT(cl_lock_invariant_trusted(env, lock));
486 cfs_list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
487 if (slice->cls_ops->clo_fits_into != NULL &&
488 !slice->cls_ops->clo_fits_into(env, slice, need, io))
494 static struct cl_lock *cl_lock_lookup(const struct lu_env *env,
495 struct cl_object *obj,
496 const struct cl_io *io,
497 const struct cl_lock_descr *need)
499 struct cl_lock *lock;
500 struct cl_object_header *head;
501 struct cl_site *site;
505 head = cl_object_header(obj);
506 site = cl_object_site(obj);
507 LINVRNT_SPIN_LOCKED(&head->coh_lock_guard);
508 cfs_atomic_inc(&site->cs_locks.cs_lookup);
509 cfs_list_for_each_entry(lock, &head->coh_locks, cll_linkage) {
512 LASSERT(cl_is_lock(lock));
513 matched = cl_lock_ext_match(&lock->cll_descr, need) &&
514 lock->cll_state < CLS_FREEING &&
515 lock->cll_error == 0 &&
516 !(lock->cll_flags & CLF_CANCELLED) &&
517 cl_lock_fits_into(env, lock, need, io);
518 CDEBUG(D_DLMTRACE, "has: "DDESCR"(%i) need: "DDESCR": %d\n",
519 PDESCR(&lock->cll_descr), lock->cll_state, PDESCR(need),
522 cl_lock_get_trust(lock);
523 cfs_atomic_inc(&cl_object_site(obj)->cs_locks.cs_hit);
531 * Returns a lock matching description \a need.
533 * This is the main entry point into the cl_lock caching interface. First, a
534 * cache (implemented as a per-object linked list) is consulted. If lock is
535 * found there, it is returned immediately. Otherwise new lock is allocated
536 * and returned. In any case, additional reference to lock is acquired.
538 * \see cl_object_find(), cl_page_find()
540 static struct cl_lock *cl_lock_find(const struct lu_env *env,
541 const struct cl_io *io,
542 const struct cl_lock_descr *need)
544 struct cl_object_header *head;
545 struct cl_object *obj;
546 struct cl_lock *lock;
547 struct cl_site *site;
552 head = cl_object_header(obj);
553 site = cl_object_site(obj);
555 cfs_spin_lock(&head->coh_lock_guard);
556 lock = cl_lock_lookup(env, obj, io, need);
557 cfs_spin_unlock(&head->coh_lock_guard);
560 lock = cl_lock_alloc(env, obj, io, need);
562 struct cl_lock *ghost;
564 cfs_spin_lock(&head->coh_lock_guard);
565 ghost = cl_lock_lookup(env, obj, io, need);
567 cfs_list_add_tail(&lock->cll_linkage,
569 cfs_spin_unlock(&head->coh_lock_guard);
570 cfs_atomic_inc(&site->cs_locks.cs_busy);
572 cfs_spin_unlock(&head->coh_lock_guard);
574 * Other threads can acquire references to the
575 * top-lock through its sub-locks. Hence, it
576 * cannot be cl_lock_free()-ed immediately.
578 cl_lock_finish(env, lock);
587 * Returns existing lock matching given description. This is similar to
588 * cl_lock_find() except that no new lock is created, and returned lock is
589 * guaranteed to be in enum cl_lock_state::CLS_HELD state.
591 struct cl_lock *cl_lock_peek(const struct lu_env *env, const struct cl_io *io,
592 const struct cl_lock_descr *need,
593 const char *scope, const void *source)
595 struct cl_object_header *head;
596 struct cl_object *obj;
597 struct cl_lock *lock;
601 head = cl_object_header(obj);
603 cfs_spin_lock(&head->coh_lock_guard);
604 lock = cl_lock_lookup(env, obj, io, need);
605 cfs_spin_unlock(&head->coh_lock_guard);
610 cl_lock_mutex_get(env, lock);
611 if (lock->cll_state == CLS_INTRANSIT)
612 cl_lock_state_wait(env, lock); /* Don't care return value. */
613 if (lock->cll_state == CLS_CACHED) {
615 result = cl_use_try(env, lock, 1);
617 cl_lock_error(env, lock, result);
619 ok = lock->cll_state == CLS_HELD;
621 cl_lock_hold_add(env, lock, scope, source);
622 cl_lock_user_add(env, lock);
623 cl_lock_put(env, lock);
625 cl_lock_mutex_put(env, lock);
627 cl_lock_put(env, lock);
633 EXPORT_SYMBOL(cl_lock_peek);
636 * Returns a slice within a lock, corresponding to the given layer in the
641 const struct cl_lock_slice *cl_lock_at(const struct cl_lock *lock,
642 const struct lu_device_type *dtype)
644 const struct cl_lock_slice *slice;
646 LINVRNT(cl_lock_invariant_trusted(NULL, lock));
649 cfs_list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
650 if (slice->cls_obj->co_lu.lo_dev->ld_type == dtype)
655 EXPORT_SYMBOL(cl_lock_at);
657 static void cl_lock_mutex_tail(const struct lu_env *env, struct cl_lock *lock)
659 struct cl_thread_counters *counters;
661 counters = cl_lock_counters(env, lock);
663 counters->ctc_nr_locks_locked++;
664 lu_ref_add(&counters->ctc_locks_locked, "cll_guard", lock);
665 cl_lock_trace(D_TRACE, env, "got mutex", lock);
669 * Locks cl_lock object.
671 * This is used to manipulate cl_lock fields, and to serialize state
672 * transitions in the lock state machine.
674 * \post cl_lock_is_mutexed(lock)
676 * \see cl_lock_mutex_put()
678 void cl_lock_mutex_get(const struct lu_env *env, struct cl_lock *lock)
680 LINVRNT(cl_lock_invariant(env, lock));
682 if (lock->cll_guarder == cfs_current()) {
683 LINVRNT(cl_lock_is_mutexed(lock));
684 LINVRNT(lock->cll_depth > 0);
686 struct cl_object_header *hdr;
687 struct cl_thread_info *info;
690 LINVRNT(lock->cll_guarder != cfs_current());
691 hdr = cl_object_header(lock->cll_descr.cld_obj);
693 * Check that mutices are taken in the bottom-to-top order.
695 info = cl_env_info(env);
696 for (i = 0; i < hdr->coh_nesting; ++i)
697 LASSERT(info->clt_counters[i].ctc_nr_locks_locked == 0);
698 cfs_mutex_lock_nested(&lock->cll_guard, hdr->coh_nesting);
699 lock->cll_guarder = cfs_current();
700 LINVRNT(lock->cll_depth == 0);
702 cl_lock_mutex_tail(env, lock);
704 EXPORT_SYMBOL(cl_lock_mutex_get);
707 * Try-locks cl_lock object.
709 * \retval 0 \a lock was successfully locked
711 * \retval -EBUSY \a lock cannot be locked right now
713 * \post ergo(result == 0, cl_lock_is_mutexed(lock))
715 * \see cl_lock_mutex_get()
717 int cl_lock_mutex_try(const struct lu_env *env, struct cl_lock *lock)
721 LINVRNT(cl_lock_invariant_trusted(env, lock));
725 if (lock->cll_guarder == cfs_current()) {
726 LINVRNT(lock->cll_depth > 0);
727 cl_lock_mutex_tail(env, lock);
728 } else if (cfs_mutex_trylock(&lock->cll_guard)) {
729 LINVRNT(lock->cll_depth == 0);
730 lock->cll_guarder = cfs_current();
731 cl_lock_mutex_tail(env, lock);
736 EXPORT_SYMBOL(cl_lock_mutex_try);
739 {* Unlocks cl_lock object.
741 * \pre cl_lock_is_mutexed(lock)
743 * \see cl_lock_mutex_get()
745 void cl_lock_mutex_put(const struct lu_env *env, struct cl_lock *lock)
747 struct cl_thread_counters *counters;
749 LINVRNT(cl_lock_invariant(env, lock));
750 LINVRNT(cl_lock_is_mutexed(lock));
751 LINVRNT(lock->cll_guarder == cfs_current());
752 LINVRNT(lock->cll_depth > 0);
754 counters = cl_lock_counters(env, lock);
755 LINVRNT(counters->ctc_nr_locks_locked > 0);
757 cl_lock_trace(D_TRACE, env, "put mutex", lock);
758 lu_ref_del(&counters->ctc_locks_locked, "cll_guard", lock);
759 counters->ctc_nr_locks_locked--;
760 if (--lock->cll_depth == 0) {
761 lock->cll_guarder = NULL;
762 cfs_mutex_unlock(&lock->cll_guard);
765 EXPORT_SYMBOL(cl_lock_mutex_put);
768 * Returns true iff lock's mutex is owned by the current thread.
770 int cl_lock_is_mutexed(struct cl_lock *lock)
772 return lock->cll_guarder == cfs_current();
774 EXPORT_SYMBOL(cl_lock_is_mutexed);
777 * Returns number of cl_lock mutices held by the current thread (environment).
779 int cl_lock_nr_mutexed(const struct lu_env *env)
781 struct cl_thread_info *info;
786 * NOTE: if summation across all nesting levels (currently 2) proves
787 * too expensive, a summary counter can be added to
788 * struct cl_thread_info.
790 info = cl_env_info(env);
791 for (i = 0, locked = 0; i < ARRAY_SIZE(info->clt_counters); ++i)
792 locked += info->clt_counters[i].ctc_nr_locks_locked;
795 EXPORT_SYMBOL(cl_lock_nr_mutexed);
797 static void cl_lock_cancel0(const struct lu_env *env, struct cl_lock *lock)
799 LINVRNT(cl_lock_is_mutexed(lock));
800 LINVRNT(cl_lock_invariant(env, lock));
802 if (!(lock->cll_flags & CLF_CANCELLED)) {
803 const struct cl_lock_slice *slice;
805 lock->cll_flags |= CLF_CANCELLED;
806 cfs_list_for_each_entry_reverse(slice, &lock->cll_layers,
808 if (slice->cls_ops->clo_cancel != NULL)
809 slice->cls_ops->clo_cancel(env, slice);
815 static void cl_lock_delete0(const struct lu_env *env, struct cl_lock *lock)
817 struct cl_object_header *head;
818 const struct cl_lock_slice *slice;
820 LINVRNT(cl_lock_is_mutexed(lock));
821 LINVRNT(cl_lock_invariant(env, lock));
824 if (lock->cll_state < CLS_FREEING) {
825 LASSERT(lock->cll_state != CLS_INTRANSIT);
826 cl_lock_state_set(env, lock, CLS_FREEING);
828 head = cl_object_header(lock->cll_descr.cld_obj);
830 cfs_spin_lock(&head->coh_lock_guard);
831 cfs_list_del_init(&lock->cll_linkage);
833 cfs_spin_unlock(&head->coh_lock_guard);
835 * From now on, no new references to this lock can be acquired
836 * by cl_lock_lookup().
838 cfs_list_for_each_entry_reverse(slice, &lock->cll_layers,
840 if (slice->cls_ops->clo_delete != NULL)
841 slice->cls_ops->clo_delete(env, slice);
844 * From now on, no new references to this lock can be acquired
845 * by layer-specific means (like a pointer from struct
846 * ldlm_lock in osc, or a pointer from top-lock to sub-lock in
849 * Lock will be finally freed in cl_lock_put() when last of
850 * existing references goes away.
857 * Mod(ifie)s cl_lock::cll_holds counter for a given lock. Also, for a
858 * top-lock (nesting == 0) accounts for this modification in the per-thread
859 * debugging counters. Sub-lock holds can be released by a thread different
860 * from one that acquired it.
862 static void cl_lock_hold_mod(const struct lu_env *env, struct cl_lock *lock,
865 struct cl_thread_counters *counters;
866 enum clt_nesting_level nesting;
868 lock->cll_holds += delta;
869 nesting = cl_lock_nesting(lock);
870 if (nesting == CNL_TOP) {
871 counters = &cl_env_info(env)->clt_counters[CNL_TOP];
872 counters->ctc_nr_held += delta;
873 LASSERT(counters->ctc_nr_held >= 0);
878 * Mod(ifie)s cl_lock::cll_users counter for a given lock. See
879 * cl_lock_hold_mod() for the explanation of the debugging code.
881 static void cl_lock_used_mod(const struct lu_env *env, struct cl_lock *lock,
884 struct cl_thread_counters *counters;
885 enum clt_nesting_level nesting;
887 lock->cll_users += delta;
888 nesting = cl_lock_nesting(lock);
889 if (nesting == CNL_TOP) {
890 counters = &cl_env_info(env)->clt_counters[CNL_TOP];
891 counters->ctc_nr_used += delta;
892 LASSERT(counters->ctc_nr_used >= 0);
896 static void cl_lock_hold_release(const struct lu_env *env, struct cl_lock *lock,
897 const char *scope, const void *source)
899 LINVRNT(cl_lock_is_mutexed(lock));
900 LINVRNT(cl_lock_invariant(env, lock));
901 LASSERT(lock->cll_holds > 0);
904 cl_lock_trace(D_DLMTRACE, env, "hold release lock", lock);
905 lu_ref_del(&lock->cll_holders, scope, source);
906 cl_lock_hold_mod(env, lock, -1);
907 if (lock->cll_holds == 0) {
908 if (lock->cll_descr.cld_mode == CLM_PHANTOM ||
909 lock->cll_descr.cld_mode == CLM_GROUP)
911 * If lock is still phantom or grouplock when user is
912 * done with it---destroy the lock.
914 lock->cll_flags |= CLF_CANCELPEND|CLF_DOOMED;
915 if (lock->cll_flags & CLF_CANCELPEND) {
916 lock->cll_flags &= ~CLF_CANCELPEND;
917 cl_lock_cancel0(env, lock);
919 if (lock->cll_flags & CLF_DOOMED) {
920 /* no longer doomed: it's dead... Jim. */
921 lock->cll_flags &= ~CLF_DOOMED;
922 cl_lock_delete0(env, lock);
930 * Waits until lock state is changed.
932 * This function is called with cl_lock mutex locked, atomically releases
933 * mutex and goes to sleep, waiting for a lock state change (signaled by
934 * cl_lock_signal()), and re-acquires the mutex before return.
936 * This function is used to wait until lock state machine makes some progress
937 * and to emulate synchronous operations on top of asynchronous lock
940 * \retval -EINTR wait was interrupted
942 * \retval 0 wait wasn't interrupted
944 * \pre cl_lock_is_mutexed(lock)
946 * \see cl_lock_signal()
948 int cl_lock_state_wait(const struct lu_env *env, struct cl_lock *lock)
950 cfs_waitlink_t waiter;
954 LINVRNT(cl_lock_is_mutexed(lock));
955 LINVRNT(cl_lock_invariant(env, lock));
956 LASSERT(lock->cll_depth == 1);
957 LASSERT(lock->cll_state != CLS_FREEING); /* too late to wait */
959 cl_lock_trace(D_DLMTRACE, env, "state wait lock", lock);
960 result = lock->cll_error;
962 cfs_waitlink_init(&waiter);
963 cfs_waitq_add(&lock->cll_wq, &waiter);
964 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
965 cl_lock_mutex_put(env, lock);
967 LASSERT(cl_lock_nr_mutexed(env) == 0);
968 cfs_waitq_wait(&waiter, CFS_TASK_INTERRUPTIBLE);
970 cl_lock_mutex_get(env, lock);
971 cfs_set_current_state(CFS_TASK_RUNNING);
972 cfs_waitq_del(&lock->cll_wq, &waiter);
973 result = cfs_signal_pending() ? -EINTR : 0;
977 EXPORT_SYMBOL(cl_lock_state_wait);
979 static void cl_lock_state_signal(const struct lu_env *env, struct cl_lock *lock,
980 enum cl_lock_state state)
982 const struct cl_lock_slice *slice;
985 LINVRNT(cl_lock_is_mutexed(lock));
986 LINVRNT(cl_lock_invariant(env, lock));
988 cfs_list_for_each_entry(slice, &lock->cll_layers, cls_linkage)
989 if (slice->cls_ops->clo_state != NULL)
990 slice->cls_ops->clo_state(env, slice, state);
991 cfs_waitq_broadcast(&lock->cll_wq);
996 * Notifies waiters that lock state changed.
998 * Wakes up all waiters sleeping in cl_lock_state_wait(), also notifies all
999 * layers about state change by calling cl_lock_operations::clo_state()
1002 void cl_lock_signal(const struct lu_env *env, struct cl_lock *lock)
1005 cl_lock_trace(D_DLMTRACE, env, "state signal lock", lock);
1006 cl_lock_state_signal(env, lock, lock->cll_state);
1009 EXPORT_SYMBOL(cl_lock_signal);
1012 * Changes lock state.
1014 * This function is invoked to notify layers that lock state changed, possible
1015 * as a result of an asynchronous event such as call-back reception.
1017 * \post lock->cll_state == state
1019 * \see cl_lock_operations::clo_state()
1021 void cl_lock_state_set(const struct lu_env *env, struct cl_lock *lock,
1022 enum cl_lock_state state)
1024 struct cl_site *site = cl_object_site(lock->cll_descr.cld_obj);
1027 LASSERT(lock->cll_state <= state ||
1028 (lock->cll_state == CLS_CACHED &&
1029 (state == CLS_HELD || /* lock found in cache */
1030 state == CLS_NEW || /* sub-lock canceled */
1031 state == CLS_INTRANSIT)) ||
1032 /* lock is in transit state */
1033 lock->cll_state == CLS_INTRANSIT);
1035 if (lock->cll_state != state) {
1036 cfs_atomic_dec(&site->cs_locks_state[lock->cll_state]);
1037 cfs_atomic_inc(&site->cs_locks_state[state]);
1039 cl_lock_state_signal(env, lock, state);
1040 lock->cll_state = state;
1044 EXPORT_SYMBOL(cl_lock_state_set);
1046 static int cl_unuse_try_internal(const struct lu_env *env, struct cl_lock *lock)
1048 const struct cl_lock_slice *slice;
1054 LINVRNT(cl_lock_is_mutexed(lock));
1055 LINVRNT(cl_lock_invariant(env, lock));
1056 LASSERT(lock->cll_state == CLS_INTRANSIT);
1059 cfs_list_for_each_entry_reverse(slice, &lock->cll_layers,
1061 if (slice->cls_ops->clo_unuse != NULL) {
1062 result = slice->cls_ops->clo_unuse(env, slice);
1067 LASSERT(result != -ENOSYS);
1068 } while (result == CLO_REPEAT);
1074 * Yanks lock from the cache (cl_lock_state::CLS_CACHED state) by calling
1075 * cl_lock_operations::clo_use() top-to-bottom to notify layers.
1076 * @atomic = 1, it must unuse the lock to recovery the lock to keep the
1077 * use process atomic
1079 int cl_use_try(const struct lu_env *env, struct cl_lock *lock, int atomic)
1081 const struct cl_lock_slice *slice;
1083 enum cl_lock_state state;
1086 cl_lock_trace(D_DLMTRACE, env, "use lock", lock);
1088 LASSERT(lock->cll_state == CLS_CACHED);
1089 if (lock->cll_error)
1090 RETURN(lock->cll_error);
1093 state = cl_lock_intransit(env, lock);
1094 cfs_list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
1095 if (slice->cls_ops->clo_use != NULL) {
1096 result = slice->cls_ops->clo_use(env, slice);
1101 LASSERT(result != -ENOSYS);
1103 LASSERTF(lock->cll_state == CLS_INTRANSIT, "Wrong state %d.\n",
1109 if (result == -ESTALE) {
1111 * ESTALE means sublock being cancelled
1112 * at this time, and set lock state to
1113 * be NEW here and ask the caller to repeat.
1116 result = CLO_REPEAT;
1119 /* @atomic means back-off-on-failure. */
1122 rc = cl_unuse_try_internal(env, lock);
1123 /* Vet the results. */
1124 if (rc < 0 && result > 0)
1129 cl_lock_extransit(env, lock, state);
1132 EXPORT_SYMBOL(cl_use_try);
1135 * Helper for cl_enqueue_try() that calls ->clo_enqueue() across all layers
1138 static int cl_enqueue_kick(const struct lu_env *env,
1139 struct cl_lock *lock,
1140 struct cl_io *io, __u32 flags)
1143 const struct cl_lock_slice *slice;
1147 cfs_list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
1148 if (slice->cls_ops->clo_enqueue != NULL) {
1149 result = slice->cls_ops->clo_enqueue(env,
1155 LASSERT(result != -ENOSYS);
1160 * Tries to enqueue a lock.
1162 * This function is called repeatedly by cl_enqueue() until either lock is
1163 * enqueued, or error occurs. This function does not block waiting for
1164 * networking communication to complete.
1166 * \post ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1167 * lock->cll_state == CLS_HELD)
1169 * \see cl_enqueue() cl_lock_operations::clo_enqueue()
1170 * \see cl_lock_state::CLS_ENQUEUED
1172 int cl_enqueue_try(const struct lu_env *env, struct cl_lock *lock,
1173 struct cl_io *io, __u32 flags)
1178 cl_lock_trace(D_DLMTRACE, env, "enqueue lock", lock);
1182 LINVRNT(cl_lock_is_mutexed(lock));
1184 if (lock->cll_error != 0)
1186 switch (lock->cll_state) {
1188 cl_lock_state_set(env, lock, CLS_QUEUING);
1192 result = cl_enqueue_kick(env, lock, io, flags);
1194 cl_lock_state_set(env, lock, CLS_ENQUEUED);
1197 LASSERT(cl_lock_is_intransit(lock));
1201 /* yank lock from the cache. */
1202 result = cl_use_try(env, lock, 0);
1211 * impossible, only held locks with increased
1212 * ->cll_holds can be enqueued, and they cannot be
1217 } while (result == CLO_REPEAT);
1219 cl_lock_error(env, lock, result);
1220 RETURN(result ?: lock->cll_error);
1222 EXPORT_SYMBOL(cl_enqueue_try);
1224 static int cl_enqueue_locked(const struct lu_env *env, struct cl_lock *lock,
1225 struct cl_io *io, __u32 enqflags)
1231 LINVRNT(cl_lock_is_mutexed(lock));
1232 LINVRNT(cl_lock_invariant(env, lock));
1233 LASSERT(lock->cll_holds > 0);
1235 cl_lock_user_add(env, lock);
1237 result = cl_enqueue_try(env, lock, io, enqflags);
1238 if (result == CLO_WAIT) {
1239 result = cl_lock_state_wait(env, lock);
1246 cl_lock_user_del(env, lock);
1247 cl_lock_error(env, lock, result);
1249 LASSERT(ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1250 lock->cll_state == CLS_HELD));
1257 * \pre current thread or io owns a hold on lock.
1259 * \post ergo(result == 0, lock->users increased)
1260 * \post ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1261 * lock->cll_state == CLS_HELD)
1263 int cl_enqueue(const struct lu_env *env, struct cl_lock *lock,
1264 struct cl_io *io, __u32 enqflags)
1270 cl_lock_lockdep_acquire(env, lock, enqflags);
1271 cl_lock_mutex_get(env, lock);
1272 result = cl_enqueue_locked(env, lock, io, enqflags);
1273 cl_lock_mutex_put(env, lock);
1275 cl_lock_lockdep_release(env, lock);
1276 LASSERT(ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1277 lock->cll_state == CLS_HELD));
1280 EXPORT_SYMBOL(cl_enqueue);
1283 * Tries to unlock a lock.
1285 * This function is called repeatedly by cl_unuse() until either lock is
1286 * unlocked, or error occurs.
1287 * cl_unuse_try is a one-shot operation, so it must NOT return CLO_WAIT.
1289 * \pre lock->cll_state == CLS_HELD
1291 * \post ergo(result == 0, lock->cll_state == CLS_CACHED)
1293 * \see cl_unuse() cl_lock_operations::clo_unuse()
1294 * \see cl_lock_state::CLS_CACHED
1296 int cl_unuse_try(const struct lu_env *env, struct cl_lock *lock)
1299 enum cl_lock_state state = CLS_NEW;
1302 cl_lock_trace(D_DLMTRACE, env, "unuse lock", lock);
1304 LASSERT(lock->cll_state == CLS_HELD || lock->cll_state == CLS_ENQUEUED);
1305 if (lock->cll_users > 1) {
1306 cl_lock_user_del(env, lock);
1311 * New lock users (->cll_users) are not protecting unlocking
1312 * from proceeding. From this point, lock eventually reaches
1313 * CLS_CACHED, is reinitialized to CLS_NEW or fails into
1316 state = cl_lock_intransit(env, lock);
1318 result = cl_unuse_try_internal(env, lock);
1319 LASSERT(lock->cll_state == CLS_INTRANSIT);
1320 LASSERT(result != CLO_WAIT);
1321 cl_lock_user_del(env, lock);
1322 if (result == 0 || result == -ESTALE) {
1324 * Return lock back to the cache. This is the only
1325 * place where lock is moved into CLS_CACHED state.
1327 * If one of ->clo_unuse() methods returned -ESTALE, lock
1328 * cannot be placed into cache and has to be
1329 * re-initialized. This happens e.g., when a sub-lock was
1330 * canceled while unlocking was in progress.
1332 if (state == CLS_HELD && result == 0)
1336 cl_lock_extransit(env, lock, state);
1339 * Hide -ESTALE error.
1340 * If the lock is a glimpse lock, and it has multiple
1341 * stripes. Assuming that one of its sublock returned -ENAVAIL,
1342 * and other sublocks are matched write locks. In this case,
1343 * we can't set this lock to error because otherwise some of
1344 * its sublocks may not be canceled. This causes some dirty
1345 * pages won't be written to OSTs. -jay
1349 CERROR("result = %d, this is unlikely!\n", result);
1350 cl_lock_extransit(env, lock, state);
1353 result = result ?: lock->cll_error;
1355 cl_lock_error(env, lock, result);
1358 EXPORT_SYMBOL(cl_unuse_try);
1360 static void cl_unuse_locked(const struct lu_env *env, struct cl_lock *lock)
1365 result = cl_unuse_try(env, lock);
1367 CL_LOCK_DEBUG(D_ERROR, env, lock, "unuse return %d\n", result);
1375 void cl_unuse(const struct lu_env *env, struct cl_lock *lock)
1378 cl_lock_mutex_get(env, lock);
1379 cl_unuse_locked(env, lock);
1380 cl_lock_mutex_put(env, lock);
1381 cl_lock_lockdep_release(env, lock);
1384 EXPORT_SYMBOL(cl_unuse);
1387 * Tries to wait for a lock.
1389 * This function is called repeatedly by cl_wait() until either lock is
1390 * granted, or error occurs. This function does not block waiting for network
1391 * communication to complete.
1393 * \see cl_wait() cl_lock_operations::clo_wait()
1394 * \see cl_lock_state::CLS_HELD
1396 int cl_wait_try(const struct lu_env *env, struct cl_lock *lock)
1398 const struct cl_lock_slice *slice;
1402 cl_lock_trace(D_DLMTRACE, env, "wait lock try", lock);
1404 LINVRNT(cl_lock_is_mutexed(lock));
1405 LINVRNT(cl_lock_invariant(env, lock));
1406 LASSERT(lock->cll_state == CLS_ENQUEUED ||
1407 lock->cll_state == CLS_HELD ||
1408 lock->cll_state == CLS_INTRANSIT);
1409 LASSERT(lock->cll_users > 0);
1410 LASSERT(lock->cll_holds > 0);
1413 if (lock->cll_error != 0)
1416 if (cl_lock_is_intransit(lock)) {
1421 if (lock->cll_state == CLS_HELD)
1426 cfs_list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
1427 if (slice->cls_ops->clo_wait != NULL) {
1428 result = slice->cls_ops->clo_wait(env, slice);
1433 LASSERT(result != -ENOSYS);
1435 LASSERT(lock->cll_state != CLS_INTRANSIT);
1436 cl_lock_state_set(env, lock, CLS_HELD);
1438 } while (result == CLO_REPEAT);
1439 RETURN(result ?: lock->cll_error);
1441 EXPORT_SYMBOL(cl_wait_try);
1444 * Waits until enqueued lock is granted.
1446 * \pre current thread or io owns a hold on the lock
1447 * \pre ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1448 * lock->cll_state == CLS_HELD)
1450 * \post ergo(result == 0, lock->cll_state == CLS_HELD)
1452 int cl_wait(const struct lu_env *env, struct cl_lock *lock)
1457 cl_lock_mutex_get(env, lock);
1459 LINVRNT(cl_lock_invariant(env, lock));
1460 LASSERTF(lock->cll_state == CLS_ENQUEUED || lock->cll_state == CLS_HELD,
1461 "Wrong state %d \n", lock->cll_state);
1462 LASSERT(lock->cll_holds > 0);
1463 cl_lock_trace(D_DLMTRACE, env, "wait lock", lock);
1466 result = cl_wait_try(env, lock);
1467 if (result == CLO_WAIT) {
1468 result = cl_lock_state_wait(env, lock);
1475 cl_lock_user_del(env, lock);
1476 cl_lock_error(env, lock, result);
1477 cl_lock_lockdep_release(env, lock);
1479 cl_lock_mutex_put(env, lock);
1480 LASSERT(ergo(result == 0, lock->cll_state == CLS_HELD));
1483 EXPORT_SYMBOL(cl_wait);
1486 * Executes cl_lock_operations::clo_weigh(), and sums results to estimate lock
1489 unsigned long cl_lock_weigh(const struct lu_env *env, struct cl_lock *lock)
1491 const struct cl_lock_slice *slice;
1492 unsigned long pound;
1493 unsigned long ounce;
1496 LINVRNT(cl_lock_is_mutexed(lock));
1497 LINVRNT(cl_lock_invariant(env, lock));
1500 cfs_list_for_each_entry_reverse(slice, &lock->cll_layers, cls_linkage) {
1501 if (slice->cls_ops->clo_weigh != NULL) {
1502 ounce = slice->cls_ops->clo_weigh(env, slice);
1504 if (pound < ounce) /* over-weight^Wflow */
1510 EXPORT_SYMBOL(cl_lock_weigh);
1513 * Notifies layers that lock description changed.
1515 * The server can grant client a lock different from one that was requested
1516 * (e.g., larger in extent). This method is called when actually granted lock
1517 * description becomes known to let layers to accommodate for changed lock
1520 * \see cl_lock_operations::clo_modify()
1522 int cl_lock_modify(const struct lu_env *env, struct cl_lock *lock,
1523 const struct cl_lock_descr *desc)
1525 const struct cl_lock_slice *slice;
1526 struct cl_object *obj = lock->cll_descr.cld_obj;
1527 struct cl_object_header *hdr = cl_object_header(obj);
1531 cl_lock_trace(D_DLMTRACE, env, "modify lock", lock);
1532 /* don't allow object to change */
1533 LASSERT(obj == desc->cld_obj);
1534 LINVRNT(cl_lock_is_mutexed(lock));
1535 LINVRNT(cl_lock_invariant(env, lock));
1537 cfs_list_for_each_entry_reverse(slice, &lock->cll_layers, cls_linkage) {
1538 if (slice->cls_ops->clo_modify != NULL) {
1539 result = slice->cls_ops->clo_modify(env, slice, desc);
1544 CL_LOCK_DEBUG(D_DLMTRACE, env, lock, " -> "DDESCR"@"DFID"\n",
1545 PDESCR(desc), PFID(lu_object_fid(&desc->cld_obj->co_lu)));
1547 * Just replace description in place. Nothing more is needed for
1548 * now. If locks were indexed according to their extent and/or mode,
1549 * that index would have to be updated here.
1551 cfs_spin_lock(&hdr->coh_lock_guard);
1552 lock->cll_descr = *desc;
1553 cfs_spin_unlock(&hdr->coh_lock_guard);
1556 EXPORT_SYMBOL(cl_lock_modify);
1559 * Initializes lock closure with a given origin.
1561 * \see cl_lock_closure
1563 void cl_lock_closure_init(const struct lu_env *env,
1564 struct cl_lock_closure *closure,
1565 struct cl_lock *origin, int wait)
1567 LINVRNT(cl_lock_is_mutexed(origin));
1568 LINVRNT(cl_lock_invariant(env, origin));
1570 CFS_INIT_LIST_HEAD(&closure->clc_list);
1571 closure->clc_origin = origin;
1572 closure->clc_wait = wait;
1573 closure->clc_nr = 0;
1575 EXPORT_SYMBOL(cl_lock_closure_init);
1578 * Builds a closure of \a lock.
1580 * Building of a closure consists of adding initial lock (\a lock) into it,
1581 * and calling cl_lock_operations::clo_closure() methods of \a lock. These
1582 * methods might call cl_lock_closure_build() recursively again, adding more
1583 * locks to the closure, etc.
1585 * \see cl_lock_closure
1587 int cl_lock_closure_build(const struct lu_env *env, struct cl_lock *lock,
1588 struct cl_lock_closure *closure)
1590 const struct cl_lock_slice *slice;
1594 LINVRNT(cl_lock_is_mutexed(closure->clc_origin));
1595 LINVRNT(cl_lock_invariant(env, closure->clc_origin));
1597 result = cl_lock_enclosure(env, lock, closure);
1599 cfs_list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
1600 if (slice->cls_ops->clo_closure != NULL) {
1601 result = slice->cls_ops->clo_closure(env, slice,
1609 cl_lock_disclosure(env, closure);
1612 EXPORT_SYMBOL(cl_lock_closure_build);
1615 * Adds new lock to a closure.
1617 * Try-locks \a lock and if succeeded, adds it to the closure (never more than
1618 * once). If try-lock failed, returns CLO_REPEAT, after optionally waiting
1619 * until next try-lock is likely to succeed.
1621 int cl_lock_enclosure(const struct lu_env *env, struct cl_lock *lock,
1622 struct cl_lock_closure *closure)
1626 cl_lock_trace(D_DLMTRACE, env, "enclosure lock", lock);
1627 if (!cl_lock_mutex_try(env, lock)) {
1629 * If lock->cll_inclosure is not empty, lock is already in
1632 if (cfs_list_empty(&lock->cll_inclosure)) {
1633 cl_lock_get_trust(lock);
1634 lu_ref_add(&lock->cll_reference, "closure", closure);
1635 cfs_list_add(&lock->cll_inclosure, &closure->clc_list);
1638 cl_lock_mutex_put(env, lock);
1641 cl_lock_disclosure(env, closure);
1642 if (closure->clc_wait) {
1643 cl_lock_get_trust(lock);
1644 lu_ref_add(&lock->cll_reference, "closure-w", closure);
1645 cl_lock_mutex_put(env, closure->clc_origin);
1647 LASSERT(cl_lock_nr_mutexed(env) == 0);
1648 cl_lock_mutex_get(env, lock);
1649 cl_lock_mutex_put(env, lock);
1651 cl_lock_mutex_get(env, closure->clc_origin);
1652 lu_ref_del(&lock->cll_reference, "closure-w", closure);
1653 cl_lock_put(env, lock);
1655 result = CLO_REPEAT;
1659 EXPORT_SYMBOL(cl_lock_enclosure);
1661 /** Releases mutices of enclosed locks. */
1662 void cl_lock_disclosure(const struct lu_env *env,
1663 struct cl_lock_closure *closure)
1665 struct cl_lock *scan;
1666 struct cl_lock *temp;
1668 cl_lock_trace(D_DLMTRACE, env, "disclosure lock", closure->clc_origin);
1669 cfs_list_for_each_entry_safe(scan, temp, &closure->clc_list,
1671 cfs_list_del_init(&scan->cll_inclosure);
1672 cl_lock_mutex_put(env, scan);
1673 lu_ref_del(&scan->cll_reference, "closure", closure);
1674 cl_lock_put(env, scan);
1677 LASSERT(closure->clc_nr == 0);
1679 EXPORT_SYMBOL(cl_lock_disclosure);
1681 /** Finalizes a closure. */
1682 void cl_lock_closure_fini(struct cl_lock_closure *closure)
1684 LASSERT(closure->clc_nr == 0);
1685 LASSERT(cfs_list_empty(&closure->clc_list));
1687 EXPORT_SYMBOL(cl_lock_closure_fini);
1690 * Destroys this lock. Notifies layers (bottom-to-top) that lock is being
1691 * destroyed, then destroy the lock. If there are holds on the lock, postpone
1692 * destruction until all holds are released. This is called when a decision is
1693 * made to destroy the lock in the future. E.g., when a blocking AST is
1694 * received on it, or fatal communication error happens.
1696 * Caller must have a reference on this lock to prevent a situation, when
1697 * deleted lock lingers in memory for indefinite time, because nobody calls
1698 * cl_lock_put() to finish it.
1700 * \pre atomic_read(&lock->cll_ref) > 0
1701 * \pre ergo(cl_lock_nesting(lock) == CNL_TOP,
1702 * cl_lock_nr_mutexed(env) == 1)
1703 * [i.e., if a top-lock is deleted, mutices of no other locks can be
1704 * held, as deletion of sub-locks might require releasing a top-lock
1707 * \see cl_lock_operations::clo_delete()
1708 * \see cl_lock::cll_holds
1710 void cl_lock_delete(const struct lu_env *env, struct cl_lock *lock)
1712 LINVRNT(cl_lock_is_mutexed(lock));
1713 LINVRNT(cl_lock_invariant(env, lock));
1714 LASSERT(ergo(cl_lock_nesting(lock) == CNL_TOP,
1715 cl_lock_nr_mutexed(env) == 1));
1718 cl_lock_trace(D_DLMTRACE, env, "delete lock", lock);
1719 if (lock->cll_holds == 0)
1720 cl_lock_delete0(env, lock);
1722 lock->cll_flags |= CLF_DOOMED;
1725 EXPORT_SYMBOL(cl_lock_delete);
1728 * Mark lock as irrecoverably failed, and mark it for destruction. This
1729 * happens when, e.g., server fails to grant a lock to us, or networking
1732 * \pre atomic_read(&lock->cll_ref) > 0
1734 * \see clo_lock_delete()
1735 * \see cl_lock::cll_holds
1737 void cl_lock_error(const struct lu_env *env, struct cl_lock *lock, int error)
1739 LINVRNT(cl_lock_is_mutexed(lock));
1740 LINVRNT(cl_lock_invariant(env, lock));
1743 cl_lock_trace(D_DLMTRACE, env, "set lock error", lock);
1744 if (lock->cll_error == 0 && error != 0) {
1745 lock->cll_error = error;
1746 cl_lock_signal(env, lock);
1747 cl_lock_cancel(env, lock);
1748 cl_lock_delete(env, lock);
1752 EXPORT_SYMBOL(cl_lock_error);
1755 * Cancels this lock. Notifies layers
1756 * (bottom-to-top) that lock is being cancelled, then destroy the lock. If
1757 * there are holds on the lock, postpone cancellation until
1758 * all holds are released.
1760 * Cancellation notification is delivered to layers at most once.
1762 * \see cl_lock_operations::clo_cancel()
1763 * \see cl_lock::cll_holds
1765 void cl_lock_cancel(const struct lu_env *env, struct cl_lock *lock)
1767 LINVRNT(cl_lock_is_mutexed(lock));
1768 LINVRNT(cl_lock_invariant(env, lock));
1771 cl_lock_trace(D_DLMTRACE, env, "cancel lock", lock);
1772 if (lock->cll_holds == 0)
1773 cl_lock_cancel0(env, lock);
1775 lock->cll_flags |= CLF_CANCELPEND;
1778 EXPORT_SYMBOL(cl_lock_cancel);
1781 * Finds an existing lock covering given page and optionally different from a
1782 * given \a except lock.
1784 struct cl_lock *cl_lock_at_page(const struct lu_env *env, struct cl_object *obj,
1785 struct cl_page *page, struct cl_lock *except,
1786 int pending, int canceld)
1788 struct cl_object_header *head;
1789 struct cl_lock *scan;
1790 struct cl_lock *lock;
1791 struct cl_lock_descr *need;
1795 head = cl_object_header(obj);
1796 need = &cl_env_info(env)->clt_descr;
1799 need->cld_mode = CLM_READ; /* CLM_READ matches both READ & WRITE, but
1801 need->cld_start = need->cld_end = page->cp_index;
1802 need->cld_enq_flags = 0;
1804 cfs_spin_lock(&head->coh_lock_guard);
1805 /* It is fine to match any group lock since there could be only one
1806 * with a uniq gid and it conflicts with all other lock modes too */
1807 cfs_list_for_each_entry(scan, &head->coh_locks, cll_linkage) {
1808 if (scan != except &&
1809 (scan->cll_descr.cld_mode == CLM_GROUP ||
1810 cl_lock_ext_match(&scan->cll_descr, need)) &&
1811 scan->cll_state >= CLS_HELD &&
1812 scan->cll_state < CLS_FREEING &&
1814 * This check is racy as the lock can be canceled right
1815 * after it is done, but this is fine, because page exists
1818 (canceld || !(scan->cll_flags & CLF_CANCELLED)) &&
1819 (pending || !(scan->cll_flags & CLF_CANCELPEND))) {
1820 /* Don't increase cs_hit here since this
1821 * is just a helper function. */
1822 cl_lock_get_trust(scan);
1827 cfs_spin_unlock(&head->coh_lock_guard);
1830 EXPORT_SYMBOL(cl_lock_at_page);
1833 * Returns a list of pages protected (only) by a given lock.
1835 * Scans an extent of page radix tree, corresponding to the \a lock and queues
1836 * all pages that are not protected by locks other than \a lock into \a queue.
1838 void cl_lock_page_list_fixup(const struct lu_env *env,
1839 struct cl_io *io, struct cl_lock *lock,
1840 struct cl_page_list *queue)
1842 struct cl_page *page;
1843 struct cl_page *temp;
1844 struct cl_page_list *plist = &cl_env_info(env)->clt_list;
1846 LINVRNT(cl_lock_invariant(env, lock));
1849 /* Now, we have a list of cl_pages under the \a lock, we need
1850 * to check if some of pages are covered by other ldlm lock.
1851 * If this is the case, they aren't needed to be written out this time.
1853 * For example, we have A:[0,200] & B:[100,300] PW locks on client, now
1854 * the latter is to be canceled, this means other client is
1855 * reading/writing [200,300] since A won't canceled. Actually
1856 * we just need to write the pages covered by [200,300]. This is safe,
1857 * since [100,200] is also protected lock A.
1860 cl_page_list_init(plist);
1861 cl_page_list_for_each_safe(page, temp, queue) {
1862 pgoff_t idx = page->cp_index;
1863 struct cl_lock *found;
1864 struct cl_lock_descr *descr;
1866 /* The algorithm counts on the index-ascending page index. */
1867 LASSERT(ergo(&temp->cp_batch != &queue->pl_pages,
1868 page->cp_index < temp->cp_index));
1870 found = cl_lock_at_page(env, lock->cll_descr.cld_obj,
1875 descr = &found->cll_descr;
1876 cfs_list_for_each_entry_safe_from(page, temp, &queue->pl_pages,
1878 idx = page->cp_index;
1879 if (descr->cld_start > idx || descr->cld_end < idx)
1881 cl_page_list_move(plist, queue, page);
1883 cl_lock_put(env, found);
1886 /* The pages in plist are covered by other locks, don't handle them
1890 cl_page_list_disown(env, io, plist);
1891 cl_page_list_fini(env, plist);
1894 EXPORT_SYMBOL(cl_lock_page_list_fixup);
1897 * Invalidate pages protected by the given lock, sending them out to the
1898 * server first, if necessary.
1900 * This function does the following:
1902 * - collects a list of pages to be invalidated,
1904 * - unmaps them from the user virtual memory,
1906 * - sends dirty pages to the server,
1908 * - waits for transfer completion,
1910 * - discards pages, and throws them out of memory.
1912 * If \a discard is set, pages are discarded without sending them to the
1915 * If error happens on any step, the process continues anyway (the reasoning
1916 * behind this being that lock cancellation cannot be delayed indefinitely).
1918 int cl_lock_page_out(const struct lu_env *env, struct cl_lock *lock,
1921 struct cl_thread_info *info = cl_env_info(env);
1922 struct cl_io *io = &info->clt_io;
1923 struct cl_2queue *queue = &info->clt_queue;
1924 struct cl_lock_descr *descr = &lock->cll_descr;
1928 LINVRNT(cl_lock_invariant(env, lock));
1931 io->ci_obj = cl_object_top(descr->cld_obj);
1932 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
1937 cl_2queue_init(queue);
1938 cl_page_gang_lookup(env, descr->cld_obj, io, descr->cld_start,
1939 descr->cld_end, &queue->c2_qin, nonblock);
1940 page_count = queue->c2_qin.pl_nr;
1941 if (page_count > 0) {
1942 result = cl_page_list_unmap(env, io, &queue->c2_qin);
1944 long timeout = 600; /* 10 minutes. */
1945 /* for debug purpose, if this request can't be
1946 * finished in 10 minutes, we hope it can
1949 result = cl_io_submit_sync(env, io, CRT_WRITE,
1953 CWARN("Writing %lu pages error: %d\n",
1954 page_count, result);
1956 cl_lock_page_list_fixup(env, io, lock, &queue->c2_qout);
1957 cl_2queue_discard(env, io, queue);
1958 cl_2queue_disown(env, io, queue);
1960 cl_2queue_fini(env, queue);
1967 cl_io_fini(env, io);
1970 EXPORT_SYMBOL(cl_lock_page_out);
1973 * Eliminate all locks for a given object.
1975 * Caller has to guarantee that no lock is in active use.
1977 * \param cancel when this is set, cl_locks_prune() cancels locks before
1980 void cl_locks_prune(const struct lu_env *env, struct cl_object *obj, int cancel)
1982 struct cl_object_header *head;
1983 struct cl_lock *lock;
1986 head = cl_object_header(obj);
1988 * If locks are destroyed without cancellation, all pages must be
1989 * already destroyed (as otherwise they will be left unprotected).
1991 LASSERT(ergo(!cancel,
1992 head->coh_tree.rnode == NULL && head->coh_pages == 0));
1994 cfs_spin_lock(&head->coh_lock_guard);
1995 while (!cfs_list_empty(&head->coh_locks)) {
1996 lock = container_of(head->coh_locks.next,
1997 struct cl_lock, cll_linkage);
1998 cl_lock_get_trust(lock);
1999 cfs_spin_unlock(&head->coh_lock_guard);
2000 lu_ref_add(&lock->cll_reference, "prune", cfs_current());
2001 cl_lock_mutex_get(env, lock);
2002 if (lock->cll_state < CLS_FREEING) {
2003 LASSERT(lock->cll_holds == 0);
2004 LASSERT(lock->cll_users == 0);
2006 cl_lock_cancel(env, lock);
2007 cl_lock_delete(env, lock);
2009 cl_lock_mutex_put(env, lock);
2010 lu_ref_del(&lock->cll_reference, "prune", cfs_current());
2011 cl_lock_put(env, lock);
2012 cfs_spin_lock(&head->coh_lock_guard);
2014 cfs_spin_unlock(&head->coh_lock_guard);
2017 EXPORT_SYMBOL(cl_locks_prune);
2020 * Returns true if \a addr is an address of an allocated cl_lock. Used in
2021 * assertions. This check is optimistically imprecise, i.e., it occasionally
2022 * returns true for the incorrect addresses, but if it returns false, then the
2023 * address is guaranteed to be incorrect. (Should be named cl_lockp().)
2027 int cl_is_lock(const void *addr)
2029 return cfs_mem_is_in_cache(addr, cl_lock_kmem);
2031 EXPORT_SYMBOL(cl_is_lock);
2033 static struct cl_lock *cl_lock_hold_mutex(const struct lu_env *env,
2034 const struct cl_io *io,
2035 const struct cl_lock_descr *need,
2036 const char *scope, const void *source)
2038 struct cl_lock *lock;
2043 lock = cl_lock_find(env, io, need);
2046 cl_lock_mutex_get(env, lock);
2047 if (lock->cll_state < CLS_FREEING &&
2048 !(lock->cll_flags & CLF_CANCELLED)) {
2049 cl_lock_hold_mod(env, lock, +1);
2050 lu_ref_add(&lock->cll_holders, scope, source);
2051 lu_ref_add(&lock->cll_reference, scope, source);
2054 cl_lock_mutex_put(env, lock);
2055 cl_lock_put(env, lock);
2061 * Returns a lock matching \a need description with a reference and a hold on
2064 * This is much like cl_lock_find(), except that cl_lock_hold() additionally
2065 * guarantees that lock is not in the CLS_FREEING state on return.
2067 struct cl_lock *cl_lock_hold(const struct lu_env *env, const struct cl_io *io,
2068 const struct cl_lock_descr *need,
2069 const char *scope, const void *source)
2071 struct cl_lock *lock;
2075 lock = cl_lock_hold_mutex(env, io, need, scope, source);
2077 cl_lock_mutex_put(env, lock);
2080 EXPORT_SYMBOL(cl_lock_hold);
2083 * Main high-level entry point of cl_lock interface that finds existing or
2084 * enqueues new lock matching given description.
2086 struct cl_lock *cl_lock_request(const struct lu_env *env, struct cl_io *io,
2087 const struct cl_lock_descr *need,
2088 const char *scope, const void *source)
2090 struct cl_lock *lock;
2091 const struct lu_fid *fid;
2094 __u32 enqflags = need->cld_enq_flags;
2097 fid = lu_object_fid(&io->ci_obj->co_lu);
2100 lock = cl_lock_hold_mutex(env, io, need, scope, source);
2101 if (!IS_ERR(lock)) {
2102 rc = cl_enqueue_locked(env, lock, io, enqflags);
2104 if (cl_lock_fits_into(env, lock, need, io)) {
2105 cl_lock_mutex_put(env, lock);
2106 cl_lock_lockdep_acquire(env,
2110 cl_unuse_locked(env, lock);
2112 cl_lock_trace(D_DLMTRACE, env, "enqueue failed", lock);
2113 cl_lock_hold_release(env, lock, scope, source);
2114 cl_lock_mutex_put(env, lock);
2115 lu_ref_del(&lock->cll_reference, scope, source);
2116 cl_lock_put(env, lock);
2124 EXPORT_SYMBOL(cl_lock_request);
2127 * Adds a hold to a known lock.
2129 void cl_lock_hold_add(const struct lu_env *env, struct cl_lock *lock,
2130 const char *scope, const void *source)
2132 LINVRNT(cl_lock_is_mutexed(lock));
2133 LINVRNT(cl_lock_invariant(env, lock));
2134 LASSERT(lock->cll_state != CLS_FREEING);
2137 cl_lock_hold_mod(env, lock, +1);
2139 lu_ref_add(&lock->cll_holders, scope, source);
2140 lu_ref_add(&lock->cll_reference, scope, source);
2143 EXPORT_SYMBOL(cl_lock_hold_add);
2146 * Releases a hold and a reference on a lock, on which caller acquired a
2149 void cl_lock_unhold(const struct lu_env *env, struct cl_lock *lock,
2150 const char *scope, const void *source)
2152 LINVRNT(cl_lock_invariant(env, lock));
2154 cl_lock_hold_release(env, lock, scope, source);
2155 lu_ref_del(&lock->cll_reference, scope, source);
2156 cl_lock_put(env, lock);
2159 EXPORT_SYMBOL(cl_lock_unhold);
2162 * Releases a hold and a reference on a lock, obtained by cl_lock_hold().
2164 void cl_lock_release(const struct lu_env *env, struct cl_lock *lock,
2165 const char *scope, const void *source)
2167 LINVRNT(cl_lock_invariant(env, lock));
2169 cl_lock_trace(D_DLMTRACE, env, "release lock", lock);
2170 cl_lock_mutex_get(env, lock);
2171 cl_lock_hold_release(env, lock, scope, source);
2172 cl_lock_mutex_put(env, lock);
2173 lu_ref_del(&lock->cll_reference, scope, source);
2174 cl_lock_put(env, lock);
2177 EXPORT_SYMBOL(cl_lock_release);
2179 void cl_lock_user_add(const struct lu_env *env, struct cl_lock *lock)
2181 LINVRNT(cl_lock_is_mutexed(lock));
2182 LINVRNT(cl_lock_invariant(env, lock));
2185 cl_lock_used_mod(env, lock, +1);
2188 EXPORT_SYMBOL(cl_lock_user_add);
2190 int cl_lock_user_del(const struct lu_env *env, struct cl_lock *lock)
2192 LINVRNT(cl_lock_is_mutexed(lock));
2193 LINVRNT(cl_lock_invariant(env, lock));
2194 LASSERT(lock->cll_users > 0);
2197 cl_lock_used_mod(env, lock, -1);
2198 RETURN(lock->cll_users == 0);
2200 EXPORT_SYMBOL(cl_lock_user_del);
2202 const char *cl_lock_mode_name(const enum cl_lock_mode mode)
2204 static const char *names[] = {
2205 [CLM_PHANTOM] = "P",
2210 if (0 <= mode && mode < ARRAY_SIZE(names))
2215 EXPORT_SYMBOL(cl_lock_mode_name);
2218 * Prints human readable representation of a lock description.
2220 void cl_lock_descr_print(const struct lu_env *env, void *cookie,
2221 lu_printer_t printer,
2222 const struct cl_lock_descr *descr)
2224 const struct lu_fid *fid;
2226 fid = lu_object_fid(&descr->cld_obj->co_lu);
2227 (*printer)(env, cookie, DDESCR"@"DFID, PDESCR(descr), PFID(fid));
2229 EXPORT_SYMBOL(cl_lock_descr_print);
2232 * Prints human readable representation of \a lock to the \a f.
2234 void cl_lock_print(const struct lu_env *env, void *cookie,
2235 lu_printer_t printer, const struct cl_lock *lock)
2237 const struct cl_lock_slice *slice;
2238 (*printer)(env, cookie, "lock@%p[%d %d %d %d %d %08lx] ",
2239 lock, cfs_atomic_read(&lock->cll_ref),
2240 lock->cll_state, lock->cll_error, lock->cll_holds,
2241 lock->cll_users, lock->cll_flags);
2242 cl_lock_descr_print(env, cookie, printer, &lock->cll_descr);
2243 (*printer)(env, cookie, " {\n");
2245 cfs_list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
2246 (*printer)(env, cookie, " %s@%p: ",
2247 slice->cls_obj->co_lu.lo_dev->ld_type->ldt_name,
2249 if (slice->cls_ops->clo_print != NULL)
2250 slice->cls_ops->clo_print(env, cookie, printer, slice);
2251 (*printer)(env, cookie, "\n");
2253 (*printer)(env, cookie, "} lock@%p\n", lock);
2255 EXPORT_SYMBOL(cl_lock_print);
2257 int cl_lock_init(void)
2259 return lu_kmem_init(cl_lock_caches);
2262 void cl_lock_fini(void)
2264 lu_kmem_fini(cl_lock_caches);