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, struct lustre_handle *lockh, int rc)
546 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
547 int is_replay = *flags & LDLM_FL_REPLAY;
548 struct ldlm_lock *lock;
549 struct ldlm_reply *reply;
550 int cleanup_phase = 1;
553 lock = ldlm_handle2lock(lockh);
554 /* ldlm_cli_enqueue is holding a reference on this lock. */
556 LASSERT(type == LDLM_FLOCK);
560 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
561 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
563 if (rc != ELDLM_OK) {
565 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
566 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
568 if (rc != ELDLM_LOCK_ABORTED)
572 /* Before we return, swab the reply */
573 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
575 GOTO(cleanup, rc = -EPROTO);
580 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
583 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
584 GOTO(cleanup, rc = size);
585 } else if (unlikely(size > lvb_len)) {
586 LDLM_ERROR(lock, "Replied LVB is larger than "
587 "expectation, expected = %d, replied = %d",
589 GOTO(cleanup, rc = -EINVAL);
594 if (rc == ELDLM_LOCK_ABORTED) {
595 if (lvb_len > 0 && lvb != NULL)
596 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
598 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
601 /* lock enqueued on the server */
604 lock_res_and_lock(lock);
605 /* Key change rehash lock in per-export hash with new key */
606 if (exp->exp_lock_hash) {
607 /* In the function below, .hs_keycmp resolves to
608 * ldlm_export_lock_keycmp() */
609 /* coverity[overrun-buffer-val] */
610 cfs_hash_rehash_key(exp->exp_lock_hash,
611 &lock->l_remote_handle,
615 lock->l_remote_handle = reply->lock_handle;
618 *flags = ldlm_flags_from_wire(reply->lock_flags);
619 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
620 LDLM_FL_INHERIT_MASK);
621 unlock_res_and_lock(lock);
623 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: "LPX64"\n",
624 lock, reply->lock_handle.cookie, *flags);
626 /* If enqueue returned a blocked lock but the completion handler has
627 * already run, then it fixed up the resource and we don't need to do it
629 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
630 int newmode = reply->lock_desc.l_req_mode;
632 if (newmode && newmode != lock->l_req_mode) {
633 LDLM_DEBUG(lock, "server returned different mode %s",
634 ldlm_lockname[newmode]);
635 lock->l_req_mode = newmode;
638 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
639 &lock->l_resource->lr_name)) {
640 CDEBUG(D_INFO, "remote intent success, locking "DLDLMRES
641 " instead of "DLDLMRES"\n",
642 PLDLMRES(&reply->lock_desc.l_resource),
643 PLDLMRES(lock->l_resource));
645 rc = ldlm_lock_change_resource(ns, lock,
646 &reply->lock_desc.l_resource.lr_name);
647 if (rc || lock->l_resource == NULL)
648 GOTO(cleanup, rc = -ENOMEM);
649 LDLM_DEBUG(lock, "client-side enqueue, new resource");
652 if (!(type == LDLM_IBITS &&
653 !(exp_connect_flags(exp) & OBD_CONNECT_IBITS)))
654 /* We assume lock type cannot change on server*/
655 ldlm_convert_policy_to_local(exp,
656 lock->l_resource->lr_type,
657 &reply->lock_desc.l_policy_data,
658 &lock->l_policy_data);
659 if (type != LDLM_PLAIN)
660 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
663 if ((*flags) & LDLM_FL_AST_SENT) {
664 lock_res_and_lock(lock);
665 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
666 unlock_res_and_lock(lock);
667 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
670 /* If the lock has already been granted by a completion AST, don't
671 * clobber the LVB with an older one. */
673 /* We must lock or a racing completion might update lvb without
674 * letting us know and we'll clobber the correct value.
675 * Cannot unlock after the check either, a that still leaves
676 * a tiny window for completion to get in */
677 lock_res_and_lock(lock);
678 if (lock->l_req_mode != lock->l_granted_mode)
679 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
680 lock->l_lvb_data, lvb_len);
681 unlock_res_and_lock(lock);
689 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
690 if (lock->l_completion_ast != NULL) {
691 int err = lock->l_completion_ast(lock, *flags, NULL);
699 if (lvb_len > 0 && lvb != NULL) {
700 /* Copy the LVB here, and not earlier, because the completion
701 * AST (if any) can override what we got in the reply */
702 memcpy(lvb, lock->l_lvb_data, lvb_len);
705 LDLM_DEBUG(lock, "client-side enqueue END");
708 if (cleanup_phase == 1 && rc)
709 failed_lock_cleanup(ns, lock, mode);
710 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
712 LDLM_LOCK_RELEASE(lock);
715 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
718 * Estimate number of lock handles that would fit into request of given
719 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
720 * a single page on the send/receive side. XXX: 512 should be changed to
721 * more adequate value.
723 static inline int ldlm_req_handles_avail(int req_size, int off)
727 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_CACHE_SIZE - 512) - req_size;
728 if (likely(avail >= 0))
729 avail /= (int)sizeof(struct lustre_handle);
732 avail += LDLM_LOCKREQ_HANDLES - off;
737 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
738 enum req_location loc,
741 __u32 size = req_capsule_msg_size(pill, loc);
742 return ldlm_req_handles_avail(size, off);
745 static inline int ldlm_format_handles_avail(struct obd_import *imp,
746 const struct req_format *fmt,
747 enum req_location loc, int off)
749 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
750 return ldlm_req_handles_avail(size, off);
754 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
755 * \a count locks in \a cancels.
757 * This is to be called by functions preparing their own requests that
758 * might contain lists of locks to cancel in addition to actual operation
759 * that needs to be performed.
761 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
762 int version, int opc, int canceloff,
763 struct list_head *cancels, int count)
765 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
766 struct req_capsule *pill = &req->rq_pill;
767 struct ldlm_request *dlm = NULL;
768 struct list_head head = LIST_HEAD_INIT(head);
769 enum ldlm_lru_flags lru_flags;
770 int avail, to_free, pack = 0;
776 if (ns_connect_cancelset(ns)) {
777 /* Estimate the amount of available space in the request. */
778 req_capsule_filled_sizes(pill, RCL_CLIENT);
779 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
781 lru_flags = ns_connect_lru_resize(ns) ?
782 LDLM_LRU_FLAG_LRUR_NO_WAIT : LDLM_LRU_FLAG_AGED;
783 to_free = !ns_connect_lru_resize(ns) &&
784 opc == LDLM_ENQUEUE ? 1 : 0;
786 /* Cancel LRU locks here _only_ if the server supports
787 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
788 * RPC, which will make us slower. */
790 count += ldlm_cancel_lru_local(ns, cancels, to_free,
797 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
798 ldlm_request_bufsize(pack, opc));
801 rc = ptlrpc_request_pack(req, version, opc);
803 ldlm_lock_list_put(cancels, l_bl_ast, count);
807 if (ns_connect_cancelset(ns)) {
809 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
811 /* Skip first lock handler in ldlm_request_pack(),
812 * this method will increment @lock_count according
813 * to the lock handle amount actually written to
815 dlm->lock_count = canceloff;
817 /* Pack into the request @pack lock handles. */
818 ldlm_cli_cancel_list(cancels, pack, req, 0);
819 /* Prepare and send separate cancel RPC for others. */
820 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
822 ldlm_lock_list_put(cancels, l_bl_ast, count);
826 EXPORT_SYMBOL(ldlm_prep_elc_req);
828 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
829 struct list_head *cancels, int count)
831 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
832 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
834 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
836 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
838 struct ptlrpc_request *req;
842 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
844 RETURN(ERR_PTR(-ENOMEM));
846 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
848 ptlrpc_request_free(req);
852 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
853 ptlrpc_request_set_replen(req);
856 EXPORT_SYMBOL(ldlm_enqueue_pack);
859 * Client-side lock enqueue.
861 * If a request has some specific initialisation it is passed in \a reqp,
862 * otherwise it is created in ldlm_cli_enqueue.
864 * Supports sync and async requests, pass \a async flag accordingly. If a
865 * request was created in ldlm_cli_enqueue and it is the async request,
866 * pass it to the caller in \a reqp.
868 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
869 struct ldlm_enqueue_info *einfo,
870 const struct ldlm_res_id *res_id,
871 union ldlm_policy_data const *policy, __u64 *flags,
872 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
873 struct lustre_handle *lockh, int async)
875 struct ldlm_namespace *ns;
876 struct ldlm_lock *lock;
877 struct ldlm_request *body;
878 int is_replay = *flags & LDLM_FL_REPLAY;
879 int req_passed_in = 1;
881 struct ptlrpc_request *req;
884 LASSERT(exp != NULL);
886 ns = exp->exp_obd->obd_namespace;
888 /* If we're replaying this lock, just check some invariants.
889 * If we're creating a new lock, get everything all setup nice. */
891 lock = ldlm_handle2lock_long(lockh, 0);
892 LASSERT(lock != NULL);
893 LDLM_DEBUG(lock, "client-side enqueue START");
894 LASSERT(exp == lock->l_conn_export);
896 const struct ldlm_callback_suite cbs = {
897 .lcs_completion = einfo->ei_cb_cp,
898 .lcs_blocking = einfo->ei_cb_bl,
899 .lcs_glimpse = einfo->ei_cb_gl
901 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
902 einfo->ei_mode, &cbs, einfo->ei_cbdata,
905 RETURN(PTR_ERR(lock));
906 /* for the local lock, add the reference */
907 ldlm_lock_addref_internal(lock, einfo->ei_mode);
908 ldlm_lock2handle(lock, lockh);
910 lock->l_policy_data = *policy;
912 if (einfo->ei_type == LDLM_EXTENT) {
913 /* extent lock without policy is a bug */
917 lock->l_req_extent = policy->l_extent;
919 LDLM_DEBUG(lock, "client-side enqueue START, flags "LPX64,
923 lock->l_conn_export = exp;
924 lock->l_export = NULL;
925 lock->l_blocking_ast = einfo->ei_cb_bl;
926 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL));
927 lock->l_last_activity = cfs_time_current_sec();
929 /* lock not sent to server yet */
931 if (reqp == NULL || *reqp == NULL) {
932 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
937 failed_lock_cleanup(ns, lock, einfo->ei_mode);
938 LDLM_LOCK_RELEASE(lock);
948 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
950 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
951 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
954 /* Dump lock data into the request buffer */
955 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
956 ldlm_lock2desc(lock, &body->lock_desc);
957 body->lock_flags = ldlm_flags_to_wire(*flags);
958 body->lock_handle[0] = *lockh;
960 /* Continue as normal. */
961 if (!req_passed_in) {
963 req_capsule_extend(&req->rq_pill,
964 &RQF_LDLM_ENQUEUE_LVB);
965 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
967 ptlrpc_request_set_replen(req);
971 LASSERT(reqp != NULL);
975 LDLM_DEBUG(lock, "sending request");
977 rc = ptlrpc_queue_wait(req);
979 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
980 einfo->ei_mode, flags, lvb, lvb_len,
983 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
984 * one reference that we took */
986 LDLM_LOCK_RELEASE(lock);
990 if (!req_passed_in && req != NULL) {
991 ptlrpc_req_finished(req);
998 EXPORT_SYMBOL(ldlm_cli_enqueue);
1000 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
1003 struct ldlm_resource *res;
1006 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1007 CERROR("Trying to cancel local lock\n");
1010 LDLM_DEBUG(lock, "client-side local convert");
1012 res = ldlm_lock_convert(lock, new_mode, flags);
1014 ldlm_reprocess_all(res);
1017 rc = LUSTRE_EDEADLK;
1019 LDLM_DEBUG(lock, "client-side local convert handler END");
1020 LDLM_LOCK_PUT(lock);
1024 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
1025 * conversion of locks which are on the waiting or converting queue */
1026 /* Caller of this code is supposed to take care of lock readers/writers
1028 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, __u32 *flags)
1030 struct ldlm_request *body;
1031 struct ldlm_reply *reply;
1032 struct ldlm_lock *lock;
1033 struct ldlm_resource *res;
1034 struct ptlrpc_request *req;
1038 lock = ldlm_handle2lock(lockh);
1045 if (lock->l_conn_export == NULL)
1046 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
1048 LDLM_DEBUG(lock, "client-side convert");
1050 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
1051 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1054 LDLM_LOCK_PUT(lock);
1058 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1059 body->lock_handle[0] = lock->l_remote_handle;
1061 body->lock_desc.l_req_mode = new_mode;
1062 body->lock_flags = ldlm_flags_to_wire(*flags);
1065 ptlrpc_request_set_replen(req);
1066 rc = ptlrpc_queue_wait(req);
1070 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1072 GOTO(out, rc = -EPROTO);
1075 GOTO(out, rc = req->rq_status);
1077 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
1079 ldlm_reprocess_all(res);
1080 /* Go to sleep until the lock is granted. */
1081 /* FIXME: or cancelled. */
1082 if (lock->l_completion_ast) {
1083 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1089 rc = LUSTRE_EDEADLK;
1093 LDLM_LOCK_PUT(lock);
1094 ptlrpc_req_finished(req);
1099 * Cancel locks locally.
1101 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1102 * \retval LDLM_FL_CANCELING otherwise;
1103 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1105 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1107 __u64 rc = LDLM_FL_LOCAL_ONLY;
1110 if (lock->l_conn_export) {
1113 LDLM_DEBUG(lock, "client-side cancel");
1114 /* Set this flag to prevent others from getting new references*/
1115 lock_res_and_lock(lock);
1116 ldlm_set_cbpending(lock);
1117 local_only = !!(lock->l_flags &
1118 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1119 ldlm_cancel_callback(lock);
1120 rc = (ldlm_is_bl_ast(lock)) ?
1121 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1122 unlock_res_and_lock(lock);
1125 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1127 rc = LDLM_FL_LOCAL_ONLY;
1129 ldlm_lock_cancel(lock);
1131 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1132 LDLM_ERROR(lock, "Trying to cancel local lock");
1135 LDLM_DEBUG(lock, "server-side local cancel");
1136 ldlm_lock_cancel(lock);
1137 ldlm_reprocess_all(lock->l_resource);
1144 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1146 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1147 struct list_head *head, int count)
1149 struct ldlm_request *dlm;
1150 struct ldlm_lock *lock;
1151 int max, packed = 0;
1154 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1155 LASSERT(dlm != NULL);
1157 /* Check the room in the request buffer. */
1158 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1159 sizeof(struct ldlm_request);
1160 max /= sizeof(struct lustre_handle);
1161 max += LDLM_LOCKREQ_HANDLES;
1162 LASSERT(max >= dlm->lock_count + count);
1164 /* XXX: it would be better to pack lock handles grouped by resource.
1165 * so that the server cancel would call filter_lvbo_update() less
1167 list_for_each_entry(lock, head, l_bl_ast) {
1170 LASSERT(lock->l_conn_export);
1171 /* Pack the lock handle to the given request buffer. */
1172 LDLM_DEBUG(lock, "packing");
1173 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1176 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1181 * Prepare and send a batched cancel RPC. It will include \a count lock
1182 * handles of locks given in \a cancels list. */
1183 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1184 int count, enum ldlm_cancel_flags flags)
1186 struct ptlrpc_request *req = NULL;
1187 struct obd_import *imp;
1192 LASSERT(exp != NULL);
1195 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1197 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1200 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1201 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1206 imp = class_exp2cliimp(exp);
1207 if (imp == NULL || imp->imp_invalid) {
1209 "skipping cancel on invalid import %p\n", imp);
1213 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1215 GOTO(out, rc = -ENOMEM);
1217 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1218 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1219 ldlm_request_bufsize(count, LDLM_CANCEL));
1221 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1223 ptlrpc_request_free(req);
1227 /* If OSP want cancel cross-MDT lock, let's not block it in
1228 * in recovery, otherwise the lock will not released, if
1229 * the remote target is also in recovery, and it also need
1230 * this lock, it might cause deadlock. */
1231 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1232 exp->exp_obd->obd_lu_dev != NULL &&
1233 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1234 struct lu_device *top_dev;
1236 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1237 if (top_dev != NULL &&
1238 top_dev->ld_obd->obd_recovering)
1239 req->rq_allow_replay = 1;
1242 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1243 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1244 ptlrpc_at_set_req_timeout(req);
1246 ldlm_cancel_pack(req, cancels, count);
1248 ptlrpc_request_set_replen(req);
1249 if (flags & LCF_ASYNC) {
1250 ptlrpcd_add_req(req);
1255 rc = ptlrpc_queue_wait(req);
1256 if (rc == LUSTRE_ESTALE) {
1257 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1258 "out of sync -- not fatal\n",
1259 libcfs_nid2str(req->rq_import->
1260 imp_connection->c_peer.nid));
1262 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1263 req->rq_import_generation == imp->imp_generation) {
1264 ptlrpc_req_finished(req);
1266 } else if (rc != ELDLM_OK) {
1267 /* -ESHUTDOWN is common on umount */
1268 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1269 "Got rc %d from cancel RPC: "
1270 "canceling anyway\n", rc);
1277 ptlrpc_req_finished(req);
1280 return sent ? sent : rc;
1283 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1285 LASSERT(imp != NULL);
1286 return &imp->imp_obd->obd_namespace->ns_pool;
1290 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1292 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1294 struct obd_device *obd;
1298 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1299 !imp_connect_lru_resize(req->rq_import)))
1302 * Do nothing for corner cases.
1307 /* In some cases RPC may contain SLV and limit zeroed out. This
1308 * is the case when server does not support LRU resize feature.
1309 * This is also possible in some recovery cases when server-side
1310 * reqs have no reference to the OBD export and thus access to
1311 * server-side namespace is not possible. */
1312 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1313 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1314 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1315 "(SLV: "LPU64", Limit: %u)",
1316 lustre_msg_get_slv(req->rq_repmsg),
1317 lustre_msg_get_limit(req->rq_repmsg));
1321 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1322 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1323 obd = req->rq_import->imp_obd;
1325 /* Set new SLV and limit in OBD fields to make them accessible
1326 * to the pool thread. We do not access obd_namespace and pool
1327 * directly here as there is no reliable way to make sure that
1328 * they are still alive at cleanup time. Evil races are possible
1329 * which may cause Oops at that time. */
1330 write_lock(&obd->obd_pool_lock);
1331 obd->obd_pool_slv = new_slv;
1332 obd->obd_pool_limit = new_limit;
1333 write_unlock(&obd->obd_pool_lock);
1339 * Client side lock cancel.
1341 * Lock must not have any readers or writers by this time.
1343 int ldlm_cli_cancel(struct lustre_handle *lockh,
1344 enum ldlm_cancel_flags cancel_flags)
1346 struct obd_export *exp;
1347 enum ldlm_lru_flags lru_flags;
1348 int avail, count = 1;
1350 struct ldlm_namespace *ns;
1351 struct ldlm_lock *lock;
1352 struct list_head cancels = LIST_HEAD_INIT(cancels);
1355 lock = ldlm_handle2lock_long(lockh, 0);
1357 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1361 lock_res_and_lock(lock);
1362 /* Lock is being canceled and the caller doesn't want to wait */
1363 if (ldlm_is_canceling(lock) && (cancel_flags & LCF_ASYNC)) {
1364 unlock_res_and_lock(lock);
1365 LDLM_LOCK_RELEASE(lock);
1369 ldlm_set_canceling(lock);
1370 unlock_res_and_lock(lock);
1372 rc = ldlm_cli_cancel_local(lock);
1373 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1374 LDLM_LOCK_RELEASE(lock);
1377 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1378 * RPC which goes to canceld portal, so we can cancel other LRU locks
1379 * here and send them all as one LDLM_CANCEL RPC. */
1380 LASSERT(list_empty(&lock->l_bl_ast));
1381 list_add(&lock->l_bl_ast, &cancels);
1383 exp = lock->l_conn_export;
1384 if (exp_connect_cancelset(exp)) {
1385 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1390 ns = ldlm_lock_to_ns(lock);
1391 lru_flags = ns_connect_lru_resize(ns) ?
1392 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1393 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1394 LCF_BL_AST, lru_flags);
1396 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1399 EXPORT_SYMBOL(ldlm_cli_cancel);
1402 * Locally cancel up to \a count locks in list \a cancels.
1403 * Return the number of cancelled locks.
1405 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1406 enum ldlm_cancel_flags cancel_flags)
1408 struct list_head head = LIST_HEAD_INIT(head);
1409 struct ldlm_lock *lock, *next;
1410 int left = 0, bl_ast = 0;
1414 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1418 if (cancel_flags & LCF_LOCAL) {
1419 rc = LDLM_FL_LOCAL_ONLY;
1420 ldlm_lock_cancel(lock);
1422 rc = ldlm_cli_cancel_local(lock);
1424 /* Until we have compound requests and can send LDLM_CANCEL
1425 * requests batched with generic RPCs, we need to send cancels
1426 * with the LDLM_FL_BL_AST flag in a separate RPC from
1427 * the one being generated now. */
1428 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1429 LDLM_DEBUG(lock, "Cancel lock separately");
1430 list_del_init(&lock->l_bl_ast);
1431 list_add(&lock->l_bl_ast, &head);
1435 if (rc == LDLM_FL_LOCAL_ONLY) {
1436 /* CANCEL RPC should not be sent to server. */
1437 list_del_init(&lock->l_bl_ast);
1438 LDLM_LOCK_RELEASE(lock);
1444 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1451 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1452 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1453 * readahead requests, ...)
1455 static enum ldlm_policy_res
1456 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1457 int unused, int added, int count)
1459 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1461 /* don't check added & count since we want to process all locks
1463 * It's fine to not take lock to access lock->l_resource since
1464 * the lock has already been granted so it won't change. */
1465 switch (lock->l_resource->lr_type) {
1468 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1471 result = LDLM_POLICY_SKIP_LOCK;
1472 lock_res_and_lock(lock);
1473 ldlm_set_skipped(lock);
1474 unlock_res_and_lock(lock);
1482 * Callback function for LRU-resize policy. Decides whether to keep
1483 * \a lock in LRU for current \a LRU size \a unused, added in current
1484 * scan \a added and number of locks to be preferably canceled \a count.
1486 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1488 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1490 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1491 struct ldlm_lock *lock,
1492 int unused, int added,
1495 cfs_time_t cur = cfs_time_current();
1496 struct ldlm_pool *pl = &ns->ns_pool;
1500 /* Stop LRU processing when we reach past @count or have checked all
1502 if (count && added >= count)
1503 return LDLM_POLICY_KEEP_LOCK;
1505 /* Despite of the LV, It doesn't make sense to keep the lock which
1506 * is unused for ns_max_age time. */
1507 if (cfs_time_after(cfs_time_current(),
1508 cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1509 return LDLM_POLICY_CANCEL_LOCK;
1511 slv = ldlm_pool_get_slv(pl);
1512 lvf = ldlm_pool_get_lvf(pl);
1513 la = cfs_duration_sec(cfs_time_sub(cur,
1514 lock->l_last_used));
1515 lv = lvf * la * unused;
1517 /* Inform pool about current CLV to see it via proc. */
1518 ldlm_pool_set_clv(pl, lv);
1520 /* Stop when SLV is not yet come from server or lv is smaller than
1522 if (slv == 0 || lv < slv)
1523 return LDLM_POLICY_KEEP_LOCK;
1525 return LDLM_POLICY_CANCEL_LOCK;
1528 static enum ldlm_policy_res
1529 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1530 struct ldlm_lock *lock,
1531 int unused, int added,
1534 enum ldlm_policy_res result;
1536 result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1537 if (result == LDLM_POLICY_KEEP_LOCK)
1540 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1544 * Callback function for proc used policy. Makes decision whether to keep
1545 * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1546 * added and number of locks to be preferably canceled \a count.
1548 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1550 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1552 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1553 struct ldlm_lock *lock,
1554 int unused, int added,
1557 /* Stop LRU processing when we reach past @count or have checked all
1559 return (added >= count) ?
1560 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1564 * Callback function for aged policy. Makes decision whether to keep \a lock in
1565 * LRU for current LRU size \a unused, added in current scan \a added and
1566 * number of locks to be preferably canceled \a count.
1568 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1570 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1572 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1573 struct ldlm_lock *lock,
1574 int unused, int added,
1577 if ((added >= count) &&
1578 cfs_time_before(cfs_time_current(),
1579 cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1580 return LDLM_POLICY_KEEP_LOCK;
1582 return LDLM_POLICY_CANCEL_LOCK;
1586 * Callback function for default policy. Makes decision whether to keep \a lock
1587 * in LRU for current LRU size \a unused, added in current scan \a added and
1588 * number of locks to be preferably canceled \a count.
1590 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1592 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1595 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1596 struct ldlm_lock *lock,
1597 int unused, int added,
1600 /* Stop LRU processing when we reach past count or have checked all
1602 return (added >= count) ?
1603 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1606 typedef enum ldlm_policy_res
1607 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1608 int unused, int added, int count);
1610 static ldlm_cancel_lru_policy_t
1611 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1613 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1614 return ldlm_cancel_no_wait_policy;
1616 if (ns_connect_lru_resize(ns)) {
1617 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1618 /* We kill passed number of old locks. */
1619 return ldlm_cancel_passed_policy;
1620 if (lru_flags & LDLM_LRU_FLAG_LRUR)
1621 return ldlm_cancel_lrur_policy;
1622 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1623 return ldlm_cancel_passed_policy;
1624 else if (lru_flags & LDLM_LRU_FLAG_LRUR_NO_WAIT)
1625 return ldlm_cancel_lrur_no_wait_policy;
1627 if (lru_flags & LDLM_LRU_FLAG_AGED)
1628 return ldlm_cancel_aged_policy;
1631 return ldlm_cancel_default_policy;
1635 * - Free space in LRU for \a count new locks,
1636 * redundant unused locks are canceled locally;
1637 * - also cancel locally unused aged locks;
1638 * - do not cancel more than \a max locks;
1639 * - GET the found locks and add them into the \a cancels list.
1641 * A client lock can be added to the l_bl_ast list only when it is
1642 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1643 * CANCEL. There are the following use cases:
1644 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1645 * ldlm_cli_cancel(), which check and set this flag properly. As any
1646 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1647 * later without any special locking.
1649 * Calling policies for enabled LRU resize:
1650 * ----------------------------------------
1651 * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1652 * cancel not more than \a count locks;
1654 * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1655 * at the beginning of LRU list);
1657 * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1658 * to memory pressre policy function;
1660 * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1662 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1663 * (typically before replaying locks) w/o
1664 * sending any RPCs or waiting for any
1665 * outstanding RPC to complete.
1667 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1668 struct list_head *cancels, int count, int max,
1669 enum ldlm_lru_flags lru_flags)
1671 ldlm_cancel_lru_policy_t pf;
1672 struct ldlm_lock *lock, *next;
1673 int added = 0, unused, remained;
1674 int no_wait = lru_flags & (LDLM_LRU_FLAG_NO_WAIT |
1675 LDLM_LRU_FLAG_LRUR_NO_WAIT);
1678 spin_lock(&ns->ns_lock);
1679 unused = ns->ns_nr_unused;
1682 if (!ns_connect_lru_resize(ns))
1683 count += unused - ns->ns_max_unused;
1685 pf = ldlm_cancel_lru_policy(ns, lru_flags);
1686 LASSERT(pf != NULL);
1688 while (!list_empty(&ns->ns_unused_list)) {
1689 enum ldlm_policy_res result;
1690 cfs_time_t last_use = 0;
1692 /* all unused locks */
1693 if (remained-- <= 0)
1696 /* For any flags, stop scanning if @max is reached. */
1697 if (max && added >= max)
1700 list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1702 /* No locks which got blocking requests. */
1703 LASSERT(!ldlm_is_bl_ast(lock));
1705 if (no_wait && ldlm_is_skipped(lock))
1706 /* already processed */
1709 last_use = lock->l_last_used;
1710 if (last_use == cfs_time_current())
1713 /* Somebody is already doing CANCEL. No need for this
1714 * lock in LRU, do not traverse it again. */
1715 if (!ldlm_is_canceling(lock))
1718 ldlm_lock_remove_from_lru_nolock(lock);
1720 if (&lock->l_lru == &ns->ns_unused_list)
1723 LDLM_LOCK_GET(lock);
1724 spin_unlock(&ns->ns_lock);
1725 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1727 /* Pass the lock through the policy filter and see if it
1728 * should stay in LRU.
1730 * Even for shrinker policy we stop scanning if
1731 * we find a lock that should stay in the cache.
1732 * We should take into account lock age anyway
1733 * as a new lock is a valuable resource even if
1734 * it has a low weight.
1736 * That is, for shrinker policy we drop only
1737 * old locks, but additionally choose them by
1738 * their weight. Big extent locks will stay in
1740 result = pf(ns, lock, unused, added, count);
1741 if (result == LDLM_POLICY_KEEP_LOCK) {
1742 lu_ref_del(&lock->l_reference,
1743 __FUNCTION__, current);
1744 LDLM_LOCK_RELEASE(lock);
1745 spin_lock(&ns->ns_lock);
1748 if (result == LDLM_POLICY_SKIP_LOCK) {
1749 lu_ref_del(&lock->l_reference,
1751 LDLM_LOCK_RELEASE(lock);
1752 spin_lock(&ns->ns_lock);
1756 lock_res_and_lock(lock);
1757 /* Check flags again under the lock. */
1758 if (ldlm_is_canceling(lock) ||
1759 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1760 /* Another thread is removing lock from LRU, or
1761 * somebody is already doing CANCEL, or there
1762 * is a blocking request which will send cancel
1763 * by itself, or the lock is no longer unused or
1764 * the lock has been used since the pf() call and
1765 * pages could be put under it. */
1766 unlock_res_and_lock(lock);
1767 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1768 LDLM_LOCK_RELEASE(lock);
1769 spin_lock(&ns->ns_lock);
1772 LASSERT(!lock->l_readers && !lock->l_writers);
1774 /* If we have chosen to cancel this lock voluntarily, we
1775 * better send cancel notification to server, so that it
1776 * frees appropriate state. This might lead to a race
1777 * where while we are doing cancel here, server is also
1778 * silently cancelling this lock. */
1779 ldlm_clear_cancel_on_block(lock);
1781 /* Setting the CBPENDING flag is a little misleading,
1782 * but prevents an important race; namely, once
1783 * CBPENDING is set, the lock can accumulate no more
1784 * readers/writers. Since readers and writers are
1785 * already zero here, ldlm_lock_decref() won't see
1786 * this flag and call l_blocking_ast */
1787 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1789 /* We can't re-add to l_lru as it confuses the
1790 * refcounting in ldlm_lock_remove_from_lru() if an AST
1791 * arrives after we drop lr_lock below. We use l_bl_ast
1792 * and can't use l_pending_chain as it is used both on
1793 * server and client nevertheless bug 5666 says it is
1794 * used only on server */
1795 LASSERT(list_empty(&lock->l_bl_ast));
1796 list_add(&lock->l_bl_ast, cancels);
1797 unlock_res_and_lock(lock);
1798 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1799 spin_lock(&ns->ns_lock);
1803 spin_unlock(&ns->ns_lock);
1807 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1809 enum ldlm_cancel_flags cancel_flags,
1810 enum ldlm_lru_flags lru_flags)
1814 added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
1818 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1822 * Cancel at least \a nr locks from given namespace LRU.
1824 * When called with LCF_ASYNC the blocking callback will be handled
1825 * in a thread and this function will return after the thread has been
1826 * asked to call the callback. When called with LCF_ASYNC the blocking
1827 * callback will be performed in this function.
1829 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
1830 enum ldlm_cancel_flags cancel_flags,
1831 enum ldlm_lru_flags lru_flags)
1833 struct list_head cancels = LIST_HEAD_INIT(cancels);
1837 /* Just prepare the list of locks, do not actually cancel them yet.
1838 * Locks are cancelled later in a separate thread. */
1839 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
1840 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
1848 * Find and cancel locally unused locks found on resource, matched to the
1849 * given policy, mode. GET the found locks and add them into the \a cancels
1852 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1853 struct list_head *cancels,
1854 union ldlm_policy_data *policy,
1855 enum ldlm_mode mode, __u64 lock_flags,
1856 enum ldlm_cancel_flags cancel_flags,
1859 struct ldlm_lock *lock;
1864 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1865 if (opaque != NULL && lock->l_ast_data != opaque) {
1866 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1867 lock->l_ast_data, opaque);
1872 if (lock->l_readers || lock->l_writers)
1875 /* If somebody is already doing CANCEL, or blocking AST came,
1876 * skip this lock. */
1877 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
1880 if (lockmode_compat(lock->l_granted_mode, mode))
1883 /* If policy is given and this is IBITS lock, add to list only
1884 * those locks that match by policy. */
1885 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1886 !(lock->l_policy_data.l_inodebits.bits &
1887 policy->l_inodebits.bits))
1890 /* See CBPENDING comment in ldlm_cancel_lru */
1891 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1894 LASSERT(list_empty(&lock->l_bl_ast));
1895 list_add(&lock->l_bl_ast, cancels);
1896 LDLM_LOCK_GET(lock);
1901 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1903 EXPORT_SYMBOL(ldlm_cancel_resource_local);
1906 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
1908 * If \a req is NULL, send CANCEL request to server with handles of locks
1909 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1910 * separately per lock.
1911 * If \a req is not NULL, put handles of locks in \a cancels into the request
1912 * buffer at the offset \a off.
1913 * Destroy \a cancels at the end.
1915 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1916 struct ptlrpc_request *req,
1917 enum ldlm_cancel_flags flags)
1919 struct ldlm_lock *lock;
1923 if (list_empty(cancels) || count == 0)
1926 /* XXX: requests (both batched and not) could be sent in parallel.
1927 * Usually it is enough to have just 1 RPC, but it is possible that
1928 * there are too many locks to be cancelled in LRU or on a resource.
1929 * It would also speed up the case when the server does not support
1932 LASSERT(!list_empty(cancels));
1933 lock = list_entry(cancels->next, struct ldlm_lock,
1935 LASSERT(lock->l_conn_export);
1937 if (exp_connect_cancelset(lock->l_conn_export)) {
1940 ldlm_cancel_pack(req, cancels, count);
1942 res = ldlm_cli_cancel_req(lock->l_conn_export,
1946 res = ldlm_cli_cancel_req(lock->l_conn_export,
1951 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1952 "ldlm_cli_cancel_list: %d\n", res);
1957 ldlm_lock_list_put(cancels, l_bl_ast, res);
1959 LASSERT(count == 0);
1962 EXPORT_SYMBOL(ldlm_cli_cancel_list);
1965 * Cancel all locks on a resource that have 0 readers/writers.
1967 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1968 * to notify the server. */
1969 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1970 const struct ldlm_res_id *res_id,
1971 union ldlm_policy_data *policy,
1972 enum ldlm_mode mode,
1973 enum ldlm_cancel_flags flags, void *opaque)
1975 struct ldlm_resource *res;
1976 struct list_head cancels = LIST_HEAD_INIT(cancels);
1981 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1983 /* This is not a problem. */
1984 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1988 LDLM_RESOURCE_ADDREF(res);
1989 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1990 0, flags | LCF_BL_AST, opaque);
1991 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1993 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
1996 LDLM_RESOURCE_DELREF(res);
1997 ldlm_resource_putref(res);
2000 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2002 struct ldlm_cli_cancel_arg {
2008 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2009 struct hlist_node *hnode, void *arg)
2011 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2012 struct ldlm_cli_cancel_arg *lc = arg;
2014 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2015 NULL, LCK_MINMODE, lc->lc_flags,
2017 /* must return 0 for hash iteration */
2022 * Cancel all locks on a namespace (or a specific resource, if given)
2023 * that have 0 readers/writers.
2025 * If flags & LCF_LOCAL, throw the locks away without trying
2026 * to notify the server. */
2027 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2028 const struct ldlm_res_id *res_id,
2029 enum ldlm_cancel_flags flags, void *opaque)
2031 struct ldlm_cli_cancel_arg arg = {
2033 .lc_opaque = opaque,
2041 if (res_id != NULL) {
2042 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2046 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2047 ldlm_cli_hash_cancel_unused, &arg, 0);
2052 /* Lock iterators. */
2054 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2057 struct list_head *tmp, *next;
2058 struct ldlm_lock *lock;
2059 int rc = LDLM_ITER_CONTINUE;
2064 RETURN(LDLM_ITER_CONTINUE);
2067 list_for_each_safe(tmp, next, &res->lr_granted) {
2068 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2070 if (iter(lock, closure) == LDLM_ITER_STOP)
2071 GOTO(out, rc = LDLM_ITER_STOP);
2074 list_for_each_safe(tmp, next, &res->lr_converting) {
2075 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2077 if (iter(lock, closure) == LDLM_ITER_STOP)
2078 GOTO(out, rc = LDLM_ITER_STOP);
2081 list_for_each_safe(tmp, next, &res->lr_waiting) {
2082 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2084 if (iter(lock, closure) == LDLM_ITER_STOP)
2085 GOTO(out, rc = LDLM_ITER_STOP);
2092 struct iter_helper_data {
2093 ldlm_iterator_t iter;
2097 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2099 struct iter_helper_data *helper = closure;
2100 return helper->iter(lock, helper->closure);
2103 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2104 struct hlist_node *hnode, void *arg)
2107 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2109 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2113 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2114 ldlm_iterator_t iter, void *closure)
2117 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2119 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2120 ldlm_res_iter_helper, &helper, 0);
2124 /* non-blocking function to manipulate a lock whose cb_data is being put away.
2125 * return 0: find no resource
2126 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2129 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2130 const struct ldlm_res_id *res_id,
2131 ldlm_iterator_t iter, void *data)
2133 struct ldlm_resource *res;
2137 LASSERTF(ns != NULL, "must pass in namespace\n");
2139 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2143 LDLM_RESOURCE_ADDREF(res);
2144 rc = ldlm_resource_foreach(res, iter, data);
2145 LDLM_RESOURCE_DELREF(res);
2146 ldlm_resource_putref(res);
2149 EXPORT_SYMBOL(ldlm_resource_iterate);
2153 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2155 struct list_head *list = closure;
2157 /* we use l_pending_chain here, because it's unused on clients. */
2158 LASSERTF(list_empty(&lock->l_pending_chain),
2159 "lock %p next %p prev %p\n",
2160 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2161 /* bug 9573: don't replay locks left after eviction, or
2162 * bug 17614: locks being actively cancelled. Get a reference
2163 * on a lock so that it does not disapear under us (e.g. due to cancel)
2165 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_CANCELING))) {
2166 list_add(&lock->l_pending_chain, list);
2167 LDLM_LOCK_GET(lock);
2170 return LDLM_ITER_CONTINUE;
2173 static int replay_lock_interpret(const struct lu_env *env,
2174 struct ptlrpc_request *req,
2175 struct ldlm_async_args *aa, int rc)
2177 struct ldlm_lock *lock;
2178 struct ldlm_reply *reply;
2179 struct obd_export *exp;
2182 atomic_dec(&req->rq_import->imp_replay_inflight);
2186 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2188 GOTO(out, rc = -EPROTO);
2190 lock = ldlm_handle2lock(&aa->lock_handle);
2192 CERROR("received replay ack for unknown local cookie "LPX64
2193 " remote cookie "LPX64 " from server %s id %s\n",
2194 aa->lock_handle.cookie, reply->lock_handle.cookie,
2195 req->rq_export->exp_client_uuid.uuid,
2196 libcfs_id2str(req->rq_peer));
2197 GOTO(out, rc = -ESTALE);
2200 /* Key change rehash lock in per-export hash with new key */
2201 exp = req->rq_export;
2202 if (exp && exp->exp_lock_hash) {
2203 /* In the function below, .hs_keycmp resolves to
2204 * ldlm_export_lock_keycmp() */
2205 /* coverity[overrun-buffer-val] */
2206 cfs_hash_rehash_key(exp->exp_lock_hash,
2207 &lock->l_remote_handle,
2208 &reply->lock_handle,
2211 lock->l_remote_handle = reply->lock_handle;
2214 LDLM_DEBUG(lock, "replayed lock:");
2215 ptlrpc_import_recovery_state_machine(req->rq_import);
2216 LDLM_LOCK_PUT(lock);
2219 ptlrpc_connect_import(req->rq_import);
2224 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2226 struct ptlrpc_request *req;
2227 struct ldlm_async_args *aa;
2228 struct ldlm_request *body;
2233 /* Bug 11974: Do not replay a lock which is actively being canceled */
2234 if (ldlm_is_canceling(lock)) {
2235 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2239 /* If this is reply-less callback lock, we cannot replay it, since
2240 * server might have long dropped it, but notification of that event was
2241 * lost by network. (and server granted conflicting lock already) */
2242 if (ldlm_is_cancel_on_block(lock)) {
2243 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2244 ldlm_lock_cancel(lock);
2249 * If granted mode matches the requested mode, this lock is granted.
2251 * If they differ, but we have a granted mode, then we were granted
2252 * one mode and now want another: ergo, converting.
2254 * If we haven't been granted anything and are on a resource list,
2255 * then we're blocked/waiting.
2257 * If we haven't been granted anything and we're NOT on a resource list,
2258 * then we haven't got a reply yet and don't have a known disposition.
2259 * This happens whenever a lock enqueue is the request that triggers
2262 if (lock->l_granted_mode == lock->l_req_mode)
2263 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2264 else if (lock->l_granted_mode)
2265 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2266 else if (!list_empty(&lock->l_res_link))
2267 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2269 flags = LDLM_FL_REPLAY;
2271 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2272 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2276 /* We're part of recovery, so don't wait for it. */
2277 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2279 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2280 ldlm_lock2desc(lock, &body->lock_desc);
2281 body->lock_flags = ldlm_flags_to_wire(flags);
2283 ldlm_lock2handle(lock, &body->lock_handle[0]);
2284 if (lock->l_lvb_len > 0)
2285 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2286 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2288 ptlrpc_request_set_replen(req);
2289 /* notify the server we've replayed all requests.
2290 * also, we mark the request to be put on a dedicated
2291 * queue to be processed after all request replayes.
2293 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2295 LDLM_DEBUG(lock, "replaying lock:");
2297 atomic_inc(&req->rq_import->imp_replay_inflight);
2298 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2299 aa = ptlrpc_req_async_args(req);
2300 aa->lock_handle = body->lock_handle[0];
2301 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2302 ptlrpcd_add_req(req);
2308 * Cancel as many unused locks as possible before replay. since we are
2309 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2312 * Called only in recovery before replaying locks. there is no need to
2313 * replay locks that are unused. since the clients may hold thousands of
2314 * cached unused locks, dropping the unused locks can greatly reduce the
2315 * load on the servers at recovery time.
2317 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2320 struct list_head cancels = LIST_HEAD_INIT(cancels);
2322 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2323 "replay for namespace %s (%d)\n",
2324 ldlm_ns_name(ns), ns->ns_nr_unused);
2326 /* We don't need to care whether or not LRU resize is enabled
2327 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2328 * count parameter */
2329 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2330 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2332 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2333 canceled, ldlm_ns_name(ns));
2336 int ldlm_replay_locks(struct obd_import *imp)
2338 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2339 struct list_head list = LIST_HEAD_INIT(list);
2340 struct ldlm_lock *lock, *next;
2345 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2347 /* don't replay locks if import failed recovery */
2348 if (imp->imp_vbr_failed)
2351 /* ensure this doesn't fall to 0 before all have been queued */
2352 atomic_inc(&imp->imp_replay_inflight);
2354 if (ldlm_cancel_unused_locks_before_replay)
2355 ldlm_cancel_unused_locks_for_replay(ns);
2357 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2359 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2360 list_del_init(&lock->l_pending_chain);
2362 LDLM_LOCK_RELEASE(lock);
2363 continue; /* or try to do the rest? */
2365 rc = replay_one_lock(imp, lock);
2366 LDLM_LOCK_RELEASE(lock);
2369 atomic_dec(&imp->imp_replay_inflight);