4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * This file contains Asynchronous System Trap (AST) handlers and related
34 * LDLM request-processing routines.
36 * An AST is a callback issued on a lock when its state is changed. There are
37 * several different types of ASTs (callbacks) registered for each lock:
39 * - completion AST: when a lock is enqueued by some process, but cannot be
40 * granted immediately due to other conflicting locks on the same resource,
41 * the completion AST is sent to notify the caller when the lock is
44 * - blocking AST: when a lock is granted to some process, if another process
45 * enqueues a conflicting (blocking) lock on a resource, a blocking AST is
46 * sent to notify the holder(s) of the lock(s) of the conflicting lock
47 * request. The lock holder(s) must release their lock(s) on that resource in
48 * a timely manner or be evicted by the server.
50 * - glimpse AST: this is used when a process wants information about a lock
51 * (i.e. the lock value block (LVB)) but does not necessarily require holding
52 * the lock. If the resource is locked, the lock holder(s) are sent glimpse
53 * ASTs and the LVB is returned to the caller, and lock holder(s) may CANCEL
54 * their lock(s) if they are idle. If the resource is not locked, the server
58 #define DEBUG_SUBSYSTEM S_LDLM
60 #include <lustre_errno.h>
61 #include <lustre_dlm.h>
62 #include <obd_class.h>
65 #include "ldlm_internal.h"
67 unsigned int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
68 module_param(ldlm_enqueue_min, uint, 0644);
69 MODULE_PARM_DESC(ldlm_enqueue_min, "lock enqueue timeout minimum");
71 /* in client side, whether the cached locks will be canceled before replay */
72 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
74 static void interrupted_completion_wait(void *data)
78 struct lock_wait_data {
79 struct ldlm_lock *lwd_lock;
83 struct ldlm_async_args {
84 struct lustre_handle lock_handle;
88 * ldlm_request_bufsize
90 * If opcode=LDLM_ENQUEUE, 1 slot is already occupied,
91 * LDLM_LOCKREQ_HANDLE -1 slots are available.
92 * Otherwise, LDLM_LOCKREQ_HANDLE slots are available.
97 * \retval size of the request buffer
99 int ldlm_request_bufsize(int count, int type)
101 int avail = LDLM_LOCKREQ_HANDLES;
103 if (type == LDLM_ENQUEUE)
104 avail -= LDLM_ENQUEUE_CANCEL_OFF;
107 avail = (count - avail) * sizeof(struct lustre_handle);
111 return sizeof(struct ldlm_request) + avail;
114 int ldlm_expired_completion_wait(void *data)
116 struct lock_wait_data *lwd = data;
117 struct ldlm_lock *lock = lwd->lwd_lock;
118 struct obd_import *imp;
119 struct obd_device *obd;
122 if (lock->l_conn_export == NULL) {
123 static time64_t next_dump, last_dump;
126 "lock timed out (enqueued at %lld, %llds ago); not entering recovery in server code, just going back to sleep",
127 (s64)lock->l_activity,
128 (s64)(ktime_get_real_seconds() -
130 if (ktime_get_seconds() > next_dump) {
131 last_dump = next_dump;
132 next_dump = ktime_get_seconds() + 300;
133 ldlm_namespace_dump(D_DLMTRACE,
134 ldlm_lock_to_ns(lock));
136 libcfs_debug_dumplog();
141 obd = lock->l_conn_export->exp_obd;
142 imp = obd->u.cli.cl_import;
143 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
145 "lock timed out (enqueued at %lld, %llds ago), entering recovery for %s@%s",
146 (s64)lock->l_activity,
147 (s64)(ktime_get_real_seconds() - lock->l_activity),
148 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
153 int is_granted_or_cancelled_nolock(struct ldlm_lock *lock)
157 check_res_locked(lock->l_resource);
158 if (ldlm_is_granted(lock) && !ldlm_is_cp_reqd(lock))
160 else if (ldlm_is_failed(lock) || ldlm_is_cancel(lock))
164 EXPORT_SYMBOL(is_granted_or_cancelled_nolock);
167 * Calculate the Completion timeout (covering enqueue, BL AST, data flush,
168 * lock cancel, and their replies). Used for lock completion timeout on the
171 * \param[in] lock lock which is waiting the completion callback
173 * \retval timeout in seconds to wait for the server reply
176 * We use the same basis for both server side and client side functions
177 * from a single node.
179 static time64_t ldlm_cp_timeout(struct ldlm_lock *lock)
187 * Wait a long time for enqueue - server may have to callback a
188 * lock from another client. Server will evict the other client if it
189 * doesn't respond reasonably, and then give us the lock.
191 timeout = at_get(ldlm_lock_to_ns_at(lock));
192 return max(3 * timeout, (time64_t) ldlm_enqueue_min);
196 * Helper function for ldlm_completion_ast(), updating timings when lock is
199 static int ldlm_completion_tail(struct ldlm_lock *lock, void *data)
204 if (ldlm_is_destroyed(lock) || ldlm_is_failed(lock)) {
205 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
207 } else if (data == NULL) {
208 LDLM_DEBUG(lock, "client-side enqueue: granted");
210 /* Take into AT only CP RPC, not immediately granted locks */
211 delay = ktime_get_real_seconds() - lock->l_activity;
212 LDLM_DEBUG(lock, "client-side enqueue: granted after %llds",
215 /* Update our time estimate */
216 at_measured(ldlm_lock_to_ns_at(lock), delay);
222 * Implementation of ->l_completion_ast() for a client, that doesn't wait
223 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
224 * other threads that cannot block for long.
226 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data)
230 if (flags == LDLM_FL_WAIT_NOREPROC) {
231 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
235 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
236 wake_up(&lock->l_waitq);
237 RETURN(ldlm_completion_tail(lock, data));
241 "client-side enqueue returned a blocked lock, going forward");
242 ldlm_reprocess_all(lock->l_resource, NULL);
245 EXPORT_SYMBOL(ldlm_completion_ast_async);
248 * Generic LDLM "completion" AST. This is called in several cases:
250 * - when a reply to an ENQUEUE RPC is received from the server
251 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
252 * this point (determined by flags);
254 * - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
257 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
260 * - to force all locks when resource is destroyed (cleanup_resource());
262 * If lock is not granted in the first case, this function waits until second
263 * or penultimate cases happen in some other thread.
266 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
268 /* XXX ALLOCATE - 160 bytes */
269 struct lock_wait_data lwd;
270 struct obd_device *obd;
271 struct obd_import *imp = NULL;
272 struct l_wait_info lwi;
278 if (flags == LDLM_FL_WAIT_NOREPROC) {
279 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
283 if (!(flags & LDLM_FL_BLOCKED_MASK)) {
284 wake_up(&lock->l_waitq);
288 LDLM_DEBUG(lock, "client-side enqueue returned a blocked locksleeping");
292 obd = class_exp2obd(lock->l_conn_export);
294 /* if this is a local lock, then there is no import */
296 imp = obd->u.cli.cl_import;
298 timeout = ldlm_cp_timeout(lock);
301 lock->l_activity = ktime_get_real_seconds();
303 if (ldlm_is_no_timeout(lock)) {
304 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
305 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
307 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
308 ldlm_expired_completion_wait,
309 interrupted_completion_wait, &lwd);
313 spin_lock(&imp->imp_lock);
314 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
315 spin_unlock(&imp->imp_lock);
318 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
319 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
320 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
321 ldlm_set_fail_loc(lock);
324 /* Go to sleep until the lock is granted or cancelled. */
325 rc = l_wait_event(lock->l_waitq,
326 is_granted_or_cancelled(lock), &lwi);
330 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
335 RETURN(ldlm_completion_tail(lock, data));
337 EXPORT_SYMBOL(ldlm_completion_ast);
340 * A helper to build a blocking AST function
342 * Perform a common operation for blocking ASTs:
343 * defferred lock cancellation.
345 * \param lock the lock blocking or canceling AST was called on
347 * \see mdt_blocking_ast
348 * \see ldlm_blocking_ast
350 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
356 ldlm_set_cbpending(lock);
357 do_ast = (!lock->l_readers && !lock->l_writers);
358 unlock_res_and_lock(lock);
361 struct lustre_handle lockh;
364 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
365 ldlm_lock2handle(lock, &lockh);
366 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
368 CERROR("ldlm_cli_cancel: %d\n", rc);
371 "Lock still has references, will be cancelled later");
375 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
378 * Server blocking AST
380 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
383 * \param lock the lock which blocks a request or cancelling lock
386 * \param flag indicates whether this cancelling or blocking callback
388 * \see ldlm_blocking_ast_nocheck
390 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
391 void *data, int flag)
395 if (flag == LDLM_CB_CANCELING) {
396 /* Don't need to do anything here. */
400 lock_res_and_lock(lock);
402 * Get this: if ldlm_blocking_ast is racing with intent_policy, such
403 * that ldlm_blocking_ast is called just before intent_policy method
404 * takes the lr_lock, then by the time we get the lock, we might not
405 * be the correct blocking function anymore. So check, and return
408 if (lock->l_blocking_ast != ldlm_blocking_ast) {
409 unlock_res_and_lock(lock);
412 RETURN(ldlm_blocking_ast_nocheck(lock));
414 EXPORT_SYMBOL(ldlm_blocking_ast);
417 * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
419 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
420 * rather subtle: with OST-side locking, it may so happen that _all_ extent
421 * locks are held by the OST. If client wants to obtain the current file size
422 * it calls ll_glimpse_size(), and (as all locks are held only on the server),
423 * this dummy glimpse callback fires and does nothing. The client still
424 * receives the correct file size due to the following fragment of code in
425 * ldlm_cb_interpret():
427 * if (rc == -ELDLM_NO_LOCK_DATA) {
428 * LDLM_DEBUG(lock, "lost race - client has a lock but no"
430 * ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
433 * That is, after the glimpse returns this error, ofd_lvbo_update() is called
434 * and returns the updated file attributes from the inode to the client.
436 * See also comment in ofd_intent_policy() on why servers must set a non-NULL
437 * l_glimpse_ast when grabbing DLM locks. Otherwise, the server will assume
438 * that the object is in the process of being destroyed.
440 * \param[in] lock DLM lock being glimpsed, unused
441 * \param[in] reqp pointer to ptlrpc_request, unused
443 * \retval -ELDLM_NO_LOCK_DATA to get attributes from disk object
445 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
447 return -ELDLM_NO_LOCK_DATA;
451 * Enqueue a local lock (typically on a server).
453 int ldlm_cli_enqueue_local(const struct lu_env *env,
454 struct ldlm_namespace *ns,
455 const struct ldlm_res_id *res_id,
456 enum ldlm_type type, union ldlm_policy_data *policy,
457 enum ldlm_mode mode, __u64 *flags,
458 ldlm_blocking_callback blocking,
459 ldlm_completion_callback completion,
460 ldlm_glimpse_callback glimpse,
461 void *data, __u32 lvb_len, enum lvb_type lvb_type,
462 const __u64 *client_cookie,
463 struct lustre_handle *lockh)
465 struct ldlm_lock *lock;
467 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
468 .lcs_blocking = blocking,
469 .lcs_glimpse = glimpse,
474 LASSERT(!(*flags & LDLM_FL_REPLAY));
475 if (unlikely(ns_is_client(ns))) {
476 CERROR("Trying to enqueue local lock in a shadow namespace\n");
480 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
483 GOTO(out_nolock, err = PTR_ERR(lock));
485 err = ldlm_lvbo_init(lock->l_resource);
487 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
488 ldlm_lock_destroy_nolock(lock);
492 ldlm_lock2handle(lock, lockh);
495 * NB: we don't have any lock now (lock_res_and_lock)
496 * because it's a new lock
498 ldlm_lock_addref_internal_nolock(lock, mode);
499 ldlm_set_local(lock);
500 if (*flags & LDLM_FL_ATOMIC_CB)
501 ldlm_set_atomic_cb(lock);
503 if (*flags & LDLM_FL_CANCEL_ON_BLOCK)
504 ldlm_set_cancel_on_block(lock);
507 lock->l_policy_data = *policy;
508 if (client_cookie != NULL)
509 lock->l_client_cookie = *client_cookie;
510 if (type == LDLM_EXTENT) {
511 /* extent lock without policy is a bug */
515 lock->l_req_extent = policy->l_extent;
518 err = ldlm_lock_enqueue(env, ns, &lock, policy, flags);
519 if (unlikely(err != ELDLM_OK))
523 *policy = lock->l_policy_data;
525 if (lock->l_completion_ast)
526 lock->l_completion_ast(lock, *flags, NULL);
528 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
531 LDLM_LOCK_RELEASE(lock);
535 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
537 static void failed_lock_cleanup(struct ldlm_namespace *ns,
538 struct ldlm_lock *lock, int mode)
542 /* Set a flag to prevent us from sending a CANCEL (b=407) */
543 lock_res_and_lock(lock);
544 /* Check that lock is not granted or failed, we might race. */
545 if (!ldlm_is_granted(lock) && !ldlm_is_failed(lock)) {
547 * Make sure that this lock will not be found by raced
548 * bl_ast and -EINVAL reply is sent to server anyways.
551 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
552 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
555 unlock_res_and_lock(lock);
559 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
561 LDLM_DEBUG(lock, "lock was granted or failed in race");
564 * XXX - HACK because we shouldn't call ldlm_lock_destroy()
565 * from llite/file.c/ll_file_flock().
568 * This code makes for the fact that we do not have blocking handler on
569 * a client for flock locks. As such this is the place where we must
570 * completely kill failed locks. (interrupted and those that
571 * were waiting to be granted when server evicted us.
573 if (lock->l_resource->lr_type == LDLM_FLOCK) {
574 lock_res_and_lock(lock);
575 if (!ldlm_is_destroyed(lock)) {
576 ldlm_resource_unlink_lock(lock);
577 ldlm_lock_decref_internal_nolock(lock, mode);
578 ldlm_lock_destroy_nolock(lock);
580 unlock_res_and_lock(lock);
582 ldlm_lock_decref_internal(lock, mode);
586 static bool ldlm_request_slot_needed(enum ldlm_type type)
588 return type == LDLM_FLOCK || type == LDLM_IBITS;
592 * Finishing portion of client lock enqueue code.
594 * Called after receiving reply from server.
596 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
597 enum ldlm_type type, __u8 with_policy,
598 enum ldlm_mode mode, __u64 *flags, void *lvb,
599 __u32 lvb_len, const struct lustre_handle *lockh,
602 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
603 const struct lu_env *env = NULL;
604 int is_replay = *flags & LDLM_FL_REPLAY;
605 struct ldlm_lock *lock;
606 struct ldlm_reply *reply;
607 int cleanup_phase = 1;
611 if (ldlm_request_slot_needed(type))
612 obd_put_request_slot(&req->rq_import->imp_obd->u.cli);
614 ptlrpc_put_mod_rpc_slot(req);
616 if (req && req->rq_svc_thread)
617 env = req->rq_svc_thread->t_env;
619 lock = ldlm_handle2lock(lockh);
620 /* ldlm_cli_enqueue is holding a reference on this lock. */
622 LASSERT(type == LDLM_FLOCK);
626 LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
627 "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
629 if (rc != ELDLM_OK) {
631 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
632 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
634 if (rc != ELDLM_LOCK_ABORTED)
638 /* Before we return, swab the reply */
639 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
641 GOTO(cleanup, rc = -EPROTO);
646 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
649 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
650 GOTO(cleanup, rc = size);
651 } else if (unlikely(size > lvb_len)) {
653 "Replied LVB is larger than expectation, expected = %d, replied = %d",
655 GOTO(cleanup, rc = -EINVAL);
660 if (rc == ELDLM_LOCK_ABORTED) {
661 if (lvb_len > 0 && lvb != NULL)
662 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
664 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
667 /* lock enqueued on the server */
670 lock_res_and_lock(lock);
671 /* Key change rehash lock in per-export hash with new key */
672 if (exp->exp_lock_hash) {
674 * In the function below, .hs_keycmp resolves to
675 * ldlm_export_lock_keycmp()
677 /* coverity[overrun-buffer-val] */
678 cfs_hash_rehash_key(exp->exp_lock_hash,
679 &lock->l_remote_handle,
683 lock->l_remote_handle = reply->lock_handle;
686 *flags = ldlm_flags_from_wire(reply->lock_flags);
687 lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
688 LDLM_FL_INHERIT_MASK);
689 unlock_res_and_lock(lock);
691 CDEBUG(D_INFO, "local: %p, remote cookie: %#llx, flags: %#llx\n",
692 lock, reply->lock_handle.cookie, *flags);
695 * If enqueue returned a blocked lock but the completion handler has
696 * already run, then it fixed up the resource and we don't need to do it
699 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
700 int newmode = reply->lock_desc.l_req_mode;
703 if (newmode && newmode != lock->l_req_mode) {
704 LDLM_DEBUG(lock, "server returned different mode %s",
705 ldlm_lockname[newmode]);
706 lock->l_req_mode = newmode;
709 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
710 &lock->l_resource->lr_name)) {
712 "remote intent success, locking "DLDLMRES", instead of "DLDLMRES"\n",
713 PLDLMRES(&reply->lock_desc.l_resource),
714 PLDLMRES(lock->l_resource));
716 rc = ldlm_lock_change_resource(ns, lock,
717 &reply->lock_desc.l_resource.lr_name);
718 if (rc || lock->l_resource == NULL)
719 GOTO(cleanup, rc = -ENOMEM);
720 LDLM_DEBUG(lock, "client-side enqueue, new resource");
724 /* We assume lock type cannot change on server*/
725 ldlm_convert_policy_to_local(exp,
726 lock->l_resource->lr_type,
727 &reply->lock_desc.l_policy_data,
728 &lock->l_policy_data);
731 if (type != LDLM_PLAIN)
733 "client-side enqueue, new policy data");
736 if ((*flags) & LDLM_FL_AST_SENT) {
737 lock_res_and_lock(lock);
738 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
739 unlock_res_and_lock(lock);
740 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
744 * If the lock has already been granted by a completion AST, don't
745 * clobber the LVB with an older one.
749 * We must lock or a racing completion might update lvb without
750 * letting us know and we'll clobber the correct value.
751 * Cannot unlock after the check either, a that still leaves
752 * a tiny window for completion to get in
754 lock_res_and_lock(lock);
755 if (!ldlm_is_granted(lock))
756 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
757 lock->l_lvb_data, lvb_len);
758 unlock_res_and_lock(lock);
766 rc = ldlm_lock_enqueue(env, ns, &lock, NULL, flags);
767 if (lock->l_completion_ast != NULL) {
768 int err = lock->l_completion_ast(lock, *flags, NULL);
777 if (lvb_len > 0 && lvb != NULL) {
779 * Copy the LVB here, and not earlier, because the completion
780 * AST (if any) can override what we got in the reply
782 memcpy(lvb, lock->l_lvb_data, lvb_len);
785 LDLM_DEBUG(lock, "client-side enqueue END");
788 if (cleanup_phase == 1 && rc)
789 failed_lock_cleanup(ns, lock, mode);
790 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
792 LDLM_LOCK_RELEASE(lock);
795 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
798 * Estimate number of lock handles that would fit into request of given
799 * size. PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
800 * a single page on the send/receive side. XXX: 512 should be changed to
801 * more adequate value.
803 static inline int ldlm_req_handles_avail(int req_size, int off)
807 avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512) - req_size;
808 if (likely(avail >= 0))
809 avail /= (int)sizeof(struct lustre_handle);
812 avail += LDLM_LOCKREQ_HANDLES - off;
817 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
818 enum req_location loc,
821 __u32 size = req_capsule_msg_size(pill, loc);
823 return ldlm_req_handles_avail(size, off);
826 static inline int ldlm_format_handles_avail(struct obd_import *imp,
827 const struct req_format *fmt,
828 enum req_location loc, int off)
830 __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
832 return ldlm_req_handles_avail(size, off);
836 * Cancel LRU locks and pack them into the enqueue request. Pack there the given
837 * \a count locks in \a cancels.
839 * This is to be called by functions preparing their own requests that
840 * might contain lists of locks to cancel in addition to actual operation
841 * that needs to be performed.
843 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
844 int version, int opc, int canceloff,
845 struct list_head *cancels, int count)
847 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
848 struct req_capsule *pill = &req->rq_pill;
849 struct ldlm_request *dlm = NULL;
851 enum ldlm_lru_flags lru_flags;
852 int avail, to_free, pack = 0;
859 if (ns_connect_cancelset(ns)) {
860 /* Estimate the amount of available space in the request. */
861 req_capsule_filled_sizes(pill, RCL_CLIENT);
862 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
864 lru_flags = LDLM_LRU_FLAG_NO_WAIT | (ns_connect_lru_resize(ns) ?
865 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED);
866 to_free = !ns_connect_lru_resize(ns) &&
867 opc == LDLM_ENQUEUE ? 1 : 0;
870 * Cancel LRU locks here _only_ if the server supports
871 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
872 * RPC, which will make us slower.
875 count += ldlm_cancel_lru_local(ns, cancels, to_free,
882 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
883 ldlm_request_bufsize(pack, opc));
886 rc = ptlrpc_request_pack(req, version, opc);
888 ldlm_lock_list_put(cancels, l_bl_ast, count);
892 if (ns_connect_cancelset(ns)) {
894 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
897 * Skip first lock handler in ldlm_request_pack(),
898 * this method will increment @lock_count according
899 * to the lock handle amount actually written to
902 dlm->lock_count = canceloff;
904 /* Pack into the request @pack lock handles. */
905 ldlm_cli_cancel_list(cancels, pack, req, 0);
906 /* Prepare and send separate cancel RPC for others. */
907 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
909 ldlm_lock_list_put(cancels, l_bl_ast, count);
913 EXPORT_SYMBOL(ldlm_prep_elc_req);
915 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
916 struct list_head *cancels, int count)
918 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
919 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
921 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
923 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
925 struct ptlrpc_request *req;
930 req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
932 RETURN(ERR_PTR(-ENOMEM));
934 rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
936 ptlrpc_request_free(req);
940 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
941 ptlrpc_request_set_replen(req);
944 EXPORT_SYMBOL(ldlm_enqueue_pack);
947 * Client-side lock enqueue.
949 * If a request has some specific initialisation it is passed in \a reqp,
950 * otherwise it is created in ldlm_cli_enqueue.
952 * Supports sync and async requests, pass \a async flag accordingly. If a
953 * request was created in ldlm_cli_enqueue and it is the async request,
954 * pass it to the caller in \a reqp.
956 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
957 struct ldlm_enqueue_info *einfo,
958 const struct ldlm_res_id *res_id,
959 union ldlm_policy_data const *policy, __u64 *flags,
960 void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
961 struct lustre_handle *lockh, int async)
963 struct ldlm_namespace *ns;
964 struct ldlm_lock *lock;
965 struct ldlm_request *body;
966 int is_replay = *flags & LDLM_FL_REPLAY;
967 int req_passed_in = 1;
969 struct ptlrpc_request *req;
973 LASSERT(exp != NULL);
975 ns = exp->exp_obd->obd_namespace;
978 * If we're replaying this lock, just check some invariants.
979 * If we're creating a new lock, get everything all setup nice.
982 lock = ldlm_handle2lock_long(lockh, 0);
983 LASSERT(lock != NULL);
984 LDLM_DEBUG(lock, "client-side enqueue START");
985 LASSERT(exp == lock->l_conn_export);
987 const struct ldlm_callback_suite cbs = {
988 .lcs_completion = einfo->ei_cb_cp,
989 .lcs_blocking = einfo->ei_cb_bl,
990 .lcs_glimpse = einfo->ei_cb_gl
992 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
993 einfo->ei_mode, &cbs, einfo->ei_cbdata,
996 RETURN(PTR_ERR(lock));
998 if (einfo->ei_cb_created)
999 einfo->ei_cb_created(lock);
1001 /* for the local lock, add the reference */
1002 ldlm_lock_addref_internal(lock, einfo->ei_mode);
1003 ldlm_lock2handle(lock, lockh);
1005 lock->l_policy_data = *policy;
1007 if (einfo->ei_type == LDLM_EXTENT) {
1008 /* extent lock without policy is a bug */
1012 lock->l_req_extent = policy->l_extent;
1014 LDLM_DEBUG(lock, "client-side enqueue START, flags %#llx",
1018 lock->l_conn_export = exp;
1019 lock->l_export = NULL;
1020 lock->l_blocking_ast = einfo->ei_cb_bl;
1021 lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL |
1022 LDLM_FL_ATOMIC_CB));
1023 lock->l_activity = ktime_get_real_seconds();
1025 /* lock not sent to server yet */
1026 if (reqp == NULL || *reqp == NULL) {
1027 req = ldlm_enqueue_pack(exp, lvb_len);
1029 failed_lock_cleanup(ns, lock, einfo->ei_mode);
1030 LDLM_LOCK_RELEASE(lock);
1031 RETURN(PTR_ERR(req));
1041 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
1043 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
1044 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
1047 if (*flags & LDLM_FL_NDELAY) {
1048 DEBUG_REQ(D_DLMTRACE, req, "enqueue lock with no delay");
1049 req->rq_no_resend = req->rq_no_delay = 1;
1051 * probably set a shorter timeout value and handle ETIMEDOUT
1052 * in osc_lock_upcall() correctly
1054 /* lustre_msg_set_timeout(req, req->rq_timeout / 2); */
1057 /* Dump lock data into the request buffer */
1058 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1059 ldlm_lock2desc(lock, &body->lock_desc);
1060 body->lock_flags = ldlm_flags_to_wire(*flags);
1061 body->lock_handle[0] = *lockh;
1063 /* extended LDLM opcodes in client stats */
1064 if (exp->exp_obd->obd_svc_stats != NULL) {
1065 bool glimpse = *flags & LDLM_FL_HAS_INTENT;
1067 /* OST glimpse has no intent buffer */
1068 if (req_capsule_has_field(&req->rq_pill, &RMF_LDLM_INTENT,
1070 struct ldlm_intent *it;
1072 it = req_capsule_client_get(&req->rq_pill,
1074 glimpse = (it && (it->opc == IT_GLIMPSE));
1078 ldlm_svc_get_eopc(body, exp->exp_obd->obd_svc_stats);
1080 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1082 LDLM_GLIMPSE_ENQUEUE);
1085 /* It is important to obtain modify RPC slot first (if applicable), so
1086 * that threads that are waiting for a modify RPC slot are not polluting
1087 * our rpcs in flight counter. */
1089 if (einfo->ei_enq_slot)
1090 ptlrpc_get_mod_rpc_slot(req);
1092 if (ldlm_request_slot_needed(einfo->ei_type)) {
1093 rc = obd_get_request_slot(&req->rq_import->imp_obd->u.cli);
1095 if (einfo->ei_enq_slot)
1096 ptlrpc_put_mod_rpc_slot(req);
1097 failed_lock_cleanup(ns, lock, einfo->ei_mode);
1098 LDLM_LOCK_RELEASE(lock);
1104 LASSERT(reqp != NULL);
1108 LDLM_DEBUG(lock, "sending request");
1110 rc = ptlrpc_queue_wait(req);
1112 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
1113 einfo->ei_mode, flags, lvb, lvb_len,
1117 * If ldlm_cli_enqueue_fini did not find the lock, we need to free
1118 * one reference that we took
1121 LDLM_LOCK_RELEASE(lock);
1126 if (!req_passed_in && req != NULL) {
1127 ptlrpc_req_finished(req);
1134 EXPORT_SYMBOL(ldlm_cli_enqueue);
1137 * Client-side lock convert reply handling.
1139 * Finish client lock converting, checks for concurrent converts
1140 * and clear 'converting' flag so lock can be placed back into LRU.
1142 static int lock_convert_interpret(const struct lu_env *env,
1143 struct ptlrpc_request *req,
1146 struct ldlm_async_args *aa = args;
1147 struct ldlm_lock *lock;
1148 struct ldlm_reply *reply;
1152 lock = ldlm_handle2lock(&aa->lock_handle);
1154 LDLM_DEBUG_NOLOCK("convert ACK for unknown local cookie %#llx",
1155 aa->lock_handle.cookie);
1159 LDLM_DEBUG(lock, "CONVERTED lock:");
1164 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1166 GOTO(out, rc = -EPROTO);
1168 if (reply->lock_handle.cookie != aa->lock_handle.cookie) {
1170 "convert ACK with wrong lock cookie %#llx but cookie %#llx from server %s id %s\n",
1171 aa->lock_handle.cookie, reply->lock_handle.cookie,
1172 req->rq_export->exp_client_uuid.uuid,
1173 libcfs_id2str(req->rq_peer));
1174 GOTO(out, rc = ELDLM_NO_LOCK_DATA);
1177 lock_res_and_lock(lock);
1179 * Lock convert is sent for any new bits to drop, the converting flag
1180 * is dropped when ibits on server are the same as on client. Meanwhile
1181 * that can be so that more later convert will be replied first with
1182 * and clear converting flag, so in case of such race just exit here.
1183 * if lock has no converting bits then
1185 if (!ldlm_is_converting(lock)) {
1187 "convert ACK for lock without converting flag, reply ibits %#llx",
1188 reply->lock_desc.l_policy_data.l_inodebits.bits);
1189 } else if (reply->lock_desc.l_policy_data.l_inodebits.bits !=
1190 lock->l_policy_data.l_inodebits.bits) {
1192 * Compare server returned lock ibits and local lock ibits
1193 * if they are the same we consider convertion is done,
1194 * otherwise we have more converts inflight and keep
1197 LDLM_DEBUG(lock, "convert ACK with ibits %#llx\n",
1198 reply->lock_desc.l_policy_data.l_inodebits.bits);
1200 ldlm_clear_converting(lock);
1203 * Concurrent BL AST may arrive and cause another convert
1204 * or cancel so just do nothing here if bl_ast is set,
1205 * finish with convert otherwise.
1207 if (!ldlm_is_bl_ast(lock)) {
1208 struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
1211 * Drop cancel_bits since there are no more converts
1212 * and put lock into LRU if it is still not used and
1215 lock->l_policy_data.l_inodebits.cancel_bits = 0;
1216 if (!lock->l_readers && !lock->l_writers &&
1217 !ldlm_is_canceling(lock)) {
1218 spin_lock(&ns->ns_lock);
1219 /* there is check for list_empty() inside */
1220 ldlm_lock_remove_from_lru_nolock(lock);
1221 ldlm_lock_add_to_lru_nolock(lock);
1222 spin_unlock(&ns->ns_lock);
1226 unlock_res_and_lock(lock);
1231 lock_res_and_lock(lock);
1232 if (ldlm_is_converting(lock)) {
1233 ldlm_clear_converting(lock);
1234 ldlm_set_cbpending(lock);
1235 ldlm_set_bl_ast(lock);
1236 lock->l_policy_data.l_inodebits.cancel_bits = 0;
1238 unlock_res_and_lock(lock);
1241 * fallback to normal lock cancel. If rc means there is no
1242 * valid lock on server, do only local cancel
1244 if (rc == ELDLM_NO_LOCK_DATA)
1249 rc = ldlm_cli_cancel(&aa->lock_handle, flag);
1251 LDLM_DEBUG(lock, "failed to cancel lock: rc = %d\n",
1254 LDLM_LOCK_PUT(lock);
1259 * Client-side IBITS lock convert.
1261 * Inform server that lock has been converted instead of canceling.
1262 * Server finishes convert on own side and does reprocess to grant
1263 * all related waiting locks.
1265 * Since convert means only ibits downgrading, client doesn't need to
1266 * wait for server reply to finish local converting process so this request
1267 * is made asynchronous.
1270 int ldlm_cli_convert(struct ldlm_lock *lock, __u32 *flags)
1272 struct ldlm_request *body;
1273 struct ptlrpc_request *req;
1274 struct ldlm_async_args *aa;
1275 struct obd_export *exp = lock->l_conn_export;
1280 LDLM_ERROR(lock, "convert must not be called on local locks.");
1285 * this is better to check earlier and it is done so already,
1286 * but this check is kept too as final one to issue an error
1287 * if any new code will miss such check.
1289 if (!exp_connect_lock_convert(exp)) {
1290 LDLM_ERROR(lock, "server doesn't support lock convert\n");
1294 if (lock->l_resource->lr_type != LDLM_IBITS) {
1295 LDLM_ERROR(lock, "convert works with IBITS locks only.");
1299 LDLM_DEBUG(lock, "client-side convert");
1301 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
1302 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1307 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1308 body->lock_handle[0] = lock->l_remote_handle;
1310 body->lock_desc.l_req_mode = lock->l_req_mode;
1311 body->lock_desc.l_granted_mode = lock->l_granted_mode;
1313 body->lock_desc.l_policy_data.l_inodebits.bits =
1314 lock->l_policy_data.l_inodebits.bits;
1315 body->lock_desc.l_policy_data.l_inodebits.cancel_bits = 0;
1317 body->lock_flags = ldlm_flags_to_wire(*flags);
1318 body->lock_count = 1;
1320 ptlrpc_request_set_replen(req);
1323 * Use cancel portals for convert as well as high-priority handling.
1325 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1326 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1328 ptlrpc_at_set_req_timeout(req);
1330 if (exp->exp_obd->obd_svc_stats != NULL)
1331 lprocfs_counter_incr(exp->exp_obd->obd_svc_stats,
1332 LDLM_CONVERT - LDLM_FIRST_OPC);
1334 aa = ptlrpc_req_async_args(aa, req);
1335 ldlm_lock2handle(lock, &aa->lock_handle);
1336 req->rq_interpret_reply = lock_convert_interpret;
1338 ptlrpcd_add_req(req);
1343 * Cancel locks locally.
1345 * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1346 * \retval LDLM_FL_CANCELING otherwise;
1347 * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1349 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1351 __u64 rc = LDLM_FL_LOCAL_ONLY;
1355 if (lock->l_conn_export) {
1358 LDLM_DEBUG(lock, "client-side cancel");
1359 /* Set this flag to prevent others from getting new references*/
1360 lock_res_and_lock(lock);
1361 ldlm_set_cbpending(lock);
1362 local_only = !!(lock->l_flags &
1363 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1364 ldlm_cancel_callback(lock);
1365 rc = (ldlm_is_bl_ast(lock)) ?
1366 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1367 unlock_res_and_lock(lock);
1371 "not sending request (at caller's instruction)\n");
1372 rc = LDLM_FL_LOCAL_ONLY;
1374 ldlm_lock_cancel(lock);
1376 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1377 LDLM_ERROR(lock, "Trying to cancel local lock");
1380 LDLM_DEBUG(lock, "server-side local cancel");
1381 ldlm_lock_cancel(lock);
1382 ldlm_reprocess_all(lock->l_resource, lock);
1389 * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1391 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1392 struct list_head *head, int count)
1394 struct ldlm_request *dlm;
1395 struct ldlm_lock *lock;
1396 int max, packed = 0;
1400 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1401 LASSERT(dlm != NULL);
1403 /* Check the room in the request buffer. */
1404 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1405 sizeof(struct ldlm_request);
1406 max /= sizeof(struct lustre_handle);
1407 max += LDLM_LOCKREQ_HANDLES;
1408 LASSERT(max >= dlm->lock_count + count);
1411 * XXX: it would be better to pack lock handles grouped by resource.
1412 * so that the server cancel would call filter_lvbo_update() less
1415 list_for_each_entry(lock, head, l_bl_ast) {
1418 LASSERT(lock->l_conn_export);
1419 /* Pack the lock handle to the given request buffer. */
1420 LDLM_DEBUG(lock, "packing");
1421 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1424 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1429 * Prepare and send a batched cancel RPC. It will include \a count lock
1430 * handles of locks given in \a cancels list.
1432 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1433 int count, enum ldlm_cancel_flags flags)
1435 struct ptlrpc_request *req = NULL;
1436 struct obd_import *imp;
1442 LASSERT(exp != NULL);
1445 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1447 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1450 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1451 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1456 imp = class_exp2cliimp(exp);
1457 if (imp == NULL || imp->imp_invalid) {
1459 "skipping cancel on invalid import %p\n", imp);
1463 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1465 GOTO(out, rc = -ENOMEM);
1467 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1468 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1469 ldlm_request_bufsize(count, LDLM_CANCEL));
1471 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1473 ptlrpc_request_free(req);
1478 * If OSP want cancel cross-MDT lock, let's not block it in
1479 * in recovery, otherwise the lock will not released, if
1480 * the remote target is also in recovery, and it also need
1481 * this lock, it might cause deadlock.
1483 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1484 exp->exp_obd->obd_lu_dev != NULL &&
1485 exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1486 struct lu_device *top_dev;
1488 top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1489 if (top_dev != NULL &&
1490 top_dev->ld_obd->obd_recovering)
1491 req->rq_allow_replay = 1;
1494 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1495 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1496 ptlrpc_at_set_req_timeout(req);
1498 ldlm_cancel_pack(req, cancels, count);
1500 ptlrpc_request_set_replen(req);
1501 if (flags & LCF_ASYNC) {
1502 ptlrpcd_add_req(req);
1507 rc = ptlrpc_queue_wait(req);
1508 if (rc == LUSTRE_ESTALE) {
1510 "client/server (nid %s) out of sync -- not fatal\n",
1511 libcfs_nid2str(req->rq_import->imp_connection->c_peer.nid));
1513 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1514 req->rq_import_generation == imp->imp_generation) {
1515 ptlrpc_req_finished(req);
1517 } else if (rc != ELDLM_OK) {
1518 /* -ESHUTDOWN is common on umount */
1519 CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1520 "Got rc %d from cancel RPC: canceling anyway\n",
1528 ptlrpc_req_finished(req);
1531 return sent ? sent : rc;
1534 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1536 LASSERT(imp != NULL);
1537 return &imp->imp_obd->obd_namespace->ns_pool;
1541 * Update client's OBD pool related fields with new SLV and Limit from \a req.
1543 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1545 struct obd_device *obd;
1550 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1551 !imp_connect_lru_resize(req->rq_import)))
1552 /* Do nothing for corner cases. */
1556 * In some cases RPC may contain SLV and limit zeroed out. This
1557 * is the case when server does not support LRU resize feature.
1558 * This is also possible in some recovery cases when server-side
1559 * reqs have no reference to the OBD export and thus access to
1560 * server-side namespace is not possible.
1562 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1563 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1564 DEBUG_REQ(D_HA, req,
1565 "Zero SLV or limit found (SLV=%llu, limit=%u)",
1566 lustre_msg_get_slv(req->rq_repmsg),
1567 lustre_msg_get_limit(req->rq_repmsg));
1571 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1572 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1573 obd = req->rq_import->imp_obd;
1576 * Set new SLV and limit in OBD fields to make them accessible
1577 * to the pool thread. We do not access obd_namespace and pool
1578 * directly here as there is no reliable way to make sure that
1579 * they are still alive at cleanup time. Evil races are possible
1580 * which may cause Oops at that time.
1582 write_lock(&obd->obd_pool_lock);
1583 obd->obd_pool_slv = new_slv;
1584 obd->obd_pool_limit = new_limit;
1585 write_unlock(&obd->obd_pool_lock);
1591 * Client side lock cancel.
1593 * Lock must not have any readers or writers by this time.
1595 int ldlm_cli_cancel(const struct lustre_handle *lockh,
1596 enum ldlm_cancel_flags cancel_flags)
1598 struct obd_export *exp;
1599 enum ldlm_lru_flags lru_flags;
1600 int avail, count = 1;
1602 struct ldlm_namespace *ns;
1603 struct ldlm_lock *lock;
1608 lock = ldlm_handle2lock_long(lockh, 0);
1610 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1614 /* Convert lock bits instead of cancel for IBITS locks */
1615 if (cancel_flags & LCF_CONVERT) {
1616 LASSERT(lock->l_resource->lr_type == LDLM_IBITS);
1617 LASSERT(lock->l_policy_data.l_inodebits.cancel_bits != 0);
1619 rc = ldlm_cli_dropbits(lock,
1620 lock->l_policy_data.l_inodebits.cancel_bits);
1622 LDLM_LOCK_RELEASE(lock);
1627 lock_res_and_lock(lock);
1628 /* Lock is being canceled and the caller doesn't want to wait */
1629 if (ldlm_is_canceling(lock)) {
1630 if (cancel_flags & LCF_ASYNC) {
1631 unlock_res_and_lock(lock);
1633 unlock_res_and_lock(lock);
1634 wait_event_idle(lock->l_waitq, is_bl_done(lock));
1636 LDLM_LOCK_RELEASE(lock);
1641 * Lock is being converted, cancel it immediately.
1642 * When convert will end, it releases lock and it will be gone.
1644 if (ldlm_is_converting(lock)) {
1645 /* set back flags removed by convert */
1646 ldlm_set_cbpending(lock);
1647 ldlm_set_bl_ast(lock);
1650 ldlm_set_canceling(lock);
1651 unlock_res_and_lock(lock);
1653 if (cancel_flags & LCF_LOCAL)
1654 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE,
1657 rc = ldlm_cli_cancel_local(lock);
1658 if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1659 LDLM_LOCK_RELEASE(lock);
1663 * Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1664 * RPC which goes to canceld portal, so we can cancel other LRU locks
1665 * here and send them all as one LDLM_CANCEL RPC.
1667 LASSERT(list_empty(&lock->l_bl_ast));
1668 list_add(&lock->l_bl_ast, &cancels);
1670 exp = lock->l_conn_export;
1671 if (exp_connect_cancelset(exp)) {
1672 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1677 ns = ldlm_lock_to_ns(lock);
1678 lru_flags = ns_connect_lru_resize(ns) ?
1679 LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1680 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1681 LCF_BL_AST, lru_flags);
1683 ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1686 EXPORT_SYMBOL(ldlm_cli_cancel);
1689 * Locally cancel up to \a count locks in list \a cancels.
1690 * Return the number of cancelled locks.
1692 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1693 enum ldlm_cancel_flags cancel_flags)
1696 struct ldlm_lock *lock, *next;
1697 int left = 0, bl_ast = 0;
1701 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1705 if (cancel_flags & LCF_LOCAL) {
1706 rc = LDLM_FL_LOCAL_ONLY;
1707 ldlm_lock_cancel(lock);
1709 rc = ldlm_cli_cancel_local(lock);
1712 * Until we have compound requests and can send LDLM_CANCEL
1713 * requests batched with generic RPCs, we need to send cancels
1714 * with the LDLM_FL_BL_AST flag in a separate RPC from
1715 * the one being generated now.
1717 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1718 LDLM_DEBUG(lock, "Cancel lock separately");
1719 list_move(&lock->l_bl_ast, &head);
1723 if (rc == LDLM_FL_LOCAL_ONLY) {
1724 /* CANCEL RPC should not be sent to server. */
1725 list_del_init(&lock->l_bl_ast);
1726 LDLM_LOCK_RELEASE(lock);
1732 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1739 * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1740 * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1741 * readahead requests, ...)
1743 static enum ldlm_policy_res
1744 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1745 int unused, int added, int count)
1747 enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1750 * don't check added & count since we want to process all locks
1752 * It's fine to not take lock to access lock->l_resource since
1753 * the lock has already been granted so it won't change.
1755 switch (lock->l_resource->lr_type) {
1758 if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1762 result = LDLM_POLICY_SKIP_LOCK;
1770 * Callback function for LRU-resize policy. Decides whether to keep
1771 * \a lock in LRU for current \a LRU size \a unused, added in current
1772 * scan \a added and number of locks to be preferably canceled \a count.
1774 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1776 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1778 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1779 struct ldlm_lock *lock,
1780 int unused, int added,
1783 ktime_t cur = ktime_get();
1784 struct ldlm_pool *pl = &ns->ns_pool;
1789 * Stop LRU processing when we reach past @count or have checked all
1792 if (count && added >= count)
1793 return LDLM_POLICY_KEEP_LOCK;
1796 * Despite of the LV, It doesn't make sense to keep the lock which
1797 * is unused for ns_max_age time.
1799 if (ktime_after(ktime_get(),
1800 ktime_add(lock->l_last_used, ns->ns_max_age)))
1801 return LDLM_POLICY_CANCEL_LOCK;
1803 slv = ldlm_pool_get_slv(pl);
1804 lvf = ldlm_pool_get_lvf(pl);
1805 la = div_u64(ktime_to_ns(ktime_sub(cur, lock->l_last_used)),
1807 lv = lvf * la * unused;
1809 /* Inform pool about current CLV to see it via debugfs. */
1810 ldlm_pool_set_clv(pl, lv);
1813 * Stop when SLV is not yet come from server or lv is smaller than
1816 if (slv == 0 || lv < slv)
1817 return LDLM_POLICY_KEEP_LOCK;
1819 return LDLM_POLICY_CANCEL_LOCK;
1822 static enum ldlm_policy_res
1823 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1824 struct ldlm_lock *lock,
1825 int unused, int added,
1828 enum ldlm_policy_res result;
1830 result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1831 if (result == LDLM_POLICY_KEEP_LOCK)
1834 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1838 * Callback function for debugfs used policy. Makes decision whether to keep
1839 * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1840 * added and number of locks to be preferably canceled \a count.
1842 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1844 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1846 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1847 struct ldlm_lock *lock,
1848 int unused, int added,
1852 * Stop LRU processing when we reach past @count or have checked all
1855 return (added >= count) ?
1856 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1860 * Callback function for aged policy. Makes decision whether to keep \a lock in
1861 * LRU for current LRU size \a unused, added in current scan \a added and
1862 * number of locks to be preferably canceled \a count.
1864 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1866 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1868 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1869 struct ldlm_lock *lock,
1870 int unused, int added,
1873 if ((added >= count) &&
1874 ktime_before(ktime_get(),
1875 ktime_add(lock->l_last_used, ns->ns_max_age)))
1876 return LDLM_POLICY_KEEP_LOCK;
1878 return LDLM_POLICY_CANCEL_LOCK;
1881 static enum ldlm_policy_res
1882 ldlm_cancel_aged_no_wait_policy(struct ldlm_namespace *ns,
1883 struct ldlm_lock *lock,
1884 int unused, int added, int count)
1886 enum ldlm_policy_res result;
1888 result = ldlm_cancel_aged_policy(ns, lock, unused, added, count);
1889 if (result == LDLM_POLICY_KEEP_LOCK)
1892 return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1896 * Callback function for default policy. Makes decision whether to keep \a lock
1897 * in LRU for current LRU size \a unused, added in current scan \a added and
1898 * number of locks to be preferably canceled \a count.
1900 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1902 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1905 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1906 struct ldlm_lock *lock,
1907 int unused, int added,
1911 * Stop LRU processing when we reach past count or have checked all
1914 return (added >= count) ?
1915 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1918 typedef enum ldlm_policy_res
1919 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1920 int unused, int added, int count);
1922 static ldlm_cancel_lru_policy_t
1923 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1925 if (ns_connect_lru_resize(ns)) {
1926 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1927 /* We kill passed number of old locks. */
1928 return ldlm_cancel_passed_policy;
1929 if (lru_flags & LDLM_LRU_FLAG_LRUR) {
1930 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1931 return ldlm_cancel_lrur_no_wait_policy;
1933 return ldlm_cancel_lrur_policy;
1935 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1936 return ldlm_cancel_passed_policy;
1938 if (lru_flags & LDLM_LRU_FLAG_AGED) {
1939 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1940 return ldlm_cancel_aged_no_wait_policy;
1942 return ldlm_cancel_aged_policy;
1945 if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1946 return ldlm_cancel_no_wait_policy;
1948 return ldlm_cancel_default_policy;
1952 * - Free space in LRU for \a count new locks,
1953 * redundant unused locks are canceled locally;
1954 * - also cancel locally unused aged locks;
1955 * - do not cancel more than \a max locks;
1956 * - GET the found locks and add them into the \a cancels list.
1958 * A client lock can be added to the l_bl_ast list only when it is
1959 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1960 * CANCEL. There are the following use cases:
1961 * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1962 * ldlm_cli_cancel(), which check and set this flag properly. As any
1963 * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1964 * later without any special locking.
1966 * Calling policies for enabled LRU resize:
1967 * ----------------------------------------
1968 * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1969 * cancel not more than \a count locks;
1971 * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1972 * at the beginning of LRU list);
1974 * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1975 * to memory pressre policy function;
1977 * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1979 * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1980 * (typically before replaying locks) w/o
1981 * sending any RPCs or waiting for any
1982 * outstanding RPC to complete.
1984 * flags & LDLM_CANCEL_CLEANUP - when cancelling read locks, do not check for
1985 * other read locks covering the same pages, just
1986 * discard those pages.
1988 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1989 struct list_head *cancels, int count, int max,
1990 enum ldlm_lru_flags lru_flags)
1992 ldlm_cancel_lru_policy_t pf;
1994 int no_wait = lru_flags & LDLM_LRU_FLAG_NO_WAIT;
1998 if (!ns_connect_lru_resize(ns))
1999 count += ns->ns_nr_unused - ns->ns_max_unused;
2001 pf = ldlm_cancel_lru_policy(ns, lru_flags);
2002 LASSERT(pf != NULL);
2004 /* For any flags, stop scanning if @max is reached. */
2005 while (!list_empty(&ns->ns_unused_list) && (max == 0 || added < max)) {
2006 struct ldlm_lock *lock;
2007 struct list_head *item, *next;
2008 enum ldlm_policy_res result;
2009 ktime_t last_use = ktime_set(0, 0);
2011 spin_lock(&ns->ns_lock);
2012 item = no_wait ? ns->ns_last_pos : &ns->ns_unused_list;
2013 for (item = item->next, next = item->next;
2014 item != &ns->ns_unused_list;
2015 item = next, next = item->next) {
2016 lock = list_entry(item, struct ldlm_lock, l_lru);
2018 /* No locks which got blocking requests. */
2019 LASSERT(!ldlm_is_bl_ast(lock));
2021 if (!ldlm_is_canceling(lock) &&
2022 !ldlm_is_converting(lock))
2026 * Somebody is already doing CANCEL. No need for this
2027 * lock in LRU, do not traverse it again.
2029 ldlm_lock_remove_from_lru_nolock(lock);
2031 if (item == &ns->ns_unused_list) {
2032 spin_unlock(&ns->ns_lock);
2036 last_use = lock->l_last_used;
2038 LDLM_LOCK_GET(lock);
2039 spin_unlock(&ns->ns_lock);
2040 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
2043 * Pass the lock through the policy filter and see if it
2044 * should stay in LRU.
2046 * Even for shrinker policy we stop scanning if
2047 * we find a lock that should stay in the cache.
2048 * We should take into account lock age anyway
2049 * as a new lock is a valuable resource even if
2050 * it has a low weight.
2052 * That is, for shrinker policy we drop only
2053 * old locks, but additionally choose them by
2054 * their weight. Big extent locks will stay in
2057 result = pf(ns, lock, ns->ns_nr_unused, added, count);
2058 if (result == LDLM_POLICY_KEEP_LOCK) {
2059 lu_ref_del(&lock->l_reference, __func__, current);
2060 LDLM_LOCK_RELEASE(lock);
2064 if (result == LDLM_POLICY_SKIP_LOCK) {
2065 lu_ref_del(&lock->l_reference, __func__, current);
2067 spin_lock(&ns->ns_lock);
2068 if (!list_empty(&lock->l_lru) &&
2069 lock->l_lru.prev == ns->ns_last_pos)
2070 ns->ns_last_pos = &lock->l_lru;
2071 spin_unlock(&ns->ns_lock);
2074 LDLM_LOCK_RELEASE(lock);
2078 lock_res_and_lock(lock);
2079 /* Check flags again under the lock. */
2080 if (ldlm_is_canceling(lock) || ldlm_is_converting(lock) ||
2081 ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
2083 * Another thread is removing lock from LRU, or
2084 * somebody is already doing CANCEL, or there
2085 * is a blocking request which will send cancel
2086 * by itself, or the lock is no longer unused or
2087 * the lock has been used since the pf() call and
2088 * pages could be put under it.
2090 unlock_res_and_lock(lock);
2091 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
2092 LDLM_LOCK_RELEASE(lock);
2095 LASSERT(!lock->l_readers && !lock->l_writers);
2098 * If we have chosen to cancel this lock voluntarily, we
2099 * better send cancel notification to server, so that it
2100 * frees appropriate state. This might lead to a race
2101 * where while we are doing cancel here, server is also
2102 * silently cancelling this lock.
2104 ldlm_clear_cancel_on_block(lock);
2107 * Setting the CBPENDING flag is a little misleading,
2108 * but prevents an important race; namely, once
2109 * CBPENDING is set, the lock can accumulate no more
2110 * readers/writers. Since readers and writers are
2111 * already zero here, ldlm_lock_decref() won't see
2112 * this flag and call l_blocking_ast
2114 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
2116 if ((lru_flags & LDLM_LRU_FLAG_CLEANUP) &&
2117 (lock->l_resource->lr_type == LDLM_EXTENT ||
2118 ldlm_has_dom(lock)) && lock->l_granted_mode == LCK_PR)
2119 ldlm_set_discard_data(lock);
2122 * We can't re-add to l_lru as it confuses the
2123 * refcounting in ldlm_lock_remove_from_lru() if an AST
2124 * arrives after we drop lr_lock below. We use l_bl_ast
2125 * and can't use l_pending_chain as it is used both on
2126 * server and client nevertheless b=5666 says it is
2127 * used only on server
2129 LASSERT(list_empty(&lock->l_bl_ast));
2130 list_add(&lock->l_bl_ast, cancels);
2131 unlock_res_and_lock(lock);
2132 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
2138 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
2140 enum ldlm_cancel_flags cancel_flags,
2141 enum ldlm_lru_flags lru_flags)
2145 added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
2149 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
2153 * Cancel at least \a nr locks from given namespace LRU.
2155 * When called with LCF_ASYNC the blocking callback will be handled
2156 * in a thread and this function will return after the thread has been
2157 * asked to call the callback. When called with LCF_ASYNC the blocking
2158 * callback will be performed in this function.
2160 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
2161 enum ldlm_cancel_flags cancel_flags,
2162 enum ldlm_lru_flags lru_flags)
2170 * Just prepare the list of locks, do not actually cancel them yet.
2171 * Locks are cancelled later in a separate thread.
2173 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
2174 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
2182 * Find and cancel locally unused locks found on resource, matched to the
2183 * given policy, mode. GET the found locks and add them into the \a cancels
2186 int ldlm_cancel_resource_local(struct ldlm_resource *res,
2187 struct list_head *cancels,
2188 union ldlm_policy_data *policy,
2189 enum ldlm_mode mode, __u64 lock_flags,
2190 enum ldlm_cancel_flags cancel_flags,
2193 struct ldlm_lock *lock;
2199 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
2200 if (opaque != NULL && lock->l_ast_data != opaque) {
2201 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
2202 lock->l_ast_data, opaque);
2206 if (lock->l_readers || lock->l_writers)
2210 * If somebody is already doing CANCEL, or blocking AST came,
2211 * or lock is being converted then skip this lock.
2213 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock) ||
2214 ldlm_is_converting(lock))
2217 if (lockmode_compat(lock->l_granted_mode, mode))
2221 * If policy is given and this is IBITS lock, add to list only
2222 * those locks that match by policy.
2224 if (policy && (lock->l_resource->lr_type == LDLM_IBITS)) {
2225 if (!(lock->l_policy_data.l_inodebits.bits &
2226 policy->l_inodebits.bits))
2228 /* Skip locks with DoM bit if it is not set in policy
2229 * to don't flush data by side-bits. Lock convert will
2230 * drop those bits separately.
2232 if (ldlm_has_dom(lock) &&
2233 !(policy->l_inodebits.bits & MDS_INODELOCK_DOM))
2237 /* See CBPENDING comment in ldlm_cancel_lru */
2238 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
2241 LASSERT(list_empty(&lock->l_bl_ast));
2242 list_add(&lock->l_bl_ast, cancels);
2243 LDLM_LOCK_GET(lock);
2248 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
2250 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2253 * Cancel client-side locks from a list and send/prepare cancel RPCs to the
2255 * If \a req is NULL, send CANCEL request to server with handles of locks
2256 * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
2257 * separately per lock.
2258 * If \a req is not NULL, put handles of locks in \a cancels into the request
2259 * buffer at the offset \a off.
2260 * Destroy \a cancels at the end.
2262 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
2263 struct ptlrpc_request *req,
2264 enum ldlm_cancel_flags flags)
2266 struct ldlm_lock *lock;
2271 if (list_empty(cancels) || count == 0)
2275 * XXX: requests (both batched and not) could be sent in parallel.
2276 * Usually it is enough to have just 1 RPC, but it is possible that
2277 * there are too many locks to be cancelled in LRU or on a resource.
2278 * It would also speed up the case when the server does not support
2282 LASSERT(!list_empty(cancels));
2283 lock = list_entry(cancels->next, struct ldlm_lock,
2285 LASSERT(lock->l_conn_export);
2287 if (exp_connect_cancelset(lock->l_conn_export)) {
2290 ldlm_cancel_pack(req, cancels, count);
2292 res = ldlm_cli_cancel_req(lock->l_conn_export,
2296 res = ldlm_cli_cancel_req(lock->l_conn_export,
2301 CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
2302 "ldlm_cli_cancel_list: %d\n", res);
2307 ldlm_lock_list_put(cancels, l_bl_ast, res);
2309 LASSERT(count == 0);
2312 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2315 * Cancel all locks on a resource that have 0 readers/writers.
2317 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
2318 * to notify the server.
2320 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
2321 const struct ldlm_res_id *res_id,
2322 union ldlm_policy_data *policy,
2323 enum ldlm_mode mode,
2324 enum ldlm_cancel_flags flags, void *opaque)
2326 struct ldlm_resource *res;
2333 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2335 /* This is not a problem. */
2336 CDEBUG(D_INFO, "No resource %llu\n", res_id->name[0]);
2340 LDLM_RESOURCE_ADDREF(res);
2341 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
2342 0, flags | LCF_BL_AST, opaque);
2343 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
2345 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
2348 LDLM_RESOURCE_DELREF(res);
2349 ldlm_resource_putref(res);
2352 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2354 struct ldlm_cli_cancel_arg {
2360 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2361 struct hlist_node *hnode, void *arg)
2363 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2364 struct ldlm_cli_cancel_arg *lc = arg;
2366 ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2367 NULL, LCK_MINMODE, lc->lc_flags,
2369 /* must return 0 for hash iteration */
2374 * Cancel all locks on a namespace (or a specific resource, if given)
2375 * that have 0 readers/writers.
2377 * If flags & LCF_LOCAL, throw the locks away without trying
2378 * to notify the server.
2380 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2381 const struct ldlm_res_id *res_id,
2382 enum ldlm_cancel_flags flags, void *opaque)
2384 struct ldlm_cli_cancel_arg arg = {
2386 .lc_opaque = opaque,
2394 if (res_id != NULL) {
2395 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2399 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2400 ldlm_cli_hash_cancel_unused, &arg, 0);
2405 /* Lock iterators. */
2407 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2410 struct list_head *tmp, *next;
2411 struct ldlm_lock *lock;
2412 int rc = LDLM_ITER_CONTINUE;
2417 RETURN(LDLM_ITER_CONTINUE);
2420 list_for_each_safe(tmp, next, &res->lr_granted) {
2421 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2423 if (iter(lock, closure) == LDLM_ITER_STOP)
2424 GOTO(out, rc = LDLM_ITER_STOP);
2427 list_for_each_safe(tmp, next, &res->lr_waiting) {
2428 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2430 if (iter(lock, closure) == LDLM_ITER_STOP)
2431 GOTO(out, rc = LDLM_ITER_STOP);
2438 struct iter_helper_data {
2439 ldlm_iterator_t iter;
2443 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2445 struct iter_helper_data *helper = closure;
2447 return helper->iter(lock, helper->closure);
2450 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2451 struct hlist_node *hnode, void *arg)
2454 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2456 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2460 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2461 ldlm_iterator_t iter, void *closure)
2464 struct iter_helper_data helper = { .iter = iter, .closure = closure };
2466 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2467 ldlm_res_iter_helper, &helper, 0);
2472 * non-blocking function to manipulate a lock whose cb_data is being put away.
2473 * return 0: find no resource
2474 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2477 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2478 const struct ldlm_res_id *res_id,
2479 ldlm_iterator_t iter, void *data)
2481 struct ldlm_resource *res;
2486 LASSERTF(ns != NULL, "must pass in namespace\n");
2488 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2492 LDLM_RESOURCE_ADDREF(res);
2493 rc = ldlm_resource_foreach(res, iter, data);
2494 LDLM_RESOURCE_DELREF(res);
2495 ldlm_resource_putref(res);
2498 EXPORT_SYMBOL(ldlm_resource_iterate);
2501 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2503 struct list_head *list = closure;
2505 /* we use l_pending_chain here, because it's unused on clients. */
2506 LASSERTF(list_empty(&lock->l_pending_chain),
2507 "lock %p next %p prev %p\n",
2508 lock, &lock->l_pending_chain.next,
2509 &lock->l_pending_chain.prev);
2511 * b=9573: don't replay locks left after eviction, or
2512 * b=17614: locks being actively cancelled. Get a reference
2513 * on a lock so that it does not disapear under us (e.g. due to cancel)
2515 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_BL_DONE))) {
2516 list_add(&lock->l_pending_chain, list);
2517 LDLM_LOCK_GET(lock);
2520 return LDLM_ITER_CONTINUE;
2523 static int replay_lock_interpret(const struct lu_env *env,
2524 struct ptlrpc_request *req, void *args, int rc)
2526 struct ldlm_async_args *aa = args;
2527 struct ldlm_lock *lock;
2528 struct ldlm_reply *reply;
2529 struct obd_export *exp;
2532 atomic_dec(&req->rq_import->imp_replay_inflight);
2536 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2538 GOTO(out, rc = -EPROTO);
2540 lock = ldlm_handle2lock(&aa->lock_handle);
2542 CERROR("received replay ack for unknown local cookie %#llx remote cookie %#llx from server %s id %s\n",
2543 aa->lock_handle.cookie, reply->lock_handle.cookie,
2544 req->rq_export->exp_client_uuid.uuid,
2545 libcfs_id2str(req->rq_peer));
2546 GOTO(out, rc = -ESTALE);
2549 /* Key change rehash lock in per-export hash with new key */
2550 exp = req->rq_export;
2551 if (exp && exp->exp_lock_hash) {
2553 * In the function below, .hs_keycmp resolves to
2554 * ldlm_export_lock_keycmp()
2556 /* coverity[overrun-buffer-val] */
2557 cfs_hash_rehash_key(exp->exp_lock_hash,
2558 &lock->l_remote_handle,
2559 &reply->lock_handle,
2562 lock->l_remote_handle = reply->lock_handle;
2565 LDLM_DEBUG(lock, "replayed lock:");
2566 ptlrpc_import_recovery_state_machine(req->rq_import);
2567 LDLM_LOCK_PUT(lock);
2570 ptlrpc_connect_import(req->rq_import);
2575 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2577 struct ptlrpc_request *req;
2578 struct ldlm_async_args *aa;
2579 struct ldlm_request *body;
2585 /* b=11974: Do not replay a lock which is actively being canceled */
2586 if (ldlm_is_bl_done(lock)) {
2587 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2592 * If this is reply-less callback lock, we cannot replay it, since
2593 * server might have long dropped it, but notification of that event was
2594 * lost by network. (and server granted conflicting lock already)
2596 if (ldlm_is_cancel_on_block(lock)) {
2597 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2598 ldlm_lock_cancel(lock);
2603 * If granted mode matches the requested mode, this lock is granted.
2605 * If we haven't been granted anything and are on a resource list,
2606 * then we're blocked/waiting.
2608 * If we haven't been granted anything and we're NOT on a resource list,
2609 * then we haven't got a reply yet and don't have a known disposition.
2610 * This happens whenever a lock enqueue is the request that triggers
2613 if (ldlm_is_granted(lock))
2614 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2615 else if (!list_empty(&lock->l_res_link))
2616 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2618 flags = LDLM_FL_REPLAY;
2620 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2621 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2625 /* We're part of recovery, so don't wait for it. */
2626 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2628 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2629 ldlm_lock2desc(lock, &body->lock_desc);
2630 body->lock_flags = ldlm_flags_to_wire(flags);
2632 ldlm_lock2handle(lock, &body->lock_handle[0]);
2633 if (lock->l_lvb_len > 0)
2634 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2635 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2637 ptlrpc_request_set_replen(req);
2639 * notify the server we've replayed all requests.
2640 * also, we mark the request to be put on a dedicated
2641 * queue to be processed after all request replayes.
2644 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2646 LDLM_DEBUG(lock, "replaying lock:");
2648 atomic_inc(&req->rq_import->imp_replay_inflight);
2649 aa = ptlrpc_req_async_args(aa, req);
2650 aa->lock_handle = body->lock_handle[0];
2651 req->rq_interpret_reply = replay_lock_interpret;
2652 ptlrpcd_add_req(req);
2658 * Cancel as many unused locks as possible before replay. since we are
2659 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2662 * Called only in recovery before replaying locks. there is no need to
2663 * replay locks that are unused. since the clients may hold thousands of
2664 * cached unused locks, dropping the unused locks can greatly reduce the
2665 * load on the servers at recovery time.
2667 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2673 "Dropping as many unused locks as possible before replay for namespace %s (%d)\n",
2674 ldlm_ns_name(ns), ns->ns_nr_unused);
2677 * We don't need to care whether or not LRU resize is enabled
2678 * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2681 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2682 LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2684 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2685 canceled, ldlm_ns_name(ns));
2688 int ldlm_replay_locks(struct obd_import *imp)
2690 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2692 struct ldlm_lock *lock, *next;
2697 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2699 /* don't replay locks if import failed recovery */
2700 if (imp->imp_vbr_failed)
2703 /* ensure this doesn't fall to 0 before all have been queued */
2704 atomic_inc(&imp->imp_replay_inflight);
2706 if (ldlm_cancel_unused_locks_before_replay)
2707 ldlm_cancel_unused_locks_for_replay(ns);
2709 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2711 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2712 list_del_init(&lock->l_pending_chain);
2714 LDLM_LOCK_RELEASE(lock);
2715 continue; /* or try to do the rest? */
2717 rc = replay_one_lock(imp, lock);
2718 LDLM_LOCK_RELEASE(lock);
2721 atomic_dec(&imp->imp_replay_inflight);