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, 2016, 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_dlm.h>
61 #include <obd_class.h>
64 #include "ldlm_internal.h"
66 unsigned int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
67 module_param(ldlm_enqueue_min, uint, 0644);
68 MODULE_PARM_DESC(ldlm_enqueue_min, "lock enqueue timeout minimum");
70 /* in client side, whether the cached locks will be canceled before replay */
71 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
73 static void interrupted_completion_wait(void *data)
77 struct lock_wait_data {
78 struct ldlm_lock *lwd_lock;
82 struct ldlm_async_args {
83 struct lustre_handle lock_handle;
87 * ldlm_request_bufsize
89 * If opcode=LDLM_ENQUEUE, 1 slot is already occupied,
90 * LDLM_LOCKREQ_HANDLE -1 slots are available.
91 * Otherwise, LDLM_LOCKREQ_HANDLE slots are available.
96 * \retval size of the request buffer
99 int ldlm_request_bufsize(int count, int type)
101 int avail = LDLM_LOCKREQ_HANDLES;
102 if (type == LDLM_ENQUEUE)
103 avail -= LDLM_ENQUEUE_CANCEL_OFF;
106 avail = (count - avail) * sizeof(struct lustre_handle);
110 return sizeof(struct ldlm_request) + avail;
113 int ldlm_expired_completion_wait(void *data)
115 struct lock_wait_data *lwd = data;
116 struct ldlm_lock *lock = lwd->lwd_lock;
117 struct obd_import *imp;
118 struct obd_device *obd;
121 if (lock->l_conn_export == NULL) {
122 static cfs_time_t next_dump = 0, last_dump = 0;
124 LDLM_ERROR(lock, "lock timed out (enqueued at %lld, %llds ago); "
125 "not entering recovery in server code, just going back to sleep",
126 (s64)lock->l_last_activity,
127 (s64)(ktime_get_real_seconds() -
128 lock->l_last_activity));
129 if (cfs_time_after(cfs_time_current(), next_dump)) {
130 last_dump = next_dump;
131 next_dump = cfs_time_shift(300);
132 ldlm_namespace_dump(D_DLMTRACE,
133 ldlm_lock_to_ns(lock));
135 libcfs_debug_dumplog();
140 obd = lock->l_conn_export->exp_obd;
141 imp = obd->u.cli.cl_import;
142 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
143 LDLM_ERROR(lock, "lock timed out (enqueued at %lld, %llds ago), entering recovery for %s@%s",
144 (s64)lock->l_last_activity,
145 (s64)(ktime_get_real_seconds() - lock->l_last_activity),
146 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
152 * Calculate the Completion timeout (covering enqueue, BL AST, data flush,
153 * lock cancel, and their replies). Used for lock completion timeout on the
156 * \param[in] lock lock which is waiting the completion callback
158 * \retval timeout in seconds to wait for the server reply
161 /* We use the same basis for both server side and client side functions
162 from a single node. */
163 static unsigned int ldlm_cp_timeout(struct ldlm_lock *lock)
165 unsigned int timeout;
170 /* Wait a long time for enqueue - server may have to callback a
171 * lock from another client. Server will evict the other client if it
172 * doesn't respond reasonably, and then give us the lock. */
173 timeout = at_get(ldlm_lock_to_ns_at(lock));
174 return max(3 * timeout, ldlm_enqueue_min);
178 * Helper function for ldlm_completion_ast(), updating timings when lock is
181 static int ldlm_completion_tail(struct ldlm_lock *lock, void *data)
186 if (ldlm_is_destroyed(lock) || ldlm_is_failed(lock)) {
187 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
189 } else if (data == NULL) {
190 LDLM_DEBUG(lock, "client-side enqueue: granted");
192 /* Take into AT only CP RPC, not immediately granted locks */
193 delay = ktime_get_real_seconds() - lock->l_last_activity;
194 LDLM_DEBUG(lock, "client-side enqueue: granted after %llds",
197 /* Update our time estimate */
198 at_measured(ldlm_lock_to_ns_at(lock), delay);
204 * Implementation of ->l_completion_ast() for a client, that doesn't wait
205 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
206 * other threads that cannot block for long.
208 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data)
212 if (flags == LDLM_FL_WAIT_NOREPROC) {
213 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
217 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
218 wake_up(&lock->l_waitq);
219 RETURN(ldlm_completion_tail(lock, data));
222 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
224 ldlm_reprocess_all(lock->l_resource);
227 EXPORT_SYMBOL(ldlm_completion_ast_async);
230 * Generic LDLM "completion" AST. This is called in several cases:
232 * - when a reply to an ENQUEUE RPC is received from the server
233 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
234 * this point (determined by flags);
236 * - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
239 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
242 * - to force all locks when resource is destroyed (cleanup_resource());
244 * - during lock conversion (not used currently).
246 * If lock is not granted in the first case, this function waits until second
247 * or penultimate cases happen in some other thread.
250 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
252 /* XXX ALLOCATE - 160 bytes */
253 struct lock_wait_data lwd;
254 struct obd_device *obd;
255 struct obd_import *imp = NULL;
256 struct l_wait_info lwi;
261 if (flags == LDLM_FL_WAIT_NOREPROC) {
262 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
266 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
267 wake_up(&lock->l_waitq);
271 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
276 obd = class_exp2obd(lock->l_conn_export);
278 /* if this is a local lock, then there is no import */
280 imp = obd->u.cli.cl_import;
283 timeout = ldlm_cp_timeout(lock);
286 lock->l_last_activity = cfs_time_current_sec();
288 if (ldlm_is_no_timeout(lock)) {
289 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
290 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
292 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
293 ldlm_expired_completion_wait,
294 interrupted_completion_wait, &lwd);
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 rc = l_wait_event(lock->l_waitq,
311 is_granted_or_cancelled(lock), &lwi);
315 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
320 RETURN(ldlm_completion_tail(lock, data));
322 EXPORT_SYMBOL(ldlm_completion_ast);
325 * A helper to build a blocking AST function
327 * Perform a common operation for blocking ASTs:
328 * defferred lock cancellation.
330 * \param lock the lock blocking or canceling AST was called on
332 * \see mdt_blocking_ast
333 * \see ldlm_blocking_ast
335 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
340 ldlm_set_cbpending(lock);
341 do_ast = (!lock->l_readers && !lock->l_writers);
342 unlock_res_and_lock(lock);
345 struct lustre_handle lockh;
348 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
349 ldlm_lock2handle(lock, &lockh);
350 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
352 CERROR("ldlm_cli_cancel: %d\n", rc);
354 LDLM_DEBUG(lock, "Lock still has references, will be "
359 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
362 * Server blocking AST
364 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
367 * \param lock the lock which blocks a request or cancelling lock
370 * \param flag indicates whether this cancelling or blocking callback
372 * \see ldlm_blocking_ast_nocheck
374 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
375 void *data, int flag)
379 if (flag == LDLM_CB_CANCELING) {
380 /* Don't need to do anything here. */
384 lock_res_and_lock(lock);
385 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
386 * that ldlm_blocking_ast is called just before intent_policy method
387 * takes the lr_lock, then by the time we get the lock, we might not
388 * be the correct blocking function anymore. So check, and return
390 if (lock->l_blocking_ast != ldlm_blocking_ast) {
391 unlock_res_and_lock(lock);
394 RETURN(ldlm_blocking_ast_nocheck(lock));
396 EXPORT_SYMBOL(ldlm_blocking_ast);
399 * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
401 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
402 * rather subtle: with OST-side locking, it may so happen that _all_ extent
403 * locks are held by the OST. If client wants to obtain the current file size
404 * it calls ll_glimpse_size(), and (as all locks are held only on the server),
405 * this dummy glimpse callback fires and does nothing. The client still
406 * receives the correct file size due to the following fragment of code in
407 * ldlm_cb_interpret():
409 * if (rc == -ELDLM_NO_LOCK_DATA) {
410 * LDLM_DEBUG(lock, "lost race - client has a lock but no"
412 * ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
415 * That is, after the glimpse returns this error, ofd_lvbo_update() is called
416 * and returns the updated file attributes from the inode to the client.
418 * See also comment in ofd_intent_policy() on why servers must set a non-NULL
419 * l_glimpse_ast when grabbing DLM locks. Otherwise, the server will assume
420 * that the object is in the process of being destroyed.
422 * \param[in] lock DLM lock being glimpsed, unused
423 * \param[in] reqp pointer to ptlrpc_request, unused
425 * \retval -ELDLM_NO_LOCK_DATA to get attributes from disk object
427 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
429 return -ELDLM_NO_LOCK_DATA;
433 * Enqueue a local lock (typically on a server).
435 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
436 const struct ldlm_res_id *res_id,
437 enum ldlm_type type, union ldlm_policy_data *policy,
438 enum ldlm_mode mode, __u64 *flags,
439 ldlm_blocking_callback blocking,
440 ldlm_completion_callback completion,
441 ldlm_glimpse_callback glimpse,
442 void *data, __u32 lvb_len, enum lvb_type lvb_type,
443 const __u64 *client_cookie,
444 struct lustre_handle *lockh)
446 struct ldlm_lock *lock;
448 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
449 .lcs_blocking = blocking,
450 .lcs_glimpse = glimpse,
454 LASSERT(!(*flags & LDLM_FL_REPLAY));
455 if (unlikely(ns_is_client(ns))) {
456 CERROR("Trying to enqueue local lock in a shadow namespace\n");
460 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
463 GOTO(out_nolock, err = PTR_ERR(lock));
465 err = ldlm_lvbo_init(lock->l_resource);
467 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
471 ldlm_lock2handle(lock, lockh);
473 /* NB: we don't have any lock now (lock_res_and_lock)
474 * because it's a new lock */
475 ldlm_lock_addref_internal_nolock(lock, mode);
476 ldlm_set_local(lock);
477 if (*flags & LDLM_FL_ATOMIC_CB)
478 ldlm_set_atomic_cb(lock);
481 lock->l_policy_data = *policy;
482 if (client_cookie != NULL)
483 lock->l_client_cookie = *client_cookie;
484 if (type == LDLM_EXTENT) {
485 /* extent lock without policy is a bug */
489 lock->l_req_extent = policy->l_extent;
492 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
493 if (unlikely(err != ELDLM_OK))
497 *policy = lock->l_policy_data;
499 if (lock->l_completion_ast)
500 lock->l_completion_ast(lock, *flags, NULL);
502 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
505 LDLM_LOCK_RELEASE(lock);
509 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
511 static void failed_lock_cleanup(struct ldlm_namespace *ns,
512 struct ldlm_lock *lock, int mode)
516 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
517 lock_res_and_lock(lock);
518 /* Check that lock is not granted or failed, we might race. */
519 if ((lock->l_req_mode != lock->l_granted_mode) &&
520 !ldlm_is_failed(lock)) {
521 /* Make sure that this lock will not be found by raced
522 * bl_ast and -EINVAL reply is sent to server anyways.
524 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
525 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
528 unlock_res_and_lock(lock);
532 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
533 "LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
535 LDLM_DEBUG(lock, "lock was granted or failed in race");
537 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
538 * from llite/file.c/ll_file_flock(). */
539 /* This code makes for the fact that we do not have blocking handler on
540 * a client for flock locks. As such this is the place where we must
541 * completely kill failed locks. (interrupted and those that
542 * were waiting to be granted when server evicted us. */
543 if (lock->l_resource->lr_type == LDLM_FLOCK) {
544 lock_res_and_lock(lock);
545 if (!ldlm_is_destroyed(lock)) {
546 ldlm_resource_unlink_lock(lock);
547 ldlm_lock_decref_internal_nolock(lock, mode);
548 ldlm_lock_destroy_nolock(lock);
550 unlock_res_and_lock(lock);
552 ldlm_lock_decref_internal(lock, mode);
557 * Finishing portion of client lock enqueue code.
559 * Called after receiving reply from server.
561 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
562 enum ldlm_type type, __u8 with_policy,
563 enum ldlm_mode mode, __u64 *flags, void *lvb,
564 __u32 lvb_len, const struct lustre_handle *lockh,
567 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
568 int is_replay = *flags & LDLM_FL_REPLAY;
569 struct ldlm_lock *lock;
570 struct ldlm_reply *reply;
571 int cleanup_phase = 1;
574 lock = ldlm_handle2lock(lockh);
575 /* ldlm_cli_enqueue is holding a reference on this lock. */
577 LASSERT(type == LDLM_FLOCK);
581 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
582 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
584 if (rc != ELDLM_OK) {
586 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
587 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
589 if (rc != ELDLM_LOCK_ABORTED)
593 /* Before we return, swab the reply */
594 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
596 GOTO(cleanup, rc = -EPROTO);
601 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
604 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
605 GOTO(cleanup, rc = size);
606 } else if (unlikely(size > lvb_len)) {
607 LDLM_ERROR(lock, "Replied LVB is larger than "
608 "expectation, expected = %d, replied = %d",
610 GOTO(cleanup, rc = -EINVAL);
615 if (rc == ELDLM_LOCK_ABORTED) {
616 if (lvb_len > 0 && lvb != NULL)
617 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
619 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
622 /* lock enqueued on the server */
625 lock_res_and_lock(lock);
626 /* Key change rehash lock in per-export hash with new key */
627 if (exp->exp_lock_hash) {
628 /* In the function below, .hs_keycmp resolves to
629 * ldlm_export_lock_keycmp() */
630 /* coverity[overrun-buffer-val] */
631 cfs_hash_rehash_key(exp->exp_lock_hash,
632 &lock->l_remote_handle,
636 lock->l_remote_handle = reply->lock_handle;
639 *flags = ldlm_flags_from_wire(reply->lock_flags);
640 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
641 LDLM_FL_INHERIT_MASK);
642 unlock_res_and_lock(lock);
644 CDEBUG(D_INFO, "local: %p, remote cookie: %#llx, flags: %#llx\n",
645 lock, reply->lock_handle.cookie, *flags);
647 /* If enqueue returned a blocked lock but the completion handler has
648 * already run, then it fixed up the resource and we don't need to do it
650 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
651 int newmode = reply->lock_desc.l_req_mode;
653 if (newmode && newmode != lock->l_req_mode) {
654 LDLM_DEBUG(lock, "server returned different mode %s",
655 ldlm_lockname[newmode]);
656 lock->l_req_mode = newmode;
659 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
660 &lock->l_resource->lr_name)) {
661 CDEBUG(D_INFO, "remote intent success, locking "DLDLMRES
662 " instead of "DLDLMRES"\n",
663 PLDLMRES(&reply->lock_desc.l_resource),
664 PLDLMRES(lock->l_resource));
666 rc = ldlm_lock_change_resource(ns, lock,
667 &reply->lock_desc.l_resource.lr_name);
668 if (rc || lock->l_resource == NULL)
669 GOTO(cleanup, rc = -ENOMEM);
670 LDLM_DEBUG(lock, "client-side enqueue, new resource");
673 if (!(type == LDLM_IBITS &&
674 !(exp_connect_flags(exp) & OBD_CONNECT_IBITS)))
675 /* We assume lock type cannot change on server*/
676 ldlm_convert_policy_to_local(exp,
677 lock->l_resource->lr_type,
678 &reply->lock_desc.l_policy_data,
679 &lock->l_policy_data);
680 if (type != LDLM_PLAIN)
681 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
684 if ((*flags) & LDLM_FL_AST_SENT) {
685 lock_res_and_lock(lock);
686 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
687 unlock_res_and_lock(lock);
688 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
691 /* If the lock has already been granted by a completion AST, don't
692 * clobber the LVB with an older one. */
694 /* We must lock or a racing completion might update lvb without
695 * letting us know and we'll clobber the correct value.
696 * Cannot unlock after the check either, a that still leaves
697 * a tiny window for completion to get in */
698 lock_res_and_lock(lock);
699 if (lock->l_req_mode != lock->l_granted_mode)
700 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
701 lock->l_lvb_data, lvb_len);
702 unlock_res_and_lock(lock);
710 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
711 if (lock->l_completion_ast != NULL) {
712 int err = lock->l_completion_ast(lock, *flags, NULL);
720 if (lvb_len > 0 && lvb != NULL) {
721 /* Copy the LVB here, and not earlier, because the completion
722 * AST (if any) can override what we got in the reply */
723 memcpy(lvb, lock->l_lvb_data, lvb_len);
726 LDLM_DEBUG(lock, "client-side enqueue END");
729 if (cleanup_phase == 1 && rc)
730 failed_lock_cleanup(ns, lock, mode);
731 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
733 LDLM_LOCK_RELEASE(lock);
736 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
739 * Estimate number of lock handles that would fit into request of given
740 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
741 * a single page on the send/receive side. XXX: 512 should be changed to
742 * more adequate value.
744 static inline int ldlm_req_handles_avail(int req_size, int off)
748 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
749 if (likely(avail >= 0))
750 avail /= (int)sizeof(struct lustre_handle);
753 avail += LDLM_LOCKREQ_HANDLES - off;
758 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
759 enum req_location loc,
762 __u32 size = req_capsule_msg_size(pill, loc);
763 return ldlm_req_handles_avail(size, off);
766 static inline int ldlm_format_handles_avail(struct obd_import *imp,
767 const struct req_format *fmt,
768 enum req_location loc, int off)
770 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
771 return ldlm_req_handles_avail(size, off);
775 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
776 * \a count locks in \a cancels.
778 * This is to be called by functions preparing their own requests that
779 * might contain lists of locks to cancel in addition to actual operation
780 * that needs to be performed.
782 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
783 int version, int opc, int canceloff,
784 struct list_head *cancels, int count)
786 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
787 struct req_capsule *pill = &req->rq_pill;
788 struct ldlm_request *dlm = NULL;
789 struct list_head head = LIST_HEAD_INIT(head);
790 enum ldlm_lru_flags lru_flags;
791 int avail, to_free, pack = 0;
797 if (ns_connect_cancelset(ns)) {
798 /* Estimate the amount of available space in the request. */
799 req_capsule_filled_sizes(pill, RCL_CLIENT);
800 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
802 lru_flags = ns_connect_lru_resize(ns) ?
803 LDLM_LRU_FLAG_LRUR_NO_WAIT : LDLM_LRU_FLAG_AGED;
804 to_free = !ns_connect_lru_resize(ns) &&
805 opc == LDLM_ENQUEUE ? 1 : 0;
807 /* Cancel LRU locks here _only_ if the server supports
808 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
809 * RPC, which will make us slower. */
811 count += ldlm_cancel_lru_local(ns, cancels, to_free,
818 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
819 ldlm_request_bufsize(pack, opc));
822 rc = ptlrpc_request_pack(req, version, opc);
824 ldlm_lock_list_put(cancels, l_bl_ast, count);
828 if (ns_connect_cancelset(ns)) {
830 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
832 /* Skip first lock handler in ldlm_request_pack(),
833 * this method will increment @lock_count according
834 * to the lock handle amount actually written to
836 dlm->lock_count = canceloff;
838 /* Pack into the request @pack lock handles. */
839 ldlm_cli_cancel_list(cancels, pack, req, 0);
840 /* Prepare and send separate cancel RPC for others. */
841 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
843 ldlm_lock_list_put(cancels, l_bl_ast, count);
847 EXPORT_SYMBOL(ldlm_prep_elc_req);
849 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
850 struct list_head *cancels, int count)
852 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
853 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
855 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
857 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
859 struct ptlrpc_request *req;
863 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
865 RETURN(ERR_PTR(-ENOMEM));
867 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
869 ptlrpc_request_free(req);
873 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
874 ptlrpc_request_set_replen(req);
877 EXPORT_SYMBOL(ldlm_enqueue_pack);
880 * Client-side lock enqueue.
882 * If a request has some specific initialisation it is passed in \a reqp,
883 * otherwise it is created in ldlm_cli_enqueue.
885 * Supports sync and async requests, pass \a async flag accordingly. If a
886 * request was created in ldlm_cli_enqueue and it is the async request,
887 * pass it to the caller in \a reqp.
889 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
890 struct ldlm_enqueue_info *einfo,
891 const struct ldlm_res_id *res_id,
892 union ldlm_policy_data const *policy, __u64 *flags,
893 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
894 struct lustre_handle *lockh, int async)
896 struct ldlm_namespace *ns;
897 struct ldlm_lock *lock;
898 struct ldlm_request *body;
899 int is_replay = *flags & LDLM_FL_REPLAY;
900 int req_passed_in = 1;
902 struct ptlrpc_request *req;
905 LASSERT(exp != NULL);
907 ns = exp->exp_obd->obd_namespace;
909 /* If we're replaying this lock, just check some invariants.
910 * If we're creating a new lock, get everything all setup nice. */
912 lock = ldlm_handle2lock_long(lockh, 0);
913 LASSERT(lock != NULL);
914 LDLM_DEBUG(lock, "client-side enqueue START");
915 LASSERT(exp == lock->l_conn_export);
917 const struct ldlm_callback_suite cbs = {
918 .lcs_completion = einfo->ei_cb_cp,
919 .lcs_blocking = einfo->ei_cb_bl,
920 .lcs_glimpse = einfo->ei_cb_gl
922 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
923 einfo->ei_mode, &cbs, einfo->ei_cbdata,
926 RETURN(PTR_ERR(lock));
927 /* for the local lock, add the reference */
928 ldlm_lock_addref_internal(lock, einfo->ei_mode);
929 ldlm_lock2handle(lock, lockh);
931 lock->l_policy_data = *policy;
933 if (einfo->ei_type == LDLM_EXTENT) {
934 /* extent lock without policy is a bug */
938 lock->l_req_extent = policy->l_extent;
940 LDLM_DEBUG(lock, "client-side enqueue START, flags %#llx",
944 lock->l_conn_export = exp;
945 lock->l_export = NULL;
946 lock->l_blocking_ast = einfo->ei_cb_bl;
947 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL));
948 lock->l_last_activity = cfs_time_current_sec();
950 /* lock not sent to server yet */
951 if (reqp == NULL || *reqp == NULL) {
952 req = ldlm_enqueue_pack(exp, lvb_len);
954 failed_lock_cleanup(ns, lock, einfo->ei_mode);
955 LDLM_LOCK_RELEASE(lock);
956 RETURN(PTR_ERR(req));
966 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
968 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
969 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
972 /* Dump lock data into the request buffer */
973 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
974 ldlm_lock2desc(lock, &body->lock_desc);
975 body->lock_flags = ldlm_flags_to_wire(*flags);
976 body->lock_handle[0] = *lockh;
979 LASSERT(reqp != NULL);
983 LDLM_DEBUG(lock, "sending request");
985 rc = ptlrpc_queue_wait(req);
987 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
988 einfo->ei_mode, flags, lvb, lvb_len,
991 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
992 * one reference that we took */
994 LDLM_LOCK_RELEASE(lock);
998 if (!req_passed_in && req != NULL) {
999 ptlrpc_req_finished(req);
1006 EXPORT_SYMBOL(ldlm_cli_enqueue);
1008 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
1011 struct ldlm_resource *res;
1014 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1015 CERROR("Trying to cancel local lock\n");
1018 LDLM_DEBUG(lock, "client-side local convert");
1020 res = ldlm_lock_convert(lock, new_mode, flags);
1022 ldlm_reprocess_all(res);
1025 rc = LUSTRE_EDEADLK;
1027 LDLM_DEBUG(lock, "client-side local convert handler END");
1028 LDLM_LOCK_PUT(lock);
1032 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
1033 * conversion of locks which are on the waiting or converting queue */
1034 /* Caller of this code is supposed to take care of lock readers/writers
1036 int ldlm_cli_convert(const struct lustre_handle *lockh, int new_mode,
1039 struct ldlm_request *body;
1040 struct ldlm_reply *reply;
1041 struct ldlm_lock *lock;
1042 struct ldlm_resource *res;
1043 struct ptlrpc_request *req;
1047 lock = ldlm_handle2lock(lockh);
1054 if (lock->l_conn_export == NULL)
1055 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
1057 LDLM_DEBUG(lock, "client-side convert");
1059 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
1060 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1063 LDLM_LOCK_PUT(lock);
1067 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1068 body->lock_handle[0] = lock->l_remote_handle;
1070 body->lock_desc.l_req_mode = new_mode;
1071 body->lock_flags = ldlm_flags_to_wire(*flags);
1074 ptlrpc_request_set_replen(req);
1075 rc = ptlrpc_queue_wait(req);
1079 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1081 GOTO(out, rc = -EPROTO);
1084 GOTO(out, rc = req->rq_status);
1086 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
1088 ldlm_reprocess_all(res);
1089 /* Go to sleep until the lock is granted. */
1090 /* FIXME: or cancelled. */
1091 if (lock->l_completion_ast) {
1092 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1098 rc = LUSTRE_EDEADLK;
1102 LDLM_LOCK_PUT(lock);
1103 ptlrpc_req_finished(req);
1108 * Cancel locks locally.
1110 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1111 * \retval LDLM_FL_CANCELING otherwise;
1112 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1114 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1116 __u64 rc = LDLM_FL_LOCAL_ONLY;
1119 if (lock->l_conn_export) {
1122 LDLM_DEBUG(lock, "client-side cancel");
1123 /* Set this flag to prevent others from getting new references*/
1124 lock_res_and_lock(lock);
1125 ldlm_set_cbpending(lock);
1126 local_only = !!(lock->l_flags &
1127 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1128 ldlm_cancel_callback(lock);
1129 rc = (ldlm_is_bl_ast(lock)) ?
1130 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1131 unlock_res_and_lock(lock);
1134 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1136 rc = LDLM_FL_LOCAL_ONLY;
1138 ldlm_lock_cancel(lock);
1140 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1141 LDLM_ERROR(lock, "Trying to cancel local lock");
1144 LDLM_DEBUG(lock, "server-side local cancel");
1145 ldlm_lock_cancel(lock);
1146 ldlm_reprocess_all(lock->l_resource);
1153 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1155 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1156 struct list_head *head, int count)
1158 struct ldlm_request *dlm;
1159 struct ldlm_lock *lock;
1160 int max, packed = 0;
1163 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1164 LASSERT(dlm != NULL);
1166 /* Check the room in the request buffer. */
1167 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1168 sizeof(struct ldlm_request);
1169 max /= sizeof(struct lustre_handle);
1170 max += LDLM_LOCKREQ_HANDLES;
1171 LASSERT(max >= dlm->lock_count + count);
1173 /* XXX: it would be better to pack lock handles grouped by resource.
1174 * so that the server cancel would call filter_lvbo_update() less
1176 list_for_each_entry(lock, head, l_bl_ast) {
1179 LASSERT(lock->l_conn_export);
1180 /* Pack the lock handle to the given request buffer. */
1181 LDLM_DEBUG(lock, "packing");
1182 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1185 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1190 * Prepare and send a batched cancel RPC. It will include \a count lock
1191 * handles of locks given in \a cancels list. */
1192 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1193 int count, enum ldlm_cancel_flags flags)
1195 struct ptlrpc_request *req = NULL;
1196 struct obd_import *imp;
1201 LASSERT(exp != NULL);
1204 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1206 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1209 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1210 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1215 imp = class_exp2cliimp(exp);
1216 if (imp == NULL || imp->imp_invalid) {
1218 "skipping cancel on invalid import %p\n", imp);
1222 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1224 GOTO(out, rc = -ENOMEM);
1226 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1227 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1228 ldlm_request_bufsize(count, LDLM_CANCEL));
1230 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1232 ptlrpc_request_free(req);
1236 /* If OSP want cancel cross-MDT lock, let's not block it in
1237 * in recovery, otherwise the lock will not released, if
1238 * the remote target is also in recovery, and it also need
1239 * this lock, it might cause deadlock. */
1240 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1241 exp->exp_obd->obd_lu_dev != NULL &&
1242 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1243 struct lu_device *top_dev;
1245 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1246 if (top_dev != NULL &&
1247 top_dev->ld_obd->obd_recovering)
1248 req->rq_allow_replay = 1;
1251 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1252 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1253 ptlrpc_at_set_req_timeout(req);
1255 ldlm_cancel_pack(req, cancels, count);
1257 ptlrpc_request_set_replen(req);
1258 if (flags & LCF_ASYNC) {
1259 ptlrpcd_add_req(req);
1264 rc = ptlrpc_queue_wait(req);
1265 if (rc == LUSTRE_ESTALE) {
1266 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1267 "out of sync -- not fatal\n",
1268 libcfs_nid2str(req->rq_import->
1269 imp_connection->c_peer.nid));
1271 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1272 req->rq_import_generation == imp->imp_generation) {
1273 ptlrpc_req_finished(req);
1275 } else if (rc != ELDLM_OK) {
1276 /* -ESHUTDOWN is common on umount */
1277 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1278 "Got rc %d from cancel RPC: "
1279 "canceling anyway\n", rc);
1286 ptlrpc_req_finished(req);
1289 return sent ? sent : rc;
1292 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1294 LASSERT(imp != NULL);
1295 return &imp->imp_obd->obd_namespace->ns_pool;
1299 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1301 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1303 struct obd_device *obd;
1307 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1308 !imp_connect_lru_resize(req->rq_import)))
1311 * Do nothing for corner cases.
1316 /* In some cases RPC may contain SLV and limit zeroed out. This
1317 * is the case when server does not support LRU resize feature.
1318 * This is also possible in some recovery cases when server-side
1319 * reqs have no reference to the OBD export and thus access to
1320 * server-side namespace is not possible. */
1321 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1322 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1323 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1324 "(SLV: %llu, Limit: %u)",
1325 lustre_msg_get_slv(req->rq_repmsg),
1326 lustre_msg_get_limit(req->rq_repmsg));
1330 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1331 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1332 obd = req->rq_import->imp_obd;
1334 /* Set new SLV and limit in OBD fields to make them accessible
1335 * to the pool thread. We do not access obd_namespace and pool
1336 * directly here as there is no reliable way to make sure that
1337 * they are still alive at cleanup time. Evil races are possible
1338 * which may cause Oops at that time. */
1339 write_lock(&obd->obd_pool_lock);
1340 obd->obd_pool_slv = new_slv;
1341 obd->obd_pool_limit = new_limit;
1342 write_unlock(&obd->obd_pool_lock);
1348 * Client side lock cancel.
1350 * Lock must not have any readers or writers by this time.
1352 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1353 enum ldlm_cancel_flags cancel_flags)
1355 struct obd_export *exp;
1356 enum ldlm_lru_flags lru_flags;
1357 int avail, count = 1;
1359 struct ldlm_namespace *ns;
1360 struct ldlm_lock *lock;
1361 struct list_head cancels = LIST_HEAD_INIT(cancels);
1364 lock = ldlm_handle2lock_long(lockh, 0);
1366 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1370 lock_res_and_lock(lock);
1371 /* Lock is being canceled and the caller doesn't want to wait */
1372 if (ldlm_is_canceling(lock) && (cancel_flags & LCF_ASYNC)) {
1373 unlock_res_and_lock(lock);
1374 LDLM_LOCK_RELEASE(lock);
1378 ldlm_set_canceling(lock);
1379 unlock_res_and_lock(lock);
1381 rc = ldlm_cli_cancel_local(lock);
1382 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1383 LDLM_LOCK_RELEASE(lock);
1386 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1387 * RPC which goes to canceld portal, so we can cancel other LRU locks
1388 * here and send them all as one LDLM_CANCEL RPC. */
1389 LASSERT(list_empty(&lock->l_bl_ast));
1390 list_add(&lock->l_bl_ast, &cancels);
1392 exp = lock->l_conn_export;
1393 if (exp_connect_cancelset(exp)) {
1394 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1399 ns = ldlm_lock_to_ns(lock);
1400 lru_flags = ns_connect_lru_resize(ns) ?
1401 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1402 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1403 LCF_BL_AST, lru_flags);
1405 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1408 EXPORT_SYMBOL(ldlm_cli_cancel);
1411 * Locally cancel up to \a count locks in list \a cancels.
1412 * Return the number of cancelled locks.
1414 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1415 enum ldlm_cancel_flags cancel_flags)
1417 struct list_head head = LIST_HEAD_INIT(head);
1418 struct ldlm_lock *lock, *next;
1419 int left = 0, bl_ast = 0;
1423 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1427 if (cancel_flags & LCF_LOCAL) {
1428 rc = LDLM_FL_LOCAL_ONLY;
1429 ldlm_lock_cancel(lock);
1431 rc = ldlm_cli_cancel_local(lock);
1433 /* Until we have compound requests and can send LDLM_CANCEL
1434 * requests batched with generic RPCs, we need to send cancels
1435 * with the LDLM_FL_BL_AST flag in a separate RPC from
1436 * the one being generated now. */
1437 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1438 LDLM_DEBUG(lock, "Cancel lock separately");
1439 list_del_init(&lock->l_bl_ast);
1440 list_add(&lock->l_bl_ast, &head);
1444 if (rc == LDLM_FL_LOCAL_ONLY) {
1445 /* CANCEL RPC should not be sent to server. */
1446 list_del_init(&lock->l_bl_ast);
1447 LDLM_LOCK_RELEASE(lock);
1453 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1460 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1461 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1462 * readahead requests, ...)
1464 static enum ldlm_policy_res
1465 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1466 int unused, int added, int count)
1468 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1470 /* don't check added & count since we want to process all locks
1472 * It's fine to not take lock to access lock->l_resource since
1473 * the lock has already been granted so it won't change. */
1474 switch (lock->l_resource->lr_type) {
1477 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1480 result = LDLM_POLICY_SKIP_LOCK;
1481 lock_res_and_lock(lock);
1482 ldlm_set_skipped(lock);
1483 unlock_res_and_lock(lock);
1491 * Callback function for LRU-resize policy. Decides whether to keep
1492 * \a lock in LRU for current \a LRU size \a unused, added in current
1493 * scan \a added and number of locks to be preferably canceled \a count.
1495 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1497 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1499 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1500 struct ldlm_lock *lock,
1501 int unused, int added,
1504 cfs_time_t cur = cfs_time_current();
1505 struct ldlm_pool *pl = &ns->ns_pool;
1509 /* Stop LRU processing when we reach past @count or have checked all
1511 if (count && added >= count)
1512 return LDLM_POLICY_KEEP_LOCK;
1514 /* Despite of the LV, It doesn't make sense to keep the lock which
1515 * is unused for ns_max_age time. */
1516 if (cfs_time_after(cfs_time_current(),
1517 cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1518 return LDLM_POLICY_CANCEL_LOCK;
1520 slv = ldlm_pool_get_slv(pl);
1521 lvf = ldlm_pool_get_lvf(pl);
1522 la = cfs_duration_sec(cfs_time_sub(cur,
1523 lock->l_last_used));
1524 lv = lvf * la * unused;
1526 /* Inform pool about current CLV to see it via proc. */
1527 ldlm_pool_set_clv(pl, lv);
1529 /* Stop when SLV is not yet come from server or lv is smaller than
1531 if (slv == 0 || lv < slv)
1532 return LDLM_POLICY_KEEP_LOCK;
1534 return LDLM_POLICY_CANCEL_LOCK;
1537 static enum ldlm_policy_res
1538 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1539 struct ldlm_lock *lock,
1540 int unused, int added,
1543 enum ldlm_policy_res result;
1545 result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1546 if (result == LDLM_POLICY_KEEP_LOCK)
1549 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1553 * Callback function for proc used policy. Makes decision whether to keep
1554 * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1555 * added and number of locks to be preferably canceled \a count.
1557 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1559 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1561 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1562 struct ldlm_lock *lock,
1563 int unused, int added,
1566 /* Stop LRU processing when we reach past @count or have checked all
1568 return (added >= count) ?
1569 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1573 * Callback function for aged policy. Makes decision whether to keep \a lock in
1574 * LRU for current LRU size \a unused, added in current scan \a added and
1575 * number of locks to be preferably canceled \a count.
1577 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1579 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1581 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1582 struct ldlm_lock *lock,
1583 int unused, int added,
1586 if ((added >= count) &&
1587 cfs_time_before(cfs_time_current(),
1588 cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1589 return LDLM_POLICY_KEEP_LOCK;
1591 return LDLM_POLICY_CANCEL_LOCK;
1595 * Callback function for default policy. Makes decision whether to keep \a lock
1596 * in LRU for current LRU size \a unused, added in current scan \a added and
1597 * number of locks to be preferably canceled \a count.
1599 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1601 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1604 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1605 struct ldlm_lock *lock,
1606 int unused, int added,
1609 /* Stop LRU processing when we reach past count or have checked all
1611 return (added >= count) ?
1612 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1615 typedef enum ldlm_policy_res
1616 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1617 int unused, int added, int count);
1619 static ldlm_cancel_lru_policy_t
1620 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1622 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1623 return ldlm_cancel_no_wait_policy;
1625 if (ns_connect_lru_resize(ns)) {
1626 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1627 /* We kill passed number of old locks. */
1628 return ldlm_cancel_passed_policy;
1629 if (lru_flags & LDLM_LRU_FLAG_LRUR)
1630 return ldlm_cancel_lrur_policy;
1631 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1632 return ldlm_cancel_passed_policy;
1633 else if (lru_flags & LDLM_LRU_FLAG_LRUR_NO_WAIT)
1634 return ldlm_cancel_lrur_no_wait_policy;
1636 if (lru_flags & LDLM_LRU_FLAG_AGED)
1637 return ldlm_cancel_aged_policy;
1640 return ldlm_cancel_default_policy;
1644 * - Free space in LRU for \a count new locks,
1645 * redundant unused locks are canceled locally;
1646 * - also cancel locally unused aged locks;
1647 * - do not cancel more than \a max locks;
1648 * - GET the found locks and add them into the \a cancels list.
1650 * A client lock can be added to the l_bl_ast list only when it is
1651 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1652 * CANCEL. There are the following use cases:
1653 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1654 * ldlm_cli_cancel(), which check and set this flag properly. As any
1655 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1656 * later without any special locking.
1658 * Calling policies for enabled LRU resize:
1659 * ----------------------------------------
1660 * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1661 * cancel not more than \a count locks;
1663 * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1664 * at the beginning of LRU list);
1666 * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1667 * to memory pressre policy function;
1669 * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1671 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1672 * (typically before replaying locks) w/o
1673 * sending any RPCs or waiting for any
1674 * outstanding RPC to complete.
1676 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1677 struct list_head *cancels, int count, int max,
1678 enum ldlm_lru_flags lru_flags)
1680 ldlm_cancel_lru_policy_t pf;
1681 struct ldlm_lock *lock, *next;
1682 int added = 0, unused, remained;
1683 int no_wait = lru_flags & (LDLM_LRU_FLAG_NO_WAIT |
1684 LDLM_LRU_FLAG_LRUR_NO_WAIT);
1687 spin_lock(&ns->ns_lock);
1688 unused = ns->ns_nr_unused;
1691 if (!ns_connect_lru_resize(ns))
1692 count += unused - ns->ns_max_unused;
1694 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1695 LASSERT(pf != NULL);
1697 while (!list_empty(&ns->ns_unused_list)) {
1698 enum ldlm_policy_res result;
1699 cfs_time_t last_use = 0;
1701 /* all unused locks */
1702 if (remained-- <= 0)
1705 /* For any flags, stop scanning if @max is reached. */
1706 if (max && added >= max)
1709 list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1711 /* No locks which got blocking requests. */
1712 LASSERT(!ldlm_is_bl_ast(lock));
1714 if (no_wait && ldlm_is_skipped(lock))
1715 /* already processed */
1718 last_use = lock->l_last_used;
1719 if (last_use == cfs_time_current())
1722 /* Somebody is already doing CANCEL. No need for this
1723 * lock in LRU, do not traverse it again. */
1724 if (!ldlm_is_canceling(lock))
1727 ldlm_lock_remove_from_lru_nolock(lock);
1729 if (&lock->l_lru == &ns->ns_unused_list)
1732 LDLM_LOCK_GET(lock);
1733 spin_unlock(&ns->ns_lock);
1734 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1736 /* Pass the lock through the policy filter and see if it
1737 * should stay in LRU.
1739 * Even for shrinker policy we stop scanning if
1740 * we find a lock that should stay in the cache.
1741 * We should take into account lock age anyway
1742 * as a new lock is a valuable resource even if
1743 * it has a low weight.
1745 * That is, for shrinker policy we drop only
1746 * old locks, but additionally choose them by
1747 * their weight. Big extent locks will stay in
1749 result = pf(ns, lock, unused, added, count);
1750 if (result == LDLM_POLICY_KEEP_LOCK) {
1751 lu_ref_del(&lock->l_reference,
1752 __FUNCTION__, current);
1753 LDLM_LOCK_RELEASE(lock);
1754 spin_lock(&ns->ns_lock);
1757 if (result == LDLM_POLICY_SKIP_LOCK) {
1758 lu_ref_del(&lock->l_reference,
1760 LDLM_LOCK_RELEASE(lock);
1761 spin_lock(&ns->ns_lock);
1765 lock_res_and_lock(lock);
1766 /* Check flags again under the lock. */
1767 if (ldlm_is_canceling(lock) ||
1768 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1769 /* Another thread is removing lock from LRU, or
1770 * somebody is already doing CANCEL, or there
1771 * is a blocking request which will send cancel
1772 * by itself, or the lock is no longer unused or
1773 * the lock has been used since the pf() call and
1774 * pages could be put under it. */
1775 unlock_res_and_lock(lock);
1776 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1777 LDLM_LOCK_RELEASE(lock);
1778 spin_lock(&ns->ns_lock);
1781 LASSERT(!lock->l_readers && !lock->l_writers);
1783 /* If we have chosen to cancel this lock voluntarily, we
1784 * better send cancel notification to server, so that it
1785 * frees appropriate state. This might lead to a race
1786 * where while we are doing cancel here, server is also
1787 * silently cancelling this lock. */
1788 ldlm_clear_cancel_on_block(lock);
1790 /* Setting the CBPENDING flag is a little misleading,
1791 * but prevents an important race; namely, once
1792 * CBPENDING is set, the lock can accumulate no more
1793 * readers/writers. Since readers and writers are
1794 * already zero here, ldlm_lock_decref() won't see
1795 * this flag and call l_blocking_ast */
1796 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1798 /* We can't re-add to l_lru as it confuses the
1799 * refcounting in ldlm_lock_remove_from_lru() if an AST
1800 * arrives after we drop lr_lock below. We use l_bl_ast
1801 * and can't use l_pending_chain as it is used both on
1802 * server and client nevertheless bug 5666 says it is
1803 * used only on server */
1804 LASSERT(list_empty(&lock->l_bl_ast));
1805 list_add(&lock->l_bl_ast, cancels);
1806 unlock_res_and_lock(lock);
1807 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1808 spin_lock(&ns->ns_lock);
1812 spin_unlock(&ns->ns_lock);
1816 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1818 enum ldlm_cancel_flags cancel_flags,
1819 enum ldlm_lru_flags lru_flags)
1823 added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
1827 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1831 * Cancel at least \a nr locks from given namespace LRU.
1833 * When called with LCF_ASYNC the blocking callback will be handled
1834 * in a thread and this function will return after the thread has been
1835 * asked to call the callback. When called with LCF_ASYNC the blocking
1836 * callback will be performed in this function.
1838 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
1839 enum ldlm_cancel_flags cancel_flags,
1840 enum ldlm_lru_flags lru_flags)
1842 struct list_head cancels = LIST_HEAD_INIT(cancels);
1846 /* Just prepare the list of locks, do not actually cancel them yet.
1847 * Locks are cancelled later in a separate thread. */
1848 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
1849 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
1857 * Find and cancel locally unused locks found on resource, matched to the
1858 * given policy, mode. GET the found locks and add them into the \a cancels
1861 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1862 struct list_head *cancels,
1863 union ldlm_policy_data *policy,
1864 enum ldlm_mode mode, __u64 lock_flags,
1865 enum ldlm_cancel_flags cancel_flags,
1868 struct ldlm_lock *lock;
1873 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1874 if (opaque != NULL && lock->l_ast_data != opaque) {
1875 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1876 lock->l_ast_data, opaque);
1881 if (lock->l_readers || lock->l_writers)
1884 /* If somebody is already doing CANCEL, or blocking AST came,
1885 * skip this lock. */
1886 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
1889 if (lockmode_compat(lock->l_granted_mode, mode))
1892 /* If policy is given and this is IBITS lock, add to list only
1893 * those locks that match by policy. */
1894 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1895 !(lock->l_policy_data.l_inodebits.bits &
1896 policy->l_inodebits.bits))
1899 /* See CBPENDING comment in ldlm_cancel_lru */
1900 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1903 LASSERT(list_empty(&lock->l_bl_ast));
1904 list_add(&lock->l_bl_ast, cancels);
1905 LDLM_LOCK_GET(lock);
1910 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1912 EXPORT_SYMBOL(ldlm_cancel_resource_local);
1915 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
1917 * If \a req is NULL, send CANCEL request to server with handles of locks
1918 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1919 * separately per lock.
1920 * If \a req is not NULL, put handles of locks in \a cancels into the request
1921 * buffer at the offset \a off.
1922 * Destroy \a cancels at the end.
1924 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1925 struct ptlrpc_request *req,
1926 enum ldlm_cancel_flags flags)
1928 struct ldlm_lock *lock;
1932 if (list_empty(cancels) || count == 0)
1935 /* XXX: requests (both batched and not) could be sent in parallel.
1936 * Usually it is enough to have just 1 RPC, but it is possible that
1937 * there are too many locks to be cancelled in LRU or on a resource.
1938 * It would also speed up the case when the server does not support
1941 LASSERT(!list_empty(cancels));
1942 lock = list_entry(cancels->next, struct ldlm_lock,
1944 LASSERT(lock->l_conn_export);
1946 if (exp_connect_cancelset(lock->l_conn_export)) {
1949 ldlm_cancel_pack(req, cancels, count);
1951 res = ldlm_cli_cancel_req(lock->l_conn_export,
1955 res = ldlm_cli_cancel_req(lock->l_conn_export,
1960 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1961 "ldlm_cli_cancel_list: %d\n", res);
1966 ldlm_lock_list_put(cancels, l_bl_ast, res);
1968 LASSERT(count == 0);
1971 EXPORT_SYMBOL(ldlm_cli_cancel_list);
1974 * Cancel all locks on a resource that have 0 readers/writers.
1976 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1977 * to notify the server. */
1978 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1979 const struct ldlm_res_id *res_id,
1980 union ldlm_policy_data *policy,
1981 enum ldlm_mode mode,
1982 enum ldlm_cancel_flags flags, void *opaque)
1984 struct ldlm_resource *res;
1985 struct list_head cancels = LIST_HEAD_INIT(cancels);
1990 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1992 /* This is not a problem. */
1993 CDEBUG(D_INFO, "No resource %llu\n", res_id->name[0]);
1997 LDLM_RESOURCE_ADDREF(res);
1998 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1999 0, flags | LCF_BL_AST, opaque);
2000 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
2002 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
2005 LDLM_RESOURCE_DELREF(res);
2006 ldlm_resource_putref(res);
2009 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2011 struct ldlm_cli_cancel_arg {
2017 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2018 struct hlist_node *hnode, void *arg)
2020 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2021 struct ldlm_cli_cancel_arg *lc = arg;
2023 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2024 NULL, LCK_MINMODE, lc->lc_flags,
2026 /* must return 0 for hash iteration */
2031 * Cancel all locks on a namespace (or a specific resource, if given)
2032 * that have 0 readers/writers.
2034 * If flags & LCF_LOCAL, throw the locks away without trying
2035 * to notify the server. */
2036 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2037 const struct ldlm_res_id *res_id,
2038 enum ldlm_cancel_flags flags, void *opaque)
2040 struct ldlm_cli_cancel_arg arg = {
2042 .lc_opaque = opaque,
2050 if (res_id != NULL) {
2051 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2055 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2056 ldlm_cli_hash_cancel_unused, &arg, 0);
2061 /* Lock iterators. */
2063 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2066 struct list_head *tmp, *next;
2067 struct ldlm_lock *lock;
2068 int rc = LDLM_ITER_CONTINUE;
2073 RETURN(LDLM_ITER_CONTINUE);
2076 list_for_each_safe(tmp, next, &res->lr_granted) {
2077 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2079 if (iter(lock, closure) == LDLM_ITER_STOP)
2080 GOTO(out, rc = LDLM_ITER_STOP);
2083 list_for_each_safe(tmp, next, &res->lr_converting) {
2084 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2086 if (iter(lock, closure) == LDLM_ITER_STOP)
2087 GOTO(out, rc = LDLM_ITER_STOP);
2090 list_for_each_safe(tmp, next, &res->lr_waiting) {
2091 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2093 if (iter(lock, closure) == LDLM_ITER_STOP)
2094 GOTO(out, rc = LDLM_ITER_STOP);
2101 struct iter_helper_data {
2102 ldlm_iterator_t iter;
2106 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2108 struct iter_helper_data *helper = closure;
2109 return helper->iter(lock, helper->closure);
2112 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2113 struct hlist_node *hnode, void *arg)
2116 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2118 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2122 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2123 ldlm_iterator_t iter, void *closure)
2126 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2128 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2129 ldlm_res_iter_helper, &helper, 0);
2133 /* non-blocking function to manipulate a lock whose cb_data is being put away.
2134 * return 0: find no resource
2135 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2138 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2139 const struct ldlm_res_id *res_id,
2140 ldlm_iterator_t iter, void *data)
2142 struct ldlm_resource *res;
2146 LASSERTF(ns != NULL, "must pass in namespace\n");
2148 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2152 LDLM_RESOURCE_ADDREF(res);
2153 rc = ldlm_resource_foreach(res, iter, data);
2154 LDLM_RESOURCE_DELREF(res);
2155 ldlm_resource_putref(res);
2158 EXPORT_SYMBOL(ldlm_resource_iterate);
2162 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2164 struct list_head *list = closure;
2166 /* we use l_pending_chain here, because it's unused on clients. */
2167 LASSERTF(list_empty(&lock->l_pending_chain),
2168 "lock %p next %p prev %p\n",
2169 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2170 /* bug 9573: don't replay locks left after eviction, or
2171 * bug 17614: locks being actively cancelled. Get a reference
2172 * on a lock so that it does not disapear under us (e.g. due to cancel)
2174 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_BL_DONE))) {
2175 list_add(&lock->l_pending_chain, list);
2176 LDLM_LOCK_GET(lock);
2179 return LDLM_ITER_CONTINUE;
2182 static int replay_lock_interpret(const struct lu_env *env,
2183 struct ptlrpc_request *req,
2184 struct ldlm_async_args *aa, int rc)
2186 struct ldlm_lock *lock;
2187 struct ldlm_reply *reply;
2188 struct obd_export *exp;
2191 atomic_dec(&req->rq_import->imp_replay_inflight);
2195 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2197 GOTO(out, rc = -EPROTO);
2199 lock = ldlm_handle2lock(&aa->lock_handle);
2201 CERROR("received replay ack for unknown local cookie %#llx"
2202 " remote cookie %#llx from server %s id %s\n",
2203 aa->lock_handle.cookie, reply->lock_handle.cookie,
2204 req->rq_export->exp_client_uuid.uuid,
2205 libcfs_id2str(req->rq_peer));
2206 GOTO(out, rc = -ESTALE);
2209 /* Key change rehash lock in per-export hash with new key */
2210 exp = req->rq_export;
2211 if (exp && exp->exp_lock_hash) {
2212 /* In the function below, .hs_keycmp resolves to
2213 * ldlm_export_lock_keycmp() */
2214 /* coverity[overrun-buffer-val] */
2215 cfs_hash_rehash_key(exp->exp_lock_hash,
2216 &lock->l_remote_handle,
2217 &reply->lock_handle,
2220 lock->l_remote_handle = reply->lock_handle;
2223 LDLM_DEBUG(lock, "replayed lock:");
2224 ptlrpc_import_recovery_state_machine(req->rq_import);
2225 LDLM_LOCK_PUT(lock);
2228 ptlrpc_connect_import(req->rq_import);
2233 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2235 struct ptlrpc_request *req;
2236 struct ldlm_async_args *aa;
2237 struct ldlm_request *body;
2242 /* Bug 11974: Do not replay a lock which is actively being canceled */
2243 if (ldlm_is_bl_done(lock)) {
2244 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2248 /* If this is reply-less callback lock, we cannot replay it, since
2249 * server might have long dropped it, but notification of that event was
2250 * lost by network. (and server granted conflicting lock already) */
2251 if (ldlm_is_cancel_on_block(lock)) {
2252 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2253 ldlm_lock_cancel(lock);
2258 * If granted mode matches the requested mode, this lock is granted.
2260 * If they differ, but we have a granted mode, then we were granted
2261 * one mode and now want another: ergo, converting.
2263 * If we haven't been granted anything and are on a resource list,
2264 * then we're blocked/waiting.
2266 * If we haven't been granted anything and we're NOT on a resource list,
2267 * then we haven't got a reply yet and don't have a known disposition.
2268 * This happens whenever a lock enqueue is the request that triggers
2271 if (lock->l_granted_mode == lock->l_req_mode)
2272 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2273 else if (lock->l_granted_mode)
2274 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2275 else if (!list_empty(&lock->l_res_link))
2276 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2278 flags = LDLM_FL_REPLAY;
2280 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2281 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2285 /* We're part of recovery, so don't wait for it. */
2286 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2288 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2289 ldlm_lock2desc(lock, &body->lock_desc);
2290 body->lock_flags = ldlm_flags_to_wire(flags);
2292 ldlm_lock2handle(lock, &body->lock_handle[0]);
2293 if (lock->l_lvb_len > 0)
2294 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2295 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2297 ptlrpc_request_set_replen(req);
2298 /* notify the server we've replayed all requests.
2299 * also, we mark the request to be put on a dedicated
2300 * queue to be processed after all request replayes.
2302 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2304 LDLM_DEBUG(lock, "replaying lock:");
2306 atomic_inc(&req->rq_import->imp_replay_inflight);
2307 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2308 aa = ptlrpc_req_async_args(req);
2309 aa->lock_handle = body->lock_handle[0];
2310 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2311 ptlrpcd_add_req(req);
2317 * Cancel as many unused locks as possible before replay. since we are
2318 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2321 * Called only in recovery before replaying locks. there is no need to
2322 * replay locks that are unused. since the clients may hold thousands of
2323 * cached unused locks, dropping the unused locks can greatly reduce the
2324 * load on the servers at recovery time.
2326 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2329 struct list_head cancels = LIST_HEAD_INIT(cancels);
2331 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2332 "replay for namespace %s (%d)\n",
2333 ldlm_ns_name(ns), ns->ns_nr_unused);
2335 /* We don't need to care whether or not LRU resize is enabled
2336 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2337 * count parameter */
2338 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2339 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2341 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2342 canceled, ldlm_ns_name(ns));
2345 int ldlm_replay_locks(struct obd_import *imp)
2347 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2348 struct list_head list = LIST_HEAD_INIT(list);
2349 struct ldlm_lock *lock, *next;
2354 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2356 /* don't replay locks if import failed recovery */
2357 if (imp->imp_vbr_failed)
2360 /* ensure this doesn't fall to 0 before all have been queued */
2361 atomic_inc(&imp->imp_replay_inflight);
2363 if (ldlm_cancel_unused_locks_before_replay)
2364 ldlm_cancel_unused_locks_for_replay(ns);
2366 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2368 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2369 list_del_init(&lock->l_pending_chain);
2371 LDLM_LOCK_RELEASE(lock);
2372 continue; /* or try to do the rest? */
2374 rc = replay_one_lock(imp, lock);
2375 LDLM_LOCK_RELEASE(lock);
2378 atomic_dec(&imp->imp_replay_inflight);