4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * This file contains Asynchronous System Trap (AST) handlers and related
34 * LDLM request-processing routines.
36 * An AST is a callback issued on a lock when its state is changed. There are
37 * several different types of ASTs (callbacks) registered for each lock:
39 * - completion AST: when a lock is enqueued by some process, but cannot be
40 * granted immediately due to other conflicting locks on the same resource,
41 * the completion AST is sent to notify the caller when the lock is
44 * - blocking AST: when a lock is granted to some process, if another process
45 * enqueues a conflicting (blocking) lock on a resource, a blocking AST is
46 * sent to notify the holder(s) of the lock(s) of the conflicting lock
47 * request. The lock holder(s) must release their lock(s) on that resource in
48 * a timely manner or be evicted by the server.
50 * - glimpse AST: this is used when a process wants information about a lock
51 * (i.e. the lock value block (LVB)) but does not necessarily require holding
52 * the lock. If the resource is locked, the lock holder(s) are sent glimpse
53 * ASTs and the LVB is returned to the caller, and lock holder(s) may CANCEL
54 * their lock(s) if they are idle. If the resource is not locked, the server
58 #define DEBUG_SUBSYSTEM S_LDLM
60 #include <lustre_errno.h>
61 #include <lustre_dlm.h>
62 #include <obd_class.h>
65 #include "ldlm_internal.h"
67 unsigned int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
68 module_param(ldlm_enqueue_min, uint, 0644);
69 MODULE_PARM_DESC(ldlm_enqueue_min, "lock enqueue timeout minimum");
71 /* in client side, whether the cached locks will be canceled before replay */
72 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
74 struct lock_wait_data {
75 struct ldlm_lock *lwd_lock;
79 struct ldlm_async_args {
80 struct lustre_handle lock_handle;
84 * ldlm_request_bufsize
86 * If opcode=LDLM_ENQUEUE, 1 slot is already occupied,
87 * LDLM_LOCKREQ_HANDLE -1 slots are available.
88 * Otherwise, LDLM_LOCKREQ_HANDLE slots are available.
93 * \retval size of the request buffer
95 int ldlm_request_bufsize(int count, int type)
97 int avail = LDLM_LOCKREQ_HANDLES;
99 if (type == LDLM_ENQUEUE)
100 avail -= LDLM_ENQUEUE_CANCEL_OFF;
103 avail = (count - avail) * sizeof(struct lustre_handle);
107 return sizeof(struct ldlm_request) + avail;
110 void ldlm_expired_completion_wait(struct lock_wait_data *lwd)
112 struct ldlm_lock *lock = lwd->lwd_lock;
113 struct obd_import *imp;
114 struct obd_device *obd;
117 if (lock->l_conn_export == NULL) {
118 static time64_t next_dump, last_dump;
121 "lock timed out (enqueued at %lld, %llds ago); not entering recovery in server code, just going back to sleep",
122 (s64)lock->l_activity,
123 (s64)(ktime_get_real_seconds() -
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",
141 (s64)lock->l_activity,
142 (s64)(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 time64_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, (time64_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)
199 if (ldlm_is_destroyed(lock) || ldlm_is_failed(lock)) {
200 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
202 } else if (data == NULL) {
203 LDLM_DEBUG(lock, "client-side enqueue: granted");
205 /* Take into AT only CP RPC, not immediately granted locks */
206 delay = ktime_get_real_seconds() - lock->l_activity;
207 LDLM_DEBUG(lock, "client-side enqueue: granted after %llds",
210 /* Update our time estimate */
211 at_measured(ldlm_lock_to_ns_at(lock), delay);
217 * Implementation of ->l_completion_ast() for a client, that doesn't wait
218 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
219 * other threads that cannot block for long.
221 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data)
225 if (flags == LDLM_FL_WAIT_NOREPROC) {
226 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
230 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
231 wake_up(&lock->l_waitq);
232 RETURN(ldlm_completion_tail(lock, data));
236 "client-side enqueue returned a blocked lock, going forward");
237 ldlm_reprocess_all(lock->l_resource, NULL);
240 EXPORT_SYMBOL(ldlm_completion_ast_async);
243 * Generic LDLM "completion" AST. This is called in several cases:
245 * - when a reply to an ENQUEUE RPC is received from the server
246 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
247 * this point (determined by flags);
249 * - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
252 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
255 * - to force all locks when resource is destroyed (cleanup_resource());
257 * If lock is not granted in the first case, this function waits until second
258 * or penultimate cases happen in some other thread.
261 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
263 /* XXX ALLOCATE - 160 bytes */
264 struct lock_wait_data lwd;
265 struct obd_device *obd;
266 struct obd_import *imp = NULL;
272 if (flags == LDLM_FL_WAIT_NOREPROC) {
273 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
277 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
278 wake_up(&lock->l_waitq);
282 LDLM_DEBUG(lock, "client-side enqueue returned a blocked locksleeping");
286 obd = class_exp2obd(lock->l_conn_export);
288 /* if this is a local lock, then there is no import */
290 imp = obd->u.cli.cl_import;
292 timeout = ldlm_cp_timeout(lock);
295 lock->l_activity = ktime_get_real_seconds();
298 spin_lock(&imp->imp_lock);
299 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
300 spin_unlock(&imp->imp_lock);
303 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
304 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
305 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
306 ldlm_set_fail_loc(lock);
309 /* Go to sleep until the lock is granted or cancelled. */
310 if (ldlm_is_no_timeout(lock)) {
311 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
312 rc = l_wait_event_abortable(
314 is_granted_or_cancelled(lock));
316 if (wait_event_idle_timeout(
318 is_granted_or_cancelled(lock),
319 cfs_time_seconds(timeout)) == 0) {
320 ldlm_expired_completion_wait(&lwd);
321 rc = l_wait_event_abortable(
323 is_granted_or_cancelled(lock));
329 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
334 RETURN(ldlm_completion_tail(lock, data));
336 EXPORT_SYMBOL(ldlm_completion_ast);
339 * A helper to build a blocking AST function
341 * Perform a common operation for blocking ASTs:
342 * defferred lock cancellation.
344 * \param lock the lock blocking or canceling AST was called on
346 * \see mdt_blocking_ast
347 * \see ldlm_blocking_ast
349 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
355 ldlm_set_cbpending(lock);
356 do_ast = (!lock->l_readers && !lock->l_writers);
357 unlock_res_and_lock(lock);
360 struct lustre_handle lockh;
363 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
364 ldlm_lock2handle(lock, &lockh);
365 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
367 CERROR("ldlm_cli_cancel: %d\n", rc);
370 "Lock still has references, will be cancelled later");
374 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
377 * Server blocking AST
379 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
382 * \param lock the lock which blocks a request or cancelling lock
385 * \param flag indicates whether this cancelling or blocking callback
387 * \see ldlm_blocking_ast_nocheck
389 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
390 void *data, int flag)
394 if (flag == LDLM_CB_CANCELING) {
395 /* Don't need to do anything here. */
399 lock_res_and_lock(lock);
401 * Get this: if ldlm_blocking_ast is racing with intent_policy, such
402 * that ldlm_blocking_ast is called just before intent_policy method
403 * takes the lr_lock, then by the time we get the lock, we might not
404 * be the correct blocking function anymore. So check, and return
407 if (lock->l_blocking_ast != ldlm_blocking_ast) {
408 unlock_res_and_lock(lock);
411 RETURN(ldlm_blocking_ast_nocheck(lock));
413 EXPORT_SYMBOL(ldlm_blocking_ast);
416 * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
418 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
419 * rather subtle: with OST-side locking, it may so happen that _all_ extent
420 * locks are held by the OST. If client wants to obtain the current file size
421 * it calls ll_glimpse_size(), and (as all locks are held only on the server),
422 * this dummy glimpse callback fires and does nothing. The client still
423 * receives the correct file size due to the following fragment of code in
424 * ldlm_cb_interpret():
426 * if (rc == -ELDLM_NO_LOCK_DATA) {
427 * LDLM_DEBUG(lock, "lost race - client has a lock but no"
429 * ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
432 * That is, after the glimpse returns this error, ofd_lvbo_update() is called
433 * and returns the updated file attributes from the inode to the client.
435 * See also comment in ofd_intent_policy() on why servers must set a non-NULL
436 * l_glimpse_ast when grabbing DLM locks. Otherwise, the server will assume
437 * that the object is in the process of being destroyed.
439 * \param[in] lock DLM lock being glimpsed, unused
440 * \param[in] reqp pointer to ptlrpc_request, unused
442 * \retval -ELDLM_NO_LOCK_DATA to get attributes from disk object
444 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
446 return -ELDLM_NO_LOCK_DATA;
450 * Enqueue a local lock (typically on a server).
452 int ldlm_cli_enqueue_local(const struct lu_env *env,
453 struct ldlm_namespace *ns,
454 const struct ldlm_res_id *res_id,
455 enum ldlm_type type, union ldlm_policy_data *policy,
456 enum ldlm_mode mode, __u64 *flags,
457 ldlm_blocking_callback blocking,
458 ldlm_completion_callback completion,
459 ldlm_glimpse_callback glimpse,
460 void *data, __u32 lvb_len, enum lvb_type lvb_type,
461 const __u64 *client_cookie,
462 struct lustre_handle *lockh)
464 struct ldlm_lock *lock;
466 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
467 .lcs_blocking = blocking,
468 .lcs_glimpse = glimpse,
473 LASSERT(!(*flags & LDLM_FL_REPLAY));
474 if (unlikely(ns_is_client(ns))) {
475 CERROR("Trying to enqueue local lock in a shadow namespace\n");
479 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
482 GOTO(out_nolock, err = PTR_ERR(lock));
484 err = ldlm_lvbo_init(lock->l_resource);
486 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
487 ldlm_lock_destroy_nolock(lock);
491 ldlm_lock2handle(lock, lockh);
494 * NB: we don't have any lock now (lock_res_and_lock)
495 * because it's a new lock
497 ldlm_lock_addref_internal_nolock(lock, mode);
498 ldlm_set_local(lock);
499 if (*flags & LDLM_FL_ATOMIC_CB)
500 ldlm_set_atomic_cb(lock);
502 if (*flags & LDLM_FL_CANCEL_ON_BLOCK)
503 ldlm_set_cancel_on_block(lock);
506 lock->l_policy_data = *policy;
507 if (client_cookie != NULL)
508 lock->l_client_cookie = *client_cookie;
509 if (type == LDLM_EXTENT) {
510 /* extent lock without policy is a bug */
514 lock->l_req_extent = policy->l_extent;
517 err = ldlm_lock_enqueue(env, ns, &lock, policy, flags);
518 if (unlikely(err != ELDLM_OK))
522 *policy = lock->l_policy_data;
524 if (lock->l_completion_ast)
525 lock->l_completion_ast(lock, *flags, NULL);
527 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
530 LDLM_LOCK_RELEASE(lock);
534 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
536 static void failed_lock_cleanup(struct ldlm_namespace *ns,
537 struct ldlm_lock *lock, int mode)
541 /* Set a flag to prevent us from sending a CANCEL (b=407) */
542 lock_res_and_lock(lock);
543 /* Check that lock is not granted or failed, we might race. */
544 if (!ldlm_is_granted(lock) && !ldlm_is_failed(lock)) {
546 * Make sure that this lock will not be found by raced
547 * bl_ast and -EINVAL reply is sent to server anyways.
550 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
551 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
554 unlock_res_and_lock(lock);
558 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
560 LDLM_DEBUG(lock, "lock was granted or failed in race");
563 * XXX - HACK because we shouldn't call ldlm_lock_destroy()
564 * from llite/file.c/ll_file_flock().
567 * This code makes for the fact that we do not have blocking handler on
568 * a client for flock locks. As such this is the place where we must
569 * completely kill failed locks. (interrupted and those that
570 * were waiting to be granted when server evicted us.
572 if (lock->l_resource->lr_type == LDLM_FLOCK) {
573 lock_res_and_lock(lock);
574 if (!ldlm_is_destroyed(lock)) {
575 ldlm_resource_unlink_lock(lock);
576 ldlm_lock_decref_internal_nolock(lock, mode);
577 ldlm_lock_destroy_nolock(lock);
579 unlock_res_and_lock(lock);
581 ldlm_lock_decref_internal(lock, mode);
585 static bool ldlm_request_slot_needed(enum ldlm_type type)
587 return type == LDLM_FLOCK || type == LDLM_IBITS;
591 * Finishing portion of client lock enqueue code.
593 * Called after receiving reply from server.
595 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
596 enum ldlm_type type, __u8 with_policy,
597 enum ldlm_mode mode, __u64 *flags, void *lvb,
598 __u32 lvb_len, const struct lustre_handle *lockh,
601 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
602 const struct lu_env *env = NULL;
603 int is_replay = *flags & LDLM_FL_REPLAY;
604 struct ldlm_lock *lock;
605 struct ldlm_reply *reply;
606 int cleanup_phase = 1;
610 if (ldlm_request_slot_needed(type))
611 obd_put_request_slot(&req->rq_import->imp_obd->u.cli);
613 ptlrpc_put_mod_rpc_slot(req);
615 if (req && req->rq_svc_thread)
616 env = req->rq_svc_thread->t_env;
618 lock = ldlm_handle2lock(lockh);
619 /* ldlm_cli_enqueue is holding a reference on this lock. */
621 LASSERT(type == LDLM_FLOCK);
625 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
626 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
628 if (rc != ELDLM_OK) {
630 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
631 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
633 if (rc != ELDLM_LOCK_ABORTED)
637 /* Before we return, swab the reply */
638 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
640 GOTO(cleanup, rc = -EPROTO);
645 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
648 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
649 GOTO(cleanup, rc = size);
650 } else if (unlikely(size > lvb_len)) {
652 "Replied LVB is larger than expectation, expected = %d, replied = %d",
654 GOTO(cleanup, rc = -EINVAL);
659 if (rc == ELDLM_LOCK_ABORTED) {
660 if (lvb_len > 0 && lvb != NULL)
661 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
663 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
666 /* lock enqueued on the server */
669 lock_res_and_lock(lock);
670 /* Key change rehash lock in per-export hash with new key */
671 if (exp->exp_lock_hash) {
673 * In the function below, .hs_keycmp resolves to
674 * ldlm_export_lock_keycmp()
676 /* coverity[overrun-buffer-val] */
677 cfs_hash_rehash_key(exp->exp_lock_hash,
678 &lock->l_remote_handle,
682 lock->l_remote_handle = reply->lock_handle;
685 *flags = ldlm_flags_from_wire(reply->lock_flags);
686 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
687 LDLM_FL_INHERIT_MASK);
688 unlock_res_and_lock(lock);
690 CDEBUG(D_INFO, "local: %p, remote cookie: %#llx, flags: %#llx\n",
691 lock, reply->lock_handle.cookie, *flags);
694 * If enqueue returned a blocked lock but the completion handler has
695 * already run, then it fixed up the resource and we don't need to do it
698 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
699 int newmode = reply->lock_desc.l_req_mode;
702 if (newmode && newmode != lock->l_req_mode) {
703 LDLM_DEBUG(lock, "server returned different mode %s",
704 ldlm_lockname[newmode]);
705 lock->l_req_mode = newmode;
708 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
709 &lock->l_resource->lr_name)) {
711 "remote intent success, locking "DLDLMRES", instead of "DLDLMRES"\n",
712 PLDLMRES(&reply->lock_desc.l_resource),
713 PLDLMRES(lock->l_resource));
715 rc = ldlm_lock_change_resource(ns, lock,
716 &reply->lock_desc.l_resource.lr_name);
717 if (rc || lock->l_resource == NULL)
718 GOTO(cleanup, rc = -ENOMEM);
719 LDLM_DEBUG(lock, "client-side enqueue, new resource");
723 /* We assume lock type cannot change on server*/
724 ldlm_convert_policy_to_local(exp,
725 lock->l_resource->lr_type,
726 &reply->lock_desc.l_policy_data,
727 &lock->l_policy_data);
730 if (type != LDLM_PLAIN)
732 "client-side enqueue, new policy data");
735 if ((*flags) & LDLM_FL_AST_SENT) {
736 lock_res_and_lock(lock);
737 ldlm_bl_desc2lock(&reply->lock_desc, lock);
738 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
739 unlock_res_and_lock(lock);
740 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
744 * If the lock has already been granted by a completion AST, don't
745 * clobber the LVB with an older one.
749 * We must lock or a racing completion might update lvb without
750 * letting us know and we'll clobber the correct value.
751 * Cannot unlock after the check either, a that still leaves
752 * a tiny window for completion to get in
754 lock_res_and_lock(lock);
755 if (!ldlm_is_granted(lock))
756 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
757 lock->l_lvb_data, lvb_len);
758 unlock_res_and_lock(lock);
766 rc = ldlm_lock_enqueue(env, ns, &lock, NULL, flags);
767 if (lock->l_completion_ast != NULL) {
768 int err = lock->l_completion_ast(lock, *flags, NULL);
777 if (lvb_len > 0 && lvb != NULL) {
779 * Copy the LVB here, and not earlier, because the completion
780 * AST (if any) can override what we got in the reply
782 memcpy(lvb, lock->l_lvb_data, lvb_len);
785 LDLM_DEBUG(lock, "client-side enqueue END");
788 if (cleanup_phase == 1 && rc)
789 failed_lock_cleanup(ns, lock, mode);
790 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
792 LDLM_LOCK_RELEASE(lock);
795 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
798 * Estimate number of lock handles that would fit into request of given
799 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
800 * a single page on the send/receive side. XXX: 512 should be changed to
801 * more adequate value.
803 static inline int ldlm_req_handles_avail(int req_size, int off)
807 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
808 if (likely(avail >= 0))
809 avail /= (int)sizeof(struct lustre_handle);
812 avail += LDLM_LOCKREQ_HANDLES - off;
817 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
818 enum req_location loc,
821 __u32 size = req_capsule_msg_size(pill, loc);
823 return ldlm_req_handles_avail(size, off);
826 static inline int ldlm_format_handles_avail(struct obd_import *imp,
827 const struct req_format *fmt,
828 enum req_location loc, int off)
830 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
832 return ldlm_req_handles_avail(size, off);
836 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
837 * \a count locks in \a cancels.
839 * This is to be called by functions preparing their own requests that
840 * might contain lists of locks to cancel in addition to actual operation
841 * that needs to be performed.
843 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
844 int version, int opc, int canceloff,
845 struct list_head *cancels, int count)
847 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
848 struct req_capsule *pill = &req->rq_pill;
849 struct ldlm_request *dlm = NULL;
851 enum ldlm_lru_flags lru_flags;
852 int avail, to_free, pack = 0;
859 if (ns_connect_cancelset(ns)) {
860 /* Estimate the amount of available space in the request. */
861 req_capsule_filled_sizes(pill, RCL_CLIENT);
862 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
864 lru_flags = LDLM_LRU_FLAG_NO_WAIT | (ns_connect_lru_resize(ns) ?
865 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED);
866 to_free = !ns_connect_lru_resize(ns) &&
867 opc == LDLM_ENQUEUE ? 1 : 0;
870 * Cancel LRU locks here _only_ if the server supports
871 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
872 * RPC, which will make us slower.
875 count += ldlm_cancel_lru_local(ns, cancels, to_free,
882 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
883 ldlm_request_bufsize(pack, opc));
886 rc = ptlrpc_request_pack(req, version, opc);
888 ldlm_lock_list_put(cancels, l_bl_ast, count);
892 if (ns_connect_cancelset(ns)) {
894 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
897 * Skip first lock handler in ldlm_request_pack(),
898 * this method will increment @lock_count according
899 * to the lock handle amount actually written to
902 dlm->lock_count = canceloff;
904 /* Pack into the request @pack lock handles. */
905 ldlm_cli_cancel_list(cancels, pack, req, 0);
906 /* Prepare and send separate cancel RPC for others. */
907 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
909 ldlm_lock_list_put(cancels, l_bl_ast, count);
913 EXPORT_SYMBOL(ldlm_prep_elc_req);
915 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
916 struct list_head *cancels, int count)
918 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
919 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
921 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
923 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
925 struct ptlrpc_request *req;
930 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
932 RETURN(ERR_PTR(-ENOMEM));
934 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
936 ptlrpc_request_free(req);
940 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
941 ptlrpc_request_set_replen(req);
944 EXPORT_SYMBOL(ldlm_enqueue_pack);
947 * Client-side lock enqueue.
949 * If a request has some specific initialisation it is passed in \a reqp,
950 * otherwise it is created in ldlm_cli_enqueue.
952 * Supports sync and async requests, pass \a async flag accordingly. If a
953 * request was created in ldlm_cli_enqueue and it is the async request,
954 * pass it to the caller in \a reqp.
956 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
957 struct ldlm_enqueue_info *einfo,
958 const struct ldlm_res_id *res_id,
959 union ldlm_policy_data const *policy, __u64 *flags,
960 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
961 struct lustre_handle *lockh, int async)
963 struct ldlm_namespace *ns;
964 struct ldlm_lock *lock;
965 struct ldlm_request *body;
966 int is_replay = *flags & LDLM_FL_REPLAY;
967 int req_passed_in = 1;
969 struct ptlrpc_request *req;
973 LASSERT(exp != NULL);
975 ns = exp->exp_obd->obd_namespace;
978 * If we're replaying this lock, just check some invariants.
979 * If we're creating a new lock, get everything all setup nice.
982 lock = ldlm_handle2lock_long(lockh, 0);
983 LASSERT(lock != NULL);
984 LDLM_DEBUG(lock, "client-side enqueue START");
985 LASSERT(exp == lock->l_conn_export);
987 const struct ldlm_callback_suite cbs = {
988 .lcs_completion = einfo->ei_cb_cp,
989 .lcs_blocking = einfo->ei_cb_bl,
990 .lcs_glimpse = einfo->ei_cb_gl
992 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
993 einfo->ei_mode, &cbs, einfo->ei_cbdata,
996 RETURN(PTR_ERR(lock));
998 if (einfo->ei_cb_created)
999 einfo->ei_cb_created(lock);
1001 /* for the local lock, add the reference */
1002 ldlm_lock_addref_internal(lock, einfo->ei_mode);
1003 ldlm_lock2handle(lock, lockh);
1005 lock->l_policy_data = *policy;
1007 if (einfo->ei_type == LDLM_EXTENT) {
1008 /* extent lock without policy is a bug */
1012 lock->l_req_extent = policy->l_extent;
1014 LDLM_DEBUG(lock, "client-side enqueue START, flags %#llx",
1018 lock->l_conn_export = exp;
1019 lock->l_export = NULL;
1020 lock->l_blocking_ast = einfo->ei_cb_bl;
1021 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL |
1022 LDLM_FL_ATOMIC_CB));
1023 lock->l_activity = ktime_get_real_seconds();
1025 /* lock not sent to server yet */
1026 if (reqp == NULL || *reqp == NULL) {
1027 req = ldlm_enqueue_pack(exp, lvb_len);
1029 failed_lock_cleanup(ns, lock, einfo->ei_mode);
1030 LDLM_LOCK_RELEASE(lock);
1031 RETURN(PTR_ERR(req));
1041 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
1043 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
1044 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
1047 if (*flags & LDLM_FL_NDELAY) {
1048 DEBUG_REQ(D_DLMTRACE, req, "enqueue lock with no delay");
1049 req->rq_no_resend = req->rq_no_delay = 1;
1051 * probably set a shorter timeout value and handle ETIMEDOUT
1052 * in osc_lock_upcall() correctly
1054 /* lustre_msg_set_timeout(req, req->rq_timeout / 2); */
1057 /* Dump lock data into the request buffer */
1058 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1059 ldlm_lock2desc(lock, &body->lock_desc);
1060 body->lock_flags = ldlm_flags_to_wire(*flags);
1061 body->lock_handle[0] = *lockh;
1063 /* extended LDLM opcodes in client stats */
1064 if (exp->exp_obd->obd_svc_stats != NULL) {
1065 bool glimpse = *flags & LDLM_FL_HAS_INTENT;
1067 /* OST glimpse has no intent buffer */
1068 if (req_capsule_has_field(&req->rq_pill, &RMF_LDLM_INTENT,
1070 struct ldlm_intent *it;
1072 it = req_capsule_client_get(&req->rq_pill,
1074 glimpse = (it && (it->opc == IT_GLIMPSE));
1078 ldlm_svc_get_eopc(body, exp->exp_obd->obd_svc_stats);
1080 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1082 LDLM_GLIMPSE_ENQUEUE);
1085 /* It is important to obtain modify RPC slot first (if applicable), so
1086 * that threads that are waiting for a modify RPC slot are not polluting
1087 * our rpcs in flight counter. */
1089 if (einfo->ei_enq_slot)
1090 ptlrpc_get_mod_rpc_slot(req);
1092 if (ldlm_request_slot_needed(einfo->ei_type)) {
1093 rc = obd_get_request_slot(&req->rq_import->imp_obd->u.cli);
1095 if (einfo->ei_enq_slot)
1096 ptlrpc_put_mod_rpc_slot(req);
1097 failed_lock_cleanup(ns, lock, einfo->ei_mode);
1098 LDLM_LOCK_RELEASE(lock);
1104 LASSERT(reqp != NULL);
1108 LDLM_DEBUG(lock, "sending request");
1110 rc = ptlrpc_queue_wait(req);
1112 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
1113 einfo->ei_mode, flags, lvb, lvb_len,
1117 * If ldlm_cli_enqueue_fini did not find the lock, we need to free
1118 * one reference that we took
1121 LDLM_LOCK_RELEASE(lock);
1126 if (!req_passed_in && req != NULL) {
1127 ptlrpc_req_finished(req);
1134 EXPORT_SYMBOL(ldlm_cli_enqueue);
1137 * Client-side IBITS lock convert.
1139 * Inform server that lock has been converted instead of canceling.
1140 * Server finishes convert on own side and does reprocess to grant
1141 * all related waiting locks.
1143 * Since convert means only ibits downgrading, client doesn't need to
1144 * wait for server reply to finish local converting process so this request
1145 * is made asynchronous.
1148 int ldlm_cli_convert_req(struct ldlm_lock *lock, __u32 *flags, __u64 new_bits)
1150 struct ldlm_request *body;
1151 struct ptlrpc_request *req;
1152 struct obd_export *exp = lock->l_conn_export;
1156 LASSERT(exp != NULL);
1159 * this is better to check earlier and it is done so already,
1160 * but this check is kept too as final one to issue an error
1161 * if any new code will miss such check.
1163 if (!exp_connect_lock_convert(exp)) {
1164 LDLM_ERROR(lock, "server doesn't support lock convert\n");
1168 if (lock->l_resource->lr_type != LDLM_IBITS) {
1169 LDLM_ERROR(lock, "convert works with IBITS locks only.");
1173 LDLM_DEBUG(lock, "client-side convert");
1175 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1176 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1181 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1182 body->lock_handle[0] = lock->l_remote_handle;
1184 body->lock_desc.l_req_mode = lock->l_req_mode;
1185 body->lock_desc.l_granted_mode = lock->l_granted_mode;
1187 body->lock_desc.l_policy_data.l_inodebits.bits = new_bits;
1188 body->lock_desc.l_policy_data.l_inodebits.cancel_bits = 0;
1190 body->lock_flags = ldlm_flags_to_wire(*flags);
1191 body->lock_count = 1;
1193 ptlrpc_request_set_replen(req);
1196 * Use cancel portals for convert as well as high-priority handling.
1198 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1199 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1201 ptlrpc_at_set_req_timeout(req);
1203 if (exp->exp_obd->obd_svc_stats != NULL)
1204 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1205 LDLM_CONVERT - LDLM_FIRST_OPC);
1207 ptlrpcd_add_req(req);
1212 * Cancel locks locally.
1214 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1215 * \retval LDLM_FL_CANCELING otherwise;
1216 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1218 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1220 __u64 rc = LDLM_FL_LOCAL_ONLY;
1224 if (lock->l_conn_export) {
1227 LDLM_DEBUG(lock, "client-side cancel");
1228 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL_LOCAL,
1231 /* Set this flag to prevent others from getting new references*/
1232 lock_res_and_lock(lock);
1233 ldlm_set_cbpending(lock);
1234 local_only = !!(lock->l_flags &
1235 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1236 ldlm_cancel_callback(lock);
1237 rc = (ldlm_is_bl_ast(lock)) ?
1238 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1239 unlock_res_and_lock(lock);
1243 "not sending request (at caller's instruction)\n");
1244 rc = LDLM_FL_LOCAL_ONLY;
1246 ldlm_lock_cancel(lock);
1248 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1249 LDLM_ERROR(lock, "Trying to cancel local lock");
1252 LDLM_DEBUG(lock, "server-side local cancel");
1253 ldlm_lock_cancel(lock);
1254 ldlm_reprocess_all(lock->l_resource, lock);
1261 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1263 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1264 struct list_head *head, int count)
1266 struct ldlm_request *dlm;
1267 struct ldlm_lock *lock;
1268 int max, packed = 0;
1272 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1273 LASSERT(dlm != NULL);
1275 /* Check the room in the request buffer. */
1276 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1277 sizeof(struct ldlm_request);
1278 max /= sizeof(struct lustre_handle);
1279 max += LDLM_LOCKREQ_HANDLES;
1280 LASSERT(max >= dlm->lock_count + count);
1283 * XXX: it would be better to pack lock handles grouped by resource.
1284 * so that the server cancel would call filter_lvbo_update() less
1287 list_for_each_entry(lock, head, l_bl_ast) {
1290 LASSERT(lock->l_conn_export);
1291 /* Pack the lock handle to the given request buffer. */
1292 LDLM_DEBUG(lock, "packing");
1293 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1296 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1301 * Prepare and send a batched cancel RPC. It will include \a count lock
1302 * handles of locks given in \a cancels list.
1304 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1305 int count, enum ldlm_cancel_flags flags)
1307 struct ptlrpc_request *req = NULL;
1308 struct obd_import *imp;
1314 LASSERT(exp != NULL);
1317 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1319 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1322 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1323 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1328 imp = class_exp2cliimp(exp);
1329 if (imp == NULL || imp->imp_invalid) {
1331 "skipping cancel on invalid import %p\n", imp);
1335 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1337 GOTO(out, rc = -ENOMEM);
1339 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1340 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1341 ldlm_request_bufsize(count, LDLM_CANCEL));
1343 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1345 ptlrpc_request_free(req);
1350 * If OSP want cancel cross-MDT lock, let's not block it in
1351 * in recovery, otherwise the lock will not released, if
1352 * the remote target is also in recovery, and it also need
1353 * this lock, it might cause deadlock.
1355 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1356 exp->exp_obd->obd_lu_dev != NULL &&
1357 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1358 struct lu_device *top_dev;
1360 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1361 if (top_dev != NULL &&
1362 top_dev->ld_obd->obd_recovering)
1363 req->rq_allow_replay = 1;
1366 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1367 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1368 ptlrpc_at_set_req_timeout(req);
1370 ldlm_cancel_pack(req, cancels, count);
1372 ptlrpc_request_set_replen(req);
1373 if (flags & LCF_ASYNC) {
1374 ptlrpcd_add_req(req);
1379 rc = ptlrpc_queue_wait(req);
1380 if (rc == LUSTRE_ESTALE) {
1382 "client/server (nid %s) out of sync -- not fatal\n",
1383 libcfs_nid2str(req->rq_import->imp_connection->c_peer.nid));
1385 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1386 req->rq_import_generation == imp->imp_generation) {
1387 ptlrpc_req_finished(req);
1389 } else if (rc != ELDLM_OK) {
1390 /* -ESHUTDOWN is common on umount */
1391 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1392 "Got rc %d from cancel RPC: canceling anyway\n",
1400 ptlrpc_req_finished(req);
1403 return sent ? sent : rc;
1406 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1408 LASSERT(imp != NULL);
1409 return &imp->imp_obd->obd_namespace->ns_pool;
1413 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1415 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1417 struct obd_device *obd;
1422 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1423 !imp_connect_lru_resize(req->rq_import)))
1424 /* Do nothing for corner cases. */
1428 * In some cases RPC may contain SLV and limit zeroed out. This
1429 * is the case when server does not support LRU resize feature.
1430 * This is also possible in some recovery cases when server-side
1431 * reqs have no reference to the OBD export and thus access to
1432 * server-side namespace is not possible.
1434 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1435 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1436 DEBUG_REQ(D_HA, req,
1437 "Zero SLV or limit found (SLV=%llu, limit=%u)",
1438 lustre_msg_get_slv(req->rq_repmsg),
1439 lustre_msg_get_limit(req->rq_repmsg));
1443 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1444 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1445 obd = req->rq_import->imp_obd;
1448 * Set new SLV and limit in OBD fields to make them accessible
1449 * to the pool thread. We do not access obd_namespace and pool
1450 * directly here as there is no reliable way to make sure that
1451 * they are still alive at cleanup time. Evil races are possible
1452 * which may cause Oops at that time.
1454 write_lock(&obd->obd_pool_lock);
1455 obd->obd_pool_slv = new_slv;
1456 obd->obd_pool_limit = new_limit;
1457 write_unlock(&obd->obd_pool_lock);
1462 int ldlm_cli_convert(struct ldlm_lock *lock,
1463 enum ldlm_cancel_flags cancel_flags)
1467 LASSERT(!lock->l_readers && !lock->l_writers);
1468 LDLM_DEBUG(lock, "client lock convert START");
1470 if (lock->l_resource->lr_type == LDLM_IBITS) {
1471 lock_res_and_lock(lock);
1473 rc = ldlm_cli_inodebits_convert(lock, cancel_flags);
1474 } while (rc == -EAGAIN);
1475 unlock_res_and_lock(lock);
1478 LDLM_DEBUG(lock, "client lock convert END");
1481 EXPORT_SYMBOL(ldlm_cli_convert);
1484 * Client side lock cancel.
1486 * Lock must not have any readers or writers by this time.
1488 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1489 enum ldlm_cancel_flags cancel_flags)
1491 struct obd_export *exp;
1492 enum ldlm_lru_flags lru_flags;
1493 int avail, count = 1;
1495 struct ldlm_namespace *ns;
1496 struct ldlm_lock *lock;
1501 lock = ldlm_handle2lock_long(lockh, 0);
1503 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1507 lock_res_and_lock(lock);
1508 LASSERT(!ldlm_is_converting(lock));
1510 /* Lock is being canceled and the caller doesn't want to wait */
1511 if (ldlm_is_canceling(lock)) {
1512 if (cancel_flags & LCF_ASYNC) {
1513 unlock_res_and_lock(lock);
1515 unlock_res_and_lock(lock);
1516 wait_event_idle(lock->l_waitq, is_bl_done(lock));
1518 LDLM_LOCK_RELEASE(lock);
1522 ldlm_set_canceling(lock);
1523 unlock_res_and_lock(lock);
1525 if (cancel_flags & LCF_LOCAL)
1526 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE,
1529 rc = ldlm_cli_cancel_local(lock);
1530 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1531 LDLM_LOCK_RELEASE(lock);
1535 * Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1536 * RPC which goes to canceld portal, so we can cancel other LRU locks
1537 * here and send them all as one LDLM_CANCEL RPC.
1539 LASSERT(list_empty(&lock->l_bl_ast));
1540 list_add(&lock->l_bl_ast, &cancels);
1542 exp = lock->l_conn_export;
1543 if (exp_connect_cancelset(exp)) {
1544 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1549 ns = ldlm_lock_to_ns(lock);
1550 lru_flags = ns_connect_lru_resize(ns) ?
1551 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1552 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1553 LCF_BL_AST, lru_flags);
1555 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1558 EXPORT_SYMBOL(ldlm_cli_cancel);
1561 * Locally cancel up to \a count locks in list \a cancels.
1562 * Return the number of cancelled locks.
1564 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1565 enum ldlm_cancel_flags cancel_flags)
1568 struct ldlm_lock *lock, *next;
1569 int left = 0, bl_ast = 0;
1573 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1577 if (cancel_flags & LCF_LOCAL) {
1578 rc = LDLM_FL_LOCAL_ONLY;
1579 ldlm_lock_cancel(lock);
1581 rc = ldlm_cli_cancel_local(lock);
1584 * Until we have compound requests and can send LDLM_CANCEL
1585 * requests batched with generic RPCs, we need to send cancels
1586 * with the LDLM_FL_BL_AST flag in a separate RPC from
1587 * the one being generated now.
1589 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1590 LDLM_DEBUG(lock, "Cancel lock separately");
1591 list_move(&lock->l_bl_ast, &head);
1595 if (rc == LDLM_FL_LOCAL_ONLY) {
1596 /* CANCEL RPC should not be sent to server. */
1597 list_del_init(&lock->l_bl_ast);
1598 LDLM_LOCK_RELEASE(lock);
1604 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1611 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1612 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1613 * readahead requests, ...)
1615 static enum ldlm_policy_res
1616 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1617 int unused, int added, int count)
1619 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1622 * don't check added & count since we want to process all locks
1624 * It's fine to not take lock to access lock->l_resource since
1625 * the lock has already been granted so it won't change.
1627 switch (lock->l_resource->lr_type) {
1630 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1634 result = LDLM_POLICY_SKIP_LOCK;
1642 * Callback function for LRU-resize policy. Decides whether to keep
1643 * \a lock in LRU for current \a LRU size \a unused, added in current
1644 * scan \a added and number of locks to be preferably canceled \a count.
1646 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1648 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1650 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1651 struct ldlm_lock *lock,
1652 int unused, int added,
1655 ktime_t cur = ktime_get();
1656 struct ldlm_pool *pl = &ns->ns_pool;
1661 * Stop LRU processing when we reach past @count or have checked all
1664 if (count && added >= count)
1665 return LDLM_POLICY_KEEP_LOCK;
1668 * Despite of the LV, It doesn't make sense to keep the lock which
1669 * is unused for ns_max_age time.
1671 if (ktime_after(ktime_get(),
1672 ktime_add(lock->l_last_used, ns->ns_max_age)))
1673 return LDLM_POLICY_CANCEL_LOCK;
1675 slv = ldlm_pool_get_slv(pl);
1676 lvf = ldlm_pool_get_lvf(pl);
1677 la = div_u64(ktime_to_ns(ktime_sub(cur, lock->l_last_used)),
1679 lv = lvf * la * unused;
1681 /* Inform pool about current CLV to see it via debugfs. */
1682 ldlm_pool_set_clv(pl, lv);
1685 * Stop when SLV is not yet come from server or lv is smaller than
1688 if (slv == 0 || lv < slv)
1689 return LDLM_POLICY_KEEP_LOCK;
1691 return LDLM_POLICY_CANCEL_LOCK;
1694 static enum ldlm_policy_res
1695 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1696 struct ldlm_lock *lock,
1697 int unused, int added,
1700 enum ldlm_policy_res result;
1702 result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1703 if (result == LDLM_POLICY_KEEP_LOCK)
1706 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1710 * Callback function for debugfs used policy. Makes decision whether to keep
1711 * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1712 * added and number of locks to be preferably canceled \a count.
1714 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1716 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1718 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1719 struct ldlm_lock *lock,
1720 int unused, int added,
1724 * Stop LRU processing when we reach past @count or have checked all
1727 return (added >= count) ?
1728 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1732 * Callback function for aged policy. Makes decision whether to keep \a lock in
1733 * LRU for current LRU size \a unused, added in current scan \a added and
1734 * number of locks to be preferably canceled \a count.
1736 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1738 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1740 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1741 struct ldlm_lock *lock,
1742 int unused, int added,
1745 if ((added >= count) &&
1746 ktime_before(ktime_get(),
1747 ktime_add(lock->l_last_used, ns->ns_max_age)))
1748 return LDLM_POLICY_KEEP_LOCK;
1750 return LDLM_POLICY_CANCEL_LOCK;
1753 static enum ldlm_policy_res
1754 ldlm_cancel_aged_no_wait_policy(struct ldlm_namespace *ns,
1755 struct ldlm_lock *lock,
1756 int unused, int added, int count)
1758 enum ldlm_policy_res result;
1760 result = ldlm_cancel_aged_policy(ns, lock, unused, added, count);
1761 if (result == LDLM_POLICY_KEEP_LOCK)
1764 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1768 * Callback function for default policy. Makes decision whether to keep \a lock
1769 * in LRU for current LRU size \a unused, added in current scan \a added and
1770 * number of locks to be preferably canceled \a count.
1772 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1774 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1777 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1778 struct ldlm_lock *lock,
1779 int unused, int added,
1783 * Stop LRU processing when we reach past count or have checked all
1786 return (added >= count) ?
1787 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1790 typedef enum ldlm_policy_res
1791 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1792 int unused, int added, int count);
1794 static ldlm_cancel_lru_policy_t
1795 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1797 if (ns_connect_lru_resize(ns)) {
1798 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1799 /* We kill passed number of old locks. */
1800 return ldlm_cancel_passed_policy;
1801 if (lru_flags & LDLM_LRU_FLAG_LRUR) {
1802 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1803 return ldlm_cancel_lrur_no_wait_policy;
1805 return ldlm_cancel_lrur_policy;
1807 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1808 return ldlm_cancel_passed_policy;
1810 if (lru_flags & LDLM_LRU_FLAG_AGED) {
1811 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1812 return ldlm_cancel_aged_no_wait_policy;
1814 return ldlm_cancel_aged_policy;
1817 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1818 return ldlm_cancel_no_wait_policy;
1820 return ldlm_cancel_default_policy;
1824 * - Free space in LRU for \a count new locks,
1825 * redundant unused locks are canceled locally;
1826 * - also cancel locally unused aged locks;
1827 * - do not cancel more than \a max locks;
1828 * - GET the found locks and add them into the \a cancels list.
1830 * A client lock can be added to the l_bl_ast list only when it is
1831 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1832 * CANCEL. There are the following use cases:
1833 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1834 * ldlm_cli_cancel(), which check and set this flag properly. As any
1835 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1836 * later without any special locking.
1838 * Calling policies for enabled LRU resize:
1839 * ----------------------------------------
1840 * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1841 * cancel not more than \a count locks;
1843 * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1844 * at the beginning of LRU list);
1846 * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1847 * to memory pressre policy function;
1849 * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1851 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1852 * (typically before replaying locks) w/o
1853 * sending any RPCs or waiting for any
1854 * outstanding RPC to complete.
1856 * flags & LDLM_CANCEL_CLEANUP - when cancelling read locks, do not check for
1857 * other read locks covering the same pages, just
1858 * discard those pages.
1860 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1861 struct list_head *cancels, int count, int max,
1862 enum ldlm_lru_flags lru_flags)
1864 ldlm_cancel_lru_policy_t pf;
1866 int no_wait = lru_flags & LDLM_LRU_FLAG_NO_WAIT;
1870 if (!ns_connect_lru_resize(ns))
1871 count += ns->ns_nr_unused - ns->ns_max_unused;
1873 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1874 LASSERT(pf != NULL);
1876 /* For any flags, stop scanning if @max is reached. */
1877 while (!list_empty(&ns->ns_unused_list) && (max == 0 || added < max)) {
1878 struct ldlm_lock *lock;
1879 struct list_head *item, *next;
1880 enum ldlm_policy_res result;
1881 ktime_t last_use = ktime_set(0, 0);
1883 spin_lock(&ns->ns_lock);
1884 item = no_wait ? ns->ns_last_pos : &ns->ns_unused_list;
1885 for (item = item->next, next = item->next;
1886 item != &ns->ns_unused_list;
1887 item = next, next = item->next) {
1888 lock = list_entry(item, struct ldlm_lock, l_lru);
1890 /* No locks which got blocking requests. */
1891 LASSERT(!ldlm_is_bl_ast(lock));
1893 if (!ldlm_is_canceling(lock))
1897 * Somebody is already doing CANCEL. No need for this
1898 * lock in LRU, do not traverse it again.
1900 ldlm_lock_remove_from_lru_nolock(lock);
1902 if (item == &ns->ns_unused_list) {
1903 spin_unlock(&ns->ns_lock);
1907 last_use = lock->l_last_used;
1909 LDLM_LOCK_GET(lock);
1910 spin_unlock(&ns->ns_lock);
1911 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1914 * Pass the lock through the policy filter and see if it
1915 * should stay in LRU.
1917 * Even for shrinker policy we stop scanning if
1918 * we find a lock that should stay in the cache.
1919 * We should take into account lock age anyway
1920 * as a new lock is a valuable resource even if
1921 * it has a low weight.
1923 * That is, for shrinker policy we drop only
1924 * old locks, but additionally choose them by
1925 * their weight. Big extent locks will stay in
1928 result = pf(ns, lock, ns->ns_nr_unused, added, count);
1929 if (result == LDLM_POLICY_KEEP_LOCK) {
1930 lu_ref_del(&lock->l_reference, __func__, current);
1931 LDLM_LOCK_RELEASE(lock);
1935 if (result == LDLM_POLICY_SKIP_LOCK) {
1936 lu_ref_del(&lock->l_reference, __func__, current);
1938 spin_lock(&ns->ns_lock);
1939 if (!list_empty(&lock->l_lru) &&
1940 lock->l_lru.prev == ns->ns_last_pos)
1941 ns->ns_last_pos = &lock->l_lru;
1942 spin_unlock(&ns->ns_lock);
1945 LDLM_LOCK_RELEASE(lock);
1949 lock_res_and_lock(lock);
1950 /* Check flags again under the lock. */
1951 if (ldlm_is_canceling(lock) ||
1952 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1954 * Another thread is removing lock from LRU, or
1955 * somebody is already doing CANCEL, or there
1956 * is a blocking request which will send cancel
1957 * by itself, or the lock is no longer unused or
1958 * the lock has been used since the pf() call and
1959 * pages could be put under it.
1961 unlock_res_and_lock(lock);
1962 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1963 LDLM_LOCK_RELEASE(lock);
1966 LASSERT(!lock->l_readers && !lock->l_writers);
1969 * If we have chosen to cancel this lock voluntarily, we
1970 * better send cancel notification to server, so that it
1971 * frees appropriate state. This might lead to a race
1972 * where while we are doing cancel here, server is also
1973 * silently cancelling this lock.
1975 ldlm_clear_cancel_on_block(lock);
1978 * Setting the CBPENDING flag is a little misleading,
1979 * but prevents an important race; namely, once
1980 * CBPENDING is set, the lock can accumulate no more
1981 * readers/writers. Since readers and writers are
1982 * already zero here, ldlm_lock_decref() won't see
1983 * this flag and call l_blocking_ast
1985 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1987 if ((lru_flags & LDLM_LRU_FLAG_CLEANUP) &&
1988 (lock->l_resource->lr_type == LDLM_EXTENT ||
1989 ldlm_has_dom(lock)) && lock->l_granted_mode == LCK_PR)
1990 ldlm_set_discard_data(lock);
1993 * We can't re-add to l_lru as it confuses the
1994 * refcounting in ldlm_lock_remove_from_lru() if an AST
1995 * arrives after we drop lr_lock below. We use l_bl_ast
1996 * and can't use l_pending_chain as it is used both on
1997 * server and client nevertheless b=5666 says it is
1998 * used only on server
2000 LASSERT(list_empty(&lock->l_bl_ast));
2001 list_add(&lock->l_bl_ast, cancels);
2002 unlock_res_and_lock(lock);
2003 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
2009 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
2011 enum ldlm_cancel_flags cancel_flags,
2012 enum ldlm_lru_flags lru_flags)
2016 added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
2020 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
2024 * Cancel at least \a nr locks from given namespace LRU.
2026 * When called with LCF_ASYNC the blocking callback will be handled
2027 * in a thread and this function will return after the thread has been
2028 * asked to call the callback. When called with LCF_ASYNC the blocking
2029 * callback will be performed in this function.
2031 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
2032 enum ldlm_cancel_flags cancel_flags,
2033 enum ldlm_lru_flags lru_flags)
2041 * Just prepare the list of locks, do not actually cancel them yet.
2042 * Locks are cancelled later in a separate thread.
2044 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
2045 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
2053 * Find and cancel locally unused locks found on resource, matched to the
2054 * given policy, mode. GET the found locks and add them into the \a cancels
2057 int ldlm_cancel_resource_local(struct ldlm_resource *res,
2058 struct list_head *cancels,
2059 union ldlm_policy_data *policy,
2060 enum ldlm_mode mode, __u64 lock_flags,
2061 enum ldlm_cancel_flags cancel_flags,
2064 struct ldlm_lock *lock;
2070 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
2071 if (opaque != NULL && lock->l_ast_data != opaque) {
2072 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
2073 lock->l_ast_data, opaque);
2077 if (lock->l_readers || lock->l_writers)
2081 * If somebody is already doing CANCEL, or blocking AST came
2082 * then skip this lock.
2084 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
2087 if (lockmode_compat(lock->l_granted_mode, mode))
2091 * If policy is given and this is IBITS lock, add to list only
2092 * those locks that match by policy.
2094 if (policy && (lock->l_resource->lr_type == LDLM_IBITS)) {
2095 if (!(lock->l_policy_data.l_inodebits.bits &
2096 policy->l_inodebits.bits))
2098 /* Skip locks with DoM bit if it is not set in policy
2099 * to don't flush data by side-bits. Lock convert will
2100 * drop those bits separately.
2102 if (ldlm_has_dom(lock) &&
2103 !(policy->l_inodebits.bits & MDS_INODELOCK_DOM))
2107 /* See CBPENDING comment in ldlm_cancel_lru */
2108 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
2110 LASSERT(list_empty(&lock->l_bl_ast));
2111 list_add(&lock->l_bl_ast, cancels);
2112 LDLM_LOCK_GET(lock);
2117 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
2119 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2122 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
2124 * If \a req is NULL, send CANCEL request to server with handles of locks
2125 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
2126 * separately per lock.
2127 * If \a req is not NULL, put handles of locks in \a cancels into the request
2128 * buffer at the offset \a off.
2129 * Destroy \a cancels at the end.
2131 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
2132 struct ptlrpc_request *req,
2133 enum ldlm_cancel_flags flags)
2135 struct ldlm_lock *lock;
2140 if (list_empty(cancels) || count == 0)
2144 * XXX: requests (both batched and not) could be sent in parallel.
2145 * Usually it is enough to have just 1 RPC, but it is possible that
2146 * there are too many locks to be cancelled in LRU or on a resource.
2147 * It would also speed up the case when the server does not support
2151 LASSERT(!list_empty(cancels));
2152 lock = list_entry(cancels->next, struct ldlm_lock,
2154 LASSERT(lock->l_conn_export);
2156 if (exp_connect_cancelset(lock->l_conn_export)) {
2159 ldlm_cancel_pack(req, cancels, count);
2161 res = ldlm_cli_cancel_req(lock->l_conn_export,
2165 res = ldlm_cli_cancel_req(lock->l_conn_export,
2170 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
2171 "ldlm_cli_cancel_list: %d\n", res);
2176 ldlm_lock_list_put(cancels, l_bl_ast, res);
2178 LASSERT(count == 0);
2181 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2184 * Cancel all locks on a resource that have 0 readers/writers.
2186 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
2187 * to notify the server.
2189 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
2190 const struct ldlm_res_id *res_id,
2191 union ldlm_policy_data *policy,
2192 enum ldlm_mode mode,
2193 enum ldlm_cancel_flags flags, void *opaque)
2195 struct ldlm_resource *res;
2202 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2204 /* This is not a problem. */
2205 CDEBUG(D_INFO, "No resource %llu\n", res_id->name[0]);
2209 LDLM_RESOURCE_ADDREF(res);
2210 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
2211 0, flags | LCF_BL_AST, opaque);
2212 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
2214 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
2217 LDLM_RESOURCE_DELREF(res);
2218 ldlm_resource_putref(res);
2221 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2223 struct ldlm_cli_cancel_arg {
2229 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2230 struct hlist_node *hnode, void *arg)
2232 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2233 struct ldlm_cli_cancel_arg *lc = arg;
2235 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2236 NULL, LCK_MINMODE, lc->lc_flags,
2238 /* must return 0 for hash iteration */
2243 * Cancel all locks on a namespace (or a specific resource, if given)
2244 * that have 0 readers/writers.
2246 * If flags & LCF_LOCAL, throw the locks away without trying
2247 * to notify the server.
2249 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2250 const struct ldlm_res_id *res_id,
2251 enum ldlm_cancel_flags flags, void *opaque)
2253 struct ldlm_cli_cancel_arg arg = {
2255 .lc_opaque = opaque,
2263 if (res_id != NULL) {
2264 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2268 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2269 ldlm_cli_hash_cancel_unused, &arg, 0);
2274 /* Lock iterators. */
2276 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2279 struct list_head *tmp, *next;
2280 struct ldlm_lock *lock;
2281 int rc = LDLM_ITER_CONTINUE;
2286 RETURN(LDLM_ITER_CONTINUE);
2289 list_for_each_safe(tmp, next, &res->lr_granted) {
2290 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2292 if (iter(lock, closure) == LDLM_ITER_STOP)
2293 GOTO(out, rc = LDLM_ITER_STOP);
2296 list_for_each_safe(tmp, next, &res->lr_waiting) {
2297 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2299 if (iter(lock, closure) == LDLM_ITER_STOP)
2300 GOTO(out, rc = LDLM_ITER_STOP);
2307 struct iter_helper_data {
2308 ldlm_iterator_t iter;
2312 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2314 struct iter_helper_data *helper = closure;
2316 return helper->iter(lock, helper->closure);
2319 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2320 struct hlist_node *hnode, void *arg)
2323 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2325 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2329 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2330 ldlm_iterator_t iter, void *closure)
2333 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2335 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2336 ldlm_res_iter_helper, &helper, 0);
2341 * non-blocking function to manipulate a lock whose cb_data is being put away.
2342 * return 0: find no resource
2343 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2346 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2347 const struct ldlm_res_id *res_id,
2348 ldlm_iterator_t iter, void *data)
2350 struct ldlm_resource *res;
2355 LASSERTF(ns != NULL, "must pass in namespace\n");
2357 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2361 LDLM_RESOURCE_ADDREF(res);
2362 rc = ldlm_resource_foreach(res, iter, data);
2363 LDLM_RESOURCE_DELREF(res);
2364 ldlm_resource_putref(res);
2367 EXPORT_SYMBOL(ldlm_resource_iterate);
2370 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2372 struct list_head *list = closure;
2374 /* we use l_pending_chain here, because it's unused on clients. */
2375 LASSERTF(list_empty(&lock->l_pending_chain),
2376 "lock %p next %p prev %p\n",
2377 lock, &lock->l_pending_chain.next,
2378 &lock->l_pending_chain.prev);
2380 * b=9573: don't replay locks left after eviction, or
2381 * b=17614: locks being actively cancelled. Get a reference
2382 * on a lock so that it does not disapear under us (e.g. due to cancel)
2384 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_BL_DONE))) {
2385 list_add(&lock->l_pending_chain, list);
2386 LDLM_LOCK_GET(lock);
2389 return LDLM_ITER_CONTINUE;
2392 static int replay_lock_interpret(const struct lu_env *env,
2393 struct ptlrpc_request *req, void *args, int rc)
2395 struct ldlm_async_args *aa = args;
2396 struct ldlm_lock *lock;
2397 struct ldlm_reply *reply;
2398 struct obd_export *exp;
2401 atomic_dec(&req->rq_import->imp_replay_inflight);
2405 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2407 GOTO(out, rc = -EPROTO);
2409 lock = ldlm_handle2lock(&aa->lock_handle);
2411 CERROR("received replay ack for unknown local cookie %#llx remote cookie %#llx from server %s id %s\n",
2412 aa->lock_handle.cookie, reply->lock_handle.cookie,
2413 req->rq_export->exp_client_uuid.uuid,
2414 libcfs_id2str(req->rq_peer));
2415 GOTO(out, rc = -ESTALE);
2418 /* Key change rehash lock in per-export hash with new key */
2419 exp = req->rq_export;
2420 if (exp && exp->exp_lock_hash) {
2422 * In the function below, .hs_keycmp resolves to
2423 * ldlm_export_lock_keycmp()
2425 /* coverity[overrun-buffer-val] */
2426 cfs_hash_rehash_key(exp->exp_lock_hash,
2427 &lock->l_remote_handle,
2428 &reply->lock_handle,
2431 lock->l_remote_handle = reply->lock_handle;
2434 LDLM_DEBUG(lock, "replayed lock:");
2435 ptlrpc_import_recovery_state_machine(req->rq_import);
2436 LDLM_LOCK_PUT(lock);
2439 ptlrpc_connect_import(req->rq_import);
2444 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2446 struct ptlrpc_request *req;
2447 struct ldlm_async_args *aa;
2448 struct ldlm_request *body;
2454 /* b=11974: Do not replay a lock which is actively being canceled */
2455 if (ldlm_is_bl_done(lock)) {
2456 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2461 * If this is reply-less callback lock, we cannot replay it, since
2462 * server might have long dropped it, but notification of that event was
2463 * lost by network. (and server granted conflicting lock already)
2465 if (ldlm_is_cancel_on_block(lock)) {
2466 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2467 ldlm_lock_cancel(lock);
2472 * If granted mode matches the requested mode, this lock is granted.
2474 * If we haven't been granted anything and are on a resource list,
2475 * then we're blocked/waiting.
2477 * If we haven't been granted anything and we're NOT on a resource list,
2478 * then we haven't got a reply yet and don't have a known disposition.
2479 * This happens whenever a lock enqueue is the request that triggers
2482 if (ldlm_is_granted(lock))
2483 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2484 else if (!list_empty(&lock->l_res_link))
2485 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2487 flags = LDLM_FL_REPLAY;
2489 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2490 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2494 /* We're part of recovery, so don't wait for it. */
2495 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2497 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2498 ldlm_lock2desc(lock, &body->lock_desc);
2499 body->lock_flags = ldlm_flags_to_wire(flags);
2501 ldlm_lock2handle(lock, &body->lock_handle[0]);
2502 if (lock->l_lvb_len > 0)
2503 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2504 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2506 ptlrpc_request_set_replen(req);
2508 * notify the server we've replayed all requests.
2509 * also, we mark the request to be put on a dedicated
2510 * queue to be processed after all request replayes.
2513 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2515 LDLM_DEBUG(lock, "replaying lock:");
2517 atomic_inc(&req->rq_import->imp_replay_inflight);
2518 aa = ptlrpc_req_async_args(aa, req);
2519 aa->lock_handle = body->lock_handle[0];
2520 req->rq_interpret_reply = replay_lock_interpret;
2521 ptlrpcd_add_req(req);
2527 * Cancel as many unused locks as possible before replay. since we are
2528 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2531 * Called only in recovery before replaying locks. there is no need to
2532 * replay locks that are unused. since the clients may hold thousands of
2533 * cached unused locks, dropping the unused locks can greatly reduce the
2534 * load on the servers at recovery time.
2536 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2542 "Dropping as many unused locks as possible before replay for namespace %s (%d)\n",
2543 ldlm_ns_name(ns), ns->ns_nr_unused);
2546 * We don't need to care whether or not LRU resize is enabled
2547 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2550 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2551 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2553 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2554 canceled, ldlm_ns_name(ns));
2557 int ldlm_replay_locks(struct obd_import *imp)
2559 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2561 struct ldlm_lock *lock, *next;
2566 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2568 /* don't replay locks if import failed recovery */
2569 if (imp->imp_vbr_failed)
2572 /* ensure this doesn't fall to 0 before all have been queued */
2573 atomic_inc(&imp->imp_replay_inflight);
2575 if (ldlm_cancel_unused_locks_before_replay)
2576 ldlm_cancel_unused_locks_for_replay(ns);
2578 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2580 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2581 list_del_init(&lock->l_pending_chain);
2583 LDLM_LOCK_RELEASE(lock);
2584 continue; /* or try to do the rest? */
2586 rc = replay_one_lock(imp, lock);
2587 LDLM_LOCK_RELEASE(lock);
2590 atomic_dec(&imp->imp_replay_inflight);