4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * This file contains Asynchronous System Trap (AST) handlers and related
34 * LDLM request-processing routines.
36 * An AST is a callback issued on a lock when its state is changed. There are
37 * several different types of ASTs (callbacks) registered for each lock:
39 * - completion AST: when a lock is enqueued by some process, but cannot be
40 * granted immediately due to other conflicting locks on the same resource,
41 * the completion AST is sent to notify the caller when the lock is
44 * - blocking AST: when a lock is granted to some process, if another process
45 * enqueues a conflicting (blocking) lock on a resource, a blocking AST is
46 * sent to notify the holder(s) of the lock(s) of the conflicting lock
47 * request. The lock holder(s) must release their lock(s) on that resource in
48 * a timely manner or be evicted by the server.
50 * - glimpse AST: this is used when a process wants information about a lock
51 * (i.e. the lock value block (LVB)) but does not necessarily require holding
52 * the lock. If the resource is locked, the lock holder(s) are sent glimpse
53 * ASTs and the LVB is returned to the caller, and lock holder(s) may CANCEL
54 * their lock(s) if they are idle. If the resource is not locked, the server
58 #define DEBUG_SUBSYSTEM S_LDLM
60 #include <lustre_dlm.h>
61 #include <obd_class.h>
64 #include "ldlm_internal.h"
66 unsigned int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
67 module_param(ldlm_enqueue_min, uint, 0644);
68 MODULE_PARM_DESC(ldlm_enqueue_min, "lock enqueue timeout minimum");
70 /* in client side, whether the cached locks will be canceled before replay */
71 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
73 static void interrupted_completion_wait(void *data)
77 struct lock_wait_data {
78 struct ldlm_lock *lwd_lock;
82 struct ldlm_async_args {
83 struct lustre_handle lock_handle;
87 * ldlm_request_bufsize
89 * If opcode=LDLM_ENQUEUE, 1 slot is already occupied,
90 * LDLM_LOCKREQ_HANDLE -1 slots are available.
91 * Otherwise, LDLM_LOCKREQ_HANDLE slots are available.
96 * \retval size of the request buffer
99 int ldlm_request_bufsize(int count, int type)
101 int avail = LDLM_LOCKREQ_HANDLES;
102 if (type == LDLM_ENQUEUE)
103 avail -= LDLM_ENQUEUE_CANCEL_OFF;
106 avail = (count - avail) * sizeof(struct lustre_handle);
110 return sizeof(struct ldlm_request) + avail;
113 int ldlm_expired_completion_wait(void *data)
115 struct lock_wait_data *lwd = data;
116 struct ldlm_lock *lock = lwd->lwd_lock;
117 struct obd_import *imp;
118 struct obd_device *obd;
121 if (lock->l_conn_export == NULL) {
122 static cfs_time_t next_dump = 0, last_dump = 0;
124 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
125 CFS_DURATION_T"s ago); not entering recovery in "
126 "server code, just going back to sleep",
127 lock->l_last_activity,
128 cfs_time_sub(cfs_time_current_sec(),
129 lock->l_last_activity));
130 if (cfs_time_after(cfs_time_current(), next_dump)) {
131 last_dump = next_dump;
132 next_dump = cfs_time_shift(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 "CFS_TIME_T", "
145 CFS_DURATION_T"s ago), entering recovery for %s@%s",
146 lock->l_last_activity,
147 cfs_time_sub(cfs_time_current_sec(), lock->l_last_activity),
148 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
154 * Calculate the Completion timeout (covering enqueue, BL AST, data flush,
155 * lock cancel, and their replies). Used for lock completion timeout on the
158 * \param[in] lock lock which is waiting the completion callback
160 * \retval timeout in seconds to wait for the server reply
163 /* We use the same basis for both server side and client side functions
164 from a single node. */
165 static unsigned int ldlm_cp_timeout(struct ldlm_lock *lock)
167 unsigned int timeout;
172 /* Wait a long time for enqueue - server may have to callback a
173 * lock from another client. Server will evict the other client if it
174 * doesn't respond reasonably, and then give us the lock. */
175 timeout = at_get(ldlm_lock_to_ns_at(lock));
176 return max(3 * timeout, ldlm_enqueue_min);
180 * Helper function for ldlm_completion_ast(), updating timings when lock is
183 static int ldlm_completion_tail(struct ldlm_lock *lock, void *data)
188 if (ldlm_is_destroyed(lock) || ldlm_is_failed(lock)) {
189 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
191 } else if (data == NULL) {
192 LDLM_DEBUG(lock, "client-side enqueue: granted");
194 /* Take into AT only CP RPC, not immediately granted locks */
195 delay = cfs_time_sub(cfs_time_current_sec(),
196 lock->l_last_activity);
197 LDLM_DEBUG(lock, "client-side enqueue: granted after "
198 CFS_DURATION_T"s", delay);
200 /* Update our time estimate */
201 at_measured(ldlm_lock_to_ns_at(lock), delay);
207 * Implementation of ->l_completion_ast() for a client, that doesn't wait
208 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
209 * other threads that cannot block for long.
211 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data)
215 if (flags == LDLM_FL_WAIT_NOREPROC) {
216 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
220 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
221 wake_up(&lock->l_waitq);
222 RETURN(ldlm_completion_tail(lock, data));
225 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
227 ldlm_reprocess_all(lock->l_resource);
230 EXPORT_SYMBOL(ldlm_completion_ast_async);
233 * Generic LDLM "completion" AST. This is called in several cases:
235 * - when a reply to an ENQUEUE RPC is received from the server
236 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
237 * this point (determined by flags);
239 * - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
242 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
245 * - to force all locks when resource is destroyed (cleanup_resource());
247 * - during lock conversion (not used currently).
249 * If lock is not granted in the first case, this function waits until second
250 * or penultimate cases happen in some other thread.
253 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
255 /* XXX ALLOCATE - 160 bytes */
256 struct lock_wait_data lwd;
257 struct obd_device *obd;
258 struct obd_import *imp = NULL;
259 struct l_wait_info lwi;
264 if (flags == LDLM_FL_WAIT_NOREPROC) {
265 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
269 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
270 wake_up(&lock->l_waitq);
274 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
279 obd = class_exp2obd(lock->l_conn_export);
281 /* if this is a local lock, then there is no import */
283 imp = obd->u.cli.cl_import;
286 timeout = ldlm_cp_timeout(lock);
289 lock->l_last_activity = cfs_time_current_sec();
291 if (ldlm_is_no_timeout(lock)) {
292 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
293 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
295 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
296 ldlm_expired_completion_wait,
297 interrupted_completion_wait, &lwd);
301 spin_lock(&imp->imp_lock);
302 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
303 spin_unlock(&imp->imp_lock);
306 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
307 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
308 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
309 ldlm_set_fail_loc(lock);
312 /* Go to sleep until the lock is granted or cancelled. */
313 rc = l_wait_event(lock->l_waitq,
314 is_granted_or_cancelled(lock), &lwi);
318 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
323 RETURN(ldlm_completion_tail(lock, data));
325 EXPORT_SYMBOL(ldlm_completion_ast);
328 * A helper to build a blocking AST function
330 * Perform a common operation for blocking ASTs:
331 * defferred lock cancellation.
333 * \param lock the lock blocking or canceling AST was called on
335 * \see mdt_blocking_ast
336 * \see ldlm_blocking_ast
338 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
343 ldlm_set_cbpending(lock);
344 do_ast = (!lock->l_readers && !lock->l_writers);
345 unlock_res_and_lock(lock);
348 struct lustre_handle lockh;
351 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
352 ldlm_lock2handle(lock, &lockh);
353 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
355 CERROR("ldlm_cli_cancel: %d\n", rc);
357 LDLM_DEBUG(lock, "Lock still has references, will be "
362 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
365 * Server blocking AST
367 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
370 * \param lock the lock which blocks a request or cancelling lock
373 * \param flag indicates whether this cancelling or blocking callback
375 * \see ldlm_blocking_ast_nocheck
377 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
378 void *data, int flag)
382 if (flag == LDLM_CB_CANCELING) {
383 /* Don't need to do anything here. */
387 lock_res_and_lock(lock);
388 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
389 * that ldlm_blocking_ast is called just before intent_policy method
390 * takes the lr_lock, then by the time we get the lock, we might not
391 * be the correct blocking function anymore. So check, and return
393 if (lock->l_blocking_ast != ldlm_blocking_ast) {
394 unlock_res_and_lock(lock);
397 RETURN(ldlm_blocking_ast_nocheck(lock));
399 EXPORT_SYMBOL(ldlm_blocking_ast);
402 * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
404 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
405 * rather subtle: with OST-side locking, it may so happen that _all_ extent
406 * locks are held by the OST. If client wants to obtain the current file size
407 * it calls ll_glimpse_size(), and (as all locks are held only on the server),
408 * this dummy glimpse callback fires and does nothing. The client still
409 * receives the correct file size due to the following fragment of code in
410 * ldlm_cb_interpret():
412 * if (rc == -ELDLM_NO_LOCK_DATA) {
413 * LDLM_DEBUG(lock, "lost race - client has a lock but no"
415 * ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
418 * That is, after the glimpse returns this error, ofd_lvbo_update() is called
419 * and returns the updated file attributes from the inode to the client.
421 * See also comment in ofd_intent_policy() on why servers must set a non-NULL
422 * l_glimpse_ast when grabbing DLM locks. Otherwise, the server will assume
423 * that the object is in the process of being destroyed.
425 * \param[in] lock DLM lock being glimpsed, unused
426 * \param[in] reqp pointer to ptlrpc_request, unused
428 * \retval -ELDLM_NO_LOCK_DATA to get attributes from disk object
430 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
432 return -ELDLM_NO_LOCK_DATA;
436 * Enqueue a local lock (typically on a server).
438 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
439 const struct ldlm_res_id *res_id,
440 enum ldlm_type type, union ldlm_policy_data *policy,
441 enum ldlm_mode mode, __u64 *flags,
442 ldlm_blocking_callback blocking,
443 ldlm_completion_callback completion,
444 ldlm_glimpse_callback glimpse,
445 void *data, __u32 lvb_len, enum lvb_type lvb_type,
446 const __u64 *client_cookie,
447 struct lustre_handle *lockh)
449 struct ldlm_lock *lock;
451 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
452 .lcs_blocking = blocking,
453 .lcs_glimpse = glimpse,
457 LASSERT(!(*flags & LDLM_FL_REPLAY));
458 if (unlikely(ns_is_client(ns))) {
459 CERROR("Trying to enqueue local lock in a shadow namespace\n");
463 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
466 GOTO(out_nolock, err = PTR_ERR(lock));
468 err = ldlm_lvbo_init(lock->l_resource);
470 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
474 ldlm_lock2handle(lock, lockh);
476 /* NB: we don't have any lock now (lock_res_and_lock)
477 * because it's a new lock */
478 ldlm_lock_addref_internal_nolock(lock, mode);
479 ldlm_set_local(lock);
480 if (*flags & LDLM_FL_ATOMIC_CB)
481 ldlm_set_atomic_cb(lock);
484 lock->l_policy_data = *policy;
485 if (client_cookie != NULL)
486 lock->l_client_cookie = *client_cookie;
487 if (type == LDLM_EXTENT) {
488 /* extent lock without policy is a bug */
492 lock->l_req_extent = policy->l_extent;
495 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
496 if (unlikely(err != ELDLM_OK))
500 *policy = lock->l_policy_data;
502 if (lock->l_completion_ast)
503 lock->l_completion_ast(lock, *flags, NULL);
505 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
508 LDLM_LOCK_RELEASE(lock);
512 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
514 static void failed_lock_cleanup(struct ldlm_namespace *ns,
515 struct ldlm_lock *lock, int mode)
519 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
520 lock_res_and_lock(lock);
521 /* Check that lock is not granted or failed, we might race. */
522 if ((lock->l_req_mode != lock->l_granted_mode) &&
523 !ldlm_is_failed(lock)) {
524 /* Make sure that this lock will not be found by raced
525 * bl_ast and -EINVAL reply is sent to server anyways.
527 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
528 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
531 unlock_res_and_lock(lock);
535 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
536 "LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
538 LDLM_DEBUG(lock, "lock was granted or failed in race");
540 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
541 * from llite/file.c/ll_file_flock(). */
542 /* This code makes for the fact that we do not have blocking handler on
543 * a client for flock locks. As such this is the place where we must
544 * completely kill failed locks. (interrupted and those that
545 * were waiting to be granted when server evicted us. */
546 if (lock->l_resource->lr_type == LDLM_FLOCK) {
547 lock_res_and_lock(lock);
548 if (!ldlm_is_destroyed(lock)) {
549 ldlm_resource_unlink_lock(lock);
550 ldlm_lock_decref_internal_nolock(lock, mode);
551 ldlm_lock_destroy_nolock(lock);
553 unlock_res_and_lock(lock);
555 ldlm_lock_decref_internal(lock, mode);
560 * Finishing portion of client lock enqueue code.
562 * Called after receiving reply from server.
564 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
565 enum ldlm_type type, __u8 with_policy,
566 enum ldlm_mode mode, __u64 *flags, void *lvb,
567 __u32 lvb_len, const struct lustre_handle *lockh,
570 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
571 int is_replay = *flags & LDLM_FL_REPLAY;
572 struct ldlm_lock *lock;
573 struct ldlm_reply *reply;
574 int cleanup_phase = 1;
577 lock = ldlm_handle2lock(lockh);
578 /* ldlm_cli_enqueue is holding a reference on this lock. */
580 LASSERT(type == LDLM_FLOCK);
584 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
585 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
587 if (rc != ELDLM_OK) {
589 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
590 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
592 if (rc != ELDLM_LOCK_ABORTED)
596 /* Before we return, swab the reply */
597 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
599 GOTO(cleanup, rc = -EPROTO);
604 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
607 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
608 GOTO(cleanup, rc = size);
609 } else if (unlikely(size > lvb_len)) {
610 LDLM_ERROR(lock, "Replied LVB is larger than "
611 "expectation, expected = %d, replied = %d",
613 GOTO(cleanup, rc = -EINVAL);
618 if (rc == ELDLM_LOCK_ABORTED) {
619 if (lvb_len > 0 && lvb != NULL)
620 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
622 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
625 /* lock enqueued on the server */
628 lock_res_and_lock(lock);
629 /* Key change rehash lock in per-export hash with new key */
630 if (exp->exp_lock_hash) {
631 /* In the function below, .hs_keycmp resolves to
632 * ldlm_export_lock_keycmp() */
633 /* coverity[overrun-buffer-val] */
634 cfs_hash_rehash_key(exp->exp_lock_hash,
635 &lock->l_remote_handle,
639 lock->l_remote_handle = reply->lock_handle;
642 *flags = ldlm_flags_from_wire(reply->lock_flags);
643 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
644 LDLM_FL_INHERIT_MASK);
645 unlock_res_and_lock(lock);
647 CDEBUG(D_INFO, "local: %p, remote cookie: %#llx, flags: %#llx\n",
648 lock, reply->lock_handle.cookie, *flags);
650 /* If enqueue returned a blocked lock but the completion handler has
651 * already run, then it fixed up the resource and we don't need to do it
653 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
654 int newmode = reply->lock_desc.l_req_mode;
656 if (newmode && newmode != lock->l_req_mode) {
657 LDLM_DEBUG(lock, "server returned different mode %s",
658 ldlm_lockname[newmode]);
659 lock->l_req_mode = newmode;
662 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
663 &lock->l_resource->lr_name)) {
664 CDEBUG(D_INFO, "remote intent success, locking "DLDLMRES
665 " instead of "DLDLMRES"\n",
666 PLDLMRES(&reply->lock_desc.l_resource),
667 PLDLMRES(lock->l_resource));
669 rc = ldlm_lock_change_resource(ns, lock,
670 &reply->lock_desc.l_resource.lr_name);
671 if (rc || lock->l_resource == NULL)
672 GOTO(cleanup, rc = -ENOMEM);
673 LDLM_DEBUG(lock, "client-side enqueue, new resource");
676 if (!(type == LDLM_IBITS &&
677 !(exp_connect_flags(exp) & OBD_CONNECT_IBITS)))
678 /* We assume lock type cannot change on server*/
679 ldlm_convert_policy_to_local(exp,
680 lock->l_resource->lr_type,
681 &reply->lock_desc.l_policy_data,
682 &lock->l_policy_data);
683 if (type != LDLM_PLAIN)
684 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
687 if ((*flags) & LDLM_FL_AST_SENT) {
688 lock_res_and_lock(lock);
689 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
690 unlock_res_and_lock(lock);
691 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
694 /* If the lock has already been granted by a completion AST, don't
695 * clobber the LVB with an older one. */
697 /* We must lock or a racing completion might update lvb without
698 * letting us know and we'll clobber the correct value.
699 * Cannot unlock after the check either, a that still leaves
700 * a tiny window for completion to get in */
701 lock_res_and_lock(lock);
702 if (lock->l_req_mode != lock->l_granted_mode)
703 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
704 lock->l_lvb_data, lvb_len);
705 unlock_res_and_lock(lock);
713 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
714 if (lock->l_completion_ast != NULL) {
715 int err = lock->l_completion_ast(lock, *flags, NULL);
723 if (lvb_len > 0 && lvb != NULL) {
724 /* Copy the LVB here, and not earlier, because the completion
725 * AST (if any) can override what we got in the reply */
726 memcpy(lvb, lock->l_lvb_data, lvb_len);
729 LDLM_DEBUG(lock, "client-side enqueue END");
732 if (cleanup_phase == 1 && rc)
733 failed_lock_cleanup(ns, lock, mode);
734 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
736 LDLM_LOCK_RELEASE(lock);
739 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
742 * Estimate number of lock handles that would fit into request of given
743 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
744 * a single page on the send/receive side. XXX: 512 should be changed to
745 * more adequate value.
747 static inline int ldlm_req_handles_avail(int req_size, int off)
751 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
752 if (likely(avail >= 0))
753 avail /= (int)sizeof(struct lustre_handle);
756 avail += LDLM_LOCKREQ_HANDLES - off;
761 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
762 enum req_location loc,
765 __u32 size = req_capsule_msg_size(pill, loc);
766 return ldlm_req_handles_avail(size, off);
769 static inline int ldlm_format_handles_avail(struct obd_import *imp,
770 const struct req_format *fmt,
771 enum req_location loc, int off)
773 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
774 return ldlm_req_handles_avail(size, off);
778 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
779 * \a count locks in \a cancels.
781 * This is to be called by functions preparing their own requests that
782 * might contain lists of locks to cancel in addition to actual operation
783 * that needs to be performed.
785 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
786 int version, int opc, int canceloff,
787 struct list_head *cancels, int count)
789 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
790 struct req_capsule *pill = &req->rq_pill;
791 struct ldlm_request *dlm = NULL;
792 struct list_head head = LIST_HEAD_INIT(head);
793 enum ldlm_lru_flags lru_flags;
794 int avail, to_free, pack = 0;
800 if (ns_connect_cancelset(ns)) {
801 /* Estimate the amount of available space in the request. */
802 req_capsule_filled_sizes(pill, RCL_CLIENT);
803 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
805 lru_flags = ns_connect_lru_resize(ns) ?
806 LDLM_LRU_FLAG_LRUR_NO_WAIT : LDLM_LRU_FLAG_AGED;
807 to_free = !ns_connect_lru_resize(ns) &&
808 opc == LDLM_ENQUEUE ? 1 : 0;
810 /* Cancel LRU locks here _only_ if the server supports
811 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
812 * RPC, which will make us slower. */
814 count += ldlm_cancel_lru_local(ns, cancels, to_free,
821 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
822 ldlm_request_bufsize(pack, opc));
825 rc = ptlrpc_request_pack(req, version, opc);
827 ldlm_lock_list_put(cancels, l_bl_ast, count);
831 if (ns_connect_cancelset(ns)) {
833 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
835 /* Skip first lock handler in ldlm_request_pack(),
836 * this method will increment @lock_count according
837 * to the lock handle amount actually written to
839 dlm->lock_count = canceloff;
841 /* Pack into the request @pack lock handles. */
842 ldlm_cli_cancel_list(cancels, pack, req, 0);
843 /* Prepare and send separate cancel RPC for others. */
844 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
846 ldlm_lock_list_put(cancels, l_bl_ast, count);
850 EXPORT_SYMBOL(ldlm_prep_elc_req);
852 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
853 struct list_head *cancels, int count)
855 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
856 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
858 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
860 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
862 struct ptlrpc_request *req;
866 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
868 RETURN(ERR_PTR(-ENOMEM));
870 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
872 ptlrpc_request_free(req);
876 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
877 ptlrpc_request_set_replen(req);
880 EXPORT_SYMBOL(ldlm_enqueue_pack);
883 * Client-side lock enqueue.
885 * If a request has some specific initialisation it is passed in \a reqp,
886 * otherwise it is created in ldlm_cli_enqueue.
888 * Supports sync and async requests, pass \a async flag accordingly. If a
889 * request was created in ldlm_cli_enqueue and it is the async request,
890 * pass it to the caller in \a reqp.
892 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
893 struct ldlm_enqueue_info *einfo,
894 const struct ldlm_res_id *res_id,
895 union ldlm_policy_data const *policy, __u64 *flags,
896 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
897 struct lustre_handle *lockh, int async)
899 struct ldlm_namespace *ns;
900 struct ldlm_lock *lock;
901 struct ldlm_request *body;
902 int is_replay = *flags & LDLM_FL_REPLAY;
903 int req_passed_in = 1;
905 struct ptlrpc_request *req;
908 LASSERT(exp != NULL);
910 ns = exp->exp_obd->obd_namespace;
912 /* If we're replaying this lock, just check some invariants.
913 * If we're creating a new lock, get everything all setup nice. */
915 lock = ldlm_handle2lock_long(lockh, 0);
916 LASSERT(lock != NULL);
917 LDLM_DEBUG(lock, "client-side enqueue START");
918 LASSERT(exp == lock->l_conn_export);
920 const struct ldlm_callback_suite cbs = {
921 .lcs_completion = einfo->ei_cb_cp,
922 .lcs_blocking = einfo->ei_cb_bl,
923 .lcs_glimpse = einfo->ei_cb_gl
925 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
926 einfo->ei_mode, &cbs, einfo->ei_cbdata,
929 RETURN(PTR_ERR(lock));
930 /* for the local lock, add the reference */
931 ldlm_lock_addref_internal(lock, einfo->ei_mode);
932 ldlm_lock2handle(lock, lockh);
934 lock->l_policy_data = *policy;
936 if (einfo->ei_type == LDLM_EXTENT) {
937 /* extent lock without policy is a bug */
941 lock->l_req_extent = policy->l_extent;
943 LDLM_DEBUG(lock, "client-side enqueue START, flags %#llx",
947 lock->l_conn_export = exp;
948 lock->l_export = NULL;
949 lock->l_blocking_ast = einfo->ei_cb_bl;
950 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL));
951 lock->l_last_activity = cfs_time_current_sec();
953 /* lock not sent to server yet */
954 if (reqp == NULL || *reqp == NULL) {
955 req = ldlm_enqueue_pack(exp, lvb_len);
957 failed_lock_cleanup(ns, lock, einfo->ei_mode);
958 LDLM_LOCK_RELEASE(lock);
959 RETURN(PTR_ERR(req));
969 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
971 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
972 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
975 /* Dump lock data into the request buffer */
976 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
977 ldlm_lock2desc(lock, &body->lock_desc);
978 body->lock_flags = ldlm_flags_to_wire(*flags);
979 body->lock_handle[0] = *lockh;
982 LASSERT(reqp != NULL);
986 LDLM_DEBUG(lock, "sending request");
988 rc = ptlrpc_queue_wait(req);
990 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
991 einfo->ei_mode, flags, lvb, lvb_len,
994 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
995 * one reference that we took */
997 LDLM_LOCK_RELEASE(lock);
1001 if (!req_passed_in && req != NULL) {
1002 ptlrpc_req_finished(req);
1009 EXPORT_SYMBOL(ldlm_cli_enqueue);
1011 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
1014 struct ldlm_resource *res;
1017 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1018 CERROR("Trying to cancel local lock\n");
1021 LDLM_DEBUG(lock, "client-side local convert");
1023 res = ldlm_lock_convert(lock, new_mode, flags);
1025 ldlm_reprocess_all(res);
1028 rc = LUSTRE_EDEADLK;
1030 LDLM_DEBUG(lock, "client-side local convert handler END");
1031 LDLM_LOCK_PUT(lock);
1035 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
1036 * conversion of locks which are on the waiting or converting queue */
1037 /* Caller of this code is supposed to take care of lock readers/writers
1039 int ldlm_cli_convert(const struct lustre_handle *lockh, int new_mode,
1042 struct ldlm_request *body;
1043 struct ldlm_reply *reply;
1044 struct ldlm_lock *lock;
1045 struct ldlm_resource *res;
1046 struct ptlrpc_request *req;
1050 lock = ldlm_handle2lock(lockh);
1057 if (lock->l_conn_export == NULL)
1058 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
1060 LDLM_DEBUG(lock, "client-side convert");
1062 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
1063 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1066 LDLM_LOCK_PUT(lock);
1070 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1071 body->lock_handle[0] = lock->l_remote_handle;
1073 body->lock_desc.l_req_mode = new_mode;
1074 body->lock_flags = ldlm_flags_to_wire(*flags);
1077 ptlrpc_request_set_replen(req);
1078 rc = ptlrpc_queue_wait(req);
1082 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1084 GOTO(out, rc = -EPROTO);
1087 GOTO(out, rc = req->rq_status);
1089 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
1091 ldlm_reprocess_all(res);
1092 /* Go to sleep until the lock is granted. */
1093 /* FIXME: or cancelled. */
1094 if (lock->l_completion_ast) {
1095 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1101 rc = LUSTRE_EDEADLK;
1105 LDLM_LOCK_PUT(lock);
1106 ptlrpc_req_finished(req);
1111 * Cancel locks locally.
1113 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1114 * \retval LDLM_FL_CANCELING otherwise;
1115 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1117 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1119 __u64 rc = LDLM_FL_LOCAL_ONLY;
1122 if (lock->l_conn_export) {
1125 LDLM_DEBUG(lock, "client-side cancel");
1126 /* Set this flag to prevent others from getting new references*/
1127 lock_res_and_lock(lock);
1128 ldlm_set_cbpending(lock);
1129 local_only = !!(lock->l_flags &
1130 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1131 ldlm_cancel_callback(lock);
1132 rc = (ldlm_is_bl_ast(lock)) ?
1133 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1134 unlock_res_and_lock(lock);
1137 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1139 rc = LDLM_FL_LOCAL_ONLY;
1141 ldlm_lock_cancel(lock);
1143 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1144 LDLM_ERROR(lock, "Trying to cancel local lock");
1147 LDLM_DEBUG(lock, "server-side local cancel");
1148 ldlm_lock_cancel(lock);
1149 ldlm_reprocess_all(lock->l_resource);
1156 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1158 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1159 struct list_head *head, int count)
1161 struct ldlm_request *dlm;
1162 struct ldlm_lock *lock;
1163 int max, packed = 0;
1166 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1167 LASSERT(dlm != NULL);
1169 /* Check the room in the request buffer. */
1170 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1171 sizeof(struct ldlm_request);
1172 max /= sizeof(struct lustre_handle);
1173 max += LDLM_LOCKREQ_HANDLES;
1174 LASSERT(max >= dlm->lock_count + count);
1176 /* XXX: it would be better to pack lock handles grouped by resource.
1177 * so that the server cancel would call filter_lvbo_update() less
1179 list_for_each_entry(lock, head, l_bl_ast) {
1182 LASSERT(lock->l_conn_export);
1183 /* Pack the lock handle to the given request buffer. */
1184 LDLM_DEBUG(lock, "packing");
1185 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1188 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1193 * Prepare and send a batched cancel RPC. It will include \a count lock
1194 * handles of locks given in \a cancels list. */
1195 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1196 int count, enum ldlm_cancel_flags flags)
1198 struct ptlrpc_request *req = NULL;
1199 struct obd_import *imp;
1204 LASSERT(exp != NULL);
1207 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1209 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1212 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1213 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1218 imp = class_exp2cliimp(exp);
1219 if (imp == NULL || imp->imp_invalid) {
1221 "skipping cancel on invalid import %p\n", imp);
1225 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1227 GOTO(out, rc = -ENOMEM);
1229 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1230 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1231 ldlm_request_bufsize(count, LDLM_CANCEL));
1233 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1235 ptlrpc_request_free(req);
1239 /* If OSP want cancel cross-MDT lock, let's not block it in
1240 * in recovery, otherwise the lock will not released, if
1241 * the remote target is also in recovery, and it also need
1242 * this lock, it might cause deadlock. */
1243 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1244 exp->exp_obd->obd_lu_dev != NULL &&
1245 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1246 struct lu_device *top_dev;
1248 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1249 if (top_dev != NULL &&
1250 top_dev->ld_obd->obd_recovering)
1251 req->rq_allow_replay = 1;
1254 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1255 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1256 ptlrpc_at_set_req_timeout(req);
1258 ldlm_cancel_pack(req, cancels, count);
1260 ptlrpc_request_set_replen(req);
1261 if (flags & LCF_ASYNC) {
1262 ptlrpcd_add_req(req);
1267 rc = ptlrpc_queue_wait(req);
1268 if (rc == LUSTRE_ESTALE) {
1269 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1270 "out of sync -- not fatal\n",
1271 libcfs_nid2str(req->rq_import->
1272 imp_connection->c_peer.nid));
1274 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1275 req->rq_import_generation == imp->imp_generation) {
1276 ptlrpc_req_finished(req);
1278 } else if (rc != ELDLM_OK) {
1279 /* -ESHUTDOWN is common on umount */
1280 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1281 "Got rc %d from cancel RPC: "
1282 "canceling anyway\n", rc);
1289 ptlrpc_req_finished(req);
1292 return sent ? sent : rc;
1295 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1297 LASSERT(imp != NULL);
1298 return &imp->imp_obd->obd_namespace->ns_pool;
1302 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1304 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1306 struct obd_device *obd;
1310 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1311 !imp_connect_lru_resize(req->rq_import)))
1314 * Do nothing for corner cases.
1319 /* In some cases RPC may contain SLV and limit zeroed out. This
1320 * is the case when server does not support LRU resize feature.
1321 * This is also possible in some recovery cases when server-side
1322 * reqs have no reference to the OBD export and thus access to
1323 * server-side namespace is not possible. */
1324 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1325 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1326 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1327 "(SLV: %llu, Limit: %u)",
1328 lustre_msg_get_slv(req->rq_repmsg),
1329 lustre_msg_get_limit(req->rq_repmsg));
1333 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1334 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1335 obd = req->rq_import->imp_obd;
1337 /* Set new SLV and limit in OBD fields to make them accessible
1338 * to the pool thread. We do not access obd_namespace and pool
1339 * directly here as there is no reliable way to make sure that
1340 * they are still alive at cleanup time. Evil races are possible
1341 * which may cause Oops at that time. */
1342 write_lock(&obd->obd_pool_lock);
1343 obd->obd_pool_slv = new_slv;
1344 obd->obd_pool_limit = new_limit;
1345 write_unlock(&obd->obd_pool_lock);
1351 * Client side lock cancel.
1353 * Lock must not have any readers or writers by this time.
1355 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1356 enum ldlm_cancel_flags cancel_flags)
1358 struct obd_export *exp;
1359 enum ldlm_lru_flags lru_flags;
1360 int avail, count = 1;
1362 struct ldlm_namespace *ns;
1363 struct ldlm_lock *lock;
1364 struct list_head cancels = LIST_HEAD_INIT(cancels);
1367 lock = ldlm_handle2lock_long(lockh, 0);
1369 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1373 lock_res_and_lock(lock);
1374 /* Lock is being canceled and the caller doesn't want to wait */
1375 if (ldlm_is_canceling(lock) && (cancel_flags & LCF_ASYNC)) {
1376 unlock_res_and_lock(lock);
1377 LDLM_LOCK_RELEASE(lock);
1381 ldlm_set_canceling(lock);
1382 unlock_res_and_lock(lock);
1384 rc = ldlm_cli_cancel_local(lock);
1385 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1386 LDLM_LOCK_RELEASE(lock);
1389 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1390 * RPC which goes to canceld portal, so we can cancel other LRU locks
1391 * here and send them all as one LDLM_CANCEL RPC. */
1392 LASSERT(list_empty(&lock->l_bl_ast));
1393 list_add(&lock->l_bl_ast, &cancels);
1395 exp = lock->l_conn_export;
1396 if (exp_connect_cancelset(exp)) {
1397 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1402 ns = ldlm_lock_to_ns(lock);
1403 lru_flags = ns_connect_lru_resize(ns) ?
1404 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1405 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1406 LCF_BL_AST, lru_flags);
1408 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1411 EXPORT_SYMBOL(ldlm_cli_cancel);
1414 * Locally cancel up to \a count locks in list \a cancels.
1415 * Return the number of cancelled locks.
1417 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1418 enum ldlm_cancel_flags cancel_flags)
1420 struct list_head head = LIST_HEAD_INIT(head);
1421 struct ldlm_lock *lock, *next;
1422 int left = 0, bl_ast = 0;
1426 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1430 if (cancel_flags & LCF_LOCAL) {
1431 rc = LDLM_FL_LOCAL_ONLY;
1432 ldlm_lock_cancel(lock);
1434 rc = ldlm_cli_cancel_local(lock);
1436 /* Until we have compound requests and can send LDLM_CANCEL
1437 * requests batched with generic RPCs, we need to send cancels
1438 * with the LDLM_FL_BL_AST flag in a separate RPC from
1439 * the one being generated now. */
1440 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1441 LDLM_DEBUG(lock, "Cancel lock separately");
1442 list_del_init(&lock->l_bl_ast);
1443 list_add(&lock->l_bl_ast, &head);
1447 if (rc == LDLM_FL_LOCAL_ONLY) {
1448 /* CANCEL RPC should not be sent to server. */
1449 list_del_init(&lock->l_bl_ast);
1450 LDLM_LOCK_RELEASE(lock);
1456 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1463 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1464 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1465 * readahead requests, ...)
1467 static enum ldlm_policy_res
1468 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1469 int unused, int added, int count)
1471 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1473 /* don't check added & count since we want to process all locks
1475 * It's fine to not take lock to access lock->l_resource since
1476 * the lock has already been granted so it won't change. */
1477 switch (lock->l_resource->lr_type) {
1480 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1483 result = LDLM_POLICY_SKIP_LOCK;
1484 lock_res_and_lock(lock);
1485 ldlm_set_skipped(lock);
1486 unlock_res_and_lock(lock);
1494 * Callback function for LRU-resize policy. Decides whether to keep
1495 * \a lock in LRU for current \a LRU size \a unused, added in current
1496 * scan \a added and number of locks to be preferably canceled \a count.
1498 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1500 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1502 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1503 struct ldlm_lock *lock,
1504 int unused, int added,
1507 cfs_time_t cur = cfs_time_current();
1508 struct ldlm_pool *pl = &ns->ns_pool;
1512 /* Stop LRU processing when we reach past @count or have checked all
1514 if (count && added >= count)
1515 return LDLM_POLICY_KEEP_LOCK;
1517 /* Despite of the LV, It doesn't make sense to keep the lock which
1518 * is unused for ns_max_age time. */
1519 if (cfs_time_after(cfs_time_current(),
1520 cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1521 return LDLM_POLICY_CANCEL_LOCK;
1523 slv = ldlm_pool_get_slv(pl);
1524 lvf = ldlm_pool_get_lvf(pl);
1525 la = cfs_duration_sec(cfs_time_sub(cur,
1526 lock->l_last_used));
1527 lv = lvf * la * unused;
1529 /* Inform pool about current CLV to see it via proc. */
1530 ldlm_pool_set_clv(pl, lv);
1532 /* Stop when SLV is not yet come from server or lv is smaller than
1534 if (slv == 0 || lv < slv)
1535 return LDLM_POLICY_KEEP_LOCK;
1537 return LDLM_POLICY_CANCEL_LOCK;
1540 static enum ldlm_policy_res
1541 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1542 struct ldlm_lock *lock,
1543 int unused, int added,
1546 enum ldlm_policy_res result;
1548 result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1549 if (result == LDLM_POLICY_KEEP_LOCK)
1552 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1556 * Callback function for proc used policy. Makes decision whether to keep
1557 * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1558 * added and number of locks to be preferably canceled \a count.
1560 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1562 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1564 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1565 struct ldlm_lock *lock,
1566 int unused, int added,
1569 /* Stop LRU processing when we reach past @count or have checked all
1571 return (added >= count) ?
1572 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1576 * Callback function for aged policy. Makes decision whether to keep \a lock in
1577 * LRU for current LRU size \a unused, added in current scan \a added and
1578 * number of locks to be preferably canceled \a count.
1580 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1582 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1584 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1585 struct ldlm_lock *lock,
1586 int unused, int added,
1589 if ((added >= count) &&
1590 cfs_time_before(cfs_time_current(),
1591 cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1592 return LDLM_POLICY_KEEP_LOCK;
1594 return LDLM_POLICY_CANCEL_LOCK;
1598 * Callback function for default policy. Makes decision whether to keep \a lock
1599 * in LRU for current LRU size \a unused, added in current scan \a added and
1600 * number of locks to be preferably canceled \a count.
1602 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1604 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1607 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1608 struct ldlm_lock *lock,
1609 int unused, int added,
1612 /* Stop LRU processing when we reach past count or have checked all
1614 return (added >= count) ?
1615 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1618 typedef enum ldlm_policy_res
1619 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1620 int unused, int added, int count);
1622 static ldlm_cancel_lru_policy_t
1623 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1625 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1626 return ldlm_cancel_no_wait_policy;
1628 if (ns_connect_lru_resize(ns)) {
1629 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1630 /* We kill passed number of old locks. */
1631 return ldlm_cancel_passed_policy;
1632 if (lru_flags & LDLM_LRU_FLAG_LRUR)
1633 return ldlm_cancel_lrur_policy;
1634 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1635 return ldlm_cancel_passed_policy;
1636 else if (lru_flags & LDLM_LRU_FLAG_LRUR_NO_WAIT)
1637 return ldlm_cancel_lrur_no_wait_policy;
1639 if (lru_flags & LDLM_LRU_FLAG_AGED)
1640 return ldlm_cancel_aged_policy;
1643 return ldlm_cancel_default_policy;
1647 * - Free space in LRU for \a count new locks,
1648 * redundant unused locks are canceled locally;
1649 * - also cancel locally unused aged locks;
1650 * - do not cancel more than \a max locks;
1651 * - GET the found locks and add them into the \a cancels list.
1653 * A client lock can be added to the l_bl_ast list only when it is
1654 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1655 * CANCEL. There are the following use cases:
1656 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1657 * ldlm_cli_cancel(), which check and set this flag properly. As any
1658 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1659 * later without any special locking.
1661 * Calling policies for enabled LRU resize:
1662 * ----------------------------------------
1663 * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1664 * cancel not more than \a count locks;
1666 * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1667 * at the beginning of LRU list);
1669 * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1670 * to memory pressre policy function;
1672 * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1674 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1675 * (typically before replaying locks) w/o
1676 * sending any RPCs or waiting for any
1677 * outstanding RPC to complete.
1679 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1680 struct list_head *cancels, int count, int max,
1681 enum ldlm_lru_flags lru_flags)
1683 ldlm_cancel_lru_policy_t pf;
1684 struct ldlm_lock *lock, *next;
1685 int added = 0, unused, remained;
1686 int no_wait = lru_flags & (LDLM_LRU_FLAG_NO_WAIT |
1687 LDLM_LRU_FLAG_LRUR_NO_WAIT);
1690 spin_lock(&ns->ns_lock);
1691 unused = ns->ns_nr_unused;
1694 if (!ns_connect_lru_resize(ns))
1695 count += unused - ns->ns_max_unused;
1697 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1698 LASSERT(pf != NULL);
1700 while (!list_empty(&ns->ns_unused_list)) {
1701 enum ldlm_policy_res result;
1702 cfs_time_t last_use = 0;
1704 /* all unused locks */
1705 if (remained-- <= 0)
1708 /* For any flags, stop scanning if @max is reached. */
1709 if (max && added >= max)
1712 list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1714 /* No locks which got blocking requests. */
1715 LASSERT(!ldlm_is_bl_ast(lock));
1717 if (no_wait && ldlm_is_skipped(lock))
1718 /* already processed */
1721 last_use = lock->l_last_used;
1722 if (last_use == cfs_time_current())
1725 /* Somebody is already doing CANCEL. No need for this
1726 * lock in LRU, do not traverse it again. */
1727 if (!ldlm_is_canceling(lock))
1730 ldlm_lock_remove_from_lru_nolock(lock);
1732 if (&lock->l_lru == &ns->ns_unused_list)
1735 LDLM_LOCK_GET(lock);
1736 spin_unlock(&ns->ns_lock);
1737 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1739 /* Pass the lock through the policy filter and see if it
1740 * should stay in LRU.
1742 * Even for shrinker policy we stop scanning if
1743 * we find a lock that should stay in the cache.
1744 * We should take into account lock age anyway
1745 * as a new lock is a valuable resource even if
1746 * it has a low weight.
1748 * That is, for shrinker policy we drop only
1749 * old locks, but additionally choose them by
1750 * their weight. Big extent locks will stay in
1752 result = pf(ns, lock, unused, added, count);
1753 if (result == LDLM_POLICY_KEEP_LOCK) {
1754 lu_ref_del(&lock->l_reference,
1755 __FUNCTION__, current);
1756 LDLM_LOCK_RELEASE(lock);
1757 spin_lock(&ns->ns_lock);
1760 if (result == LDLM_POLICY_SKIP_LOCK) {
1761 lu_ref_del(&lock->l_reference,
1763 LDLM_LOCK_RELEASE(lock);
1764 spin_lock(&ns->ns_lock);
1768 lock_res_and_lock(lock);
1769 /* Check flags again under the lock. */
1770 if (ldlm_is_canceling(lock) ||
1771 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1772 /* Another thread is removing lock from LRU, or
1773 * somebody is already doing CANCEL, or there
1774 * is a blocking request which will send cancel
1775 * by itself, or the lock is no longer unused or
1776 * the lock has been used since the pf() call and
1777 * pages could be put under it. */
1778 unlock_res_and_lock(lock);
1779 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1780 LDLM_LOCK_RELEASE(lock);
1781 spin_lock(&ns->ns_lock);
1784 LASSERT(!lock->l_readers && !lock->l_writers);
1786 /* If we have chosen to cancel this lock voluntarily, we
1787 * better send cancel notification to server, so that it
1788 * frees appropriate state. This might lead to a race
1789 * where while we are doing cancel here, server is also
1790 * silently cancelling this lock. */
1791 ldlm_clear_cancel_on_block(lock);
1793 /* Setting the CBPENDING flag is a little misleading,
1794 * but prevents an important race; namely, once
1795 * CBPENDING is set, the lock can accumulate no more
1796 * readers/writers. Since readers and writers are
1797 * already zero here, ldlm_lock_decref() won't see
1798 * this flag and call l_blocking_ast */
1799 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1801 /* We can't re-add to l_lru as it confuses the
1802 * refcounting in ldlm_lock_remove_from_lru() if an AST
1803 * arrives after we drop lr_lock below. We use l_bl_ast
1804 * and can't use l_pending_chain as it is used both on
1805 * server and client nevertheless bug 5666 says it is
1806 * used only on server */
1807 LASSERT(list_empty(&lock->l_bl_ast));
1808 list_add(&lock->l_bl_ast, cancels);
1809 unlock_res_and_lock(lock);
1810 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1811 spin_lock(&ns->ns_lock);
1815 spin_unlock(&ns->ns_lock);
1819 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1821 enum ldlm_cancel_flags cancel_flags,
1822 enum ldlm_lru_flags lru_flags)
1826 added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
1830 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1834 * Cancel at least \a nr locks from given namespace LRU.
1836 * When called with LCF_ASYNC the blocking callback will be handled
1837 * in a thread and this function will return after the thread has been
1838 * asked to call the callback. When called with LCF_ASYNC the blocking
1839 * callback will be performed in this function.
1841 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
1842 enum ldlm_cancel_flags cancel_flags,
1843 enum ldlm_lru_flags lru_flags)
1845 struct list_head cancels = LIST_HEAD_INIT(cancels);
1849 /* Just prepare the list of locks, do not actually cancel them yet.
1850 * Locks are cancelled later in a separate thread. */
1851 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
1852 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
1860 * Find and cancel locally unused locks found on resource, matched to the
1861 * given policy, mode. GET the found locks and add them into the \a cancels
1864 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1865 struct list_head *cancels,
1866 union ldlm_policy_data *policy,
1867 enum ldlm_mode mode, __u64 lock_flags,
1868 enum ldlm_cancel_flags cancel_flags,
1871 struct ldlm_lock *lock;
1876 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1877 if (opaque != NULL && lock->l_ast_data != opaque) {
1878 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1879 lock->l_ast_data, opaque);
1884 if (lock->l_readers || lock->l_writers)
1887 /* If somebody is already doing CANCEL, or blocking AST came,
1888 * skip this lock. */
1889 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
1892 if (lockmode_compat(lock->l_granted_mode, mode))
1895 /* If policy is given and this is IBITS lock, add to list only
1896 * those locks that match by policy. */
1897 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1898 !(lock->l_policy_data.l_inodebits.bits &
1899 policy->l_inodebits.bits))
1902 /* See CBPENDING comment in ldlm_cancel_lru */
1903 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1906 LASSERT(list_empty(&lock->l_bl_ast));
1907 list_add(&lock->l_bl_ast, cancels);
1908 LDLM_LOCK_GET(lock);
1913 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1915 EXPORT_SYMBOL(ldlm_cancel_resource_local);
1918 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
1920 * If \a req is NULL, send CANCEL request to server with handles of locks
1921 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1922 * separately per lock.
1923 * If \a req is not NULL, put handles of locks in \a cancels into the request
1924 * buffer at the offset \a off.
1925 * Destroy \a cancels at the end.
1927 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1928 struct ptlrpc_request *req,
1929 enum ldlm_cancel_flags flags)
1931 struct ldlm_lock *lock;
1935 if (list_empty(cancels) || count == 0)
1938 /* XXX: requests (both batched and not) could be sent in parallel.
1939 * Usually it is enough to have just 1 RPC, but it is possible that
1940 * there are too many locks to be cancelled in LRU or on a resource.
1941 * It would also speed up the case when the server does not support
1944 LASSERT(!list_empty(cancels));
1945 lock = list_entry(cancels->next, struct ldlm_lock,
1947 LASSERT(lock->l_conn_export);
1949 if (exp_connect_cancelset(lock->l_conn_export)) {
1952 ldlm_cancel_pack(req, cancels, count);
1954 res = ldlm_cli_cancel_req(lock->l_conn_export,
1958 res = ldlm_cli_cancel_req(lock->l_conn_export,
1963 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1964 "ldlm_cli_cancel_list: %d\n", res);
1969 ldlm_lock_list_put(cancels, l_bl_ast, res);
1971 LASSERT(count == 0);
1974 EXPORT_SYMBOL(ldlm_cli_cancel_list);
1977 * Cancel all locks on a resource that have 0 readers/writers.
1979 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1980 * to notify the server. */
1981 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1982 const struct ldlm_res_id *res_id,
1983 union ldlm_policy_data *policy,
1984 enum ldlm_mode mode,
1985 enum ldlm_cancel_flags flags, void *opaque)
1987 struct ldlm_resource *res;
1988 struct list_head cancels = LIST_HEAD_INIT(cancels);
1993 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1995 /* This is not a problem. */
1996 CDEBUG(D_INFO, "No resource %llu\n", res_id->name[0]);
2000 LDLM_RESOURCE_ADDREF(res);
2001 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
2002 0, flags | LCF_BL_AST, opaque);
2003 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
2005 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
2008 LDLM_RESOURCE_DELREF(res);
2009 ldlm_resource_putref(res);
2012 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2014 struct ldlm_cli_cancel_arg {
2020 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2021 struct hlist_node *hnode, void *arg)
2023 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2024 struct ldlm_cli_cancel_arg *lc = arg;
2026 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2027 NULL, LCK_MINMODE, lc->lc_flags,
2029 /* must return 0 for hash iteration */
2034 * Cancel all locks on a namespace (or a specific resource, if given)
2035 * that have 0 readers/writers.
2037 * If flags & LCF_LOCAL, throw the locks away without trying
2038 * to notify the server. */
2039 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2040 const struct ldlm_res_id *res_id,
2041 enum ldlm_cancel_flags flags, void *opaque)
2043 struct ldlm_cli_cancel_arg arg = {
2045 .lc_opaque = opaque,
2053 if (res_id != NULL) {
2054 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2058 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2059 ldlm_cli_hash_cancel_unused, &arg, 0);
2064 /* Lock iterators. */
2066 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2069 struct list_head *tmp, *next;
2070 struct ldlm_lock *lock;
2071 int rc = LDLM_ITER_CONTINUE;
2076 RETURN(LDLM_ITER_CONTINUE);
2079 list_for_each_safe(tmp, next, &res->lr_granted) {
2080 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2082 if (iter(lock, closure) == LDLM_ITER_STOP)
2083 GOTO(out, rc = LDLM_ITER_STOP);
2086 list_for_each_safe(tmp, next, &res->lr_converting) {
2087 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2089 if (iter(lock, closure) == LDLM_ITER_STOP)
2090 GOTO(out, rc = LDLM_ITER_STOP);
2093 list_for_each_safe(tmp, next, &res->lr_waiting) {
2094 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2096 if (iter(lock, closure) == LDLM_ITER_STOP)
2097 GOTO(out, rc = LDLM_ITER_STOP);
2104 struct iter_helper_data {
2105 ldlm_iterator_t iter;
2109 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2111 struct iter_helper_data *helper = closure;
2112 return helper->iter(lock, helper->closure);
2115 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2116 struct hlist_node *hnode, void *arg)
2119 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2121 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2125 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2126 ldlm_iterator_t iter, void *closure)
2129 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2131 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2132 ldlm_res_iter_helper, &helper, 0);
2136 /* non-blocking function to manipulate a lock whose cb_data is being put away.
2137 * return 0: find no resource
2138 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2141 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2142 const struct ldlm_res_id *res_id,
2143 ldlm_iterator_t iter, void *data)
2145 struct ldlm_resource *res;
2149 LASSERTF(ns != NULL, "must pass in namespace\n");
2151 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2155 LDLM_RESOURCE_ADDREF(res);
2156 rc = ldlm_resource_foreach(res, iter, data);
2157 LDLM_RESOURCE_DELREF(res);
2158 ldlm_resource_putref(res);
2161 EXPORT_SYMBOL(ldlm_resource_iterate);
2165 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2167 struct list_head *list = closure;
2169 /* we use l_pending_chain here, because it's unused on clients. */
2170 LASSERTF(list_empty(&lock->l_pending_chain),
2171 "lock %p next %p prev %p\n",
2172 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2173 /* bug 9573: don't replay locks left after eviction, or
2174 * bug 17614: locks being actively cancelled. Get a reference
2175 * on a lock so that it does not disapear under us (e.g. due to cancel)
2177 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_BL_DONE))) {
2178 list_add(&lock->l_pending_chain, list);
2179 LDLM_LOCK_GET(lock);
2182 return LDLM_ITER_CONTINUE;
2185 static int replay_lock_interpret(const struct lu_env *env,
2186 struct ptlrpc_request *req,
2187 struct ldlm_async_args *aa, int rc)
2189 struct ldlm_lock *lock;
2190 struct ldlm_reply *reply;
2191 struct obd_export *exp;
2194 atomic_dec(&req->rq_import->imp_replay_inflight);
2198 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2200 GOTO(out, rc = -EPROTO);
2202 lock = ldlm_handle2lock(&aa->lock_handle);
2204 CERROR("received replay ack for unknown local cookie %#llx"
2205 " remote cookie %#llx from server %s id %s\n",
2206 aa->lock_handle.cookie, reply->lock_handle.cookie,
2207 req->rq_export->exp_client_uuid.uuid,
2208 libcfs_id2str(req->rq_peer));
2209 GOTO(out, rc = -ESTALE);
2212 /* Key change rehash lock in per-export hash with new key */
2213 exp = req->rq_export;
2214 if (exp && exp->exp_lock_hash) {
2215 /* In the function below, .hs_keycmp resolves to
2216 * ldlm_export_lock_keycmp() */
2217 /* coverity[overrun-buffer-val] */
2218 cfs_hash_rehash_key(exp->exp_lock_hash,
2219 &lock->l_remote_handle,
2220 &reply->lock_handle,
2223 lock->l_remote_handle = reply->lock_handle;
2226 LDLM_DEBUG(lock, "replayed lock:");
2227 ptlrpc_import_recovery_state_machine(req->rq_import);
2228 LDLM_LOCK_PUT(lock);
2231 ptlrpc_connect_import(req->rq_import);
2236 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2238 struct ptlrpc_request *req;
2239 struct ldlm_async_args *aa;
2240 struct ldlm_request *body;
2245 /* Bug 11974: Do not replay a lock which is actively being canceled */
2246 if (ldlm_is_bl_done(lock)) {
2247 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2251 /* If this is reply-less callback lock, we cannot replay it, since
2252 * server might have long dropped it, but notification of that event was
2253 * lost by network. (and server granted conflicting lock already) */
2254 if (ldlm_is_cancel_on_block(lock)) {
2255 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2256 ldlm_lock_cancel(lock);
2261 * If granted mode matches the requested mode, this lock is granted.
2263 * If they differ, but we have a granted mode, then we were granted
2264 * one mode and now want another: ergo, converting.
2266 * If we haven't been granted anything and are on a resource list,
2267 * then we're blocked/waiting.
2269 * If we haven't been granted anything and we're NOT on a resource list,
2270 * then we haven't got a reply yet and don't have a known disposition.
2271 * This happens whenever a lock enqueue is the request that triggers
2274 if (lock->l_granted_mode == lock->l_req_mode)
2275 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2276 else if (lock->l_granted_mode)
2277 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2278 else if (!list_empty(&lock->l_res_link))
2279 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2281 flags = LDLM_FL_REPLAY;
2283 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2284 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2288 /* We're part of recovery, so don't wait for it. */
2289 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2291 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2292 ldlm_lock2desc(lock, &body->lock_desc);
2293 body->lock_flags = ldlm_flags_to_wire(flags);
2295 ldlm_lock2handle(lock, &body->lock_handle[0]);
2296 if (lock->l_lvb_len > 0)
2297 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2298 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2300 ptlrpc_request_set_replen(req);
2301 /* notify the server we've replayed all requests.
2302 * also, we mark the request to be put on a dedicated
2303 * queue to be processed after all request replayes.
2305 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2307 LDLM_DEBUG(lock, "replaying lock:");
2309 atomic_inc(&req->rq_import->imp_replay_inflight);
2310 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2311 aa = ptlrpc_req_async_args(req);
2312 aa->lock_handle = body->lock_handle[0];
2313 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2314 ptlrpcd_add_req(req);
2320 * Cancel as many unused locks as possible before replay. since we are
2321 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2324 * Called only in recovery before replaying locks. there is no need to
2325 * replay locks that are unused. since the clients may hold thousands of
2326 * cached unused locks, dropping the unused locks can greatly reduce the
2327 * load on the servers at recovery time.
2329 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2332 struct list_head cancels = LIST_HEAD_INIT(cancels);
2334 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2335 "replay for namespace %s (%d)\n",
2336 ldlm_ns_name(ns), ns->ns_nr_unused);
2338 /* We don't need to care whether or not LRU resize is enabled
2339 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2340 * count parameter */
2341 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2342 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2344 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2345 canceled, ldlm_ns_name(ns));
2348 int ldlm_replay_locks(struct obd_import *imp)
2350 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2351 struct list_head list = LIST_HEAD_INIT(list);
2352 struct ldlm_lock *lock, *next;
2357 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2359 /* don't replay locks if import failed recovery */
2360 if (imp->imp_vbr_failed)
2363 /* ensure this doesn't fall to 0 before all have been queued */
2364 atomic_inc(&imp->imp_replay_inflight);
2366 if (ldlm_cancel_unused_locks_before_replay)
2367 ldlm_cancel_unused_locks_for_replay(ns);
2369 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2371 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2372 list_del_init(&lock->l_pending_chain);
2374 LDLM_LOCK_RELEASE(lock);
2375 continue; /* or try to do the rest? */
2377 rc = replay_one_lock(imp, lock);
2378 LDLM_LOCK_RELEASE(lock);
2381 atomic_dec(&imp->imp_replay_inflight);