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 struct lock_class_key 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 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 = 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 #define RETIP ((unsigned long)__builtin_return_address(0))
132 #ifdef CONFIG_LOCKDEP
133 static struct lock_class_key cl_lock_key;
135 static void cl_lock_lockdep_init(struct cl_lock *lock)
137 lockdep_set_class_and_name(lock, &cl_lock_key, "EXT");
140 static void cl_lock_lockdep_acquire(const struct lu_env *env,
141 struct cl_lock *lock, __u32 enqflags)
143 cl_lock_counters(env, lock)->ctc_nr_locks_acquired++;
144 lock_acquire(&lock->dep_map, !!(enqflags & CEF_ASYNC),
145 /* try: */ 0, lock->cll_descr.cld_mode <= CLM_READ,
146 /* check: */ 2, RETIP);
149 static void cl_lock_lockdep_release(const struct lu_env *env,
150 struct cl_lock *lock)
152 cl_lock_counters(env, lock)->ctc_nr_locks_acquired--;
153 lock_release(&lock->dep_map, 0, RETIP);
156 #else /* !CONFIG_LOCKDEP */
158 static void cl_lock_lockdep_init(struct cl_lock *lock)
160 static void cl_lock_lockdep_acquire(const struct lu_env *env,
161 struct cl_lock *lock, __u32 enqflags)
163 static void cl_lock_lockdep_release(const struct lu_env *env,
164 struct cl_lock *lock)
167 #endif /* !CONFIG_LOCKDEP */
170 * Adds lock slice to the compound lock.
172 * This is called by cl_object_operations::coo_lock_init() methods to add a
173 * per-layer state to the lock. New state is added at the end of
174 * cl_lock::cll_layers list, that is, it is at the bottom of the stack.
176 * \see cl_req_slice_add(), cl_page_slice_add(), cl_io_slice_add()
178 void cl_lock_slice_add(struct cl_lock *lock, struct cl_lock_slice *slice,
179 struct cl_object *obj,
180 const struct cl_lock_operations *ops)
183 slice->cls_lock = lock;
184 list_add_tail(&slice->cls_linkage, &lock->cll_layers);
185 slice->cls_obj = obj;
186 slice->cls_ops = ops;
189 EXPORT_SYMBOL(cl_lock_slice_add);
192 * Returns true iff a lock with the mode \a has provides at least the same
193 * guarantees as a lock with the mode \a need.
195 int cl_lock_mode_match(enum cl_lock_mode has, enum cl_lock_mode need)
197 LINVRNT(need == CLM_READ || need == CLM_WRITE ||
198 need == CLM_PHANTOM || need == CLM_GROUP);
199 LINVRNT(has == CLM_READ || has == CLM_WRITE ||
200 has == CLM_PHANTOM || has == CLM_GROUP);
201 CLASSERT(CLM_PHANTOM < CLM_READ);
202 CLASSERT(CLM_READ < CLM_WRITE);
203 CLASSERT(CLM_WRITE < CLM_GROUP);
205 if (has != CLM_GROUP)
210 EXPORT_SYMBOL(cl_lock_mode_match);
213 * Returns true iff extent portions of lock descriptions match.
215 int cl_lock_ext_match(const struct cl_lock_descr *has,
216 const struct cl_lock_descr *need)
219 has->cld_start <= need->cld_start &&
220 has->cld_end >= need->cld_end &&
221 cl_lock_mode_match(has->cld_mode, need->cld_mode) &&
222 (has->cld_mode != CLM_GROUP || has->cld_gid == need->cld_gid);
224 EXPORT_SYMBOL(cl_lock_ext_match);
227 * Returns true iff a lock with the description \a has provides at least the
228 * same guarantees as a lock with the description \a need.
230 int cl_lock_descr_match(const struct cl_lock_descr *has,
231 const struct cl_lock_descr *need)
234 cl_object_same(has->cld_obj, need->cld_obj) &&
235 cl_lock_ext_match(has, need);
237 EXPORT_SYMBOL(cl_lock_descr_match);
239 static void cl_lock_free(const struct lu_env *env, struct cl_lock *lock)
241 struct cl_object *obj = lock->cll_descr.cld_obj;
243 LASSERT(cl_is_lock(lock));
244 LINVRNT(!cl_lock_is_mutexed(lock));
248 while (!list_empty(&lock->cll_layers)) {
249 struct cl_lock_slice *slice;
251 slice = list_entry(lock->cll_layers.next, struct cl_lock_slice,
253 list_del_init(lock->cll_layers.next);
254 slice->cls_ops->clo_fini(env, slice);
256 atomic_dec(&cl_object_site(obj)->cs_locks.cs_total);
257 atomic_dec(&cl_object_site(obj)->cs_locks_state[lock->cll_state]);
258 lu_object_ref_del_at(&obj->co_lu, lock->cll_obj_ref, "cl_lock", lock);
259 cl_object_put(env, obj);
260 lu_ref_fini(&lock->cll_reference);
261 lu_ref_fini(&lock->cll_holders);
262 mutex_destroy(&lock->cll_guard);
263 OBD_SLAB_FREE_PTR(lock, cl_lock_kmem);
268 * Releases a reference on a lock.
270 * When last reference is released, lock is returned to the cache, unless it
271 * is in cl_lock_state::CLS_FREEING state, in which case it is destroyed
274 * \see cl_object_put(), cl_page_put()
276 void cl_lock_put(const struct lu_env *env, struct cl_lock *lock)
278 struct cl_object *obj;
279 struct cl_object_header *head;
280 struct cl_site *site;
282 LINVRNT(cl_lock_invariant(env, lock));
284 obj = lock->cll_descr.cld_obj;
285 LINVRNT(obj != NULL);
286 head = cl_object_header(obj);
287 site = cl_object_site(obj);
289 CDEBUG(D_DLMTRACE, "releasing reference: %d %p %lu\n",
290 atomic_read(&lock->cll_ref), lock, RETIP);
292 if (atomic_dec_and_test(&lock->cll_ref)) {
293 if (lock->cll_state == CLS_FREEING) {
294 LASSERT(list_empty(&lock->cll_linkage));
295 cl_lock_free(env, lock);
297 atomic_dec(&site->cs_locks.cs_busy);
301 EXPORT_SYMBOL(cl_lock_put);
304 * Acquires an additional reference to a lock.
306 * This can be called only by caller already possessing a reference to \a
309 * \see cl_object_get(), cl_page_get()
311 void cl_lock_get(struct cl_lock *lock)
313 LINVRNT(cl_lock_invariant(NULL, lock));
314 CDEBUG(D_DLMTRACE|D_TRACE, "acquiring reference: %d %p %lu\n",
315 atomic_read(&lock->cll_ref), lock, RETIP);
316 atomic_inc(&lock->cll_ref);
318 EXPORT_SYMBOL(cl_lock_get);
321 * Acquires a reference to a lock.
323 * This is much like cl_lock_get(), except that this function can be used to
324 * acquire initial reference to the cached lock. Caller has to deal with all
325 * possible races. Use with care!
327 * \see cl_page_get_trust()
329 void cl_lock_get_trust(struct cl_lock *lock)
331 struct cl_site *site = cl_object_site(lock->cll_descr.cld_obj);
333 LASSERT(cl_is_lock(lock));
334 CDEBUG(D_DLMTRACE|D_TRACE, "acquiring trusted reference: %d %p %lu\n",
335 atomic_read(&lock->cll_ref), lock, RETIP);
336 if (atomic_inc_return(&lock->cll_ref) == 1)
337 atomic_inc(&site->cs_locks.cs_busy);
339 EXPORT_SYMBOL(cl_lock_get_trust);
342 * Helper function destroying the lock that wasn't completely initialized.
344 * Other threads can acquire references to the top-lock through its
345 * sub-locks. Hence, it cannot be cl_lock_free()-ed immediately.
347 static void cl_lock_finish(const struct lu_env *env, struct cl_lock *lock)
349 cl_lock_mutex_get(env, lock);
350 cl_lock_delete(env, lock);
351 cl_lock_mutex_put(env, lock);
352 cl_lock_put(env, lock);
355 static struct cl_lock *cl_lock_alloc(const struct lu_env *env,
356 struct cl_object *obj,
357 const struct cl_io *io,
358 const struct cl_lock_descr *descr)
360 struct cl_lock *lock;
361 struct lu_object_header *head;
362 struct cl_site *site = cl_object_site(obj);
365 OBD_SLAB_ALLOC_PTR_GFP(lock, cl_lock_kmem, CFS_ALLOC_IO);
367 atomic_set(&lock->cll_ref, 1);
368 lock->cll_descr = *descr;
369 lock->cll_state = CLS_NEW;
371 lock->cll_obj_ref = lu_object_ref_add(&obj->co_lu,
373 CFS_INIT_LIST_HEAD(&lock->cll_layers);
374 CFS_INIT_LIST_HEAD(&lock->cll_linkage);
375 CFS_INIT_LIST_HEAD(&lock->cll_inclosure);
376 lu_ref_init(&lock->cll_reference);
377 lu_ref_init(&lock->cll_holders);
378 mutex_init(&lock->cll_guard);
379 lockdep_set_class(&lock->cll_guard, &cl_lock_guard_class);
380 cfs_waitq_init(&lock->cll_wq);
381 head = obj->co_lu.lo_header;
382 atomic_inc(&site->cs_locks_state[CLS_NEW]);
383 atomic_inc(&site->cs_locks.cs_total);
384 atomic_inc(&site->cs_locks.cs_created);
385 cl_lock_lockdep_init(lock);
386 list_for_each_entry(obj, &head->loh_layers, co_lu.lo_linkage) {
389 err = obj->co_ops->coo_lock_init(env, obj, lock, io);
391 cl_lock_finish(env, lock);
397 lock = ERR_PTR(-ENOMEM);
402 * Returns true iff lock is "suitable" for given io. E.g., locks acquired by
403 * truncate and O_APPEND cannot be reused for read/non-append-write, as they
404 * cover multiple stripes and can trigger cascading timeouts.
406 static int cl_lock_fits_into(const struct lu_env *env,
407 const struct cl_lock *lock,
408 const struct cl_lock_descr *need,
409 const struct cl_io *io)
411 const struct cl_lock_slice *slice;
413 LINVRNT(cl_lock_invariant_trusted(env, lock));
415 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
416 if (slice->cls_ops->clo_fits_into != NULL &&
417 !slice->cls_ops->clo_fits_into(env, slice, need, io))
423 static struct cl_lock *cl_lock_lookup(const struct lu_env *env,
424 struct cl_object *obj,
425 const struct cl_io *io,
426 const struct cl_lock_descr *need)
428 struct cl_lock *lock;
429 struct cl_object_header *head;
430 struct cl_site *site;
434 head = cl_object_header(obj);
435 site = cl_object_site(obj);
436 LINVRNT_SPIN_LOCKED(&head->coh_lock_guard);
437 atomic_inc(&site->cs_locks.cs_lookup);
438 list_for_each_entry(lock, &head->coh_locks, cll_linkage) {
441 LASSERT(cl_is_lock(lock));
442 matched = cl_lock_ext_match(&lock->cll_descr, need) &&
443 lock->cll_state < CLS_FREEING &&
444 !(lock->cll_flags & CLF_CANCELLED) &&
445 cl_lock_fits_into(env, lock, need, io);
446 CDEBUG(D_DLMTRACE, "has: "DDESCR"(%i) need: "DDESCR": %d\n",
447 PDESCR(&lock->cll_descr), lock->cll_state, PDESCR(need),
450 cl_lock_get_trust(lock);
451 /* move the lock to the LRU head */
452 list_move(&lock->cll_linkage, &head->coh_locks);
453 atomic_inc(&cl_object_site(obj)->cs_locks.cs_hit);
461 * Returns a lock matching description \a need.
463 * This is the main entry point into the cl_lock caching interface. First, a
464 * cache (implemented as a per-object linked list) is consulted. If lock is
465 * found there, it is returned immediately. Otherwise new lock is allocated
466 * and returned. In any case, additional reference to lock is acquired.
468 * \see cl_object_find(), cl_page_find()
470 static struct cl_lock *cl_lock_find(const struct lu_env *env,
471 const struct cl_io *io,
472 const struct cl_lock_descr *need)
474 struct cl_object_header *head;
475 struct cl_object *obj;
476 struct cl_lock *lock;
477 struct cl_site *site;
482 head = cl_object_header(obj);
483 site = cl_object_site(obj);
485 spin_lock(&head->coh_lock_guard);
486 lock = cl_lock_lookup(env, obj, io, need);
487 spin_unlock(&head->coh_lock_guard);
490 lock = cl_lock_alloc(env, obj, io, need);
492 struct cl_lock *ghost;
494 spin_lock(&head->coh_lock_guard);
495 ghost = cl_lock_lookup(env, obj, io, need);
497 list_add(&lock->cll_linkage, &head->coh_locks);
498 spin_unlock(&head->coh_lock_guard);
499 atomic_inc(&site->cs_locks.cs_busy);
501 spin_unlock(&head->coh_lock_guard);
503 * Other threads can acquire references to the
504 * top-lock through its sub-locks. Hence, it
505 * cannot be cl_lock_free()-ed immediately.
507 cl_lock_finish(env, lock);
516 * Returns existing lock matching given description. This is similar to
517 * cl_lock_find() except that no new lock is created, and returned lock is
518 * guaranteed to be in enum cl_lock_state::CLS_HELD state.
520 struct cl_lock *cl_lock_peek(const struct lu_env *env, const struct cl_io *io,
521 const struct cl_lock_descr *need,
522 const char *scope, const void *source)
524 struct cl_object_header *head;
525 struct cl_object *obj;
526 struct cl_lock *lock;
529 head = cl_object_header(obj);
531 spin_lock(&head->coh_lock_guard);
532 lock = cl_lock_lookup(env, obj, io, need);
533 spin_unlock(&head->coh_lock_guard);
538 cl_lock_mutex_get(env, lock);
539 if (lock->cll_state == CLS_CACHED)
540 cl_use_try(env, lock);
541 ok = lock->cll_state == CLS_HELD;
543 cl_lock_hold_add(env, lock, scope, source);
544 cl_lock_user_add(env, lock);
545 cl_lock_put(env, lock);
547 cl_lock_mutex_put(env, lock);
549 cl_lock_put(env, lock);
555 EXPORT_SYMBOL(cl_lock_peek);
558 * Returns a slice within a lock, corresponding to the given layer in the
563 const struct cl_lock_slice *cl_lock_at(const struct cl_lock *lock,
564 const struct lu_device_type *dtype)
566 const struct cl_lock_slice *slice;
568 LINVRNT(cl_lock_invariant_trusted(NULL, lock));
571 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
572 if (slice->cls_obj->co_lu.lo_dev->ld_type == dtype)
577 EXPORT_SYMBOL(cl_lock_at);
579 static void cl_lock_trace(struct cl_thread_counters *counters,
580 const char *prefix, const struct cl_lock *lock)
582 CDEBUG(D_DLMTRACE|D_TRACE, "%s: %i@%p %p %i %i\n", prefix,
583 atomic_read(&lock->cll_ref), lock, lock->cll_guarder,
584 lock->cll_depth, counters->ctc_nr_locks_locked);
587 static void cl_lock_mutex_tail(const struct lu_env *env, struct cl_lock *lock)
589 struct cl_thread_counters *counters;
591 counters = cl_lock_counters(env, lock);
593 counters->ctc_nr_locks_locked++;
594 lu_ref_add(&counters->ctc_locks_locked, "cll_guard", lock);
595 cl_lock_trace(counters, "got mutex", lock);
599 * Locks cl_lock object.
601 * This is used to manipulate cl_lock fields, and to serialize state
602 * transitions in the lock state machine.
604 * \post cl_lock_is_mutexed(lock)
606 * \see cl_lock_mutex_put()
608 void cl_lock_mutex_get(const struct lu_env *env, struct cl_lock *lock)
610 LINVRNT(cl_lock_invariant(env, lock));
612 if (lock->cll_guarder == cfs_current()) {
613 LINVRNT(cl_lock_is_mutexed(lock));
614 LINVRNT(lock->cll_depth > 0);
616 struct cl_object_header *hdr;
617 struct cl_thread_info *info;
620 LINVRNT(lock->cll_guarder != cfs_current());
621 hdr = cl_object_header(lock->cll_descr.cld_obj);
623 * Check that mutices are taken in the bottom-to-top order.
625 info = cl_env_info(env);
626 for (i = 0; i < hdr->coh_nesting; ++i)
627 LASSERT(info->clt_counters[i].ctc_nr_locks_locked == 0);
628 mutex_lock_nested(&lock->cll_guard, hdr->coh_nesting);
629 lock->cll_guarder = cfs_current();
630 LINVRNT(lock->cll_depth == 0);
632 cl_lock_mutex_tail(env, lock);
634 EXPORT_SYMBOL(cl_lock_mutex_get);
637 * Try-locks cl_lock object.
639 * \retval 0 \a lock was successfully locked
641 * \retval -EBUSY \a lock cannot be locked right now
643 * \post ergo(result == 0, cl_lock_is_mutexed(lock))
645 * \see cl_lock_mutex_get()
647 int cl_lock_mutex_try(const struct lu_env *env, struct cl_lock *lock)
651 LINVRNT(cl_lock_invariant_trusted(env, lock));
655 if (lock->cll_guarder == cfs_current()) {
656 LINVRNT(lock->cll_depth > 0);
657 cl_lock_mutex_tail(env, lock);
658 } else if (mutex_trylock(&lock->cll_guard)) {
659 LINVRNT(lock->cll_depth == 0);
660 lock->cll_guarder = cfs_current();
661 cl_lock_mutex_tail(env, lock);
666 EXPORT_SYMBOL(cl_lock_mutex_try);
669 * Unlocks cl_lock object.
671 * \pre cl_lock_is_mutexed(lock)
673 * \see cl_lock_mutex_get()
675 void cl_lock_mutex_put(const struct lu_env *env, struct cl_lock *lock)
677 struct cl_thread_counters *counters;
679 LINVRNT(cl_lock_invariant(env, lock));
680 LINVRNT(cl_lock_is_mutexed(lock));
681 LINVRNT(lock->cll_guarder == cfs_current());
682 LINVRNT(lock->cll_depth > 0);
684 counters = cl_lock_counters(env, lock);
685 LINVRNT(counters->ctc_nr_locks_locked > 0);
687 cl_lock_trace(counters, "put mutex", lock);
688 lu_ref_del(&counters->ctc_locks_locked, "cll_guard", lock);
689 counters->ctc_nr_locks_locked--;
690 if (--lock->cll_depth == 0) {
691 lock->cll_guarder = NULL;
692 mutex_unlock(&lock->cll_guard);
695 EXPORT_SYMBOL(cl_lock_mutex_put);
698 * Returns true iff lock's mutex is owned by the current thread.
700 int cl_lock_is_mutexed(struct cl_lock *lock)
702 return lock->cll_guarder == cfs_current();
704 EXPORT_SYMBOL(cl_lock_is_mutexed);
707 * Returns number of cl_lock mutices held by the current thread (environment).
709 int cl_lock_nr_mutexed(const struct lu_env *env)
711 struct cl_thread_info *info;
716 * NOTE: if summation across all nesting levels (currently 2) proves
717 * too expensive, a summary counter can be added to
718 * struct cl_thread_info.
720 info = cl_env_info(env);
721 for (i = 0, locked = 0; i < ARRAY_SIZE(info->clt_counters); ++i)
722 locked += info->clt_counters[i].ctc_nr_locks_locked;
725 EXPORT_SYMBOL(cl_lock_nr_mutexed);
727 static void cl_lock_cancel0(const struct lu_env *env, struct cl_lock *lock)
729 LINVRNT(cl_lock_is_mutexed(lock));
730 LINVRNT(cl_lock_invariant(env, lock));
732 if (!(lock->cll_flags & CLF_CANCELLED)) {
733 const struct cl_lock_slice *slice;
735 lock->cll_flags |= CLF_CANCELLED;
736 list_for_each_entry_reverse(slice, &lock->cll_layers,
738 if (slice->cls_ops->clo_cancel != NULL)
739 slice->cls_ops->clo_cancel(env, slice);
745 static void cl_lock_delete0(const struct lu_env *env, struct cl_lock *lock)
747 struct cl_object_header *head;
748 const struct cl_lock_slice *slice;
750 LINVRNT(cl_lock_is_mutexed(lock));
751 LINVRNT(cl_lock_invariant(env, lock));
754 if (lock->cll_state < CLS_FREEING) {
755 cl_lock_state_set(env, lock, CLS_FREEING);
757 head = cl_object_header(lock->cll_descr.cld_obj);
759 spin_lock(&head->coh_lock_guard);
760 list_del_init(&lock->cll_linkage);
762 * No locks, no pages. This is only valid for bottom sub-locks
763 * and head->coh_nesting == 1 check assumes two level top-sub
766 LASSERT(ergo(head->coh_nesting == 1 &&
767 list_empty(&head->coh_locks), !head->coh_pages));
768 spin_unlock(&head->coh_lock_guard);
770 * From now on, no new references to this lock can be acquired
771 * by cl_lock_lookup().
773 list_for_each_entry_reverse(slice, &lock->cll_layers,
775 if (slice->cls_ops->clo_delete != NULL)
776 slice->cls_ops->clo_delete(env, slice);
779 * From now on, no new references to this lock can be acquired
780 * by layer-specific means (like a pointer from struct
781 * ldlm_lock in osc, or a pointer from top-lock to sub-lock in
784 * Lock will be finally freed in cl_lock_put() when last of
785 * existing references goes away.
792 * Mod(ifie)s cl_lock::cll_holds counter for a given lock. Also, for a
793 * top-lock (nesting == 0) accounts for this modification in the per-thread
794 * debugging counters. Sub-lock holds can be released by a thread different
795 * from one that acquired it.
797 static void cl_lock_hold_mod(const struct lu_env *env, struct cl_lock *lock,
800 struct cl_thread_counters *counters;
801 enum clt_nesting_level nesting;
803 lock->cll_holds += delta;
804 nesting = cl_lock_nesting(lock);
805 if (nesting == CNL_TOP) {
806 counters = &cl_env_info(env)->clt_counters[CNL_TOP];
807 counters->ctc_nr_held += delta;
808 LASSERT(counters->ctc_nr_held >= 0);
813 * Mod(ifie)s cl_lock::cll_users counter for a given lock. See
814 * cl_lock_hold_mod() for the explanation of the debugging code.
816 static void cl_lock_used_mod(const struct lu_env *env, struct cl_lock *lock,
819 struct cl_thread_counters *counters;
820 enum clt_nesting_level nesting;
822 lock->cll_users += delta;
823 nesting = cl_lock_nesting(lock);
824 if (nesting == CNL_TOP) {
825 counters = &cl_env_info(env)->clt_counters[CNL_TOP];
826 counters->ctc_nr_used += delta;
827 LASSERT(counters->ctc_nr_used >= 0);
831 static void cl_lock_hold_release(const struct lu_env *env, struct cl_lock *lock,
832 const char *scope, const void *source)
834 LINVRNT(cl_lock_is_mutexed(lock));
835 LINVRNT(cl_lock_invariant(env, lock));
836 LASSERT(lock->cll_holds > 0);
839 lu_ref_del(&lock->cll_holders, scope, source);
840 cl_lock_hold_mod(env, lock, -1);
841 if (lock->cll_holds == 0) {
842 if (lock->cll_descr.cld_mode == CLM_PHANTOM ||
843 lock->cll_descr.cld_mode == CLM_GROUP)
845 * If lock is still phantom or grouplock when user is
846 * done with it---destroy the lock.
848 lock->cll_flags |= CLF_CANCELPEND|CLF_DOOMED;
849 if (lock->cll_flags & CLF_CANCELPEND) {
850 lock->cll_flags &= ~CLF_CANCELPEND;
851 cl_lock_cancel0(env, lock);
853 if (lock->cll_flags & CLF_DOOMED) {
854 /* no longer doomed: it's dead... Jim. */
855 lock->cll_flags &= ~CLF_DOOMED;
856 cl_lock_delete0(env, lock);
864 * Waits until lock state is changed.
866 * This function is called with cl_lock mutex locked, atomically releases
867 * mutex and goes to sleep, waiting for a lock state change (signaled by
868 * cl_lock_signal()), and re-acquires the mutex before return.
870 * This function is used to wait until lock state machine makes some progress
871 * and to emulate synchronous operations on top of asynchronous lock
874 * \retval -EINTR wait was interrupted
876 * \retval 0 wait wasn't interrupted
878 * \pre cl_lock_is_mutexed(lock)
880 * \see cl_lock_signal()
882 int cl_lock_state_wait(const struct lu_env *env, struct cl_lock *lock)
884 cfs_waitlink_t waiter;
888 LINVRNT(cl_lock_is_mutexed(lock));
889 LINVRNT(cl_lock_invariant(env, lock));
890 LASSERT(lock->cll_depth == 1);
891 LASSERT(lock->cll_state != CLS_FREEING); /* too late to wait */
893 result = lock->cll_error;
894 if (result == 0 && !(lock->cll_flags & CLF_STATE)) {
895 cfs_waitlink_init(&waiter);
896 cfs_waitq_add(&lock->cll_wq, &waiter);
897 set_current_state(CFS_TASK_INTERRUPTIBLE);
898 cl_lock_mutex_put(env, lock);
900 LASSERT(cl_lock_nr_mutexed(env) == 0);
901 cfs_waitq_wait(&waiter, CFS_TASK_INTERRUPTIBLE);
903 cl_lock_mutex_get(env, lock);
904 set_current_state(CFS_TASK_RUNNING);
905 cfs_waitq_del(&lock->cll_wq, &waiter);
906 result = cfs_signal_pending() ? -EINTR : 0;
908 lock->cll_flags &= ~CLF_STATE;
911 EXPORT_SYMBOL(cl_lock_state_wait);
913 static void cl_lock_state_signal(const struct lu_env *env, struct cl_lock *lock,
914 enum cl_lock_state state)
916 const struct cl_lock_slice *slice;
919 LINVRNT(cl_lock_is_mutexed(lock));
920 LINVRNT(cl_lock_invariant(env, lock));
922 list_for_each_entry(slice, &lock->cll_layers, cls_linkage)
923 if (slice->cls_ops->clo_state != NULL)
924 slice->cls_ops->clo_state(env, slice, state);
925 lock->cll_flags |= CLF_STATE;
926 cfs_waitq_broadcast(&lock->cll_wq);
931 * Notifies waiters that lock state changed.
933 * Wakes up all waiters sleeping in cl_lock_state_wait(), also notifies all
934 * layers about state change by calling cl_lock_operations::clo_state()
937 void cl_lock_signal(const struct lu_env *env, struct cl_lock *lock)
940 cl_lock_state_signal(env, lock, lock->cll_state);
943 EXPORT_SYMBOL(cl_lock_signal);
946 * Changes lock state.
948 * This function is invoked to notify layers that lock state changed, possible
949 * as a result of an asynchronous event such as call-back reception.
951 * \post lock->cll_state == state
953 * \see cl_lock_operations::clo_state()
955 void cl_lock_state_set(const struct lu_env *env, struct cl_lock *lock,
956 enum cl_lock_state state)
958 struct cl_site *site = cl_object_site(lock->cll_descr.cld_obj);
961 LASSERT(lock->cll_state <= state ||
962 (lock->cll_state == CLS_CACHED &&
963 (state == CLS_HELD || /* lock found in cache */
964 state == CLS_NEW /* sub-lock canceled */)) ||
965 /* sub-lock canceled during unlocking */
966 (lock->cll_state == CLS_UNLOCKING && state == CLS_NEW));
968 if (lock->cll_state != state) {
969 atomic_dec(&site->cs_locks_state[lock->cll_state]);
970 atomic_inc(&site->cs_locks_state[state]);
972 cl_lock_state_signal(env, lock, state);
973 lock->cll_state = state;
977 EXPORT_SYMBOL(cl_lock_state_set);
980 * Yanks lock from the cache (cl_lock_state::CLS_CACHED state) by calling
981 * cl_lock_operations::clo_use() top-to-bottom to notify layers.
983 int cl_use_try(const struct lu_env *env, struct cl_lock *lock)
986 const struct cl_lock_slice *slice;
990 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
991 if (slice->cls_ops->clo_use != NULL) {
992 result = slice->cls_ops->clo_use(env, slice);
997 LASSERT(result != -ENOSYS);
999 cl_lock_state_set(env, lock, CLS_HELD);
1002 EXPORT_SYMBOL(cl_use_try);
1005 * Helper for cl_enqueue_try() that calls ->clo_enqueue() across all layers
1008 static int cl_enqueue_kick(const struct lu_env *env,
1009 struct cl_lock *lock,
1010 struct cl_io *io, __u32 flags)
1013 const struct cl_lock_slice *slice;
1017 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
1018 if (slice->cls_ops->clo_enqueue != NULL) {
1019 result = slice->cls_ops->clo_enqueue(env,
1025 LASSERT(result != -ENOSYS);
1030 * Tries to enqueue a lock.
1032 * This function is called repeatedly by cl_enqueue() until either lock is
1033 * enqueued, or error occurs. This function does not block waiting for
1034 * networking communication to complete.
1036 * \post ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1037 * lock->cll_state == CLS_HELD)
1039 * \see cl_enqueue() cl_lock_operations::clo_enqueue()
1040 * \see cl_lock_state::CLS_ENQUEUED
1042 int cl_enqueue_try(const struct lu_env *env, struct cl_lock *lock,
1043 struct cl_io *io, __u32 flags)
1051 LINVRNT(cl_lock_is_mutexed(lock));
1053 if (lock->cll_error != 0)
1055 switch (lock->cll_state) {
1057 cl_lock_state_set(env, lock, CLS_QUEUING);
1061 result = cl_enqueue_kick(env, lock, io, flags);
1063 cl_lock_state_set(env, lock, CLS_ENQUEUED);
1066 /* wait until unlocking finishes, and enqueue lock
1071 /* yank lock from the cache. */
1072 result = cl_use_try(env, lock);
1081 * impossible, only held locks with increased
1082 * ->cll_holds can be enqueued, and they cannot be
1087 } while (result == CLO_REPEAT);
1089 cl_lock_error(env, lock, result);
1090 RETURN(result ?: lock->cll_error);
1092 EXPORT_SYMBOL(cl_enqueue_try);
1094 static int cl_enqueue_locked(const struct lu_env *env, struct cl_lock *lock,
1095 struct cl_io *io, __u32 enqflags)
1101 LINVRNT(cl_lock_is_mutexed(lock));
1102 LINVRNT(cl_lock_invariant(env, lock));
1103 LASSERT(lock->cll_holds > 0);
1105 cl_lock_user_add(env, lock);
1107 result = cl_enqueue_try(env, lock, io, enqflags);
1108 if (result == CLO_WAIT) {
1109 result = cl_lock_state_wait(env, lock);
1116 cl_lock_user_del(env, lock);
1117 if (result != -EINTR)
1118 cl_lock_error(env, lock, result);
1120 LASSERT(ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1121 lock->cll_state == CLS_HELD));
1128 * \pre current thread or io owns a hold on lock.
1130 * \post ergo(result == 0, lock->users increased)
1131 * \post ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1132 * lock->cll_state == CLS_HELD)
1134 int cl_enqueue(const struct lu_env *env, struct cl_lock *lock,
1135 struct cl_io *io, __u32 enqflags)
1141 cl_lock_lockdep_acquire(env, lock, enqflags);
1142 cl_lock_mutex_get(env, lock);
1143 result = cl_enqueue_locked(env, lock, io, enqflags);
1144 cl_lock_mutex_put(env, lock);
1146 cl_lock_lockdep_release(env, lock);
1147 LASSERT(ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1148 lock->cll_state == CLS_HELD));
1151 EXPORT_SYMBOL(cl_enqueue);
1154 * Tries to unlock a lock.
1156 * This function is called repeatedly by cl_unuse() until either lock is
1157 * unlocked, or error occurs.
1159 * \ppre lock->cll_state <= CLS_HELD || lock->cll_state == CLS_UNLOCKING
1161 * \post ergo(result == 0, lock->cll_state == CLS_CACHED)
1163 * \see cl_unuse() cl_lock_operations::clo_unuse()
1164 * \see cl_lock_state::CLS_CACHED
1166 int cl_unuse_try(const struct lu_env *env, struct cl_lock *lock)
1168 const struct cl_lock_slice *slice;
1172 if (lock->cll_state != CLS_UNLOCKING) {
1173 if (lock->cll_users > 1) {
1174 cl_lock_user_del(env, lock);
1178 * New lock users (->cll_users) are not protecting unlocking
1179 * from proceeding. From this point, lock eventually reaches
1180 * CLS_CACHED, is reinitialized to CLS_NEW or fails into
1183 cl_lock_state_set(env, lock, CLS_UNLOCKING);
1188 if (lock->cll_error != 0)
1191 LINVRNT(cl_lock_is_mutexed(lock));
1192 LINVRNT(cl_lock_invariant(env, lock));
1193 LASSERT(lock->cll_state == CLS_UNLOCKING);
1194 LASSERT(lock->cll_users > 0);
1195 LASSERT(lock->cll_holds > 0);
1198 list_for_each_entry_reverse(slice, &lock->cll_layers,
1200 if (slice->cls_ops->clo_unuse != NULL) {
1201 result = slice->cls_ops->clo_unuse(env, slice);
1206 LASSERT(result != -ENOSYS);
1207 } while (result == CLO_REPEAT);
1208 if (result != CLO_WAIT)
1210 * Once there is no more need to iterate ->clo_unuse() calls,
1211 * remove lock user. This is done even if unrecoverable error
1212 * happened during unlocking, because nothing else can be
1215 cl_lock_user_del(env, lock);
1216 if (result == 0 || result == -ESTALE) {
1217 enum cl_lock_state state;
1220 * Return lock back to the cache. This is the only
1221 * place where lock is moved into CLS_CACHED state.
1223 * If one of ->clo_unuse() methods returned -ESTALE, lock
1224 * cannot be placed into cache and has to be
1225 * re-initialized. This happens e.g., when a sub-lock was
1226 * canceled while unlocking was in progress.
1228 state = result == 0 ? CLS_CACHED : CLS_NEW;
1229 cl_lock_state_set(env, lock, state);
1232 * Hide -ESTALE error.
1233 * If the lock is a glimpse lock, and it has multiple
1234 * stripes. Assuming that one of its sublock returned -ENAVAIL,
1235 * and other sublocks are matched write locks. In this case,
1236 * we can't set this lock to error because otherwise some of
1237 * its sublocks may not be canceled. This causes some dirty
1238 * pages won't be written to OSTs. -jay
1242 result = result ?: lock->cll_error;
1244 cl_lock_error(env, lock, result);
1247 EXPORT_SYMBOL(cl_unuse_try);
1249 static void cl_unuse_locked(const struct lu_env *env, struct cl_lock *lock)
1252 LASSERT(lock->cll_state <= CLS_HELD);
1256 result = cl_unuse_try(env, lock);
1257 if (result == CLO_WAIT) {
1258 result = cl_lock_state_wait(env, lock);
1270 void cl_unuse(const struct lu_env *env, struct cl_lock *lock)
1273 cl_lock_mutex_get(env, lock);
1274 cl_unuse_locked(env, lock);
1275 cl_lock_mutex_put(env, lock);
1276 cl_lock_lockdep_release(env, lock);
1279 EXPORT_SYMBOL(cl_unuse);
1282 * Tries to wait for a lock.
1284 * This function is called repeatedly by cl_wait() until either lock is
1285 * granted, or error occurs. This function does not block waiting for network
1286 * communication to complete.
1288 * \see cl_wait() cl_lock_operations::clo_wait()
1289 * \see cl_lock_state::CLS_HELD
1291 int cl_wait_try(const struct lu_env *env, struct cl_lock *lock)
1293 const struct cl_lock_slice *slice;
1298 LINVRNT(cl_lock_is_mutexed(lock));
1299 LINVRNT(cl_lock_invariant(env, lock));
1300 LASSERT(lock->cll_state == CLS_ENQUEUED ||
1301 lock->cll_state == CLS_HELD);
1302 LASSERT(lock->cll_users > 0);
1303 LASSERT(lock->cll_holds > 0);
1306 if (lock->cll_error != 0)
1308 if (lock->cll_state == CLS_HELD)
1313 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
1314 if (slice->cls_ops->clo_wait != NULL) {
1315 result = slice->cls_ops->clo_wait(env, slice);
1320 LASSERT(result != -ENOSYS);
1322 cl_lock_state_set(env, lock, CLS_HELD);
1323 } while (result == CLO_REPEAT);
1324 RETURN(result ?: lock->cll_error);
1326 EXPORT_SYMBOL(cl_wait_try);
1329 * Waits until enqueued lock is granted.
1331 * \pre current thread or io owns a hold on the lock
1332 * \pre ergo(result == 0, lock->cll_state == CLS_ENQUEUED ||
1333 * lock->cll_state == CLS_HELD)
1335 * \post ergo(result == 0, lock->cll_state == CLS_HELD)
1337 int cl_wait(const struct lu_env *env, struct cl_lock *lock)
1342 cl_lock_mutex_get(env, lock);
1344 LINVRNT(cl_lock_invariant(env, lock));
1345 LASSERT(lock->cll_state == CLS_ENQUEUED || lock->cll_state == CLS_HELD);
1346 LASSERT(lock->cll_holds > 0);
1349 result = cl_wait_try(env, lock);
1350 if (result == CLO_WAIT) {
1351 result = cl_lock_state_wait(env, lock);
1358 cl_lock_user_del(env, lock);
1359 if (result != -EINTR)
1360 cl_lock_error(env, lock, result);
1361 cl_lock_lockdep_release(env, lock);
1363 cl_lock_mutex_put(env, lock);
1364 LASSERT(ergo(result == 0, lock->cll_state == CLS_HELD));
1367 EXPORT_SYMBOL(cl_wait);
1370 * Executes cl_lock_operations::clo_weigh(), and sums results to estimate lock
1373 unsigned long cl_lock_weigh(const struct lu_env *env, struct cl_lock *lock)
1375 const struct cl_lock_slice *slice;
1376 unsigned long pound;
1377 unsigned long ounce;
1380 LINVRNT(cl_lock_is_mutexed(lock));
1381 LINVRNT(cl_lock_invariant(env, lock));
1384 list_for_each_entry_reverse(slice, &lock->cll_layers, cls_linkage) {
1385 if (slice->cls_ops->clo_weigh != NULL) {
1386 ounce = slice->cls_ops->clo_weigh(env, slice);
1388 if (pound < ounce) /* over-weight^Wflow */
1394 EXPORT_SYMBOL(cl_lock_weigh);
1397 * Notifies layers that lock description changed.
1399 * The server can grant client a lock different from one that was requested
1400 * (e.g., larger in extent). This method is called when actually granted lock
1401 * description becomes known to let layers to accommodate for changed lock
1404 * \see cl_lock_operations::clo_modify()
1406 int cl_lock_modify(const struct lu_env *env, struct cl_lock *lock,
1407 const struct cl_lock_descr *desc)
1409 const struct cl_lock_slice *slice;
1410 struct cl_object *obj = lock->cll_descr.cld_obj;
1411 struct cl_object_header *hdr = cl_object_header(obj);
1415 /* don't allow object to change */
1416 LASSERT(obj == desc->cld_obj);
1417 LINVRNT(cl_lock_is_mutexed(lock));
1418 LINVRNT(cl_lock_invariant(env, lock));
1420 list_for_each_entry_reverse(slice, &lock->cll_layers, cls_linkage) {
1421 if (slice->cls_ops->clo_modify != NULL) {
1422 result = slice->cls_ops->clo_modify(env, slice, desc);
1427 CL_LOCK_DEBUG(D_DLMTRACE, env, lock, " -> "DDESCR"@"DFID"\n",
1428 PDESCR(desc), PFID(lu_object_fid(&desc->cld_obj->co_lu)));
1430 * Just replace description in place. Nothing more is needed for
1431 * now. If locks were indexed according to their extent and/or mode,
1432 * that index would have to be updated here.
1434 spin_lock(&hdr->coh_lock_guard);
1435 lock->cll_descr = *desc;
1436 spin_unlock(&hdr->coh_lock_guard);
1439 EXPORT_SYMBOL(cl_lock_modify);
1442 * Initializes lock closure with a given origin.
1444 * \see cl_lock_closure
1446 void cl_lock_closure_init(const struct lu_env *env,
1447 struct cl_lock_closure *closure,
1448 struct cl_lock *origin, int wait)
1450 LINVRNT(cl_lock_is_mutexed(origin));
1451 LINVRNT(cl_lock_invariant(env, origin));
1453 CFS_INIT_LIST_HEAD(&closure->clc_list);
1454 closure->clc_origin = origin;
1455 closure->clc_wait = wait;
1456 closure->clc_nr = 0;
1458 EXPORT_SYMBOL(cl_lock_closure_init);
1461 * Builds a closure of \a lock.
1463 * Building of a closure consists of adding initial lock (\a lock) into it,
1464 * and calling cl_lock_operations::clo_closure() methods of \a lock. These
1465 * methods might call cl_lock_closure_build() recursively again, adding more
1466 * locks to the closure, etc.
1468 * \see cl_lock_closure
1470 int cl_lock_closure_build(const struct lu_env *env, struct cl_lock *lock,
1471 struct cl_lock_closure *closure)
1473 const struct cl_lock_slice *slice;
1477 LINVRNT(cl_lock_is_mutexed(closure->clc_origin));
1478 LINVRNT(cl_lock_invariant(env, closure->clc_origin));
1480 result = cl_lock_enclosure(env, lock, closure);
1482 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
1483 if (slice->cls_ops->clo_closure != NULL) {
1484 result = slice->cls_ops->clo_closure(env, slice,
1492 cl_lock_disclosure(env, closure);
1495 EXPORT_SYMBOL(cl_lock_closure_build);
1498 * Adds new lock to a closure.
1500 * Try-locks \a lock and if succeeded, adds it to the closure (never more than
1501 * once). If try-lock failed, returns CLO_REPEAT, after optionally waiting
1502 * until next try-lock is likely to succeed.
1504 int cl_lock_enclosure(const struct lu_env *env, struct cl_lock *lock,
1505 struct cl_lock_closure *closure)
1509 if (!cl_lock_mutex_try(env, lock)) {
1511 * If lock->cll_inclosure is not empty, lock is already in
1514 if (list_empty(&lock->cll_inclosure)) {
1515 cl_lock_get_trust(lock);
1516 lu_ref_add(&lock->cll_reference, "closure", closure);
1517 list_add(&lock->cll_inclosure, &closure->clc_list);
1520 cl_lock_mutex_put(env, lock);
1523 cl_lock_disclosure(env, closure);
1524 if (closure->clc_wait) {
1525 cl_lock_get_trust(lock);
1526 lu_ref_add(&lock->cll_reference, "closure-w", closure);
1527 cl_lock_mutex_put(env, closure->clc_origin);
1529 LASSERT(cl_lock_nr_mutexed(env) == 0);
1530 cl_lock_mutex_get(env, lock);
1531 cl_lock_mutex_put(env, lock);
1533 cl_lock_mutex_get(env, closure->clc_origin);
1534 lu_ref_del(&lock->cll_reference, "closure-w", closure);
1535 cl_lock_put(env, lock);
1537 result = CLO_REPEAT;
1541 EXPORT_SYMBOL(cl_lock_enclosure);
1543 /** Releases mutices of enclosed locks. */
1544 void cl_lock_disclosure(const struct lu_env *env,
1545 struct cl_lock_closure *closure)
1547 struct cl_lock *scan;
1548 struct cl_lock *temp;
1550 list_for_each_entry_safe(scan, temp, &closure->clc_list, cll_inclosure){
1551 list_del_init(&scan->cll_inclosure);
1552 cl_lock_mutex_put(env, scan);
1553 lu_ref_del(&scan->cll_reference, "closure", closure);
1554 cl_lock_put(env, scan);
1557 LASSERT(closure->clc_nr == 0);
1559 EXPORT_SYMBOL(cl_lock_disclosure);
1561 /** Finalizes a closure. */
1562 void cl_lock_closure_fini(struct cl_lock_closure *closure)
1564 LASSERT(closure->clc_nr == 0);
1565 LASSERT(list_empty(&closure->clc_list));
1567 EXPORT_SYMBOL(cl_lock_closure_fini);
1570 * Destroys this lock. Notifies layers (bottom-to-top) that lock is being
1571 * destroyed, then destroy the lock. If there are holds on the lock, postpone
1572 * destruction until all holds are released. This is called when a decision is
1573 * made to destroy the lock in the future. E.g., when a blocking AST is
1574 * received on it, or fatal communication error happens.
1576 * Caller must have a reference on this lock to prevent a situation, when
1577 * deleted lock lingers in memory for indefinite time, because nobody calls
1578 * cl_lock_put() to finish it.
1580 * \pre atomic_read(&lock->cll_ref) > 0
1581 * \pre ergo(cl_lock_nesting(lock) == CNL_TOP,
1582 * cl_lock_nr_mutexed(env) == 1)
1583 * [i.e., if a top-lock is deleted, mutices of no other locks can be
1584 * held, as deletion of sub-locks might require releasing a top-lock
1587 * \see cl_lock_operations::clo_delete()
1588 * \see cl_lock::cll_holds
1590 void cl_lock_delete(const struct lu_env *env, struct cl_lock *lock)
1592 LINVRNT(cl_lock_is_mutexed(lock));
1593 LINVRNT(cl_lock_invariant(env, lock));
1594 LASSERT(ergo(cl_lock_nesting(lock) == CNL_TOP,
1595 cl_lock_nr_mutexed(env) == 1));
1598 if (lock->cll_holds == 0)
1599 cl_lock_delete0(env, lock);
1601 lock->cll_flags |= CLF_DOOMED;
1604 EXPORT_SYMBOL(cl_lock_delete);
1607 * Mark lock as irrecoverably failed, and mark it for destruction. This
1608 * happens when, e.g., server fails to grant a lock to us, or networking
1611 * \pre atomic_read(&lock->cll_ref) > 0
1613 * \see clo_lock_delete()
1614 * \see cl_lock::cll_holds
1616 void cl_lock_error(const struct lu_env *env, struct cl_lock *lock, int error)
1618 LINVRNT(cl_lock_is_mutexed(lock));
1619 LINVRNT(cl_lock_invariant(env, lock));
1622 if (lock->cll_error == 0 && error != 0) {
1623 lock->cll_error = error;
1624 cl_lock_signal(env, lock);
1625 cl_lock_cancel(env, lock);
1626 cl_lock_delete(env, lock);
1630 EXPORT_SYMBOL(cl_lock_error);
1633 * Cancels this lock. Notifies layers
1634 * (bottom-to-top) that lock is being cancelled, then destroy the lock. If
1635 * there are holds on the lock, postpone cancellation until
1636 * all holds are released.
1638 * Cancellation notification is delivered to layers at most once.
1640 * \see cl_lock_operations::clo_cancel()
1641 * \see cl_lock::cll_holds
1643 void cl_lock_cancel(const struct lu_env *env, struct cl_lock *lock)
1645 LINVRNT(cl_lock_is_mutexed(lock));
1646 LINVRNT(cl_lock_invariant(env, lock));
1649 if (lock->cll_holds == 0)
1650 cl_lock_cancel0(env, lock);
1652 lock->cll_flags |= CLF_CANCELPEND;
1655 EXPORT_SYMBOL(cl_lock_cancel);
1658 * Finds an existing lock covering given page and optionally different from a
1659 * given \a except lock.
1661 struct cl_lock *cl_lock_at_page(const struct lu_env *env, struct cl_object *obj,
1662 struct cl_page *page, struct cl_lock *except,
1663 int pending, int canceld)
1665 struct cl_object_header *head;
1666 struct cl_lock *scan;
1667 struct cl_lock *lock;
1668 struct cl_lock_descr *need;
1672 head = cl_object_header(obj);
1673 need = &cl_env_info(env)->clt_descr;
1676 need->cld_mode = CLM_READ; /* CLM_READ matches both READ & WRITE, but
1678 need->cld_start = need->cld_end = page->cp_index;
1680 spin_lock(&head->coh_lock_guard);
1681 list_for_each_entry(scan, &head->coh_locks, cll_linkage) {
1682 if (scan != except &&
1683 cl_lock_ext_match(&scan->cll_descr, need) &&
1684 scan->cll_state >= CLS_HELD &&
1685 scan->cll_state < CLS_FREEING &&
1687 * This check is racy as the lock can be canceled right
1688 * after it is done, but this is fine, because page exists
1691 (canceld || !(scan->cll_flags & CLF_CANCELLED)) &&
1692 (pending || !(scan->cll_flags & CLF_CANCELPEND))) {
1693 /* Don't increase cs_hit here since this
1694 * is just a helper function. */
1695 cl_lock_get_trust(scan);
1700 spin_unlock(&head->coh_lock_guard);
1703 EXPORT_SYMBOL(cl_lock_at_page);
1706 * Returns a list of pages protected (only) by a given lock.
1708 * Scans an extent of page radix tree, corresponding to the \a lock and queues
1709 * all pages that are not protected by locks other than \a lock into \a queue.
1711 void cl_lock_page_list_fixup(const struct lu_env *env,
1712 struct cl_io *io, struct cl_lock *lock,
1713 struct cl_page_list *queue)
1715 struct cl_page *page;
1716 struct cl_page *temp;
1717 struct cl_page_list *plist = &cl_env_info(env)->clt_list;
1719 LINVRNT(cl_lock_invariant(env, lock));
1722 /* Now, we have a list of cl_pages under the \a lock, we need
1723 * to check if some of pages are covered by other ldlm lock.
1724 * If this is the case, they aren't needed to be written out this time.
1726 * For example, we have A:[0,200] & B:[100,300] PW locks on client, now
1727 * the latter is to be canceled, this means other client is
1728 * reading/writing [200,300] since A won't canceled. Actually
1729 * we just need to write the pages covered by [200,300]. This is safe,
1730 * since [100,200] is also protected lock A.
1733 cl_page_list_init(plist);
1734 cl_page_list_for_each_safe(page, temp, queue) {
1735 pgoff_t idx = page->cp_index;
1736 struct cl_lock *found;
1737 struct cl_lock_descr *descr;
1739 /* The algorithm counts on the index-ascending page index. */
1740 LASSERT(ergo(&temp->cp_batch != &queue->pl_pages,
1741 page->cp_index < temp->cp_index));
1743 found = cl_lock_at_page(env, lock->cll_descr.cld_obj,
1748 descr = &found->cll_descr;
1749 list_for_each_entry_safe_from(page, temp, &queue->pl_pages,
1751 idx = page->cp_index;
1752 if (descr->cld_start > idx || descr->cld_end < idx)
1754 cl_page_list_move(plist, queue, page);
1756 cl_lock_put(env, found);
1759 /* The pages in plist are covered by other locks, don't handle them
1763 cl_page_list_disown(env, io, plist);
1764 cl_page_list_fini(env, plist);
1767 EXPORT_SYMBOL(cl_lock_page_list_fixup);
1770 * Invalidate pages protected by the given lock, sending them out to the
1771 * server first, if necessary.
1773 * This function does the following:
1775 * - collects a list of pages to be invalidated,
1777 * - unmaps them from the user virtual memory,
1779 * - sends dirty pages to the server,
1781 * - waits for transfer completion,
1783 * - discards pages, and throws them out of memory.
1785 * If \a discard is set, pages are discarded without sending them to the
1788 * If error happens on any step, the process continues anyway (the reasoning
1789 * behind this being that lock cancellation cannot be delayed indefinitely).
1791 int cl_lock_page_out(const struct lu_env *env, struct cl_lock *lock,
1794 struct cl_thread_info *info = cl_env_info(env);
1795 struct cl_io *io = &info->clt_io;
1796 struct cl_2queue *queue = &info->clt_queue;
1797 struct cl_lock_descr *descr = &lock->cll_descr;
1802 LINVRNT(cl_lock_invariant(env, lock));
1805 io->ci_obj = cl_object_top(descr->cld_obj);
1806 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
1809 cl_2queue_init(queue);
1810 cl_page_gang_lookup(env, descr->cld_obj, io, descr->cld_start,
1811 descr->cld_end, &queue->c2_qin);
1812 if (queue->c2_qin.pl_nr > 0) {
1813 result = cl_page_list_unmap(env, io, &queue->c2_qin);
1815 rc0 = cl_io_submit_rw(env, io, CRT_WRITE,
1817 rc1 = cl_page_list_own(env, io,
1819 result = result ?: rc0 ?: rc1;
1821 cl_lock_page_list_fixup(env, io, lock, &queue->c2_qout);
1822 cl_2queue_discard(env, io, queue);
1823 cl_2queue_disown(env, io, queue);
1825 cl_2queue_fini(env, queue);
1827 cl_io_fini(env, io);
1830 EXPORT_SYMBOL(cl_lock_page_out);
1833 * Eliminate all locks for a given object.
1835 * Caller has to guarantee that no lock is in active use.
1837 * \param cancel when this is set, cl_locks_prune() cancels locks before
1840 void cl_locks_prune(const struct lu_env *env, struct cl_object *obj, int cancel)
1842 struct cl_object_header *head;
1843 struct cl_lock *lock;
1846 head = cl_object_header(obj);
1848 * If locks are destroyed without cancellation, all pages must be
1849 * already destroyed (as otherwise they will be left unprotected).
1851 LASSERT(ergo(!cancel,
1852 head->coh_tree.rnode == NULL && head->coh_pages == 0));
1854 spin_lock(&head->coh_lock_guard);
1855 while (!list_empty(&head->coh_locks)) {
1856 lock = container_of(head->coh_locks.next,
1857 struct cl_lock, cll_linkage);
1858 cl_lock_get_trust(lock);
1859 spin_unlock(&head->coh_lock_guard);
1860 lu_ref_add(&lock->cll_reference, "prune", cfs_current());
1861 cl_lock_mutex_get(env, lock);
1862 if (lock->cll_state < CLS_FREEING) {
1863 LASSERT(lock->cll_holds == 0);
1864 LASSERT(lock->cll_users == 0);
1866 cl_lock_cancel(env, lock);
1867 cl_lock_delete(env, lock);
1869 cl_lock_mutex_put(env, lock);
1870 lu_ref_del(&lock->cll_reference, "prune", cfs_current());
1871 cl_lock_put(env, lock);
1872 spin_lock(&head->coh_lock_guard);
1874 spin_unlock(&head->coh_lock_guard);
1877 EXPORT_SYMBOL(cl_locks_prune);
1880 * Returns true if \a addr is an address of an allocated cl_lock. Used in
1881 * assertions. This check is optimistically imprecise, i.e., it occasionally
1882 * returns true for the incorrect addresses, but if it returns false, then the
1883 * address is guaranteed to be incorrect. (Should be named cl_lockp().)
1887 int cl_is_lock(const void *addr)
1889 return cfs_mem_is_in_cache(addr, cl_lock_kmem);
1891 EXPORT_SYMBOL(cl_is_lock);
1893 static struct cl_lock *cl_lock_hold_mutex(const struct lu_env *env,
1894 const struct cl_io *io,
1895 const struct cl_lock_descr *need,
1896 const char *scope, const void *source)
1898 struct cl_lock *lock;
1903 lock = cl_lock_find(env, io, need);
1906 cl_lock_mutex_get(env, lock);
1907 if (lock->cll_state < CLS_FREEING) {
1908 cl_lock_hold_mod(env, lock, +1);
1909 lu_ref_add(&lock->cll_holders, scope, source);
1910 lu_ref_add(&lock->cll_reference, scope, source);
1913 cl_lock_mutex_put(env, lock);
1914 cl_lock_put(env, lock);
1920 * Returns a lock matching \a need description with a reference and a hold on
1923 * This is much like cl_lock_find(), except that cl_lock_hold() additionally
1924 * guarantees that lock is not in the CLS_FREEING state on return.
1926 struct cl_lock *cl_lock_hold(const struct lu_env *env, const struct cl_io *io,
1927 const struct cl_lock_descr *need,
1928 const char *scope, const void *source)
1930 struct cl_lock *lock;
1934 lock = cl_lock_hold_mutex(env, io, need, scope, source);
1936 cl_lock_mutex_put(env, lock);
1939 EXPORT_SYMBOL(cl_lock_hold);
1942 * Main high-level entry point of cl_lock interface that finds existing or
1943 * enqueues new lock matching given description.
1945 struct cl_lock *cl_lock_request(const struct lu_env *env, struct cl_io *io,
1946 const struct cl_lock_descr *need,
1948 const char *scope, const void *source)
1950 struct cl_lock *lock;
1951 const struct lu_fid *fid;
1957 fid = lu_object_fid(&io->ci_obj->co_lu);
1960 warn = iter >= 16 && IS_PO2(iter);
1961 CDEBUG(warn ? D_WARNING : D_DLMTRACE,
1962 DDESCR"@"DFID" %i %08x `%s'\n",
1963 PDESCR(need), PFID(fid), iter, enqflags, scope);
1964 lock = cl_lock_hold_mutex(env, io, need, scope, source);
1965 if (!IS_ERR(lock)) {
1966 rc = cl_enqueue_locked(env, lock, io, enqflags);
1968 if (cl_lock_fits_into(env, lock, need, io)) {
1969 cl_lock_mutex_put(env, lock);
1970 cl_lock_lockdep_acquire(env,
1974 CL_LOCK_DEBUG(D_WARNING, env, lock,
1975 "got (see bug 17665)\n");
1976 cl_unuse_locked(env, lock);
1978 cl_lock_hold_release(env, lock, scope, source);
1979 cl_lock_mutex_put(env, lock);
1980 lu_ref_del(&lock->cll_reference, scope, source);
1981 cl_lock_put(env, lock);
1989 EXPORT_SYMBOL(cl_lock_request);
1992 * Adds a hold to a known lock.
1994 void cl_lock_hold_add(const struct lu_env *env, struct cl_lock *lock,
1995 const char *scope, const void *source)
1997 LINVRNT(cl_lock_is_mutexed(lock));
1998 LINVRNT(cl_lock_invariant(env, lock));
1999 LASSERT(lock->cll_state != CLS_FREEING);
2002 cl_lock_hold_mod(env, lock, +1);
2004 lu_ref_add(&lock->cll_holders, scope, source);
2005 lu_ref_add(&lock->cll_reference, scope, source);
2008 EXPORT_SYMBOL(cl_lock_hold_add);
2011 * Releases a hold and a reference on a lock, on which caller acquired a
2014 void cl_lock_unhold(const struct lu_env *env, struct cl_lock *lock,
2015 const char *scope, const void *source)
2017 LINVRNT(cl_lock_invariant(env, lock));
2019 cl_lock_hold_release(env, lock, scope, source);
2020 lu_ref_del(&lock->cll_reference, scope, source);
2021 cl_lock_put(env, lock);
2024 EXPORT_SYMBOL(cl_lock_unhold);
2027 * Releases a hold and a reference on a lock, obtained by cl_lock_hold().
2029 void cl_lock_release(const struct lu_env *env, struct cl_lock *lock,
2030 const char *scope, const void *source)
2032 LINVRNT(cl_lock_invariant(env, lock));
2034 cl_lock_mutex_get(env, lock);
2035 cl_lock_hold_release(env, lock, scope, source);
2036 cl_lock_mutex_put(env, lock);
2037 lu_ref_del(&lock->cll_reference, scope, source);
2038 cl_lock_put(env, lock);
2041 EXPORT_SYMBOL(cl_lock_release);
2043 void cl_lock_user_add(const struct lu_env *env, struct cl_lock *lock)
2045 LINVRNT(cl_lock_is_mutexed(lock));
2046 LINVRNT(cl_lock_invariant(env, lock));
2049 cl_lock_used_mod(env, lock, +1);
2052 EXPORT_SYMBOL(cl_lock_user_add);
2054 int cl_lock_user_del(const struct lu_env *env, struct cl_lock *lock)
2056 LINVRNT(cl_lock_is_mutexed(lock));
2057 LINVRNT(cl_lock_invariant(env, lock));
2058 LASSERT(lock->cll_users > 0);
2061 cl_lock_used_mod(env, lock, -1);
2062 RETURN(lock->cll_users == 0);
2064 EXPORT_SYMBOL(cl_lock_user_del);
2067 * Check if two lock's mode are compatible.
2069 * This returns true iff en-queuing \a lock2 won't cause cancellation of \a
2070 * lock1 even when these locks overlap.
2072 int cl_lock_compatible(const struct cl_lock *lock1, const struct cl_lock *lock2)
2074 enum cl_lock_mode mode1;
2075 enum cl_lock_mode mode2;
2078 mode1 = lock1->cll_descr.cld_mode;
2079 mode2 = lock2->cll_descr.cld_mode;
2080 RETURN(mode2 == CLM_PHANTOM ||
2081 (mode1 == CLM_READ && mode2 == CLM_READ));
2083 EXPORT_SYMBOL(cl_lock_compatible);
2085 const char *cl_lock_mode_name(const enum cl_lock_mode mode)
2087 static const char *names[] = {
2088 [CLM_PHANTOM] = "PHANTOM",
2089 [CLM_READ] = "READ",
2090 [CLM_WRITE] = "WRITE",
2091 [CLM_GROUP] = "GROUP"
2093 if (0 <= mode && mode < ARRAY_SIZE(names))
2098 EXPORT_SYMBOL(cl_lock_mode_name);
2101 * Prints human readable representation of a lock description.
2103 void cl_lock_descr_print(const struct lu_env *env, void *cookie,
2104 lu_printer_t printer,
2105 const struct cl_lock_descr *descr)
2107 const struct lu_fid *fid;
2109 fid = lu_object_fid(&descr->cld_obj->co_lu);
2110 (*printer)(env, cookie, DDESCR"@"DFID, PDESCR(descr), PFID(fid));
2112 EXPORT_SYMBOL(cl_lock_descr_print);
2115 * Prints human readable representation of \a lock to the \a f.
2117 void cl_lock_print(const struct lu_env *env, void *cookie,
2118 lu_printer_t printer, const struct cl_lock *lock)
2120 const struct cl_lock_slice *slice;
2121 (*printer)(env, cookie, "lock@%p[%d %d %d %d %d %08lx] ",
2122 lock, atomic_read(&lock->cll_ref),
2123 lock->cll_state, lock->cll_error, lock->cll_holds,
2124 lock->cll_users, lock->cll_flags);
2125 cl_lock_descr_print(env, cookie, printer, &lock->cll_descr);
2126 (*printer)(env, cookie, " {\n");
2128 list_for_each_entry(slice, &lock->cll_layers, cls_linkage) {
2129 (*printer)(env, cookie, " %s@%p: ",
2130 slice->cls_obj->co_lu.lo_dev->ld_type->ldt_name,
2132 if (slice->cls_ops->clo_print != NULL)
2133 slice->cls_ops->clo_print(env, cookie, printer, slice);
2134 (*printer)(env, cookie, "\n");
2136 (*printer)(env, cookie, "} lock@%p\n", lock);
2138 EXPORT_SYMBOL(cl_lock_print);
2140 int cl_lock_init(void)
2142 return lu_kmem_init(cl_lock_caches);
2145 void cl_lock_fini(void)
2147 lu_kmem_fini(cl_lock_caches);