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_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);
472 ldlm_lock2handle(lock, lockh);
474 /* NB: we don't have any lock now (lock_res_and_lock)
475 * because it's a new lock */
476 ldlm_lock_addref_internal_nolock(lock, mode);
477 ldlm_set_local(lock);
478 if (*flags & LDLM_FL_ATOMIC_CB)
479 ldlm_set_atomic_cb(lock);
482 lock->l_policy_data = *policy;
483 if (client_cookie != NULL)
484 lock->l_client_cookie = *client_cookie;
485 if (type == LDLM_EXTENT) {
486 /* extent lock without policy is a bug */
490 lock->l_req_extent = policy->l_extent;
493 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
494 if (unlikely(err != ELDLM_OK))
498 *policy = lock->l_policy_data;
500 if (lock->l_completion_ast)
501 lock->l_completion_ast(lock, *flags, NULL);
503 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
506 LDLM_LOCK_RELEASE(lock);
510 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
512 static void failed_lock_cleanup(struct ldlm_namespace *ns,
513 struct ldlm_lock *lock, int mode)
517 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
518 lock_res_and_lock(lock);
519 /* Check that lock is not granted or failed, we might race. */
520 if ((lock->l_req_mode != lock->l_granted_mode) &&
521 !ldlm_is_failed(lock)) {
522 /* Make sure that this lock will not be found by raced
523 * bl_ast and -EINVAL reply is sent to server anyways.
525 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
526 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
529 unlock_res_and_lock(lock);
533 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
534 "LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
536 LDLM_DEBUG(lock, "lock was granted or failed in race");
538 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
539 * from llite/file.c/ll_file_flock(). */
540 /* This code makes for the fact that we do not have blocking handler on
541 * a client for flock locks. As such this is the place where we must
542 * completely kill failed locks. (interrupted and those that
543 * were waiting to be granted when server evicted us. */
544 if (lock->l_resource->lr_type == LDLM_FLOCK) {
545 lock_res_and_lock(lock);
546 if (!ldlm_is_destroyed(lock)) {
547 ldlm_resource_unlink_lock(lock);
548 ldlm_lock_decref_internal_nolock(lock, mode);
549 ldlm_lock_destroy_nolock(lock);
551 unlock_res_and_lock(lock);
553 ldlm_lock_decref_internal(lock, mode);
558 * Finishing portion of client lock enqueue code.
560 * Called after receiving reply from server.
562 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
563 enum ldlm_type type, __u8 with_policy,
564 enum ldlm_mode mode, __u64 *flags, void *lvb,
565 __u32 lvb_len, const struct lustre_handle *lockh,
568 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
569 int is_replay = *flags & LDLM_FL_REPLAY;
570 struct ldlm_lock *lock;
571 struct ldlm_reply *reply;
572 int cleanup_phase = 1;
575 lock = ldlm_handle2lock(lockh);
576 /* ldlm_cli_enqueue is holding a reference on this lock. */
578 LASSERT(type == LDLM_FLOCK);
582 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
583 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
585 if (rc != ELDLM_OK) {
587 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
588 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
590 if (rc != ELDLM_LOCK_ABORTED)
594 /* Before we return, swab the reply */
595 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
597 GOTO(cleanup, rc = -EPROTO);
602 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
605 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
606 GOTO(cleanup, rc = size);
607 } else if (unlikely(size > lvb_len)) {
608 LDLM_ERROR(lock, "Replied LVB is larger than "
609 "expectation, expected = %d, replied = %d",
611 GOTO(cleanup, rc = -EINVAL);
616 if (rc == ELDLM_LOCK_ABORTED) {
617 if (lvb_len > 0 && lvb != NULL)
618 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
620 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
623 /* lock enqueued on the server */
626 lock_res_and_lock(lock);
627 /* Key change rehash lock in per-export hash with new key */
628 if (exp->exp_lock_hash) {
629 /* In the function below, .hs_keycmp resolves to
630 * ldlm_export_lock_keycmp() */
631 /* coverity[overrun-buffer-val] */
632 cfs_hash_rehash_key(exp->exp_lock_hash,
633 &lock->l_remote_handle,
637 lock->l_remote_handle = reply->lock_handle;
640 *flags = ldlm_flags_from_wire(reply->lock_flags);
641 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
642 LDLM_FL_INHERIT_MASK);
643 unlock_res_and_lock(lock);
645 CDEBUG(D_INFO, "local: %p, remote cookie: %#llx, flags: %#llx\n",
646 lock, reply->lock_handle.cookie, *flags);
648 /* If enqueue returned a blocked lock but the completion handler has
649 * already run, then it fixed up the resource and we don't need to do it
651 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
652 int newmode = reply->lock_desc.l_req_mode;
654 if (newmode && newmode != lock->l_req_mode) {
655 LDLM_DEBUG(lock, "server returned different mode %s",
656 ldlm_lockname[newmode]);
657 lock->l_req_mode = newmode;
660 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
661 &lock->l_resource->lr_name)) {
662 CDEBUG(D_INFO, "remote intent success, locking "DLDLMRES
663 " instead of "DLDLMRES"\n",
664 PLDLMRES(&reply->lock_desc.l_resource),
665 PLDLMRES(lock->l_resource));
667 rc = ldlm_lock_change_resource(ns, lock,
668 &reply->lock_desc.l_resource.lr_name);
669 if (rc || lock->l_resource == NULL)
670 GOTO(cleanup, rc = -ENOMEM);
671 LDLM_DEBUG(lock, "client-side enqueue, new resource");
674 if (!(type == LDLM_IBITS &&
675 !(exp_connect_flags(exp) & OBD_CONNECT_IBITS)))
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);
681 if (type != LDLM_PLAIN)
682 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
685 if ((*flags) & LDLM_FL_AST_SENT) {
686 lock_res_and_lock(lock);
687 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
688 unlock_res_and_lock(lock);
689 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
692 /* If the lock has already been granted by a completion AST, don't
693 * clobber the LVB with an older one. */
695 /* We must lock or a racing completion might update lvb without
696 * letting us know and we'll clobber the correct value.
697 * Cannot unlock after the check either, a that still leaves
698 * a tiny window for completion to get in */
699 lock_res_and_lock(lock);
700 if (lock->l_req_mode != lock->l_granted_mode)
701 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
702 lock->l_lvb_data, lvb_len);
703 unlock_res_and_lock(lock);
711 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
712 if (lock->l_completion_ast != NULL) {
713 int err = lock->l_completion_ast(lock, *flags, NULL);
721 if (lvb_len > 0 && lvb != NULL) {
722 /* Copy the LVB here, and not earlier, because the completion
723 * AST (if any) can override what we got in the reply */
724 memcpy(lvb, lock->l_lvb_data, lvb_len);
727 LDLM_DEBUG(lock, "client-side enqueue END");
730 if (cleanup_phase == 1 && rc)
731 failed_lock_cleanup(ns, lock, mode);
732 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
734 LDLM_LOCK_RELEASE(lock);
737 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
740 * Estimate number of lock handles that would fit into request of given
741 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
742 * a single page on the send/receive side. XXX: 512 should be changed to
743 * more adequate value.
745 static inline int ldlm_req_handles_avail(int req_size, int off)
749 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
750 if (likely(avail >= 0))
751 avail /= (int)sizeof(struct lustre_handle);
754 avail += LDLM_LOCKREQ_HANDLES - off;
759 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
760 enum req_location loc,
763 __u32 size = req_capsule_msg_size(pill, loc);
764 return ldlm_req_handles_avail(size, off);
767 static inline int ldlm_format_handles_avail(struct obd_import *imp,
768 const struct req_format *fmt,
769 enum req_location loc, int off)
771 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
772 return ldlm_req_handles_avail(size, off);
776 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
777 * \a count locks in \a cancels.
779 * This is to be called by functions preparing their own requests that
780 * might contain lists of locks to cancel in addition to actual operation
781 * that needs to be performed.
783 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
784 int version, int opc, int canceloff,
785 struct list_head *cancels, int count)
787 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
788 struct req_capsule *pill = &req->rq_pill;
789 struct ldlm_request *dlm = NULL;
790 struct list_head head = LIST_HEAD_INIT(head);
791 enum ldlm_lru_flags lru_flags;
792 int avail, to_free, pack = 0;
798 if (ns_connect_cancelset(ns)) {
799 /* Estimate the amount of available space in the request. */
800 req_capsule_filled_sizes(pill, RCL_CLIENT);
801 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
803 lru_flags = LDLM_LRU_FLAG_NO_WAIT | (ns_connect_lru_resize(ns) ?
804 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED);
805 to_free = !ns_connect_lru_resize(ns) &&
806 opc == LDLM_ENQUEUE ? 1 : 0;
808 /* Cancel LRU locks here _only_ if the server supports
809 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
810 * RPC, which will make us slower. */
812 count += ldlm_cancel_lru_local(ns, cancels, to_free,
819 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
820 ldlm_request_bufsize(pack, opc));
823 rc = ptlrpc_request_pack(req, version, opc);
825 ldlm_lock_list_put(cancels, l_bl_ast, count);
829 if (ns_connect_cancelset(ns)) {
831 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
833 /* Skip first lock handler in ldlm_request_pack(),
834 * this method will increment @lock_count according
835 * to the lock handle amount actually written to
837 dlm->lock_count = canceloff;
839 /* Pack into the request @pack lock handles. */
840 ldlm_cli_cancel_list(cancels, pack, req, 0);
841 /* Prepare and send separate cancel RPC for others. */
842 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
844 ldlm_lock_list_put(cancels, l_bl_ast, count);
848 EXPORT_SYMBOL(ldlm_prep_elc_req);
850 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
851 struct list_head *cancels, int count)
853 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
854 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
856 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
858 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
860 struct ptlrpc_request *req;
864 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
866 RETURN(ERR_PTR(-ENOMEM));
868 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
870 ptlrpc_request_free(req);
874 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
875 ptlrpc_request_set_replen(req);
878 EXPORT_SYMBOL(ldlm_enqueue_pack);
881 * Client-side lock enqueue.
883 * If a request has some specific initialisation it is passed in \a reqp,
884 * otherwise it is created in ldlm_cli_enqueue.
886 * Supports sync and async requests, pass \a async flag accordingly. If a
887 * request was created in ldlm_cli_enqueue and it is the async request,
888 * pass it to the caller in \a reqp.
890 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
891 struct ldlm_enqueue_info *einfo,
892 const struct ldlm_res_id *res_id,
893 union ldlm_policy_data const *policy, __u64 *flags,
894 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
895 struct lustre_handle *lockh, int async)
897 struct ldlm_namespace *ns;
898 struct ldlm_lock *lock;
899 struct ldlm_request *body;
900 int is_replay = *flags & LDLM_FL_REPLAY;
901 int req_passed_in = 1;
903 struct ptlrpc_request *req;
906 LASSERT(exp != NULL);
908 ns = exp->exp_obd->obd_namespace;
910 /* If we're replaying this lock, just check some invariants.
911 * If we're creating a new lock, get everything all setup nice. */
913 lock = ldlm_handle2lock_long(lockh, 0);
914 LASSERT(lock != NULL);
915 LDLM_DEBUG(lock, "client-side enqueue START");
916 LASSERT(exp == lock->l_conn_export);
918 const struct ldlm_callback_suite cbs = {
919 .lcs_completion = einfo->ei_cb_cp,
920 .lcs_blocking = einfo->ei_cb_bl,
921 .lcs_glimpse = einfo->ei_cb_gl
923 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
924 einfo->ei_mode, &cbs, einfo->ei_cbdata,
927 RETURN(PTR_ERR(lock));
928 /* for the local lock, add the reference */
929 ldlm_lock_addref_internal(lock, einfo->ei_mode);
930 ldlm_lock2handle(lock, lockh);
932 lock->l_policy_data = *policy;
934 if (einfo->ei_type == LDLM_EXTENT) {
935 /* extent lock without policy is a bug */
939 lock->l_req_extent = policy->l_extent;
941 LDLM_DEBUG(lock, "client-side enqueue START, flags %#llx",
945 lock->l_conn_export = exp;
946 lock->l_export = NULL;
947 lock->l_blocking_ast = einfo->ei_cb_bl;
948 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL));
949 lock->l_last_activity = ktime_get_real_seconds();
951 /* lock not sent to server yet */
952 if (reqp == NULL || *reqp == NULL) {
953 req = ldlm_enqueue_pack(exp, lvb_len);
955 failed_lock_cleanup(ns, lock, einfo->ei_mode);
956 LDLM_LOCK_RELEASE(lock);
957 RETURN(PTR_ERR(req));
967 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
969 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
970 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
973 /* Dump lock data into the request buffer */
974 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
975 ldlm_lock2desc(lock, &body->lock_desc);
976 body->lock_flags = ldlm_flags_to_wire(*flags);
977 body->lock_handle[0] = *lockh;
979 /* extended LDLM opcodes in client stats */
980 if (exp->exp_obd->obd_svc_stats != NULL) {
981 bool glimpse = *flags & LDLM_FL_HAS_INTENT;
983 /* OST glimpse has no intent buffer */
984 if (req_capsule_has_field(&req->rq_pill, &RMF_LDLM_INTENT,
986 struct ldlm_intent *it;
988 it = req_capsule_client_get(&req->rq_pill,
990 glimpse = (it && (it->opc == IT_GLIMPSE));
994 ldlm_svc_get_eopc(body, exp->exp_obd->obd_svc_stats);
996 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
998 LDLM_GLIMPSE_ENQUEUE);
1002 LASSERT(reqp != NULL);
1006 LDLM_DEBUG(lock, "sending request");
1008 rc = ptlrpc_queue_wait(req);
1010 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
1011 einfo->ei_mode, flags, lvb, lvb_len,
1014 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
1015 * one reference that we took */
1017 LDLM_LOCK_RELEASE(lock);
1021 if (!req_passed_in && req != NULL) {
1022 ptlrpc_req_finished(req);
1029 EXPORT_SYMBOL(ldlm_cli_enqueue);
1031 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
1034 struct ldlm_resource *res;
1037 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1038 CERROR("Trying to cancel local lock\n");
1041 LDLM_DEBUG(lock, "client-side local convert");
1043 res = ldlm_lock_convert(lock, new_mode, flags);
1045 ldlm_reprocess_all(res);
1048 rc = LUSTRE_EDEADLK;
1050 LDLM_DEBUG(lock, "client-side local convert handler END");
1051 LDLM_LOCK_PUT(lock);
1055 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
1056 * conversion of locks which are on the waiting or converting queue */
1057 /* Caller of this code is supposed to take care of lock readers/writers
1059 int ldlm_cli_convert(const struct lustre_handle *lockh, int new_mode,
1062 struct ldlm_request *body;
1063 struct ldlm_reply *reply;
1064 struct ldlm_lock *lock;
1065 struct ldlm_resource *res;
1066 struct ptlrpc_request *req;
1070 lock = ldlm_handle2lock(lockh);
1077 if (lock->l_conn_export == NULL)
1078 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
1080 LDLM_DEBUG(lock, "client-side convert");
1082 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
1083 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1086 LDLM_LOCK_PUT(lock);
1090 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1091 body->lock_handle[0] = lock->l_remote_handle;
1093 body->lock_desc.l_req_mode = new_mode;
1094 body->lock_flags = ldlm_flags_to_wire(*flags);
1097 ptlrpc_request_set_replen(req);
1098 rc = ptlrpc_queue_wait(req);
1102 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1104 GOTO(out, rc = -EPROTO);
1107 GOTO(out, rc = req->rq_status);
1109 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
1111 ldlm_reprocess_all(res);
1112 /* Go to sleep until the lock is granted. */
1113 /* FIXME: or cancelled. */
1114 if (lock->l_completion_ast) {
1115 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1121 rc = LUSTRE_EDEADLK;
1125 LDLM_LOCK_PUT(lock);
1126 ptlrpc_req_finished(req);
1131 * Cancel locks locally.
1133 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1134 * \retval LDLM_FL_CANCELING otherwise;
1135 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1137 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1139 __u64 rc = LDLM_FL_LOCAL_ONLY;
1142 if (lock->l_conn_export) {
1145 LDLM_DEBUG(lock, "client-side cancel");
1146 /* Set this flag to prevent others from getting new references*/
1147 lock_res_and_lock(lock);
1148 ldlm_set_cbpending(lock);
1149 local_only = !!(lock->l_flags &
1150 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1151 ldlm_cancel_callback(lock);
1152 rc = (ldlm_is_bl_ast(lock)) ?
1153 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1154 unlock_res_and_lock(lock);
1157 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1159 rc = LDLM_FL_LOCAL_ONLY;
1161 ldlm_lock_cancel(lock);
1163 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1164 LDLM_ERROR(lock, "Trying to cancel local lock");
1167 LDLM_DEBUG(lock, "server-side local cancel");
1168 ldlm_lock_cancel(lock);
1169 ldlm_reprocess_all(lock->l_resource);
1176 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1178 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1179 struct list_head *head, int count)
1181 struct ldlm_request *dlm;
1182 struct ldlm_lock *lock;
1183 int max, packed = 0;
1186 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1187 LASSERT(dlm != NULL);
1189 /* Check the room in the request buffer. */
1190 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1191 sizeof(struct ldlm_request);
1192 max /= sizeof(struct lustre_handle);
1193 max += LDLM_LOCKREQ_HANDLES;
1194 LASSERT(max >= dlm->lock_count + count);
1196 /* XXX: it would be better to pack lock handles grouped by resource.
1197 * so that the server cancel would call filter_lvbo_update() less
1199 list_for_each_entry(lock, head, l_bl_ast) {
1202 LASSERT(lock->l_conn_export);
1203 /* Pack the lock handle to the given request buffer. */
1204 LDLM_DEBUG(lock, "packing");
1205 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1208 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1213 * Prepare and send a batched cancel RPC. It will include \a count lock
1214 * handles of locks given in \a cancels list. */
1215 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1216 int count, enum ldlm_cancel_flags flags)
1218 struct ptlrpc_request *req = NULL;
1219 struct obd_import *imp;
1224 LASSERT(exp != NULL);
1227 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1229 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1232 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1233 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1238 imp = class_exp2cliimp(exp);
1239 if (imp == NULL || imp->imp_invalid) {
1241 "skipping cancel on invalid import %p\n", imp);
1245 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1247 GOTO(out, rc = -ENOMEM);
1249 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1250 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1251 ldlm_request_bufsize(count, LDLM_CANCEL));
1253 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1255 ptlrpc_request_free(req);
1259 /* If OSP want cancel cross-MDT lock, let's not block it in
1260 * in recovery, otherwise the lock will not released, if
1261 * the remote target is also in recovery, and it also need
1262 * this lock, it might cause deadlock. */
1263 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1264 exp->exp_obd->obd_lu_dev != NULL &&
1265 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1266 struct lu_device *top_dev;
1268 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1269 if (top_dev != NULL &&
1270 top_dev->ld_obd->obd_recovering)
1271 req->rq_allow_replay = 1;
1274 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1275 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1276 ptlrpc_at_set_req_timeout(req);
1278 ldlm_cancel_pack(req, cancels, count);
1280 ptlrpc_request_set_replen(req);
1281 if (flags & LCF_ASYNC) {
1282 ptlrpcd_add_req(req);
1287 rc = ptlrpc_queue_wait(req);
1288 if (rc == LUSTRE_ESTALE) {
1289 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1290 "out of sync -- not fatal\n",
1291 libcfs_nid2str(req->rq_import->
1292 imp_connection->c_peer.nid));
1294 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1295 req->rq_import_generation == imp->imp_generation) {
1296 ptlrpc_req_finished(req);
1298 } else if (rc != ELDLM_OK) {
1299 /* -ESHUTDOWN is common on umount */
1300 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1301 "Got rc %d from cancel RPC: "
1302 "canceling anyway\n", rc);
1309 ptlrpc_req_finished(req);
1312 return sent ? sent : rc;
1315 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1317 LASSERT(imp != NULL);
1318 return &imp->imp_obd->obd_namespace->ns_pool;
1322 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1324 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1326 struct obd_device *obd;
1330 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1331 !imp_connect_lru_resize(req->rq_import)))
1334 * Do nothing for corner cases.
1339 /* In some cases RPC may contain SLV and limit zeroed out. This
1340 * is the case when server does not support LRU resize feature.
1341 * This is also possible in some recovery cases when server-side
1342 * reqs have no reference to the OBD export and thus access to
1343 * server-side namespace is not possible. */
1344 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1345 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1346 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1347 "(SLV: %llu, Limit: %u)",
1348 lustre_msg_get_slv(req->rq_repmsg),
1349 lustre_msg_get_limit(req->rq_repmsg));
1353 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1354 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1355 obd = req->rq_import->imp_obd;
1357 /* Set new SLV and limit in OBD fields to make them accessible
1358 * to the pool thread. We do not access obd_namespace and pool
1359 * directly here as there is no reliable way to make sure that
1360 * they are still alive at cleanup time. Evil races are possible
1361 * which may cause Oops at that time. */
1362 write_lock(&obd->obd_pool_lock);
1363 obd->obd_pool_slv = new_slv;
1364 obd->obd_pool_limit = new_limit;
1365 write_unlock(&obd->obd_pool_lock);
1371 * Client side lock cancel.
1373 * Lock must not have any readers or writers by this time.
1375 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1376 enum ldlm_cancel_flags cancel_flags)
1378 struct obd_export *exp;
1379 enum ldlm_lru_flags lru_flags;
1380 int avail, count = 1;
1382 struct ldlm_namespace *ns;
1383 struct ldlm_lock *lock;
1384 struct list_head cancels = LIST_HEAD_INIT(cancels);
1387 lock = ldlm_handle2lock_long(lockh, 0);
1389 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1393 lock_res_and_lock(lock);
1394 /* Lock is being canceled and the caller doesn't want to wait */
1395 if (ldlm_is_canceling(lock)) {
1396 if (cancel_flags & LCF_ASYNC) {
1397 unlock_res_and_lock(lock);
1399 struct l_wait_info lwi = { 0 };
1401 unlock_res_and_lock(lock);
1402 l_wait_event(lock->l_waitq, is_bl_done(lock), &lwi);
1404 LDLM_LOCK_RELEASE(lock);
1408 ldlm_set_canceling(lock);
1409 unlock_res_and_lock(lock);
1411 rc = ldlm_cli_cancel_local(lock);
1412 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1413 LDLM_LOCK_RELEASE(lock);
1416 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1417 * RPC which goes to canceld portal, so we can cancel other LRU locks
1418 * here and send them all as one LDLM_CANCEL RPC. */
1419 LASSERT(list_empty(&lock->l_bl_ast));
1420 list_add(&lock->l_bl_ast, &cancels);
1422 exp = lock->l_conn_export;
1423 if (exp_connect_cancelset(exp)) {
1424 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1429 ns = ldlm_lock_to_ns(lock);
1430 lru_flags = ns_connect_lru_resize(ns) ?
1431 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1432 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1433 LCF_BL_AST, lru_flags);
1435 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1438 EXPORT_SYMBOL(ldlm_cli_cancel);
1441 * Locally cancel up to \a count locks in list \a cancels.
1442 * Return the number of cancelled locks.
1444 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1445 enum ldlm_cancel_flags cancel_flags)
1447 struct list_head head = LIST_HEAD_INIT(head);
1448 struct ldlm_lock *lock, *next;
1449 int left = 0, bl_ast = 0;
1453 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1457 if (cancel_flags & LCF_LOCAL) {
1458 rc = LDLM_FL_LOCAL_ONLY;
1459 ldlm_lock_cancel(lock);
1461 rc = ldlm_cli_cancel_local(lock);
1463 /* Until we have compound requests and can send LDLM_CANCEL
1464 * requests batched with generic RPCs, we need to send cancels
1465 * with the LDLM_FL_BL_AST flag in a separate RPC from
1466 * the one being generated now. */
1467 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1468 LDLM_DEBUG(lock, "Cancel lock separately");
1469 list_del_init(&lock->l_bl_ast);
1470 list_add(&lock->l_bl_ast, &head);
1474 if (rc == LDLM_FL_LOCAL_ONLY) {
1475 /* CANCEL RPC should not be sent to server. */
1476 list_del_init(&lock->l_bl_ast);
1477 LDLM_LOCK_RELEASE(lock);
1483 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1490 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1491 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1492 * readahead requests, ...)
1494 static enum ldlm_policy_res
1495 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1496 int unused, int added, int count)
1498 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1500 /* don't check added & count since we want to process all locks
1502 * It's fine to not take lock to access lock->l_resource since
1503 * the lock has already been granted so it won't change. */
1504 switch (lock->l_resource->lr_type) {
1507 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1510 result = LDLM_POLICY_SKIP_LOCK;
1511 lock_res_and_lock(lock);
1512 ldlm_set_skipped(lock);
1513 unlock_res_and_lock(lock);
1521 * Callback function for LRU-resize policy. Decides whether to keep
1522 * \a lock in LRU for current \a LRU size \a unused, added in current
1523 * scan \a added and number of locks to be preferably canceled \a count.
1525 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1527 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1529 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1530 struct ldlm_lock *lock,
1531 int unused, int added,
1534 ktime_t cur = ktime_get();
1535 struct ldlm_pool *pl = &ns->ns_pool;
1539 /* Stop LRU processing when we reach past @count or have checked all
1541 if (count && added >= count)
1542 return LDLM_POLICY_KEEP_LOCK;
1544 /* Despite of the LV, It doesn't make sense to keep the lock which
1545 * is unused for ns_max_age time.
1547 if (ktime_after(ktime_get(),
1548 ktime_add(lock->l_last_used, ns->ns_max_age)))
1549 return LDLM_POLICY_CANCEL_LOCK;
1551 slv = ldlm_pool_get_slv(pl);
1552 lvf = ldlm_pool_get_lvf(pl);
1553 la = ktime_to_ns(ktime_sub(cur, lock->l_last_used)) / NSEC_PER_SEC;
1554 lv = lvf * la * unused;
1556 /* Inform pool about current CLV to see it via proc. */
1557 ldlm_pool_set_clv(pl, lv);
1559 /* Stop when SLV is not yet come from server or lv is smaller than
1561 if (slv == 0 || lv < slv)
1562 return LDLM_POLICY_KEEP_LOCK;
1564 return LDLM_POLICY_CANCEL_LOCK;
1567 static enum ldlm_policy_res
1568 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1569 struct ldlm_lock *lock,
1570 int unused, int added,
1573 enum ldlm_policy_res result;
1575 result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1576 if (result == LDLM_POLICY_KEEP_LOCK)
1579 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1583 * Callback function for proc used policy. Makes decision whether to keep
1584 * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1585 * added and number of locks to be preferably canceled \a count.
1587 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1589 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1591 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1592 struct ldlm_lock *lock,
1593 int unused, int added,
1596 /* Stop LRU processing when we reach past @count or have checked all
1598 return (added >= count) ?
1599 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1603 * Callback function for aged policy. Makes decision whether to keep \a lock in
1604 * LRU for current LRU size \a unused, added in current scan \a added and
1605 * number of locks to be preferably canceled \a count.
1607 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1609 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1611 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1612 struct ldlm_lock *lock,
1613 int unused, int added,
1616 if ((added >= count) &&
1617 ktime_before(ktime_get(),
1618 ktime_add(lock->l_last_used, ns->ns_max_age)))
1619 return LDLM_POLICY_KEEP_LOCK;
1621 return LDLM_POLICY_CANCEL_LOCK;
1624 static enum ldlm_policy_res
1625 ldlm_cancel_aged_no_wait_policy(struct ldlm_namespace *ns,
1626 struct ldlm_lock *lock,
1627 int unused, int added, int count)
1629 enum ldlm_policy_res result;
1631 result = ldlm_cancel_aged_policy(ns, lock, unused, added, count);
1632 if (result == LDLM_POLICY_KEEP_LOCK)
1635 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1639 * Callback function for default policy. Makes decision whether to keep \a lock
1640 * in LRU for current LRU size \a unused, added in current scan \a added and
1641 * number of locks to be preferably canceled \a count.
1643 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1645 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1648 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1649 struct ldlm_lock *lock,
1650 int unused, int added,
1653 /* Stop LRU processing when we reach past count or have checked all
1655 return (added >= count) ?
1656 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1659 typedef enum ldlm_policy_res
1660 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1661 int unused, int added, int count);
1663 static ldlm_cancel_lru_policy_t
1664 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1666 if (ns_connect_lru_resize(ns)) {
1667 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1668 /* We kill passed number of old locks. */
1669 return ldlm_cancel_passed_policy;
1670 if (lru_flags & LDLM_LRU_FLAG_LRUR) {
1671 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1672 return ldlm_cancel_lrur_no_wait_policy;
1674 return ldlm_cancel_lrur_policy;
1676 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1677 return ldlm_cancel_passed_policy;
1679 if (lru_flags & LDLM_LRU_FLAG_AGED) {
1680 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1681 return ldlm_cancel_aged_no_wait_policy;
1683 return ldlm_cancel_aged_policy;
1686 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1687 return ldlm_cancel_no_wait_policy;
1689 return ldlm_cancel_default_policy;
1693 * - Free space in LRU for \a count new locks,
1694 * redundant unused locks are canceled locally;
1695 * - also cancel locally unused aged locks;
1696 * - do not cancel more than \a max locks;
1697 * - GET the found locks and add them into the \a cancels list.
1699 * A client lock can be added to the l_bl_ast list only when it is
1700 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1701 * CANCEL. There are the following use cases:
1702 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1703 * ldlm_cli_cancel(), which check and set this flag properly. As any
1704 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1705 * later without any special locking.
1707 * Calling policies for enabled LRU resize:
1708 * ----------------------------------------
1709 * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1710 * cancel not more than \a count locks;
1712 * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1713 * at the beginning of LRU list);
1715 * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1716 * to memory pressre policy function;
1718 * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1720 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1721 * (typically before replaying locks) w/o
1722 * sending any RPCs or waiting for any
1723 * outstanding RPC to complete.
1725 * flags & LDLM_CANCEL_CLEANUP - when cancelling read locks, do not check for
1726 * other read locks covering the same pages, just
1727 * discard those pages.
1729 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1730 struct list_head *cancels, int count, int max,
1731 enum ldlm_lru_flags lru_flags)
1733 ldlm_cancel_lru_policy_t pf;
1734 struct ldlm_lock *lock, *next;
1735 int added = 0, unused, remained;
1736 int no_wait = lru_flags & LDLM_LRU_FLAG_NO_WAIT;
1739 spin_lock(&ns->ns_lock);
1740 unused = ns->ns_nr_unused;
1743 if (!ns_connect_lru_resize(ns))
1744 count += unused - ns->ns_max_unused;
1746 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1747 LASSERT(pf != NULL);
1749 while (!list_empty(&ns->ns_unused_list)) {
1750 enum ldlm_policy_res result;
1751 ktime_t last_use = ktime_set(0, 0);
1753 /* all unused locks */
1754 if (remained-- <= 0)
1757 /* For any flags, stop scanning if @max is reached. */
1758 if (max && added >= max)
1761 list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1763 /* No locks which got blocking requests. */
1764 LASSERT(!ldlm_is_bl_ast(lock));
1766 if (no_wait && ldlm_is_skipped(lock))
1767 /* already processed */
1770 last_use = lock->l_last_used;
1772 /* Somebody is already doing CANCEL. No need for this
1773 * lock in LRU, do not traverse it again. */
1774 if (!ldlm_is_canceling(lock))
1777 ldlm_lock_remove_from_lru_nolock(lock);
1779 if (&lock->l_lru == &ns->ns_unused_list)
1782 LDLM_LOCK_GET(lock);
1783 spin_unlock(&ns->ns_lock);
1784 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1786 /* Pass the lock through the policy filter and see if it
1787 * should stay in LRU.
1789 * Even for shrinker policy we stop scanning if
1790 * we find a lock that should stay in the cache.
1791 * We should take into account lock age anyway
1792 * as a new lock is a valuable resource even if
1793 * it has a low weight.
1795 * That is, for shrinker policy we drop only
1796 * old locks, but additionally choose them by
1797 * their weight. Big extent locks will stay in
1799 result = pf(ns, lock, unused, added, count);
1800 if (result == LDLM_POLICY_KEEP_LOCK) {
1801 lu_ref_del(&lock->l_reference,
1802 __FUNCTION__, current);
1803 LDLM_LOCK_RELEASE(lock);
1804 spin_lock(&ns->ns_lock);
1807 if (result == LDLM_POLICY_SKIP_LOCK) {
1808 lu_ref_del(&lock->l_reference,
1810 LDLM_LOCK_RELEASE(lock);
1811 spin_lock(&ns->ns_lock);
1815 lock_res_and_lock(lock);
1816 /* Check flags again under the lock. */
1817 if (ldlm_is_canceling(lock) ||
1818 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1819 /* Another thread is removing lock from LRU, or
1820 * somebody is already doing CANCEL, or there
1821 * is a blocking request which will send cancel
1822 * by itself, or the lock is no longer unused or
1823 * the lock has been used since the pf() call and
1824 * pages could be put under it. */
1825 unlock_res_and_lock(lock);
1826 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1827 LDLM_LOCK_RELEASE(lock);
1828 spin_lock(&ns->ns_lock);
1831 LASSERT(!lock->l_readers && !lock->l_writers);
1833 /* If we have chosen to cancel this lock voluntarily, we
1834 * better send cancel notification to server, so that it
1835 * frees appropriate state. This might lead to a race
1836 * where while we are doing cancel here, server is also
1837 * silently cancelling this lock. */
1838 ldlm_clear_cancel_on_block(lock);
1840 /* Setting the CBPENDING flag is a little misleading,
1841 * but prevents an important race; namely, once
1842 * CBPENDING is set, the lock can accumulate no more
1843 * readers/writers. Since readers and writers are
1844 * already zero here, ldlm_lock_decref() won't see
1845 * this flag and call l_blocking_ast */
1846 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1848 if ((lru_flags & LDLM_LRU_FLAG_CLEANUP) &&
1849 (lock->l_resource->lr_type == LDLM_EXTENT ||
1850 ldlm_has_dom(lock)) && lock->l_granted_mode == LCK_PR)
1851 ldlm_set_discard_data(lock);
1853 /* We can't re-add to l_lru as it confuses the
1854 * refcounting in ldlm_lock_remove_from_lru() if an AST
1855 * arrives after we drop lr_lock below. We use l_bl_ast
1856 * and can't use l_pending_chain as it is used both on
1857 * server and client nevertheless bug 5666 says it is
1858 * used only on server */
1859 LASSERT(list_empty(&lock->l_bl_ast));
1860 list_add(&lock->l_bl_ast, cancels);
1861 unlock_res_and_lock(lock);
1862 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1863 spin_lock(&ns->ns_lock);
1867 spin_unlock(&ns->ns_lock);
1871 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1873 enum ldlm_cancel_flags cancel_flags,
1874 enum ldlm_lru_flags lru_flags)
1878 added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
1882 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1886 * Cancel at least \a nr locks from given namespace LRU.
1888 * When called with LCF_ASYNC the blocking callback will be handled
1889 * in a thread and this function will return after the thread has been
1890 * asked to call the callback. When called with LCF_ASYNC the blocking
1891 * callback will be performed in this function.
1893 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
1894 enum ldlm_cancel_flags cancel_flags,
1895 enum ldlm_lru_flags lru_flags)
1897 struct list_head cancels = LIST_HEAD_INIT(cancels);
1901 /* Just prepare the list of locks, do not actually cancel them yet.
1902 * Locks are cancelled later in a separate thread. */
1903 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
1904 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
1912 * Find and cancel locally unused locks found on resource, matched to the
1913 * given policy, mode. GET the found locks and add them into the \a cancels
1916 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1917 struct list_head *cancels,
1918 union ldlm_policy_data *policy,
1919 enum ldlm_mode mode, __u64 lock_flags,
1920 enum ldlm_cancel_flags cancel_flags,
1923 struct ldlm_lock *lock;
1928 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1929 if (opaque != NULL && lock->l_ast_data != opaque) {
1930 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1931 lock->l_ast_data, opaque);
1936 if (lock->l_readers || lock->l_writers)
1939 /* If somebody is already doing CANCEL, or blocking AST came,
1940 * skip this lock. */
1941 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
1944 if (lockmode_compat(lock->l_granted_mode, mode))
1947 /* If policy is given and this is IBITS lock, add to list only
1948 * those locks that match by policy. */
1949 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1950 !(lock->l_policy_data.l_inodebits.bits &
1951 policy->l_inodebits.bits))
1954 /* See CBPENDING comment in ldlm_cancel_lru */
1955 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1958 LASSERT(list_empty(&lock->l_bl_ast));
1959 list_add(&lock->l_bl_ast, cancels);
1960 LDLM_LOCK_GET(lock);
1965 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1967 EXPORT_SYMBOL(ldlm_cancel_resource_local);
1970 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
1972 * If \a req is NULL, send CANCEL request to server with handles of locks
1973 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1974 * separately per lock.
1975 * If \a req is not NULL, put handles of locks in \a cancels into the request
1976 * buffer at the offset \a off.
1977 * Destroy \a cancels at the end.
1979 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1980 struct ptlrpc_request *req,
1981 enum ldlm_cancel_flags flags)
1983 struct ldlm_lock *lock;
1987 if (list_empty(cancels) || count == 0)
1990 /* XXX: requests (both batched and not) could be sent in parallel.
1991 * Usually it is enough to have just 1 RPC, but it is possible that
1992 * there are too many locks to be cancelled in LRU or on a resource.
1993 * It would also speed up the case when the server does not support
1996 LASSERT(!list_empty(cancels));
1997 lock = list_entry(cancels->next, struct ldlm_lock,
1999 LASSERT(lock->l_conn_export);
2001 if (exp_connect_cancelset(lock->l_conn_export)) {
2004 ldlm_cancel_pack(req, cancels, count);
2006 res = ldlm_cli_cancel_req(lock->l_conn_export,
2010 res = ldlm_cli_cancel_req(lock->l_conn_export,
2015 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
2016 "ldlm_cli_cancel_list: %d\n", res);
2021 ldlm_lock_list_put(cancels, l_bl_ast, res);
2023 LASSERT(count == 0);
2026 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2029 * Cancel all locks on a resource that have 0 readers/writers.
2031 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
2032 * to notify the server. */
2033 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
2034 const struct ldlm_res_id *res_id,
2035 union ldlm_policy_data *policy,
2036 enum ldlm_mode mode,
2037 enum ldlm_cancel_flags flags, void *opaque)
2039 struct ldlm_resource *res;
2040 struct list_head cancels = LIST_HEAD_INIT(cancels);
2045 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2047 /* This is not a problem. */
2048 CDEBUG(D_INFO, "No resource %llu\n", res_id->name[0]);
2052 LDLM_RESOURCE_ADDREF(res);
2053 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
2054 0, flags | LCF_BL_AST, opaque);
2055 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
2057 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
2060 LDLM_RESOURCE_DELREF(res);
2061 ldlm_resource_putref(res);
2064 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2066 struct ldlm_cli_cancel_arg {
2072 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2073 struct hlist_node *hnode, void *arg)
2075 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2076 struct ldlm_cli_cancel_arg *lc = arg;
2078 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2079 NULL, LCK_MINMODE, lc->lc_flags,
2081 /* must return 0 for hash iteration */
2086 * Cancel all locks on a namespace (or a specific resource, if given)
2087 * that have 0 readers/writers.
2089 * If flags & LCF_LOCAL, throw the locks away without trying
2090 * to notify the server. */
2091 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2092 const struct ldlm_res_id *res_id,
2093 enum ldlm_cancel_flags flags, void *opaque)
2095 struct ldlm_cli_cancel_arg arg = {
2097 .lc_opaque = opaque,
2105 if (res_id != NULL) {
2106 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2110 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2111 ldlm_cli_hash_cancel_unused, &arg, 0);
2116 /* Lock iterators. */
2118 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2121 struct list_head *tmp, *next;
2122 struct ldlm_lock *lock;
2123 int rc = LDLM_ITER_CONTINUE;
2128 RETURN(LDLM_ITER_CONTINUE);
2131 list_for_each_safe(tmp, next, &res->lr_granted) {
2132 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2134 if (iter(lock, closure) == LDLM_ITER_STOP)
2135 GOTO(out, rc = LDLM_ITER_STOP);
2138 list_for_each_safe(tmp, next, &res->lr_converting) {
2139 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2141 if (iter(lock, closure) == LDLM_ITER_STOP)
2142 GOTO(out, rc = LDLM_ITER_STOP);
2145 list_for_each_safe(tmp, next, &res->lr_waiting) {
2146 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2148 if (iter(lock, closure) == LDLM_ITER_STOP)
2149 GOTO(out, rc = LDLM_ITER_STOP);
2156 struct iter_helper_data {
2157 ldlm_iterator_t iter;
2161 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2163 struct iter_helper_data *helper = closure;
2164 return helper->iter(lock, helper->closure);
2167 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2168 struct hlist_node *hnode, void *arg)
2171 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2173 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2177 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2178 ldlm_iterator_t iter, void *closure)
2181 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2183 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2184 ldlm_res_iter_helper, &helper, 0);
2188 /* non-blocking function to manipulate a lock whose cb_data is being put away.
2189 * return 0: find no resource
2190 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2193 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2194 const struct ldlm_res_id *res_id,
2195 ldlm_iterator_t iter, void *data)
2197 struct ldlm_resource *res;
2201 LASSERTF(ns != NULL, "must pass in namespace\n");
2203 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2207 LDLM_RESOURCE_ADDREF(res);
2208 rc = ldlm_resource_foreach(res, iter, data);
2209 LDLM_RESOURCE_DELREF(res);
2210 ldlm_resource_putref(res);
2213 EXPORT_SYMBOL(ldlm_resource_iterate);
2217 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2219 struct list_head *list = closure;
2221 /* we use l_pending_chain here, because it's unused on clients. */
2222 LASSERTF(list_empty(&lock->l_pending_chain),
2223 "lock %p next %p prev %p\n",
2224 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2225 /* bug 9573: don't replay locks left after eviction, or
2226 * bug 17614: locks being actively cancelled. Get a reference
2227 * on a lock so that it does not disapear under us (e.g. due to cancel)
2229 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_BL_DONE))) {
2230 list_add(&lock->l_pending_chain, list);
2231 LDLM_LOCK_GET(lock);
2234 return LDLM_ITER_CONTINUE;
2237 static int replay_lock_interpret(const struct lu_env *env,
2238 struct ptlrpc_request *req,
2239 struct ldlm_async_args *aa, int rc)
2241 struct ldlm_lock *lock;
2242 struct ldlm_reply *reply;
2243 struct obd_export *exp;
2246 atomic_dec(&req->rq_import->imp_replay_inflight);
2250 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2252 GOTO(out, rc = -EPROTO);
2254 lock = ldlm_handle2lock(&aa->lock_handle);
2256 CERROR("received replay ack for unknown local cookie %#llx"
2257 " remote cookie %#llx from server %s id %s\n",
2258 aa->lock_handle.cookie, reply->lock_handle.cookie,
2259 req->rq_export->exp_client_uuid.uuid,
2260 libcfs_id2str(req->rq_peer));
2261 GOTO(out, rc = -ESTALE);
2264 /* Key change rehash lock in per-export hash with new key */
2265 exp = req->rq_export;
2266 if (exp && exp->exp_lock_hash) {
2267 /* In the function below, .hs_keycmp resolves to
2268 * ldlm_export_lock_keycmp() */
2269 /* coverity[overrun-buffer-val] */
2270 cfs_hash_rehash_key(exp->exp_lock_hash,
2271 &lock->l_remote_handle,
2272 &reply->lock_handle,
2275 lock->l_remote_handle = reply->lock_handle;
2278 LDLM_DEBUG(lock, "replayed lock:");
2279 ptlrpc_import_recovery_state_machine(req->rq_import);
2280 LDLM_LOCK_PUT(lock);
2283 ptlrpc_connect_import(req->rq_import);
2288 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2290 struct ptlrpc_request *req;
2291 struct ldlm_async_args *aa;
2292 struct ldlm_request *body;
2297 /* Bug 11974: Do not replay a lock which is actively being canceled */
2298 if (ldlm_is_bl_done(lock)) {
2299 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2303 /* If this is reply-less callback lock, we cannot replay it, since
2304 * server might have long dropped it, but notification of that event was
2305 * lost by network. (and server granted conflicting lock already) */
2306 if (ldlm_is_cancel_on_block(lock)) {
2307 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2308 ldlm_lock_cancel(lock);
2313 * If granted mode matches the requested mode, this lock is granted.
2315 * If they differ, but we have a granted mode, then we were granted
2316 * one mode and now want another: ergo, converting.
2318 * If we haven't been granted anything and are on a resource list,
2319 * then we're blocked/waiting.
2321 * If we haven't been granted anything and we're NOT on a resource list,
2322 * then we haven't got a reply yet and don't have a known disposition.
2323 * This happens whenever a lock enqueue is the request that triggers
2326 if (lock->l_granted_mode == lock->l_req_mode)
2327 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2328 else if (lock->l_granted_mode)
2329 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2330 else if (!list_empty(&lock->l_res_link))
2331 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2333 flags = LDLM_FL_REPLAY;
2335 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2336 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2340 /* We're part of recovery, so don't wait for it. */
2341 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2343 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2344 ldlm_lock2desc(lock, &body->lock_desc);
2345 body->lock_flags = ldlm_flags_to_wire(flags);
2347 ldlm_lock2handle(lock, &body->lock_handle[0]);
2348 if (lock->l_lvb_len > 0)
2349 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2350 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2352 ptlrpc_request_set_replen(req);
2353 /* notify the server we've replayed all requests.
2354 * also, we mark the request to be put on a dedicated
2355 * queue to be processed after all request replayes.
2357 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2359 LDLM_DEBUG(lock, "replaying lock:");
2361 atomic_inc(&req->rq_import->imp_replay_inflight);
2362 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2363 aa = ptlrpc_req_async_args(req);
2364 aa->lock_handle = body->lock_handle[0];
2365 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2366 ptlrpcd_add_req(req);
2372 * Cancel as many unused locks as possible before replay. since we are
2373 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2376 * Called only in recovery before replaying locks. there is no need to
2377 * replay locks that are unused. since the clients may hold thousands of
2378 * cached unused locks, dropping the unused locks can greatly reduce the
2379 * load on the servers at recovery time.
2381 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2384 struct list_head cancels = LIST_HEAD_INIT(cancels);
2386 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2387 "replay for namespace %s (%d)\n",
2388 ldlm_ns_name(ns), ns->ns_nr_unused);
2390 /* We don't need to care whether or not LRU resize is enabled
2391 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2392 * count parameter */
2393 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2394 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2396 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2397 canceled, ldlm_ns_name(ns));
2400 int ldlm_replay_locks(struct obd_import *imp)
2402 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2403 struct list_head list = LIST_HEAD_INIT(list);
2404 struct ldlm_lock *lock, *next;
2409 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2411 /* don't replay locks if import failed recovery */
2412 if (imp->imp_vbr_failed)
2415 /* ensure this doesn't fall to 0 before all have been queued */
2416 atomic_inc(&imp->imp_replay_inflight);
2418 if (ldlm_cancel_unused_locks_before_replay)
2419 ldlm_cancel_unused_locks_for_replay(ns);
2421 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2423 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2424 list_del_init(&lock->l_pending_chain);
2426 LDLM_LOCK_RELEASE(lock);
2427 continue; /* or try to do the rest? */
2429 rc = replay_one_lock(imp, lock);
2430 LDLM_LOCK_RELEASE(lock);
2433 atomic_dec(&imp->imp_replay_inflight);