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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * This file contains Asynchronous System Trap (AST) handlers and related
34 * LDLM request-processing routines.
36 * An AST is a callback issued on a lock when its state is changed. There are
37 * several different types of ASTs (callbacks) registered for each lock:
39 * - completion AST: when a lock is enqueued by some process, but cannot be
40 * granted immediately due to other conflicting locks on the same resource,
41 * the completion AST is sent to notify the caller when the lock is
44 * - blocking AST: when a lock is granted to some process, if another process
45 * enqueues a conflicting (blocking) lock on a resource, a blocking AST is
46 * sent to notify the holder(s) of the lock(s) of the conflicting lock
47 * request. The lock holder(s) must release their lock(s) on that resource in
48 * a timely manner or be evicted by the server.
50 * - glimpse AST: this is used when a process wants information about a lock
51 * (i.e. the lock value block (LVB)) but does not necessarily require holding
52 * the lock. If the resource is locked, the lock holder(s) are sent glimpse
53 * ASTs and the LVB is returned to the caller, and lock holder(s) may CANCEL
54 * their lock(s) if they are idle. If the resource is not locked, the server
58 #define DEBUG_SUBSYSTEM S_LDLM
60 #include <lustre_errno.h>
61 #include <lustre_dlm.h>
62 #include <obd_class.h>
65 #include "ldlm_internal.h"
67 unsigned int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
68 module_param(ldlm_enqueue_min, uint, 0644);
69 MODULE_PARM_DESC(ldlm_enqueue_min, "lock enqueue timeout minimum");
71 /* in client side, whether the cached locks will be canceled before replay */
72 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
74 static void interrupted_completion_wait(void *data)
78 struct lock_wait_data {
79 struct ldlm_lock *lwd_lock;
83 struct ldlm_async_args {
84 struct lustre_handle lock_handle;
88 * ldlm_request_bufsize
90 * If opcode=LDLM_ENQUEUE, 1 slot is already occupied,
91 * LDLM_LOCKREQ_HANDLE -1 slots are available.
92 * Otherwise, LDLM_LOCKREQ_HANDLE slots are available.
97 * \retval size of the request buffer
100 int ldlm_request_bufsize(int count, int type)
102 int avail = LDLM_LOCKREQ_HANDLES;
103 if (type == LDLM_ENQUEUE)
104 avail -= LDLM_ENQUEUE_CANCEL_OFF;
107 avail = (count - avail) * sizeof(struct lustre_handle);
111 return sizeof(struct ldlm_request) + avail;
114 int ldlm_expired_completion_wait(void *data)
116 struct lock_wait_data *lwd = data;
117 struct ldlm_lock *lock = lwd->lwd_lock;
118 struct obd_import *imp;
119 struct obd_device *obd;
122 if (lock->l_conn_export == NULL) {
123 static time64_t next_dump, last_dump;
125 LDLM_ERROR(lock, "lock timed out (enqueued at %lld, %llds ago); "
126 "not entering recovery in server code, just going back to sleep",
127 (s64)lock->l_activity,
128 (s64)(ktime_get_real_seconds() -
130 if (ktime_get_seconds() > next_dump) {
131 last_dump = next_dump;
132 next_dump = ktime_get_seconds() + 300;
133 ldlm_namespace_dump(D_DLMTRACE,
134 ldlm_lock_to_ns(lock));
136 libcfs_debug_dumplog();
141 obd = lock->l_conn_export->exp_obd;
142 imp = obd->u.cli.cl_import;
143 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
144 LDLM_ERROR(lock, "lock timed out (enqueued at %lld, %llds ago), entering recovery for %s@%s",
145 (s64)lock->l_activity,
146 (s64)(ktime_get_real_seconds() - lock->l_activity),
147 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
153 * Calculate the Completion timeout (covering enqueue, BL AST, data flush,
154 * lock cancel, and their replies). Used for lock completion timeout on the
157 * \param[in] lock lock which is waiting the completion callback
159 * \retval timeout in seconds to wait for the server reply
162 /* We use the same basis for both server side and client side functions
163 from a single node. */
164 static time64_t ldlm_cp_timeout(struct ldlm_lock *lock)
171 /* Wait a long time for enqueue - server may have to callback a
172 * lock from another client. Server will evict the other client if it
173 * doesn't respond reasonably, and then give us the lock. */
174 timeout = at_get(ldlm_lock_to_ns_at(lock));
175 return max(3 * timeout, (time64_t) ldlm_enqueue_min);
179 * Helper function for ldlm_completion_ast(), updating timings when lock is
182 static int ldlm_completion_tail(struct ldlm_lock *lock, void *data)
187 if (ldlm_is_destroyed(lock) || ldlm_is_failed(lock)) {
188 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
190 } else if (data == NULL) {
191 LDLM_DEBUG(lock, "client-side enqueue: granted");
193 /* Take into AT only CP RPC, not immediately granted locks */
194 delay = ktime_get_real_seconds() - lock->l_activity;
195 LDLM_DEBUG(lock, "client-side enqueue: granted after %llds",
198 /* Update our time estimate */
199 at_measured(ldlm_lock_to_ns_at(lock), delay);
205 * Implementation of ->l_completion_ast() for a client, that doesn't wait
206 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
207 * other threads that cannot block for long.
209 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data)
213 if (flags == LDLM_FL_WAIT_NOREPROC) {
214 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
218 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
219 wake_up(&lock->l_waitq);
220 RETURN(ldlm_completion_tail(lock, data));
223 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
225 ldlm_reprocess_all(lock->l_resource);
228 EXPORT_SYMBOL(ldlm_completion_ast_async);
231 * Generic LDLM "completion" AST. This is called in several cases:
233 * - when a reply to an ENQUEUE RPC is received from the server
234 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
235 * this point (determined by flags);
237 * - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
240 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
243 * - to force all locks when resource is destroyed (cleanup_resource());
245 * If lock is not granted in the first case, this function waits until second
246 * or penultimate cases happen in some other thread.
249 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
251 /* XXX ALLOCATE - 160 bytes */
252 struct lock_wait_data lwd;
253 struct obd_device *obd;
254 struct obd_import *imp = NULL;
255 struct l_wait_info lwi;
260 if (flags == LDLM_FL_WAIT_NOREPROC) {
261 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
265 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
266 wake_up(&lock->l_waitq);
270 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
275 obd = class_exp2obd(lock->l_conn_export);
277 /* if this is a local lock, then there is no import */
279 imp = obd->u.cli.cl_import;
282 timeout = ldlm_cp_timeout(lock);
285 lock->l_activity = ktime_get_real_seconds();
287 if (ldlm_is_no_timeout(lock)) {
288 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
289 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
291 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
292 ldlm_expired_completion_wait,
293 interrupted_completion_wait, &lwd);
297 spin_lock(&imp->imp_lock);
298 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
299 spin_unlock(&imp->imp_lock);
302 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
303 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
304 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
305 ldlm_set_fail_loc(lock);
308 /* Go to sleep until the lock is granted or cancelled. */
309 rc = l_wait_event(lock->l_waitq,
310 is_granted_or_cancelled(lock), &lwi);
314 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
319 RETURN(ldlm_completion_tail(lock, data));
321 EXPORT_SYMBOL(ldlm_completion_ast);
324 * A helper to build a blocking AST function
326 * Perform a common operation for blocking ASTs:
327 * defferred lock cancellation.
329 * \param lock the lock blocking or canceling AST was called on
331 * \see mdt_blocking_ast
332 * \see ldlm_blocking_ast
334 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
339 ldlm_set_cbpending(lock);
340 do_ast = (!lock->l_readers && !lock->l_writers);
341 unlock_res_and_lock(lock);
344 struct lustre_handle lockh;
347 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
348 ldlm_lock2handle(lock, &lockh);
349 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
351 CERROR("ldlm_cli_cancel: %d\n", rc);
353 LDLM_DEBUG(lock, "Lock still has references, will be "
358 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
361 * Server blocking AST
363 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
366 * \param lock the lock which blocks a request or cancelling lock
369 * \param flag indicates whether this cancelling or blocking callback
371 * \see ldlm_blocking_ast_nocheck
373 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
374 void *data, int flag)
378 if (flag == LDLM_CB_CANCELING) {
379 /* Don't need to do anything here. */
383 lock_res_and_lock(lock);
384 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
385 * that ldlm_blocking_ast is called just before intent_policy method
386 * takes the lr_lock, then by the time we get the lock, we might not
387 * be the correct blocking function anymore. So check, and return
389 if (lock->l_blocking_ast != ldlm_blocking_ast) {
390 unlock_res_and_lock(lock);
393 RETURN(ldlm_blocking_ast_nocheck(lock));
395 EXPORT_SYMBOL(ldlm_blocking_ast);
398 * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
400 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
401 * rather subtle: with OST-side locking, it may so happen that _all_ extent
402 * locks are held by the OST. If client wants to obtain the current file size
403 * it calls ll_glimpse_size(), and (as all locks are held only on the server),
404 * this dummy glimpse callback fires and does nothing. The client still
405 * receives the correct file size due to the following fragment of code in
406 * ldlm_cb_interpret():
408 * if (rc == -ELDLM_NO_LOCK_DATA) {
409 * LDLM_DEBUG(lock, "lost race - client has a lock but no"
411 * ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
414 * That is, after the glimpse returns this error, ofd_lvbo_update() is called
415 * and returns the updated file attributes from the inode to the client.
417 * See also comment in ofd_intent_policy() on why servers must set a non-NULL
418 * l_glimpse_ast when grabbing DLM locks. Otherwise, the server will assume
419 * that the object is in the process of being destroyed.
421 * \param[in] lock DLM lock being glimpsed, unused
422 * \param[in] reqp pointer to ptlrpc_request, unused
424 * \retval -ELDLM_NO_LOCK_DATA to get attributes from disk object
426 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
428 return -ELDLM_NO_LOCK_DATA;
432 * Enqueue a local lock (typically on a server).
434 int ldlm_cli_enqueue_local(const struct lu_env *env,
435 struct ldlm_namespace *ns,
436 const struct ldlm_res_id *res_id,
437 enum ldlm_type type, union ldlm_policy_data *policy,
438 enum ldlm_mode mode, __u64 *flags,
439 ldlm_blocking_callback blocking,
440 ldlm_completion_callback completion,
441 ldlm_glimpse_callback glimpse,
442 void *data, __u32 lvb_len, enum lvb_type lvb_type,
443 const __u64 *client_cookie,
444 struct lustre_handle *lockh)
446 struct ldlm_lock *lock;
448 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
449 .lcs_blocking = blocking,
450 .lcs_glimpse = glimpse,
454 LASSERT(!(*flags & LDLM_FL_REPLAY));
455 if (unlikely(ns_is_client(ns))) {
456 CERROR("Trying to enqueue local lock in a shadow namespace\n");
460 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
463 GOTO(out_nolock, err = PTR_ERR(lock));
465 err = ldlm_lvbo_init(env, lock->l_resource);
467 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
468 ldlm_lock_destroy_nolock(lock);
472 ldlm_lock2handle(lock, lockh);
474 /* NB: we don't have any lock now (lock_res_and_lock)
475 * because it's a new lock */
476 ldlm_lock_addref_internal_nolock(lock, mode);
477 ldlm_set_local(lock);
478 if (*flags & LDLM_FL_ATOMIC_CB)
479 ldlm_set_atomic_cb(lock);
482 lock->l_policy_data = *policy;
483 if (client_cookie != NULL)
484 lock->l_client_cookie = *client_cookie;
485 if (type == LDLM_EXTENT) {
486 /* extent lock without policy is a bug */
490 lock->l_req_extent = policy->l_extent;
493 err = ldlm_lock_enqueue(env, ns, &lock, policy, flags);
494 if (unlikely(err != ELDLM_OK))
498 *policy = lock->l_policy_data;
500 if (lock->l_completion_ast)
501 lock->l_completion_ast(lock, *flags, NULL);
503 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
506 LDLM_LOCK_RELEASE(lock);
510 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
512 static void failed_lock_cleanup(struct ldlm_namespace *ns,
513 struct ldlm_lock *lock, int mode)
517 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
518 lock_res_and_lock(lock);
519 /* Check that lock is not granted or failed, we might race. */
520 if ((lock->l_req_mode != lock->l_granted_mode) &&
521 !ldlm_is_failed(lock)) {
522 /* Make sure that this lock will not be found by raced
523 * bl_ast and -EINVAL reply is sent to server anyways.
525 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
526 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
529 unlock_res_and_lock(lock);
533 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
534 "LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
536 LDLM_DEBUG(lock, "lock was granted or failed in race");
538 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
539 * from llite/file.c/ll_file_flock(). */
540 /* This code makes for the fact that we do not have blocking handler on
541 * a client for flock locks. As such this is the place where we must
542 * completely kill failed locks. (interrupted and those that
543 * were waiting to be granted when server evicted us. */
544 if (lock->l_resource->lr_type == LDLM_FLOCK) {
545 lock_res_and_lock(lock);
546 if (!ldlm_is_destroyed(lock)) {
547 ldlm_resource_unlink_lock(lock);
548 ldlm_lock_decref_internal_nolock(lock, mode);
549 ldlm_lock_destroy_nolock(lock);
551 unlock_res_and_lock(lock);
553 ldlm_lock_decref_internal(lock, mode);
558 * Finishing portion of client lock enqueue code.
560 * Called after receiving reply from server.
562 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
563 enum ldlm_type type, __u8 with_policy,
564 enum ldlm_mode mode, __u64 *flags, void *lvb,
565 __u32 lvb_len, const struct lustre_handle *lockh,
568 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
569 const struct lu_env *env = NULL;
570 int is_replay = *flags & LDLM_FL_REPLAY;
571 struct ldlm_lock *lock;
572 struct ldlm_reply *reply;
573 int cleanup_phase = 1;
576 if (req && req->rq_svc_thread)
577 env = req->rq_svc_thread->t_env;
579 lock = ldlm_handle2lock(lockh);
580 /* ldlm_cli_enqueue is holding a reference on this lock. */
582 LASSERT(type == LDLM_FLOCK);
586 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
587 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
589 if (rc != ELDLM_OK) {
591 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
592 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
594 if (rc != ELDLM_LOCK_ABORTED)
598 /* Before we return, swab the reply */
599 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
601 GOTO(cleanup, rc = -EPROTO);
606 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
609 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
610 GOTO(cleanup, rc = size);
611 } else if (unlikely(size > lvb_len)) {
612 LDLM_ERROR(lock, "Replied LVB is larger than "
613 "expectation, expected = %d, replied = %d",
615 GOTO(cleanup, rc = -EINVAL);
620 if (rc == ELDLM_LOCK_ABORTED) {
621 if (lvb_len > 0 && lvb != NULL)
622 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
624 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
627 /* lock enqueued on the server */
630 lock_res_and_lock(lock);
631 /* Key change rehash lock in per-export hash with new key */
632 if (exp->exp_lock_hash) {
633 /* In the function below, .hs_keycmp resolves to
634 * ldlm_export_lock_keycmp() */
635 /* coverity[overrun-buffer-val] */
636 cfs_hash_rehash_key(exp->exp_lock_hash,
637 &lock->l_remote_handle,
641 lock->l_remote_handle = reply->lock_handle;
644 *flags = ldlm_flags_from_wire(reply->lock_flags);
645 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
646 LDLM_FL_INHERIT_MASK);
647 unlock_res_and_lock(lock);
649 CDEBUG(D_INFO, "local: %p, remote cookie: %#llx, flags: %#llx\n",
650 lock, reply->lock_handle.cookie, *flags);
652 /* If enqueue returned a blocked lock but the completion handler has
653 * already run, then it fixed up the resource and we don't need to do it
655 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
656 int newmode = reply->lock_desc.l_req_mode;
658 if (newmode && newmode != lock->l_req_mode) {
659 LDLM_DEBUG(lock, "server returned different mode %s",
660 ldlm_lockname[newmode]);
661 lock->l_req_mode = newmode;
664 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
665 &lock->l_resource->lr_name)) {
666 CDEBUG(D_INFO, "remote intent success, locking "DLDLMRES
667 " instead of "DLDLMRES"\n",
668 PLDLMRES(&reply->lock_desc.l_resource),
669 PLDLMRES(lock->l_resource));
671 rc = ldlm_lock_change_resource(ns, lock,
672 &reply->lock_desc.l_resource.lr_name);
673 if (rc || lock->l_resource == NULL)
674 GOTO(cleanup, rc = -ENOMEM);
675 LDLM_DEBUG(lock, "client-side enqueue, new resource");
679 /* We assume lock type cannot change on server*/
680 ldlm_convert_policy_to_local(exp,
681 lock->l_resource->lr_type,
682 &reply->lock_desc.l_policy_data,
683 &lock->l_policy_data);
686 if (type != LDLM_PLAIN)
687 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
690 if ((*flags) & LDLM_FL_AST_SENT) {
691 lock_res_and_lock(lock);
692 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
693 unlock_res_and_lock(lock);
694 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
697 /* If the lock has already been granted by a completion AST, don't
698 * clobber the LVB with an older one. */
700 /* We must lock or a racing completion might update lvb without
701 * letting us know and we'll clobber the correct value.
702 * Cannot unlock after the check either, a that still leaves
703 * a tiny window for completion to get in */
704 lock_res_and_lock(lock);
705 if (lock->l_req_mode != lock->l_granted_mode)
706 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
707 lock->l_lvb_data, lvb_len);
708 unlock_res_and_lock(lock);
716 rc = ldlm_lock_enqueue(env, ns, &lock, NULL, flags);
717 if (lock->l_completion_ast != NULL) {
718 int err = lock->l_completion_ast(lock, *flags, NULL);
726 if (lvb_len > 0 && lvb != NULL) {
727 /* Copy the LVB here, and not earlier, because the completion
728 * AST (if any) can override what we got in the reply */
729 memcpy(lvb, lock->l_lvb_data, lvb_len);
732 LDLM_DEBUG(lock, "client-side enqueue END");
735 if (cleanup_phase == 1 && rc)
736 failed_lock_cleanup(ns, lock, mode);
737 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
739 LDLM_LOCK_RELEASE(lock);
742 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
745 * Estimate number of lock handles that would fit into request of given
746 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
747 * a single page on the send/receive side. XXX: 512 should be changed to
748 * more adequate value.
750 static inline int ldlm_req_handles_avail(int req_size, int off)
754 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
755 if (likely(avail >= 0))
756 avail /= (int)sizeof(struct lustre_handle);
759 avail += LDLM_LOCKREQ_HANDLES - off;
764 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
765 enum req_location loc,
768 __u32 size = req_capsule_msg_size(pill, loc);
769 return ldlm_req_handles_avail(size, off);
772 static inline int ldlm_format_handles_avail(struct obd_import *imp,
773 const struct req_format *fmt,
774 enum req_location loc, int off)
776 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
777 return ldlm_req_handles_avail(size, off);
781 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
782 * \a count locks in \a cancels.
784 * This is to be called by functions preparing their own requests that
785 * might contain lists of locks to cancel in addition to actual operation
786 * that needs to be performed.
788 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
789 int version, int opc, int canceloff,
790 struct list_head *cancels, int count)
792 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
793 struct req_capsule *pill = &req->rq_pill;
794 struct ldlm_request *dlm = NULL;
795 struct list_head head = LIST_HEAD_INIT(head);
796 enum ldlm_lru_flags lru_flags;
797 int avail, to_free, pack = 0;
803 if (ns_connect_cancelset(ns)) {
804 /* Estimate the amount of available space in the request. */
805 req_capsule_filled_sizes(pill, RCL_CLIENT);
806 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
808 lru_flags = LDLM_LRU_FLAG_NO_WAIT | (ns_connect_lru_resize(ns) ?
809 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED);
810 to_free = !ns_connect_lru_resize(ns) &&
811 opc == LDLM_ENQUEUE ? 1 : 0;
813 /* Cancel LRU locks here _only_ if the server supports
814 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
815 * RPC, which will make us slower. */
817 count += ldlm_cancel_lru_local(ns, cancels, to_free,
824 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
825 ldlm_request_bufsize(pack, opc));
828 rc = ptlrpc_request_pack(req, version, opc);
830 ldlm_lock_list_put(cancels, l_bl_ast, count);
834 if (ns_connect_cancelset(ns)) {
836 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
838 /* Skip first lock handler in ldlm_request_pack(),
839 * this method will increment @lock_count according
840 * to the lock handle amount actually written to
842 dlm->lock_count = canceloff;
844 /* Pack into the request @pack lock handles. */
845 ldlm_cli_cancel_list(cancels, pack, req, 0);
846 /* Prepare and send separate cancel RPC for others. */
847 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
849 ldlm_lock_list_put(cancels, l_bl_ast, count);
853 EXPORT_SYMBOL(ldlm_prep_elc_req);
855 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
856 struct list_head *cancels, int count)
858 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
859 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
861 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
863 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
865 struct ptlrpc_request *req;
869 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
871 RETURN(ERR_PTR(-ENOMEM));
873 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
875 ptlrpc_request_free(req);
879 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
880 ptlrpc_request_set_replen(req);
883 EXPORT_SYMBOL(ldlm_enqueue_pack);
886 * Client-side lock enqueue.
888 * If a request has some specific initialisation it is passed in \a reqp,
889 * otherwise it is created in ldlm_cli_enqueue.
891 * Supports sync and async requests, pass \a async flag accordingly. If a
892 * request was created in ldlm_cli_enqueue and it is the async request,
893 * pass it to the caller in \a reqp.
895 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
896 struct ldlm_enqueue_info *einfo,
897 const struct ldlm_res_id *res_id,
898 union ldlm_policy_data const *policy, __u64 *flags,
899 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
900 struct lustre_handle *lockh, int async)
902 struct ldlm_namespace *ns;
903 struct ldlm_lock *lock;
904 struct ldlm_request *body;
905 int is_replay = *flags & LDLM_FL_REPLAY;
906 int req_passed_in = 1;
908 struct ptlrpc_request *req;
911 LASSERT(exp != NULL);
913 ns = exp->exp_obd->obd_namespace;
915 /* If we're replaying this lock, just check some invariants.
916 * If we're creating a new lock, get everything all setup nice. */
918 lock = ldlm_handle2lock_long(lockh, 0);
919 LASSERT(lock != NULL);
920 LDLM_DEBUG(lock, "client-side enqueue START");
921 LASSERT(exp == lock->l_conn_export);
923 const struct ldlm_callback_suite cbs = {
924 .lcs_completion = einfo->ei_cb_cp,
925 .lcs_blocking = einfo->ei_cb_bl,
926 .lcs_glimpse = einfo->ei_cb_gl
928 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
929 einfo->ei_mode, &cbs, einfo->ei_cbdata,
932 RETURN(PTR_ERR(lock));
933 /* for the local lock, add the reference */
934 ldlm_lock_addref_internal(lock, einfo->ei_mode);
935 ldlm_lock2handle(lock, lockh);
937 lock->l_policy_data = *policy;
939 if (einfo->ei_type == LDLM_EXTENT) {
940 /* extent lock without policy is a bug */
944 lock->l_req_extent = policy->l_extent;
946 LDLM_DEBUG(lock, "client-side enqueue START, flags %#llx",
950 lock->l_conn_export = exp;
951 lock->l_export = NULL;
952 lock->l_blocking_ast = einfo->ei_cb_bl;
953 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL));
954 lock->l_activity = ktime_get_real_seconds();
956 /* lock not sent to server yet */
957 if (reqp == NULL || *reqp == NULL) {
958 req = ldlm_enqueue_pack(exp, lvb_len);
960 failed_lock_cleanup(ns, lock, einfo->ei_mode);
961 LDLM_LOCK_RELEASE(lock);
962 RETURN(PTR_ERR(req));
972 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
974 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
975 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
978 if (*flags & LDLM_FL_NDELAY) {
979 DEBUG_REQ(D_DLMTRACE, req, "enque lock with no delay\n");
980 req->rq_no_resend = req->rq_no_delay = 1;
981 /* probably set a shorter timeout value and handle ETIMEDOUT
982 * in osc_lock_upcall() correctly */
983 /* lustre_msg_set_timeout(req, req->rq_timeout / 2); */
986 /* Dump lock data into the request buffer */
987 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
988 ldlm_lock2desc(lock, &body->lock_desc);
989 body->lock_flags = ldlm_flags_to_wire(*flags);
990 body->lock_handle[0] = *lockh;
992 /* extended LDLM opcodes in client stats */
993 if (exp->exp_obd->obd_svc_stats != NULL) {
994 bool glimpse = *flags & LDLM_FL_HAS_INTENT;
996 /* OST glimpse has no intent buffer */
997 if (req_capsule_has_field(&req->rq_pill, &RMF_LDLM_INTENT,
999 struct ldlm_intent *it;
1001 it = req_capsule_client_get(&req->rq_pill,
1003 glimpse = (it && (it->opc == IT_GLIMPSE));
1007 ldlm_svc_get_eopc(body, exp->exp_obd->obd_svc_stats);
1009 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1011 LDLM_GLIMPSE_ENQUEUE);
1015 LASSERT(reqp != NULL);
1019 LDLM_DEBUG(lock, "sending request");
1021 rc = ptlrpc_queue_wait(req);
1023 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
1024 einfo->ei_mode, flags, lvb, lvb_len,
1027 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
1028 * one reference that we took */
1030 LDLM_LOCK_RELEASE(lock);
1034 if (!req_passed_in && req != NULL) {
1035 ptlrpc_req_finished(req);
1042 EXPORT_SYMBOL(ldlm_cli_enqueue);
1045 * Client-side lock convert reply handling.
1047 * Finish client lock converting, checks for concurrent converts
1048 * and clear 'converting' flag so lock can be placed back into LRU.
1050 static int lock_convert_interpret(const struct lu_env *env,
1051 struct ptlrpc_request *req,
1052 struct ldlm_async_args *aa, int rc)
1054 struct ldlm_lock *lock;
1055 struct ldlm_reply *reply;
1059 lock = ldlm_handle2lock(&aa->lock_handle);
1061 LDLM_DEBUG_NOLOCK("convert ACK for unknown local cookie %#llx",
1062 aa->lock_handle.cookie);
1066 LDLM_DEBUG(lock, "CONVERTED lock:");
1071 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1073 GOTO(out, rc = -EPROTO);
1075 if (reply->lock_handle.cookie != aa->lock_handle.cookie) {
1076 LDLM_ERROR(lock, "convert ACK with wrong lock cookie %#llx"
1077 " but cookie %#llx from server %s id %s\n",
1078 aa->lock_handle.cookie, reply->lock_handle.cookie,
1079 req->rq_export->exp_client_uuid.uuid,
1080 libcfs_id2str(req->rq_peer));
1081 GOTO(out, rc = ELDLM_NO_LOCK_DATA);
1084 lock_res_and_lock(lock);
1085 /* Lock convert is sent for any new bits to drop, the converting flag
1086 * is dropped when ibits on server are the same as on client. Meanwhile
1087 * that can be so that more later convert will be replied first with
1088 * and clear converting flag, so in case of such race just exit here.
1089 * if lock has no converting bits then */
1090 if (!ldlm_is_converting(lock)) {
1091 LDLM_DEBUG(lock, "convert ACK for lock without converting flag,"
1092 " reply ibits %#llx",
1093 reply->lock_desc.l_policy_data.l_inodebits.bits);
1094 } else if (reply->lock_desc.l_policy_data.l_inodebits.bits !=
1095 lock->l_policy_data.l_inodebits.bits) {
1096 /* Compare server returned lock ibits and local lock ibits
1097 * if they are the same we consider convertion is done,
1098 * otherwise we have more converts inflight and keep
1101 LDLM_DEBUG(lock, "convert ACK with ibits %#llx\n",
1102 reply->lock_desc.l_policy_data.l_inodebits.bits);
1104 ldlm_clear_converting(lock);
1106 /* Concurrent BL AST may arrive and cause another convert
1107 * or cancel so just do nothing here if bl_ast is set,
1108 * finish with convert otherwise.
1110 if (!ldlm_is_bl_ast(lock)) {
1111 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
1113 /* Drop cancel_bits since there are no more converts
1114 * and put lock into LRU if it is still not used and
1117 lock->l_policy_data.l_inodebits.cancel_bits = 0;
1118 if (!lock->l_readers && !lock->l_writers &&
1119 !ldlm_is_canceling(lock)) {
1120 spin_lock(&ns->ns_lock);
1121 /* there is check for list_empty() inside */
1122 ldlm_lock_remove_from_lru_nolock(lock);
1123 ldlm_lock_add_to_lru_nolock(lock);
1124 spin_unlock(&ns->ns_lock);
1128 unlock_res_and_lock(lock);
1133 lock_res_and_lock(lock);
1134 if (ldlm_is_converting(lock)) {
1135 ldlm_clear_converting(lock);
1136 ldlm_set_cbpending(lock);
1137 ldlm_set_bl_ast(lock);
1138 lock->l_policy_data.l_inodebits.cancel_bits = 0;
1140 unlock_res_and_lock(lock);
1142 /* fallback to normal lock cancel. If rc means there is no
1143 * valid lock on server, do only local cancel */
1144 if (rc == ELDLM_NO_LOCK_DATA)
1149 rc = ldlm_cli_cancel(&aa->lock_handle, flag);
1151 LDLM_DEBUG(lock, "failed to cancel lock: rc = %d\n",
1154 LDLM_LOCK_PUT(lock);
1159 * Client-side IBITS lock convert.
1161 * Inform server that lock has been converted instead of canceling.
1162 * Server finishes convert on own side and does reprocess to grant
1163 * all related waiting locks.
1165 * Since convert means only ibits downgrading, client doesn't need to
1166 * wait for server reply to finish local converting process so this request
1167 * is made asynchronous.
1170 int ldlm_cli_convert(struct ldlm_lock *lock, __u32 *flags)
1172 struct ldlm_request *body;
1173 struct ptlrpc_request *req;
1174 struct ldlm_async_args *aa;
1175 struct obd_export *exp = lock->l_conn_export;
1180 LDLM_ERROR(lock, "convert must not be called on local locks.");
1184 /* this is better to check earlier and it is done so already,
1185 * but this check is kept too as final one to issue an error
1186 * if any new code will miss such check.
1188 if (!exp_connect_lock_convert(exp)) {
1189 LDLM_ERROR(lock, "server doesn't support lock convert\n");
1193 if (lock->l_resource->lr_type != LDLM_IBITS) {
1194 LDLM_ERROR(lock, "convert works with IBITS locks only.");
1198 LDLM_DEBUG(lock, "client-side convert");
1200 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1201 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1206 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1207 body->lock_handle[0] = lock->l_remote_handle;
1209 body->lock_desc.l_req_mode = lock->l_req_mode;
1210 body->lock_desc.l_granted_mode = lock->l_granted_mode;
1212 body->lock_desc.l_policy_data.l_inodebits.bits =
1213 lock->l_policy_data.l_inodebits.bits;
1214 body->lock_desc.l_policy_data.l_inodebits.cancel_bits = 0;
1216 body->lock_flags = ldlm_flags_to_wire(*flags);
1217 body->lock_count = 1;
1219 ptlrpc_request_set_replen(req);
1222 * Use cancel portals for convert as well as high-priority handling.
1224 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1225 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1227 ptlrpc_at_set_req_timeout(req);
1229 if (exp->exp_obd->obd_svc_stats != NULL)
1230 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1231 LDLM_CONVERT - LDLM_FIRST_OPC);
1233 aa = ptlrpc_req_async_args(req);
1234 ldlm_lock2handle(lock, &aa->lock_handle);
1235 req->rq_interpret_reply = (ptlrpc_interpterer_t)lock_convert_interpret;
1237 ptlrpcd_add_req(req);
1242 * Cancel locks locally.
1244 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1245 * \retval LDLM_FL_CANCELING otherwise;
1246 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1248 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1250 __u64 rc = LDLM_FL_LOCAL_ONLY;
1253 if (lock->l_conn_export) {
1256 LDLM_DEBUG(lock, "client-side cancel");
1257 /* Set this flag to prevent others from getting new references*/
1258 lock_res_and_lock(lock);
1259 ldlm_set_cbpending(lock);
1260 local_only = !!(lock->l_flags &
1261 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1262 ldlm_cancel_callback(lock);
1263 rc = (ldlm_is_bl_ast(lock)) ?
1264 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1265 unlock_res_and_lock(lock);
1268 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1270 rc = LDLM_FL_LOCAL_ONLY;
1272 ldlm_lock_cancel(lock);
1274 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1275 LDLM_ERROR(lock, "Trying to cancel local lock");
1278 LDLM_DEBUG(lock, "server-side local cancel");
1279 ldlm_lock_cancel(lock);
1280 ldlm_reprocess_all(lock->l_resource);
1287 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1289 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1290 struct list_head *head, int count)
1292 struct ldlm_request *dlm;
1293 struct ldlm_lock *lock;
1294 int max, packed = 0;
1297 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1298 LASSERT(dlm != NULL);
1300 /* Check the room in the request buffer. */
1301 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1302 sizeof(struct ldlm_request);
1303 max /= sizeof(struct lustre_handle);
1304 max += LDLM_LOCKREQ_HANDLES;
1305 LASSERT(max >= dlm->lock_count + count);
1307 /* XXX: it would be better to pack lock handles grouped by resource.
1308 * so that the server cancel would call filter_lvbo_update() less
1310 list_for_each_entry(lock, head, l_bl_ast) {
1313 LASSERT(lock->l_conn_export);
1314 /* Pack the lock handle to the given request buffer. */
1315 LDLM_DEBUG(lock, "packing");
1316 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1319 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1324 * Prepare and send a batched cancel RPC. It will include \a count lock
1325 * handles of locks given in \a cancels list. */
1326 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1327 int count, enum ldlm_cancel_flags flags)
1329 struct ptlrpc_request *req = NULL;
1330 struct obd_import *imp;
1335 LASSERT(exp != NULL);
1338 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1340 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1343 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1344 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1349 imp = class_exp2cliimp(exp);
1350 if (imp == NULL || imp->imp_invalid) {
1352 "skipping cancel on invalid import %p\n", imp);
1356 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1358 GOTO(out, rc = -ENOMEM);
1360 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1361 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1362 ldlm_request_bufsize(count, LDLM_CANCEL));
1364 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1366 ptlrpc_request_free(req);
1370 /* If OSP want cancel cross-MDT lock, let's not block it in
1371 * in recovery, otherwise the lock will not released, if
1372 * the remote target is also in recovery, and it also need
1373 * this lock, it might cause deadlock. */
1374 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1375 exp->exp_obd->obd_lu_dev != NULL &&
1376 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1377 struct lu_device *top_dev;
1379 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1380 if (top_dev != NULL &&
1381 top_dev->ld_obd->obd_recovering)
1382 req->rq_allow_replay = 1;
1385 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1386 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1387 ptlrpc_at_set_req_timeout(req);
1389 ldlm_cancel_pack(req, cancels, count);
1391 ptlrpc_request_set_replen(req);
1392 if (flags & LCF_ASYNC) {
1393 ptlrpcd_add_req(req);
1398 rc = ptlrpc_queue_wait(req);
1399 if (rc == LUSTRE_ESTALE) {
1400 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1401 "out of sync -- not fatal\n",
1402 libcfs_nid2str(req->rq_import->
1403 imp_connection->c_peer.nid));
1405 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1406 req->rq_import_generation == imp->imp_generation) {
1407 ptlrpc_req_finished(req);
1409 } else if (rc != ELDLM_OK) {
1410 /* -ESHUTDOWN is common on umount */
1411 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1412 "Got rc %d from cancel RPC: "
1413 "canceling anyway\n", rc);
1420 ptlrpc_req_finished(req);
1423 return sent ? sent : rc;
1426 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1428 LASSERT(imp != NULL);
1429 return &imp->imp_obd->obd_namespace->ns_pool;
1433 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1435 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1437 struct obd_device *obd;
1441 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1442 !imp_connect_lru_resize(req->rq_import)))
1445 * Do nothing for corner cases.
1450 /* In some cases RPC may contain SLV and limit zeroed out. This
1451 * is the case when server does not support LRU resize feature.
1452 * This is also possible in some recovery cases when server-side
1453 * reqs have no reference to the OBD export and thus access to
1454 * server-side namespace is not possible. */
1455 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1456 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1457 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1458 "(SLV: %llu, Limit: %u)",
1459 lustre_msg_get_slv(req->rq_repmsg),
1460 lustre_msg_get_limit(req->rq_repmsg));
1464 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1465 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1466 obd = req->rq_import->imp_obd;
1468 /* Set new SLV and limit in OBD fields to make them accessible
1469 * to the pool thread. We do not access obd_namespace and pool
1470 * directly here as there is no reliable way to make sure that
1471 * they are still alive at cleanup time. Evil races are possible
1472 * which may cause Oops at that time. */
1473 write_lock(&obd->obd_pool_lock);
1474 obd->obd_pool_slv = new_slv;
1475 obd->obd_pool_limit = new_limit;
1476 write_unlock(&obd->obd_pool_lock);
1482 * Client side lock cancel.
1484 * Lock must not have any readers or writers by this time.
1486 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1487 enum ldlm_cancel_flags cancel_flags)
1489 struct obd_export *exp;
1490 enum ldlm_lru_flags lru_flags;
1491 int avail, count = 1;
1493 struct ldlm_namespace *ns;
1494 struct ldlm_lock *lock;
1495 struct list_head cancels = LIST_HEAD_INIT(cancels);
1499 lock = ldlm_handle2lock_long(lockh, 0);
1501 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1505 /* Convert lock bits instead of cancel for IBITS locks */
1506 if (cancel_flags & LCF_CONVERT) {
1507 LASSERT(lock->l_resource->lr_type == LDLM_IBITS);
1508 LASSERT(lock->l_policy_data.l_inodebits.cancel_bits != 0);
1510 rc = ldlm_cli_dropbits(lock,
1511 lock->l_policy_data.l_inodebits.cancel_bits);
1513 LDLM_LOCK_RELEASE(lock);
1518 lock_res_and_lock(lock);
1519 /* Lock is being canceled and the caller doesn't want to wait */
1520 if (ldlm_is_canceling(lock)) {
1521 if (cancel_flags & LCF_ASYNC) {
1522 unlock_res_and_lock(lock);
1524 struct l_wait_info lwi = { 0 };
1526 unlock_res_and_lock(lock);
1527 l_wait_event(lock->l_waitq, is_bl_done(lock), &lwi);
1529 LDLM_LOCK_RELEASE(lock);
1533 /* Lock is being converted, cancel it immediately.
1534 * When convert will end, it releases lock and it will be gone.
1536 if (ldlm_is_converting(lock)) {
1537 /* set back flags removed by convert */
1538 ldlm_set_cbpending(lock);
1539 ldlm_set_bl_ast(lock);
1542 ldlm_set_canceling(lock);
1543 unlock_res_and_lock(lock);
1545 rc = ldlm_cli_cancel_local(lock);
1546 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1547 LDLM_LOCK_RELEASE(lock);
1550 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1551 * RPC which goes to canceld portal, so we can cancel other LRU locks
1552 * here and send them all as one LDLM_CANCEL RPC. */
1553 LASSERT(list_empty(&lock->l_bl_ast));
1554 list_add(&lock->l_bl_ast, &cancels);
1556 exp = lock->l_conn_export;
1557 if (exp_connect_cancelset(exp)) {
1558 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1563 ns = ldlm_lock_to_ns(lock);
1564 lru_flags = ns_connect_lru_resize(ns) ?
1565 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1566 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1567 LCF_BL_AST, lru_flags);
1569 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1572 EXPORT_SYMBOL(ldlm_cli_cancel);
1575 * Locally cancel up to \a count locks in list \a cancels.
1576 * Return the number of cancelled locks.
1578 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1579 enum ldlm_cancel_flags cancel_flags)
1581 struct list_head head = LIST_HEAD_INIT(head);
1582 struct ldlm_lock *lock, *next;
1583 int left = 0, bl_ast = 0;
1587 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1591 if (cancel_flags & LCF_LOCAL) {
1592 rc = LDLM_FL_LOCAL_ONLY;
1593 ldlm_lock_cancel(lock);
1595 rc = ldlm_cli_cancel_local(lock);
1597 /* Until we have compound requests and can send LDLM_CANCEL
1598 * requests batched with generic RPCs, we need to send cancels
1599 * with the LDLM_FL_BL_AST flag in a separate RPC from
1600 * the one being generated now. */
1601 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1602 LDLM_DEBUG(lock, "Cancel lock separately");
1603 list_del_init(&lock->l_bl_ast);
1604 list_add(&lock->l_bl_ast, &head);
1608 if (rc == LDLM_FL_LOCAL_ONLY) {
1609 /* CANCEL RPC should not be sent to server. */
1610 list_del_init(&lock->l_bl_ast);
1611 LDLM_LOCK_RELEASE(lock);
1617 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1624 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1625 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1626 * readahead requests, ...)
1628 static enum ldlm_policy_res
1629 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1630 int unused, int added, int count)
1632 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1634 /* don't check added & count since we want to process all locks
1636 * It's fine to not take lock to access lock->l_resource since
1637 * the lock has already been granted so it won't change. */
1638 switch (lock->l_resource->lr_type) {
1641 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1644 result = LDLM_POLICY_SKIP_LOCK;
1652 * Callback function for LRU-resize policy. Decides whether to keep
1653 * \a lock in LRU for current \a LRU size \a unused, added in current
1654 * scan \a added and number of locks to be preferably canceled \a count.
1656 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1658 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1660 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1661 struct ldlm_lock *lock,
1662 int unused, int added,
1665 ktime_t cur = ktime_get();
1666 struct ldlm_pool *pl = &ns->ns_pool;
1670 /* Stop LRU processing when we reach past @count or have checked all
1672 if (count && added >= count)
1673 return LDLM_POLICY_KEEP_LOCK;
1675 /* Despite of the LV, It doesn't make sense to keep the lock which
1676 * is unused for ns_max_age time.
1678 if (ktime_after(ktime_get(),
1679 ktime_add(lock->l_last_used, ns->ns_max_age)))
1680 return LDLM_POLICY_CANCEL_LOCK;
1682 slv = ldlm_pool_get_slv(pl);
1683 lvf = ldlm_pool_get_lvf(pl);
1684 la = div_u64(ktime_to_ns(ktime_sub(cur, lock->l_last_used)),
1686 lv = lvf * la * unused;
1688 /* Inform pool about current CLV to see it via debugfs. */
1689 ldlm_pool_set_clv(pl, lv);
1691 /* Stop when SLV is not yet come from server or lv is smaller than
1693 if (slv == 0 || lv < slv)
1694 return LDLM_POLICY_KEEP_LOCK;
1696 return LDLM_POLICY_CANCEL_LOCK;
1699 static enum ldlm_policy_res
1700 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1701 struct ldlm_lock *lock,
1702 int unused, int added,
1705 enum ldlm_policy_res result;
1707 result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1708 if (result == LDLM_POLICY_KEEP_LOCK)
1711 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1715 * Callback function for debugfs used policy. Makes decision whether to keep
1716 * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1717 * added and number of locks to be preferably canceled \a count.
1719 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1721 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1723 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1724 struct ldlm_lock *lock,
1725 int unused, int added,
1728 /* Stop LRU processing when we reach past @count or have checked all
1730 return (added >= count) ?
1731 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1735 * Callback function for aged policy. Makes decision whether to keep \a lock in
1736 * LRU for current LRU size \a unused, added in current scan \a added and
1737 * number of locks to be preferably canceled \a count.
1739 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1741 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1743 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1744 struct ldlm_lock *lock,
1745 int unused, int added,
1748 if ((added >= count) &&
1749 ktime_before(ktime_get(),
1750 ktime_add(lock->l_last_used, ns->ns_max_age)))
1751 return LDLM_POLICY_KEEP_LOCK;
1753 return LDLM_POLICY_CANCEL_LOCK;
1756 static enum ldlm_policy_res
1757 ldlm_cancel_aged_no_wait_policy(struct ldlm_namespace *ns,
1758 struct ldlm_lock *lock,
1759 int unused, int added, int count)
1761 enum ldlm_policy_res result;
1763 result = ldlm_cancel_aged_policy(ns, lock, unused, added, count);
1764 if (result == LDLM_POLICY_KEEP_LOCK)
1767 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1771 * Callback function for default policy. Makes decision whether to keep \a lock
1772 * in LRU for current LRU size \a unused, added in current scan \a added and
1773 * number of locks to be preferably canceled \a count.
1775 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1777 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1780 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1781 struct ldlm_lock *lock,
1782 int unused, int added,
1785 /* Stop LRU processing when we reach past count or have checked all
1787 return (added >= count) ?
1788 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1791 typedef enum ldlm_policy_res
1792 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1793 int unused, int added, int count);
1795 static ldlm_cancel_lru_policy_t
1796 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1798 if (ns_connect_lru_resize(ns)) {
1799 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1800 /* We kill passed number of old locks. */
1801 return ldlm_cancel_passed_policy;
1802 if (lru_flags & LDLM_LRU_FLAG_LRUR) {
1803 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1804 return ldlm_cancel_lrur_no_wait_policy;
1806 return ldlm_cancel_lrur_policy;
1808 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1809 return ldlm_cancel_passed_policy;
1811 if (lru_flags & LDLM_LRU_FLAG_AGED) {
1812 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1813 return ldlm_cancel_aged_no_wait_policy;
1815 return ldlm_cancel_aged_policy;
1818 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1819 return ldlm_cancel_no_wait_policy;
1821 return ldlm_cancel_default_policy;
1825 * - Free space in LRU for \a count new locks,
1826 * redundant unused locks are canceled locally;
1827 * - also cancel locally unused aged locks;
1828 * - do not cancel more than \a max locks;
1829 * - GET the found locks and add them into the \a cancels list.
1831 * A client lock can be added to the l_bl_ast list only when it is
1832 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1833 * CANCEL. There are the following use cases:
1834 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1835 * ldlm_cli_cancel(), which check and set this flag properly. As any
1836 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1837 * later without any special locking.
1839 * Calling policies for enabled LRU resize:
1840 * ----------------------------------------
1841 * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1842 * cancel not more than \a count locks;
1844 * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1845 * at the beginning of LRU list);
1847 * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1848 * to memory pressre policy function;
1850 * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1852 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1853 * (typically before replaying locks) w/o
1854 * sending any RPCs or waiting for any
1855 * outstanding RPC to complete.
1857 * flags & LDLM_CANCEL_CLEANUP - when cancelling read locks, do not check for
1858 * other read locks covering the same pages, just
1859 * discard those pages.
1861 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1862 struct list_head *cancels, int count, int max,
1863 enum ldlm_lru_flags lru_flags)
1865 ldlm_cancel_lru_policy_t pf;
1867 int no_wait = lru_flags & LDLM_LRU_FLAG_NO_WAIT;
1871 if (!ns_connect_lru_resize(ns))
1872 count += ns->ns_nr_unused - ns->ns_max_unused;
1874 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1875 LASSERT(pf != NULL);
1877 /* For any flags, stop scanning if @max is reached. */
1878 while (!list_empty(&ns->ns_unused_list) && (max == 0 || added < max)) {
1879 struct ldlm_lock *lock;
1880 struct list_head *item, *next;
1881 enum ldlm_policy_res result;
1882 ktime_t last_use = ktime_set(0, 0);
1884 spin_lock(&ns->ns_lock);
1885 item = no_wait ? ns->ns_last_pos : &ns->ns_unused_list;
1886 for (item = item->next, next = item->next;
1887 item != &ns->ns_unused_list;
1888 item = next, next = item->next) {
1889 lock = list_entry(item, struct ldlm_lock, l_lru);
1891 /* No locks which got blocking requests. */
1892 LASSERT(!ldlm_is_bl_ast(lock));
1894 if (!ldlm_is_canceling(lock) &&
1895 !ldlm_is_converting(lock))
1898 /* Somebody is already doing CANCEL. No need for this
1899 * lock in LRU, do not traverse it again. */
1900 ldlm_lock_remove_from_lru_nolock(lock);
1902 if (item == &ns->ns_unused_list) {
1903 spin_unlock(&ns->ns_lock);
1907 last_use = lock->l_last_used;
1909 LDLM_LOCK_GET(lock);
1910 spin_unlock(&ns->ns_lock);
1911 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1913 /* Pass the lock through the policy filter and see if it
1914 * should stay in LRU.
1916 * Even for shrinker policy we stop scanning if
1917 * we find a lock that should stay in the cache.
1918 * We should take into account lock age anyway
1919 * as a new lock is a valuable resource even if
1920 * it has a low weight.
1922 * That is, for shrinker policy we drop only
1923 * old locks, but additionally choose them by
1924 * their weight. Big extent locks will stay in
1926 result = pf(ns, lock, ns->ns_nr_unused, added, count);
1927 if (result == LDLM_POLICY_KEEP_LOCK) {
1928 lu_ref_del(&lock->l_reference, __func__, current);
1929 LDLM_LOCK_RELEASE(lock);
1933 if (result == LDLM_POLICY_SKIP_LOCK) {
1934 lu_ref_del(&lock->l_reference, __func__, current);
1936 spin_lock(&ns->ns_lock);
1937 if (!list_empty(&lock->l_lru) &&
1938 lock->l_lru.prev == ns->ns_last_pos)
1939 ns->ns_last_pos = &lock->l_lru;
1940 spin_unlock(&ns->ns_lock);
1943 LDLM_LOCK_RELEASE(lock);
1947 lock_res_and_lock(lock);
1948 /* Check flags again under the lock. */
1949 if (ldlm_is_canceling(lock) || ldlm_is_converting(lock) ||
1950 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1951 /* Another thread is removing lock from LRU, or
1952 * somebody is already doing CANCEL, or there
1953 * is a blocking request which will send cancel
1954 * by itself, or the lock is no longer unused or
1955 * the lock has been used since the pf() call and
1956 * pages could be put under it. */
1957 unlock_res_and_lock(lock);
1958 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1959 LDLM_LOCK_RELEASE(lock);
1962 LASSERT(!lock->l_readers && !lock->l_writers);
1964 /* If we have chosen to cancel this lock voluntarily, we
1965 * better send cancel notification to server, so that it
1966 * frees appropriate state. This might lead to a race
1967 * where while we are doing cancel here, server is also
1968 * silently cancelling this lock. */
1969 ldlm_clear_cancel_on_block(lock);
1971 /* Setting the CBPENDING flag is a little misleading,
1972 * but prevents an important race; namely, once
1973 * CBPENDING is set, the lock can accumulate no more
1974 * readers/writers. Since readers and writers are
1975 * already zero here, ldlm_lock_decref() won't see
1976 * this flag and call l_blocking_ast */
1977 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1979 if ((lru_flags & LDLM_LRU_FLAG_CLEANUP) &&
1980 (lock->l_resource->lr_type == LDLM_EXTENT ||
1981 ldlm_has_dom(lock)) && lock->l_granted_mode == LCK_PR)
1982 ldlm_set_discard_data(lock);
1984 /* We can't re-add to l_lru as it confuses the
1985 * refcounting in ldlm_lock_remove_from_lru() if an AST
1986 * arrives after we drop lr_lock below. We use l_bl_ast
1987 * and can't use l_pending_chain as it is used both on
1988 * server and client nevertheless bug 5666 says it is
1989 * used only on server */
1990 LASSERT(list_empty(&lock->l_bl_ast));
1991 list_add(&lock->l_bl_ast, cancels);
1992 unlock_res_and_lock(lock);
1993 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1999 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
2001 enum ldlm_cancel_flags cancel_flags,
2002 enum ldlm_lru_flags lru_flags)
2006 added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
2010 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
2014 * Cancel at least \a nr locks from given namespace LRU.
2016 * When called with LCF_ASYNC the blocking callback will be handled
2017 * in a thread and this function will return after the thread has been
2018 * asked to call the callback. When called with LCF_ASYNC the blocking
2019 * callback will be performed in this function.
2021 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
2022 enum ldlm_cancel_flags cancel_flags,
2023 enum ldlm_lru_flags lru_flags)
2025 struct list_head cancels = LIST_HEAD_INIT(cancels);
2029 /* Just prepare the list of locks, do not actually cancel them yet.
2030 * Locks are cancelled later in a separate thread. */
2031 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
2032 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
2040 * Find and cancel locally unused locks found on resource, matched to the
2041 * given policy, mode. GET the found locks and add them into the \a cancels
2044 int ldlm_cancel_resource_local(struct ldlm_resource *res,
2045 struct list_head *cancels,
2046 union ldlm_policy_data *policy,
2047 enum ldlm_mode mode, __u64 lock_flags,
2048 enum ldlm_cancel_flags cancel_flags,
2051 struct ldlm_lock *lock;
2057 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
2058 if (opaque != NULL && lock->l_ast_data != opaque) {
2059 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
2060 lock->l_ast_data, opaque);
2064 if (lock->l_readers || lock->l_writers)
2067 /* If somebody is already doing CANCEL, or blocking AST came,
2068 * or lock is being converted then skip this lock. */
2069 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock) ||
2070 ldlm_is_converting(lock))
2073 if (lockmode_compat(lock->l_granted_mode, mode))
2076 /* If policy is given and this is IBITS lock, add to list only
2077 * those locks that match by policy.
2078 * Skip locks with DoM bit always to don't flush data.
2080 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
2081 (!(lock->l_policy_data.l_inodebits.bits &
2082 policy->l_inodebits.bits) || ldlm_has_dom(lock)))
2085 /* See CBPENDING comment in ldlm_cancel_lru */
2086 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
2089 LASSERT(list_empty(&lock->l_bl_ast));
2090 list_add(&lock->l_bl_ast, cancels);
2091 LDLM_LOCK_GET(lock);
2096 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
2098 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2101 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
2103 * If \a req is NULL, send CANCEL request to server with handles of locks
2104 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
2105 * separately per lock.
2106 * If \a req is not NULL, put handles of locks in \a cancels into the request
2107 * buffer at the offset \a off.
2108 * Destroy \a cancels at the end.
2110 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
2111 struct ptlrpc_request *req,
2112 enum ldlm_cancel_flags flags)
2114 struct ldlm_lock *lock;
2118 if (list_empty(cancels) || count == 0)
2121 /* XXX: requests (both batched and not) could be sent in parallel.
2122 * Usually it is enough to have just 1 RPC, but it is possible that
2123 * there are too many locks to be cancelled in LRU or on a resource.
2124 * It would also speed up the case when the server does not support
2127 LASSERT(!list_empty(cancels));
2128 lock = list_entry(cancels->next, struct ldlm_lock,
2130 LASSERT(lock->l_conn_export);
2132 if (exp_connect_cancelset(lock->l_conn_export)) {
2135 ldlm_cancel_pack(req, cancels, count);
2137 res = ldlm_cli_cancel_req(lock->l_conn_export,
2141 res = ldlm_cli_cancel_req(lock->l_conn_export,
2146 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
2147 "ldlm_cli_cancel_list: %d\n", res);
2152 ldlm_lock_list_put(cancels, l_bl_ast, res);
2154 LASSERT(count == 0);
2157 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2160 * Cancel all locks on a resource that have 0 readers/writers.
2162 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
2163 * to notify the server. */
2164 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
2165 const struct ldlm_res_id *res_id,
2166 union ldlm_policy_data *policy,
2167 enum ldlm_mode mode,
2168 enum ldlm_cancel_flags flags, void *opaque)
2170 struct ldlm_resource *res;
2171 struct list_head cancels = LIST_HEAD_INIT(cancels);
2176 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2178 /* This is not a problem. */
2179 CDEBUG(D_INFO, "No resource %llu\n", res_id->name[0]);
2183 LDLM_RESOURCE_ADDREF(res);
2184 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
2185 0, flags | LCF_BL_AST, opaque);
2186 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
2188 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
2191 LDLM_RESOURCE_DELREF(res);
2192 ldlm_resource_putref(res);
2195 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2197 struct ldlm_cli_cancel_arg {
2203 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2204 struct hlist_node *hnode, void *arg)
2206 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2207 struct ldlm_cli_cancel_arg *lc = arg;
2209 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2210 NULL, LCK_MINMODE, lc->lc_flags,
2212 /* must return 0 for hash iteration */
2217 * Cancel all locks on a namespace (or a specific resource, if given)
2218 * that have 0 readers/writers.
2220 * If flags & LCF_LOCAL, throw the locks away without trying
2221 * to notify the server. */
2222 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2223 const struct ldlm_res_id *res_id,
2224 enum ldlm_cancel_flags flags, void *opaque)
2226 struct ldlm_cli_cancel_arg arg = {
2228 .lc_opaque = opaque,
2236 if (res_id != NULL) {
2237 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2241 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2242 ldlm_cli_hash_cancel_unused, &arg, 0);
2247 /* Lock iterators. */
2249 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2252 struct list_head *tmp, *next;
2253 struct ldlm_lock *lock;
2254 int rc = LDLM_ITER_CONTINUE;
2259 RETURN(LDLM_ITER_CONTINUE);
2262 list_for_each_safe(tmp, next, &res->lr_granted) {
2263 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2265 if (iter(lock, closure) == LDLM_ITER_STOP)
2266 GOTO(out, rc = LDLM_ITER_STOP);
2269 list_for_each_safe(tmp, next, &res->lr_waiting) {
2270 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2272 if (iter(lock, closure) == LDLM_ITER_STOP)
2273 GOTO(out, rc = LDLM_ITER_STOP);
2280 struct iter_helper_data {
2281 ldlm_iterator_t iter;
2285 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2287 struct iter_helper_data *helper = closure;
2288 return helper->iter(lock, helper->closure);
2291 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2292 struct hlist_node *hnode, void *arg)
2295 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2297 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2301 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2302 ldlm_iterator_t iter, void *closure)
2305 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2307 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2308 ldlm_res_iter_helper, &helper, 0);
2312 /* non-blocking function to manipulate a lock whose cb_data is being put away.
2313 * return 0: find no resource
2314 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2317 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2318 const struct ldlm_res_id *res_id,
2319 ldlm_iterator_t iter, void *data)
2321 struct ldlm_resource *res;
2325 LASSERTF(ns != NULL, "must pass in namespace\n");
2327 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2331 LDLM_RESOURCE_ADDREF(res);
2332 rc = ldlm_resource_foreach(res, iter, data);
2333 LDLM_RESOURCE_DELREF(res);
2334 ldlm_resource_putref(res);
2337 EXPORT_SYMBOL(ldlm_resource_iterate);
2341 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2343 struct list_head *list = closure;
2345 /* we use l_pending_chain here, because it's unused on clients. */
2346 LASSERTF(list_empty(&lock->l_pending_chain),
2347 "lock %p next %p prev %p\n",
2348 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2349 /* bug 9573: don't replay locks left after eviction, or
2350 * bug 17614: locks being actively cancelled. Get a reference
2351 * on a lock so that it does not disapear under us (e.g. due to cancel)
2353 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_BL_DONE))) {
2354 list_add(&lock->l_pending_chain, list);
2355 LDLM_LOCK_GET(lock);
2358 return LDLM_ITER_CONTINUE;
2361 static int replay_lock_interpret(const struct lu_env *env,
2362 struct ptlrpc_request *req,
2363 struct ldlm_async_args *aa, int rc)
2365 struct ldlm_lock *lock;
2366 struct ldlm_reply *reply;
2367 struct obd_export *exp;
2370 atomic_dec(&req->rq_import->imp_replay_inflight);
2374 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2376 GOTO(out, rc = -EPROTO);
2378 lock = ldlm_handle2lock(&aa->lock_handle);
2380 CERROR("received replay ack for unknown local cookie %#llx"
2381 " remote cookie %#llx from server %s id %s\n",
2382 aa->lock_handle.cookie, reply->lock_handle.cookie,
2383 req->rq_export->exp_client_uuid.uuid,
2384 libcfs_id2str(req->rq_peer));
2385 GOTO(out, rc = -ESTALE);
2388 /* Key change rehash lock in per-export hash with new key */
2389 exp = req->rq_export;
2390 if (exp && exp->exp_lock_hash) {
2391 /* In the function below, .hs_keycmp resolves to
2392 * ldlm_export_lock_keycmp() */
2393 /* coverity[overrun-buffer-val] */
2394 cfs_hash_rehash_key(exp->exp_lock_hash,
2395 &lock->l_remote_handle,
2396 &reply->lock_handle,
2399 lock->l_remote_handle = reply->lock_handle;
2402 LDLM_DEBUG(lock, "replayed lock:");
2403 ptlrpc_import_recovery_state_machine(req->rq_import);
2404 LDLM_LOCK_PUT(lock);
2407 ptlrpc_connect_import(req->rq_import);
2412 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2414 struct ptlrpc_request *req;
2415 struct ldlm_async_args *aa;
2416 struct ldlm_request *body;
2421 /* Bug 11974: Do not replay a lock which is actively being canceled */
2422 if (ldlm_is_bl_done(lock)) {
2423 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2427 /* If this is reply-less callback lock, we cannot replay it, since
2428 * server might have long dropped it, but notification of that event was
2429 * lost by network. (and server granted conflicting lock already) */
2430 if (ldlm_is_cancel_on_block(lock)) {
2431 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2432 ldlm_lock_cancel(lock);
2437 * If granted mode matches the requested mode, this lock is granted.
2439 * If we haven't been granted anything and are on a resource list,
2440 * then we're blocked/waiting.
2442 * If we haven't been granted anything and we're NOT on a resource list,
2443 * then we haven't got a reply yet and don't have a known disposition.
2444 * This happens whenever a lock enqueue is the request that triggers
2447 if (lock->l_granted_mode == lock->l_req_mode)
2448 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2449 else if (!list_empty(&lock->l_res_link))
2450 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2452 flags = LDLM_FL_REPLAY;
2454 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2455 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2459 /* We're part of recovery, so don't wait for it. */
2460 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2462 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2463 ldlm_lock2desc(lock, &body->lock_desc);
2464 body->lock_flags = ldlm_flags_to_wire(flags);
2466 ldlm_lock2handle(lock, &body->lock_handle[0]);
2467 if (lock->l_lvb_len > 0)
2468 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2469 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2471 ptlrpc_request_set_replen(req);
2472 /* notify the server we've replayed all requests.
2473 * also, we mark the request to be put on a dedicated
2474 * queue to be processed after all request replayes.
2476 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2478 LDLM_DEBUG(lock, "replaying lock:");
2480 atomic_inc(&req->rq_import->imp_replay_inflight);
2481 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2482 aa = ptlrpc_req_async_args(req);
2483 aa->lock_handle = body->lock_handle[0];
2484 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2485 ptlrpcd_add_req(req);
2491 * Cancel as many unused locks as possible before replay. since we are
2492 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2495 * Called only in recovery before replaying locks. there is no need to
2496 * replay locks that are unused. since the clients may hold thousands of
2497 * cached unused locks, dropping the unused locks can greatly reduce the
2498 * load on the servers at recovery time.
2500 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2503 struct list_head cancels = LIST_HEAD_INIT(cancels);
2505 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2506 "replay for namespace %s (%d)\n",
2507 ldlm_ns_name(ns), ns->ns_nr_unused);
2509 /* We don't need to care whether or not LRU resize is enabled
2510 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2511 * count parameter */
2512 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2513 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2515 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2516 canceled, ldlm_ns_name(ns));
2519 int ldlm_replay_locks(struct obd_import *imp)
2521 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2522 struct list_head list = LIST_HEAD_INIT(list);
2523 struct ldlm_lock *lock, *next;
2528 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2530 /* don't replay locks if import failed recovery */
2531 if (imp->imp_vbr_failed)
2534 /* ensure this doesn't fall to 0 before all have been queued */
2535 atomic_inc(&imp->imp_replay_inflight);
2537 if (ldlm_cancel_unused_locks_before_replay)
2538 ldlm_cancel_unused_locks_for_replay(ns);
2540 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2542 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2543 list_del_init(&lock->l_pending_chain);
2545 LDLM_LOCK_RELEASE(lock);
2546 continue; /* or try to do the rest? */
2548 rc = replay_one_lock(imp, lock);
2549 LDLM_LOCK_RELEASE(lock);
2552 atomic_dec(&imp->imp_replay_inflight);