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");
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));
241 "client-side enqueue returned a blocked lock, going forward");
242 ldlm_reprocess_all(lock->l_resource, NULL);
245 EXPORT_SYMBOL(ldlm_completion_ast_async);
248 * Generic LDLM "completion" AST. This is called in several cases:
250 * - when a reply to an ENQUEUE RPC is received from the server
251 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
252 * this point (determined by flags);
254 * - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
257 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
260 * - to force all locks when resource is destroyed (cleanup_resource());
262 * If lock is not granted in the first case, this function waits until second
263 * or penultimate cases happen in some other thread.
266 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
268 /* XXX ALLOCATE - 160 bytes */
269 struct lock_wait_data lwd;
270 struct obd_device *obd;
271 struct obd_import *imp = NULL;
277 if (flags == LDLM_FL_WAIT_NOREPROC) {
278 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
282 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
283 wake_up(&lock->l_waitq);
287 LDLM_DEBUG(lock, "client-side enqueue returned a blocked locksleeping");
291 obd = class_exp2obd(lock->l_conn_export);
293 /* if this is a local lock, then there is no import */
295 imp = obd->u.cli.cl_import;
297 timeout = ldlm_cp_timeout(lock);
300 lock->l_activity = ktime_get_real_seconds();
303 spin_lock(&imp->imp_lock);
304 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
305 spin_unlock(&imp->imp_lock);
308 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
309 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
310 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
311 ldlm_set_fail_loc(lock);
314 /* Go to sleep until the lock is granted or cancelled. */
315 if (ldlm_is_no_timeout(lock)) {
316 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
317 rc = l_wait_event_abortable(
319 is_granted_or_cancelled(lock));
321 if (wait_event_idle_timeout(
323 is_granted_or_cancelled(lock),
324 cfs_time_seconds(timeout)) == 0) {
325 ldlm_expired_completion_wait(&lwd);
326 rc = l_wait_event_abortable(
328 is_granted_or_cancelled(lock));
334 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
339 RETURN(ldlm_completion_tail(lock, data));
341 EXPORT_SYMBOL(ldlm_completion_ast);
344 * A helper to build a blocking AST function
346 * Perform a common operation for blocking ASTs:
347 * defferred lock cancellation.
349 * \param lock the lock blocking or canceling AST was called on
351 * \see mdt_blocking_ast
352 * \see ldlm_blocking_ast
354 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
360 ldlm_set_cbpending(lock);
361 do_ast = (!lock->l_readers && !lock->l_writers);
362 unlock_res_and_lock(lock);
365 struct lustre_handle lockh;
368 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
369 ldlm_lock2handle(lock, &lockh);
370 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
372 CERROR("ldlm_cli_cancel: %d\n", rc);
375 "Lock still has references, will be cancelled later");
379 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
382 * Server blocking AST
384 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
387 * \param lock the lock which blocks a request or cancelling lock
390 * \param flag indicates whether this cancelling or blocking callback
392 * \see ldlm_blocking_ast_nocheck
394 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
395 void *data, int flag)
399 if (flag == LDLM_CB_CANCELING) {
400 /* Don't need to do anything here. */
404 lock_res_and_lock(lock);
406 * Get this: if ldlm_blocking_ast is racing with intent_policy, such
407 * that ldlm_blocking_ast is called just before intent_policy method
408 * takes the lr_lock, then by the time we get the lock, we might not
409 * be the correct blocking function anymore. So check, and return
412 if (lock->l_blocking_ast != ldlm_blocking_ast) {
413 unlock_res_and_lock(lock);
416 RETURN(ldlm_blocking_ast_nocheck(lock));
418 EXPORT_SYMBOL(ldlm_blocking_ast);
421 * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
423 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
424 * rather subtle: with OST-side locking, it may so happen that _all_ extent
425 * locks are held by the OST. If client wants to obtain the current file size
426 * it calls ll_glimpse_size(), and (as all locks are held only on the server),
427 * this dummy glimpse callback fires and does nothing. The client still
428 * receives the correct file size due to the following fragment of code in
429 * ldlm_cb_interpret():
431 * if (rc == -ELDLM_NO_LOCK_DATA) {
432 * LDLM_DEBUG(lock, "lost race - client has a lock but no"
434 * ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
437 * That is, after the glimpse returns this error, ofd_lvbo_update() is called
438 * and returns the updated file attributes from the inode to the client.
440 * See also comment in ofd_intent_policy() on why servers must set a non-NULL
441 * l_glimpse_ast when grabbing DLM locks. Otherwise, the server will assume
442 * that the object is in the process of being destroyed.
444 * \param[in] lock DLM lock being glimpsed, unused
445 * \param[in] reqp pointer to ptlrpc_request, unused
447 * \retval -ELDLM_NO_LOCK_DATA to get attributes from disk object
449 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
451 return -ELDLM_NO_LOCK_DATA;
455 * Enqueue a local lock (typically on a server).
457 int ldlm_cli_enqueue_local(const struct lu_env *env,
458 struct ldlm_namespace *ns,
459 const struct ldlm_res_id *res_id,
460 enum ldlm_type type, union ldlm_policy_data *policy,
461 enum ldlm_mode mode, __u64 *flags,
462 ldlm_blocking_callback blocking,
463 ldlm_completion_callback completion,
464 ldlm_glimpse_callback glimpse,
465 void *data, __u32 lvb_len, enum lvb_type lvb_type,
466 const __u64 *client_cookie,
467 struct lustre_handle *lockh)
469 struct ldlm_lock *lock;
471 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
472 .lcs_blocking = blocking,
473 .lcs_glimpse = glimpse,
478 LASSERT(!(*flags & LDLM_FL_REPLAY));
479 if (unlikely(ns_is_client(ns))) {
480 CERROR("Trying to enqueue local lock in a shadow namespace\n");
484 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
487 GOTO(out_nolock, err = PTR_ERR(lock));
489 err = ldlm_lvbo_init(lock->l_resource);
491 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
492 ldlm_lock_destroy_nolock(lock);
496 ldlm_lock2handle(lock, lockh);
499 * NB: we don't have any lock now (lock_res_and_lock)
500 * because it's a new lock
502 ldlm_lock_addref_internal_nolock(lock, mode);
503 ldlm_set_local(lock);
504 if (*flags & LDLM_FL_ATOMIC_CB)
505 ldlm_set_atomic_cb(lock);
507 if (*flags & LDLM_FL_CANCEL_ON_BLOCK)
508 ldlm_set_cancel_on_block(lock);
511 lock->l_policy_data = *policy;
512 if (client_cookie != NULL)
513 lock->l_client_cookie = *client_cookie;
514 if (type == LDLM_EXTENT) {
515 /* extent lock without policy is a bug */
519 lock->l_req_extent = policy->l_extent;
522 err = ldlm_lock_enqueue(env, ns, &lock, policy, flags);
523 if (unlikely(err != ELDLM_OK))
527 *policy = lock->l_policy_data;
529 if (lock->l_completion_ast)
530 lock->l_completion_ast(lock, *flags, NULL);
532 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
535 LDLM_LOCK_RELEASE(lock);
539 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
541 static void failed_lock_cleanup(struct ldlm_namespace *ns,
542 struct ldlm_lock *lock, int mode)
546 /* Set a flag to prevent us from sending a CANCEL (b=407) */
547 lock_res_and_lock(lock);
548 /* Check that lock is not granted or failed, we might race. */
549 if (!ldlm_is_granted(lock) && !ldlm_is_failed(lock)) {
551 * Make sure that this lock will not be found by raced
552 * bl_ast and -EINVAL reply is sent to server anyways.
555 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
556 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
559 unlock_res_and_lock(lock);
563 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
565 LDLM_DEBUG(lock, "lock was granted or failed in race");
568 * XXX - HACK because we shouldn't call ldlm_lock_destroy()
569 * from llite/file.c/ll_file_flock().
572 * This code makes for the fact that we do not have blocking handler on
573 * a client for flock locks. As such this is the place where we must
574 * completely kill failed locks. (interrupted and those that
575 * were waiting to be granted when server evicted us.
577 if (lock->l_resource->lr_type == LDLM_FLOCK) {
578 lock_res_and_lock(lock);
579 if (!ldlm_is_destroyed(lock)) {
580 ldlm_resource_unlink_lock(lock);
581 ldlm_lock_decref_internal_nolock(lock, mode);
582 ldlm_lock_destroy_nolock(lock);
584 unlock_res_and_lock(lock);
586 ldlm_lock_decref_internal(lock, mode);
590 static bool ldlm_request_slot_needed(enum ldlm_type type)
592 return type == LDLM_FLOCK || type == LDLM_IBITS;
596 * Finishing portion of client lock enqueue code.
598 * Called after receiving reply from server.
600 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
601 enum ldlm_type type, __u8 with_policy,
602 enum ldlm_mode mode, __u64 *flags, void *lvb,
603 __u32 lvb_len, const struct lustre_handle *lockh,
606 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
607 const struct lu_env *env = NULL;
608 int is_replay = *flags & LDLM_FL_REPLAY;
609 struct ldlm_lock *lock;
610 struct ldlm_reply *reply;
611 int cleanup_phase = 1;
615 if (ldlm_request_slot_needed(type))
616 obd_put_request_slot(&req->rq_import->imp_obd->u.cli);
618 ptlrpc_put_mod_rpc_slot(req);
620 if (req && req->rq_svc_thread)
621 env = req->rq_svc_thread->t_env;
623 lock = ldlm_handle2lock(lockh);
624 /* ldlm_cli_enqueue is holding a reference on this lock. */
626 LASSERT(type == LDLM_FLOCK);
630 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
631 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
633 if (rc != ELDLM_OK) {
635 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
636 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
638 if (rc != ELDLM_LOCK_ABORTED)
642 /* Before we return, swab the reply */
643 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
645 GOTO(cleanup, rc = -EPROTO);
650 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
653 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
654 GOTO(cleanup, rc = size);
655 } else if (unlikely(size > lvb_len)) {
657 "Replied LVB is larger than expectation, expected = %d, replied = %d",
659 GOTO(cleanup, rc = -EINVAL);
664 if (rc == ELDLM_LOCK_ABORTED) {
665 if (lvb_len > 0 && lvb != NULL)
666 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
668 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
671 /* lock enqueued on the server */
674 lock_res_and_lock(lock);
675 /* Key change rehash lock in per-export hash with new key */
676 if (exp->exp_lock_hash) {
678 * In the function below, .hs_keycmp resolves to
679 * ldlm_export_lock_keycmp()
681 /* coverity[overrun-buffer-val] */
682 cfs_hash_rehash_key(exp->exp_lock_hash,
683 &lock->l_remote_handle,
687 lock->l_remote_handle = reply->lock_handle;
690 *flags = ldlm_flags_from_wire(reply->lock_flags);
691 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
692 LDLM_FL_INHERIT_MASK);
693 unlock_res_and_lock(lock);
695 CDEBUG(D_INFO, "local: %p, remote cookie: %#llx, flags: %#llx\n",
696 lock, reply->lock_handle.cookie, *flags);
699 * If enqueue returned a blocked lock but the completion handler has
700 * already run, then it fixed up the resource and we don't need to do it
703 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
704 int newmode = reply->lock_desc.l_req_mode;
707 if (newmode && newmode != lock->l_req_mode) {
708 LDLM_DEBUG(lock, "server returned different mode %s",
709 ldlm_lockname[newmode]);
710 lock->l_req_mode = newmode;
713 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
714 &lock->l_resource->lr_name)) {
716 "remote intent success, locking "DLDLMRES", instead of "DLDLMRES"\n",
717 PLDLMRES(&reply->lock_desc.l_resource),
718 PLDLMRES(lock->l_resource));
720 rc = ldlm_lock_change_resource(ns, lock,
721 &reply->lock_desc.l_resource.lr_name);
722 if (rc || lock->l_resource == NULL)
723 GOTO(cleanup, rc = -ENOMEM);
724 LDLM_DEBUG(lock, "client-side enqueue, new resource");
728 /* We assume lock type cannot change on server*/
729 ldlm_convert_policy_to_local(exp,
730 lock->l_resource->lr_type,
731 &reply->lock_desc.l_policy_data,
732 &lock->l_policy_data);
735 if (type != LDLM_PLAIN)
737 "client-side enqueue, new policy data");
740 if ((*flags) & LDLM_FL_AST_SENT) {
741 lock_res_and_lock(lock);
742 ldlm_bl_desc2lock(&reply->lock_desc, lock);
743 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
744 unlock_res_and_lock(lock);
745 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
749 * If the lock has already been granted by a completion AST, don't
750 * clobber the LVB with an older one.
754 * We must lock or a racing completion might update lvb without
755 * letting us know and we'll clobber the correct value.
756 * Cannot unlock after the check either, a that still leaves
757 * a tiny window for completion to get in
759 lock_res_and_lock(lock);
760 if (!ldlm_is_granted(lock))
761 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
762 lock->l_lvb_data, lvb_len);
763 unlock_res_and_lock(lock);
771 rc = ldlm_lock_enqueue(env, ns, &lock, NULL, flags);
772 if (lock->l_completion_ast != NULL) {
773 int err = lock->l_completion_ast(lock, *flags, NULL);
782 if (lvb_len > 0 && lvb != NULL) {
784 * Copy the LVB here, and not earlier, because the completion
785 * AST (if any) can override what we got in the reply
787 memcpy(lvb, lock->l_lvb_data, lvb_len);
790 LDLM_DEBUG(lock, "client-side enqueue END");
793 if (cleanup_phase == 1 && rc)
794 failed_lock_cleanup(ns, lock, mode);
795 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
797 LDLM_LOCK_RELEASE(lock);
800 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
803 * Estimate number of lock handles that would fit into request of given
804 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
805 * a single page on the send/receive side. XXX: 512 should be changed to
806 * more adequate value.
808 static inline int ldlm_req_handles_avail(int req_size, int off)
812 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
813 if (likely(avail >= 0))
814 avail /= (int)sizeof(struct lustre_handle);
817 avail += LDLM_LOCKREQ_HANDLES - off;
822 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
823 enum req_location loc,
826 __u32 size = req_capsule_msg_size(pill, loc);
828 return ldlm_req_handles_avail(size, off);
831 static inline int ldlm_format_handles_avail(struct obd_import *imp,
832 const struct req_format *fmt,
833 enum req_location loc, int off)
835 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
837 return ldlm_req_handles_avail(size, off);
841 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
842 * \a count locks in \a cancels.
844 * This is to be called by functions preparing their own requests that
845 * might contain lists of locks to cancel in addition to actual operation
846 * that needs to be performed.
848 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
849 int version, int opc, int canceloff,
850 struct list_head *cancels, int count)
852 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
853 struct req_capsule *pill = &req->rq_pill;
854 struct ldlm_request *dlm = NULL;
856 int avail, to_free = 0, pack = 0;
863 if (ns_connect_cancelset(ns)) {
864 /* Estimate the amount of available space in the request. */
865 req_capsule_filled_sizes(pill, RCL_CLIENT);
866 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
868 /* If we have reached the limit, free +1 slot for the new one */
869 if (!ns_connect_lru_resize(ns) && opc == LDLM_ENQUEUE &&
870 ns->ns_nr_unused >= ns->ns_max_unused)
874 * Cancel LRU locks here _only_ if the server supports
875 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
876 * RPC, which will make us slower.
879 count += ldlm_cancel_lru_local(ns, cancels, to_free,
881 LDLM_LRU_FLAG_NO_WAIT);
886 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
887 ldlm_request_bufsize(pack, opc));
890 rc = ptlrpc_request_pack(req, version, opc);
892 ldlm_lock_list_put(cancels, l_bl_ast, count);
896 if (ns_connect_cancelset(ns)) {
898 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
901 * Skip first lock handler in ldlm_request_pack(),
902 * this method will increment @lock_count according
903 * to the lock handle amount actually written to
906 dlm->lock_count = canceloff;
908 /* Pack into the request @pack lock handles. */
909 ldlm_cli_cancel_list(cancels, pack, req, 0);
910 /* Prepare and send separate cancel RPC for others. */
911 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
913 ldlm_lock_list_put(cancels, l_bl_ast, count);
917 EXPORT_SYMBOL(ldlm_prep_elc_req);
919 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
920 struct list_head *cancels, int count)
922 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
923 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
925 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
927 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
929 struct ptlrpc_request *req;
934 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
936 RETURN(ERR_PTR(-ENOMEM));
938 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
940 ptlrpc_request_free(req);
944 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
945 ptlrpc_request_set_replen(req);
948 EXPORT_SYMBOL(ldlm_enqueue_pack);
951 * Client-side lock enqueue.
953 * If a request has some specific initialisation it is passed in \a reqp,
954 * otherwise it is created in ldlm_cli_enqueue.
956 * Supports sync and async requests, pass \a async flag accordingly. If a
957 * request was created in ldlm_cli_enqueue and it is the async request,
958 * pass it to the caller in \a reqp.
960 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
961 struct ldlm_enqueue_info *einfo,
962 const struct ldlm_res_id *res_id,
963 union ldlm_policy_data const *policy, __u64 *flags,
964 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
965 struct lustre_handle *lockh, int async)
967 struct ldlm_namespace *ns;
968 struct ldlm_lock *lock;
969 struct ldlm_request *body;
970 int is_replay = *flags & LDLM_FL_REPLAY;
971 int req_passed_in = 1;
973 struct ptlrpc_request *req;
977 LASSERT(exp != NULL);
979 ns = exp->exp_obd->obd_namespace;
982 * If we're replaying this lock, just check some invariants.
983 * If we're creating a new lock, get everything all setup nice.
986 lock = ldlm_handle2lock_long(lockh, 0);
987 LASSERT(lock != NULL);
988 LDLM_DEBUG(lock, "client-side enqueue START");
989 LASSERT(exp == lock->l_conn_export);
991 const struct ldlm_callback_suite cbs = {
992 .lcs_completion = einfo->ei_cb_cp,
993 .lcs_blocking = einfo->ei_cb_bl,
994 .lcs_glimpse = einfo->ei_cb_gl
996 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
997 einfo->ei_mode, &cbs, einfo->ei_cbdata,
1000 RETURN(PTR_ERR(lock));
1002 if (einfo->ei_cb_created)
1003 einfo->ei_cb_created(lock);
1005 /* for the local lock, add the reference */
1006 ldlm_lock_addref_internal(lock, einfo->ei_mode);
1007 ldlm_lock2handle(lock, lockh);
1009 lock->l_policy_data = *policy;
1011 if (einfo->ei_type == LDLM_EXTENT) {
1012 /* extent lock without policy is a bug */
1016 lock->l_req_extent = policy->l_extent;
1018 LDLM_DEBUG(lock, "client-side enqueue START, flags %#llx",
1022 lock->l_conn_export = exp;
1023 lock->l_export = NULL;
1024 lock->l_blocking_ast = einfo->ei_cb_bl;
1025 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL |
1026 LDLM_FL_ATOMIC_CB));
1027 lock->l_activity = ktime_get_real_seconds();
1029 /* lock not sent to server yet */
1030 if (reqp == NULL || *reqp == NULL) {
1031 req = ldlm_enqueue_pack(exp, lvb_len);
1033 failed_lock_cleanup(ns, lock, einfo->ei_mode);
1034 LDLM_LOCK_RELEASE(lock);
1035 RETURN(PTR_ERR(req));
1045 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
1047 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
1048 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
1051 if (*flags & LDLM_FL_NDELAY) {
1052 DEBUG_REQ(D_DLMTRACE, req, "enqueue lock with no delay");
1053 req->rq_no_resend = req->rq_no_delay = 1;
1055 * probably set a shorter timeout value and handle ETIMEDOUT
1056 * in osc_lock_upcall() correctly
1058 /* lustre_msg_set_timeout(req, req->rq_timeout / 2); */
1061 /* Dump lock data into the request buffer */
1062 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1063 ldlm_lock2desc(lock, &body->lock_desc);
1064 body->lock_flags = ldlm_flags_to_wire(*flags);
1065 body->lock_handle[0] = *lockh;
1067 /* extended LDLM opcodes in client stats */
1068 if (exp->exp_obd->obd_svc_stats != NULL) {
1069 bool glimpse = *flags & LDLM_FL_HAS_INTENT;
1071 /* OST glimpse has no intent buffer */
1072 if (req_capsule_has_field(&req->rq_pill, &RMF_LDLM_INTENT,
1074 struct ldlm_intent *it;
1076 it = req_capsule_client_get(&req->rq_pill,
1078 glimpse = (it && (it->opc == IT_GLIMPSE));
1082 ldlm_svc_get_eopc(body, exp->exp_obd->obd_svc_stats);
1084 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1086 LDLM_GLIMPSE_ENQUEUE);
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_enq_slot)
1094 ptlrpc_get_mod_rpc_slot(req);
1096 if (ldlm_request_slot_needed(einfo->ei_type)) {
1097 rc = obd_get_request_slot(&req->rq_import->imp_obd->u.cli);
1099 if (einfo->ei_enq_slot)
1100 ptlrpc_put_mod_rpc_slot(req);
1101 failed_lock_cleanup(ns, lock, einfo->ei_mode);
1102 LDLM_LOCK_RELEASE(lock);
1108 LASSERT(reqp != NULL);
1112 LDLM_DEBUG(lock, "sending request");
1114 rc = ptlrpc_queue_wait(req);
1116 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
1117 einfo->ei_mode, flags, lvb, lvb_len,
1121 * If ldlm_cli_enqueue_fini did not find the lock, we need to free
1122 * one reference that we took
1125 LDLM_LOCK_RELEASE(lock);
1130 if (!req_passed_in && req != NULL) {
1131 ptlrpc_req_finished(req);
1138 EXPORT_SYMBOL(ldlm_cli_enqueue);
1141 * Client-side IBITS lock convert.
1143 * Inform server that lock has been converted instead of canceling.
1144 * Server finishes convert on own side and does reprocess to grant
1145 * all related waiting locks.
1147 * Since convert means only ibits downgrading, client doesn't need to
1148 * wait for server reply to finish local converting process so this request
1149 * is made asynchronous.
1152 int ldlm_cli_convert_req(struct ldlm_lock *lock, __u32 *flags, __u64 new_bits)
1154 struct ldlm_request *body;
1155 struct ptlrpc_request *req;
1156 struct obd_export *exp = lock->l_conn_export;
1160 LASSERT(exp != NULL);
1163 * this is better to check earlier and it is done so already,
1164 * but this check is kept too as final one to issue an error
1165 * if any new code will miss such check.
1167 if (!exp_connect_lock_convert(exp)) {
1168 LDLM_ERROR(lock, "server doesn't support lock convert\n");
1172 if (lock->l_resource->lr_type != LDLM_IBITS) {
1173 LDLM_ERROR(lock, "convert works with IBITS locks only.");
1177 LDLM_DEBUG(lock, "client-side convert");
1179 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1180 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1185 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1186 body->lock_handle[0] = lock->l_remote_handle;
1188 body->lock_desc.l_req_mode = lock->l_req_mode;
1189 body->lock_desc.l_granted_mode = lock->l_granted_mode;
1191 body->lock_desc.l_policy_data.l_inodebits.bits = new_bits;
1192 body->lock_desc.l_policy_data.l_inodebits.cancel_bits = 0;
1194 body->lock_flags = ldlm_flags_to_wire(*flags);
1195 body->lock_count = 1;
1197 ptlrpc_request_set_replen(req);
1200 * Use cancel portals for convert as well as high-priority handling.
1202 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1203 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1205 ptlrpc_at_set_req_timeout(req);
1207 if (exp->exp_obd->obd_svc_stats != NULL)
1208 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1209 LDLM_CONVERT - LDLM_FIRST_OPC);
1211 ptlrpcd_add_req(req);
1216 * Cancel locks locally.
1218 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1219 * \retval LDLM_FL_CANCELING otherwise;
1220 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1222 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1224 __u64 rc = LDLM_FL_LOCAL_ONLY;
1228 if (lock->l_conn_export) {
1231 LDLM_DEBUG(lock, "client-side cancel");
1232 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL_LOCAL,
1235 /* Set this flag to prevent others from getting new references*/
1236 lock_res_and_lock(lock);
1237 ldlm_set_cbpending(lock);
1238 local_only = !!(lock->l_flags &
1239 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1240 ldlm_cancel_callback(lock);
1241 rc = (ldlm_is_bl_ast(lock)) ?
1242 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1243 unlock_res_and_lock(lock);
1247 "not sending request (at caller's instruction)\n");
1248 rc = LDLM_FL_LOCAL_ONLY;
1250 ldlm_lock_cancel(lock);
1252 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1253 LDLM_ERROR(lock, "Trying to cancel local lock");
1256 LDLM_DEBUG(lock, "server-side local cancel");
1257 ldlm_lock_cancel(lock);
1258 ldlm_reprocess_all(lock->l_resource, lock);
1265 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1267 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1268 struct list_head *head, int count)
1270 struct ldlm_request *dlm;
1271 struct ldlm_lock *lock;
1272 int max, packed = 0;
1276 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1277 LASSERT(dlm != NULL);
1279 /* Check the room in the request buffer. */
1280 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1281 sizeof(struct ldlm_request);
1282 max /= sizeof(struct lustre_handle);
1283 max += LDLM_LOCKREQ_HANDLES;
1284 LASSERT(max >= dlm->lock_count + count);
1287 * XXX: it would be better to pack lock handles grouped by resource.
1288 * so that the server cancel would call filter_lvbo_update() less
1291 list_for_each_entry(lock, head, l_bl_ast) {
1294 LASSERT(lock->l_conn_export);
1295 /* Pack the lock handle to the given request buffer. */
1296 LDLM_DEBUG(lock, "packing");
1297 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1300 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1305 * Prepare and send a batched cancel RPC. It will include \a count lock
1306 * handles of locks given in \a cancels list.
1308 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1309 int count, enum ldlm_cancel_flags flags)
1311 struct ptlrpc_request *req = NULL;
1312 struct obd_import *imp;
1318 LASSERT(exp != NULL);
1321 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1323 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1326 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1327 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1332 imp = class_exp2cliimp(exp);
1333 if (imp == NULL || imp->imp_invalid) {
1335 "skipping cancel on invalid import %p\n", imp);
1339 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1341 GOTO(out, rc = -ENOMEM);
1343 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1344 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1345 ldlm_request_bufsize(count, LDLM_CANCEL));
1347 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1349 ptlrpc_request_free(req);
1354 * If OSP want cancel cross-MDT lock, let's not block it in
1355 * in recovery, otherwise the lock will not released, if
1356 * the remote target is also in recovery, and it also need
1357 * this lock, it might cause deadlock.
1359 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1360 exp->exp_obd->obd_lu_dev != NULL &&
1361 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1362 struct lu_device *top_dev;
1364 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1365 if (top_dev != NULL &&
1366 top_dev->ld_obd->obd_recovering)
1367 req->rq_allow_replay = 1;
1370 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1371 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1372 ptlrpc_at_set_req_timeout(req);
1374 ldlm_cancel_pack(req, cancels, count);
1376 ptlrpc_request_set_replen(req);
1377 if (flags & LCF_ASYNC) {
1378 ptlrpcd_add_req(req);
1383 rc = ptlrpc_queue_wait(req);
1384 if (rc == LUSTRE_ESTALE) {
1386 "client/server (nid %s) out of sync -- not fatal\n",
1387 libcfs_nid2str(req->rq_import->imp_connection->c_peer.nid));
1389 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1390 req->rq_import_generation == imp->imp_generation) {
1391 ptlrpc_req_finished(req);
1393 } else if (rc != ELDLM_OK) {
1394 /* -ESHUTDOWN is common on umount */
1395 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1396 "Got rc %d from cancel RPC: canceling anyway\n",
1404 ptlrpc_req_finished(req);
1407 return sent ? sent : rc;
1410 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1412 LASSERT(imp != NULL);
1413 return &imp->imp_obd->obd_namespace->ns_pool;
1417 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1419 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1421 struct obd_device *obd;
1426 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1427 !imp_connect_lru_resize(req->rq_import)))
1428 /* Do nothing for corner cases. */
1432 * In some cases RPC may contain SLV and limit zeroed out. This
1433 * is the case when server does not support LRU resize feature.
1434 * This is also possible in some recovery cases when server-side
1435 * reqs have no reference to the OBD export and thus access to
1436 * server-side namespace is not possible.
1438 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1439 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1440 DEBUG_REQ(D_HA, req,
1441 "Zero SLV or limit found (SLV=%llu, limit=%u)",
1442 lustre_msg_get_slv(req->rq_repmsg),
1443 lustre_msg_get_limit(req->rq_repmsg));
1447 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1448 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1449 obd = req->rq_import->imp_obd;
1451 read_lock(&obd->obd_pool_lock);
1452 if (obd->obd_pool_slv == new_slv &&
1453 obd->obd_pool_limit == new_limit) {
1454 read_unlock(&obd->obd_pool_lock);
1457 read_unlock(&obd->obd_pool_lock);
1460 * Set new SLV and limit in OBD fields to make them accessible
1461 * to the pool thread. We do not access obd_namespace and pool
1462 * directly here as there is no reliable way to make sure that
1463 * they are still alive at cleanup time. Evil races are possible
1464 * which may cause Oops at that time.
1466 write_lock(&obd->obd_pool_lock);
1467 obd->obd_pool_slv = new_slv;
1468 obd->obd_pool_limit = new_limit;
1469 write_unlock(&obd->obd_pool_lock);
1474 int ldlm_cli_convert(struct ldlm_lock *lock,
1475 enum ldlm_cancel_flags cancel_flags)
1479 LASSERT(!lock->l_readers && !lock->l_writers);
1480 LDLM_DEBUG(lock, "client lock convert START");
1482 if (lock->l_resource->lr_type == LDLM_IBITS) {
1483 lock_res_and_lock(lock);
1485 rc = ldlm_cli_inodebits_convert(lock, cancel_flags);
1486 } while (rc == -EAGAIN);
1487 unlock_res_and_lock(lock);
1490 LDLM_DEBUG(lock, "client lock convert END");
1493 EXPORT_SYMBOL(ldlm_cli_convert);
1496 * Client side lock cancel.
1498 * Lock must not have any readers or writers by this time.
1500 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1501 enum ldlm_cancel_flags cancel_flags)
1503 struct obd_export *exp;
1504 int avail, count = 1;
1506 struct ldlm_namespace *ns;
1507 struct ldlm_lock *lock;
1512 lock = ldlm_handle2lock_long(lockh, 0);
1514 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1518 lock_res_and_lock(lock);
1519 LASSERT(!ldlm_is_converting(lock));
1521 /* Lock is being canceled and the caller doesn't want to wait */
1522 if (ldlm_is_canceling(lock)) {
1523 if (cancel_flags & LCF_ASYNC) {
1524 unlock_res_and_lock(lock);
1526 unlock_res_and_lock(lock);
1527 wait_event_idle(lock->l_waitq, is_bl_done(lock));
1529 LDLM_LOCK_RELEASE(lock);
1533 ldlm_set_canceling(lock);
1534 unlock_res_and_lock(lock);
1536 if (cancel_flags & LCF_LOCAL)
1537 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE,
1540 rc = ldlm_cli_cancel_local(lock);
1541 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1542 LDLM_LOCK_RELEASE(lock);
1546 * Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1547 * RPC which goes to canceld portal, so we can cancel other LRU locks
1548 * here and send them all as one LDLM_CANCEL RPC.
1550 LASSERT(list_empty(&lock->l_bl_ast));
1551 list_add(&lock->l_bl_ast, &cancels);
1553 exp = lock->l_conn_export;
1554 if (exp_connect_cancelset(exp)) {
1555 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1560 ns = ldlm_lock_to_ns(lock);
1561 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1564 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1567 EXPORT_SYMBOL(ldlm_cli_cancel);
1570 * Locally cancel up to \a count locks in list \a cancels.
1571 * Return the number of cancelled locks.
1573 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1574 enum ldlm_cancel_flags cancel_flags)
1577 struct ldlm_lock *lock, *next;
1578 int left = 0, bl_ast = 0;
1582 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1586 if (cancel_flags & LCF_LOCAL) {
1587 rc = LDLM_FL_LOCAL_ONLY;
1588 ldlm_lock_cancel(lock);
1590 rc = ldlm_cli_cancel_local(lock);
1593 * Until we have compound requests and can send LDLM_CANCEL
1594 * requests batched with generic RPCs, we need to send cancels
1595 * with the LDLM_FL_BL_AST flag in a separate RPC from
1596 * the one being generated now.
1598 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1599 LDLM_DEBUG(lock, "Cancel lock separately");
1600 list_move(&lock->l_bl_ast, &head);
1604 if (rc == LDLM_FL_LOCAL_ONLY) {
1605 /* CANCEL RPC should not be sent to server. */
1606 list_del_init(&lock->l_bl_ast);
1607 LDLM_LOCK_RELEASE(lock);
1613 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1620 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1621 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1622 * readahead requests, ...)
1624 static enum ldlm_policy_res
1625 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1628 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1631 * don't check @added & @min since we want to process all locks
1633 * It's fine to not take lock to access lock->l_resource since
1634 * the lock has already been granted so it won't change.
1636 switch (lock->l_resource->lr_type) {
1639 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1643 result = LDLM_POLICY_SKIP_LOCK;
1651 * Callback function for LRU-resize policy. Decides whether to keep
1652 * \a lock in LRU for \a added in current scan and \a min number of locks
1653 * to be preferably canceled.
1655 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1657 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1659 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1660 struct ldlm_lock *lock,
1663 ktime_t cur = ktime_get();
1664 struct ldlm_pool *pl = &ns->ns_pool;
1669 return LDLM_POLICY_CANCEL_LOCK;
1672 * Despite of the LV, It doesn't make sense to keep the lock which
1673 * is unused for ns_max_age time.
1675 if (ktime_after(cur, ktime_add(lock->l_last_used, ns->ns_max_age)))
1676 return LDLM_POLICY_CANCEL_LOCK;
1678 slv = ldlm_pool_get_slv(pl);
1679 lvf = ldlm_pool_get_lvf(pl);
1680 la = div_u64(ktime_to_ns(ktime_sub(cur, lock->l_last_used)),
1682 lv = lvf * la * ns->ns_nr_unused;
1684 /* Inform pool about current CLV to see it via debugfs. */
1685 ldlm_pool_set_clv(pl, lv);
1688 * Stop when SLV is not yet come from server or lv is smaller than
1691 if (slv == 0 || lv < slv)
1692 return LDLM_POLICY_KEEP_LOCK;
1694 return LDLM_POLICY_CANCEL_LOCK;
1697 static enum ldlm_policy_res
1698 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1699 struct ldlm_lock *lock,
1702 enum ldlm_policy_res result;
1704 result = ldlm_cancel_lrur_policy(ns, lock, added, min);
1705 if (result == LDLM_POLICY_KEEP_LOCK)
1708 return ldlm_cancel_no_wait_policy(ns, lock, added, min);
1712 * Callback function for aged policy. Decides whether to keep
1713 * \a lock in LRU for \a added in current scan and \a min number of locks
1714 * to be preferably canceled.
1716 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1718 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1720 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1721 struct ldlm_lock *lock,
1724 if ((added >= min) &&
1725 ktime_before(ktime_get(),
1726 ktime_add(lock->l_last_used, ns->ns_max_age)))
1727 return LDLM_POLICY_KEEP_LOCK;
1729 return LDLM_POLICY_CANCEL_LOCK;
1732 static enum ldlm_policy_res
1733 ldlm_cancel_aged_no_wait_policy(struct ldlm_namespace *ns,
1734 struct ldlm_lock *lock,
1737 enum ldlm_policy_res result;
1739 result = ldlm_cancel_aged_policy(ns, lock, added, min);
1740 if (result == LDLM_POLICY_KEEP_LOCK)
1743 return ldlm_cancel_no_wait_policy(ns, lock, added, min);
1746 typedef enum ldlm_policy_res
1747 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1748 int added, int min);
1750 static ldlm_cancel_lru_policy_t
1751 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1753 if (ns_connect_lru_resize(ns)) {
1754 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1755 return ldlm_cancel_lrur_no_wait_policy;
1757 return ldlm_cancel_lrur_policy;
1759 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1760 return ldlm_cancel_aged_no_wait_policy;
1762 return ldlm_cancel_aged_policy;
1767 * - Free space in LRU for \a min new locks,
1768 * redundant unused locks are canceled locally;
1769 * - also cancel locally unused aged locks;
1770 * - do not cancel more than \a max locks;
1771 * - if some locks are cancelled, try to cancel at least \a batch locks
1772 * - GET the found locks and add them into the \a cancels list.
1774 * A client lock can be added to the l_bl_ast list only when it is
1775 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1776 * CANCEL. There are the following use cases:
1777 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1778 * ldlm_cli_cancel(), which check and set this flag properly. As any
1779 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1780 * later without any special locking.
1782 * Locks are cancelled according to the LRU resize policy (SLV from server)
1783 * if LRU resize is enabled; otherwise, the "aged policy" is used;
1786 * ----------------------------------------
1788 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel locks w/o sending any RPCs or waiting
1789 * for any outstanding RPC to complete.
1791 * flags & LDLM_CANCEL_CLEANUP - when cancelling read locks, do not check for
1792 * other read locks covering the same pages, just
1793 * discard those pages.
1795 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1796 struct list_head *cancels,
1797 int min, int max, int batch,
1798 enum ldlm_lru_flags lru_flags)
1800 ldlm_cancel_lru_policy_t pf;
1802 int no_wait = lru_flags & LDLM_LRU_FLAG_NO_WAIT;
1806 * Let only 1 thread to proceed. However, not for those which have the
1807 * @max limit given (ELC), as LRU may be left not cleaned up in full.
1810 if (test_and_set_bit(LDLM_LRU_CANCEL, &ns->ns_flags))
1812 } else if (test_bit(LDLM_LRU_CANCEL, &ns->ns_flags))
1815 LASSERT(ergo(max, min <= max));
1816 /* No sense to give @batch for ELC */
1817 LASSERT(ergo(max, batch == 0));
1819 if (!ns_connect_lru_resize(ns))
1820 min = max_t(int, min, ns->ns_nr_unused - ns->ns_max_unused);
1822 /* If at least 1 lock is to be cancelled, cancel at least @batch locks */
1823 if (min && min < batch)
1826 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1827 LASSERT(pf != NULL);
1829 /* For any flags, stop scanning if @max is reached. */
1830 while (!list_empty(&ns->ns_unused_list) && (max == 0 || added < max)) {
1831 struct ldlm_lock *lock;
1832 struct list_head *item, *next;
1833 enum ldlm_policy_res result;
1834 ktime_t last_use = ktime_set(0, 0);
1836 spin_lock(&ns->ns_lock);
1837 item = no_wait ? ns->ns_last_pos : &ns->ns_unused_list;
1838 for (item = item->next, next = item->next;
1839 item != &ns->ns_unused_list;
1840 item = next, next = item->next) {
1841 lock = list_entry(item, struct ldlm_lock, l_lru);
1843 /* No locks which got blocking requests. */
1844 LASSERT(!ldlm_is_bl_ast(lock));
1846 if (!ldlm_is_canceling(lock))
1850 * Somebody is already doing CANCEL. No need for this
1851 * lock in LRU, do not traverse it again.
1853 ldlm_lock_remove_from_lru_nolock(lock);
1855 if (item == &ns->ns_unused_list) {
1856 spin_unlock(&ns->ns_lock);
1860 last_use = lock->l_last_used;
1862 LDLM_LOCK_GET(lock);
1863 spin_unlock(&ns->ns_lock);
1864 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1867 * Pass the lock through the policy filter and see if it
1868 * should stay in LRU.
1870 * Even for shrinker policy we stop scanning if
1871 * we find a lock that should stay in the cache.
1872 * We should take into account lock age anyway
1873 * as a new lock is a valuable resource even if
1874 * it has a low weight.
1876 * That is, for shrinker policy we drop only
1877 * old locks, but additionally choose them by
1878 * their weight. Big extent locks will stay in
1881 result = pf(ns, lock, added, min);
1882 if (result == LDLM_POLICY_KEEP_LOCK) {
1883 lu_ref_del(&lock->l_reference, __func__, current);
1884 LDLM_LOCK_RELEASE(lock);
1888 if (result == LDLM_POLICY_SKIP_LOCK) {
1889 lu_ref_del(&lock->l_reference, __func__, current);
1891 spin_lock(&ns->ns_lock);
1892 if (!list_empty(&lock->l_lru) &&
1893 lock->l_lru.prev == ns->ns_last_pos)
1894 ns->ns_last_pos = &lock->l_lru;
1895 spin_unlock(&ns->ns_lock);
1898 LDLM_LOCK_RELEASE(lock);
1902 lock_res_and_lock(lock);
1903 /* Check flags again under the lock. */
1904 if (ldlm_is_canceling(lock) ||
1905 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1907 * Another thread is removing lock from LRU, or
1908 * somebody is already doing CANCEL, or there
1909 * is a blocking request which will send cancel
1910 * by itself, or the lock is no longer unused or
1911 * the lock has been used since the pf() call and
1912 * pages could be put under it.
1914 unlock_res_and_lock(lock);
1915 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1916 LDLM_LOCK_RELEASE(lock);
1919 LASSERT(!lock->l_readers && !lock->l_writers);
1922 * If we have chosen to cancel this lock voluntarily, we
1923 * better send cancel notification to server, so that it
1924 * frees appropriate state. This might lead to a race
1925 * where while we are doing cancel here, server is also
1926 * silently cancelling this lock.
1928 ldlm_clear_cancel_on_block(lock);
1931 * Setting the CBPENDING flag is a little misleading,
1932 * but prevents an important race; namely, once
1933 * CBPENDING is set, the lock can accumulate no more
1934 * readers/writers. Since readers and writers are
1935 * already zero here, ldlm_lock_decref() won't see
1936 * this flag and call l_blocking_ast
1938 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1940 if ((lru_flags & LDLM_LRU_FLAG_CLEANUP) &&
1941 (lock->l_resource->lr_type == LDLM_EXTENT ||
1942 ldlm_has_dom(lock)) && lock->l_granted_mode == LCK_PR)
1943 ldlm_set_discard_data(lock);
1946 * We can't re-add to l_lru as it confuses the
1947 * refcounting in ldlm_lock_remove_from_lru() if an AST
1948 * arrives after we drop lr_lock below. We use l_bl_ast
1949 * and can't use l_pending_chain as it is used both on
1950 * server and client nevertheless b=5666 says it is
1951 * used only on server
1953 LASSERT(list_empty(&lock->l_bl_ast));
1954 list_add(&lock->l_bl_ast, cancels);
1955 unlock_res_and_lock(lock);
1956 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1958 /* Once a lock added, batch the requested amount */
1964 clear_bit(LDLM_LRU_CANCEL, &ns->ns_flags);
1969 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1971 enum ldlm_cancel_flags cancel_flags,
1972 enum ldlm_lru_flags lru_flags)
1976 added = ldlm_prepare_lru_list(ns, cancels, min, max, 0, lru_flags);
1980 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1984 * Cancel at least \a min locks from given namespace LRU.
1986 * When called with LCF_ASYNC the blocking callback will be handled
1987 * in a thread and this function will return after the thread has been
1988 * asked to call the callback. When called with LCF_ASYNC the blocking
1989 * callback will be performed in this function.
1991 int ldlm_cancel_lru(struct ldlm_namespace *ns, int min,
1992 enum ldlm_cancel_flags cancel_flags,
1993 enum ldlm_lru_flags lru_flags)
2001 * Just prepare the list of locks, do not actually cancel them yet.
2002 * Locks are cancelled later in a separate thread.
2004 count = ldlm_prepare_lru_list(ns, &cancels, min, 0, 0, lru_flags);
2005 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
2013 * Find and cancel locally unused locks found on resource, matched to the
2014 * given policy, mode. GET the found locks and add them into the \a cancels
2017 int ldlm_cancel_resource_local(struct ldlm_resource *res,
2018 struct list_head *cancels,
2019 union ldlm_policy_data *policy,
2020 enum ldlm_mode mode, __u64 lock_flags,
2021 enum ldlm_cancel_flags cancel_flags,
2024 struct ldlm_lock *lock;
2030 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
2031 if (opaque != NULL && lock->l_ast_data != opaque) {
2032 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
2033 lock->l_ast_data, opaque);
2037 if (lock->l_readers || lock->l_writers)
2041 * If somebody is already doing CANCEL, or blocking AST came
2042 * then skip this lock.
2044 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
2047 if (lockmode_compat(lock->l_granted_mode, mode))
2051 * If policy is given and this is IBITS lock, add to list only
2052 * those locks that match by policy.
2054 if (policy && (lock->l_resource->lr_type == LDLM_IBITS)) {
2055 if (!(lock->l_policy_data.l_inodebits.bits &
2056 policy->l_inodebits.bits))
2058 /* Skip locks with DoM bit if it is not set in policy
2059 * to don't flush data by side-bits. Lock convert will
2060 * drop those bits separately.
2062 if (ldlm_has_dom(lock) &&
2063 !(policy->l_inodebits.bits & MDS_INODELOCK_DOM))
2067 /* See CBPENDING comment in ldlm_cancel_lru */
2068 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
2070 LASSERT(list_empty(&lock->l_bl_ast));
2071 list_add(&lock->l_bl_ast, cancels);
2072 LDLM_LOCK_GET(lock);
2077 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
2079 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2082 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
2084 * If \a req is NULL, send CANCEL request to server with handles of locks
2085 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
2086 * separately per lock.
2087 * If \a req is not NULL, put handles of locks in \a cancels into the request
2088 * buffer at the offset \a off.
2089 * Destroy \a cancels at the end.
2091 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
2092 struct ptlrpc_request *req,
2093 enum ldlm_cancel_flags flags)
2095 struct ldlm_lock *lock;
2100 if (list_empty(cancels) || count == 0)
2104 * XXX: requests (both batched and not) could be sent in parallel.
2105 * Usually it is enough to have just 1 RPC, but it is possible that
2106 * there are too many locks to be cancelled in LRU or on a resource.
2107 * It would also speed up the case when the server does not support
2111 LASSERT(!list_empty(cancels));
2112 lock = list_entry(cancels->next, struct ldlm_lock,
2114 LASSERT(lock->l_conn_export);
2116 if (exp_connect_cancelset(lock->l_conn_export)) {
2119 ldlm_cancel_pack(req, cancels, count);
2121 res = ldlm_cli_cancel_req(lock->l_conn_export,
2125 res = ldlm_cli_cancel_req(lock->l_conn_export,
2130 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
2131 "ldlm_cli_cancel_list: %d\n", res);
2136 ldlm_lock_list_put(cancels, l_bl_ast, res);
2138 LASSERT(count == 0);
2141 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2144 * Cancel all locks on a resource that have 0 readers/writers.
2146 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
2147 * to notify the server.
2149 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
2150 const struct ldlm_res_id *res_id,
2151 union ldlm_policy_data *policy,
2152 enum ldlm_mode mode,
2153 enum ldlm_cancel_flags flags, void *opaque)
2155 struct ldlm_resource *res;
2162 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2164 /* This is not a problem. */
2165 CDEBUG(D_INFO, "No resource %llu\n", res_id->name[0]);
2169 LDLM_RESOURCE_ADDREF(res);
2170 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
2171 0, flags | LCF_BL_AST, opaque);
2172 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
2174 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
2177 LDLM_RESOURCE_DELREF(res);
2178 ldlm_resource_putref(res);
2181 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2183 struct ldlm_cli_cancel_arg {
2189 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2190 struct hlist_node *hnode, void *arg)
2192 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2193 struct ldlm_cli_cancel_arg *lc = arg;
2195 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2196 NULL, LCK_MINMODE, lc->lc_flags,
2198 /* must return 0 for hash iteration */
2203 * Cancel all locks on a namespace (or a specific resource, if given)
2204 * that have 0 readers/writers.
2206 * If flags & LCF_LOCAL, throw the locks away without trying
2207 * to notify the server.
2209 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2210 const struct ldlm_res_id *res_id,
2211 enum ldlm_cancel_flags flags, void *opaque)
2213 struct ldlm_cli_cancel_arg arg = {
2215 .lc_opaque = opaque,
2223 if (res_id != NULL) {
2224 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2228 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2229 ldlm_cli_hash_cancel_unused, &arg, 0);
2234 /* Lock iterators. */
2236 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2239 struct list_head *tmp, *next;
2240 struct ldlm_lock *lock;
2241 int rc = LDLM_ITER_CONTINUE;
2246 RETURN(LDLM_ITER_CONTINUE);
2249 list_for_each_safe(tmp, next, &res->lr_granted) {
2250 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2252 if (iter(lock, closure) == LDLM_ITER_STOP)
2253 GOTO(out, rc = LDLM_ITER_STOP);
2256 list_for_each_safe(tmp, next, &res->lr_waiting) {
2257 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2259 if (iter(lock, closure) == LDLM_ITER_STOP)
2260 GOTO(out, rc = LDLM_ITER_STOP);
2267 struct iter_helper_data {
2268 ldlm_iterator_t iter;
2272 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2274 struct iter_helper_data *helper = closure;
2276 return helper->iter(lock, helper->closure);
2279 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2280 struct hlist_node *hnode, void *arg)
2283 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2285 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2289 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2290 ldlm_iterator_t iter, void *closure)
2293 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2295 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2296 ldlm_res_iter_helper, &helper, 0);
2301 * non-blocking function to manipulate a lock whose cb_data is being put away.
2302 * return 0: find no resource
2303 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2306 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2307 const struct ldlm_res_id *res_id,
2308 ldlm_iterator_t iter, void *data)
2310 struct ldlm_resource *res;
2315 LASSERTF(ns != NULL, "must pass in namespace\n");
2317 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2321 LDLM_RESOURCE_ADDREF(res);
2322 rc = ldlm_resource_foreach(res, iter, data);
2323 LDLM_RESOURCE_DELREF(res);
2324 ldlm_resource_putref(res);
2327 EXPORT_SYMBOL(ldlm_resource_iterate);
2330 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2332 struct list_head *list = closure;
2334 /* we use l_pending_chain here, because it's unused on clients. */
2335 LASSERTF(list_empty(&lock->l_pending_chain),
2336 "lock %p next %p prev %p\n",
2337 lock, &lock->l_pending_chain.next,
2338 &lock->l_pending_chain.prev);
2340 * b=9573: don't replay locks left after eviction, or
2341 * b=17614: locks being actively cancelled. Get a reference
2342 * on a lock so that it does not disapear under us (e.g. due to cancel)
2344 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_BL_DONE))) {
2345 list_add(&lock->l_pending_chain, list);
2346 LDLM_LOCK_GET(lock);
2349 return LDLM_ITER_CONTINUE;
2352 static int replay_lock_interpret(const struct lu_env *env,
2353 struct ptlrpc_request *req, void *args, int rc)
2355 struct ldlm_async_args *aa = args;
2356 struct ldlm_lock *lock;
2357 struct ldlm_reply *reply;
2358 struct obd_export *exp;
2361 atomic_dec(&req->rq_import->imp_replay_inflight);
2362 wake_up(&req->rq_import->imp_replay_waitq);
2367 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2369 GOTO(out, rc = -EPROTO);
2371 lock = ldlm_handle2lock(&aa->lock_handle);
2373 CERROR("received replay ack for unknown local cookie %#llx remote cookie %#llx from server %s id %s\n",
2374 aa->lock_handle.cookie, reply->lock_handle.cookie,
2375 req->rq_export->exp_client_uuid.uuid,
2376 libcfs_id2str(req->rq_peer));
2377 GOTO(out, rc = -ESTALE);
2380 /* Key change rehash lock in per-export hash with new key */
2381 exp = req->rq_export;
2382 if (exp && exp->exp_lock_hash) {
2384 * In the function below, .hs_keycmp resolves to
2385 * ldlm_export_lock_keycmp()
2387 /* coverity[overrun-buffer-val] */
2388 cfs_hash_rehash_key(exp->exp_lock_hash,
2389 &lock->l_remote_handle,
2390 &reply->lock_handle,
2393 lock->l_remote_handle = reply->lock_handle;
2396 LDLM_DEBUG(lock, "replayed lock:");
2397 ptlrpc_import_recovery_state_machine(req->rq_import);
2398 LDLM_LOCK_PUT(lock);
2401 ptlrpc_connect_import(req->rq_import);
2406 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2408 struct ptlrpc_request *req;
2409 struct ldlm_async_args *aa;
2410 struct ldlm_request *body;
2416 /* b=11974: Do not replay a lock which is actively being canceled */
2417 if (ldlm_is_bl_done(lock)) {
2418 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2423 * If this is reply-less callback lock, we cannot replay it, since
2424 * server might have long dropped it, but notification of that event was
2425 * lost by network. (and server granted conflicting lock already)
2427 if (ldlm_is_cancel_on_block(lock)) {
2428 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2429 ldlm_lock_cancel(lock);
2434 * If granted mode matches the requested mode, this lock is granted.
2436 * If we haven't been granted anything and are on a resource list,
2437 * then we're blocked/waiting.
2439 * If we haven't been granted anything and we're NOT on a resource list,
2440 * then we haven't got a reply yet and don't have a known disposition.
2441 * This happens whenever a lock enqueue is the request that triggers
2444 if (ldlm_is_granted(lock))
2445 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2446 else if (!list_empty(&lock->l_res_link))
2447 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2449 flags = LDLM_FL_REPLAY;
2451 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2452 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2456 /* We're part of recovery, so don't wait for it. */
2457 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2459 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2460 ldlm_lock2desc(lock, &body->lock_desc);
2461 body->lock_flags = ldlm_flags_to_wire(flags);
2463 ldlm_lock2handle(lock, &body->lock_handle[0]);
2464 if (lock->l_lvb_len > 0)
2465 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2466 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2468 ptlrpc_request_set_replen(req);
2470 * notify the server we've replayed all requests.
2471 * also, we mark the request to be put on a dedicated
2472 * queue to be processed after all request replayes.
2475 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2477 LDLM_DEBUG(lock, "replaying lock:");
2479 atomic_inc(&imp->imp_replay_inflight);
2480 aa = ptlrpc_req_async_args(aa, req);
2481 aa->lock_handle = body->lock_handle[0];
2482 req->rq_interpret_reply = replay_lock_interpret;
2483 ptlrpcd_add_req(req);
2489 * Cancel as many unused locks as possible before replay. since we are
2490 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2493 * Called only in recovery before replaying locks. there is no need to
2494 * replay locks that are unused. since the clients may hold thousands of
2495 * cached unused locks, dropping the unused locks can greatly reduce the
2496 * load on the servers at recovery time.
2498 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2504 "Dropping as many unused locks as possible before replay for namespace %s (%d)\n",
2505 ldlm_ns_name(ns), ns->ns_nr_unused);
2508 * We don't need to care whether or not LRU resize is enabled
2509 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
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 static int lock_can_replay(struct obd_import *imp)
2521 struct client_obd *cli = &imp->imp_obd->u.cli;
2523 CDEBUG(D_HA, "check lock replay limit, inflights = %u(%u)\n",
2524 atomic_read(&imp->imp_replay_inflight) - 1,
2525 cli->cl_max_rpcs_in_flight);
2527 /* +1 due to ldlm_lock_replay() increment */
2528 return atomic_read(&imp->imp_replay_inflight) <
2529 1 + min_t(u32, cli->cl_max_rpcs_in_flight, 8);
2532 int __ldlm_replay_locks(struct obd_import *imp, bool rate_limit)
2534 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2536 struct ldlm_lock *lock, *next;
2541 LASSERT(atomic_read(&imp->imp_replay_inflight) == 1);
2543 /* don't replay locks if import failed recovery */
2544 if (imp->imp_vbr_failed)
2547 if (ldlm_cancel_unused_locks_before_replay)
2548 ldlm_cancel_unused_locks_for_replay(ns);
2550 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2552 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2553 list_del_init(&lock->l_pending_chain);
2555 LDLM_LOCK_RELEASE(lock);
2556 continue; /* or try to do the rest? */
2558 rc = replay_one_lock(imp, lock);
2559 LDLM_LOCK_RELEASE(lock);
2562 wait_event_idle_exclusive(imp->imp_replay_waitq,
2563 lock_can_replay(imp));
2570 * Lock replay uses rate control and can sleep waiting so
2571 * must be in separate thread from ptlrpcd itself
2573 static int ldlm_lock_replay_thread(void *data)
2575 struct obd_import *imp = data;
2577 CDEBUG(D_HA, "lock replay thread %s to %s@%s\n",
2578 imp->imp_obd->obd_name, obd2cli_tgt(imp->imp_obd),
2579 imp->imp_connection->c_remote_uuid.uuid);
2581 __ldlm_replay_locks(imp, true);
2582 atomic_dec(&imp->imp_replay_inflight);
2583 ptlrpc_import_recovery_state_machine(imp);
2584 class_import_put(imp);
2589 int ldlm_replay_locks(struct obd_import *imp)
2591 struct task_struct *task;
2594 class_import_get(imp);
2595 /* ensure this doesn't fall to 0 before all have been queued */
2596 atomic_inc(&imp->imp_replay_inflight);
2598 task = kthread_run(ldlm_lock_replay_thread, imp, "ldlm_lock_replay");
2601 CDEBUG(D_HA, "can't start lock replay thread: rc = %d\n", rc);
2603 /* run lock replay without rate control */
2604 rc = __ldlm_replay_locks(imp, false);
2605 atomic_dec(&imp->imp_replay_inflight);
2606 class_import_put(imp);