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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2015, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 * This file contains Asynchronous System Trap (AST) handlers and related
38 * LDLM request-processing routines.
40 * An AST is a callback issued on a lock when its state is changed. There are
41 * several different types of ASTs (callbacks) registered for each lock:
43 * - completion AST: when a lock is enqueued by some process, but cannot be
44 * granted immediately due to other conflicting locks on the same resource,
45 * the completion AST is sent to notify the caller when the lock is
48 * - blocking AST: when a lock is granted to some process, if another process
49 * enqueues a conflicting (blocking) lock on a resource, a blocking AST is
50 * sent to notify the holder(s) of the lock(s) of the conflicting lock
51 * request. The lock holder(s) must release their lock(s) on that resource in
52 * a timely manner or be evicted by the server.
54 * - glimpse AST: this is used when a process wants information about a lock
55 * (i.e. the lock value block (LVB)) but does not necessarily require holding
56 * the lock. If the resource is locked, the lock holder(s) are sent glimpse
57 * ASTs and the LVB is returned to the caller, and lock holder(s) may CANCEL
58 * their lock(s) if they are idle. If the resource is not locked, the server
62 #define DEBUG_SUBSYSTEM S_LDLM
64 #include <lustre_dlm.h>
65 #include <obd_class.h>
68 #include "ldlm_internal.h"
70 unsigned int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
71 module_param(ldlm_enqueue_min, uint, 0644);
72 MODULE_PARM_DESC(ldlm_enqueue_min, "lock enqueue timeout minimum");
74 /* in client side, whether the cached locks will be canceled before replay */
75 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
77 static void interrupted_completion_wait(void *data)
81 struct lock_wait_data {
82 struct ldlm_lock *lwd_lock;
86 struct ldlm_async_args {
87 struct lustre_handle lock_handle;
90 int ldlm_expired_completion_wait(void *data)
92 struct lock_wait_data *lwd = data;
93 struct ldlm_lock *lock = lwd->lwd_lock;
94 struct obd_import *imp;
95 struct obd_device *obd;
98 if (lock->l_conn_export == NULL) {
99 static cfs_time_t next_dump = 0, last_dump = 0;
101 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
102 CFS_DURATION_T"s ago); not entering recovery in "
103 "server code, just going back to sleep",
104 lock->l_last_activity,
105 cfs_time_sub(cfs_time_current_sec(),
106 lock->l_last_activity));
107 if (cfs_time_after(cfs_time_current(), next_dump)) {
108 last_dump = next_dump;
109 next_dump = cfs_time_shift(300);
110 ldlm_namespace_dump(D_DLMTRACE,
111 ldlm_lock_to_ns(lock));
113 libcfs_debug_dumplog();
118 obd = lock->l_conn_export->exp_obd;
119 imp = obd->u.cli.cl_import;
120 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
121 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
122 CFS_DURATION_T"s ago), entering recovery for %s@%s",
123 lock->l_last_activity,
124 cfs_time_sub(cfs_time_current_sec(), lock->l_last_activity),
125 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
131 * Calculate the Completion timeout (covering enqueue, BL AST, data flush,
132 * lock cancel, and their replies). Used for lock completion timeout on the
135 * \param[in] lock lock which is waiting the completion callback
137 * \retval timeout in seconds to wait for the server reply
140 /* We use the same basis for both server side and client side functions
141 from a single node. */
142 static unsigned int ldlm_cp_timeout(struct ldlm_lock *lock)
144 unsigned int timeout;
149 /* Wait a long time for enqueue - server may have to callback a
150 * lock from another client. Server will evict the other client if it
151 * doesn't respond reasonably, and then give us the lock. */
152 timeout = at_get(ldlm_lock_to_ns_at(lock));
153 return max(3 * timeout, ldlm_enqueue_min);
157 * Helper function for ldlm_completion_ast(), updating timings when lock is
160 static int ldlm_completion_tail(struct ldlm_lock *lock, void *data)
165 if (ldlm_is_destroyed(lock) || ldlm_is_failed(lock)) {
166 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
168 } else if (data == NULL) {
169 LDLM_DEBUG(lock, "client-side enqueue: granted");
171 /* Take into AT only CP RPC, not immediately granted locks */
172 delay = cfs_time_sub(cfs_time_current_sec(),
173 lock->l_last_activity);
174 LDLM_DEBUG(lock, "client-side enqueue: granted after "
175 CFS_DURATION_T"s", delay);
177 /* Update our time estimate */
178 at_measured(ldlm_lock_to_ns_at(lock), delay);
184 * Implementation of ->l_completion_ast() for a client, that doesn't wait
185 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
186 * other threads that cannot block for long.
188 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data)
192 if (flags == LDLM_FL_WAIT_NOREPROC) {
193 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
197 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
198 wake_up(&lock->l_waitq);
199 RETURN(ldlm_completion_tail(lock, data));
202 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
204 ldlm_reprocess_all(lock->l_resource);
207 EXPORT_SYMBOL(ldlm_completion_ast_async);
210 * Generic LDLM "completion" AST. This is called in several cases:
212 * - when a reply to an ENQUEUE RPC is received from the server
213 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
214 * this point (determined by flags);
216 * - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
219 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
222 * - to force all locks when resource is destroyed (cleanup_resource());
224 * - during lock conversion (not used currently).
226 * If lock is not granted in the first case, this function waits until second
227 * or penultimate cases happen in some other thread.
230 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
232 /* XXX ALLOCATE - 160 bytes */
233 struct lock_wait_data lwd;
234 struct obd_device *obd;
235 struct obd_import *imp = NULL;
236 struct l_wait_info lwi;
241 if (flags == LDLM_FL_WAIT_NOREPROC) {
242 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
246 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
247 wake_up(&lock->l_waitq);
251 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
256 obd = class_exp2obd(lock->l_conn_export);
258 /* if this is a local lock, then there is no import */
260 imp = obd->u.cli.cl_import;
263 timeout = ldlm_cp_timeout(lock);
266 lock->l_last_activity = cfs_time_current_sec();
268 if (ldlm_is_no_timeout(lock)) {
269 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
270 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
272 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
273 ldlm_expired_completion_wait,
274 interrupted_completion_wait, &lwd);
278 spin_lock(&imp->imp_lock);
279 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
280 spin_unlock(&imp->imp_lock);
283 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
284 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
285 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
286 ldlm_set_fail_loc(lock);
289 /* Go to sleep until the lock is granted or cancelled. */
290 rc = l_wait_event(lock->l_waitq,
291 is_granted_or_cancelled(lock), &lwi);
295 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
300 RETURN(ldlm_completion_tail(lock, data));
302 EXPORT_SYMBOL(ldlm_completion_ast);
305 * A helper to build a blocking AST function
307 * Perform a common operation for blocking ASTs:
308 * defferred lock cancellation.
310 * \param lock the lock blocking or canceling AST was called on
312 * \see mdt_blocking_ast
313 * \see ldlm_blocking_ast
315 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
320 ldlm_set_cbpending(lock);
321 do_ast = (!lock->l_readers && !lock->l_writers);
322 unlock_res_and_lock(lock);
325 struct lustre_handle lockh;
328 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
329 ldlm_lock2handle(lock, &lockh);
330 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
332 CERROR("ldlm_cli_cancel: %d\n", rc);
334 LDLM_DEBUG(lock, "Lock still has references, will be "
339 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
342 * Server blocking AST
344 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
347 * \param lock the lock which blocks a request or cancelling lock
350 * \param flag indicates whether this cancelling or blocking callback
352 * \see ldlm_blocking_ast_nocheck
354 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
355 void *data, int flag)
359 if (flag == LDLM_CB_CANCELING) {
360 /* Don't need to do anything here. */
364 lock_res_and_lock(lock);
365 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
366 * that ldlm_blocking_ast is called just before intent_policy method
367 * takes the lr_lock, then by the time we get the lock, we might not
368 * be the correct blocking function anymore. So check, and return
370 if (lock->l_blocking_ast != ldlm_blocking_ast) {
371 unlock_res_and_lock(lock);
374 RETURN(ldlm_blocking_ast_nocheck(lock));
376 EXPORT_SYMBOL(ldlm_blocking_ast);
379 * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
381 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
382 * rather subtle: with OST-side locking, it may so happen that _all_ extent
383 * locks are held by the OST. If client wants to obtain the current file size
384 * it calls ll_glimpse_size(), and (as all locks are held only on the server),
385 * this dummy glimpse callback fires and does nothing. The client still
386 * receives the correct file size due to the following fragment of code in
387 * ldlm_cb_interpret():
389 * if (rc == -ELDLM_NO_LOCK_DATA) {
390 * LDLM_DEBUG(lock, "lost race - client has a lock but no"
392 * ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
395 * That is, after the glimpse returns this error, ofd_lvbo_update() is called
396 * and returns the updated file attributes from the inode to the client.
398 * See also comment in ofd_intent_policy() on why servers must set a non-NULL
399 * l_glimpse_ast when grabbing DLM locks. Otherwise, the server will assume
400 * that the object is in the process of being destroyed.
402 * \param[in] lock DLM lock being glimpsed, unused
403 * \param[in] reqp pointer to ptlrpc_request, unused
405 * \retval -ELDLM_NO_LOCK_DATA to get attributes from disk object
407 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
409 return -ELDLM_NO_LOCK_DATA;
413 * Enqueue a local lock (typically on a server).
415 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
416 const struct ldlm_res_id *res_id,
417 enum ldlm_type type, union ldlm_policy_data *policy,
418 enum ldlm_mode mode, __u64 *flags,
419 ldlm_blocking_callback blocking,
420 ldlm_completion_callback completion,
421 ldlm_glimpse_callback glimpse,
422 void *data, __u32 lvb_len, enum lvb_type lvb_type,
423 const __u64 *client_cookie,
424 struct lustre_handle *lockh)
426 struct ldlm_lock *lock;
428 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
429 .lcs_blocking = blocking,
430 .lcs_glimpse = glimpse,
434 LASSERT(!(*flags & LDLM_FL_REPLAY));
435 if (unlikely(ns_is_client(ns))) {
436 CERROR("Trying to enqueue local lock in a shadow namespace\n");
440 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
443 GOTO(out_nolock, err = PTR_ERR(lock));
445 err = ldlm_lvbo_init(lock->l_resource);
447 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
451 ldlm_lock2handle(lock, lockh);
453 /* NB: we don't have any lock now (lock_res_and_lock)
454 * because it's a new lock */
455 ldlm_lock_addref_internal_nolock(lock, mode);
456 ldlm_set_local(lock);
457 if (*flags & LDLM_FL_ATOMIC_CB)
458 ldlm_set_atomic_cb(lock);
461 lock->l_policy_data = *policy;
462 if (client_cookie != NULL)
463 lock->l_client_cookie = *client_cookie;
464 if (type == LDLM_EXTENT) {
465 /* extent lock without policy is a bug */
469 lock->l_req_extent = policy->l_extent;
472 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
473 if (unlikely(err != ELDLM_OK))
477 *policy = lock->l_policy_data;
479 if (lock->l_completion_ast)
480 lock->l_completion_ast(lock, *flags, NULL);
482 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
485 LDLM_LOCK_RELEASE(lock);
489 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
491 static void failed_lock_cleanup(struct ldlm_namespace *ns,
492 struct ldlm_lock *lock, int mode)
496 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
497 lock_res_and_lock(lock);
498 /* Check that lock is not granted or failed, we might race. */
499 if ((lock->l_req_mode != lock->l_granted_mode) &&
500 !ldlm_is_failed(lock)) {
501 /* Make sure that this lock will not be found by raced
502 * bl_ast and -EINVAL reply is sent to server anyways.
504 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
505 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
508 unlock_res_and_lock(lock);
512 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
513 "LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
515 LDLM_DEBUG(lock, "lock was granted or failed in race");
517 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
518 * from llite/file.c/ll_file_flock(). */
519 /* This code makes for the fact that we do not have blocking handler on
520 * a client for flock locks. As such this is the place where we must
521 * completely kill failed locks. (interrupted and those that
522 * were waiting to be granted when server evicted us. */
523 if (lock->l_resource->lr_type == LDLM_FLOCK) {
524 lock_res_and_lock(lock);
525 if (!ldlm_is_destroyed(lock)) {
526 ldlm_resource_unlink_lock(lock);
527 ldlm_lock_decref_internal_nolock(lock, mode);
528 ldlm_lock_destroy_nolock(lock);
530 unlock_res_and_lock(lock);
532 ldlm_lock_decref_internal(lock, mode);
537 * Finishing portion of client lock enqueue code.
539 * Called after receiving reply from server.
541 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
542 enum ldlm_type type, __u8 with_policy,
543 enum ldlm_mode mode, __u64 *flags, void *lvb,
544 __u32 lvb_len, const struct lustre_handle *lockh,
547 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
548 int is_replay = *flags & LDLM_FL_REPLAY;
549 struct ldlm_lock *lock;
550 struct ldlm_reply *reply;
551 int cleanup_phase = 1;
554 lock = ldlm_handle2lock(lockh);
555 /* ldlm_cli_enqueue is holding a reference on this lock. */
557 LASSERT(type == LDLM_FLOCK);
561 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
562 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
564 if (rc != ELDLM_OK) {
566 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
567 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
569 if (rc != ELDLM_LOCK_ABORTED)
573 /* Before we return, swab the reply */
574 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
576 GOTO(cleanup, rc = -EPROTO);
581 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
584 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
585 GOTO(cleanup, rc = size);
586 } else if (unlikely(size > lvb_len)) {
587 LDLM_ERROR(lock, "Replied LVB is larger than "
588 "expectation, expected = %d, replied = %d",
590 GOTO(cleanup, rc = -EINVAL);
595 if (rc == ELDLM_LOCK_ABORTED) {
596 if (lvb_len > 0 && lvb != NULL)
597 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
599 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
602 /* lock enqueued on the server */
605 lock_res_and_lock(lock);
606 /* Key change rehash lock in per-export hash with new key */
607 if (exp->exp_lock_hash) {
608 /* In the function below, .hs_keycmp resolves to
609 * ldlm_export_lock_keycmp() */
610 /* coverity[overrun-buffer-val] */
611 cfs_hash_rehash_key(exp->exp_lock_hash,
612 &lock->l_remote_handle,
616 lock->l_remote_handle = reply->lock_handle;
619 *flags = ldlm_flags_from_wire(reply->lock_flags);
620 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
621 LDLM_FL_INHERIT_MASK);
622 unlock_res_and_lock(lock);
624 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: "LPX64"\n",
625 lock, reply->lock_handle.cookie, *flags);
627 /* If enqueue returned a blocked lock but the completion handler has
628 * already run, then it fixed up the resource and we don't need to do it
630 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
631 int newmode = reply->lock_desc.l_req_mode;
633 if (newmode && newmode != lock->l_req_mode) {
634 LDLM_DEBUG(lock, "server returned different mode %s",
635 ldlm_lockname[newmode]);
636 lock->l_req_mode = newmode;
639 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
640 &lock->l_resource->lr_name)) {
641 CDEBUG(D_INFO, "remote intent success, locking "DLDLMRES
642 " instead of "DLDLMRES"\n",
643 PLDLMRES(&reply->lock_desc.l_resource),
644 PLDLMRES(lock->l_resource));
646 rc = ldlm_lock_change_resource(ns, lock,
647 &reply->lock_desc.l_resource.lr_name);
648 if (rc || lock->l_resource == NULL)
649 GOTO(cleanup, rc = -ENOMEM);
650 LDLM_DEBUG(lock, "client-side enqueue, new resource");
653 if (!(type == LDLM_IBITS &&
654 !(exp_connect_flags(exp) & OBD_CONNECT_IBITS)))
655 /* We assume lock type cannot change on server*/
656 ldlm_convert_policy_to_local(exp,
657 lock->l_resource->lr_type,
658 &reply->lock_desc.l_policy_data,
659 &lock->l_policy_data);
660 if (type != LDLM_PLAIN)
661 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
664 if ((*flags) & LDLM_FL_AST_SENT) {
665 lock_res_and_lock(lock);
666 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
667 unlock_res_and_lock(lock);
668 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
671 /* If the lock has already been granted by a completion AST, don't
672 * clobber the LVB with an older one. */
674 /* We must lock or a racing completion might update lvb without
675 * letting us know and we'll clobber the correct value.
676 * Cannot unlock after the check either, a that still leaves
677 * a tiny window for completion to get in */
678 lock_res_and_lock(lock);
679 if (lock->l_req_mode != lock->l_granted_mode)
680 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
681 lock->l_lvb_data, lvb_len);
682 unlock_res_and_lock(lock);
690 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
691 if (lock->l_completion_ast != NULL) {
692 int err = lock->l_completion_ast(lock, *flags, NULL);
700 if (lvb_len > 0 && lvb != NULL) {
701 /* Copy the LVB here, and not earlier, because the completion
702 * AST (if any) can override what we got in the reply */
703 memcpy(lvb, lock->l_lvb_data, lvb_len);
706 LDLM_DEBUG(lock, "client-side enqueue END");
709 if (cleanup_phase == 1 && rc)
710 failed_lock_cleanup(ns, lock, mode);
711 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
713 LDLM_LOCK_RELEASE(lock);
716 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
719 * Estimate number of lock handles that would fit into request of given
720 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
721 * a single page on the send/receive side. XXX: 512 should be changed to
722 * more adequate value.
724 static inline int ldlm_req_handles_avail(int req_size, int off)
728 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_CACHE_SIZE - 512) - req_size;
729 if (likely(avail >= 0))
730 avail /= (int)sizeof(struct lustre_handle);
733 avail += LDLM_LOCKREQ_HANDLES - off;
738 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
739 enum req_location loc,
742 __u32 size = req_capsule_msg_size(pill, loc);
743 return ldlm_req_handles_avail(size, off);
746 static inline int ldlm_format_handles_avail(struct obd_import *imp,
747 const struct req_format *fmt,
748 enum req_location loc, int off)
750 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
751 return ldlm_req_handles_avail(size, off);
755 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
756 * \a count locks in \a cancels.
758 * This is to be called by functions preparing their own requests that
759 * might contain lists of locks to cancel in addition to actual operation
760 * that needs to be performed.
762 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
763 int version, int opc, int canceloff,
764 struct list_head *cancels, int count)
766 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
767 struct req_capsule *pill = &req->rq_pill;
768 struct ldlm_request *dlm = NULL;
769 struct list_head head = LIST_HEAD_INIT(head);
770 enum ldlm_lru_flags lru_flags;
771 int avail, to_free, pack = 0;
777 if (ns_connect_cancelset(ns)) {
778 /* Estimate the amount of available space in the request. */
779 req_capsule_filled_sizes(pill, RCL_CLIENT);
780 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
782 lru_flags = ns_connect_lru_resize(ns) ?
783 LDLM_LRU_FLAG_LRUR_NO_WAIT : LDLM_LRU_FLAG_AGED;
784 to_free = !ns_connect_lru_resize(ns) &&
785 opc == LDLM_ENQUEUE ? 1 : 0;
787 /* Cancel LRU locks here _only_ if the server supports
788 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
789 * RPC, which will make us slower. */
791 count += ldlm_cancel_lru_local(ns, cancels, to_free,
798 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
799 ldlm_request_bufsize(pack, opc));
802 rc = ptlrpc_request_pack(req, version, opc);
804 ldlm_lock_list_put(cancels, l_bl_ast, count);
808 if (ns_connect_cancelset(ns)) {
810 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
812 /* Skip first lock handler in ldlm_request_pack(),
813 * this method will increment @lock_count according
814 * to the lock handle amount actually written to
816 dlm->lock_count = canceloff;
818 /* Pack into the request @pack lock handles. */
819 ldlm_cli_cancel_list(cancels, pack, req, 0);
820 /* Prepare and send separate cancel RPC for others. */
821 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
823 ldlm_lock_list_put(cancels, l_bl_ast, count);
827 EXPORT_SYMBOL(ldlm_prep_elc_req);
829 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
830 struct list_head *cancels, int count)
832 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
833 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
835 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
837 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
839 struct ptlrpc_request *req;
843 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
845 RETURN(ERR_PTR(-ENOMEM));
847 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
849 ptlrpc_request_free(req);
853 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
854 ptlrpc_request_set_replen(req);
857 EXPORT_SYMBOL(ldlm_enqueue_pack);
860 * Client-side lock enqueue.
862 * If a request has some specific initialisation it is passed in \a reqp,
863 * otherwise it is created in ldlm_cli_enqueue.
865 * Supports sync and async requests, pass \a async flag accordingly. If a
866 * request was created in ldlm_cli_enqueue and it is the async request,
867 * pass it to the caller in \a reqp.
869 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
870 struct ldlm_enqueue_info *einfo,
871 const struct ldlm_res_id *res_id,
872 union ldlm_policy_data const *policy, __u64 *flags,
873 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
874 struct lustre_handle *lockh, int async)
876 struct ldlm_namespace *ns;
877 struct ldlm_lock *lock;
878 struct ldlm_request *body;
879 int is_replay = *flags & LDLM_FL_REPLAY;
880 int req_passed_in = 1;
882 struct ptlrpc_request *req;
885 LASSERT(exp != NULL);
887 ns = exp->exp_obd->obd_namespace;
889 /* If we're replaying this lock, just check some invariants.
890 * If we're creating a new lock, get everything all setup nice. */
892 lock = ldlm_handle2lock_long(lockh, 0);
893 LASSERT(lock != NULL);
894 LDLM_DEBUG(lock, "client-side enqueue START");
895 LASSERT(exp == lock->l_conn_export);
897 const struct ldlm_callback_suite cbs = {
898 .lcs_completion = einfo->ei_cb_cp,
899 .lcs_blocking = einfo->ei_cb_bl,
900 .lcs_glimpse = einfo->ei_cb_gl
902 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
903 einfo->ei_mode, &cbs, einfo->ei_cbdata,
906 RETURN(PTR_ERR(lock));
907 /* for the local lock, add the reference */
908 ldlm_lock_addref_internal(lock, einfo->ei_mode);
909 ldlm_lock2handle(lock, lockh);
911 lock->l_policy_data = *policy;
913 if (einfo->ei_type == LDLM_EXTENT) {
914 /* extent lock without policy is a bug */
918 lock->l_req_extent = policy->l_extent;
920 LDLM_DEBUG(lock, "client-side enqueue START, flags "LPX64,
924 lock->l_conn_export = exp;
925 lock->l_export = NULL;
926 lock->l_blocking_ast = einfo->ei_cb_bl;
927 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL));
928 lock->l_last_activity = cfs_time_current_sec();
930 /* lock not sent to server yet */
932 if (reqp == NULL || *reqp == NULL) {
933 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
938 failed_lock_cleanup(ns, lock, einfo->ei_mode);
939 LDLM_LOCK_RELEASE(lock);
949 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
951 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
952 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
955 /* Dump lock data into the request buffer */
956 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
957 ldlm_lock2desc(lock, &body->lock_desc);
958 body->lock_flags = ldlm_flags_to_wire(*flags);
959 body->lock_handle[0] = *lockh;
961 /* Continue as normal. */
962 if (!req_passed_in) {
964 req_capsule_extend(&req->rq_pill,
965 &RQF_LDLM_ENQUEUE_LVB);
966 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
968 ptlrpc_request_set_replen(req);
972 LASSERT(reqp != NULL);
976 LDLM_DEBUG(lock, "sending request");
978 rc = ptlrpc_queue_wait(req);
980 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
981 einfo->ei_mode, flags, lvb, lvb_len,
984 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
985 * one reference that we took */
987 LDLM_LOCK_RELEASE(lock);
991 if (!req_passed_in && req != NULL) {
992 ptlrpc_req_finished(req);
999 EXPORT_SYMBOL(ldlm_cli_enqueue);
1001 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
1004 struct ldlm_resource *res;
1007 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1008 CERROR("Trying to cancel local lock\n");
1011 LDLM_DEBUG(lock, "client-side local convert");
1013 res = ldlm_lock_convert(lock, new_mode, flags);
1015 ldlm_reprocess_all(res);
1018 rc = LUSTRE_EDEADLK;
1020 LDLM_DEBUG(lock, "client-side local convert handler END");
1021 LDLM_LOCK_PUT(lock);
1025 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
1026 * conversion of locks which are on the waiting or converting queue */
1027 /* Caller of this code is supposed to take care of lock readers/writers
1029 int ldlm_cli_convert(const struct lustre_handle *lockh, int new_mode,
1032 struct ldlm_request *body;
1033 struct ldlm_reply *reply;
1034 struct ldlm_lock *lock;
1035 struct ldlm_resource *res;
1036 struct ptlrpc_request *req;
1040 lock = ldlm_handle2lock(lockh);
1047 if (lock->l_conn_export == NULL)
1048 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
1050 LDLM_DEBUG(lock, "client-side convert");
1052 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
1053 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1056 LDLM_LOCK_PUT(lock);
1060 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1061 body->lock_handle[0] = lock->l_remote_handle;
1063 body->lock_desc.l_req_mode = new_mode;
1064 body->lock_flags = ldlm_flags_to_wire(*flags);
1067 ptlrpc_request_set_replen(req);
1068 rc = ptlrpc_queue_wait(req);
1072 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1074 GOTO(out, rc = -EPROTO);
1077 GOTO(out, rc = req->rq_status);
1079 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
1081 ldlm_reprocess_all(res);
1082 /* Go to sleep until the lock is granted. */
1083 /* FIXME: or cancelled. */
1084 if (lock->l_completion_ast) {
1085 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1091 rc = LUSTRE_EDEADLK;
1095 LDLM_LOCK_PUT(lock);
1096 ptlrpc_req_finished(req);
1101 * Cancel locks locally.
1103 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1104 * \retval LDLM_FL_CANCELING otherwise;
1105 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1107 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1109 __u64 rc = LDLM_FL_LOCAL_ONLY;
1112 if (lock->l_conn_export) {
1115 LDLM_DEBUG(lock, "client-side cancel");
1116 /* Set this flag to prevent others from getting new references*/
1117 lock_res_and_lock(lock);
1118 ldlm_set_cbpending(lock);
1119 local_only = !!(lock->l_flags &
1120 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1121 ldlm_cancel_callback(lock);
1122 rc = (ldlm_is_bl_ast(lock)) ?
1123 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1124 unlock_res_and_lock(lock);
1127 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1129 rc = LDLM_FL_LOCAL_ONLY;
1131 ldlm_lock_cancel(lock);
1133 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1134 LDLM_ERROR(lock, "Trying to cancel local lock");
1137 LDLM_DEBUG(lock, "server-side local cancel");
1138 ldlm_lock_cancel(lock);
1139 ldlm_reprocess_all(lock->l_resource);
1146 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1148 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1149 struct list_head *head, int count)
1151 struct ldlm_request *dlm;
1152 struct ldlm_lock *lock;
1153 int max, packed = 0;
1156 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1157 LASSERT(dlm != NULL);
1159 /* Check the room in the request buffer. */
1160 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1161 sizeof(struct ldlm_request);
1162 max /= sizeof(struct lustre_handle);
1163 max += LDLM_LOCKREQ_HANDLES;
1164 LASSERT(max >= dlm->lock_count + count);
1166 /* XXX: it would be better to pack lock handles grouped by resource.
1167 * so that the server cancel would call filter_lvbo_update() less
1169 list_for_each_entry(lock, head, l_bl_ast) {
1172 LASSERT(lock->l_conn_export);
1173 /* Pack the lock handle to the given request buffer. */
1174 LDLM_DEBUG(lock, "packing");
1175 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1178 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1183 * Prepare and send a batched cancel RPC. It will include \a count lock
1184 * handles of locks given in \a cancels list. */
1185 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1186 int count, enum ldlm_cancel_flags flags)
1188 struct ptlrpc_request *req = NULL;
1189 struct obd_import *imp;
1194 LASSERT(exp != NULL);
1197 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1199 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1202 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1203 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1208 imp = class_exp2cliimp(exp);
1209 if (imp == NULL || imp->imp_invalid) {
1211 "skipping cancel on invalid import %p\n", imp);
1215 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1217 GOTO(out, rc = -ENOMEM);
1219 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1220 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1221 ldlm_request_bufsize(count, LDLM_CANCEL));
1223 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1225 ptlrpc_request_free(req);
1229 /* If OSP want cancel cross-MDT lock, let's not block it in
1230 * in recovery, otherwise the lock will not released, if
1231 * the remote target is also in recovery, and it also need
1232 * this lock, it might cause deadlock. */
1233 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1234 exp->exp_obd->obd_lu_dev != NULL &&
1235 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1236 struct lu_device *top_dev;
1238 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1239 if (top_dev != NULL &&
1240 top_dev->ld_obd->obd_recovering)
1241 req->rq_allow_replay = 1;
1244 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1245 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1246 ptlrpc_at_set_req_timeout(req);
1248 ldlm_cancel_pack(req, cancels, count);
1250 ptlrpc_request_set_replen(req);
1251 if (flags & LCF_ASYNC) {
1252 ptlrpcd_add_req(req);
1257 rc = ptlrpc_queue_wait(req);
1258 if (rc == LUSTRE_ESTALE) {
1259 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1260 "out of sync -- not fatal\n",
1261 libcfs_nid2str(req->rq_import->
1262 imp_connection->c_peer.nid));
1264 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1265 req->rq_import_generation == imp->imp_generation) {
1266 ptlrpc_req_finished(req);
1268 } else if (rc != ELDLM_OK) {
1269 /* -ESHUTDOWN is common on umount */
1270 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1271 "Got rc %d from cancel RPC: "
1272 "canceling anyway\n", rc);
1279 ptlrpc_req_finished(req);
1282 return sent ? sent : rc;
1285 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1287 LASSERT(imp != NULL);
1288 return &imp->imp_obd->obd_namespace->ns_pool;
1292 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1294 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1296 struct obd_device *obd;
1300 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1301 !imp_connect_lru_resize(req->rq_import)))
1304 * Do nothing for corner cases.
1309 /* In some cases RPC may contain SLV and limit zeroed out. This
1310 * is the case when server does not support LRU resize feature.
1311 * This is also possible in some recovery cases when server-side
1312 * reqs have no reference to the OBD export and thus access to
1313 * server-side namespace is not possible. */
1314 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1315 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1316 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1317 "(SLV: "LPU64", Limit: %u)",
1318 lustre_msg_get_slv(req->rq_repmsg),
1319 lustre_msg_get_limit(req->rq_repmsg));
1323 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1324 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1325 obd = req->rq_import->imp_obd;
1327 /* Set new SLV and limit in OBD fields to make them accessible
1328 * to the pool thread. We do not access obd_namespace and pool
1329 * directly here as there is no reliable way to make sure that
1330 * they are still alive at cleanup time. Evil races are possible
1331 * which may cause Oops at that time. */
1332 write_lock(&obd->obd_pool_lock);
1333 obd->obd_pool_slv = new_slv;
1334 obd->obd_pool_limit = new_limit;
1335 write_unlock(&obd->obd_pool_lock);
1341 * Client side lock cancel.
1343 * Lock must not have any readers or writers by this time.
1345 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1346 enum ldlm_cancel_flags cancel_flags)
1348 struct obd_export *exp;
1349 enum ldlm_lru_flags lru_flags;
1350 int avail, count = 1;
1352 struct ldlm_namespace *ns;
1353 struct ldlm_lock *lock;
1354 struct list_head cancels = LIST_HEAD_INIT(cancels);
1357 lock = ldlm_handle2lock_long(lockh, 0);
1359 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1363 lock_res_and_lock(lock);
1364 /* Lock is being canceled and the caller doesn't want to wait */
1365 if (ldlm_is_canceling(lock) && (cancel_flags & LCF_ASYNC)) {
1366 unlock_res_and_lock(lock);
1367 LDLM_LOCK_RELEASE(lock);
1371 ldlm_set_canceling(lock);
1372 unlock_res_and_lock(lock);
1374 rc = ldlm_cli_cancel_local(lock);
1375 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1376 LDLM_LOCK_RELEASE(lock);
1379 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1380 * RPC which goes to canceld portal, so we can cancel other LRU locks
1381 * here and send them all as one LDLM_CANCEL RPC. */
1382 LASSERT(list_empty(&lock->l_bl_ast));
1383 list_add(&lock->l_bl_ast, &cancels);
1385 exp = lock->l_conn_export;
1386 if (exp_connect_cancelset(exp)) {
1387 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1392 ns = ldlm_lock_to_ns(lock);
1393 lru_flags = ns_connect_lru_resize(ns) ?
1394 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1395 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1396 LCF_BL_AST, lru_flags);
1398 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1401 EXPORT_SYMBOL(ldlm_cli_cancel);
1404 * Locally cancel up to \a count locks in list \a cancels.
1405 * Return the number of cancelled locks.
1407 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1408 enum ldlm_cancel_flags cancel_flags)
1410 struct list_head head = LIST_HEAD_INIT(head);
1411 struct ldlm_lock *lock, *next;
1412 int left = 0, bl_ast = 0;
1416 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1420 if (cancel_flags & LCF_LOCAL) {
1421 rc = LDLM_FL_LOCAL_ONLY;
1422 ldlm_lock_cancel(lock);
1424 rc = ldlm_cli_cancel_local(lock);
1426 /* Until we have compound requests and can send LDLM_CANCEL
1427 * requests batched with generic RPCs, we need to send cancels
1428 * with the LDLM_FL_BL_AST flag in a separate RPC from
1429 * the one being generated now. */
1430 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1431 LDLM_DEBUG(lock, "Cancel lock separately");
1432 list_del_init(&lock->l_bl_ast);
1433 list_add(&lock->l_bl_ast, &head);
1437 if (rc == LDLM_FL_LOCAL_ONLY) {
1438 /* CANCEL RPC should not be sent to server. */
1439 list_del_init(&lock->l_bl_ast);
1440 LDLM_LOCK_RELEASE(lock);
1446 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1453 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1454 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1455 * readahead requests, ...)
1457 static enum ldlm_policy_res
1458 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1459 int unused, int added, int count)
1461 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1463 /* don't check added & count since we want to process all locks
1465 * It's fine to not take lock to access lock->l_resource since
1466 * the lock has already been granted so it won't change. */
1467 switch (lock->l_resource->lr_type) {
1470 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1473 result = LDLM_POLICY_SKIP_LOCK;
1474 lock_res_and_lock(lock);
1475 ldlm_set_skipped(lock);
1476 unlock_res_and_lock(lock);
1484 * Callback function for LRU-resize policy. Decides whether to keep
1485 * \a lock in LRU for current \a LRU size \a unused, added in current
1486 * scan \a added and number of locks to be preferably canceled \a count.
1488 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1490 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1492 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1493 struct ldlm_lock *lock,
1494 int unused, int added,
1497 cfs_time_t cur = cfs_time_current();
1498 struct ldlm_pool *pl = &ns->ns_pool;
1502 /* Stop LRU processing when we reach past @count or have checked all
1504 if (count && added >= count)
1505 return LDLM_POLICY_KEEP_LOCK;
1507 /* Despite of the LV, It doesn't make sense to keep the lock which
1508 * is unused for ns_max_age time. */
1509 if (cfs_time_after(cfs_time_current(),
1510 cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1511 return LDLM_POLICY_CANCEL_LOCK;
1513 slv = ldlm_pool_get_slv(pl);
1514 lvf = ldlm_pool_get_lvf(pl);
1515 la = cfs_duration_sec(cfs_time_sub(cur,
1516 lock->l_last_used));
1517 lv = lvf * la * unused;
1519 /* Inform pool about current CLV to see it via proc. */
1520 ldlm_pool_set_clv(pl, lv);
1522 /* Stop when SLV is not yet come from server or lv is smaller than
1524 if (slv == 0 || lv < slv)
1525 return LDLM_POLICY_KEEP_LOCK;
1527 return LDLM_POLICY_CANCEL_LOCK;
1530 static enum ldlm_policy_res
1531 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1532 struct ldlm_lock *lock,
1533 int unused, int added,
1536 enum ldlm_policy_res result;
1538 result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1539 if (result == LDLM_POLICY_KEEP_LOCK)
1542 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1546 * Callback function for proc used policy. Makes decision whether to keep
1547 * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1548 * added and number of locks to be preferably canceled \a count.
1550 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1552 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1554 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1555 struct ldlm_lock *lock,
1556 int unused, int added,
1559 /* Stop LRU processing when we reach past @count or have checked all
1561 return (added >= count) ?
1562 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1566 * Callback function for aged policy. Makes decision whether to keep \a lock in
1567 * LRU for current LRU size \a unused, added in current scan \a added and
1568 * number of locks to be preferably canceled \a count.
1570 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1572 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1574 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1575 struct ldlm_lock *lock,
1576 int unused, int added,
1579 if ((added >= count) &&
1580 cfs_time_before(cfs_time_current(),
1581 cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1582 return LDLM_POLICY_KEEP_LOCK;
1584 return LDLM_POLICY_CANCEL_LOCK;
1588 * Callback function for default policy. Makes decision whether to keep \a lock
1589 * in LRU for current LRU size \a unused, added in current scan \a added and
1590 * number of locks to be preferably canceled \a count.
1592 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1594 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1597 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1598 struct ldlm_lock *lock,
1599 int unused, int added,
1602 /* Stop LRU processing when we reach past count or have checked all
1604 return (added >= count) ?
1605 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1608 typedef enum ldlm_policy_res
1609 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1610 int unused, int added, int count);
1612 static ldlm_cancel_lru_policy_t
1613 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1615 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1616 return ldlm_cancel_no_wait_policy;
1618 if (ns_connect_lru_resize(ns)) {
1619 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1620 /* We kill passed number of old locks. */
1621 return ldlm_cancel_passed_policy;
1622 if (lru_flags & LDLM_LRU_FLAG_LRUR)
1623 return ldlm_cancel_lrur_policy;
1624 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1625 return ldlm_cancel_passed_policy;
1626 else if (lru_flags & LDLM_LRU_FLAG_LRUR_NO_WAIT)
1627 return ldlm_cancel_lrur_no_wait_policy;
1629 if (lru_flags & LDLM_LRU_FLAG_AGED)
1630 return ldlm_cancel_aged_policy;
1633 return ldlm_cancel_default_policy;
1637 * - Free space in LRU for \a count new locks,
1638 * redundant unused locks are canceled locally;
1639 * - also cancel locally unused aged locks;
1640 * - do not cancel more than \a max locks;
1641 * - GET the found locks and add them into the \a cancels list.
1643 * A client lock can be added to the l_bl_ast list only when it is
1644 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1645 * CANCEL. There are the following use cases:
1646 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1647 * ldlm_cli_cancel(), which check and set this flag properly. As any
1648 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1649 * later without any special locking.
1651 * Calling policies for enabled LRU resize:
1652 * ----------------------------------------
1653 * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1654 * cancel not more than \a count locks;
1656 * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1657 * at the beginning of LRU list);
1659 * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1660 * to memory pressre policy function;
1662 * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1664 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1665 * (typically before replaying locks) w/o
1666 * sending any RPCs or waiting for any
1667 * outstanding RPC to complete.
1669 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1670 struct list_head *cancels, int count, int max,
1671 enum ldlm_lru_flags lru_flags)
1673 ldlm_cancel_lru_policy_t pf;
1674 struct ldlm_lock *lock, *next;
1675 int added = 0, unused, remained;
1676 int no_wait = lru_flags & (LDLM_LRU_FLAG_NO_WAIT |
1677 LDLM_LRU_FLAG_LRUR_NO_WAIT);
1680 spin_lock(&ns->ns_lock);
1681 unused = ns->ns_nr_unused;
1684 if (!ns_connect_lru_resize(ns))
1685 count += unused - ns->ns_max_unused;
1687 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1688 LASSERT(pf != NULL);
1690 while (!list_empty(&ns->ns_unused_list)) {
1691 enum ldlm_policy_res result;
1692 cfs_time_t last_use = 0;
1694 /* all unused locks */
1695 if (remained-- <= 0)
1698 /* For any flags, stop scanning if @max is reached. */
1699 if (max && added >= max)
1702 list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1704 /* No locks which got blocking requests. */
1705 LASSERT(!ldlm_is_bl_ast(lock));
1707 if (no_wait && ldlm_is_skipped(lock))
1708 /* already processed */
1711 last_use = lock->l_last_used;
1712 if (last_use == cfs_time_current())
1715 /* Somebody is already doing CANCEL. No need for this
1716 * lock in LRU, do not traverse it again. */
1717 if (!ldlm_is_canceling(lock))
1720 ldlm_lock_remove_from_lru_nolock(lock);
1722 if (&lock->l_lru == &ns->ns_unused_list)
1725 LDLM_LOCK_GET(lock);
1726 spin_unlock(&ns->ns_lock);
1727 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1729 /* Pass the lock through the policy filter and see if it
1730 * should stay in LRU.
1732 * Even for shrinker policy we stop scanning if
1733 * we find a lock that should stay in the cache.
1734 * We should take into account lock age anyway
1735 * as a new lock is a valuable resource even if
1736 * it has a low weight.
1738 * That is, for shrinker policy we drop only
1739 * old locks, but additionally choose them by
1740 * their weight. Big extent locks will stay in
1742 result = pf(ns, lock, unused, added, count);
1743 if (result == LDLM_POLICY_KEEP_LOCK) {
1744 lu_ref_del(&lock->l_reference,
1745 __FUNCTION__, current);
1746 LDLM_LOCK_RELEASE(lock);
1747 spin_lock(&ns->ns_lock);
1750 if (result == LDLM_POLICY_SKIP_LOCK) {
1751 lu_ref_del(&lock->l_reference,
1753 LDLM_LOCK_RELEASE(lock);
1754 spin_lock(&ns->ns_lock);
1758 lock_res_and_lock(lock);
1759 /* Check flags again under the lock. */
1760 if (ldlm_is_canceling(lock) ||
1761 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1762 /* Another thread is removing lock from LRU, or
1763 * somebody is already doing CANCEL, or there
1764 * is a blocking request which will send cancel
1765 * by itself, or the lock is no longer unused or
1766 * the lock has been used since the pf() call and
1767 * pages could be put under it. */
1768 unlock_res_and_lock(lock);
1769 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1770 LDLM_LOCK_RELEASE(lock);
1771 spin_lock(&ns->ns_lock);
1774 LASSERT(!lock->l_readers && !lock->l_writers);
1776 /* If we have chosen to cancel this lock voluntarily, we
1777 * better send cancel notification to server, so that it
1778 * frees appropriate state. This might lead to a race
1779 * where while we are doing cancel here, server is also
1780 * silently cancelling this lock. */
1781 ldlm_clear_cancel_on_block(lock);
1783 /* Setting the CBPENDING flag is a little misleading,
1784 * but prevents an important race; namely, once
1785 * CBPENDING is set, the lock can accumulate no more
1786 * readers/writers. Since readers and writers are
1787 * already zero here, ldlm_lock_decref() won't see
1788 * this flag and call l_blocking_ast */
1789 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1791 /* We can't re-add to l_lru as it confuses the
1792 * refcounting in ldlm_lock_remove_from_lru() if an AST
1793 * arrives after we drop lr_lock below. We use l_bl_ast
1794 * and can't use l_pending_chain as it is used both on
1795 * server and client nevertheless bug 5666 says it is
1796 * used only on server */
1797 LASSERT(list_empty(&lock->l_bl_ast));
1798 list_add(&lock->l_bl_ast, cancels);
1799 unlock_res_and_lock(lock);
1800 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1801 spin_lock(&ns->ns_lock);
1805 spin_unlock(&ns->ns_lock);
1809 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1811 enum ldlm_cancel_flags cancel_flags,
1812 enum ldlm_lru_flags lru_flags)
1816 added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
1820 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1824 * Cancel at least \a nr locks from given namespace LRU.
1826 * When called with LCF_ASYNC the blocking callback will be handled
1827 * in a thread and this function will return after the thread has been
1828 * asked to call the callback. When called with LCF_ASYNC the blocking
1829 * callback will be performed in this function.
1831 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
1832 enum ldlm_cancel_flags cancel_flags,
1833 enum ldlm_lru_flags lru_flags)
1835 struct list_head cancels = LIST_HEAD_INIT(cancels);
1839 /* Just prepare the list of locks, do not actually cancel them yet.
1840 * Locks are cancelled later in a separate thread. */
1841 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
1842 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
1850 * Find and cancel locally unused locks found on resource, matched to the
1851 * given policy, mode. GET the found locks and add them into the \a cancels
1854 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1855 struct list_head *cancels,
1856 union ldlm_policy_data *policy,
1857 enum ldlm_mode mode, __u64 lock_flags,
1858 enum ldlm_cancel_flags cancel_flags,
1861 struct ldlm_lock *lock;
1866 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1867 if (opaque != NULL && lock->l_ast_data != opaque) {
1868 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1869 lock->l_ast_data, opaque);
1874 if (lock->l_readers || lock->l_writers)
1877 /* If somebody is already doing CANCEL, or blocking AST came,
1878 * skip this lock. */
1879 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
1882 if (lockmode_compat(lock->l_granted_mode, mode))
1885 /* If policy is given and this is IBITS lock, add to list only
1886 * those locks that match by policy. */
1887 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1888 !(lock->l_policy_data.l_inodebits.bits &
1889 policy->l_inodebits.bits))
1892 /* See CBPENDING comment in ldlm_cancel_lru */
1893 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1896 LASSERT(list_empty(&lock->l_bl_ast));
1897 list_add(&lock->l_bl_ast, cancels);
1898 LDLM_LOCK_GET(lock);
1903 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1905 EXPORT_SYMBOL(ldlm_cancel_resource_local);
1908 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
1910 * If \a req is NULL, send CANCEL request to server with handles of locks
1911 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1912 * separately per lock.
1913 * If \a req is not NULL, put handles of locks in \a cancels into the request
1914 * buffer at the offset \a off.
1915 * Destroy \a cancels at the end.
1917 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1918 struct ptlrpc_request *req,
1919 enum ldlm_cancel_flags flags)
1921 struct ldlm_lock *lock;
1925 if (list_empty(cancels) || count == 0)
1928 /* XXX: requests (both batched and not) could be sent in parallel.
1929 * Usually it is enough to have just 1 RPC, but it is possible that
1930 * there are too many locks to be cancelled in LRU or on a resource.
1931 * It would also speed up the case when the server does not support
1934 LASSERT(!list_empty(cancels));
1935 lock = list_entry(cancels->next, struct ldlm_lock,
1937 LASSERT(lock->l_conn_export);
1939 if (exp_connect_cancelset(lock->l_conn_export)) {
1942 ldlm_cancel_pack(req, cancels, count);
1944 res = ldlm_cli_cancel_req(lock->l_conn_export,
1948 res = ldlm_cli_cancel_req(lock->l_conn_export,
1953 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1954 "ldlm_cli_cancel_list: %d\n", res);
1959 ldlm_lock_list_put(cancels, l_bl_ast, res);
1961 LASSERT(count == 0);
1964 EXPORT_SYMBOL(ldlm_cli_cancel_list);
1967 * Cancel all locks on a resource that have 0 readers/writers.
1969 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1970 * to notify the server. */
1971 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1972 const struct ldlm_res_id *res_id,
1973 union ldlm_policy_data *policy,
1974 enum ldlm_mode mode,
1975 enum ldlm_cancel_flags flags, void *opaque)
1977 struct ldlm_resource *res;
1978 struct list_head cancels = LIST_HEAD_INIT(cancels);
1983 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1985 /* This is not a problem. */
1986 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1990 LDLM_RESOURCE_ADDREF(res);
1991 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1992 0, flags | LCF_BL_AST, opaque);
1993 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1995 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
1998 LDLM_RESOURCE_DELREF(res);
1999 ldlm_resource_putref(res);
2002 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2004 struct ldlm_cli_cancel_arg {
2010 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2011 struct hlist_node *hnode, void *arg)
2013 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2014 struct ldlm_cli_cancel_arg *lc = arg;
2016 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2017 NULL, LCK_MINMODE, lc->lc_flags,
2019 /* must return 0 for hash iteration */
2024 * Cancel all locks on a namespace (or a specific resource, if given)
2025 * that have 0 readers/writers.
2027 * If flags & LCF_LOCAL, throw the locks away without trying
2028 * to notify the server. */
2029 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2030 const struct ldlm_res_id *res_id,
2031 enum ldlm_cancel_flags flags, void *opaque)
2033 struct ldlm_cli_cancel_arg arg = {
2035 .lc_opaque = opaque,
2043 if (res_id != NULL) {
2044 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2048 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2049 ldlm_cli_hash_cancel_unused, &arg, 0);
2054 /* Lock iterators. */
2056 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2059 struct list_head *tmp, *next;
2060 struct ldlm_lock *lock;
2061 int rc = LDLM_ITER_CONTINUE;
2066 RETURN(LDLM_ITER_CONTINUE);
2069 list_for_each_safe(tmp, next, &res->lr_granted) {
2070 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2072 if (iter(lock, closure) == LDLM_ITER_STOP)
2073 GOTO(out, rc = LDLM_ITER_STOP);
2076 list_for_each_safe(tmp, next, &res->lr_converting) {
2077 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2079 if (iter(lock, closure) == LDLM_ITER_STOP)
2080 GOTO(out, rc = LDLM_ITER_STOP);
2083 list_for_each_safe(tmp, next, &res->lr_waiting) {
2084 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2086 if (iter(lock, closure) == LDLM_ITER_STOP)
2087 GOTO(out, rc = LDLM_ITER_STOP);
2094 struct iter_helper_data {
2095 ldlm_iterator_t iter;
2099 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2101 struct iter_helper_data *helper = closure;
2102 return helper->iter(lock, helper->closure);
2105 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2106 struct hlist_node *hnode, void *arg)
2109 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2111 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2115 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2116 ldlm_iterator_t iter, void *closure)
2119 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2121 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2122 ldlm_res_iter_helper, &helper, 0);
2126 /* non-blocking function to manipulate a lock whose cb_data is being put away.
2127 * return 0: find no resource
2128 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2131 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2132 const struct ldlm_res_id *res_id,
2133 ldlm_iterator_t iter, void *data)
2135 struct ldlm_resource *res;
2139 LASSERTF(ns != NULL, "must pass in namespace\n");
2141 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2145 LDLM_RESOURCE_ADDREF(res);
2146 rc = ldlm_resource_foreach(res, iter, data);
2147 LDLM_RESOURCE_DELREF(res);
2148 ldlm_resource_putref(res);
2151 EXPORT_SYMBOL(ldlm_resource_iterate);
2155 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2157 struct list_head *list = closure;
2159 /* we use l_pending_chain here, because it's unused on clients. */
2160 LASSERTF(list_empty(&lock->l_pending_chain),
2161 "lock %p next %p prev %p\n",
2162 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2163 /* bug 9573: don't replay locks left after eviction, or
2164 * bug 17614: locks being actively cancelled. Get a reference
2165 * on a lock so that it does not disapear under us (e.g. due to cancel)
2167 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_CANCELING))) {
2168 list_add(&lock->l_pending_chain, list);
2169 LDLM_LOCK_GET(lock);
2172 return LDLM_ITER_CONTINUE;
2175 static int replay_lock_interpret(const struct lu_env *env,
2176 struct ptlrpc_request *req,
2177 struct ldlm_async_args *aa, int rc)
2179 struct ldlm_lock *lock;
2180 struct ldlm_reply *reply;
2181 struct obd_export *exp;
2184 atomic_dec(&req->rq_import->imp_replay_inflight);
2188 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2190 GOTO(out, rc = -EPROTO);
2192 lock = ldlm_handle2lock(&aa->lock_handle);
2194 CERROR("received replay ack for unknown local cookie "LPX64
2195 " remote cookie "LPX64 " from server %s id %s\n",
2196 aa->lock_handle.cookie, reply->lock_handle.cookie,
2197 req->rq_export->exp_client_uuid.uuid,
2198 libcfs_id2str(req->rq_peer));
2199 GOTO(out, rc = -ESTALE);
2202 /* Key change rehash lock in per-export hash with new key */
2203 exp = req->rq_export;
2204 if (exp && exp->exp_lock_hash) {
2205 /* In the function below, .hs_keycmp resolves to
2206 * ldlm_export_lock_keycmp() */
2207 /* coverity[overrun-buffer-val] */
2208 cfs_hash_rehash_key(exp->exp_lock_hash,
2209 &lock->l_remote_handle,
2210 &reply->lock_handle,
2213 lock->l_remote_handle = reply->lock_handle;
2216 LDLM_DEBUG(lock, "replayed lock:");
2217 ptlrpc_import_recovery_state_machine(req->rq_import);
2218 LDLM_LOCK_PUT(lock);
2221 ptlrpc_connect_import(req->rq_import);
2226 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2228 struct ptlrpc_request *req;
2229 struct ldlm_async_args *aa;
2230 struct ldlm_request *body;
2235 /* Bug 11974: Do not replay a lock which is actively being canceled */
2236 if (ldlm_is_canceling(lock)) {
2237 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2241 /* If this is reply-less callback lock, we cannot replay it, since
2242 * server might have long dropped it, but notification of that event was
2243 * lost by network. (and server granted conflicting lock already) */
2244 if (ldlm_is_cancel_on_block(lock)) {
2245 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2246 ldlm_lock_cancel(lock);
2251 * If granted mode matches the requested mode, this lock is granted.
2253 * If they differ, but we have a granted mode, then we were granted
2254 * one mode and now want another: ergo, converting.
2256 * If we haven't been granted anything and are on a resource list,
2257 * then we're blocked/waiting.
2259 * If we haven't been granted anything and we're NOT on a resource list,
2260 * then we haven't got a reply yet and don't have a known disposition.
2261 * This happens whenever a lock enqueue is the request that triggers
2264 if (lock->l_granted_mode == lock->l_req_mode)
2265 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2266 else if (lock->l_granted_mode)
2267 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2268 else if (!list_empty(&lock->l_res_link))
2269 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2271 flags = LDLM_FL_REPLAY;
2273 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2274 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2278 /* We're part of recovery, so don't wait for it. */
2279 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2281 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2282 ldlm_lock2desc(lock, &body->lock_desc);
2283 body->lock_flags = ldlm_flags_to_wire(flags);
2285 ldlm_lock2handle(lock, &body->lock_handle[0]);
2286 if (lock->l_lvb_len > 0)
2287 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2288 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2290 ptlrpc_request_set_replen(req);
2291 /* notify the server we've replayed all requests.
2292 * also, we mark the request to be put on a dedicated
2293 * queue to be processed after all request replayes.
2295 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2297 LDLM_DEBUG(lock, "replaying lock:");
2299 atomic_inc(&req->rq_import->imp_replay_inflight);
2300 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2301 aa = ptlrpc_req_async_args(req);
2302 aa->lock_handle = body->lock_handle[0];
2303 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2304 ptlrpcd_add_req(req);
2310 * Cancel as many unused locks as possible before replay. since we are
2311 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2314 * Called only in recovery before replaying locks. there is no need to
2315 * replay locks that are unused. since the clients may hold thousands of
2316 * cached unused locks, dropping the unused locks can greatly reduce the
2317 * load on the servers at recovery time.
2319 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2322 struct list_head cancels = LIST_HEAD_INIT(cancels);
2324 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2325 "replay for namespace %s (%d)\n",
2326 ldlm_ns_name(ns), ns->ns_nr_unused);
2328 /* We don't need to care whether or not LRU resize is enabled
2329 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2330 * count parameter */
2331 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2332 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2334 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2335 canceled, ldlm_ns_name(ns));
2338 int ldlm_replay_locks(struct obd_import *imp)
2340 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2341 struct list_head list = LIST_HEAD_INIT(list);
2342 struct ldlm_lock *lock, *next;
2347 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2349 /* don't replay locks if import failed recovery */
2350 if (imp->imp_vbr_failed)
2353 /* ensure this doesn't fall to 0 before all have been queued */
2354 atomic_inc(&imp->imp_replay_inflight);
2356 if (ldlm_cancel_unused_locks_before_replay)
2357 ldlm_cancel_unused_locks_for_replay(ns);
2359 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2361 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2362 list_del_init(&lock->l_pending_chain);
2364 LDLM_LOCK_RELEASE(lock);
2365 continue; /* or try to do the rest? */
2367 rc = replay_one_lock(imp, lock);
2368 LDLM_LOCK_RELEASE(lock);
2371 atomic_dec(&imp->imp_replay_inflight);