4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Implementation of cl_lock for OSC layer.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
38 #define DEBUG_SUBSYSTEM S_OSC
40 #include <libcfs/libcfs.h>
41 /* fid_build_reg_res_name() */
42 #include <lustre_fid.h>
43 #include <lustre_osc.h>
45 #include "osc_internal.h"
51 /*****************************************************************************
57 static const struct cl_lock_operations osc_lock_ops;
58 static const struct cl_lock_operations osc_lock_lockless_ops;
59 static void osc_lock_to_lockless(const struct lu_env *env,
60 struct osc_lock *ols, int force);
62 int osc_lock_is_lockless(const struct osc_lock *olck)
64 return (olck->ols_cl.cls_ops == &osc_lock_lockless_ops);
68 * Returns a weak pointer to the ldlm lock identified by a handle. Returned
69 * pointer cannot be dereferenced, as lock is not protected from concurrent
70 * reclaim. This function is a helper for osc_lock_invariant().
72 static struct ldlm_lock *osc_handle_ptr(struct lustre_handle *handle)
74 struct ldlm_lock *lock;
76 lock = ldlm_handle2lock(handle);
83 * Invariant that has to be true all of the time.
85 static int osc_lock_invariant(struct osc_lock *ols)
87 struct ldlm_lock *lock = osc_handle_ptr(&ols->ols_handle);
88 struct ldlm_lock *olock = ols->ols_dlmlock;
89 int handle_used = lustre_handle_is_used(&ols->ols_handle);
91 if (ergo(osc_lock_is_lockless(ols),
92 ols->ols_locklessable && ols->ols_dlmlock == NULL))
96 * If all the following "ergo"s are true, return 1, otherwise 0
98 if (! ergo(olock != NULL, handle_used))
101 if (! ergo(olock != NULL,
102 olock->l_handle.h_cookie == ols->ols_handle.cookie))
105 if (! ergo(handle_used,
106 ergo(lock != NULL && olock != NULL, lock == olock) &&
107 ergo(lock == NULL, olock == NULL)))
110 * Check that ->ols_handle and ->ols_dlmlock are consistent, but
111 * take into account that they are set at the different time.
113 if (! ergo(ols->ols_state == OLS_CANCELLED,
114 olock == NULL && !handle_used))
117 * DLM lock is destroyed only after we have seen cancellation
120 if (! ergo(olock != NULL && ols->ols_state < OLS_CANCELLED,
121 !ldlm_is_destroyed(olock)))
124 if (! ergo(ols->ols_state == OLS_GRANTED,
126 olock->l_req_mode == olock->l_granted_mode &&
132 /*****************************************************************************
138 static void osc_lock_fini(const struct lu_env *env,
139 struct cl_lock_slice *slice)
141 struct osc_lock *ols = cl2osc_lock(slice);
143 LINVRNT(osc_lock_invariant(ols));
144 LASSERT(ols->ols_dlmlock == NULL);
146 OBD_SLAB_FREE_PTR(ols, osc_lock_kmem);
149 static void osc_lock_build_policy(const struct lu_env *env,
150 const struct cl_lock *lock,
151 union ldlm_policy_data *policy)
153 const struct cl_lock_descr *d = &lock->cll_descr;
155 osc_index2policy(policy, d->cld_obj, d->cld_start, d->cld_end);
156 policy->l_extent.gid = d->cld_gid;
159 static __u64 osc_enq2ldlm_flags(__u32 enqflags)
163 CDEBUG(D_DLMTRACE, "flags: %x\n", enqflags);
165 LASSERT((enqflags & ~CEF_MASK) == 0);
167 if (enqflags & CEF_NONBLOCK)
168 result |= LDLM_FL_BLOCK_NOWAIT;
169 if (enqflags & CEF_GLIMPSE)
170 result |= LDLM_FL_HAS_INTENT;
171 if (enqflags & CEF_DISCARD_DATA)
172 result |= LDLM_FL_AST_DISCARD_DATA;
173 if (enqflags & CEF_PEEK)
174 result |= LDLM_FL_TEST_LOCK;
175 if (enqflags & CEF_LOCK_MATCH)
176 result |= LDLM_FL_MATCH_LOCK;
177 if (enqflags & CEF_LOCK_NO_EXPAND)
178 result |= LDLM_FL_NO_EXPANSION;
179 if (enqflags & CEF_SPECULATIVE)
180 result |= LDLM_FL_SPECULATIVE;
185 * Updates object attributes from a lock value block (lvb) received together
186 * with the DLM lock reply from the server. Copy of osc_update_enqueue()
189 * This can be optimized to not update attributes when lock is a result of a
192 * Called under lock and resource spin-locks.
194 static void osc_lock_lvb_update(const struct lu_env *env,
195 struct osc_object *osc,
196 struct ldlm_lock *dlmlock,
199 struct cl_object *obj = osc2cl(osc);
200 struct lov_oinfo *oinfo = osc->oo_oinfo;
201 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
206 valid = CAT_BLOCKS | CAT_ATIME | CAT_CTIME | CAT_MTIME | CAT_SIZE;
208 LASSERT(dlmlock != NULL);
209 lvb = dlmlock->l_lvb_data;
211 cl_lvb2attr(attr, lvb);
213 cl_object_attr_lock(obj);
214 if (dlmlock != NULL) {
217 check_res_locked(dlmlock->l_resource);
219 LASSERT(lvb == dlmlock->l_lvb_data);
220 size = lvb->lvb_size;
222 /* Extend KMS up to the end of this lock and no further
223 * A lock on [x,y] means a KMS of up to y + 1 bytes! */
224 if (size > dlmlock->l_policy_data.l_extent.end)
225 size = dlmlock->l_policy_data.l_extent.end + 1;
226 if (size >= oinfo->loi_kms) {
227 LDLM_DEBUG(dlmlock, "lock acquired, setting rss=%llu"
228 ", kms=%llu", lvb->lvb_size, size);
230 attr->cat_kms = size;
232 LDLM_DEBUG(dlmlock, "lock acquired, setting rss="
233 "%llu; leaving kms=%llu, end=%llu",
234 lvb->lvb_size, oinfo->loi_kms,
235 dlmlock->l_policy_data.l_extent.end);
237 ldlm_lock_allow_match_locked(dlmlock);
240 cl_object_attr_update(env, obj, attr, valid);
241 cl_object_attr_unlock(obj);
246 static void osc_lock_granted(const struct lu_env *env, struct osc_lock *oscl,
247 struct lustre_handle *lockh, bool lvb_update)
249 struct ldlm_lock *dlmlock;
251 dlmlock = ldlm_handle2lock_long(lockh, 0);
252 LASSERT(dlmlock != NULL);
254 /* lock reference taken by ldlm_handle2lock_long() is
255 * owned by osc_lock and released in osc_lock_detach()
257 lu_ref_add(&dlmlock->l_reference, "osc_lock", oscl);
258 oscl->ols_has_ref = 1;
260 LASSERT(oscl->ols_dlmlock == NULL);
261 oscl->ols_dlmlock = dlmlock;
263 /* This may be a matched lock for glimpse request, do not hold
264 * lock reference in that case. */
265 if (!oscl->ols_glimpse) {
266 /* hold a refc for non glimpse lock which will
267 * be released in osc_lock_cancel() */
268 lustre_handle_copy(&oscl->ols_handle, lockh);
269 ldlm_lock_addref(lockh, oscl->ols_einfo.ei_mode);
273 /* Lock must have been granted. */
274 lock_res_and_lock(dlmlock);
275 if (dlmlock->l_granted_mode == dlmlock->l_req_mode) {
276 struct ldlm_extent *ext = &dlmlock->l_policy_data.l_extent;
277 struct cl_lock_descr *descr = &oscl->ols_cl.cls_lock->cll_descr;
279 /* extend the lock extent, otherwise it will have problem when
280 * we decide whether to grant a lockless lock. */
281 descr->cld_mode = osc_ldlm2cl_lock(dlmlock->l_granted_mode);
282 descr->cld_start = cl_index(descr->cld_obj, ext->start);
283 descr->cld_end = cl_index(descr->cld_obj, ext->end);
284 descr->cld_gid = ext->gid;
286 /* no lvb update for matched lock */
288 LASSERT(oscl->ols_flags & LDLM_FL_LVB_READY);
289 osc_lock_lvb_update(env, cl2osc(oscl->ols_cl.cls_obj),
292 LINVRNT(osc_lock_invariant(oscl));
294 unlock_res_and_lock(dlmlock);
296 LASSERT(oscl->ols_state != OLS_GRANTED);
297 oscl->ols_state = OLS_GRANTED;
301 * Lock upcall function that is executed either when a reply to ENQUEUE rpc is
302 * received from a server, or after osc_enqueue_base() matched a local DLM
305 static int osc_lock_upcall(void *cookie, struct lustre_handle *lockh,
308 struct osc_lock *oscl = cookie;
309 struct cl_lock_slice *slice = &oscl->ols_cl;
315 env = cl_env_percpu_get();
316 /* should never happen, similar to osc_ldlm_blocking_ast(). */
317 LASSERT(!IS_ERR(env));
319 rc = ldlm_error2errno(errcode);
320 if (oscl->ols_state == OLS_ENQUEUED) {
321 oscl->ols_state = OLS_UPCALL_RECEIVED;
322 } else if (oscl->ols_state == OLS_CANCELLED) {
325 CERROR("Impossible state: %d\n", oscl->ols_state);
330 osc_lock_granted(env, oscl, lockh, errcode == ELDLM_OK);
332 /* Error handling, some errors are tolerable. */
333 if (oscl->ols_locklessable && rc == -EUSERS) {
334 /* This is a tolerable error, turn this lock into
337 osc_object_set_contended(cl2osc(slice->cls_obj));
338 LASSERT(slice->cls_ops == &osc_lock_ops);
340 /* Change this lock to ldlmlock-less lock. */
341 osc_lock_to_lockless(env, oscl, 1);
342 oscl->ols_state = OLS_GRANTED;
344 } else if (oscl->ols_glimpse && rc == -ENAVAIL) {
345 LASSERT(oscl->ols_flags & LDLM_FL_LVB_READY);
346 osc_lock_lvb_update(env, cl2osc(slice->cls_obj),
347 NULL, &oscl->ols_lvb);
348 /* Hide the error. */
352 if (oscl->ols_owner != NULL)
353 cl_sync_io_note(env, oscl->ols_owner, rc);
354 cl_env_percpu_put(env);
359 static int osc_lock_upcall_speculative(void *cookie,
360 struct lustre_handle *lockh,
363 struct osc_object *osc = cookie;
364 struct ldlm_lock *dlmlock;
369 env = cl_env_get(&refcheck);
370 LASSERT(!IS_ERR(env));
372 if (errcode == ELDLM_LOCK_MATCHED)
373 GOTO(out, errcode = ELDLM_OK);
375 if (errcode != ELDLM_OK)
378 dlmlock = ldlm_handle2lock(lockh);
379 LASSERT(dlmlock != NULL);
381 lock_res_and_lock(dlmlock);
382 LASSERT(dlmlock->l_granted_mode == dlmlock->l_req_mode);
384 /* there is no osc_lock associated with speculative locks */
385 osc_lock_lvb_update(env, osc, dlmlock, NULL);
387 unlock_res_and_lock(dlmlock);
388 LDLM_LOCK_PUT(dlmlock);
391 cl_object_put(env, osc2cl(osc));
392 cl_env_put(env, &refcheck);
393 RETURN(ldlm_error2errno(errcode));
396 static int osc_lock_flush(struct osc_object *obj, pgoff_t start, pgoff_t end,
397 enum cl_lock_mode mode, bool discard)
406 env = cl_env_get(&refcheck);
408 RETURN(PTR_ERR(env));
410 if (mode == CLM_WRITE) {
411 rc = osc_cache_writeback_range(env, obj, start, end, 1,
413 CDEBUG(D_CACHE, "object %p: [%lu -> %lu] %d pages were %s.\n",
415 discard ? "discarded" : "written back");
420 rc2 = osc_lock_discard_pages(env, obj, start, end, discard);
421 if (rc == 0 && rc2 < 0)
424 cl_env_put(env, &refcheck);
429 * Helper for osc_dlm_blocking_ast() handling discrepancies between cl_lock
430 * and ldlm_lock caches.
432 static int osc_dlm_blocking_ast0(const struct lu_env *env,
433 struct ldlm_lock *dlmlock,
434 void *data, int flag)
436 struct cl_object *obj = NULL;
439 enum cl_lock_mode mode = CLM_READ;
442 LASSERT(flag == LDLM_CB_CANCELING);
444 lock_res_and_lock(dlmlock);
445 if (dlmlock->l_granted_mode != dlmlock->l_req_mode) {
446 dlmlock->l_ast_data = NULL;
447 unlock_res_and_lock(dlmlock);
451 discard = ldlm_is_discard_data(dlmlock);
452 if (dlmlock->l_granted_mode & (LCK_PW | LCK_GROUP))
455 if (dlmlock->l_ast_data != NULL) {
456 obj = osc2cl(dlmlock->l_ast_data);
457 dlmlock->l_ast_data = NULL;
462 unlock_res_and_lock(dlmlock);
464 /* if l_ast_data is NULL, the dlmlock was enqueued by AGL or
465 * the object has been destroyed. */
467 struct ldlm_extent *extent = &dlmlock->l_policy_data.l_extent;
468 struct cl_attr *attr = &osc_env_info(env)->oti_attr;
471 /* Destroy pages covered by the extent of the DLM lock */
472 result = osc_lock_flush(cl2osc(obj),
473 cl_index(obj, extent->start),
474 cl_index(obj, extent->end),
477 /* losing a lock, update kms */
478 lock_res_and_lock(dlmlock);
479 cl_object_attr_lock(obj);
480 /* Must get the value under the lock to avoid race. */
481 old_kms = cl2osc(obj)->oo_oinfo->loi_kms;
482 /* Update the kms. Need to loop all granted locks.
483 * Not a problem for the client */
484 attr->cat_kms = ldlm_extent_shift_kms(dlmlock, old_kms);
486 cl_object_attr_update(env, obj, attr, CAT_KMS);
487 cl_object_attr_unlock(obj);
488 unlock_res_and_lock(dlmlock);
490 cl_object_put(env, obj);
496 * Blocking ast invoked by ldlm when dlm lock is either blocking progress of
497 * some other lock, or is canceled. This function is installed as a
498 * ldlm_lock::l_blocking_ast() for client extent locks.
500 * Control flow is tricky, because ldlm uses the same call-back
501 * (ldlm_lock::l_blocking_ast()) for both blocking and cancellation ast's.
503 * \param dlmlock lock for which ast occurred.
505 * \param new description of a conflicting lock in case of blocking ast.
507 * \param data value of dlmlock->l_ast_data
509 * \param flag LDLM_CB_BLOCKING or LDLM_CB_CANCELING. Used to distinguish
510 * cancellation and blocking ast's.
512 * Possible use cases:
514 * - ldlm calls dlmlock->l_blocking_ast(..., LDLM_CB_CANCELING) to cancel
515 * lock due to lock lru pressure, or explicit user request to purge
518 * - ldlm calls dlmlock->l_blocking_ast(..., LDLM_CB_BLOCKING) to notify
519 * us that dlmlock conflicts with another lock that some client is
520 * enqueuing. Lock is canceled.
522 * - cl_lock_cancel() is called. osc_lock_cancel() calls
523 * ldlm_cli_cancel() that calls
525 * dlmlock->l_blocking_ast(..., LDLM_CB_CANCELING)
527 * recursively entering osc_ldlm_blocking_ast().
529 * - client cancels lock voluntary (e.g., as a part of early cancellation):
532 * osc_lock_cancel()->
533 * ldlm_cli_cancel()->
534 * dlmlock->l_blocking_ast(..., LDLM_CB_CANCELING)
537 static int osc_ldlm_blocking_ast(struct ldlm_lock *dlmlock,
538 struct ldlm_lock_desc *new, void *data,
545 case LDLM_CB_BLOCKING: {
546 struct lustre_handle lockh;
548 ldlm_lock2handle(dlmlock, &lockh);
549 result = ldlm_cli_cancel(&lockh, LCF_ASYNC);
550 if (result == -ENODATA)
554 case LDLM_CB_CANCELING: {
559 * This can be called in the context of outer IO, e.g.,
561 * osc_enqueue_base()->...
562 * ->ldlm_prep_elc_req()->...
563 * ->ldlm_cancel_callback()->...
564 * ->osc_ldlm_blocking_ast()
566 * new environment has to be created to not corrupt outer
569 env = cl_env_get(&refcheck);
571 result = PTR_ERR(env);
575 result = osc_dlm_blocking_ast0(env, dlmlock, data, flag);
576 cl_env_put(env, &refcheck);
585 static int osc_ldlm_glimpse_ast(struct ldlm_lock *dlmlock, void *data)
587 struct ptlrpc_request *req = data;
590 struct req_capsule *cap;
591 struct cl_object *obj = NULL;
597 LASSERT(lustre_msg_get_opc(req->rq_reqmsg) == LDLM_GL_CALLBACK);
599 env = cl_env_get(&refcheck);
601 GOTO(out, result = PTR_ERR(env));
604 lock_res_and_lock(dlmlock);
605 if (dlmlock->l_ast_data != NULL) {
606 obj = osc2cl(dlmlock->l_ast_data);
609 unlock_res_and_lock(dlmlock);
612 /* Do not grab the mutex of cl_lock for glimpse.
613 * See LU-1274 for details.
614 * BTW, it's okay for cl_lock to be cancelled during
615 * this period because server can handle this race.
616 * See ldlm_server_glimpse_ast() for details.
617 * cl_lock_mutex_get(env, lock); */
619 req_capsule_extend(cap, &RQF_LDLM_GL_CALLBACK);
620 req_capsule_set_size(cap, &RMF_DLM_LVB, RCL_SERVER,
622 result = req_capsule_server_pack(cap);
624 lvb = req_capsule_server_get(cap, &RMF_DLM_LVB);
625 result = cl_object_glimpse(env, obj, lvb);
627 if (!exp_connect_lvb_type(req->rq_export))
628 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB,
629 sizeof(struct ost_lvb_v1), RCL_SERVER);
630 cl_object_put(env, obj);
633 * These errors are normal races, so we don't want to
634 * fill the console with messages by calling
637 lustre_pack_reply(req, 1, NULL, NULL);
638 result = -ELDLM_NO_LOCK_DATA;
640 cl_env_put(env, &refcheck);
644 req->rq_status = result;
648 static int weigh_cb(const struct lu_env *env, struct cl_io *io,
649 struct osc_page *ops, void *cbdata)
651 struct cl_page *page = ops->ops_cl.cpl_page;
653 if (cl_page_is_vmlocked(env, page)
654 || PageDirty(page->cp_vmpage) || PageWriteback(page->cp_vmpage)
656 return CLP_GANG_ABORT;
658 *(pgoff_t *)cbdata = osc_index(ops) + 1;
659 return CLP_GANG_OKAY;
662 static unsigned long osc_lock_weight(const struct lu_env *env,
663 struct osc_object *oscobj,
664 struct ldlm_extent *extent)
666 struct cl_io *io = &osc_env_info(env)->oti_io;
667 struct cl_object *obj = cl_object_top(&oscobj->oo_cl);
673 io->ci_ignore_layout = 1;
674 result = cl_io_init(env, io, CIT_MISC, io->ci_obj);
678 page_index = cl_index(obj, extent->start);
680 result = osc_page_gang_lookup(env, io, oscobj,
682 cl_index(obj, extent->end),
683 weigh_cb, (void *)&page_index);
684 if (result == CLP_GANG_ABORT)
686 if (result == CLP_GANG_RESCHED)
688 } while (result != CLP_GANG_OKAY);
691 return result == CLP_GANG_ABORT ? 1 : 0;
695 * Get the weight of dlm lock for early cancellation.
697 unsigned long osc_ldlm_weigh_ast(struct ldlm_lock *dlmlock)
700 struct osc_object *obj;
701 struct osc_lock *oscl;
702 unsigned long weight;
709 * osc_ldlm_weigh_ast has a complex context since it might be called
710 * because of lock canceling, or from user's input. We have to make
711 * a new environment for it. Probably it is implementation safe to use
712 * the upper context because cl_lock_put don't modify environment
713 * variables. But just in case ..
715 env = cl_env_get(&refcheck);
717 /* Mostly because lack of memory, do not eliminate this lock */
720 LASSERT(dlmlock->l_resource->lr_type == LDLM_EXTENT);
721 lock_res_and_lock(dlmlock);
722 obj = dlmlock->l_ast_data;
724 cl_object_get(osc2cl(obj));
725 unlock_res_and_lock(dlmlock);
728 GOTO(out, weight = 1);
730 spin_lock(&obj->oo_ol_spin);
731 list_for_each_entry(oscl, &obj->oo_ol_list, ols_nextlock_oscobj) {
732 if (oscl->ols_dlmlock != NULL && oscl->ols_dlmlock != dlmlock)
736 spin_unlock(&obj->oo_ol_spin);
739 * If the lock is being used by an IO, definitely not cancel it.
741 GOTO(out, weight = 1);
744 weight = osc_lock_weight(env, obj, &dlmlock->l_policy_data.l_extent);
749 cl_object_put(env, osc2cl(obj));
751 cl_env_put(env, &refcheck);
755 static void osc_lock_build_einfo(const struct lu_env *env,
756 const struct cl_lock *lock,
757 struct osc_object *osc,
758 struct ldlm_enqueue_info *einfo)
760 einfo->ei_type = LDLM_EXTENT;
761 einfo->ei_mode = osc_cl_lock2ldlm(lock->cll_descr.cld_mode);
762 einfo->ei_cb_bl = osc_ldlm_blocking_ast;
763 einfo->ei_cb_cp = ldlm_completion_ast;
764 einfo->ei_cb_gl = osc_ldlm_glimpse_ast;
765 einfo->ei_cbdata = osc; /* value to be put into ->l_ast_data */
769 * Determine if the lock should be converted into a lockless lock.
772 * - if the lock has an explicite requirment for a non-lockless lock;
773 * - if the io lock request type ci_lockreq;
774 * - send the enqueue rpc to ost to make the further decision;
775 * - special treat to truncate lockless lock
777 * Additional policy can be implemented here, e.g., never do lockless-io
780 static void osc_lock_to_lockless(const struct lu_env *env,
781 struct osc_lock *ols, int force)
783 struct cl_lock_slice *slice = &ols->ols_cl;
785 LASSERT(ols->ols_state == OLS_NEW ||
786 ols->ols_state == OLS_UPCALL_RECEIVED);
789 ols->ols_locklessable = 1;
790 slice->cls_ops = &osc_lock_lockless_ops;
792 struct osc_io *oio = osc_env_io(env);
793 struct cl_io *io = oio->oi_cl.cis_io;
794 struct cl_object *obj = slice->cls_obj;
795 struct osc_object *oob = cl2osc(obj);
796 const struct osc_device *osd = lu2osc_dev(obj->co_lu.lo_dev);
797 struct obd_connect_data *ocd;
799 LASSERT(io->ci_lockreq == CILR_MANDATORY ||
800 io->ci_lockreq == CILR_MAYBE ||
801 io->ci_lockreq == CILR_NEVER);
803 ocd = &class_exp2cliimp(osc_export(oob))->imp_connect_data;
804 ols->ols_locklessable = (io->ci_type != CIT_SETATTR) &&
805 (io->ci_lockreq == CILR_MAYBE) &&
806 (ocd->ocd_connect_flags & OBD_CONNECT_SRVLOCK);
807 if (io->ci_lockreq == CILR_NEVER ||
809 (ols->ols_locklessable && osc_object_is_contended(oob)) ||
810 /* lockless truncate */
811 (cl_io_is_trunc(io) &&
812 (ocd->ocd_connect_flags & OBD_CONNECT_TRUNCLOCK) &&
813 osd->od_lockless_truncate)) {
814 ols->ols_locklessable = 1;
815 slice->cls_ops = &osc_lock_lockless_ops;
818 LASSERT(ergo(ols->ols_glimpse, !osc_lock_is_lockless(ols)));
821 static bool osc_lock_compatible(const struct osc_lock *qing,
822 const struct osc_lock *qed)
824 struct cl_lock_descr *qed_descr = &qed->ols_cl.cls_lock->cll_descr;
825 struct cl_lock_descr *qing_descr = &qing->ols_cl.cls_lock->cll_descr;
827 if (qed->ols_glimpse || qed->ols_speculative)
830 if (qing_descr->cld_mode == CLM_READ && qed_descr->cld_mode == CLM_READ)
833 if (qed->ols_state < OLS_GRANTED)
836 if (qed_descr->cld_mode >= qing_descr->cld_mode &&
837 qed_descr->cld_start <= qing_descr->cld_start &&
838 qed_descr->cld_end >= qing_descr->cld_end)
844 static void osc_lock_wake_waiters(const struct lu_env *env,
845 struct osc_object *osc,
846 struct osc_lock *oscl)
848 spin_lock(&osc->oo_ol_spin);
849 list_del_init(&oscl->ols_nextlock_oscobj);
850 spin_unlock(&osc->oo_ol_spin);
852 spin_lock(&oscl->ols_lock);
853 while (!list_empty(&oscl->ols_waiting_list)) {
854 struct osc_lock *scan;
856 scan = list_entry(oscl->ols_waiting_list.next, struct osc_lock,
858 list_del_init(&scan->ols_wait_entry);
860 cl_sync_io_note(env, scan->ols_owner, 0);
862 spin_unlock(&oscl->ols_lock);
865 static int osc_lock_enqueue_wait(const struct lu_env *env,
866 struct osc_object *obj, struct osc_lock *oscl)
868 struct osc_lock *tmp_oscl;
869 struct cl_lock_descr *need = &oscl->ols_cl.cls_lock->cll_descr;
870 struct cl_sync_io *waiter = &osc_env_info(env)->oti_anchor;
874 spin_lock(&obj->oo_ol_spin);
875 list_add_tail(&oscl->ols_nextlock_oscobj, &obj->oo_ol_list);
878 list_for_each_entry(tmp_oscl, &obj->oo_ol_list,
879 ols_nextlock_oscobj) {
880 struct cl_lock_descr *descr;
882 if (tmp_oscl == oscl)
885 descr = &tmp_oscl->ols_cl.cls_lock->cll_descr;
886 if (descr->cld_start > need->cld_end ||
887 descr->cld_end < need->cld_start)
890 /* We're not supposed to give up group lock */
891 if (descr->cld_mode == CLM_GROUP)
894 if (!osc_lock_is_lockless(oscl) &&
895 osc_lock_compatible(oscl, tmp_oscl))
898 /* wait for conflicting lock to be canceled */
899 cl_sync_io_init(waiter, 1, cl_sync_io_end);
900 oscl->ols_owner = waiter;
902 spin_lock(&tmp_oscl->ols_lock);
903 /* add oscl into tmp's ols_waiting list */
904 list_add_tail(&oscl->ols_wait_entry,
905 &tmp_oscl->ols_waiting_list);
906 spin_unlock(&tmp_oscl->ols_lock);
908 spin_unlock(&obj->oo_ol_spin);
909 rc = cl_sync_io_wait(env, waiter, 0);
910 spin_lock(&obj->oo_ol_spin);
915 oscl->ols_owner = NULL;
918 spin_unlock(&obj->oo_ol_spin);
924 * Implementation of cl_lock_operations::clo_enqueue() method for osc
925 * layer. This initiates ldlm enqueue:
927 * - cancels conflicting locks early (osc_lock_enqueue_wait());
929 * - calls osc_enqueue_base() to do actual enqueue.
931 * osc_enqueue_base() is supplied with an upcall function that is executed
932 * when lock is received either after a local cached ldlm lock is matched, or
933 * when a reply from the server is received.
935 * This function does not wait for the network communication to complete.
937 static int osc_lock_enqueue(const struct lu_env *env,
938 const struct cl_lock_slice *slice,
939 struct cl_io *unused, struct cl_sync_io *anchor)
941 struct osc_thread_info *info = osc_env_info(env);
942 struct osc_io *oio = osc_env_io(env);
943 struct osc_object *osc = cl2osc(slice->cls_obj);
944 struct osc_lock *oscl = cl2osc_lock(slice);
945 struct obd_export *exp = osc_export(osc);
946 struct cl_lock *lock = slice->cls_lock;
947 struct ldlm_res_id *resname = &info->oti_resname;
948 union ldlm_policy_data *policy = &info->oti_policy;
949 osc_enqueue_upcall_f upcall = osc_lock_upcall;
956 LASSERTF(ergo(oscl->ols_glimpse, lock->cll_descr.cld_mode <= CLM_READ),
957 "lock = %p, ols = %p\n", lock, oscl);
959 if (oscl->ols_state == OLS_GRANTED)
962 if ((oscl->ols_flags & LDLM_FL_NO_EXPANSION) &&
963 !(exp_connect_lockahead_old(exp) || exp_connect_lockahead(exp))) {
964 result = -EOPNOTSUPP;
965 CERROR("%s: server does not support lockahead/locknoexpand:"
966 "rc = %d\n", exp->exp_obd->obd_name, result);
970 if (oscl->ols_flags & LDLM_FL_TEST_LOCK)
971 GOTO(enqueue_base, 0);
973 /* For glimpse and/or speculative locks, do not wait for reply from
974 * server on LDLM request */
975 if (oscl->ols_glimpse || oscl->ols_speculative) {
976 /* Speculative and glimpse locks do not have an anchor */
977 LASSERT(equi(oscl->ols_speculative, anchor == NULL));
979 GOTO(enqueue_base, 0);
982 result = osc_lock_enqueue_wait(env, osc, oscl);
986 /* we can grant lockless lock right after all conflicting locks
988 if (osc_lock_is_lockless(oscl)) {
989 oscl->ols_state = OLS_GRANTED;
990 oio->oi_lockless = 1;
995 oscl->ols_state = OLS_ENQUEUED;
996 if (anchor != NULL) {
997 atomic_inc(&anchor->csi_sync_nr);
998 oscl->ols_owner = anchor;
1002 * DLM lock's ast data must be osc_object;
1003 * if glimpse or speculative lock, async of osc_enqueue_base()
1006 * For non-speculative locks:
1007 * DLM's enqueue callback set to osc_lock_upcall() with cookie as
1009 * For speculative locks:
1010 * osc_lock_upcall_speculative & cookie is the osc object, since
1011 * there is no osc_lock
1013 ostid_build_res_name(&osc->oo_oinfo->loi_oi, resname);
1014 osc_lock_build_policy(env, lock, policy);
1015 if (oscl->ols_speculative) {
1016 oscl->ols_einfo.ei_cbdata = NULL;
1017 /* hold a reference for callback */
1018 cl_object_get(osc2cl(osc));
1019 upcall = osc_lock_upcall_speculative;
1022 result = osc_enqueue_base(exp, resname, &oscl->ols_flags,
1023 policy, &oscl->ols_lvb,
1024 osc->oo_oinfo->loi_kms_valid,
1026 &oscl->ols_einfo, PTLRPCD_SET, async,
1027 oscl->ols_speculative);
1029 if (osc_lock_is_lockless(oscl)) {
1030 oio->oi_lockless = 1;
1031 } else if (!async) {
1032 LASSERT(oscl->ols_state == OLS_GRANTED);
1033 LASSERT(oscl->ols_hold);
1034 LASSERT(oscl->ols_dlmlock != NULL);
1036 } else if (oscl->ols_speculative) {
1037 cl_object_put(env, osc2cl(osc));
1038 if (oscl->ols_glimpse) {
1039 /* hide error for AGL request */
1046 oscl->ols_state = OLS_CANCELLED;
1047 osc_lock_wake_waiters(env, osc, oscl);
1050 cl_sync_io_note(env, anchor, result);
1056 * Breaks a link between osc_lock and dlm_lock.
1058 static void osc_lock_detach(const struct lu_env *env, struct osc_lock *olck)
1060 struct ldlm_lock *dlmlock;
1064 dlmlock = olck->ols_dlmlock;
1065 if (dlmlock == NULL)
1068 if (olck->ols_hold) {
1070 ldlm_lock_decref(&olck->ols_handle, olck->ols_einfo.ei_mode);
1071 olck->ols_handle.cookie = 0ULL;
1074 olck->ols_dlmlock = NULL;
1076 /* release a reference taken in osc_lock_upcall(). */
1077 LASSERT(olck->ols_has_ref);
1078 lu_ref_del(&dlmlock->l_reference, "osc_lock", olck);
1079 LDLM_LOCK_RELEASE(dlmlock);
1080 olck->ols_has_ref = 0;
1086 * Implements cl_lock_operations::clo_cancel() method for osc layer. This is
1087 * called (as part of cl_lock_cancel()) when lock is canceled either voluntary
1088 * (LRU pressure, early cancellation, umount, etc.) or due to the conflict
1089 * with some other lock some where in the cluster. This function does the
1092 * - invalidates all pages protected by this lock (after sending dirty
1093 * ones to the server, as necessary);
1095 * - decref's underlying ldlm lock;
1097 * - cancels ldlm lock (ldlm_cli_cancel()).
1099 static void osc_lock_cancel(const struct lu_env *env,
1100 const struct cl_lock_slice *slice)
1102 struct osc_object *obj = cl2osc(slice->cls_obj);
1103 struct osc_lock *oscl = cl2osc_lock(slice);
1107 LINVRNT(osc_lock_invariant(oscl));
1109 osc_lock_detach(env, oscl);
1110 oscl->ols_state = OLS_CANCELLED;
1111 oscl->ols_flags &= ~LDLM_FL_LVB_READY;
1113 osc_lock_wake_waiters(env, obj, oscl);
1117 static int osc_lock_print(const struct lu_env *env, void *cookie,
1118 lu_printer_t p, const struct cl_lock_slice *slice)
1120 struct osc_lock *lock = cl2osc_lock(slice);
1122 (*p)(env, cookie, "%p %#llx %#llx %d %p ",
1123 lock->ols_dlmlock, lock->ols_flags, lock->ols_handle.cookie,
1124 lock->ols_state, lock->ols_owner);
1125 osc_lvb_print(env, cookie, p, &lock->ols_lvb);
1129 static const struct cl_lock_operations osc_lock_ops = {
1130 .clo_fini = osc_lock_fini,
1131 .clo_enqueue = osc_lock_enqueue,
1132 .clo_cancel = osc_lock_cancel,
1133 .clo_print = osc_lock_print,
1136 static void osc_lock_lockless_cancel(const struct lu_env *env,
1137 const struct cl_lock_slice *slice)
1139 struct osc_lock *ols = cl2osc_lock(slice);
1140 struct osc_object *osc = cl2osc(slice->cls_obj);
1141 struct cl_lock_descr *descr = &slice->cls_lock->cll_descr;
1144 LASSERT(ols->ols_dlmlock == NULL);
1145 result = osc_lock_flush(osc, descr->cld_start, descr->cld_end,
1146 descr->cld_mode, false);
1148 CERROR("Pages for lockless lock %p were not purged(%d)\n",
1151 osc_lock_wake_waiters(env, osc, ols);
1154 static const struct cl_lock_operations osc_lock_lockless_ops = {
1155 .clo_fini = osc_lock_fini,
1156 .clo_enqueue = osc_lock_enqueue,
1157 .clo_cancel = osc_lock_lockless_cancel,
1158 .clo_print = osc_lock_print
1161 static void osc_lock_set_writer(const struct lu_env *env,
1162 const struct cl_io *io,
1163 struct cl_object *obj, struct osc_lock *oscl)
1165 struct cl_lock_descr *descr = &oscl->ols_cl.cls_lock->cll_descr;
1169 if (!cl_object_same(io->ci_obj, obj))
1172 if (likely(io->ci_type == CIT_WRITE)) {
1173 io_start = cl_index(obj, io->u.ci_rw.rw_range.cir_pos);
1174 io_end = cl_index(obj, io->u.ci_rw.rw_range.cir_pos +
1175 io->u.ci_rw.rw_range.cir_count - 1);
1177 LASSERT(cl_io_is_mkwrite(io));
1178 io_start = io_end = io->u.ci_fault.ft_index;
1181 if (descr->cld_mode >= CLM_WRITE &&
1182 (cl_io_is_append(io) ||
1183 (descr->cld_start <= io_start && descr->cld_end >= io_end))) {
1184 struct osc_io *oio = osc_env_io(env);
1186 /* There must be only one lock to match the write region */
1187 LASSERT(oio->oi_write_osclock == NULL);
1188 oio->oi_write_osclock = oscl;
1192 int osc_lock_init(const struct lu_env *env,
1193 struct cl_object *obj, struct cl_lock *lock,
1194 const struct cl_io *io)
1196 struct osc_lock *oscl;
1197 __u32 enqflags = lock->cll_descr.cld_enq_flags;
1199 OBD_SLAB_ALLOC_PTR_GFP(oscl, osc_lock_kmem, GFP_NOFS);
1203 oscl->ols_state = OLS_NEW;
1204 spin_lock_init(&oscl->ols_lock);
1205 INIT_LIST_HEAD(&oscl->ols_waiting_list);
1206 INIT_LIST_HEAD(&oscl->ols_wait_entry);
1207 INIT_LIST_HEAD(&oscl->ols_nextlock_oscobj);
1209 /* Speculative lock requests must be either no_expand or glimpse
1210 * request (CEF_GLIMPSE). non-glimpse no_expand speculative extent
1211 * locks will break ofd_intent_cb. (see comment there)*/
1212 LASSERT(ergo((enqflags & CEF_SPECULATIVE) != 0,
1213 (enqflags & (CEF_LOCK_NO_EXPAND | CEF_GLIMPSE)) != 0));
1215 oscl->ols_flags = osc_enq2ldlm_flags(enqflags);
1216 oscl->ols_speculative = !!(enqflags & CEF_SPECULATIVE);
1218 if (oscl->ols_flags & LDLM_FL_HAS_INTENT) {
1219 oscl->ols_flags |= LDLM_FL_BLOCK_GRANTED;
1220 oscl->ols_glimpse = 1;
1222 osc_lock_build_einfo(env, lock, cl2osc(obj), &oscl->ols_einfo);
1224 cl_lock_slice_add(lock, &oscl->ols_cl, obj, &osc_lock_ops);
1226 if (!(enqflags & CEF_MUST))
1227 /* try to convert this lock to a lockless lock */
1228 osc_lock_to_lockless(env, oscl, (enqflags & CEF_NEVER));
1229 if (oscl->ols_locklessable && !(enqflags & CEF_DISCARD_DATA))
1230 oscl->ols_flags |= LDLM_FL_DENY_ON_CONTENTION;
1232 if (io->ci_type == CIT_WRITE || cl_io_is_mkwrite(io))
1233 osc_lock_set_writer(env, io, obj, oscl);
1235 LDLM_DEBUG_NOLOCK("lock %p, osc lock %p, flags %#llx",
1236 lock, oscl, oscl->ols_flags);
1242 * Finds an existing lock covering given index and optionally different from a
1243 * given \a except lock.
1245 struct ldlm_lock *osc_dlmlock_at_pgoff(const struct lu_env *env,
1246 struct osc_object *obj, pgoff_t index,
1247 enum osc_dap_flags dap_flags)
1249 struct osc_thread_info *info = osc_env_info(env);
1250 struct ldlm_res_id *resname = &info->oti_resname;
1251 union ldlm_policy_data *policy = &info->oti_policy;
1252 struct lustre_handle lockh;
1253 struct ldlm_lock *lock = NULL;
1254 enum ldlm_mode mode;
1259 ostid_build_res_name(&obj->oo_oinfo->loi_oi, resname);
1260 osc_index2policy(policy, osc2cl(obj), index, index);
1261 policy->l_extent.gid = LDLM_GID_ANY;
1263 flags = LDLM_FL_BLOCK_GRANTED | LDLM_FL_CBPENDING;
1264 if (dap_flags & OSC_DAP_FL_TEST_LOCK)
1265 flags |= LDLM_FL_TEST_LOCK;
1267 * It is fine to match any group lock since there could be only one
1268 * with a uniq gid and it conflicts with all other lock modes too
1271 mode = osc_match_base(osc_export(obj), resname, LDLM_EXTENT, policy,
1272 LCK_PR | LCK_PW | LCK_GROUP, &flags, obj, &lockh,
1273 dap_flags & OSC_DAP_FL_CANCELING);
1275 lock = ldlm_handle2lock(&lockh);
1276 /* RACE: the lock is cancelled so let's try again */
1277 if (unlikely(lock == NULL))