4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * This file contains Asynchronous System Trap (AST) handlers and related
34 * LDLM request-processing routines.
36 * An AST is a callback issued on a lock when its state is changed. There are
37 * several different types of ASTs (callbacks) registered for each lock:
39 * - completion AST: when a lock is enqueued by some process, but cannot be
40 * granted immediately due to other conflicting locks on the same resource,
41 * the completion AST is sent to notify the caller when the lock is
44 * - blocking AST: when a lock is granted to some process, if another process
45 * enqueues a conflicting (blocking) lock on a resource, a blocking AST is
46 * sent to notify the holder(s) of the lock(s) of the conflicting lock
47 * request. The lock holder(s) must release their lock(s) on that resource in
48 * a timely manner or be evicted by the server.
50 * - glimpse AST: this is used when a process wants information about a lock
51 * (i.e. the lock value block (LVB)) but does not necessarily require holding
52 * the lock. If the resource is locked, the lock holder(s) are sent glimpse
53 * ASTs and the LVB is returned to the caller, and lock holder(s) may CANCEL
54 * their lock(s) if they are idle. If the resource is not locked, the server
58 #define DEBUG_SUBSYSTEM S_LDLM
60 #include <lustre_errno.h>
61 #include <lustre_dlm.h>
62 #include <obd_class.h>
65 #include "ldlm_internal.h"
67 unsigned int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
68 module_param(ldlm_enqueue_min, uint, 0644);
69 MODULE_PARM_DESC(ldlm_enqueue_min, "lock enqueue timeout minimum");
71 /* in client side, whether the cached locks will be canceled before replay */
72 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
74 static void interrupted_completion_wait(void *data)
78 struct lock_wait_data {
79 struct ldlm_lock *lwd_lock;
83 struct ldlm_async_args {
84 struct lustre_handle lock_handle;
88 * ldlm_request_bufsize
90 * If opcode=LDLM_ENQUEUE, 1 slot is already occupied,
91 * LDLM_LOCKREQ_HANDLE -1 slots are available.
92 * Otherwise, LDLM_LOCKREQ_HANDLE slots are available.
97 * \retval size of the request buffer
100 int ldlm_request_bufsize(int count, int type)
102 int avail = LDLM_LOCKREQ_HANDLES;
103 if (type == LDLM_ENQUEUE)
104 avail -= LDLM_ENQUEUE_CANCEL_OFF;
107 avail = (count - avail) * sizeof(struct lustre_handle);
111 return sizeof(struct ldlm_request) + avail;
114 int ldlm_expired_completion_wait(void *data)
116 struct lock_wait_data *lwd = data;
117 struct ldlm_lock *lock = lwd->lwd_lock;
118 struct obd_import *imp;
119 struct obd_device *obd;
122 if (lock->l_conn_export == NULL) {
123 static time64_t next_dump, last_dump;
125 LDLM_ERROR(lock, "lock timed out (enqueued at %lld, %llds ago); "
126 "not entering recovery in server code, just going back to sleep",
127 (s64)lock->l_last_activity,
128 (s64)(ktime_get_real_seconds() -
129 lock->l_last_activity));
130 if (ktime_get_seconds() > next_dump) {
131 last_dump = next_dump;
132 next_dump = ktime_get_seconds() + 300;
133 ldlm_namespace_dump(D_DLMTRACE,
134 ldlm_lock_to_ns(lock));
136 libcfs_debug_dumplog();
141 obd = lock->l_conn_export->exp_obd;
142 imp = obd->u.cli.cl_import;
143 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
144 LDLM_ERROR(lock, "lock timed out (enqueued at %lld, %llds ago), entering recovery for %s@%s",
145 (s64)lock->l_last_activity,
146 (s64)(ktime_get_real_seconds() - lock->l_last_activity),
147 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
153 * Calculate the Completion timeout (covering enqueue, BL AST, data flush,
154 * lock cancel, and their replies). Used for lock completion timeout on the
157 * \param[in] lock lock which is waiting the completion callback
159 * \retval timeout in seconds to wait for the server reply
162 /* We use the same basis for both server side and client side functions
163 from a single node. */
164 static time64_t ldlm_cp_timeout(struct ldlm_lock *lock)
171 /* Wait a long time for enqueue - server may have to callback a
172 * lock from another client. Server will evict the other client if it
173 * doesn't respond reasonably, and then give us the lock. */
174 timeout = at_get(ldlm_lock_to_ns_at(lock));
175 return max(3 * timeout, (time64_t) ldlm_enqueue_min);
179 * Helper function for ldlm_completion_ast(), updating timings when lock is
182 static int ldlm_completion_tail(struct ldlm_lock *lock, void *data)
187 if (ldlm_is_destroyed(lock) || ldlm_is_failed(lock)) {
188 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
190 } else if (data == NULL) {
191 LDLM_DEBUG(lock, "client-side enqueue: granted");
193 /* Take into AT only CP RPC, not immediately granted locks */
194 delay = ktime_get_real_seconds() - lock->l_last_activity;
195 LDLM_DEBUG(lock, "client-side enqueue: granted after %llds",
198 /* Update our time estimate */
199 at_measured(ldlm_lock_to_ns_at(lock), delay);
205 * Implementation of ->l_completion_ast() for a client, that doesn't wait
206 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
207 * other threads that cannot block for long.
209 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data)
213 if (flags == LDLM_FL_WAIT_NOREPROC) {
214 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
218 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
219 wake_up(&lock->l_waitq);
220 RETURN(ldlm_completion_tail(lock, data));
223 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
225 ldlm_reprocess_all(lock->l_resource);
228 EXPORT_SYMBOL(ldlm_completion_ast_async);
231 * Generic LDLM "completion" AST. This is called in several cases:
233 * - when a reply to an ENQUEUE RPC is received from the server
234 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
235 * this point (determined by flags);
237 * - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
240 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
243 * - to force all locks when resource is destroyed (cleanup_resource());
245 * - during lock conversion (not used currently).
247 * If lock is not granted in the first case, this function waits until second
248 * or penultimate cases happen in some other thread.
251 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
253 /* XXX ALLOCATE - 160 bytes */
254 struct lock_wait_data lwd;
255 struct obd_device *obd;
256 struct obd_import *imp = NULL;
257 struct l_wait_info lwi;
262 if (flags == LDLM_FL_WAIT_NOREPROC) {
263 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
267 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
268 wake_up(&lock->l_waitq);
272 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
277 obd = class_exp2obd(lock->l_conn_export);
279 /* if this is a local lock, then there is no import */
281 imp = obd->u.cli.cl_import;
284 timeout = ldlm_cp_timeout(lock);
287 lock->l_last_activity = ktime_get_real_seconds();
289 if (ldlm_is_no_timeout(lock)) {
290 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
291 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
293 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
294 ldlm_expired_completion_wait,
295 interrupted_completion_wait, &lwd);
299 spin_lock(&imp->imp_lock);
300 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
301 spin_unlock(&imp->imp_lock);
304 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
305 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
306 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
307 ldlm_set_fail_loc(lock);
310 /* Go to sleep until the lock is granted or cancelled. */
311 rc = l_wait_event(lock->l_waitq,
312 is_granted_or_cancelled(lock), &lwi);
316 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
321 RETURN(ldlm_completion_tail(lock, data));
323 EXPORT_SYMBOL(ldlm_completion_ast);
326 * A helper to build a blocking AST function
328 * Perform a common operation for blocking ASTs:
329 * defferred lock cancellation.
331 * \param lock the lock blocking or canceling AST was called on
333 * \see mdt_blocking_ast
334 * \see ldlm_blocking_ast
336 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
341 ldlm_set_cbpending(lock);
342 do_ast = (!lock->l_readers && !lock->l_writers);
343 unlock_res_and_lock(lock);
346 struct lustre_handle lockh;
349 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
350 ldlm_lock2handle(lock, &lockh);
351 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
353 CERROR("ldlm_cli_cancel: %d\n", rc);
355 LDLM_DEBUG(lock, "Lock still has references, will be "
360 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
363 * Server blocking AST
365 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
368 * \param lock the lock which blocks a request or cancelling lock
371 * \param flag indicates whether this cancelling or blocking callback
373 * \see ldlm_blocking_ast_nocheck
375 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
376 void *data, int flag)
380 if (flag == LDLM_CB_CANCELING) {
381 /* Don't need to do anything here. */
385 lock_res_and_lock(lock);
386 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
387 * that ldlm_blocking_ast is called just before intent_policy method
388 * takes the lr_lock, then by the time we get the lock, we might not
389 * be the correct blocking function anymore. So check, and return
391 if (lock->l_blocking_ast != ldlm_blocking_ast) {
392 unlock_res_and_lock(lock);
395 RETURN(ldlm_blocking_ast_nocheck(lock));
397 EXPORT_SYMBOL(ldlm_blocking_ast);
400 * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
402 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
403 * rather subtle: with OST-side locking, it may so happen that _all_ extent
404 * locks are held by the OST. If client wants to obtain the current file size
405 * it calls ll_glimpse_size(), and (as all locks are held only on the server),
406 * this dummy glimpse callback fires and does nothing. The client still
407 * receives the correct file size due to the following fragment of code in
408 * ldlm_cb_interpret():
410 * if (rc == -ELDLM_NO_LOCK_DATA) {
411 * LDLM_DEBUG(lock, "lost race - client has a lock but no"
413 * ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
416 * That is, after the glimpse returns this error, ofd_lvbo_update() is called
417 * and returns the updated file attributes from the inode to the client.
419 * See also comment in ofd_intent_policy() on why servers must set a non-NULL
420 * l_glimpse_ast when grabbing DLM locks. Otherwise, the server will assume
421 * that the object is in the process of being destroyed.
423 * \param[in] lock DLM lock being glimpsed, unused
424 * \param[in] reqp pointer to ptlrpc_request, unused
426 * \retval -ELDLM_NO_LOCK_DATA to get attributes from disk object
428 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
430 return -ELDLM_NO_LOCK_DATA;
434 * Enqueue a local lock (typically on a server).
436 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
437 const struct ldlm_res_id *res_id,
438 enum ldlm_type type, union ldlm_policy_data *policy,
439 enum ldlm_mode mode, __u64 *flags,
440 ldlm_blocking_callback blocking,
441 ldlm_completion_callback completion,
442 ldlm_glimpse_callback glimpse,
443 void *data, __u32 lvb_len, enum lvb_type lvb_type,
444 const __u64 *client_cookie,
445 struct lustre_handle *lockh)
447 struct ldlm_lock *lock;
449 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
450 .lcs_blocking = blocking,
451 .lcs_glimpse = glimpse,
455 LASSERT(!(*flags & LDLM_FL_REPLAY));
456 if (unlikely(ns_is_client(ns))) {
457 CERROR("Trying to enqueue local lock in a shadow namespace\n");
461 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
464 GOTO(out_nolock, err = PTR_ERR(lock));
466 err = ldlm_lvbo_init(lock->l_resource);
468 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
469 ldlm_lock_destroy_nolock(lock);
473 ldlm_lock2handle(lock, lockh);
475 /* NB: we don't have any lock now (lock_res_and_lock)
476 * because it's a new lock */
477 ldlm_lock_addref_internal_nolock(lock, mode);
478 ldlm_set_local(lock);
479 if (*flags & LDLM_FL_ATOMIC_CB)
480 ldlm_set_atomic_cb(lock);
483 lock->l_policy_data = *policy;
484 if (client_cookie != NULL)
485 lock->l_client_cookie = *client_cookie;
486 if (type == LDLM_EXTENT) {
487 /* extent lock without policy is a bug */
491 lock->l_req_extent = policy->l_extent;
494 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
495 if (unlikely(err != ELDLM_OK))
499 *policy = lock->l_policy_data;
501 if (lock->l_completion_ast)
502 lock->l_completion_ast(lock, *flags, NULL);
504 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
507 LDLM_LOCK_RELEASE(lock);
511 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
513 static void failed_lock_cleanup(struct ldlm_namespace *ns,
514 struct ldlm_lock *lock, int mode)
518 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
519 lock_res_and_lock(lock);
520 /* Check that lock is not granted or failed, we might race. */
521 if ((lock->l_req_mode != lock->l_granted_mode) &&
522 !ldlm_is_failed(lock)) {
523 /* Make sure that this lock will not be found by raced
524 * bl_ast and -EINVAL reply is sent to server anyways.
526 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
527 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
530 unlock_res_and_lock(lock);
534 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
535 "LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
537 LDLM_DEBUG(lock, "lock was granted or failed in race");
539 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
540 * from llite/file.c/ll_file_flock(). */
541 /* This code makes for the fact that we do not have blocking handler on
542 * a client for flock locks. As such this is the place where we must
543 * completely kill failed locks. (interrupted and those that
544 * were waiting to be granted when server evicted us. */
545 if (lock->l_resource->lr_type == LDLM_FLOCK) {
546 lock_res_and_lock(lock);
547 if (!ldlm_is_destroyed(lock)) {
548 ldlm_resource_unlink_lock(lock);
549 ldlm_lock_decref_internal_nolock(lock, mode);
550 ldlm_lock_destroy_nolock(lock);
552 unlock_res_and_lock(lock);
554 ldlm_lock_decref_internal(lock, mode);
559 * Finishing portion of client lock enqueue code.
561 * Called after receiving reply from server.
563 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
564 enum ldlm_type type, __u8 with_policy,
565 enum ldlm_mode mode, __u64 *flags, void *lvb,
566 __u32 lvb_len, const struct lustre_handle *lockh,
569 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
570 int is_replay = *flags & LDLM_FL_REPLAY;
571 struct ldlm_lock *lock;
572 struct ldlm_reply *reply;
573 int cleanup_phase = 1;
576 lock = ldlm_handle2lock(lockh);
577 /* ldlm_cli_enqueue is holding a reference on this lock. */
579 LASSERT(type == LDLM_FLOCK);
583 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
584 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
586 if (rc != ELDLM_OK) {
588 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
589 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
591 if (rc != ELDLM_LOCK_ABORTED)
595 /* Before we return, swab the reply */
596 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
598 GOTO(cleanup, rc = -EPROTO);
603 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
606 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
607 GOTO(cleanup, rc = size);
608 } else if (unlikely(size > lvb_len)) {
609 LDLM_ERROR(lock, "Replied LVB is larger than "
610 "expectation, expected = %d, replied = %d",
612 GOTO(cleanup, rc = -EINVAL);
617 if (rc == ELDLM_LOCK_ABORTED) {
618 if (lvb_len > 0 && lvb != NULL)
619 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
621 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
624 /* lock enqueued on the server */
627 lock_res_and_lock(lock);
628 /* Key change rehash lock in per-export hash with new key */
629 if (exp->exp_lock_hash) {
630 /* In the function below, .hs_keycmp resolves to
631 * ldlm_export_lock_keycmp() */
632 /* coverity[overrun-buffer-val] */
633 cfs_hash_rehash_key(exp->exp_lock_hash,
634 &lock->l_remote_handle,
638 lock->l_remote_handle = reply->lock_handle;
641 *flags = ldlm_flags_from_wire(reply->lock_flags);
642 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
643 LDLM_FL_INHERIT_MASK);
644 unlock_res_and_lock(lock);
646 CDEBUG(D_INFO, "local: %p, remote cookie: %#llx, flags: %#llx\n",
647 lock, reply->lock_handle.cookie, *flags);
649 /* If enqueue returned a blocked lock but the completion handler has
650 * already run, then it fixed up the resource and we don't need to do it
652 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
653 int newmode = reply->lock_desc.l_req_mode;
655 if (newmode && newmode != lock->l_req_mode) {
656 LDLM_DEBUG(lock, "server returned different mode %s",
657 ldlm_lockname[newmode]);
658 lock->l_req_mode = newmode;
661 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
662 &lock->l_resource->lr_name)) {
663 CDEBUG(D_INFO, "remote intent success, locking "DLDLMRES
664 " instead of "DLDLMRES"\n",
665 PLDLMRES(&reply->lock_desc.l_resource),
666 PLDLMRES(lock->l_resource));
668 rc = ldlm_lock_change_resource(ns, lock,
669 &reply->lock_desc.l_resource.lr_name);
670 if (rc || lock->l_resource == NULL)
671 GOTO(cleanup, rc = -ENOMEM);
672 LDLM_DEBUG(lock, "client-side enqueue, new resource");
676 /* We assume lock type cannot change on server*/
677 ldlm_convert_policy_to_local(exp,
678 lock->l_resource->lr_type,
679 &reply->lock_desc.l_policy_data,
680 &lock->l_policy_data);
683 if (type != LDLM_PLAIN)
684 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
687 if ((*flags) & LDLM_FL_AST_SENT) {
688 lock_res_and_lock(lock);
689 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
690 unlock_res_and_lock(lock);
691 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
694 /* If the lock has already been granted by a completion AST, don't
695 * clobber the LVB with an older one. */
697 /* We must lock or a racing completion might update lvb without
698 * letting us know and we'll clobber the correct value.
699 * Cannot unlock after the check either, a that still leaves
700 * a tiny window for completion to get in */
701 lock_res_and_lock(lock);
702 if (lock->l_req_mode != lock->l_granted_mode)
703 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
704 lock->l_lvb_data, lvb_len);
705 unlock_res_and_lock(lock);
713 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
714 if (lock->l_completion_ast != NULL) {
715 int err = lock->l_completion_ast(lock, *flags, NULL);
723 if (lvb_len > 0 && lvb != NULL) {
724 /* Copy the LVB here, and not earlier, because the completion
725 * AST (if any) can override what we got in the reply */
726 memcpy(lvb, lock->l_lvb_data, lvb_len);
729 LDLM_DEBUG(lock, "client-side enqueue END");
732 if (cleanup_phase == 1 && rc)
733 failed_lock_cleanup(ns, lock, mode);
734 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
736 LDLM_LOCK_RELEASE(lock);
739 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
742 * Estimate number of lock handles that would fit into request of given
743 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
744 * a single page on the send/receive side. XXX: 512 should be changed to
745 * more adequate value.
747 static inline int ldlm_req_handles_avail(int req_size, int off)
751 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
752 if (likely(avail >= 0))
753 avail /= (int)sizeof(struct lustre_handle);
756 avail += LDLM_LOCKREQ_HANDLES - off;
761 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
762 enum req_location loc,
765 __u32 size = req_capsule_msg_size(pill, loc);
766 return ldlm_req_handles_avail(size, off);
769 static inline int ldlm_format_handles_avail(struct obd_import *imp,
770 const struct req_format *fmt,
771 enum req_location loc, int off)
773 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
774 return ldlm_req_handles_avail(size, off);
778 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
779 * \a count locks in \a cancels.
781 * This is to be called by functions preparing their own requests that
782 * might contain lists of locks to cancel in addition to actual operation
783 * that needs to be performed.
785 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
786 int version, int opc, int canceloff,
787 struct list_head *cancels, int count)
789 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
790 struct req_capsule *pill = &req->rq_pill;
791 struct ldlm_request *dlm = NULL;
792 struct list_head head = LIST_HEAD_INIT(head);
793 enum ldlm_lru_flags lru_flags;
794 int avail, to_free, pack = 0;
800 if (ns_connect_cancelset(ns)) {
801 /* Estimate the amount of available space in the request. */
802 req_capsule_filled_sizes(pill, RCL_CLIENT);
803 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
805 lru_flags = LDLM_LRU_FLAG_NO_WAIT | (ns_connect_lru_resize(ns) ?
806 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED);
807 to_free = !ns_connect_lru_resize(ns) &&
808 opc == LDLM_ENQUEUE ? 1 : 0;
810 /* Cancel LRU locks here _only_ if the server supports
811 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
812 * RPC, which will make us slower. */
814 count += ldlm_cancel_lru_local(ns, cancels, to_free,
821 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
822 ldlm_request_bufsize(pack, opc));
825 rc = ptlrpc_request_pack(req, version, opc);
827 ldlm_lock_list_put(cancels, l_bl_ast, count);
831 if (ns_connect_cancelset(ns)) {
833 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
835 /* Skip first lock handler in ldlm_request_pack(),
836 * this method will increment @lock_count according
837 * to the lock handle amount actually written to
839 dlm->lock_count = canceloff;
841 /* Pack into the request @pack lock handles. */
842 ldlm_cli_cancel_list(cancels, pack, req, 0);
843 /* Prepare and send separate cancel RPC for others. */
844 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
846 ldlm_lock_list_put(cancels, l_bl_ast, count);
850 EXPORT_SYMBOL(ldlm_prep_elc_req);
852 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
853 struct list_head *cancels, int count)
855 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
856 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
858 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
860 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
862 struct ptlrpc_request *req;
866 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
868 RETURN(ERR_PTR(-ENOMEM));
870 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
872 ptlrpc_request_free(req);
876 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
877 ptlrpc_request_set_replen(req);
880 EXPORT_SYMBOL(ldlm_enqueue_pack);
883 * Client-side lock enqueue.
885 * If a request has some specific initialisation it is passed in \a reqp,
886 * otherwise it is created in ldlm_cli_enqueue.
888 * Supports sync and async requests, pass \a async flag accordingly. If a
889 * request was created in ldlm_cli_enqueue and it is the async request,
890 * pass it to the caller in \a reqp.
892 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
893 struct ldlm_enqueue_info *einfo,
894 const struct ldlm_res_id *res_id,
895 union ldlm_policy_data const *policy, __u64 *flags,
896 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
897 struct lustre_handle *lockh, int async)
899 struct ldlm_namespace *ns;
900 struct ldlm_lock *lock;
901 struct ldlm_request *body;
902 int is_replay = *flags & LDLM_FL_REPLAY;
903 int req_passed_in = 1;
905 struct ptlrpc_request *req;
908 LASSERT(exp != NULL);
910 ns = exp->exp_obd->obd_namespace;
912 /* If we're replaying this lock, just check some invariants.
913 * If we're creating a new lock, get everything all setup nice. */
915 lock = ldlm_handle2lock_long(lockh, 0);
916 LASSERT(lock != NULL);
917 LDLM_DEBUG(lock, "client-side enqueue START");
918 LASSERT(exp == lock->l_conn_export);
920 const struct ldlm_callback_suite cbs = {
921 .lcs_completion = einfo->ei_cb_cp,
922 .lcs_blocking = einfo->ei_cb_bl,
923 .lcs_glimpse = einfo->ei_cb_gl
925 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
926 einfo->ei_mode, &cbs, einfo->ei_cbdata,
929 RETURN(PTR_ERR(lock));
930 /* for the local lock, add the reference */
931 ldlm_lock_addref_internal(lock, einfo->ei_mode);
932 ldlm_lock2handle(lock, lockh);
934 lock->l_policy_data = *policy;
936 if (einfo->ei_type == LDLM_EXTENT) {
937 /* extent lock without policy is a bug */
941 lock->l_req_extent = policy->l_extent;
943 LDLM_DEBUG(lock, "client-side enqueue START, flags %#llx",
947 lock->l_conn_export = exp;
948 lock->l_export = NULL;
949 lock->l_blocking_ast = einfo->ei_cb_bl;
950 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL));
951 lock->l_last_activity = ktime_get_real_seconds();
953 /* lock not sent to server yet */
954 if (reqp == NULL || *reqp == NULL) {
955 req = ldlm_enqueue_pack(exp, lvb_len);
957 failed_lock_cleanup(ns, lock, einfo->ei_mode);
958 LDLM_LOCK_RELEASE(lock);
959 RETURN(PTR_ERR(req));
969 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
971 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
972 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
975 if (*flags & LDLM_FL_NDELAY) {
976 DEBUG_REQ(D_DLMTRACE, req, "enque lock with no delay\n");
977 req->rq_no_resend = req->rq_no_delay = 1;
978 /* probably set a shorter timeout value and handle ETIMEDOUT
979 * in osc_lock_upcall() correctly */
980 /* lustre_msg_set_timeout(req, req->rq_timeout / 2); */
983 /* Dump lock data into the request buffer */
984 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
985 ldlm_lock2desc(lock, &body->lock_desc);
986 body->lock_flags = ldlm_flags_to_wire(*flags);
987 body->lock_handle[0] = *lockh;
989 /* extended LDLM opcodes in client stats */
990 if (exp->exp_obd->obd_svc_stats != NULL) {
991 bool glimpse = *flags & LDLM_FL_HAS_INTENT;
993 /* OST glimpse has no intent buffer */
994 if (req_capsule_has_field(&req->rq_pill, &RMF_LDLM_INTENT,
996 struct ldlm_intent *it;
998 it = req_capsule_client_get(&req->rq_pill,
1000 glimpse = (it && (it->opc == IT_GLIMPSE));
1004 ldlm_svc_get_eopc(body, exp->exp_obd->obd_svc_stats);
1006 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1008 LDLM_GLIMPSE_ENQUEUE);
1012 LASSERT(reqp != NULL);
1016 LDLM_DEBUG(lock, "sending request");
1018 rc = ptlrpc_queue_wait(req);
1020 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
1021 einfo->ei_mode, flags, lvb, lvb_len,
1024 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
1025 * one reference that we took */
1027 LDLM_LOCK_RELEASE(lock);
1031 if (!req_passed_in && req != NULL) {
1032 ptlrpc_req_finished(req);
1039 EXPORT_SYMBOL(ldlm_cli_enqueue);
1041 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
1044 struct ldlm_resource *res;
1047 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1048 CERROR("Trying to cancel local lock\n");
1051 LDLM_DEBUG(lock, "client-side local convert");
1053 res = ldlm_lock_convert(lock, new_mode, flags);
1055 ldlm_reprocess_all(res);
1058 rc = LUSTRE_EDEADLK;
1060 LDLM_DEBUG(lock, "client-side local convert handler END");
1061 LDLM_LOCK_PUT(lock);
1065 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
1066 * conversion of locks which are on the waiting or converting queue */
1067 /* Caller of this code is supposed to take care of lock readers/writers
1069 int ldlm_cli_convert(const struct lustre_handle *lockh, int new_mode,
1072 struct ldlm_request *body;
1073 struct ldlm_reply *reply;
1074 struct ldlm_lock *lock;
1075 struct ldlm_resource *res;
1076 struct ptlrpc_request *req;
1080 lock = ldlm_handle2lock(lockh);
1087 if (lock->l_conn_export == NULL)
1088 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
1090 LDLM_DEBUG(lock, "client-side convert");
1092 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
1093 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1096 LDLM_LOCK_PUT(lock);
1100 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1101 body->lock_handle[0] = lock->l_remote_handle;
1103 body->lock_desc.l_req_mode = new_mode;
1104 body->lock_flags = ldlm_flags_to_wire(*flags);
1107 ptlrpc_request_set_replen(req);
1108 rc = ptlrpc_queue_wait(req);
1112 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1114 GOTO(out, rc = -EPROTO);
1117 GOTO(out, rc = req->rq_status);
1119 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
1121 ldlm_reprocess_all(res);
1122 /* Go to sleep until the lock is granted. */
1123 /* FIXME: or cancelled. */
1124 if (lock->l_completion_ast) {
1125 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1131 rc = LUSTRE_EDEADLK;
1135 LDLM_LOCK_PUT(lock);
1136 ptlrpc_req_finished(req);
1141 * Cancel locks locally.
1143 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1144 * \retval LDLM_FL_CANCELING otherwise;
1145 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1147 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1149 __u64 rc = LDLM_FL_LOCAL_ONLY;
1152 if (lock->l_conn_export) {
1155 LDLM_DEBUG(lock, "client-side cancel");
1156 /* Set this flag to prevent others from getting new references*/
1157 lock_res_and_lock(lock);
1158 ldlm_set_cbpending(lock);
1159 local_only = !!(lock->l_flags &
1160 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1161 ldlm_cancel_callback(lock);
1162 rc = (ldlm_is_bl_ast(lock)) ?
1163 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1164 unlock_res_and_lock(lock);
1167 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1169 rc = LDLM_FL_LOCAL_ONLY;
1171 ldlm_lock_cancel(lock);
1173 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1174 LDLM_ERROR(lock, "Trying to cancel local lock");
1177 LDLM_DEBUG(lock, "server-side local cancel");
1178 ldlm_lock_cancel(lock);
1179 ldlm_reprocess_all(lock->l_resource);
1186 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1188 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1189 struct list_head *head, int count)
1191 struct ldlm_request *dlm;
1192 struct ldlm_lock *lock;
1193 int max, packed = 0;
1196 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1197 LASSERT(dlm != NULL);
1199 /* Check the room in the request buffer. */
1200 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1201 sizeof(struct ldlm_request);
1202 max /= sizeof(struct lustre_handle);
1203 max += LDLM_LOCKREQ_HANDLES;
1204 LASSERT(max >= dlm->lock_count + count);
1206 /* XXX: it would be better to pack lock handles grouped by resource.
1207 * so that the server cancel would call filter_lvbo_update() less
1209 list_for_each_entry(lock, head, l_bl_ast) {
1212 LASSERT(lock->l_conn_export);
1213 /* Pack the lock handle to the given request buffer. */
1214 LDLM_DEBUG(lock, "packing");
1215 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1218 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1223 * Prepare and send a batched cancel RPC. It will include \a count lock
1224 * handles of locks given in \a cancels list. */
1225 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1226 int count, enum ldlm_cancel_flags flags)
1228 struct ptlrpc_request *req = NULL;
1229 struct obd_import *imp;
1234 LASSERT(exp != NULL);
1237 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1239 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1242 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1243 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1248 imp = class_exp2cliimp(exp);
1249 if (imp == NULL || imp->imp_invalid) {
1251 "skipping cancel on invalid import %p\n", imp);
1255 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1257 GOTO(out, rc = -ENOMEM);
1259 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1260 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1261 ldlm_request_bufsize(count, LDLM_CANCEL));
1263 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1265 ptlrpc_request_free(req);
1269 /* If OSP want cancel cross-MDT lock, let's not block it in
1270 * in recovery, otherwise the lock will not released, if
1271 * the remote target is also in recovery, and it also need
1272 * this lock, it might cause deadlock. */
1273 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1274 exp->exp_obd->obd_lu_dev != NULL &&
1275 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1276 struct lu_device *top_dev;
1278 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1279 if (top_dev != NULL &&
1280 top_dev->ld_obd->obd_recovering)
1281 req->rq_allow_replay = 1;
1284 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1285 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1286 ptlrpc_at_set_req_timeout(req);
1288 ldlm_cancel_pack(req, cancels, count);
1290 ptlrpc_request_set_replen(req);
1291 if (flags & LCF_ASYNC) {
1292 ptlrpcd_add_req(req);
1297 rc = ptlrpc_queue_wait(req);
1298 if (rc == LUSTRE_ESTALE) {
1299 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1300 "out of sync -- not fatal\n",
1301 libcfs_nid2str(req->rq_import->
1302 imp_connection->c_peer.nid));
1304 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1305 req->rq_import_generation == imp->imp_generation) {
1306 ptlrpc_req_finished(req);
1308 } else if (rc != ELDLM_OK) {
1309 /* -ESHUTDOWN is common on umount */
1310 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1311 "Got rc %d from cancel RPC: "
1312 "canceling anyway\n", rc);
1319 ptlrpc_req_finished(req);
1322 return sent ? sent : rc;
1325 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1327 LASSERT(imp != NULL);
1328 return &imp->imp_obd->obd_namespace->ns_pool;
1332 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1334 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1336 struct obd_device *obd;
1340 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1341 !imp_connect_lru_resize(req->rq_import)))
1344 * Do nothing for corner cases.
1349 /* In some cases RPC may contain SLV and limit zeroed out. This
1350 * is the case when server does not support LRU resize feature.
1351 * This is also possible in some recovery cases when server-side
1352 * reqs have no reference to the OBD export and thus access to
1353 * server-side namespace is not possible. */
1354 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1355 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1356 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1357 "(SLV: %llu, Limit: %u)",
1358 lustre_msg_get_slv(req->rq_repmsg),
1359 lustre_msg_get_limit(req->rq_repmsg));
1363 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1364 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1365 obd = req->rq_import->imp_obd;
1367 /* Set new SLV and limit in OBD fields to make them accessible
1368 * to the pool thread. We do not access obd_namespace and pool
1369 * directly here as there is no reliable way to make sure that
1370 * they are still alive at cleanup time. Evil races are possible
1371 * which may cause Oops at that time. */
1372 write_lock(&obd->obd_pool_lock);
1373 obd->obd_pool_slv = new_slv;
1374 obd->obd_pool_limit = new_limit;
1375 write_unlock(&obd->obd_pool_lock);
1381 * Client side lock cancel.
1383 * Lock must not have any readers or writers by this time.
1385 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1386 enum ldlm_cancel_flags cancel_flags)
1388 struct obd_export *exp;
1389 enum ldlm_lru_flags lru_flags;
1390 int avail, count = 1;
1392 struct ldlm_namespace *ns;
1393 struct ldlm_lock *lock;
1394 struct list_head cancels = LIST_HEAD_INIT(cancels);
1397 lock = ldlm_handle2lock_long(lockh, 0);
1399 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1403 lock_res_and_lock(lock);
1404 /* Lock is being canceled and the caller doesn't want to wait */
1405 if (ldlm_is_canceling(lock)) {
1406 if (cancel_flags & LCF_ASYNC) {
1407 unlock_res_and_lock(lock);
1409 struct l_wait_info lwi = { 0 };
1411 unlock_res_and_lock(lock);
1412 l_wait_event(lock->l_waitq, is_bl_done(lock), &lwi);
1414 LDLM_LOCK_RELEASE(lock);
1418 ldlm_set_canceling(lock);
1419 unlock_res_and_lock(lock);
1421 rc = ldlm_cli_cancel_local(lock);
1422 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1423 LDLM_LOCK_RELEASE(lock);
1426 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1427 * RPC which goes to canceld portal, so we can cancel other LRU locks
1428 * here and send them all as one LDLM_CANCEL RPC. */
1429 LASSERT(list_empty(&lock->l_bl_ast));
1430 list_add(&lock->l_bl_ast, &cancels);
1432 exp = lock->l_conn_export;
1433 if (exp_connect_cancelset(exp)) {
1434 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1439 ns = ldlm_lock_to_ns(lock);
1440 lru_flags = ns_connect_lru_resize(ns) ?
1441 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1442 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1443 LCF_BL_AST, lru_flags);
1445 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1448 EXPORT_SYMBOL(ldlm_cli_cancel);
1451 * Locally cancel up to \a count locks in list \a cancels.
1452 * Return the number of cancelled locks.
1454 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1455 enum ldlm_cancel_flags cancel_flags)
1457 struct list_head head = LIST_HEAD_INIT(head);
1458 struct ldlm_lock *lock, *next;
1459 int left = 0, bl_ast = 0;
1463 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1467 if (cancel_flags & LCF_LOCAL) {
1468 rc = LDLM_FL_LOCAL_ONLY;
1469 ldlm_lock_cancel(lock);
1471 rc = ldlm_cli_cancel_local(lock);
1473 /* Until we have compound requests and can send LDLM_CANCEL
1474 * requests batched with generic RPCs, we need to send cancels
1475 * with the LDLM_FL_BL_AST flag in a separate RPC from
1476 * the one being generated now. */
1477 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1478 LDLM_DEBUG(lock, "Cancel lock separately");
1479 list_del_init(&lock->l_bl_ast);
1480 list_add(&lock->l_bl_ast, &head);
1484 if (rc == LDLM_FL_LOCAL_ONLY) {
1485 /* CANCEL RPC should not be sent to server. */
1486 list_del_init(&lock->l_bl_ast);
1487 LDLM_LOCK_RELEASE(lock);
1493 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1500 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1501 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1502 * readahead requests, ...)
1504 static enum ldlm_policy_res
1505 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1506 int unused, int added, int count)
1508 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1510 /* don't check added & count since we want to process all locks
1512 * It's fine to not take lock to access lock->l_resource since
1513 * the lock has already been granted so it won't change. */
1514 switch (lock->l_resource->lr_type) {
1517 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1520 result = LDLM_POLICY_SKIP_LOCK;
1521 lock_res_and_lock(lock);
1522 ldlm_set_skipped(lock);
1523 unlock_res_and_lock(lock);
1531 * Callback function for LRU-resize policy. Decides whether to keep
1532 * \a lock in LRU for current \a LRU size \a unused, added in current
1533 * scan \a added and number of locks to be preferably canceled \a count.
1535 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1537 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1539 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1540 struct ldlm_lock *lock,
1541 int unused, int added,
1544 ktime_t cur = ktime_get();
1545 struct ldlm_pool *pl = &ns->ns_pool;
1549 /* Stop LRU processing when we reach past @count or have checked all
1551 if (count && added >= count)
1552 return LDLM_POLICY_KEEP_LOCK;
1554 /* Despite of the LV, It doesn't make sense to keep the lock which
1555 * is unused for ns_max_age time.
1557 if (ktime_after(ktime_get(),
1558 ktime_add(lock->l_last_used, ns->ns_max_age)))
1559 return LDLM_POLICY_CANCEL_LOCK;
1561 slv = ldlm_pool_get_slv(pl);
1562 lvf = ldlm_pool_get_lvf(pl);
1563 la = ktime_to_ns(ktime_sub(cur, lock->l_last_used)) / NSEC_PER_SEC;
1564 lv = lvf * la * unused;
1566 /* Inform pool about current CLV to see it via proc. */
1567 ldlm_pool_set_clv(pl, lv);
1569 /* Stop when SLV is not yet come from server or lv is smaller than
1571 if (slv == 0 || lv < slv)
1572 return LDLM_POLICY_KEEP_LOCK;
1574 return LDLM_POLICY_CANCEL_LOCK;
1577 static enum ldlm_policy_res
1578 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1579 struct ldlm_lock *lock,
1580 int unused, int added,
1583 enum ldlm_policy_res result;
1585 result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1586 if (result == LDLM_POLICY_KEEP_LOCK)
1589 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1593 * Callback function for proc used policy. Makes decision whether to keep
1594 * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1595 * added and number of locks to be preferably canceled \a count.
1597 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1599 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1601 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1602 struct ldlm_lock *lock,
1603 int unused, int added,
1606 /* Stop LRU processing when we reach past @count or have checked all
1608 return (added >= count) ?
1609 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1613 * Callback function for aged policy. Makes decision whether to keep \a lock in
1614 * LRU for current LRU size \a unused, added in current scan \a added and
1615 * number of locks to be preferably canceled \a count.
1617 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1619 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1621 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1622 struct ldlm_lock *lock,
1623 int unused, int added,
1626 if ((added >= count) &&
1627 ktime_before(ktime_get(),
1628 ktime_add(lock->l_last_used, ns->ns_max_age)))
1629 return LDLM_POLICY_KEEP_LOCK;
1631 return LDLM_POLICY_CANCEL_LOCK;
1634 static enum ldlm_policy_res
1635 ldlm_cancel_aged_no_wait_policy(struct ldlm_namespace *ns,
1636 struct ldlm_lock *lock,
1637 int unused, int added, int count)
1639 enum ldlm_policy_res result;
1641 result = ldlm_cancel_aged_policy(ns, lock, unused, added, count);
1642 if (result == LDLM_POLICY_KEEP_LOCK)
1645 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1649 * Callback function for default policy. Makes decision whether to keep \a lock
1650 * in LRU for current LRU size \a unused, added in current scan \a added and
1651 * number of locks to be preferably canceled \a count.
1653 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1655 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1658 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1659 struct ldlm_lock *lock,
1660 int unused, int added,
1663 /* Stop LRU processing when we reach past count or have checked all
1665 return (added >= count) ?
1666 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1669 typedef enum ldlm_policy_res
1670 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1671 int unused, int added, int count);
1673 static ldlm_cancel_lru_policy_t
1674 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1676 if (ns_connect_lru_resize(ns)) {
1677 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1678 /* We kill passed number of old locks. */
1679 return ldlm_cancel_passed_policy;
1680 if (lru_flags & LDLM_LRU_FLAG_LRUR) {
1681 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1682 return ldlm_cancel_lrur_no_wait_policy;
1684 return ldlm_cancel_lrur_policy;
1686 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1687 return ldlm_cancel_passed_policy;
1689 if (lru_flags & LDLM_LRU_FLAG_AGED) {
1690 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1691 return ldlm_cancel_aged_no_wait_policy;
1693 return ldlm_cancel_aged_policy;
1696 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1697 return ldlm_cancel_no_wait_policy;
1699 return ldlm_cancel_default_policy;
1703 * - Free space in LRU for \a count new locks,
1704 * redundant unused locks are canceled locally;
1705 * - also cancel locally unused aged locks;
1706 * - do not cancel more than \a max locks;
1707 * - GET the found locks and add them into the \a cancels list.
1709 * A client lock can be added to the l_bl_ast list only when it is
1710 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1711 * CANCEL. There are the following use cases:
1712 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1713 * ldlm_cli_cancel(), which check and set this flag properly. As any
1714 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1715 * later without any special locking.
1717 * Calling policies for enabled LRU resize:
1718 * ----------------------------------------
1719 * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1720 * cancel not more than \a count locks;
1722 * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1723 * at the beginning of LRU list);
1725 * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1726 * to memory pressre policy function;
1728 * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1730 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1731 * (typically before replaying locks) w/o
1732 * sending any RPCs or waiting for any
1733 * outstanding RPC to complete.
1735 * flags & LDLM_CANCEL_CLEANUP - when cancelling read locks, do not check for
1736 * other read locks covering the same pages, just
1737 * discard those pages.
1739 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1740 struct list_head *cancels, int count, int max,
1741 enum ldlm_lru_flags lru_flags)
1743 ldlm_cancel_lru_policy_t pf;
1744 struct ldlm_lock *lock, *next;
1745 int added = 0, unused, remained;
1746 int no_wait = lru_flags & LDLM_LRU_FLAG_NO_WAIT;
1749 spin_lock(&ns->ns_lock);
1750 unused = ns->ns_nr_unused;
1753 if (!ns_connect_lru_resize(ns))
1754 count += unused - ns->ns_max_unused;
1756 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1757 LASSERT(pf != NULL);
1759 while (!list_empty(&ns->ns_unused_list)) {
1760 enum ldlm_policy_res result;
1761 ktime_t last_use = ktime_set(0, 0);
1763 /* all unused locks */
1764 if (remained-- <= 0)
1767 /* For any flags, stop scanning if @max is reached. */
1768 if (max && added >= max)
1771 list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1773 /* No locks which got blocking requests. */
1774 LASSERT(!ldlm_is_bl_ast(lock));
1776 if (no_wait && ldlm_is_skipped(lock))
1777 /* already processed */
1780 last_use = lock->l_last_used;
1782 /* Somebody is already doing CANCEL. No need for this
1783 * lock in LRU, do not traverse it again. */
1784 if (!ldlm_is_canceling(lock))
1787 ldlm_lock_remove_from_lru_nolock(lock);
1789 if (&lock->l_lru == &ns->ns_unused_list)
1792 LDLM_LOCK_GET(lock);
1793 spin_unlock(&ns->ns_lock);
1794 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1796 /* Pass the lock through the policy filter and see if it
1797 * should stay in LRU.
1799 * Even for shrinker policy we stop scanning if
1800 * we find a lock that should stay in the cache.
1801 * We should take into account lock age anyway
1802 * as a new lock is a valuable resource even if
1803 * it has a low weight.
1805 * That is, for shrinker policy we drop only
1806 * old locks, but additionally choose them by
1807 * their weight. Big extent locks will stay in
1809 result = pf(ns, lock, unused, added, count);
1810 if (result == LDLM_POLICY_KEEP_LOCK) {
1811 lu_ref_del(&lock->l_reference,
1812 __FUNCTION__, current);
1813 LDLM_LOCK_RELEASE(lock);
1814 spin_lock(&ns->ns_lock);
1817 if (result == LDLM_POLICY_SKIP_LOCK) {
1818 lu_ref_del(&lock->l_reference,
1820 LDLM_LOCK_RELEASE(lock);
1821 spin_lock(&ns->ns_lock);
1825 lock_res_and_lock(lock);
1826 /* Check flags again under the lock. */
1827 if (ldlm_is_canceling(lock) ||
1828 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1829 /* Another thread is removing lock from LRU, or
1830 * somebody is already doing CANCEL, or there
1831 * is a blocking request which will send cancel
1832 * by itself, or the lock is no longer unused or
1833 * the lock has been used since the pf() call and
1834 * pages could be put under it. */
1835 unlock_res_and_lock(lock);
1836 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1837 LDLM_LOCK_RELEASE(lock);
1838 spin_lock(&ns->ns_lock);
1841 LASSERT(!lock->l_readers && !lock->l_writers);
1843 /* If we have chosen to cancel this lock voluntarily, we
1844 * better send cancel notification to server, so that it
1845 * frees appropriate state. This might lead to a race
1846 * where while we are doing cancel here, server is also
1847 * silently cancelling this lock. */
1848 ldlm_clear_cancel_on_block(lock);
1850 /* Setting the CBPENDING flag is a little misleading,
1851 * but prevents an important race; namely, once
1852 * CBPENDING is set, the lock can accumulate no more
1853 * readers/writers. Since readers and writers are
1854 * already zero here, ldlm_lock_decref() won't see
1855 * this flag and call l_blocking_ast */
1856 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1858 if ((lru_flags & LDLM_LRU_FLAG_CLEANUP) &&
1859 (lock->l_resource->lr_type == LDLM_EXTENT ||
1860 ldlm_has_dom(lock)) && lock->l_granted_mode == LCK_PR)
1861 ldlm_set_discard_data(lock);
1863 /* We can't re-add to l_lru as it confuses the
1864 * refcounting in ldlm_lock_remove_from_lru() if an AST
1865 * arrives after we drop lr_lock below. We use l_bl_ast
1866 * and can't use l_pending_chain as it is used both on
1867 * server and client nevertheless bug 5666 says it is
1868 * used only on server */
1869 LASSERT(list_empty(&lock->l_bl_ast));
1870 list_add(&lock->l_bl_ast, cancels);
1871 unlock_res_and_lock(lock);
1872 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1873 spin_lock(&ns->ns_lock);
1877 spin_unlock(&ns->ns_lock);
1881 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1883 enum ldlm_cancel_flags cancel_flags,
1884 enum ldlm_lru_flags lru_flags)
1888 added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
1892 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1896 * Cancel at least \a nr locks from given namespace LRU.
1898 * When called with LCF_ASYNC the blocking callback will be handled
1899 * in a thread and this function will return after the thread has been
1900 * asked to call the callback. When called with LCF_ASYNC the blocking
1901 * callback will be performed in this function.
1903 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
1904 enum ldlm_cancel_flags cancel_flags,
1905 enum ldlm_lru_flags lru_flags)
1907 struct list_head cancels = LIST_HEAD_INIT(cancels);
1911 /* Just prepare the list of locks, do not actually cancel them yet.
1912 * Locks are cancelled later in a separate thread. */
1913 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
1914 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
1922 * Find and cancel locally unused locks found on resource, matched to the
1923 * given policy, mode. GET the found locks and add them into the \a cancels
1926 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1927 struct list_head *cancels,
1928 union ldlm_policy_data *policy,
1929 enum ldlm_mode mode, __u64 lock_flags,
1930 enum ldlm_cancel_flags cancel_flags,
1933 struct ldlm_lock *lock;
1938 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1939 if (opaque != NULL && lock->l_ast_data != opaque) {
1940 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1941 lock->l_ast_data, opaque);
1946 if (lock->l_readers || lock->l_writers)
1949 /* If somebody is already doing CANCEL, or blocking AST came,
1950 * skip this lock. */
1951 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
1954 if (lockmode_compat(lock->l_granted_mode, mode))
1957 /* If policy is given and this is IBITS lock, add to list only
1958 * those locks that match by policy. */
1959 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1960 !(lock->l_policy_data.l_inodebits.bits &
1961 policy->l_inodebits.bits))
1964 /* See CBPENDING comment in ldlm_cancel_lru */
1965 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1968 LASSERT(list_empty(&lock->l_bl_ast));
1969 list_add(&lock->l_bl_ast, cancels);
1970 LDLM_LOCK_GET(lock);
1975 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1977 EXPORT_SYMBOL(ldlm_cancel_resource_local);
1980 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
1982 * If \a req is NULL, send CANCEL request to server with handles of locks
1983 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1984 * separately per lock.
1985 * If \a req is not NULL, put handles of locks in \a cancels into the request
1986 * buffer at the offset \a off.
1987 * Destroy \a cancels at the end.
1989 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1990 struct ptlrpc_request *req,
1991 enum ldlm_cancel_flags flags)
1993 struct ldlm_lock *lock;
1997 if (list_empty(cancels) || count == 0)
2000 /* XXX: requests (both batched and not) could be sent in parallel.
2001 * Usually it is enough to have just 1 RPC, but it is possible that
2002 * there are too many locks to be cancelled in LRU or on a resource.
2003 * It would also speed up the case when the server does not support
2006 LASSERT(!list_empty(cancels));
2007 lock = list_entry(cancels->next, struct ldlm_lock,
2009 LASSERT(lock->l_conn_export);
2011 if (exp_connect_cancelset(lock->l_conn_export)) {
2014 ldlm_cancel_pack(req, cancels, count);
2016 res = ldlm_cli_cancel_req(lock->l_conn_export,
2020 res = ldlm_cli_cancel_req(lock->l_conn_export,
2025 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
2026 "ldlm_cli_cancel_list: %d\n", res);
2031 ldlm_lock_list_put(cancels, l_bl_ast, res);
2033 LASSERT(count == 0);
2036 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2039 * Cancel all locks on a resource that have 0 readers/writers.
2041 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
2042 * to notify the server. */
2043 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
2044 const struct ldlm_res_id *res_id,
2045 union ldlm_policy_data *policy,
2046 enum ldlm_mode mode,
2047 enum ldlm_cancel_flags flags, void *opaque)
2049 struct ldlm_resource *res;
2050 struct list_head cancels = LIST_HEAD_INIT(cancels);
2055 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2057 /* This is not a problem. */
2058 CDEBUG(D_INFO, "No resource %llu\n", res_id->name[0]);
2062 LDLM_RESOURCE_ADDREF(res);
2063 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
2064 0, flags | LCF_BL_AST, opaque);
2065 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
2067 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
2070 LDLM_RESOURCE_DELREF(res);
2071 ldlm_resource_putref(res);
2074 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2076 struct ldlm_cli_cancel_arg {
2082 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2083 struct hlist_node *hnode, void *arg)
2085 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2086 struct ldlm_cli_cancel_arg *lc = arg;
2088 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2089 NULL, LCK_MINMODE, lc->lc_flags,
2091 /* must return 0 for hash iteration */
2096 * Cancel all locks on a namespace (or a specific resource, if given)
2097 * that have 0 readers/writers.
2099 * If flags & LCF_LOCAL, throw the locks away without trying
2100 * to notify the server. */
2101 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2102 const struct ldlm_res_id *res_id,
2103 enum ldlm_cancel_flags flags, void *opaque)
2105 struct ldlm_cli_cancel_arg arg = {
2107 .lc_opaque = opaque,
2115 if (res_id != NULL) {
2116 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2120 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2121 ldlm_cli_hash_cancel_unused, &arg, 0);
2126 /* Lock iterators. */
2128 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2131 struct list_head *tmp, *next;
2132 struct ldlm_lock *lock;
2133 int rc = LDLM_ITER_CONTINUE;
2138 RETURN(LDLM_ITER_CONTINUE);
2141 list_for_each_safe(tmp, next, &res->lr_granted) {
2142 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2144 if (iter(lock, closure) == LDLM_ITER_STOP)
2145 GOTO(out, rc = LDLM_ITER_STOP);
2148 list_for_each_safe(tmp, next, &res->lr_converting) {
2149 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2151 if (iter(lock, closure) == LDLM_ITER_STOP)
2152 GOTO(out, rc = LDLM_ITER_STOP);
2155 list_for_each_safe(tmp, next, &res->lr_waiting) {
2156 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2158 if (iter(lock, closure) == LDLM_ITER_STOP)
2159 GOTO(out, rc = LDLM_ITER_STOP);
2166 struct iter_helper_data {
2167 ldlm_iterator_t iter;
2171 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2173 struct iter_helper_data *helper = closure;
2174 return helper->iter(lock, helper->closure);
2177 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2178 struct hlist_node *hnode, void *arg)
2181 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2183 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2187 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2188 ldlm_iterator_t iter, void *closure)
2191 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2193 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2194 ldlm_res_iter_helper, &helper, 0);
2198 /* non-blocking function to manipulate a lock whose cb_data is being put away.
2199 * return 0: find no resource
2200 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2203 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2204 const struct ldlm_res_id *res_id,
2205 ldlm_iterator_t iter, void *data)
2207 struct ldlm_resource *res;
2211 LASSERTF(ns != NULL, "must pass in namespace\n");
2213 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2217 LDLM_RESOURCE_ADDREF(res);
2218 rc = ldlm_resource_foreach(res, iter, data);
2219 LDLM_RESOURCE_DELREF(res);
2220 ldlm_resource_putref(res);
2223 EXPORT_SYMBOL(ldlm_resource_iterate);
2227 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2229 struct list_head *list = closure;
2231 /* we use l_pending_chain here, because it's unused on clients. */
2232 LASSERTF(list_empty(&lock->l_pending_chain),
2233 "lock %p next %p prev %p\n",
2234 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2235 /* bug 9573: don't replay locks left after eviction, or
2236 * bug 17614: locks being actively cancelled. Get a reference
2237 * on a lock so that it does not disapear under us (e.g. due to cancel)
2239 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_BL_DONE))) {
2240 list_add(&lock->l_pending_chain, list);
2241 LDLM_LOCK_GET(lock);
2244 return LDLM_ITER_CONTINUE;
2247 static int replay_lock_interpret(const struct lu_env *env,
2248 struct ptlrpc_request *req,
2249 struct ldlm_async_args *aa, int rc)
2251 struct ldlm_lock *lock;
2252 struct ldlm_reply *reply;
2253 struct obd_export *exp;
2256 atomic_dec(&req->rq_import->imp_replay_inflight);
2260 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2262 GOTO(out, rc = -EPROTO);
2264 lock = ldlm_handle2lock(&aa->lock_handle);
2266 CERROR("received replay ack for unknown local cookie %#llx"
2267 " remote cookie %#llx from server %s id %s\n",
2268 aa->lock_handle.cookie, reply->lock_handle.cookie,
2269 req->rq_export->exp_client_uuid.uuid,
2270 libcfs_id2str(req->rq_peer));
2271 GOTO(out, rc = -ESTALE);
2274 /* Key change rehash lock in per-export hash with new key */
2275 exp = req->rq_export;
2276 if (exp && exp->exp_lock_hash) {
2277 /* In the function below, .hs_keycmp resolves to
2278 * ldlm_export_lock_keycmp() */
2279 /* coverity[overrun-buffer-val] */
2280 cfs_hash_rehash_key(exp->exp_lock_hash,
2281 &lock->l_remote_handle,
2282 &reply->lock_handle,
2285 lock->l_remote_handle = reply->lock_handle;
2288 LDLM_DEBUG(lock, "replayed lock:");
2289 ptlrpc_import_recovery_state_machine(req->rq_import);
2290 LDLM_LOCK_PUT(lock);
2293 ptlrpc_connect_import(req->rq_import);
2298 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2300 struct ptlrpc_request *req;
2301 struct ldlm_async_args *aa;
2302 struct ldlm_request *body;
2307 /* Bug 11974: Do not replay a lock which is actively being canceled */
2308 if (ldlm_is_bl_done(lock)) {
2309 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2313 /* If this is reply-less callback lock, we cannot replay it, since
2314 * server might have long dropped it, but notification of that event was
2315 * lost by network. (and server granted conflicting lock already) */
2316 if (ldlm_is_cancel_on_block(lock)) {
2317 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2318 ldlm_lock_cancel(lock);
2323 * If granted mode matches the requested mode, this lock is granted.
2325 * If they differ, but we have a granted mode, then we were granted
2326 * one mode and now want another: ergo, converting.
2328 * If we haven't been granted anything and are on a resource list,
2329 * then we're blocked/waiting.
2331 * If we haven't been granted anything and we're NOT on a resource list,
2332 * then we haven't got a reply yet and don't have a known disposition.
2333 * This happens whenever a lock enqueue is the request that triggers
2336 if (lock->l_granted_mode == lock->l_req_mode)
2337 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2338 else if (lock->l_granted_mode)
2339 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2340 else if (!list_empty(&lock->l_res_link))
2341 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2343 flags = LDLM_FL_REPLAY;
2345 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2346 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2350 /* We're part of recovery, so don't wait for it. */
2351 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2353 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2354 ldlm_lock2desc(lock, &body->lock_desc);
2355 body->lock_flags = ldlm_flags_to_wire(flags);
2357 ldlm_lock2handle(lock, &body->lock_handle[0]);
2358 if (lock->l_lvb_len > 0)
2359 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2360 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2362 ptlrpc_request_set_replen(req);
2363 /* notify the server we've replayed all requests.
2364 * also, we mark the request to be put on a dedicated
2365 * queue to be processed after all request replayes.
2367 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2369 LDLM_DEBUG(lock, "replaying lock:");
2371 atomic_inc(&req->rq_import->imp_replay_inflight);
2372 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2373 aa = ptlrpc_req_async_args(req);
2374 aa->lock_handle = body->lock_handle[0];
2375 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2376 ptlrpcd_add_req(req);
2382 * Cancel as many unused locks as possible before replay. since we are
2383 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2386 * Called only in recovery before replaying locks. there is no need to
2387 * replay locks that are unused. since the clients may hold thousands of
2388 * cached unused locks, dropping the unused locks can greatly reduce the
2389 * load on the servers at recovery time.
2391 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2394 struct list_head cancels = LIST_HEAD_INIT(cancels);
2396 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2397 "replay for namespace %s (%d)\n",
2398 ldlm_ns_name(ns), ns->ns_nr_unused);
2400 /* We don't need to care whether or not LRU resize is enabled
2401 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2402 * count parameter */
2403 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2404 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2406 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2407 canceled, ldlm_ns_name(ns));
2410 int ldlm_replay_locks(struct obd_import *imp)
2412 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2413 struct list_head list = LIST_HEAD_INIT(list);
2414 struct ldlm_lock *lock, *next;
2419 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2421 /* don't replay locks if import failed recovery */
2422 if (imp->imp_vbr_failed)
2425 /* ensure this doesn't fall to 0 before all have been queued */
2426 atomic_inc(&imp->imp_replay_inflight);
2428 if (ldlm_cancel_unused_locks_before_replay)
2429 ldlm_cancel_unused_locks_for_replay(ns);
2431 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2433 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2434 list_del_init(&lock->l_pending_chain);
2436 LDLM_LOCK_RELEASE(lock);
2437 continue; /* or try to do the rest? */
2439 rc = replay_one_lock(imp, lock);
2440 LDLM_LOCK_RELEASE(lock);
2443 atomic_dec(&imp->imp_replay_inflight);