1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_lock for OSC layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_OSC
44 # include <libcfs/libcfs.h>
46 # include <liblustre.h>
48 /* fid_build_reg_res_name() */
49 #include <lustre_fid.h>
51 #include "osc_cl_internal.h"
57 /*****************************************************************************
63 static const struct cl_lock_operations osc_lock_ops;
64 static const struct cl_lock_operations osc_lock_lockless_ops;
65 static void osc_lock_to_lockless(const struct lu_env *env,
66 struct osc_lock *ols, int force);
67 static int osc_lock_has_pages(struct osc_lock *olck);
69 int osc_lock_is_lockless(const struct osc_lock *olck)
71 return (olck->ols_cl.cls_ops == &osc_lock_lockless_ops);
75 * Returns a weak pointer to the ldlm lock identified by a handle. Returned
76 * pointer cannot be dereferenced, as lock is not protected from concurrent
77 * reclaim. This function is a helper for osc_lock_invariant().
79 static struct ldlm_lock *osc_handle_ptr(struct lustre_handle *handle)
81 struct ldlm_lock *lock;
83 lock = ldlm_handle2lock(handle);
90 * Invariant that has to be true all of the time.
92 static int osc_lock_invariant(struct osc_lock *ols)
94 struct ldlm_lock *lock = osc_handle_ptr(&ols->ols_handle);
95 struct ldlm_lock *olock = ols->ols_lock;
96 int handle_used = lustre_handle_is_used(&ols->ols_handle);
99 ergo(osc_lock_is_lockless(ols),
100 ols->ols_locklessable && ols->ols_lock == NULL) ||
101 (ergo(olock != NULL, handle_used) &&
103 olock->l_handle.h_cookie == ols->ols_handle.cookie) &&
105 * Check that ->ols_handle and ->ols_lock are consistent, but
106 * take into account that they are set at the different time.
109 ergo(lock != NULL && olock != NULL, lock == olock) &&
110 ergo(lock == NULL, olock == NULL)) &&
111 ergo(ols->ols_state == OLS_CANCELLED,
112 olock == NULL && !handle_used) &&
114 * DLM lock is destroyed only after we have seen cancellation
117 ergo(olock != NULL && ols->ols_state < OLS_CANCELLED,
118 !olock->l_destroyed) &&
119 ergo(ols->ols_state == OLS_GRANTED,
121 olock->l_req_mode == olock->l_granted_mode &&
125 /*****************************************************************************
132 * Breaks a link between osc_lock and dlm_lock.
134 static void osc_lock_detach(const struct lu_env *env, struct osc_lock *olck)
136 struct ldlm_lock *dlmlock;
138 spin_lock(&osc_ast_guard);
139 dlmlock = olck->ols_lock;
140 if (dlmlock == NULL) {
141 spin_unlock(&osc_ast_guard);
145 olck->ols_lock = NULL;
146 /* wb(); --- for all who checks (ols->ols_lock != NULL) before
147 * call to osc_lock_detach() */
148 dlmlock->l_ast_data = NULL;
149 olck->ols_handle.cookie = 0ULL;
150 spin_unlock(&osc_ast_guard);
152 lock_res_and_lock(dlmlock);
153 if (dlmlock->l_granted_mode == dlmlock->l_req_mode) {
154 struct cl_object *obj = olck->ols_cl.cls_obj;
155 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
156 __u64 old_kms = cl2osc(obj)->oo_oinfo->loi_kms;
158 /* Update the kms. Need to loop all granted locks.
159 * Not a problem for the client */
160 attr->cat_kms = ldlm_extent_shift_kms(dlmlock, old_kms);
161 unlock_res_and_lock(dlmlock);
163 cl_object_attr_lock(obj);
164 cl_object_attr_set(env, obj, attr, CAT_KMS);
165 cl_object_attr_unlock(obj);
167 unlock_res_and_lock(dlmlock);
169 /* release a reference taken in osc_lock_upcall0(). */
170 lu_ref_del(&dlmlock->l_reference, "osc_lock", olck);
171 LDLM_LOCK_RELEASE(dlmlock);
174 static int osc_lock_unuse(const struct lu_env *env,
175 const struct cl_lock_slice *slice)
177 struct osc_lock *ols = cl2osc_lock(slice);
180 LASSERT(ols->ols_state == OLS_GRANTED ||
181 ols->ols_state == OLS_UPCALL_RECEIVED);
182 LINVRNT(osc_lock_invariant(ols));
184 if (ols->ols_glimpse) {
185 LASSERT(ols->ols_hold == 0);
188 LASSERT(ols->ols_hold);
191 * Move lock into OLS_RELEASED state before calling osc_cancel_base()
192 * so that possible synchronous cancellation (that always happens
193 * e.g., for liblustre) sees that lock is released.
195 ols->ols_state = OLS_RELEASED;
197 result = osc_cancel_base(&ols->ols_handle, ols->ols_einfo.ei_mode);
198 ols->ols_has_ref = 0;
202 static void osc_lock_fini(const struct lu_env *env,
203 struct cl_lock_slice *slice)
205 struct osc_lock *ols = cl2osc_lock(slice);
207 LINVRNT(osc_lock_invariant(ols));
209 * ->ols_hold can still be true at this point if, for example, a
210 * thread that requested a lock was killed (and released a reference
211 * to the lock), before reply from a server was received. In this case
212 * lock is destroyed immediately after upcall.
215 osc_lock_unuse(env, slice);
216 LASSERT(ols->ols_lock == NULL);
218 OBD_SLAB_FREE_PTR(ols, osc_lock_kmem);
221 void osc_lock_build_res(const struct lu_env *env, const struct osc_object *obj,
222 struct ldlm_res_id *resname)
224 const struct lu_fid *fid = lu_object_fid(&obj->oo_cl.co_lu);
227 * In the perfect world of the future, where ost servers talk
230 fid_build_reg_res_name(fid, resname);
233 * In reality, where ost server expects ->lsm_object_id and
234 * ->lsm_object_gr in rename.
236 osc_build_res_name(obj->oo_oinfo->loi_id, obj->oo_oinfo->loi_gr,
241 static void osc_lock_build_policy(const struct lu_env *env,
242 const struct cl_lock *lock,
243 ldlm_policy_data_t *policy)
245 const struct cl_lock_descr *d = &lock->cll_descr;
247 osc_index2policy(policy, d->cld_obj, d->cld_start, d->cld_end);
248 policy->l_extent.gid = d->cld_gid;
251 static int osc_enq2ldlm_flags(__u32 enqflags)
255 LASSERT((enqflags & ~CEF_MASK) == 0);
257 if (enqflags & CEF_NONBLOCK)
258 result |= LDLM_FL_BLOCK_NOWAIT;
259 if (enqflags & CEF_ASYNC)
260 result |= LDLM_FL_HAS_INTENT;
261 if (enqflags & CEF_DISCARD_DATA)
262 result |= LDLM_AST_DISCARD_DATA;
267 * Global spin-lock protecting consistency of ldlm_lock::l_ast_data
268 * pointers. Initialized in osc_init().
270 spinlock_t osc_ast_guard;
272 static struct osc_lock *osc_ast_data_get(struct ldlm_lock *dlm_lock)
274 struct osc_lock *olck;
276 lock_res_and_lock(dlm_lock);
277 spin_lock(&osc_ast_guard);
278 olck = dlm_lock->l_ast_data;
280 struct cl_lock *lock = olck->ols_cl.cls_lock;
282 * If osc_lock holds a reference on ldlm lock, return it even
283 * when cl_lock is in CLS_FREEING state. This way
285 * osc_ast_data_get(dlmlock) == NULL
287 * guarantees that all osc references on dlmlock were
288 * released. osc_dlm_blocking_ast0() relies on that.
290 if (lock->cll_state < CLS_FREEING || olck->ols_has_ref) {
291 cl_lock_get_trust(lock);
292 lu_ref_add_atomic(&lock->cll_reference,
293 "ast", cfs_current());
297 spin_unlock(&osc_ast_guard);
298 unlock_res_and_lock(dlm_lock);
302 static void osc_ast_data_put(const struct lu_env *env, struct osc_lock *olck)
304 struct cl_lock *lock;
306 lock = olck->ols_cl.cls_lock;
307 lu_ref_del(&lock->cll_reference, "ast", cfs_current());
308 cl_lock_put(env, lock);
312 * Updates object attributes from a lock value block (lvb) received together
313 * with the DLM lock reply from the server. Copy of osc_update_enqueue()
316 * This can be optimized to not update attributes when lock is a result of a
319 * Called under lock and resource spin-locks.
321 static void osc_lock_lvb_update(const struct lu_env *env, struct osc_lock *olck,
325 struct cl_object *obj;
326 struct lov_oinfo *oinfo;
327 struct cl_attr *attr;
332 if (!(olck->ols_flags & LDLM_FL_LVB_READY)) {
337 lvb = &olck->ols_lvb;
338 obj = olck->ols_cl.cls_obj;
339 oinfo = cl2osc(obj)->oo_oinfo;
340 attr = &osc_env_info(env)->oti_attr;
341 valid = CAT_BLOCKS | CAT_ATIME | CAT_CTIME | CAT_MTIME | CAT_SIZE;
342 cl_lvb2attr(attr, lvb);
344 cl_object_attr_lock(obj);
346 struct ldlm_lock *dlmlock;
349 dlmlock = olck->ols_lock;
350 LASSERT(dlmlock != NULL);
352 /* re-grab LVB from a dlm lock under DLM spin-locks. */
353 *lvb = *(struct ost_lvb *)dlmlock->l_lvb_data;
354 size = lvb->lvb_size;
355 /* Extend KMS up to the end of this lock and no further
356 * A lock on [x,y] means a KMS of up to y + 1 bytes! */
357 if (size > dlmlock->l_policy_data.l_extent.end)
358 size = dlmlock->l_policy_data.l_extent.end + 1;
359 if (size >= oinfo->loi_kms) {
360 LDLM_DEBUG(dlmlock, "lock acquired, setting rss="LPU64
361 ", kms="LPU64, lvb->lvb_size, size);
363 attr->cat_kms = size;
365 LDLM_DEBUG(dlmlock, "lock acquired, setting rss="
366 LPU64"; leaving kms="LPU64", end="LPU64,
367 lvb->lvb_size, oinfo->loi_kms,
368 dlmlock->l_policy_data.l_extent.end);
370 ldlm_lock_allow_match_locked(dlmlock);
371 } else if (rc == -ENAVAIL && olck->ols_glimpse) {
372 CDEBUG(D_INODE, "glimpsed, setting rss="LPU64"; leaving"
373 " kms="LPU64"\n", lvb->lvb_size, oinfo->loi_kms);
378 cl_object_attr_set(env, obj, attr, valid);
380 cl_object_attr_unlock(obj);
386 * Called when a lock is granted, from an upcall (when server returned a
387 * granted lock), or from completion AST, when server returned a blocked lock.
389 * Called under lock and resource spin-locks, that are released temporarily
392 static void osc_lock_granted(const struct lu_env *env, struct osc_lock *olck,
393 struct ldlm_lock *dlmlock, int rc)
395 struct ldlm_extent *ext;
396 struct cl_lock *lock;
397 struct cl_lock_descr *descr;
399 LASSERT(dlmlock->l_granted_mode == dlmlock->l_req_mode);
402 if (olck->ols_state != OLS_GRANTED) {
403 lock = olck->ols_cl.cls_lock;
404 ext = &dlmlock->l_policy_data.l_extent;
405 descr = &osc_env_info(env)->oti_descr;
406 descr->cld_obj = lock->cll_descr.cld_obj;
408 /* XXX check that ->l_granted_mode is valid. */
409 descr->cld_mode = osc_ldlm2cl_lock(dlmlock->l_granted_mode);
410 descr->cld_start = cl_index(descr->cld_obj, ext->start);
411 descr->cld_end = cl_index(descr->cld_obj, ext->end);
412 descr->cld_gid = ext->gid;
414 * tell upper layers the extent of the lock that was actually
417 olck->ols_state = OLS_GRANTED;
418 osc_lock_lvb_update(env, olck, rc);
420 /* release DLM spin-locks to allow cl_lock_{modify,signal}()
421 * to take a semaphore on a parent lock. This is safe, because
422 * spin-locks are needed to protect consistency of
423 * dlmlock->l_*_mode and LVB, and we have finished processing
425 unlock_res_and_lock(dlmlock);
426 cl_lock_modify(env, lock, descr);
427 cl_lock_signal(env, lock);
428 LINVRNT(osc_lock_invariant(olck));
429 lock_res_and_lock(dlmlock);
434 static void osc_lock_upcall0(const struct lu_env *env, struct osc_lock *olck)
437 struct ldlm_lock *dlmlock;
441 dlmlock = ldlm_handle2lock_long(&olck->ols_handle, 0);
442 LASSERT(dlmlock != NULL);
444 lock_res_and_lock(dlmlock);
445 spin_lock(&osc_ast_guard);
446 LASSERT(dlmlock->l_ast_data == olck);
447 LASSERT(olck->ols_lock == NULL);
448 olck->ols_lock = dlmlock;
449 spin_unlock(&osc_ast_guard);
452 * Lock might be not yet granted. In this case, completion ast
453 * (osc_ldlm_completion_ast()) comes later and finishes lock
456 if (dlmlock->l_granted_mode == dlmlock->l_req_mode)
457 osc_lock_granted(env, olck, dlmlock, 0);
458 unlock_res_and_lock(dlmlock);
461 * osc_enqueue_interpret() decrefs asynchronous locks, counter
464 ldlm_lock_addref(&olck->ols_handle, olck->ols_einfo.ei_mode);
465 olck->ols_hold = olck->ols_has_ref = 1;
467 /* lock reference taken by ldlm_handle2lock_long() is owned by
468 * osc_lock and released in osc_lock_detach() */
469 lu_ref_add(&dlmlock->l_reference, "osc_lock", olck);
473 * Lock upcall function that is executed either when a reply to ENQUEUE rpc is
474 * received from a server, or after osc_enqueue_base() matched a local DLM
477 static int osc_lock_upcall(void *cookie, int errcode)
479 struct osc_lock *olck = cookie;
480 struct cl_lock_slice *slice = &olck->ols_cl;
481 struct cl_lock *lock = slice->cls_lock;
483 struct cl_env_nest nest;
486 env = cl_env_nested_get(&nest);
490 cl_lock_mutex_get(env, lock);
492 LASSERT(lock->cll_state >= CLS_QUEUING);
493 if (olck->ols_state == OLS_ENQUEUED) {
494 olck->ols_state = OLS_UPCALL_RECEIVED;
495 rc = ldlm_error2errno(errcode);
496 } else if (olck->ols_state == OLS_CANCELLED) {
499 CERROR("Impossible state: %i\n", olck->ols_state);
503 struct ldlm_lock *dlmlock;
505 dlmlock = ldlm_handle2lock(&olck->ols_handle);
506 if (dlmlock != NULL) {
507 lock_res_and_lock(dlmlock);
508 spin_lock(&osc_ast_guard);
509 LASSERT(olck->ols_lock == NULL);
510 dlmlock->l_ast_data = NULL;
511 olck->ols_handle.cookie = 0ULL;
512 spin_unlock(&osc_ast_guard);
513 unlock_res_and_lock(dlmlock);
514 LDLM_LOCK_PUT(dlmlock);
517 if (olck->ols_glimpse)
518 olck->ols_glimpse = 0;
519 osc_lock_upcall0(env, olck);
522 /* Error handling, some errors are tolerable. */
523 if (olck->ols_locklessable && rc == -EUSERS) {
524 /* This is a tolerable error, turn this lock into
527 osc_object_set_contended(cl2osc(slice->cls_obj));
528 LASSERT(slice->cls_ops == &osc_lock_ops);
530 /* Change this lock to ldlmlock-less lock. */
531 osc_lock_to_lockless(env, olck, 1);
532 olck->ols_state = OLS_GRANTED;
534 } else if (olck->ols_glimpse && rc == -ENAVAIL) {
535 osc_lock_lvb_update(env, olck, rc);
536 cl_lock_delete(env, lock);
537 /* Hide the error. */
542 /* on error, lock was signaled by cl_lock_error() */
543 cl_lock_signal(env, lock);
545 cl_lock_error(env, lock, rc);
547 cl_lock_mutex_put(env, lock);
549 /* release cookie reference, acquired by osc_lock_enqueue() */
550 lu_ref_del(&lock->cll_reference, "upcall", lock);
551 cl_lock_put(env, lock);
552 cl_env_nested_put(&nest, env);
554 /* should never happen, similar to osc_ldlm_blocking_ast(). */
560 * Core of osc_dlm_blocking_ast() logic.
562 static void osc_lock_blocking(const struct lu_env *env,
563 struct ldlm_lock *dlmlock,
564 struct osc_lock *olck, int blocking)
566 struct cl_lock *lock = olck->ols_cl.cls_lock;
568 LASSERT(olck->ols_lock == dlmlock);
569 CLASSERT(OLS_BLOCKED < OLS_CANCELLED);
570 LASSERT(!osc_lock_is_lockless(olck));
574 * Lock might be still addref-ed here, if e.g., blocking ast
575 * is sent for a failed lock.
577 osc_lock_unuse(env, &olck->ols_cl);
579 if (blocking && olck->ols_state < OLS_BLOCKED)
581 * Move osc_lock into OLS_BLOCKED before canceling the lock,
582 * because it recursively re-enters osc_lock_blocking(), with
583 * the state set to OLS_CANCELLED.
585 olck->ols_state = OLS_BLOCKED;
587 * cancel and destroy lock at least once no matter how blocking ast is
588 * entered (see comment above osc_ldlm_blocking_ast() for use
589 * cases). cl_lock_cancel() and cl_lock_delete() are idempotent.
591 cl_lock_cancel(env, lock);
592 cl_lock_delete(env, lock);
596 * Helper for osc_dlm_blocking_ast() handling discrepancies between cl_lock
597 * and ldlm_lock caches.
599 static int osc_dlm_blocking_ast0(const struct lu_env *env,
600 struct ldlm_lock *dlmlock,
601 void *data, int flag)
603 struct osc_lock *olck;
604 struct cl_lock *lock;
608 LASSERT(flag == LDLM_CB_BLOCKING || flag == LDLM_CB_CANCELING);
611 olck = osc_ast_data_get(dlmlock);
613 lock = olck->ols_cl.cls_lock;
614 cl_lock_mutex_get(env, lock);
615 LINVRNT(osc_lock_invariant(olck));
616 if (olck->ols_ast_wait) {
617 /* wake up osc_lock_use() */
618 cl_lock_signal(env, lock);
619 olck->ols_ast_wait = 0;
622 * Lock might have been canceled while this thread was
623 * sleeping for lock mutex, but olck is pinned in memory.
625 if (olck == dlmlock->l_ast_data) {
627 * NOTE: DLM sends blocking AST's for failed locks
628 * (that are still in pre-OLS_GRANTED state)
629 * too, and they have to be canceled otherwise
630 * DLM lock is never destroyed and stuck in
633 * Alternatively, ldlm_cli_cancel() can be
634 * called here directly for osc_locks with
635 * ols_state < OLS_GRANTED to maintain an
636 * invariant that ->clo_cancel() is only called
637 * for locks that were granted.
639 LASSERT(data == olck);
640 osc_lock_blocking(env, dlmlock,
641 olck, flag == LDLM_CB_BLOCKING);
644 cl_lock_mutex_put(env, lock);
645 osc_ast_data_put(env, olck);
648 * DLM lock exists, but there is no cl_lock attached to it.
649 * This is a `normal' race. cl_object and its cl_lock's can be
650 * removed by memory pressure, together with all pages.
652 cancel = (flag == LDLM_CB_BLOCKING);
655 struct lustre_handle *lockh;
657 lockh = &osc_env_info(env)->oti_handle;
658 ldlm_lock2handle(dlmlock, lockh);
659 result = ldlm_cli_cancel(lockh);
666 * Blocking ast invoked by ldlm when dlm lock is either blocking progress of
667 * some other lock, or is canceled. This function is installed as a
668 * ldlm_lock::l_blocking_ast() for client extent locks.
670 * Control flow is tricky, because ldlm uses the same call-back
671 * (ldlm_lock::l_blocking_ast()) for both blocking and cancellation ast's.
673 * \param dlmlock lock for which ast occurred.
675 * \param new description of a conflicting lock in case of blocking ast.
677 * \param data value of dlmlock->l_ast_data
679 * \param flag LDLM_CB_BLOCKING or LDLM_CB_CANCELING. Used to distinguish
680 * cancellation and blocking ast's.
682 * Possible use cases:
684 * - ldlm calls dlmlock->l_blocking_ast(..., LDLM_CB_CANCELING) to cancel
685 * lock due to lock lru pressure, or explicit user request to purge
688 * - ldlm calls dlmlock->l_blocking_ast(..., LDLM_CB_BLOCKING) to notify
689 * us that dlmlock conflicts with another lock that some client is
690 * enqueing. Lock is canceled.
692 * - cl_lock_cancel() is called. osc_lock_cancel() calls
693 * ldlm_cli_cancel() that calls
695 * dlmlock->l_blocking_ast(..., LDLM_CB_CANCELING)
697 * recursively entering osc_ldlm_blocking_ast().
699 * - client cancels lock voluntary (e.g., as a part of early cancellation):
702 * osc_lock_cancel()->
703 * ldlm_cli_cancel()->
704 * dlmlock->l_blocking_ast(..., LDLM_CB_CANCELING)
707 static int osc_ldlm_blocking_ast(struct ldlm_lock *dlmlock,
708 struct ldlm_lock_desc *new, void *data,
712 struct cl_env_nest nest;
716 * This can be called in the context of outer IO, e.g.,
719 * ->osc_enqueue_base()->...
720 * ->ldlm_prep_elc_req()->...
721 * ->ldlm_cancel_callback()->...
722 * ->osc_ldlm_blocking_ast()
724 * new environment has to be created to not corrupt outer context.
726 env = cl_env_nested_get(&nest);
728 result = osc_dlm_blocking_ast0(env, dlmlock, data, flag);
729 cl_env_nested_put(&nest, env);
731 result = PTR_ERR(env);
733 * XXX This should never happen, as cl_lock is
734 * stuck. Pre-allocated environment a la vvp_inode_fini_env
740 if (result == -ENODATA)
743 CERROR("BAST failed: %d\n", result);
748 static int osc_ldlm_completion_ast(struct ldlm_lock *dlmlock,
749 int flags, void *data)
751 struct cl_env_nest nest;
753 struct osc_lock *olck;
754 struct cl_lock *lock;
758 /* first, do dlm part of the work */
759 dlmrc = ldlm_completion_ast_async(dlmlock, flags, data);
760 /* then, notify cl_lock */
761 env = cl_env_nested_get(&nest);
763 olck = osc_ast_data_get(dlmlock);
765 lock = olck->ols_cl.cls_lock;
766 cl_lock_mutex_get(env, lock);
768 * ldlm_handle_cp_callback() copied LVB from request
769 * to lock->l_lvb_data, store it in osc_lock.
771 LASSERT(dlmlock->l_lvb_data != NULL);
772 lock_res_and_lock(dlmlock);
773 olck->ols_lvb = *(struct ost_lvb *)dlmlock->l_lvb_data;
774 if (olck->ols_lock == NULL)
776 * upcall (osc_lock_upcall()) hasn't yet been
777 * called. Do nothing now, upcall will bind
778 * olck to dlmlock and signal the waiters.
780 * This maintains an invariant that osc_lock
781 * and ldlm_lock are always bound when
782 * osc_lock is in OLS_GRANTED state.
785 else if (dlmlock->l_granted_mode != LCK_MINMODE)
786 osc_lock_granted(env, olck, dlmlock, dlmrc);
787 unlock_res_and_lock(dlmlock);
789 cl_lock_error(env, lock, dlmrc);
790 cl_lock_mutex_put(env, lock);
791 osc_ast_data_put(env, olck);
794 result = -ELDLM_NO_LOCK_DATA;
795 cl_env_nested_put(&nest, env);
797 result = PTR_ERR(env);
798 return dlmrc ?: result;
801 static int osc_ldlm_glimpse_ast(struct ldlm_lock *dlmlock, void *data)
803 struct ptlrpc_request *req = data;
804 struct osc_lock *olck;
805 struct cl_lock *lock;
806 struct cl_object *obj;
807 struct cl_env_nest nest;
810 struct req_capsule *cap;
813 LASSERT(lustre_msg_get_opc(req->rq_reqmsg) == LDLM_GL_CALLBACK);
815 env = cl_env_nested_get(&nest);
818 * osc_ast_data_get() has to go after environment is
819 * allocated, because osc_ast_data() acquires a
820 * reference to a lock, and it can only be released in
823 olck = osc_ast_data_get(dlmlock);
825 lock = olck->ols_cl.cls_lock;
826 cl_lock_mutex_get(env, lock);
828 req_capsule_extend(cap, &RQF_LDLM_GL_CALLBACK);
829 req_capsule_set_size(cap, &RMF_DLM_LVB, RCL_SERVER,
831 result = req_capsule_server_pack(cap);
833 lvb = req_capsule_server_get(cap, &RMF_DLM_LVB);
834 obj = lock->cll_descr.cld_obj;
835 result = cl_object_glimpse(env, obj, lvb);
837 cl_lock_mutex_put(env, lock);
838 osc_ast_data_put(env, olck);
841 * These errors are normal races, so we don't want to
842 * fill the console with messages by calling
845 lustre_pack_reply(req, 1, NULL, NULL);
846 result = -ELDLM_NO_LOCK_DATA;
848 cl_env_nested_put(&nest, env);
850 result = PTR_ERR(env);
851 req->rq_status = result;
855 static unsigned long osc_lock_weigh(const struct lu_env *env,
856 const struct cl_lock_slice *slice)
859 * don't need to grab coh_page_guard since we don't care the exact #
862 return cl_object_header(slice->cls_obj)->coh_pages;
866 * Get the weight of dlm lock for early cancellation.
868 * XXX: it should return the pages covered by this \a dlmlock.
870 static unsigned long osc_ldlm_weigh_ast(struct ldlm_lock *dlmlock)
872 struct cl_env_nest nest;
874 struct osc_lock *lock;
876 unsigned long weight;
881 * osc_ldlm_weigh_ast has a complex context since it might be called
882 * because of lock canceling, or from user's input. We have to make
883 * a new environment for it. Probably it is implementation safe to use
884 * the upper context because cl_lock_put don't modify environment
885 * variables. But in case of ..
887 env = cl_env_nested_get(&nest);
889 /* Mostly because lack of memory, tend to eliminate this lock*/
892 LASSERT(dlmlock->l_resource->lr_type == LDLM_EXTENT);
893 lock = osc_ast_data_get(dlmlock);
895 /* cl_lock was destroyed because of memory pressure.
896 * It is much reasonable to assign this type of lock
899 GOTO(out, weight = 0);
902 cll = lock->ols_cl.cls_lock;
903 cl_lock_mutex_get(env, cll);
904 weight = cl_lock_weigh(env, cll);
905 cl_lock_mutex_put(env, cll);
906 osc_ast_data_put(env, lock);
910 cl_env_nested_put(&nest, env);
914 static void osc_lock_build_einfo(const struct lu_env *env,
915 const struct cl_lock *clock,
916 struct osc_lock *lock,
917 struct ldlm_enqueue_info *einfo)
919 enum cl_lock_mode mode;
921 mode = clock->cll_descr.cld_mode;
922 if (mode == CLM_PHANTOM)
924 * For now, enqueue all glimpse locks in read mode. In the
925 * future, client might choose to enqueue LCK_PW lock for
926 * glimpse on a file opened for write.
930 einfo->ei_type = LDLM_EXTENT;
931 einfo->ei_mode = osc_cl_lock2ldlm(mode);
932 einfo->ei_cb_bl = osc_ldlm_blocking_ast;
933 einfo->ei_cb_cp = osc_ldlm_completion_ast;
934 einfo->ei_cb_gl = osc_ldlm_glimpse_ast;
935 einfo->ei_cb_wg = osc_ldlm_weigh_ast;
936 einfo->ei_cbdata = lock; /* value to be put into ->l_ast_data */
939 static int osc_lock_delete0(struct cl_lock *conflict)
941 struct cl_env_nest nest;
945 env = cl_env_nested_get(&nest);
947 cl_lock_delete(env, conflict);
948 cl_env_nested_put(&nest, env);
954 * Cancels \a conflict lock and waits until it reached CLS_FREEING state. This
955 * is called as a part of enqueuing to cancel conflicting locks early.
957 * \retval 0: success, \a conflict was cancelled and destroyed.
959 * \retval CLO_REPEAT: \a conflict was cancelled, but \a lock mutex was
960 * released in the process. Repeat enqueing.
962 * \retval -EWOULDBLOCK: \a conflict cannot be cancelled immediately, and
963 * either \a lock is non-blocking, or current thread
964 * holds other locks, that prevent it from waiting
965 * for cancel to complete.
967 * \retval -ve: other error, including -EINTR.
970 static int osc_lock_cancel_wait(const struct lu_env *env, struct cl_lock *lock,
971 struct cl_lock *conflict, int canwait)
975 LASSERT(cl_lock_is_mutexed(lock));
976 LASSERT(cl_lock_is_mutexed(conflict));
979 if (conflict->cll_state != CLS_FREEING) {
980 cl_lock_cancel(env, conflict);
981 rc = osc_lock_delete0(conflict);
984 if (conflict->cll_flags & (CLF_CANCELPEND|CLF_DOOMED)) {
986 if (cl_lock_nr_mutexed(env) > 2)
988 * If mutices of locks other than @lock and
989 * @scan are held by the current thread, it
990 * cannot wait on @scan state change in a
991 * dead-lock safe matter, so simply skip early
992 * cancellation in this case.
994 * This means that early cancellation doesn't
995 * work when there is even slight mutex
996 * contention, as top-lock's mutex is usually
1001 /* Waiting for @scan to be destroyed */
1002 cl_lock_mutex_put(env, lock);
1004 rc = cl_lock_state_wait(env, conflict);
1006 conflict->cll_state < CLS_FREEING);
1007 /* mutex was released, repeat enqueue. */
1008 rc = rc ?: CLO_REPEAT;
1009 cl_lock_mutex_get(env, lock);
1012 LASSERT(ergo(!rc, conflict->cll_state == CLS_FREEING));
1013 CDEBUG(D_INFO, "lock %p was %s freed now, rc (%d)\n",
1014 conflict, rc ? "not":"", rc);
1020 * Determine if the lock should be converted into a lockless lock.
1023 * - if the lock has an explicite requirment for a non-lockless lock;
1024 * - if the io lock request type ci_lockreq;
1025 * - send the enqueue rpc to ost to make the further decision;
1026 * - special treat to truncate lockless lock
1028 * Additional policy can be implemented here, e.g., never do lockless-io
1029 * for large extents.
1031 static void osc_lock_to_lockless(const struct lu_env *env,
1032 struct osc_lock *ols, int force)
1034 struct cl_lock_slice *slice = &ols->ols_cl;
1035 struct cl_lock *lock = slice->cls_lock;
1037 LASSERT(ols->ols_state == OLS_NEW ||
1038 ols->ols_state == OLS_UPCALL_RECEIVED);
1041 ols->ols_locklessable = 1;
1042 LASSERT(cl_lock_is_mutexed(lock));
1043 slice->cls_ops = &osc_lock_lockless_ops;
1045 struct osc_io *oio = osc_env_io(env);
1046 struct cl_io *io = oio->oi_cl.cis_io;
1047 struct cl_object *obj = slice->cls_obj;
1048 struct osc_object *oob = cl2osc(obj);
1049 const struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
1050 struct obd_connect_data *ocd;
1052 LASSERT(io->ci_lockreq == CILR_MANDATORY ||
1053 io->ci_lockreq == CILR_MAYBE ||
1054 io->ci_lockreq == CILR_NEVER);
1056 ocd = &class_exp2cliimp(osc_export(oob))->imp_connect_data;
1057 ols->ols_locklessable = (io->ci_type != CIT_TRUNC) &&
1058 (io->ci_lockreq == CILR_MAYBE) &&
1059 (ocd->ocd_connect_flags & OBD_CONNECT_SRVLOCK);
1060 if (io->ci_lockreq == CILR_NEVER ||
1062 (ols->ols_locklessable && osc_object_is_contended(oob)) ||
1063 /* lockless truncate */
1064 (io->ci_type == CIT_TRUNC &&
1065 (ocd->ocd_connect_flags & OBD_CONNECT_TRUNCLOCK) &&
1066 osd->od_lockless_truncate)) {
1067 ols->ols_locklessable = 1;
1068 slice->cls_ops = &osc_lock_lockless_ops;
1071 LASSERT(ergo(ols->ols_glimpse, !osc_lock_is_lockless(ols)));
1075 * Cancel all conflicting locks and wait for them to be destroyed.
1077 * This function is used for two purposes:
1079 * - early cancel all conflicting locks before starting IO, and
1081 * - guarantee that pages added to the page cache by lockless IO are never
1082 * covered by locks other than lockless IO lock, and, hence, are not
1083 * visible to other threads.
1085 static int osc_lock_enqueue_wait(const struct lu_env *env,
1086 const struct osc_lock *olck)
1088 struct cl_lock *lock = olck->ols_cl.cls_lock;
1089 struct cl_lock_descr *descr = &lock->cll_descr;
1090 struct cl_object_header *hdr = cl_object_header(descr->cld_obj);
1091 struct cl_lock_closure *closure = &osc_env_info(env)->oti_closure;
1092 struct cl_lock *scan;
1093 struct cl_lock *temp;
1094 int lockless = osc_lock_is_lockless(olck);
1100 LASSERT(cl_lock_is_mutexed(lock));
1101 LASSERT(lock->cll_state == CLS_QUEUING);
1104 * XXX This function could be sped up if we had asynchronous
1109 !(olck->ols_flags & LDLM_FL_BLOCK_NOWAIT) &&
1110 cl_lock_nr_mutexed(env) == 1;
1111 cl_lock_closure_init(env, closure, lock, canwait);
1112 spin_lock(&hdr->coh_lock_guard);
1113 list_for_each_entry_safe(scan, temp, &hdr->coh_locks, cll_linkage) {
1117 if (scan->cll_state < CLS_QUEUING ||
1118 scan->cll_state == CLS_FREEING ||
1119 scan->cll_descr.cld_start > descr->cld_end ||
1120 scan->cll_descr.cld_end < descr->cld_start)
1123 /* overlapped and living locks. */
1125 /* We're not supposed to give up group lock. */
1126 if (scan->cll_descr.cld_mode == CLM_GROUP) {
1127 LASSERT(descr->cld_mode != CLM_GROUP ||
1128 descr->cld_gid != scan->cll_descr.cld_gid);
1132 /* A tricky case for lockless pages:
1133 * We need to cancel the compatible locks if we're enqueuing
1134 * a lockless lock, for example:
1135 * imagine that client has PR lock on [0, 1000], and thread T0
1136 * is doing lockless IO in [500, 1500] region. Concurrent
1137 * thread T1 can see lockless data in [500, 1000], which is
1138 * wrong, because these data are possibly stale.
1140 if (!lockless && cl_lock_compatible(scan, lock))
1143 /* Now @scan is conflicting with @lock, this means current
1144 * thread have to sleep for @scan being destroyed. */
1145 cl_lock_get_trust(scan);
1146 if (&temp->cll_linkage != &hdr->coh_locks)
1147 cl_lock_get_trust(temp);
1148 spin_unlock(&hdr->coh_lock_guard);
1149 lu_ref_add(&scan->cll_reference, "cancel-wait", lock);
1151 LASSERT(list_empty(&closure->clc_list));
1152 rc = cl_lock_closure_build(env, scan, closure);
1154 rc = osc_lock_cancel_wait(env, lock, scan, canwait);
1155 cl_lock_disclosure(env, closure);
1156 if (rc == -EWOULDBLOCK)
1159 if (rc == CLO_REPEAT && !canwait)
1160 /* cannot wait... no early cancellation. */
1163 lu_ref_del(&scan->cll_reference, "cancel-wait", lock);
1164 cl_lock_put(env, scan);
1165 spin_lock(&hdr->coh_lock_guard);
1167 * Lock list could have been modified, while spin-lock was
1168 * released. Check that it is safe to continue.
1170 stop = list_empty(&temp->cll_linkage);
1171 if (&temp->cll_linkage != &hdr->coh_locks)
1172 cl_lock_put(env, temp);
1173 if (stop || rc != 0)
1176 spin_unlock(&hdr->coh_lock_guard);
1177 cl_lock_closure_fini(closure);
1182 * Deadlock avoidance for osc_lock_enqueue(). Consider following scenario:
1184 * - Thread0: obtains PR:[0, 10]. Lock is busy.
1186 * - Thread1: enqueues PW:[5, 50]. Blocking ast is sent to
1187 * PR:[0, 10], but cancellation of busy lock is postponed.
1189 * - Thread0: enqueue PR:[30, 40]. Lock is locally matched to
1190 * PW:[5, 50], and thread0 waits for the lock completion never
1191 * releasing PR:[0, 10]---deadlock.
1193 * The second PR lock can be glimpse (it is to deal with that situation that
1194 * ll_glimpse_size() has second argument, preventing local match of
1195 * not-yet-granted locks, see bug 10295). Similar situation is possible in the
1196 * case of memory mapped user level buffer.
1198 * To prevent this we can detect a situation when current "thread" or "io"
1199 * already holds a lock on this object and either add LDLM_FL_BLOCK_GRANTED to
1200 * the ols->ols_flags, or prevent local match with PW locks.
1202 static int osc_deadlock_is_possible(const struct lu_env *env,
1203 struct cl_lock *lock)
1205 struct cl_object *obj;
1206 struct cl_object_header *head;
1207 struct cl_lock *scan;
1214 LASSERT(cl_lock_is_mutexed(lock));
1216 oio = osc_env_io(env);
1217 obj = lock->cll_descr.cld_obj;
1218 head = cl_object_header(obj);
1221 spin_lock(&head->coh_lock_guard);
1222 list_for_each_entry(scan, &head->coh_locks, cll_linkage) {
1224 struct osc_lock *oscan;
1226 oscan = osc_lock_at(scan);
1227 LASSERT(oscan != NULL);
1228 if (oscan->ols_owner == oio) {
1234 spin_unlock(&head->coh_lock_guard);
1239 * Implementation of cl_lock_operations::clo_enqueue() method for osc
1240 * layer. This initiates ldlm enqueue:
1242 * - checks for possible dead-lock conditions (osc_deadlock_is_possible());
1244 * - cancels conflicting locks early (osc_lock_enqueue_wait());
1246 * - calls osc_enqueue_base() to do actual enqueue.
1248 * osc_enqueue_base() is supplied with an upcall function that is executed
1249 * when lock is received either after a local cached ldlm lock is matched, or
1250 * when a reply from the server is received.
1252 * This function does not wait for the network communication to complete.
1254 static int osc_lock_enqueue(const struct lu_env *env,
1255 const struct cl_lock_slice *slice,
1256 struct cl_io *unused, __u32 enqflags)
1258 struct osc_lock *ols = cl2osc_lock(slice);
1259 struct cl_lock *lock = ols->ols_cl.cls_lock;
1260 struct osc_object *obj = cl2osc(slice->cls_obj);
1261 struct osc_thread_info *info = osc_env_info(env);
1262 struct ldlm_res_id *resname = &info->oti_resname;
1263 ldlm_policy_data_t *policy = &info->oti_policy;
1264 struct ldlm_enqueue_info *einfo = &ols->ols_einfo;
1268 LASSERT(cl_lock_is_mutexed(lock));
1269 LASSERT(lock->cll_state == CLS_QUEUING);
1270 LASSERT(ols->ols_state == OLS_NEW);
1272 osc_lock_build_res(env, obj, resname);
1273 osc_lock_build_policy(env, lock, policy);
1274 ols->ols_flags = osc_enq2ldlm_flags(enqflags);
1275 if (osc_deadlock_is_possible(env, lock))
1276 ols->ols_flags |= LDLM_FL_BLOCK_GRANTED;
1277 if (ols->ols_flags & LDLM_FL_HAS_INTENT)
1278 ols->ols_glimpse = 1;
1280 result = osc_lock_enqueue_wait(env, ols);
1282 if (!(enqflags & CEF_MUST))
1283 /* try to convert this lock to a lockless lock */
1284 osc_lock_to_lockless(env, ols, (enqflags & CEF_NEVER));
1285 if (!osc_lock_is_lockless(ols)) {
1286 if (ols->ols_locklessable)
1287 ols->ols_flags |= LDLM_FL_DENY_ON_CONTENTION;
1289 /* a reference for lock, passed as an upcall cookie */
1291 lu_ref_add(&lock->cll_reference, "upcall", lock);
1292 ols->ols_state = OLS_ENQUEUED;
1295 * XXX: this is possible blocking point as
1296 * ldlm_lock_match(LDLM_FL_LVB_READY) waits for
1299 result = osc_enqueue_base(osc_export(obj), resname,
1300 &ols->ols_flags, policy,
1302 obj->oo_oinfo->loi_kms_valid,
1304 ols, einfo, &ols->ols_handle,
1307 lu_ref_del(&lock->cll_reference,
1309 cl_lock_put(env, lock);
1312 ols->ols_state = OLS_GRANTED;
1315 LASSERT(ergo(ols->ols_glimpse, !osc_lock_is_lockless(ols)));
1319 static int osc_lock_wait(const struct lu_env *env,
1320 const struct cl_lock_slice *slice)
1322 struct osc_lock *olck = cl2osc_lock(slice);
1323 struct cl_lock *lock = olck->ols_cl.cls_lock;
1325 LINVRNT(osc_lock_invariant(olck));
1326 if (olck->ols_glimpse && olck->ols_state >= OLS_UPCALL_RECEIVED)
1329 LASSERT(equi(olck->ols_state >= OLS_UPCALL_RECEIVED &&
1330 lock->cll_error == 0, olck->ols_lock != NULL));
1332 return lock->cll_error ?: olck->ols_state >= OLS_GRANTED ? 0 : CLO_WAIT;
1336 * An implementation of cl_lock_operations::clo_use() method that pins cached
1339 static int osc_lock_use(const struct lu_env *env,
1340 const struct cl_lock_slice *slice)
1342 struct osc_lock *olck = cl2osc_lock(slice);
1345 LASSERT(!olck->ols_hold);
1347 * Atomically check for LDLM_FL_CBPENDING and addref a lock if this
1348 * flag is not set. This protects us from a concurrent blocking ast.
1350 rc = ldlm_lock_addref_try(&olck->ols_handle, olck->ols_einfo.ei_mode);
1352 olck->ols_hold = olck->ols_has_ref = 1;
1353 olck->ols_state = OLS_GRANTED;
1355 struct cl_lock *lock;
1358 * Lock is being cancelled somewhere within
1359 * ldlm_handle_bl_callback(): LDLM_FL_CBPENDING is already
1360 * set, but osc_ldlm_blocking_ast() hasn't yet acquired
1363 lock = slice->cls_lock;
1364 LASSERT(lock->cll_state == CLS_INTRANSIT);
1365 LASSERT(lock->cll_users > 0);
1366 /* set a flag for osc_dlm_blocking_ast0() to signal the
1368 olck->ols_ast_wait = 1;
1374 static int osc_lock_flush(struct osc_lock *ols, int discard)
1376 struct cl_lock *lock = ols->ols_cl.cls_lock;
1377 struct cl_env_nest nest;
1381 env = cl_env_nested_get(&nest);
1383 result = cl_lock_page_out(env, lock, discard);
1384 cl_env_nested_put(&nest, env);
1386 result = PTR_ERR(env);
1389 LINVRNT(!osc_lock_has_pages(ols));
1395 * Implements cl_lock_operations::clo_cancel() method for osc layer. This is
1396 * called (as part of cl_lock_cancel()) when lock is canceled either voluntary
1397 * (LRU pressure, early cancellation, umount, etc.) or due to the conflict
1398 * with some other lock some where in the cluster. This function does the
1401 * - invalidates all pages protected by this lock (after sending dirty
1402 * ones to the server, as necessary);
1404 * - decref's underlying ldlm lock;
1406 * - cancels ldlm lock (ldlm_cli_cancel()).
1408 static void osc_lock_cancel(const struct lu_env *env,
1409 const struct cl_lock_slice *slice)
1411 struct cl_lock *lock = slice->cls_lock;
1412 struct osc_lock *olck = cl2osc_lock(slice);
1413 struct ldlm_lock *dlmlock = olck->ols_lock;
1417 LASSERT(cl_lock_is_mutexed(lock));
1418 LINVRNT(osc_lock_invariant(olck));
1420 if (dlmlock != NULL) {
1423 discard = dlmlock->l_flags & LDLM_FL_DISCARD_DATA;
1424 result = osc_lock_flush(olck, discard);
1426 osc_lock_unuse(env, slice);
1428 lock_res_and_lock(dlmlock);
1429 /* Now that we're the only user of dlm read/write reference,
1430 * mostly the ->l_readers + ->l_writers should be zero.
1431 * However, there is a corner case.
1432 * See bug 18829 for details.*/
1433 do_cancel = (dlmlock->l_readers == 0 &&
1434 dlmlock->l_writers == 0);
1435 dlmlock->l_flags |= LDLM_FL_CBPENDING;
1436 unlock_res_and_lock(dlmlock);
1438 result = ldlm_cli_cancel(&olck->ols_handle);
1440 CL_LOCK_DEBUG(D_ERROR, env, lock,
1441 "lock %p cancel failure with error(%d)\n",
1444 olck->ols_state = OLS_CANCELLED;
1445 osc_lock_detach(env, olck);
1448 void cl_lock_page_list_fixup(const struct lu_env *env,
1449 struct cl_io *io, struct cl_lock *lock,
1450 struct cl_page_list *queue);
1452 #ifdef INVARIANT_CHECK
1454 * Returns true iff there are pages under \a olck not protected by other
1457 static int osc_lock_has_pages(struct osc_lock *olck)
1459 struct cl_lock *lock;
1460 struct cl_lock_descr *descr;
1461 struct cl_object *obj;
1462 struct osc_object *oob;
1463 struct cl_page_list *plist;
1464 struct cl_page *page;
1465 struct cl_env_nest nest;
1470 env = cl_env_nested_get(&nest);
1472 obj = olck->ols_cl.cls_obj;
1474 io = &oob->oo_debug_io;
1475 lock = olck->ols_cl.cls_lock;
1476 descr = &lock->cll_descr;
1477 plist = &osc_env_info(env)->oti_plist;
1478 cl_page_list_init(plist);
1480 mutex_lock(&oob->oo_debug_mutex);
1482 io->ci_obj = cl_object_top(obj);
1483 cl_io_init(env, io, CIT_MISC, io->ci_obj);
1484 cl_page_gang_lookup(env, obj, io,
1485 descr->cld_start, descr->cld_end, plist, 0);
1486 cl_lock_page_list_fixup(env, io, lock, plist);
1487 if (plist->pl_nr > 0) {
1488 CL_LOCK_DEBUG(D_ERROR, env, lock, "still has pages\n");
1489 cl_page_list_for_each(page, plist)
1490 CL_PAGE_DEBUG(D_ERROR, env, page, "\n");
1492 result = plist->pl_nr > 0;
1493 cl_page_list_disown(env, io, plist);
1494 cl_page_list_fini(env, plist);
1495 cl_io_fini(env, io);
1496 mutex_unlock(&oob->oo_debug_mutex);
1497 cl_env_nested_put(&nest, env);
1503 static int osc_lock_has_pages(struct osc_lock *olck)
1507 #endif /* INVARIANT_CHECK */
1509 static void osc_lock_delete(const struct lu_env *env,
1510 const struct cl_lock_slice *slice)
1512 struct osc_lock *olck;
1514 olck = cl2osc_lock(slice);
1515 if (olck->ols_glimpse) {
1516 LASSERT(!olck->ols_hold);
1517 LASSERT(!olck->ols_lock);
1521 LINVRNT(osc_lock_invariant(olck));
1522 LINVRNT(!osc_lock_has_pages(olck));
1525 osc_lock_unuse(env, slice);
1526 osc_lock_detach(env, olck);
1530 * Implements cl_lock_operations::clo_state() method for osc layer.
1532 * Maintains osc_lock::ols_owner field.
1534 * This assumes that lock always enters CLS_HELD (from some other state) in
1535 * the same IO context as one that requested the lock. This should not be a
1536 * problem, because context is by definition shared by all activity pertaining
1537 * to the same high-level IO.
1539 static void osc_lock_state(const struct lu_env *env,
1540 const struct cl_lock_slice *slice,
1541 enum cl_lock_state state)
1543 struct osc_lock *lock = cl2osc_lock(slice);
1544 struct osc_io *oio = osc_env_io(env);
1547 * XXX multiple io contexts can use the lock at the same time.
1549 LINVRNT(osc_lock_invariant(lock));
1550 if (state == CLS_HELD && slice->cls_lock->cll_state != CLS_HELD) {
1551 LASSERT(lock->ols_owner == NULL);
1552 lock->ols_owner = oio;
1553 } else if (state != CLS_HELD)
1554 lock->ols_owner = NULL;
1557 static int osc_lock_print(const struct lu_env *env, void *cookie,
1558 lu_printer_t p, const struct cl_lock_slice *slice)
1560 struct osc_lock *lock = cl2osc_lock(slice);
1563 * XXX print ldlm lock and einfo properly.
1565 (*p)(env, cookie, "%p %08x "LPU64" %d %p ",
1566 lock->ols_lock, lock->ols_flags, lock->ols_handle.cookie,
1567 lock->ols_state, lock->ols_owner);
1568 osc_lvb_print(env, cookie, p, &lock->ols_lvb);
1572 static int osc_lock_fits_into(const struct lu_env *env,
1573 const struct cl_lock_slice *slice,
1574 const struct cl_lock_descr *need,
1575 const struct cl_io *io)
1577 struct osc_lock *ols = cl2osc_lock(slice);
1579 if (need->cld_enq_flags & CEF_NEVER)
1582 if (need->cld_mode == CLM_PHANTOM) {
1584 * Note: the QUEUED lock can't be matched here, otherwise
1585 * it might cause the deadlocks.
1587 * P1: enqueued read lock, create sublock1
1588 * P2: enqueued write lock, create sublock2(conflicted
1590 * P1: Grant read lock.
1591 * P1: enqueued glimpse lock(with holding sublock1_read),
1592 * matched with sublock2, waiting sublock2 to be granted.
1593 * But sublock2 can not be granted, because P1
1594 * will not release sublock1. Bang!
1596 if (ols->ols_state < OLS_GRANTED ||
1597 ols->ols_state > OLS_RELEASED)
1599 } else if (need->cld_enq_flags & CEF_MUST) {
1601 * If the lock hasn't ever enqueued, it can't be matched
1602 * because enqueue process brings in many information
1603 * which can be used to determine things such as lockless,
1606 if (ols->ols_state < OLS_GRANTED ||
1607 ols->ols_state > OLS_RELEASED)
1609 if (ols->ols_state < OLS_UPCALL_RECEIVED &&
1610 ols->ols_locklessable)
1616 static const struct cl_lock_operations osc_lock_ops = {
1617 .clo_fini = osc_lock_fini,
1618 .clo_enqueue = osc_lock_enqueue,
1619 .clo_wait = osc_lock_wait,
1620 .clo_unuse = osc_lock_unuse,
1621 .clo_use = osc_lock_use,
1622 .clo_delete = osc_lock_delete,
1623 .clo_state = osc_lock_state,
1624 .clo_cancel = osc_lock_cancel,
1625 .clo_weigh = osc_lock_weigh,
1626 .clo_print = osc_lock_print,
1627 .clo_fits_into = osc_lock_fits_into,
1630 static int osc_lock_lockless_enqueue(const struct lu_env *env,
1631 const struct cl_lock_slice *slice,
1632 struct cl_io *unused, __u32 enqflags)
1638 static int osc_lock_lockless_unuse(const struct lu_env *env,
1639 const struct cl_lock_slice *slice)
1641 struct osc_lock *ols = cl2osc_lock(slice);
1642 struct cl_lock *lock = slice->cls_lock;
1644 LASSERT(ols->ols_state == OLS_GRANTED);
1645 LINVRNT(osc_lock_invariant(ols));
1647 cl_lock_cancel(env, lock);
1648 cl_lock_delete(env, lock);
1652 static void osc_lock_lockless_cancel(const struct lu_env *env,
1653 const struct cl_lock_slice *slice)
1655 struct osc_lock *ols = cl2osc_lock(slice);
1658 result = osc_lock_flush(ols, 0);
1660 CERROR("Pages for lockless lock %p were not purged(%d)\n",
1662 ols->ols_state = OLS_CANCELLED;
1665 static int osc_lock_lockless_wait(const struct lu_env *env,
1666 const struct cl_lock_slice *slice)
1668 struct osc_lock *olck = cl2osc_lock(slice);
1669 struct cl_lock *lock = olck->ols_cl.cls_lock;
1671 LINVRNT(osc_lock_invariant(olck));
1672 LASSERT(olck->ols_state >= OLS_UPCALL_RECEIVED);
1674 return lock->cll_error;
1677 static void osc_lock_lockless_state(const struct lu_env *env,
1678 const struct cl_lock_slice *slice,
1679 enum cl_lock_state state)
1681 struct osc_lock *lock = cl2osc_lock(slice);
1682 struct osc_io *oio = osc_env_io(env);
1684 LINVRNT(osc_lock_invariant(lock));
1685 if (state == CLS_HELD) {
1686 LASSERT(lock->ols_owner == NULL);
1687 lock->ols_owner = oio;
1689 /* set the io to be lockless if this lock is for io's
1691 if (cl_object_same(oio->oi_cl.cis_obj, slice->cls_obj))
1692 oio->oi_lockless = 1;
1694 lock->ols_owner = NULL;
1697 static int osc_lock_lockless_fits_into(const struct lu_env *env,
1698 const struct cl_lock_slice *slice,
1699 const struct cl_lock_descr *need,
1700 const struct cl_io *io)
1705 static const struct cl_lock_operations osc_lock_lockless_ops = {
1706 .clo_fini = osc_lock_fini,
1707 .clo_enqueue = osc_lock_lockless_enqueue,
1708 .clo_wait = osc_lock_lockless_wait,
1709 .clo_unuse = osc_lock_lockless_unuse,
1710 .clo_state = osc_lock_lockless_state,
1711 .clo_fits_into = osc_lock_lockless_fits_into,
1712 .clo_cancel = osc_lock_lockless_cancel,
1713 .clo_print = osc_lock_print
1716 int osc_lock_init(const struct lu_env *env,
1717 struct cl_object *obj, struct cl_lock *lock,
1718 const struct cl_io *unused)
1720 struct osc_lock *clk;
1723 OBD_SLAB_ALLOC_PTR_GFP(clk, osc_lock_kmem, CFS_ALLOC_IO);
1725 osc_lock_build_einfo(env, lock, clk, &clk->ols_einfo);
1726 clk->ols_state = OLS_NEW;
1727 cl_lock_slice_add(lock, &clk->ols_cl, obj, &osc_lock_ops);