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/
32 * This file contains Asynchronous System Trap (AST) handlers and related
33 * LDLM request-processing routines.
35 * An AST is a callback issued on a lock when its state is changed. There are
36 * several different types of ASTs (callbacks) registered for each lock:
38 * - completion AST: when a lock is enqueued by some process, but cannot be
39 * granted immediately due to other conflicting locks on the same resource,
40 * the completion AST is sent to notify the caller when the lock is
43 * - blocking AST: when a lock is granted to some process, if another process
44 * enqueues a conflicting (blocking) lock on a resource, a blocking AST is
45 * sent to notify the holder(s) of the lock(s) of the conflicting lock
46 * request. The lock holder(s) must release their lock(s) on that resource in
47 * a timely manner or be evicted by the server.
49 * - glimpse AST: this is used when a process wants information about a lock
50 * (i.e. the lock value block (LVB)) but does not necessarily require holding
51 * the lock. If the resource is locked, the lock holder(s) are sent glimpse
52 * ASTs and the LVB is returned to the caller, and lock holder(s) may CANCEL
53 * their lock(s) if they are idle. If the resource is not locked, the server
57 #define DEBUG_SUBSYSTEM S_LDLM
59 #include <linux/fs_struct.h>
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");
70 EXPORT_SYMBOL(ldlm_enqueue_min);
72 /* in client side, whether the cached locks will be canceled before replay */
73 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
75 struct lock_wait_data {
76 struct ldlm_lock *lwd_lock;
80 struct ldlm_async_args {
81 struct lustre_handle lock_handle;
85 * ldlm_request_bufsize
87 * If opcode=LDLM_ENQUEUE, 1 slot is already occupied,
88 * LDLM_LOCKREQ_HANDLE -1 slots are available.
89 * Otherwise, LDLM_LOCKREQ_HANDLE slots are available.
94 * \retval size of the request buffer
96 int ldlm_request_bufsize(int count, int type)
98 int avail = LDLM_LOCKREQ_HANDLES;
100 if (type == LDLM_ENQUEUE)
101 avail -= LDLM_ENQUEUE_CANCEL_OFF;
104 avail = (count - avail) * sizeof(struct lustre_handle);
108 return sizeof(struct ldlm_request) + avail;
111 void ldlm_expired_completion_wait(struct lock_wait_data *lwd)
113 struct ldlm_lock *lock = lwd->lwd_lock;
114 struct obd_import *imp;
115 struct obd_device *obd;
118 if (lock->l_conn_export == NULL) {
119 static time64_t next_dump, last_dump;
122 "lock timed out (enqueued at %lld, %llds ago); not entering recovery in server code, just going back to sleep",
124 ktime_get_real_seconds() - lock->l_activity);
125 if (ktime_get_seconds() > next_dump) {
126 last_dump = next_dump;
127 next_dump = ktime_get_seconds() + 300;
128 ldlm_namespace_dump(D_DLMTRACE,
129 ldlm_lock_to_ns(lock));
131 libcfs_debug_dumplog();
136 obd = lock->l_conn_export->exp_obd;
137 imp = obd->u.cli.cl_import;
138 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
140 "lock timed out (enqueued at %lld, %llds ago), entering recovery for %s@%s",
142 ktime_get_real_seconds() - lock->l_activity,
143 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
148 int is_granted_or_cancelled_nolock(struct ldlm_lock *lock)
152 check_res_locked(lock->l_resource);
153 if (ldlm_is_granted(lock) && !ldlm_is_cp_reqd(lock))
155 else if (ldlm_is_failed(lock) || ldlm_is_cancel(lock))
159 EXPORT_SYMBOL(is_granted_or_cancelled_nolock);
162 * Calculate the Completion timeout (covering enqueue, BL AST, data flush,
163 * lock cancel, and their replies). Used for lock completion timeout on the
166 * \param[in] lock lock which is waiting the completion callback
168 * \retval timeout in seconds to wait for the server reply
171 * We use the same basis for both server side and client side functions
172 * from a single node.
174 static timeout_t ldlm_cp_timeout(struct ldlm_lock *lock)
182 * Wait a long time for enqueue - server may have to callback a
183 * lock from another client. Server will evict the other client if it
184 * doesn't respond reasonably, and then give us the lock.
186 timeout = at_get(ldlm_lock_to_ns_at(lock));
187 return max(3 * timeout, (timeout_t)ldlm_enqueue_min);
191 * Helper function for ldlm_completion_ast(), updating timings when lock is
194 static int ldlm_completion_tail(struct ldlm_lock *lock, void *data)
198 if (ldlm_is_destroyed(lock) || ldlm_is_failed(lock)) {
199 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
201 } else if (data == NULL) {
202 LDLM_DEBUG(lock, "client-side enqueue: granted");
204 /* Take into AT only CP RPC, not immediately granted locks */
207 /* Discard negative timeouts. We should also limit the
208 * maximum value of the timeout
210 if (ktime_get_real_seconds() > lock->l_activity)
211 delay = ktime_get_real_seconds() - lock->l_activity;
213 LDLM_DEBUG(lock, "client-side enqueue: granted after %ds",
215 /* Update our time estimate */
216 at_measured(ldlm_lock_to_ns_at(lock), delay);
222 * Implementation of ->l_completion_ast() for a client, that doesn't wait
223 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
224 * other threads that cannot block for long.
226 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data)
230 if (flags == LDLM_FL_WAIT_NOREPROC) {
231 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
235 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
236 wake_up(&lock->l_waitq);
237 RETURN(ldlm_completion_tail(lock, data));
240 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, going forward");
241 ldlm_reprocess_all(lock->l_resource, 0);
244 EXPORT_SYMBOL(ldlm_completion_ast_async);
247 * Generic LDLM "completion" AST. This is called in several cases:
249 * - when a reply to an ENQUEUE RPC is received from the server
250 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
251 * this point (determined by flags);
253 * - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
256 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
259 * - to force all locks when resource is destroyed (cleanup_resource());
261 * If lock is not granted in the first case, this function waits until second
262 * or penultimate cases happen in some other thread.
265 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
267 /* XXX ALLOCATE - 160 bytes */
268 struct lock_wait_data lwd;
269 struct obd_device *obd;
270 struct obd_import *imp = NULL;
276 if (flags == LDLM_FL_WAIT_NOREPROC) {
277 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
281 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
282 wake_up(&lock->l_waitq);
286 LDLM_DEBUG(lock, "client-side enqueue returned a blocked locksleeping");
290 obd = class_exp2obd(lock->l_conn_export);
292 /* if this is a local lock, then there is no import */
294 imp = obd->u.cli.cl_import;
296 timeout = ldlm_cp_timeout(lock);
299 lock->l_activity = ktime_get_real_seconds();
302 spin_lock(&imp->imp_lock);
303 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
304 spin_unlock(&imp->imp_lock);
307 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
308 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
309 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
310 ldlm_set_fail_loc(lock);
313 /* Go to sleep until the lock is granted or cancelled. */
314 if (ldlm_is_no_timeout(lock)) {
315 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
316 rc = l_wait_event_abortable(
318 is_granted_or_cancelled(lock));
320 if (wait_event_idle_timeout(
322 is_granted_or_cancelled(lock),
323 cfs_time_seconds(timeout)) == 0) {
324 ldlm_expired_completion_wait(&lwd);
325 rc = l_wait_event_abortable(
327 is_granted_or_cancelled(lock));
333 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
338 RETURN(ldlm_completion_tail(lock, data));
340 EXPORT_SYMBOL(ldlm_completion_ast);
343 * A helper to build a blocking AST function
345 * Perform a common operation for blocking ASTs:
346 * defferred lock cancellation.
348 * \param lock the lock blocking or canceling AST was called on
350 * \see mdt_blocking_ast
351 * \see ldlm_blocking_ast
353 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
359 ldlm_set_cbpending(lock);
360 do_ast = (!lock->l_readers && !lock->l_writers);
361 unlock_res_and_lock(lock);
364 struct lustre_handle lockh;
367 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
368 ldlm_lock2handle(lock, &lockh);
369 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
371 CERROR("ldlm_cli_cancel: %d\n", rc);
374 "Lock still has references, will be cancelled later");
378 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
381 * Server blocking AST
383 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
386 * \param lock the lock which blocks a request or cancelling lock
389 * \param flag indicates whether this cancelling or blocking callback
391 * \see ldlm_blocking_ast_nocheck
393 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
394 void *data, int flag)
398 if (flag == LDLM_CB_CANCELING) {
399 /* Don't need to do anything here. */
403 lock_res_and_lock(lock);
405 * Get this: if ldlm_blocking_ast is racing with intent_policy, such
406 * that ldlm_blocking_ast is called just before intent_policy method
407 * takes the lr_lock, then by the time we get the lock, we might not
408 * be the correct blocking function anymore. So check, and return
411 if (lock->l_blocking_ast != ldlm_blocking_ast) {
412 unlock_res_and_lock(lock);
415 RETURN(ldlm_blocking_ast_nocheck(lock));
417 EXPORT_SYMBOL(ldlm_blocking_ast);
420 * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
422 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
423 * rather subtle: with OST-side locking, it may so happen that _all_ extent
424 * locks are held by the OST. If client wants to obtain the current file size
425 * it calls ll_glimpse_size(), and (as all locks are held only on the server),
426 * this dummy glimpse callback fires and does nothing. The client still
427 * receives the correct file size due to the following fragment of code in
428 * ldlm_cb_interpret():
430 * if (rc == -ELDLM_NO_LOCK_DATA) {
431 * LDLM_DEBUG(lock, "lost race - client has a lock but no"
433 * ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
436 * That is, after the glimpse returns this error, ofd_lvbo_update() is called
437 * and returns the updated file attributes from the inode to the client.
439 * See also comment in ofd_intent_policy() on why servers must set a non-NULL
440 * l_glimpse_ast when grabbing DLM locks. Otherwise, the server will assume
441 * that the object is in the process of being destroyed.
443 * \param[in] lock DLM lock being glimpsed, unused
444 * \param[in] reqp pointer to ptlrpc_request, unused
446 * \retval -ELDLM_NO_LOCK_DATA to get attributes from disk object
448 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
450 return -ELDLM_NO_LOCK_DATA;
454 * Enqueue a local lock (typically on a server).
456 int ldlm_cli_enqueue_local(const struct lu_env *env,
457 struct ldlm_namespace *ns,
458 const struct ldlm_res_id *res_id,
459 enum ldlm_type type, union ldlm_policy_data *policy,
460 enum ldlm_mode mode, __u64 *flags,
461 ldlm_blocking_callback blocking,
462 ldlm_completion_callback completion,
463 ldlm_glimpse_callback glimpse,
464 void *data, __u32 lvb_len, enum lvb_type lvb_type,
465 const __u64 *client_cookie,
466 struct lustre_handle *lockh)
468 struct ldlm_lock *lock;
470 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
471 .lcs_blocking = blocking,
472 .lcs_glimpse = glimpse,
477 LASSERT(!(*flags & LDLM_FL_REPLAY));
478 if (unlikely(ns_is_client(ns))) {
479 CERROR("Trying to enqueue local lock in a shadow namespace\n");
483 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
486 GOTO(out_nolock, err = PTR_ERR(lock));
488 err = ldlm_lvbo_init(lock->l_resource);
490 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
491 ldlm_lock_destroy_nolock(lock);
495 ldlm_lock2handle(lock, lockh);
498 * NB: we don't have any lock now (lock_res_and_lock)
499 * because it's a new lock
501 ldlm_lock_addref_internal_nolock(lock, mode);
502 ldlm_set_local(lock);
503 if (*flags & LDLM_FL_ATOMIC_CB)
504 ldlm_set_atomic_cb(lock);
506 if (*flags & LDLM_FL_CANCEL_ON_BLOCK)
507 ldlm_set_cancel_on_block(lock);
510 lock->l_policy_data = *policy;
511 if (client_cookie != NULL)
512 lock->l_client_cookie = *client_cookie;
513 if (type == LDLM_EXTENT) {
514 /* extent lock without policy is a bug */
518 lock->l_req_extent = policy->l_extent;
521 err = ldlm_lock_enqueue(env, ns, &lock, policy, flags);
522 if (unlikely(err != ELDLM_OK))
526 *policy = lock->l_policy_data;
528 if (lock->l_completion_ast)
529 lock->l_completion_ast(lock, *flags, NULL);
531 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
534 LDLM_LOCK_RELEASE(lock);
538 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
540 static void failed_lock_cleanup(struct ldlm_namespace *ns,
541 struct ldlm_lock *lock, int mode)
545 /* Set a flag to prevent us from sending a CANCEL (b=407) */
546 lock_res_and_lock(lock);
547 /* Check that lock is not granted or failed, we might race. */
548 if (!ldlm_is_granted(lock) && !ldlm_is_failed(lock)) {
550 * Make sure that this lock will not be found by raced
551 * bl_ast and -EINVAL reply is sent to server anyways.
554 lock->l_flags |= LDLM_FL_FAILED |
555 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
556 if (!(ldlm_is_bl_ast(lock) &&
557 lock->l_remote_handle.cookie != 0))
558 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
561 unlock_res_and_lock(lock);
565 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
567 LDLM_DEBUG(lock, "lock was granted or failed in race");
570 * XXX - HACK because we shouldn't call ldlm_lock_destroy()
571 * from llite/file.c/ll_file_flock().
574 * This code makes for the fact that we do not have blocking handler on
575 * a client for flock locks. As such this is the place where we must
576 * completely kill failed locks. (interrupted and those that
577 * were waiting to be granted when server evicted us.
579 if (lock->l_resource->lr_type == LDLM_FLOCK) {
580 lock_res_and_lock(lock);
581 if (!ldlm_is_destroyed(lock)) {
582 ldlm_resource_unlink_lock(lock);
583 ldlm_lock_decref_internal_nolock(lock, mode);
584 ldlm_lock_destroy_nolock(lock);
586 unlock_res_and_lock(lock);
588 ldlm_lock_decref_internal(lock, mode);
592 static bool ldlm_request_slot_needed(struct ldlm_enqueue_info *einfo)
594 /* exclude EXTENT locks and DOM-only IBITS locks because they
595 * are asynchronous and don't wait on server being blocked.
597 return einfo->ei_req_slot &&
598 (einfo->ei_type == LDLM_FLOCK ||
599 (einfo->ei_type == LDLM_IBITS &&
600 einfo->ei_inodebits != MDS_INODELOCK_DOM));
604 * Finishing portion of client lock enqueue code.
606 * Called after receiving reply from server.
608 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
609 struct ldlm_enqueue_info *einfo,
610 __u8 with_policy, __u64 *ldlm_flags, void *lvb,
611 __u32 lvb_len, const struct lustre_handle *lockh,
612 int rc, bool request_slot)
614 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
615 const struct lu_env *env = NULL;
616 int is_replay = *ldlm_flags & LDLM_FL_REPLAY;
617 struct ldlm_lock *lock;
618 struct ldlm_reply *reply;
619 int cleanup_phase = 1;
624 obd_put_request_slot(&req->rq_import->imp_obd->u.cli);
626 ptlrpc_put_mod_rpc_slot(req);
628 if (req && req->rq_svc_thread)
629 env = req->rq_svc_thread->t_env;
631 lock = ldlm_handle2lock(lockh);
632 /* ldlm_cli_enqueue is holding a reference on this lock. */
634 LASSERT(einfo->ei_type == LDLM_FLOCK);
638 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
639 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
641 if (rc != ELDLM_OK) {
643 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
644 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
646 if (rc != ELDLM_LOCK_ABORTED)
650 /* Before we return, swab the reply */
651 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
653 GOTO(cleanup, rc = -EPROTO);
658 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
661 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
662 GOTO(cleanup, rc = size);
663 } else if (unlikely(size > lvb_len)) {
665 "Replied LVB is larger than expectation, expected = %d, replied = %d",
667 GOTO(cleanup, rc = -EINVAL);
672 if (rc == ELDLM_LOCK_ABORTED) {
673 if (lvb_len > 0 && lvb != NULL)
674 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
676 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
679 /* lock enqueued on the server */
682 lock_res_and_lock(lock);
683 /* Key change rehash lock in per-export hash with new key */
684 if (exp->exp_lock_hash) {
686 * In the function below, .hs_keycmp resolves to
687 * ldlm_export_lock_keycmp()
689 /* coverity[overrun-buffer-val] */
690 cfs_hash_rehash_key(exp->exp_lock_hash,
691 &lock->l_remote_handle,
695 lock->l_remote_handle = reply->lock_handle;
698 *ldlm_flags = ldlm_flags_from_wire(reply->lock_flags);
699 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
700 LDLM_FL_INHERIT_MASK);
701 unlock_res_and_lock(lock);
703 CDEBUG(D_INFO, "local: %p, remote cookie: %#llx, flags: %#llx\n",
704 lock, reply->lock_handle.cookie, *ldlm_flags);
707 * If enqueue returned a blocked lock but the completion handler has
708 * already run, then it fixed up the resource and we don't need to do it
711 if ((*ldlm_flags) & LDLM_FL_LOCK_CHANGED) {
712 int newmode = reply->lock_desc.l_req_mode;
715 if (newmode && newmode != lock->l_req_mode) {
716 LDLM_DEBUG(lock, "server returned different mode %s",
717 ldlm_lockname[newmode]);
718 lock->l_req_mode = newmode;
721 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
722 &lock->l_resource->lr_name)) {
724 "remote intent success, locking "DLDLMRES", instead of "DLDLMRES"\n",
725 PLDLMRES(&reply->lock_desc.l_resource),
726 PLDLMRES(lock->l_resource));
728 rc = ldlm_lock_change_resource(ns, lock,
729 &reply->lock_desc.l_resource.lr_name);
730 if (rc || lock->l_resource == NULL)
731 GOTO(cleanup, rc = -ENOMEM);
732 LDLM_DEBUG(lock, "client-side enqueue, new resource");
736 /* We assume lock type cannot change on server*/
737 ldlm_convert_policy_to_local(exp,
738 lock->l_resource->lr_type,
739 &reply->lock_desc.l_policy_data,
740 &lock->l_policy_data);
743 if (einfo->ei_type != LDLM_PLAIN)
745 "client-side enqueue, new policy data");
748 if ((*ldlm_flags) & LDLM_FL_AST_SENT) {
749 lock_res_and_lock(lock);
750 ldlm_bl_desc2lock(&reply->lock_desc, lock);
751 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
752 unlock_res_and_lock(lock);
753 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
757 * If the lock has already been granted by a completion AST, don't
758 * clobber the LVB with an older one.
762 * We must lock or a racing completion might update lvb without
763 * letting us know and we'll clobber the correct value.
764 * Cannot unlock after the check either, a that still leaves
765 * a tiny window for completion to get in
767 lock_res_and_lock(lock);
768 if (!ldlm_is_granted(lock))
769 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
770 lock->l_lvb_data, lvb_len);
771 unlock_res_and_lock(lock);
779 rc = ldlm_lock_enqueue(env, ns, &lock, NULL, ldlm_flags);
780 if (lock->l_completion_ast != NULL) {
781 int err = lock->l_completion_ast(lock, *ldlm_flags,
791 if (lvb_len > 0 && lvb != NULL) {
793 * Copy the LVB here, and not earlier, because the completion
794 * AST (if any) can override what we got in the reply
796 memcpy(lvb, lock->l_lvb_data, lvb_len);
799 LDLM_DEBUG(lock, "client-side enqueue END");
802 if (cleanup_phase == 1 && rc)
803 failed_lock_cleanup(ns, lock, einfo->ei_mode);
804 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
806 LDLM_LOCK_RELEASE(lock);
809 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
812 * Estimate number of lock handles that would fit into request of given
813 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
814 * a single page on the send/receive side. XXX: 512 should be changed to
815 * more adequate value.
817 static inline int ldlm_req_handles_avail(int req_size, int off)
821 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
822 if (likely(avail >= 0))
823 avail /= (int)sizeof(struct lustre_handle);
826 avail += LDLM_LOCKREQ_HANDLES - off;
831 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
832 enum req_location loc,
835 __u32 size = req_capsule_msg_size(pill, loc);
837 return ldlm_req_handles_avail(size, off);
840 static inline int ldlm_format_handles_avail(struct obd_import *imp,
841 const struct req_format *fmt,
842 enum req_location loc, int off)
844 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
846 return ldlm_req_handles_avail(size, off);
850 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
851 * \a count locks in \a cancels.
853 * This is to be called by functions preparing their own requests that
854 * might contain lists of locks to cancel in addition to actual operation
855 * that needs to be performed.
857 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
858 int version, int opc, int canceloff,
859 struct list_head *cancels, int count)
861 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
862 struct req_capsule *pill = &req->rq_pill;
863 struct ldlm_request *dlm = NULL;
865 int avail, to_free = 0, pack = 0;
872 if (ns_connect_cancelset(ns)) {
873 /* Estimate the amount of available space in the request. */
874 req_capsule_filled_sizes(pill, RCL_CLIENT);
875 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
877 /* If we have reached the limit, free +1 slot for the new one */
878 if (!ns_connect_lru_resize(ns) && opc == LDLM_ENQUEUE &&
879 ns->ns_nr_unused >= ns->ns_max_unused)
883 * Cancel LRU locks here _only_ if the server supports
884 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
885 * RPC, which will make us slower.
888 count += ldlm_cancel_lru_local(ns, cancels, to_free,
890 LDLM_LRU_FLAG_NO_WAIT);
895 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
896 ldlm_request_bufsize(pack, opc));
899 rc = ptlrpc_request_pack(req, version, opc);
901 ldlm_lock_list_put(cancels, l_bl_ast, count);
905 if (ns_connect_cancelset(ns)) {
907 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
910 * Skip first lock handler in ldlm_request_pack(),
911 * this method will increment @lock_count according
912 * to the lock handle amount actually written to
915 dlm->lock_count = canceloff;
917 /* Pack into the request @pack lock handles. */
918 ldlm_cli_cancel_list(cancels, pack, req, 0);
919 /* Prepare and send separate cancel RPC for others. */
920 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
922 ldlm_lock_list_put(cancels, l_bl_ast, count);
926 EXPORT_SYMBOL(ldlm_prep_elc_req);
928 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
929 struct list_head *cancels, int count)
931 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
932 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
934 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
936 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
938 struct ptlrpc_request *req;
943 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
945 RETURN(ERR_PTR(-ENOMEM));
947 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
949 ptlrpc_request_free(req);
953 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
954 ptlrpc_request_set_replen(req);
957 EXPORT_SYMBOL(ldlm_enqueue_pack);
960 * Client-side lock enqueue.
962 * If a request has some specific initialisation it is passed in \a reqp,
963 * otherwise it is created in ldlm_cli_enqueue.
965 * Supports sync and async requests, pass \a async flag accordingly. If a
966 * request was created in ldlm_cli_enqueue and it is the async request,
967 * pass it to the caller in \a reqp.
969 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
970 struct ldlm_enqueue_info *einfo,
971 const struct ldlm_res_id *res_id,
972 union ldlm_policy_data const *policy, __u64 *flags,
973 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
974 struct lustre_handle *lockh, int async)
976 struct ldlm_namespace *ns;
977 struct ldlm_lock *lock;
978 struct ldlm_request *body;
979 int is_replay = *flags & LDLM_FL_REPLAY;
980 int req_passed_in = 1;
983 struct ptlrpc_request *req;
987 LASSERT(exp != NULL);
989 ns = exp->exp_obd->obd_namespace;
992 * If we're replaying this lock, just check some invariants.
993 * If we're creating a new lock, get everything all setup nice.
996 lock = ldlm_handle2lock_long(lockh, 0);
997 LASSERT(lock != NULL);
998 LDLM_DEBUG(lock, "client-side enqueue START");
999 LASSERT(exp == lock->l_conn_export);
1001 const struct ldlm_callback_suite cbs = {
1002 .lcs_completion = einfo->ei_cb_cp,
1003 .lcs_blocking = einfo->ei_cb_bl,
1004 .lcs_glimpse = einfo->ei_cb_gl
1006 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
1007 einfo->ei_mode, &cbs, einfo->ei_cbdata,
1010 RETURN(PTR_ERR(lock));
1012 if (einfo->ei_cb_created)
1013 einfo->ei_cb_created(lock);
1015 /* for the local lock, add the reference */
1016 ldlm_lock_addref_internal(lock, einfo->ei_mode);
1017 ldlm_lock2handle(lock, lockh);
1019 lock->l_policy_data = *policy;
1021 if (einfo->ei_type == LDLM_EXTENT) {
1022 /* extent lock without policy is a bug */
1026 lock->l_req_extent = policy->l_extent;
1028 LDLM_DEBUG(lock, "client-side enqueue START, flags %#llx",
1032 lock->l_conn_export = exp;
1033 lock->l_export = NULL;
1034 lock->l_blocking_ast = einfo->ei_cb_bl;
1035 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL));
1036 lock->l_activity = ktime_get_real_seconds();
1038 /* lock not sent to server yet */
1039 if (reqp == NULL || *reqp == NULL) {
1040 req = ldlm_enqueue_pack(exp, lvb_len);
1042 failed_lock_cleanup(ns, lock, einfo->ei_mode);
1043 LDLM_LOCK_RELEASE(lock);
1044 RETURN(PTR_ERR(req));
1054 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
1056 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
1057 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
1060 if (*flags & LDLM_FL_NDELAY) {
1061 DEBUG_REQ(D_DLMTRACE, req, "enqueue lock with no delay");
1062 req->rq_no_resend = req->rq_no_delay = 1;
1064 * probably set a shorter timeout value and handle ETIMEDOUT
1065 * in osc_lock_upcall() correctly
1067 /* lustre_msg_set_timeout(req, req->rq_timeout / 2); */
1070 /* Dump lock data into the request buffer */
1071 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1072 ldlm_lock2desc(lock, &body->lock_desc);
1073 body->lock_flags = ldlm_flags_to_wire(*flags);
1074 body->lock_handle[0] = *lockh;
1076 /* extended LDLM opcodes in client stats */
1077 if (exp->exp_obd->obd_svc_stats != NULL) {
1078 /* glimpse is intent with no intent buffer */
1079 if (*flags & LDLM_FL_HAS_INTENT &&
1080 !req_capsule_has_field(&req->rq_pill, &RMF_LDLM_INTENT,
1082 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1084 LDLM_GLIMPSE_ENQUEUE);
1086 ldlm_svc_get_eopc(body, exp->exp_obd->obd_svc_stats);
1089 /* It is important to obtain modify RPC slot first (if applicable), so
1090 * that threads that are waiting for a modify RPC slot are not polluting
1091 * our rpcs in flight counter. */
1093 if (einfo->ei_mod_slot)
1094 ptlrpc_get_mod_rpc_slot(req);
1096 need_req_slot = ldlm_request_slot_needed(einfo);
1098 if (need_req_slot) {
1099 rc = obd_get_request_slot(&req->rq_import->imp_obd->u.cli);
1101 if (einfo->ei_mod_slot)
1102 ptlrpc_put_mod_rpc_slot(req);
1103 failed_lock_cleanup(ns, lock, einfo->ei_mode);
1104 LDLM_LOCK_RELEASE(lock);
1106 ptlrpc_req_finished(req);
1112 LASSERT(reqp != NULL);
1116 LDLM_DEBUG(lock, "sending request");
1118 rc = ptlrpc_queue_wait(req);
1120 err = ldlm_cli_enqueue_fini(exp, req, einfo, policy ? 1 : 0, flags,
1121 lvb, lvb_len, lockh, rc, need_req_slot);
1124 * If ldlm_cli_enqueue_fini did not find the lock, we need to free
1125 * one reference that we took
1128 LDLM_LOCK_RELEASE(lock);
1133 if (!req_passed_in && req != NULL) {
1134 ptlrpc_req_finished(req);
1141 EXPORT_SYMBOL(ldlm_cli_enqueue);
1144 * Client-side IBITS lock convert.
1146 * Inform server that lock has been converted instead of canceling.
1147 * Server finishes convert on own side and does reprocess to grant
1148 * all related waiting locks.
1150 * Since convert means only ibits downgrading, client doesn't need to
1151 * wait for server reply to finish local converting process so this request
1152 * is made asynchronous.
1155 int ldlm_cli_convert_req(struct ldlm_lock *lock, __u32 *flags, __u64 new_bits)
1157 struct ldlm_request *body;
1158 struct ptlrpc_request *req;
1159 struct obd_export *exp = lock->l_conn_export;
1163 LASSERT(exp != NULL);
1166 * this is better to check earlier and it is done so already,
1167 * but this check is kept too as final one to issue an error
1168 * if any new code will miss such check.
1170 if (!exp_connect_lock_convert(exp)) {
1171 LDLM_ERROR(lock, "server doesn't support lock convert\n");
1175 if (lock->l_resource->lr_type != LDLM_IBITS) {
1176 LDLM_ERROR(lock, "convert works with IBITS locks only.");
1180 LDLM_DEBUG(lock, "client-side convert");
1182 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1183 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1188 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1189 body->lock_handle[0] = lock->l_remote_handle;
1191 body->lock_desc.l_req_mode = lock->l_req_mode;
1192 body->lock_desc.l_granted_mode = lock->l_granted_mode;
1194 body->lock_desc.l_policy_data.l_inodebits.bits = new_bits;
1195 body->lock_desc.l_policy_data.l_inodebits.cancel_bits = 0;
1197 body->lock_flags = ldlm_flags_to_wire(*flags);
1198 body->lock_count = 1;
1200 ptlrpc_request_set_replen(req);
1203 * Use cancel portals for convert as well as high-priority handling.
1205 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1206 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1208 ptlrpc_at_set_req_timeout(req);
1210 if (exp->exp_obd->obd_svc_stats != NULL)
1211 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1212 LDLM_CONVERT - LDLM_FIRST_OPC);
1214 ptlrpcd_add_req(req);
1219 * Cancel locks locally.
1221 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1222 * \retval LDLM_FL_CANCELING otherwise;
1223 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1225 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1227 __u64 rc = LDLM_FL_LOCAL_ONLY;
1231 if (lock->l_conn_export) {
1234 LDLM_DEBUG(lock, "client-side cancel");
1235 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL_LOCAL,
1238 /* Set this flag to prevent others from getting new references*/
1239 lock_res_and_lock(lock);
1240 ldlm_set_cbpending(lock);
1241 local_only = !!(lock->l_flags &
1242 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1243 ldlm_cancel_callback(lock);
1244 rc = (ldlm_is_bl_ast(lock)) ?
1245 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1246 unlock_res_and_lock(lock);
1250 "not sending request (at caller's instruction)\n");
1251 rc = LDLM_FL_LOCAL_ONLY;
1253 ldlm_lock_cancel(lock);
1255 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1256 LDLM_ERROR(lock, "Trying to cancel local lock");
1259 LDLM_DEBUG(lock, "server-side local cancel");
1260 ldlm_lock_cancel(lock);
1261 ldlm_reprocess_all(lock->l_resource,
1262 lock->l_policy_data.l_inodebits.bits);
1268 static inline int __ldlm_pack_lock(struct ldlm_lock *lock,
1269 struct ldlm_request *dlm)
1271 LASSERT(lock->l_conn_export);
1272 lock_res_and_lock(lock);
1273 if (ldlm_is_ast_sent(lock)) {
1274 unlock_res_and_lock(lock);
1277 ldlm_set_ast_sent(lock);
1278 unlock_res_and_lock(lock);
1280 /* Pack the lock handle to the given request buffer. */
1281 LDLM_DEBUG(lock, "packing");
1282 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1286 #define ldlm_cancel_pack(req, head, count) \
1287 _ldlm_cancel_pack(req, NULL, head, count)
1289 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1291 static int _ldlm_cancel_pack(struct ptlrpc_request *req, struct ldlm_lock *lock,
1292 struct list_head *head, int count)
1294 struct ldlm_request *dlm;
1295 int max, packed = 0;
1299 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1300 LASSERT(dlm != NULL);
1302 /* Check the room in the request buffer. */
1303 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1304 sizeof(struct ldlm_request);
1305 max /= sizeof(struct lustre_handle);
1306 max += LDLM_LOCKREQ_HANDLES;
1307 LASSERT(max >= dlm->lock_count + count);
1310 * XXX: it would be better to pack lock handles grouped by resource.
1311 * so that the server cancel would call filter_lvbo_update() less
1314 if (lock) { /* only pack one lock */
1315 packed = __ldlm_pack_lock(lock, dlm);
1317 list_for_each_entry(lock, head, l_bl_ast) {
1320 packed += __ldlm_pack_lock(lock, dlm);
1323 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1328 * Prepare and send a batched cancel RPC. It will include \a count lock
1329 * handles of locks given in \a cancels list.
1331 int ldlm_cli_cancel_req(struct obd_export *exp, void *ptr,
1332 int count, enum ldlm_cancel_flags flags)
1334 struct ptlrpc_request *req = NULL;
1335 struct obd_import *imp;
1341 LASSERT(exp != NULL);
1344 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1346 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1349 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1350 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1355 imp = class_exp2cliimp(exp);
1356 if (imp == NULL || imp->imp_invalid) {
1358 "skipping cancel on invalid import %p\n", imp);
1362 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1364 GOTO(out, rc = -ENOMEM);
1366 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1367 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1368 ldlm_request_bufsize(count, LDLM_CANCEL));
1370 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1372 ptlrpc_request_free(req);
1377 * If OSP want cancel cross-MDT lock, let's not block it in
1378 * in recovery, otherwise the lock will not released, if
1379 * the remote target is also in recovery, and it also need
1380 * this lock, it might cause deadlock.
1382 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1383 exp->exp_obd->obd_lu_dev != NULL &&
1384 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1385 struct lu_device *top_dev;
1387 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1388 if (top_dev != NULL &&
1389 top_dev->ld_obd->obd_recovering)
1390 req->rq_allow_replay = 1;
1393 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1394 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1395 ptlrpc_at_set_req_timeout(req);
1397 if (flags & LCF_ONE_LOCK)
1398 rc = _ldlm_cancel_pack(req, ptr, NULL, count);
1400 rc = _ldlm_cancel_pack(req, NULL, ptr, count);
1402 ptlrpc_req_finished(req);
1407 ptlrpc_request_set_replen(req);
1408 if (flags & LCF_ASYNC) {
1409 ptlrpcd_add_req(req);
1414 rc = ptlrpc_queue_wait(req);
1415 if (rc == LUSTRE_ESTALE) {
1417 "client/server (nid %s) out of sync -- not fatal\n",
1418 libcfs_nidstr(&req->rq_import->imp_connection->c_peer.nid));
1420 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1421 req->rq_import_generation == imp->imp_generation) {
1422 ptlrpc_req_finished(req);
1424 } else if (rc != ELDLM_OK) {
1425 /* -ESHUTDOWN is common on umount */
1426 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1427 "Got rc %d from cancel RPC: canceling anyway\n",
1435 ptlrpc_req_finished(req);
1438 return sent ? sent : rc;
1441 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1443 LASSERT(imp != NULL);
1444 return &imp->imp_obd->obd_namespace->ns_pool;
1448 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1450 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1452 struct ldlm_namespace *ns;
1453 struct obd_device *obd;
1454 __u64 new_slv, ratio;
1458 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1459 !imp_connect_lru_resize(req->rq_import)))
1460 /* Do nothing for corner cases. */
1464 * In some cases RPC may contain SLV and limit zeroed out. This
1465 * is the case when server does not support LRU resize feature.
1466 * This is also possible in some recovery cases when server-side
1467 * reqs have no reference to the OBD export and thus access to
1468 * server-side namespace is not possible.
1470 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1471 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1472 DEBUG_REQ(D_HA, req,
1473 "Zero SLV or limit found (SLV=%llu, limit=%u)",
1474 lustre_msg_get_slv(req->rq_repmsg),
1475 lustre_msg_get_limit(req->rq_repmsg));
1479 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1480 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1481 obd = req->rq_import->imp_obd;
1483 read_lock(&obd->obd_pool_lock);
1484 if (obd->obd_pool_slv == new_slv &&
1485 obd->obd_pool_limit == new_limit) {
1486 read_unlock(&obd->obd_pool_lock);
1489 read_unlock(&obd->obd_pool_lock);
1492 * OBD device keeps the new pool attributes before they are handled by
1495 write_lock(&obd->obd_pool_lock);
1496 obd->obd_pool_slv = new_slv;
1497 obd->obd_pool_limit = new_limit;
1498 write_unlock(&obd->obd_pool_lock);
1501 * Check if an urgent pool recalc is needed, let it to be a change of
1502 * SLV on 10%. It is applicable to LRU resize enabled case only.
1504 ns = obd->obd_namespace;
1505 if (!ns_connect_lru_resize(ns) ||
1506 ldlm_pool_get_slv(&ns->ns_pool) < new_slv)
1509 ratio = 100 * new_slv / ldlm_pool_get_slv(&ns->ns_pool);
1510 if (100 - ratio >= ns->ns_recalc_pct &&
1511 !ns->ns_stopping && !ns->ns_rpc_recalc) {
1512 bool recalc = false;
1514 spin_lock(&ns->ns_lock);
1515 if (!ns->ns_stopping && !ns->ns_rpc_recalc) {
1516 ldlm_namespace_get(ns);
1518 ns->ns_rpc_recalc = 1;
1520 spin_unlock(&ns->ns_lock);
1522 ldlm_bl_to_thread_ns(ns);
1528 int ldlm_cli_convert(struct ldlm_lock *lock,
1529 enum ldlm_cancel_flags cancel_flags)
1533 LASSERT(!lock->l_readers && !lock->l_writers);
1534 LDLM_DEBUG(lock, "client lock convert START");
1536 if (lock->l_resource->lr_type == LDLM_IBITS) {
1537 lock_res_and_lock(lock);
1539 rc = ldlm_cli_inodebits_convert(lock, cancel_flags);
1540 } while (rc == -EAGAIN);
1541 unlock_res_and_lock(lock);
1544 LDLM_DEBUG(lock, "client lock convert END");
1547 EXPORT_SYMBOL(ldlm_cli_convert);
1550 * Client side lock cancel.
1552 * Lock must not have any readers or writers by this time.
1554 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1555 enum ldlm_cancel_flags flags)
1557 struct obd_export *exp;
1558 int avail, count = 1, bl_ast = 0;
1560 struct ldlm_namespace *ns;
1561 struct ldlm_lock *lock;
1566 lock = ldlm_handle2lock_long(lockh, 0);
1568 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1572 lock_res_and_lock(lock);
1573 LASSERT(!ldlm_is_converting(lock));
1575 if (ldlm_is_bl_ast(lock)) {
1576 if (ldlm_is_ast_sent(lock)) {
1577 unlock_res_and_lock(lock);
1578 LDLM_LOCK_RELEASE(lock);
1582 } else if (ldlm_is_canceling(lock)) {
1583 /* Lock is being canceled and the caller doesn't want to wait */
1584 if (flags & LCF_ASYNC) {
1585 unlock_res_and_lock(lock);
1587 unlock_res_and_lock(lock);
1588 wait_event_idle(lock->l_waitq, is_bl_done(lock));
1590 LDLM_LOCK_RELEASE(lock);
1594 ldlm_set_canceling(lock);
1595 unlock_res_and_lock(lock);
1597 if (flags & LCF_LOCAL)
1598 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE,
1601 rc = ldlm_cli_cancel_local(lock);
1602 if (rc == LDLM_FL_LOCAL_ONLY || flags & LCF_LOCAL) {
1603 LDLM_LOCK_RELEASE(lock);
1607 exp = lock->l_conn_export;
1608 if (bl_ast) { /* Send RPC immedaitly for LDLM_FL_BL_AST */
1609 ldlm_cli_cancel_req(exp, lock, count, flags | LCF_ONE_LOCK);
1610 LDLM_LOCK_RELEASE(lock);
1614 LASSERT(list_empty(&lock->l_bl_ast));
1615 list_add(&lock->l_bl_ast, &cancels);
1617 * This is a LDLM_CANCEL RPC which goes to canceld portal,
1618 * so we can cancel other LRU locks here and send them all
1619 * as one LDLM_CANCEL RPC.
1621 if (exp_connect_cancelset(exp)) {
1622 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1627 ns = ldlm_lock_to_ns(lock);
1628 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1631 ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1635 EXPORT_SYMBOL(ldlm_cli_cancel);
1638 * Locally cancel up to \a count locks in list \a cancels.
1639 * Return the number of cancelled locks.
1641 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1642 enum ldlm_cancel_flags cancel_flags)
1645 struct ldlm_lock *lock, *next;
1646 int left = 0, bl_ast = 0;
1650 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1654 if (cancel_flags & LCF_LOCAL) {
1655 rc = LDLM_FL_LOCAL_ONLY;
1656 ldlm_lock_cancel(lock);
1658 rc = ldlm_cli_cancel_local(lock);
1661 * Until we have compound requests and can send LDLM_CANCEL
1662 * requests batched with generic RPCs, we need to send cancels
1663 * with the LDLM_FL_BL_AST flag in a separate RPC from
1664 * the one being generated now.
1666 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1667 LDLM_DEBUG(lock, "Cancel lock separately");
1668 list_move(&lock->l_bl_ast, &head);
1672 if (rc == LDLM_FL_LOCAL_ONLY) {
1673 /* CANCEL RPC should not be sent to server. */
1674 list_del_init(&lock->l_bl_ast);
1675 LDLM_LOCK_RELEASE(lock);
1681 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1688 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1689 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1690 * readahead requests, ...)
1692 static enum ldlm_policy_res
1693 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1696 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1699 * don't check @added & @min since we want to process all locks
1701 * It's fine to not take lock to access lock->l_resource since
1702 * the lock has already been granted so it won't change.
1704 switch (lock->l_resource->lr_type) {
1707 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1711 result = LDLM_POLICY_SKIP_LOCK;
1719 * Callback function for LRU-resize policy. Decides whether to keep
1720 * \a lock in LRU for \a added in current scan and \a min number of locks
1721 * to be preferably canceled.
1723 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1725 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1727 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1728 struct ldlm_lock *lock,
1731 ktime_t cur = ktime_get();
1732 struct ldlm_pool *pl = &ns->ns_pool;
1737 return LDLM_POLICY_CANCEL_LOCK;
1740 * Despite of the LV, It doesn't make sense to keep the lock which
1741 * is unused for ns_max_age time.
1743 if (ktime_after(cur, ktime_add(lock->l_last_used, ns->ns_max_age)))
1744 return LDLM_POLICY_CANCEL_LOCK;
1746 slv = ldlm_pool_get_slv(pl);
1747 lvf = ldlm_pool_get_lvf(pl);
1748 la = div_u64(ktime_to_ns(ktime_sub(cur, lock->l_last_used)),
1750 lv = lvf * la * ns->ns_nr_unused >> 8;
1752 /* Inform pool about current CLV to see it via debugfs. */
1753 ldlm_pool_set_clv(pl, lv);
1756 * Stop when SLV is not yet come from server or lv is smaller than
1759 if (slv == 0 || lv < slv)
1760 return LDLM_POLICY_KEEP_LOCK;
1762 return LDLM_POLICY_CANCEL_LOCK;
1765 static enum ldlm_policy_res
1766 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1767 struct ldlm_lock *lock,
1770 enum ldlm_policy_res result;
1772 result = ldlm_cancel_lrur_policy(ns, lock, added, min);
1773 if (result == LDLM_POLICY_KEEP_LOCK)
1776 return ldlm_cancel_no_wait_policy(ns, lock, added, min);
1780 * Callback function for aged policy. Decides whether to keep
1781 * \a lock in LRU for \a added in current scan and \a min number of locks
1782 * to be preferably canceled.
1784 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1786 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1788 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1789 struct ldlm_lock *lock,
1792 if ((added >= min) &&
1793 ktime_before(ktime_get(),
1794 ktime_add(lock->l_last_used, ns->ns_max_age)))
1795 return LDLM_POLICY_KEEP_LOCK;
1797 return LDLM_POLICY_CANCEL_LOCK;
1800 static enum ldlm_policy_res
1801 ldlm_cancel_aged_no_wait_policy(struct ldlm_namespace *ns,
1802 struct ldlm_lock *lock,
1805 enum ldlm_policy_res result;
1807 result = ldlm_cancel_aged_policy(ns, lock, added, min);
1808 if (result == LDLM_POLICY_KEEP_LOCK)
1811 return ldlm_cancel_no_wait_policy(ns, lock, added, min);
1814 typedef enum ldlm_policy_res
1815 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1816 int added, int min);
1818 static ldlm_cancel_lru_policy_t
1819 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1821 if (ns_connect_lru_resize(ns)) {
1822 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1823 return ldlm_cancel_lrur_no_wait_policy;
1825 return ldlm_cancel_lrur_policy;
1827 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1828 return ldlm_cancel_aged_no_wait_policy;
1830 return ldlm_cancel_aged_policy;
1835 * - Free space in LRU for \a min new locks,
1836 * redundant unused locks are canceled locally;
1837 * - also cancel locally unused aged locks;
1838 * - do not cancel more than \a max locks;
1839 * - if some locks are cancelled, try to cancel at least \a batch locks
1840 * - GET the found locks and add them into the \a cancels list.
1842 * A client lock can be added to the l_bl_ast list only when it is
1843 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1844 * CANCEL. There are the following use cases:
1845 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1846 * ldlm_cli_cancel(), which check and set this flag properly. As any
1847 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1848 * later without any special locking.
1850 * Locks are cancelled according to the LRU resize policy (SLV from server)
1851 * if LRU resize is enabled; otherwise, the "aged policy" is used;
1854 * ----------------------------------------
1856 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel locks w/o sending any RPCs or waiting
1857 * for any outstanding RPC to complete.
1859 * flags & LDLM_CANCEL_CLEANUP - when cancelling read locks, do not check for
1860 * other read locks covering the same pages, just
1861 * discard those pages.
1863 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1864 struct list_head *cancels,
1865 int min, int max, int batch,
1866 enum ldlm_lru_flags lru_flags)
1868 ldlm_cancel_lru_policy_t pf;
1870 int no_wait = lru_flags & LDLM_LRU_FLAG_NO_WAIT;
1874 * Let only 1 thread to proceed. However, not for those which have the
1875 * @max limit given (ELC), as LRU may be left not cleaned up in full.
1878 if (test_and_set_bit(LDLM_LRU_CANCEL, &ns->ns_flags))
1880 } else if (test_bit(LDLM_LRU_CANCEL, &ns->ns_flags))
1883 LASSERT(ergo(max, min <= max));
1884 /* No sense to give @batch for ELC */
1885 LASSERT(ergo(max, batch == 0));
1887 if (!ns_connect_lru_resize(ns))
1888 min = max_t(int, min, ns->ns_nr_unused - ns->ns_max_unused);
1890 /* If at least 1 lock is to be cancelled, cancel at least @batch locks */
1891 if (min && min < batch)
1894 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1895 LASSERT(pf != NULL);
1897 /* For any flags, stop scanning if @max is reached. */
1898 while (!list_empty(&ns->ns_unused_list) && (max == 0 || added < max)) {
1899 struct ldlm_lock *lock;
1900 struct list_head *item, *next;
1901 enum ldlm_policy_res result;
1902 ktime_t last_use = ktime_set(0, 0);
1904 spin_lock(&ns->ns_lock);
1905 item = no_wait ? ns->ns_last_pos : &ns->ns_unused_list;
1906 for (item = item->next, next = item->next;
1907 item != &ns->ns_unused_list;
1908 item = next, next = item->next) {
1909 lock = list_entry(item, struct ldlm_lock, l_lru);
1911 /* No locks which got blocking requests. */
1912 LASSERT(!ldlm_is_bl_ast(lock));
1914 if (!ldlm_is_canceling(lock))
1918 * Somebody is already doing CANCEL. No need for this
1919 * lock in LRU, do not traverse it again.
1921 ldlm_lock_remove_from_lru_nolock(lock);
1923 if (item == &ns->ns_unused_list) {
1924 spin_unlock(&ns->ns_lock);
1928 last_use = lock->l_last_used;
1930 LDLM_LOCK_GET(lock);
1931 spin_unlock(&ns->ns_lock);
1932 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1935 * Pass the lock through the policy filter and see if it
1936 * should stay in LRU.
1938 * Even for shrinker policy we stop scanning if
1939 * we find a lock that should stay in the cache.
1940 * We should take into account lock age anyway
1941 * as a new lock is a valuable resource even if
1942 * it has a low weight.
1944 * That is, for shrinker policy we drop only
1945 * old locks, but additionally choose them by
1946 * their weight. Big extent locks will stay in
1949 result = pf(ns, lock, added, min);
1950 if (result == LDLM_POLICY_KEEP_LOCK) {
1951 lu_ref_del(&lock->l_reference, __func__, current);
1952 LDLM_LOCK_RELEASE(lock);
1956 if (result == LDLM_POLICY_SKIP_LOCK) {
1957 lu_ref_del(&lock->l_reference, __func__, current);
1959 spin_lock(&ns->ns_lock);
1960 if (!list_empty(&lock->l_lru) &&
1961 lock->l_lru.prev == ns->ns_last_pos)
1962 ns->ns_last_pos = &lock->l_lru;
1963 spin_unlock(&ns->ns_lock);
1966 LDLM_LOCK_RELEASE(lock);
1970 lock_res_and_lock(lock);
1971 /* Check flags again under the lock. */
1972 if (ldlm_is_canceling(lock) ||
1973 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1975 * Another thread is removing lock from LRU, or
1976 * somebody is already doing CANCEL, or there
1977 * is a blocking request which will send cancel
1978 * by itself, or the lock is no longer unused or
1979 * the lock has been used since the pf() call and
1980 * pages could be put under it.
1982 unlock_res_and_lock(lock);
1983 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1984 LDLM_LOCK_RELEASE(lock);
1987 LASSERT(!lock->l_readers && !lock->l_writers);
1990 * If we have chosen to cancel this lock voluntarily, we
1991 * better send cancel notification to server, so that it
1992 * frees appropriate state. This might lead to a race
1993 * where while we are doing cancel here, server is also
1994 * silently cancelling this lock.
1996 ldlm_clear_cancel_on_block(lock);
1999 * Setting the CBPENDING flag is a little misleading,
2000 * but prevents an important race; namely, once
2001 * CBPENDING is set, the lock can accumulate no more
2002 * readers/writers. Since readers and writers are
2003 * already zero here, ldlm_lock_decref() won't see
2004 * this flag and call l_blocking_ast
2006 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
2008 if ((lru_flags & LDLM_LRU_FLAG_CLEANUP) &&
2009 (lock->l_resource->lr_type == LDLM_EXTENT ||
2010 ldlm_has_dom(lock)) && lock->l_granted_mode == LCK_PR)
2011 ldlm_set_discard_data(lock);
2014 * We can't re-add to l_lru as it confuses the
2015 * refcounting in ldlm_lock_remove_from_lru() if an AST
2016 * arrives after we drop lr_lock below. We use l_bl_ast
2017 * and can't use l_pending_chain as it is used both on
2018 * server and client nevertheless b=5666 says it is
2019 * used only on server
2021 LASSERT(list_empty(&lock->l_bl_ast));
2022 list_add(&lock->l_bl_ast, cancels);
2023 unlock_res_and_lock(lock);
2024 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
2026 /* Once a lock added, batch the requested amount */
2032 clear_bit(LDLM_LRU_CANCEL, &ns->ns_flags);
2037 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
2039 enum ldlm_cancel_flags cancel_flags,
2040 enum ldlm_lru_flags lru_flags)
2044 added = ldlm_prepare_lru_list(ns, cancels, min, max, 0, lru_flags);
2048 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
2052 * Cancel at least \a min locks from given namespace LRU.
2054 * When called with LCF_ASYNC the blocking callback will be handled
2055 * in a thread and this function will return after the thread has been
2056 * asked to call the callback. When called with LCF_ASYNC the blocking
2057 * callback will be performed in this function.
2059 int ldlm_cancel_lru(struct ldlm_namespace *ns, int min,
2060 enum ldlm_cancel_flags cancel_flags,
2061 enum ldlm_lru_flags lru_flags)
2069 * Just prepare the list of locks, do not actually cancel them yet.
2070 * Locks are cancelled later in a separate thread.
2072 count = ldlm_prepare_lru_list(ns, &cancels, min, 0,
2073 ns->ns_cancel_batch, lru_flags);
2074 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
2082 * Find and cancel locally unused locks found on resource, matched to the
2083 * given policy, mode. GET the found locks and add them into the \a cancels
2086 int ldlm_cancel_resource_local(struct ldlm_resource *res,
2087 struct list_head *cancels,
2088 union ldlm_policy_data *policy,
2089 enum ldlm_mode mode, __u64 lock_flags,
2090 enum ldlm_cancel_flags cancel_flags,
2093 struct ldlm_lock *lock;
2099 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
2100 if (opaque != NULL && lock->l_ast_data != opaque) {
2101 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
2102 lock->l_ast_data, opaque);
2106 if (lock->l_readers || lock->l_writers)
2110 * If somebody is already doing CANCEL, or blocking AST came
2111 * then skip this lock.
2113 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
2116 if (lockmode_compat(lock->l_granted_mode, mode))
2120 * If policy is given and this is IBITS lock, add to list only
2121 * those locks that match by policy.
2123 if (policy && (lock->l_resource->lr_type == LDLM_IBITS)) {
2124 if (!(lock->l_policy_data.l_inodebits.bits &
2125 policy->l_inodebits.bits))
2127 /* Skip locks with DoM bit if it is not set in policy
2128 * to don't flush data by side-bits. Lock convert will
2129 * drop those bits separately.
2131 if (ldlm_has_dom(lock) &&
2132 !(policy->l_inodebits.bits & MDS_INODELOCK_DOM))
2136 /* See CBPENDING comment in ldlm_cancel_lru */
2137 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
2139 LASSERT(list_empty(&lock->l_bl_ast));
2140 list_add(&lock->l_bl_ast, cancels);
2141 LDLM_LOCK_GET(lock);
2146 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
2148 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2151 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
2153 * If \a req is NULL, send CANCEL request to server with handles of locks
2154 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
2155 * separately per lock.
2156 * If \a req is not NULL, put handles of locks in \a cancels into the request
2157 * buffer at the offset \a off.
2158 * Destroy \a cancels at the end.
2160 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
2161 struct ptlrpc_request *req,
2162 enum ldlm_cancel_flags flags)
2164 struct ldlm_lock *lock;
2169 if (list_empty(cancels) || count == 0)
2173 * XXX: requests (both batched and not) could be sent in parallel.
2174 * Usually it is enough to have just 1 RPC, but it is possible that
2175 * there are too many locks to be cancelled in LRU or on a resource.
2176 * It would also speed up the case when the server does not support
2180 LASSERT(!list_empty(cancels));
2181 lock = list_first_entry(cancels, struct ldlm_lock, l_bl_ast);
2182 LASSERT(lock->l_conn_export);
2184 if (exp_connect_cancelset(lock->l_conn_export)) {
2187 ldlm_cancel_pack(req, cancels, count);
2189 res = ldlm_cli_cancel_req(lock->l_conn_export,
2193 res = ldlm_cli_cancel_req(lock->l_conn_export,
2198 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
2199 "ldlm_cli_cancel_list: %d\n", res);
2204 ldlm_lock_list_put(cancels, l_bl_ast, res);
2206 LASSERT(count == 0);
2209 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2212 * Cancel all locks on a resource that have 0 readers/writers.
2214 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
2215 * to notify the server.
2217 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
2218 const struct ldlm_res_id *res_id,
2219 union ldlm_policy_data *policy,
2220 enum ldlm_mode mode,
2221 enum ldlm_cancel_flags flags, void *opaque)
2223 struct ldlm_resource *res;
2230 res = ldlm_resource_get(ns, res_id, 0, 0);
2232 /* This is not a problem. */
2233 CDEBUG(D_INFO, "No resource %llu\n", res_id->name[0]);
2237 LDLM_RESOURCE_ADDREF(res);
2238 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
2239 0, flags | LCF_BL_AST, opaque);
2240 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
2242 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
2245 LDLM_RESOURCE_DELREF(res);
2246 ldlm_resource_putref(res);
2249 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2251 struct ldlm_cli_cancel_arg {
2257 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2258 struct hlist_node *hnode, void *arg)
2260 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2261 struct ldlm_cli_cancel_arg *lc = arg;
2263 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2264 NULL, LCK_MINMODE, lc->lc_flags,
2266 /* must return 0 for hash iteration */
2271 * Cancel all locks on a namespace (or a specific resource, if given)
2272 * that have 0 readers/writers.
2274 * If flags & LCF_LOCAL, throw the locks away without trying
2275 * to notify the server.
2277 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2278 const struct ldlm_res_id *res_id,
2279 enum ldlm_cancel_flags flags, void *opaque)
2281 struct ldlm_cli_cancel_arg arg = {
2283 .lc_opaque = opaque,
2291 if (res_id != NULL) {
2292 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2296 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2297 ldlm_cli_hash_cancel_unused, &arg, 0);
2302 /* Lock iterators. */
2304 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2307 struct ldlm_lock *tmp;
2308 struct ldlm_lock *lock;
2309 int rc = LDLM_ITER_CONTINUE;
2313 RETURN(LDLM_ITER_CONTINUE);
2316 list_for_each_entry_safe(lock, tmp, &res->lr_granted, l_res_link) {
2317 if (iter(lock, closure) == LDLM_ITER_STOP)
2318 GOTO(out, rc = LDLM_ITER_STOP);
2321 list_for_each_entry_safe(lock, tmp, &res->lr_waiting, l_res_link) {
2322 if (iter(lock, closure) == LDLM_ITER_STOP)
2323 GOTO(out, rc = LDLM_ITER_STOP);
2330 struct iter_helper_data {
2331 ldlm_iterator_t iter;
2335 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2337 struct iter_helper_data *helper = closure;
2339 return helper->iter(lock, helper->closure);
2342 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2343 struct hlist_node *hnode, void *arg)
2346 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2348 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2352 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2353 ldlm_iterator_t iter, void *closure)
2356 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2358 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2359 ldlm_res_iter_helper, &helper, 0);
2364 * non-blocking function to manipulate a lock whose cb_data is being put away.
2365 * return 0: find no resource
2366 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2369 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2370 const struct ldlm_res_id *res_id,
2371 ldlm_iterator_t iter, void *data)
2373 struct ldlm_resource *res;
2378 LASSERTF(ns != NULL, "must pass in namespace\n");
2380 res = ldlm_resource_get(ns, res_id, 0, 0);
2384 LDLM_RESOURCE_ADDREF(res);
2385 rc = ldlm_resource_foreach(res, iter, data);
2386 LDLM_RESOURCE_DELREF(res);
2387 ldlm_resource_putref(res);
2390 EXPORT_SYMBOL(ldlm_resource_iterate);
2393 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2395 struct list_head *list = closure;
2397 /* we use l_pending_chain here, because it's unused on clients. */
2398 LASSERTF(list_empty(&lock->l_pending_chain),
2399 "lock %p next %p prev %p\n",
2400 lock, &lock->l_pending_chain.next,
2401 &lock->l_pending_chain.prev);
2403 * b=9573: don't replay locks left after eviction, or
2404 * b=17614: locks being actively cancelled. Get a reference
2405 * on a lock so that it does not disapear under us (e.g. due to cancel)
2407 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_BL_DONE))) {
2408 list_add(&lock->l_pending_chain, list);
2409 LDLM_LOCK_GET(lock);
2412 return LDLM_ITER_CONTINUE;
2415 static int replay_lock_interpret(const struct lu_env *env,
2416 struct ptlrpc_request *req, void *args, int rc)
2418 struct ldlm_async_args *aa = args;
2419 struct ldlm_lock *lock;
2420 struct ldlm_reply *reply;
2421 struct obd_export *exp;
2424 atomic_dec(&req->rq_import->imp_replay_inflight);
2425 wake_up(&req->rq_import->imp_replay_waitq);
2430 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2432 GOTO(out, rc = -EPROTO);
2434 lock = ldlm_handle2lock(&aa->lock_handle);
2436 CERROR("received replay ack for unknown local cookie %#llx remote cookie %#llx from server %s id %s\n",
2437 aa->lock_handle.cookie, reply->lock_handle.cookie,
2438 req->rq_export->exp_client_uuid.uuid,
2439 libcfs_idstr(&req->rq_peer));
2440 GOTO(out, rc = -ESTALE);
2443 /* Key change rehash lock in per-export hash with new key */
2444 exp = req->rq_export;
2445 if (exp && exp->exp_lock_hash) {
2447 * In the function below, .hs_keycmp resolves to
2448 * ldlm_export_lock_keycmp()
2450 /* coverity[overrun-buffer-val] */
2451 cfs_hash_rehash_key(exp->exp_lock_hash,
2452 &lock->l_remote_handle,
2453 &reply->lock_handle,
2456 lock->l_remote_handle = reply->lock_handle;
2459 LDLM_DEBUG(lock, "replayed lock:");
2460 ptlrpc_import_recovery_state_machine(req->rq_import);
2461 LDLM_LOCK_PUT(lock);
2464 ptlrpc_connect_import(req->rq_import);
2469 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2471 struct ptlrpc_request *req;
2472 struct ldlm_async_args *aa;
2473 struct ldlm_request *body;
2479 /* b=11974: Do not replay a lock which is actively being canceled */
2480 if (ldlm_is_bl_done(lock)) {
2481 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2486 * If this is reply-less callback lock, we cannot replay it, since
2487 * server might have long dropped it, but notification of that event was
2488 * lost by network. (and server granted conflicting lock already)
2490 if (ldlm_is_cancel_on_block(lock)) {
2491 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2492 ldlm_lock_cancel(lock);
2497 * If granted mode matches the requested mode, this lock is granted.
2499 * If we haven't been granted anything and are on a resource list,
2500 * then we're blocked/waiting.
2502 * If we haven't been granted anything and we're NOT on a resource list,
2503 * then we haven't got a reply yet and don't have a known disposition.
2504 * This happens whenever a lock enqueue is the request that triggers
2507 if (ldlm_is_granted(lock))
2508 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2509 else if (!list_empty(&lock->l_res_link))
2510 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2512 flags = LDLM_FL_REPLAY;
2514 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2515 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2519 /* We're part of recovery, so don't wait for it. */
2520 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2521 /* If the state changed while we were prepared, don't wait */
2522 req->rq_no_delay = 1;
2524 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2525 ldlm_lock2desc(lock, &body->lock_desc);
2526 body->lock_flags = ldlm_flags_to_wire(flags);
2528 ldlm_lock2handle(lock, &body->lock_handle[0]);
2529 if (lock->l_lvb_len > 0)
2530 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2531 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2533 ptlrpc_request_set_replen(req);
2535 * notify the server we've replayed all requests.
2536 * also, we mark the request to be put on a dedicated
2537 * queue to be processed after all request replayes.
2540 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2542 LDLM_DEBUG(lock, "replaying lock:");
2544 atomic_inc(&imp->imp_replay_inflight);
2545 aa = ptlrpc_req_async_args(aa, req);
2546 aa->lock_handle = body->lock_handle[0];
2547 req->rq_interpret_reply = replay_lock_interpret;
2548 ptlrpcd_add_req(req);
2554 * Cancel as many unused locks as possible before replay. since we are
2555 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2558 * Called only in recovery before replaying locks. there is no need to
2559 * replay locks that are unused. since the clients may hold thousands of
2560 * cached unused locks, dropping the unused locks can greatly reduce the
2561 * load on the servers at recovery time.
2563 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2569 "Dropping as many unused locks as possible before replay for namespace %s (%d)\n",
2570 ldlm_ns_name(ns), ns->ns_nr_unused);
2572 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_REPLAY_PAUSE, cfs_fail_val);
2575 * We don't need to care whether or not LRU resize is enabled
2576 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2579 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2580 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2582 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2583 canceled, ldlm_ns_name(ns));
2586 static int lock_can_replay(struct obd_import *imp)
2588 struct client_obd *cli = &imp->imp_obd->u.cli;
2590 CDEBUG(D_HA, "check lock replay limit, inflights = %u(%u)\n",
2591 atomic_read(&imp->imp_replay_inflight) - 1,
2592 cli->cl_max_rpcs_in_flight);
2594 /* +1 due to ldlm_lock_replay() increment */
2595 return atomic_read(&imp->imp_replay_inflight) <
2596 1 + min_t(u32, cli->cl_max_rpcs_in_flight, 8);
2599 int __ldlm_replay_locks(struct obd_import *imp, bool rate_limit)
2601 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2603 struct ldlm_lock *lock, *next;
2608 while (atomic_read(&imp->imp_replay_inflight) != 1)
2611 /* don't replay locks if import failed recovery */
2612 if (imp->imp_vbr_failed)
2615 if (ldlm_cancel_unused_locks_before_replay)
2616 ldlm_cancel_unused_locks_for_replay(ns);
2618 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2620 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2621 list_del_init(&lock->l_pending_chain);
2622 /* If we disconnected in the middle - cleanup and let
2623 * reconnection to happen again. LU-14027 */
2624 if (rc || (imp->imp_state != LUSTRE_IMP_REPLAY_LOCKS)) {
2625 LDLM_LOCK_RELEASE(lock);
2628 rc = replay_one_lock(imp, lock);
2629 LDLM_LOCK_RELEASE(lock);
2632 wait_event_idle_exclusive(imp->imp_replay_waitq,
2633 lock_can_replay(imp));
2640 * Lock replay uses rate control and can sleep waiting so
2641 * must be in separate thread from ptlrpcd itself
2643 static int ldlm_lock_replay_thread(void *data)
2645 struct obd_import *imp = data;
2647 unshare_fs_struct();
2648 CDEBUG(D_HA, "lock replay thread %s to %s@%s\n",
2649 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
2650 imp->imp_connection->c_remote_uuid.uuid);
2652 __ldlm_replay_locks(imp, true);
2653 atomic_dec(&imp->imp_replay_inflight);
2654 ptlrpc_import_recovery_state_machine(imp);
2655 class_import_put(imp);
2660 int ldlm_replay_locks(struct obd_import *imp)
2662 struct task_struct *task;
2665 /* ensure this doesn't fall to 0 before all have been queued */
2666 if (atomic_inc_return(&imp->imp_replay_inflight) > 1) {
2667 atomic_dec(&imp->imp_replay_inflight);
2670 class_import_get(imp);
2672 task = kthread_run(ldlm_lock_replay_thread, imp, "ldlm_lock_replay");
2675 CDEBUG(D_HA, "can't start lock replay thread: rc = %d\n", rc);
2677 /* run lock replay without rate control */
2678 rc = __ldlm_replay_locks(imp, false);
2679 atomic_dec(&imp->imp_replay_inflight);
2680 class_import_put(imp);