1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
32 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
40 #define DEBUG_SUBSYSTEM S_LDLM
43 #include <liblustre.h>
46 #include <lustre_dlm.h>
47 #include <obd_class.h>
50 #include "ldlm_internal.h"
52 int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
53 CFS_MODULE_PARM(ldlm_enqueue_min, "i", int, 0644,
54 "lock enqueue timeout minimum");
56 /* in client side, whether the cached locks will be canceled before replay */
57 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
59 static void interrupted_completion_wait(void *data)
63 struct lock_wait_data {
64 struct ldlm_lock *lwd_lock;
68 struct ldlm_async_args {
69 struct lustre_handle lock_handle;
72 int ldlm_expired_completion_wait(void *data)
74 struct lock_wait_data *lwd = data;
75 struct ldlm_lock *lock = lwd->lwd_lock;
76 struct obd_import *imp;
77 struct obd_device *obd;
80 if (lock->l_conn_export == NULL) {
81 static cfs_time_t next_dump = 0, last_dump = 0;
83 if (ptlrpc_check_suspend())
86 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
87 CFS_DURATION_T"s ago); not entering recovery in "
88 "server code, just going back to sleep",
89 lock->l_last_activity,
90 cfs_time_sub(cfs_time_current_sec(),
91 lock->l_last_activity));
92 if (cfs_time_after(cfs_time_current(), next_dump)) {
93 last_dump = next_dump;
94 next_dump = cfs_time_shift(300);
95 ldlm_namespace_dump(D_DLMTRACE,
96 ldlm_lock_to_ns(lock));
98 libcfs_debug_dumplog();
103 obd = lock->l_conn_export->exp_obd;
104 imp = obd->u.cli.cl_import;
105 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
106 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
107 CFS_DURATION_T"s ago), entering recovery for %s@%s",
108 lock->l_last_activity,
109 cfs_time_sub(cfs_time_current_sec(), lock->l_last_activity),
110 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
115 /* We use the same basis for both server side and client side functions
116 from a single node. */
117 int ldlm_get_enq_timeout(struct ldlm_lock *lock)
119 int timeout = at_get(ldlm_lock_to_ns_at(lock));
121 return obd_timeout / 2;
122 /* Since these are non-updating timeouts, we should be conservative.
123 It would be nice to have some kind of "early reply" mechanism for
124 lock callbacks too... */
125 timeout = min_t(int, at_max, timeout + (timeout >> 1)); /* 150% */
126 return max(timeout, ldlm_enqueue_min);
128 EXPORT_SYMBOL(ldlm_get_enq_timeout);
131 * Helper function for ldlm_completion_ast(), updating timings when lock is
134 static int ldlm_completion_tail(struct ldlm_lock *lock)
139 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
140 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
143 delay = cfs_time_sub(cfs_time_current_sec(),
144 lock->l_last_activity);
145 LDLM_DEBUG(lock, "client-side enqueue: granted after "
146 CFS_DURATION_T"s", delay);
148 /* Update our time estimate */
149 at_measured(ldlm_lock_to_ns_at(lock),
157 * Implementation of ->l_completion_ast() for a client, that doesn't wait
158 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
159 * other threads that cannot block for long.
161 int ldlm_completion_ast_async(struct ldlm_lock *lock, int flags, void *data)
165 if (flags == LDLM_FL_WAIT_NOREPROC) {
166 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
170 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
171 LDLM_FL_BLOCK_CONV))) {
172 cfs_waitq_signal(&lock->l_waitq);
173 RETURN(ldlm_completion_tail(lock));
176 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
178 ldlm_lock_dump(D_OTHER, lock, 0);
179 ldlm_reprocess_all(lock->l_resource);
184 * Client side LDLM "completion" AST. This is called in several cases:
186 * - when a reply to an ENQUEUE rpc is received from the server
187 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
188 * this point (determined by flags);
190 * - when LDLM_CP_CALLBACK rpc comes to client to notify it that lock has
193 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
196 * - to force all locks when resource is destroyed (cleanup_resource());
198 * - during lock conversion (not used currently).
200 * If lock is not granted in the first case, this function waits until second
201 * or penultimate cases happen in some other thread.
204 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
206 /* XXX ALLOCATE - 160 bytes */
207 struct lock_wait_data lwd;
208 struct obd_device *obd;
209 struct obd_import *imp = NULL;
210 struct l_wait_info lwi;
215 if (flags == LDLM_FL_WAIT_NOREPROC) {
216 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
220 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
221 LDLM_FL_BLOCK_CONV))) {
222 cfs_waitq_signal(&lock->l_waitq);
226 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
228 ldlm_lock_dump(D_OTHER, lock, 0);
232 obd = class_exp2obd(lock->l_conn_export);
234 /* if this is a local lock, then there is no import */
236 imp = obd->u.cli.cl_import;
239 /* Wait a long time for enqueue - server may have to callback a
240 lock from another client. Server will evict the other client if it
241 doesn't respond reasonably, and then give us the lock. */
242 timeout = ldlm_get_enq_timeout(lock) * 2;
246 if (lock->l_flags & LDLM_FL_NO_TIMEOUT) {
247 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
248 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
250 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
251 ldlm_expired_completion_wait,
252 interrupted_completion_wait, &lwd);
256 cfs_spin_lock(&imp->imp_lock);
257 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
258 cfs_spin_unlock(&imp->imp_lock);
261 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
262 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
263 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
264 lock->l_flags |= LDLM_FL_FAIL_LOC;
267 /* Go to sleep until the lock is granted or cancelled. */
268 rc = l_wait_event(lock->l_waitq,
269 is_granted_or_cancelled(lock), &lwi);
273 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
278 RETURN(ldlm_completion_tail(lock));
282 * A helper to build a blocking ast function
284 * Perform a common operation for blocking asts:
285 * defferred lock cancellation.
287 * \param lock the lock blocking or canceling ast was called on
289 * \see mdt_blocking_ast
290 * \see ldlm_blocking_ast
292 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
297 lock->l_flags |= LDLM_FL_CBPENDING;
298 do_ast = (!lock->l_readers && !lock->l_writers);
299 unlock_res_and_lock(lock);
302 struct lustre_handle lockh;
305 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
306 ldlm_lock2handle(lock, &lockh);
307 rc = ldlm_cli_cancel(&lockh);
309 CERROR("ldlm_cli_cancel: %d\n", rc);
311 LDLM_DEBUG(lock, "Lock still has references, will be "
318 * Server blocking AST
320 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
323 * \param lock the lock which blocks a request or cancelling lock
326 * \param flag indicates whether this cancelling or blocking callback
328 * \see ldlm_blocking_ast_nocheck
330 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
331 void *data, int flag)
335 if (flag == LDLM_CB_CANCELING) {
336 /* Don't need to do anything here. */
340 lock_res_and_lock(lock);
341 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
342 * that ldlm_blocking_ast is called just before intent_policy method
343 * takes the lr_lock, then by the time we get the lock, we might not
344 * be the correct blocking function anymore. So check, and return
346 if (lock->l_blocking_ast != ldlm_blocking_ast) {
347 unlock_res_and_lock(lock);
350 RETURN(ldlm_blocking_ast_nocheck(lock));
354 * ->l_glimpse_ast() for DLM extent locks acquired on the server-side. See
355 * comment in filter_intent_policy() on why you may need this.
357 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
360 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for
361 * that is rather subtle: with OST-side locking, it may so happen that
362 * _all_ extent locks are held by the OST. If client wants to obtain
363 * current file size it calls ll{,u}_glimpse_size(), and (as locks are
364 * on the server), dummy glimpse callback fires and does
365 * nothing. Client still receives correct file size due to the
366 * following fragment in filter_intent_policy():
368 * rc = l->l_glimpse_ast(l, NULL); // this will update the LVB
369 * if (rc != 0 && res->lr_namespace->ns_lvbo &&
370 * res->lr_namespace->ns_lvbo->lvbo_update) {
371 * res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
374 * that is, after glimpse_ast() fails, filter_lvbo_update() runs, and
375 * returns correct file size to the client.
377 return -ELDLM_NO_LOCK_DATA;
380 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
381 const struct ldlm_res_id *res_id,
382 ldlm_type_t type, ldlm_policy_data_t *policy,
383 ldlm_mode_t mode, int *flags,
384 ldlm_blocking_callback blocking,
385 ldlm_completion_callback completion,
386 ldlm_glimpse_callback glimpse,
387 void *data, __u32 lvb_len,
388 const __u64 *client_cookie,
389 struct lustre_handle *lockh)
391 struct ldlm_lock *lock;
393 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
394 .lcs_blocking = blocking,
395 .lcs_glimpse = glimpse,
399 LASSERT(!(*flags & LDLM_FL_REPLAY));
400 if (unlikely(ns_is_client(ns))) {
401 CERROR("Trying to enqueue local lock in a shadow namespace\n");
405 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len);
407 GOTO(out_nolock, err = -ENOMEM);
409 ldlm_lock2handle(lock, lockh);
411 /* NB: we don't have any lock now (lock_res_and_lock)
412 * because it's a new lock */
413 ldlm_lock_addref_internal_nolock(lock, mode);
414 lock->l_flags |= LDLM_FL_LOCAL;
415 if (*flags & LDLM_FL_ATOMIC_CB)
416 lock->l_flags |= LDLM_FL_ATOMIC_CB;
419 lock->l_policy_data = *policy;
420 if (client_cookie != NULL)
421 lock->l_client_cookie = *client_cookie;
422 if (type == LDLM_EXTENT)
423 lock->l_req_extent = policy->l_extent;
425 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
426 if (unlikely(err != ELDLM_OK))
430 *policy = lock->l_policy_data;
432 if (lock->l_completion_ast)
433 lock->l_completion_ast(lock, *flags, NULL);
435 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
438 LDLM_LOCK_RELEASE(lock);
443 static void failed_lock_cleanup(struct ldlm_namespace *ns,
444 struct ldlm_lock *lock, int mode)
448 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
449 lock_res_and_lock(lock);
450 /* Check that lock is not granted or failed, we might race. */
451 if ((lock->l_req_mode != lock->l_granted_mode) &&
452 !(lock->l_flags & LDLM_FL_FAILED)) {
453 /* Make sure that this lock will not be found by raced
454 * bl_ast and -EINVAL reply is sent to server anyways.
456 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
457 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
460 unlock_res_and_lock(lock);
464 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
465 "LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
467 LDLM_DEBUG(lock, "lock was granted or failed in race");
469 ldlm_lock_decref_internal(lock, mode);
471 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
472 * from llite/file.c/ll_file_flock(). */
473 /* This code makes for the fact that we do not have blocking handler on
474 * a client for flock locks. As such this is the place where we must
475 * completely kill failed locks. (interrupted and those that
476 * were waiting to be granted when server evicted us. */
477 if (lock->l_resource->lr_type == LDLM_FLOCK) {
478 lock_res_and_lock(lock);
479 ldlm_resource_unlink_lock(lock);
480 ldlm_lock_destroy_nolock(lock);
481 unlock_res_and_lock(lock);
485 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
486 ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
487 int *flags, void *lvb, __u32 lvb_len,
488 struct lustre_handle *lockh,int rc)
490 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
491 int is_replay = *flags & LDLM_FL_REPLAY;
492 struct ldlm_lock *lock;
493 struct ldlm_reply *reply;
494 struct ost_lvb *tmplvb;
495 int cleanup_phase = 1;
498 lock = ldlm_handle2lock(lockh);
499 /* ldlm_cli_enqueue is holding a reference on this lock. */
501 LASSERT(type == LDLM_FLOCK);
505 if (rc != ELDLM_OK) {
507 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
508 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
509 if (rc == ELDLM_LOCK_ABORTED) {
510 /* Before we return, swab the reply */
511 reply = req_capsule_server_get(&req->rq_pill,
517 req_capsule_set_size(&req->rq_pill,
518 &RMF_DLM_LVB, RCL_SERVER,
520 tmplvb = req_capsule_server_get(&req->rq_pill,
523 GOTO(cleanup, rc = -EPROTO);
525 memcpy(lvb, tmplvb, lvb_len);
531 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
533 GOTO(cleanup, rc = -EPROTO);
535 /* lock enqueued on the server */
538 lock_res_and_lock(lock);
539 /* Key change rehash lock in per-export hash with new key */
540 if (exp->exp_lock_hash) {
541 cfs_hash_rehash_key(exp->exp_lock_hash,
542 &lock->l_remote_handle,
546 lock->l_remote_handle = reply->lock_handle;
549 *flags = reply->lock_flags;
550 lock->l_flags |= reply->lock_flags & LDLM_INHERIT_FLAGS;
551 /* move NO_TIMEOUT flag to the lock to force ldlm_lock_match()
552 * to wait with no timeout as well */
553 lock->l_flags |= reply->lock_flags & LDLM_FL_NO_TIMEOUT;
554 unlock_res_and_lock(lock);
556 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
557 lock, reply->lock_handle.cookie, *flags);
559 /* If enqueue returned a blocked lock but the completion handler has
560 * already run, then it fixed up the resource and we don't need to do it
562 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
563 int newmode = reply->lock_desc.l_req_mode;
565 if (newmode && newmode != lock->l_req_mode) {
566 LDLM_DEBUG(lock, "server returned different mode %s",
567 ldlm_lockname[newmode]);
568 lock->l_req_mode = newmode;
571 if (memcmp(reply->lock_desc.l_resource.lr_name.name,
572 lock->l_resource->lr_name.name,
573 sizeof(struct ldlm_res_id))) {
574 CDEBUG(D_INFO, "remote intent success, locking "
575 "(%ld,%ld,%ld) instead of "
577 (long)reply->lock_desc.l_resource.lr_name.name[0],
578 (long)reply->lock_desc.l_resource.lr_name.name[1],
579 (long)reply->lock_desc.l_resource.lr_name.name[2],
580 (long)lock->l_resource->lr_name.name[0],
581 (long)lock->l_resource->lr_name.name[1],
582 (long)lock->l_resource->lr_name.name[2]);
584 rc = ldlm_lock_change_resource(ns, lock,
585 &reply->lock_desc.l_resource.lr_name);
586 if (rc || lock->l_resource == NULL)
587 GOTO(cleanup, rc = -ENOMEM);
588 LDLM_DEBUG(lock, "client-side enqueue, new resource");
591 if (!(type == LDLM_IBITS && !(exp->exp_connect_flags &
593 /* We assume lock type cannot change on server*/
594 ldlm_convert_policy_to_local(
595 lock->l_resource->lr_type,
596 &reply->lock_desc.l_policy_data,
597 &lock->l_policy_data);
598 if (type != LDLM_PLAIN)
599 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
602 if ((*flags) & LDLM_FL_AST_SENT ||
603 /* Cancel extent locks as soon as possible on a liblustre client,
604 * because it cannot handle asynchronous ASTs robustly (see
606 (LIBLUSTRE_CLIENT && type == LDLM_EXTENT)) {
607 lock_res_and_lock(lock);
608 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
609 unlock_res_and_lock(lock);
610 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
613 /* If the lock has already been granted by a completion AST, don't
614 * clobber the LVB with an older one. */
616 /* We must lock or a racing completion might update lvb
617 without letting us know and we'll clobber the correct value.
618 Cannot unlock after the check either, a that still leaves
619 a tiny window for completion to get in */
620 lock_res_and_lock(lock);
621 if (lock->l_req_mode != lock->l_granted_mode) {
623 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
624 RCL_SERVER, lvb_len);
625 tmplvb = req_capsule_server_get(&req->rq_pill,
627 if (tmplvb == NULL) {
628 unlock_res_and_lock(lock);
629 GOTO(cleanup, rc = -EPROTO);
631 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
633 unlock_res_and_lock(lock);
637 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
638 if (lock->l_completion_ast != NULL) {
639 int err = lock->l_completion_ast(lock, *flags, NULL);
647 if (lvb_len && lvb != NULL) {
648 /* Copy the LVB here, and not earlier, because the completion
649 * AST (if any) can override what we got in the reply */
650 memcpy(lvb, lock->l_lvb_data, lvb_len);
653 LDLM_DEBUG(lock, "client-side enqueue END");
656 if (cleanup_phase == 1 && rc)
657 failed_lock_cleanup(ns, lock, mode);
658 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
660 LDLM_LOCK_RELEASE(lock);
664 /* PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
665 * a single page on the send/receive side. XXX: 512 should be changed
666 * to more adequate value. */
667 static inline int ldlm_req_handles_avail(int req_size, int off)
671 avail = min_t(int, LDLM_MAXREQSIZE, CFS_PAGE_SIZE - 512) - req_size;
672 if (likely(avail >= 0))
673 avail /= (int)sizeof(struct lustre_handle);
676 avail += LDLM_LOCKREQ_HANDLES - off;
681 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
682 enum req_location loc,
685 int size = req_capsule_msg_size(pill, loc);
686 return ldlm_req_handles_avail(size, off);
689 static inline int ldlm_format_handles_avail(struct obd_import *imp,
690 const struct req_format *fmt,
691 enum req_location loc, int off)
693 int size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
694 return ldlm_req_handles_avail(size, off);
697 /* Cancel lru locks and pack them into the enqueue request. Pack there the given
698 * @count locks in @cancels. */
699 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
700 int version, int opc, int canceloff,
701 cfs_list_t *cancels, int count)
703 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
704 struct req_capsule *pill = &req->rq_pill;
705 struct ldlm_request *dlm = NULL;
706 int flags, avail, to_free, bufcount, pack = 0;
713 if (exp_connect_cancelset(exp)) {
714 /* Estimate the amount of available space in the request. */
715 bufcount = req_capsule_filled_sizes(pill, RCL_CLIENT);
716 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
718 flags = ns_connect_lru_resize(ns) ?
719 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
720 to_free = !ns_connect_lru_resize(ns) &&
721 opc == LDLM_ENQUEUE ? 1 : 0;
723 /* Cancel lru locks here _only_ if the server supports
724 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
725 * rpc, what will make us slower. */
727 count += ldlm_cancel_lru_local(ns, cancels, to_free,
728 avail - count, 0, flags);
733 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
734 ldlm_request_bufsize(pack, opc));
737 rc = ptlrpc_request_pack(req, version, opc);
739 ldlm_lock_list_put(cancels, l_bl_ast, count);
743 if (exp_connect_cancelset(exp)) {
745 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
747 /* Skip first lock handler in ldlm_request_pack(),
748 * this method will incrment @lock_count according
749 * to the lock handle amount actually written to
751 dlm->lock_count = canceloff;
753 /* Pack into the request @pack lock handles. */
754 ldlm_cli_cancel_list(cancels, pack, req, 0);
755 /* Prepare and send separate cancel rpc for others. */
756 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
758 ldlm_lock_list_put(cancels, l_bl_ast, count);
763 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
764 cfs_list_t *cancels, int count)
766 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
767 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
770 /* If a request has some specific initialisation it is passed in @reqp,
771 * otherwise it is created in ldlm_cli_enqueue.
773 * Supports sync and async requests, pass @async flag accordingly. If a
774 * request was created in ldlm_cli_enqueue and it is the async request,
775 * pass it to the caller in @reqp. */
776 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
777 struct ldlm_enqueue_info *einfo,
778 const struct ldlm_res_id *res_id,
779 ldlm_policy_data_t const *policy, int *flags,
780 void *lvb, __u32 lvb_len, struct lustre_handle *lockh,
783 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
784 struct ldlm_lock *lock;
785 struct ldlm_request *body;
786 int is_replay = *flags & LDLM_FL_REPLAY;
787 int req_passed_in = 1;
789 struct ptlrpc_request *req;
792 LASSERT(exp != NULL);
794 /* If we're replaying this lock, just check some invariants.
795 * If we're creating a new lock, get everything all setup nice. */
797 lock = ldlm_handle2lock_long(lockh, 0);
798 LASSERT(lock != NULL);
799 LDLM_DEBUG(lock, "client-side enqueue START");
800 LASSERT(exp == lock->l_conn_export);
802 const struct ldlm_callback_suite cbs = {
803 .lcs_completion = einfo->ei_cb_cp,
804 .lcs_blocking = einfo->ei_cb_bl,
805 .lcs_glimpse = einfo->ei_cb_gl,
806 .lcs_weigh = einfo->ei_cb_wg
808 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
809 einfo->ei_mode, &cbs, einfo->ei_cbdata,
813 /* for the local lock, add the reference */
814 ldlm_lock_addref_internal(lock, einfo->ei_mode);
815 ldlm_lock2handle(lock, lockh);
816 if (policy != NULL) {
817 /* INODEBITS_INTEROP: If the server does not support
818 * inodebits, we will request a plain lock in the
819 * descriptor (ldlm_lock2desc() below) but use an
820 * inodebits lock internally with both bits set.
822 if (einfo->ei_type == LDLM_IBITS &&
823 !(exp->exp_connect_flags & OBD_CONNECT_IBITS))
824 lock->l_policy_data.l_inodebits.bits =
825 MDS_INODELOCK_LOOKUP |
826 MDS_INODELOCK_UPDATE;
828 lock->l_policy_data = *policy;
831 if (einfo->ei_type == LDLM_EXTENT)
832 lock->l_req_extent = policy->l_extent;
833 LDLM_DEBUG(lock, "client-side enqueue START");
836 /* lock not sent to server yet */
838 if (reqp == NULL || *reqp == NULL) {
839 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
844 failed_lock_cleanup(ns, lock, einfo->ei_mode);
845 LDLM_LOCK_RELEASE(lock);
855 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
857 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
858 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
861 lock->l_conn_export = exp;
862 lock->l_export = NULL;
863 lock->l_blocking_ast = einfo->ei_cb_bl;
864 lock->l_flags |= (*flags & LDLM_FL_NO_LRU);
866 /* Dump lock data into the request buffer */
867 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
868 ldlm_lock2desc(lock, &body->lock_desc);
869 body->lock_flags = *flags;
870 body->lock_handle[0] = *lockh;
872 /* Continue as normal. */
873 if (!req_passed_in) {
875 req_capsule_extend(&req->rq_pill,
876 &RQF_LDLM_ENQUEUE_LVB);
877 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
878 RCL_SERVER, lvb_len);
880 ptlrpc_request_set_replen(req);
884 * Liblustre client doesn't get extent locks, except for O_APPEND case
885 * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
886 * [i_size, OBD_OBJECT_EOF] lock is taken.
888 LASSERT(ergo(LIBLUSTRE_CLIENT, einfo->ei_type != LDLM_EXTENT ||
889 policy->l_extent.end == OBD_OBJECT_EOF));
892 LASSERT(reqp != NULL);
896 LDLM_DEBUG(lock, "sending request");
898 rc = ptlrpc_queue_wait(req);
900 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
901 einfo->ei_mode, flags, lvb, lvb_len,
904 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
905 * one reference that we took */
907 LDLM_LOCK_RELEASE(lock);
911 if (!req_passed_in && req != NULL) {
912 ptlrpc_req_finished(req);
920 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
923 struct ldlm_resource *res;
926 if (ns_is_client(ldlm_lock_to_ns(lock))) {
927 CERROR("Trying to cancel local lock\n");
930 LDLM_DEBUG(lock, "client-side local convert");
932 res = ldlm_lock_convert(lock, new_mode, flags);
934 ldlm_reprocess_all(res);
939 LDLM_DEBUG(lock, "client-side local convert handler END");
944 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
945 * conversion of locks which are on the waiting or converting queue */
946 /* Caller of this code is supposed to take care of lock readers/writers
948 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, __u32 *flags)
950 struct ldlm_request *body;
951 struct ldlm_reply *reply;
952 struct ldlm_lock *lock;
953 struct ldlm_resource *res;
954 struct ptlrpc_request *req;
958 lock = ldlm_handle2lock(lockh);
965 if (lock->l_conn_export == NULL)
966 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
968 LDLM_DEBUG(lock, "client-side convert");
970 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
971 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
978 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
979 body->lock_handle[0] = lock->l_remote_handle;
981 body->lock_desc.l_req_mode = new_mode;
982 body->lock_flags = *flags;
985 ptlrpc_request_set_replen(req);
986 rc = ptlrpc_queue_wait(req);
990 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
992 GOTO(out, rc = -EPROTO);
995 GOTO(out, rc = req->rq_status);
997 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
999 ldlm_reprocess_all(res);
1000 /* Go to sleep until the lock is granted. */
1001 /* FIXME: or cancelled. */
1002 if (lock->l_completion_ast) {
1003 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1013 LDLM_LOCK_PUT(lock);
1014 ptlrpc_req_finished(req);
1018 /* Cancel locks locally.
1020 * LDLM_FL_LOCAL_ONLY if tere is no need in a CANCEL rpc to the server;
1021 * LDLM_FL_CANCELING otherwise;
1022 * LDLM_FL_BL_AST if there is a need in a separate CANCEL rpc. */
1023 static int ldlm_cli_cancel_local(struct ldlm_lock *lock)
1025 int rc = LDLM_FL_LOCAL_ONLY;
1028 if (lock->l_conn_export) {
1031 LDLM_DEBUG(lock, "client-side cancel");
1032 /* Set this flag to prevent others from getting new references*/
1033 lock_res_and_lock(lock);
1034 lock->l_flags |= LDLM_FL_CBPENDING;
1035 local_only = (lock->l_flags &
1036 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1037 ldlm_cancel_callback(lock);
1038 rc = (lock->l_flags & LDLM_FL_BL_AST) ?
1039 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1040 unlock_res_and_lock(lock);
1043 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1045 rc = LDLM_FL_LOCAL_ONLY;
1047 ldlm_lock_cancel(lock);
1049 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1050 LDLM_ERROR(lock, "Trying to cancel local lock");
1053 LDLM_DEBUG(lock, "server-side local cancel");
1054 ldlm_lock_cancel(lock);
1055 ldlm_reprocess_all(lock->l_resource);
1061 /* Pack @count locks in @head into ldlm_request buffer at the offset @off,
1062 of the request @req. */
1063 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1064 cfs_list_t *head, int count)
1066 struct ldlm_request *dlm;
1067 struct ldlm_lock *lock;
1068 int max, packed = 0;
1071 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1072 LASSERT(dlm != NULL);
1074 /* Check the room in the request buffer. */
1075 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1076 sizeof(struct ldlm_request);
1077 max /= sizeof(struct lustre_handle);
1078 max += LDLM_LOCKREQ_HANDLES;
1079 LASSERT(max >= dlm->lock_count + count);
1081 /* XXX: it would be better to pack lock handles grouped by resource.
1082 * so that the server cancel would call filter_lvbo_update() less
1084 cfs_list_for_each_entry(lock, head, l_bl_ast) {
1087 LASSERT(lock->l_conn_export);
1088 /* Pack the lock handle to the given request buffer. */
1089 LDLM_DEBUG(lock, "packing");
1090 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1093 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1097 /* Prepare and send a batched cancel rpc, it will include count lock handles
1098 * of locks given in @head. */
1099 int ldlm_cli_cancel_req(struct obd_export *exp, cfs_list_t *cancels,
1100 int count, ldlm_cancel_flags_t flags)
1102 struct ptlrpc_request *req = NULL;
1103 struct obd_import *imp;
1108 LASSERT(exp != NULL);
1111 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1113 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1116 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1117 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1124 imp = class_exp2cliimp(exp);
1125 if (imp == NULL || imp->imp_invalid) {
1127 "skipping cancel on invalid import %p\n", imp);
1131 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1133 GOTO(out, rc = -ENOMEM);
1135 bufcount = req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1136 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1137 ldlm_request_bufsize(count, LDLM_CANCEL));
1139 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1141 ptlrpc_request_free(req);
1144 req->rq_no_resend = 1;
1145 req->rq_no_delay = 1;
1147 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1148 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1149 ptlrpc_at_set_req_timeout(req);
1151 ldlm_cancel_pack(req, cancels, count);
1153 ptlrpc_request_set_replen(req);
1154 if (flags & LCF_ASYNC) {
1155 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
1159 rc = ptlrpc_queue_wait(req);
1162 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1163 "out of sync -- not fatal\n",
1164 libcfs_nid2str(req->rq_import->
1165 imp_connection->c_peer.nid));
1167 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1168 req->rq_import_generation == imp->imp_generation) {
1169 ptlrpc_req_finished(req);
1171 } else if (rc != ELDLM_OK) {
1172 CERROR("Got rc %d from cancel RPC: canceling "
1180 ptlrpc_req_finished(req);
1183 return sent ? sent : rc;
1186 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1188 LASSERT(imp != NULL);
1189 return &imp->imp_obd->obd_namespace->ns_pool;
1193 * Update client's obd pool related fields with new SLV and Limit from \a req.
1195 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1197 struct obd_device *obd;
1198 __u64 old_slv, new_slv;
1201 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1202 !imp_connect_lru_resize(req->rq_import)))
1205 * Do nothing for corner cases.
1211 * In some cases RPC may contain slv and limit zeroed out. This is
1212 * the case when server does not support lru resize feature. This is
1213 * also possible in some recovery cases when server side reqs have no
1214 * ref to obd export and thus access to server side namespace is no
1217 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1218 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1219 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1220 "(SLV: "LPU64", Limit: %u)",
1221 lustre_msg_get_slv(req->rq_repmsg),
1222 lustre_msg_get_limit(req->rq_repmsg));
1226 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1227 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1228 obd = req->rq_import->imp_obd;
1231 * Set new SLV and Limit to obd fields to make accessible for pool
1232 * thread. We do not access obd_namespace and pool directly here
1233 * as there is no reliable way to make sure that they are still
1234 * alive in cleanup time. Evil races are possible which may cause
1235 * oops in that time.
1237 cfs_write_lock(&obd->obd_pool_lock);
1238 old_slv = obd->obd_pool_slv;
1239 obd->obd_pool_slv = new_slv;
1240 obd->obd_pool_limit = new_limit;
1241 cfs_write_unlock(&obd->obd_pool_lock);
1245 EXPORT_SYMBOL(ldlm_cli_update_pool);
1247 int ldlm_cli_cancel(struct lustre_handle *lockh)
1249 struct obd_export *exp;
1250 int avail, flags, count = 1, rc = 0;
1251 struct ldlm_namespace *ns;
1252 struct ldlm_lock *lock;
1253 CFS_LIST_HEAD(cancels);
1256 /* concurrent cancels on the same handle can happen */
1257 lock = ldlm_handle2lock_long(lockh, LDLM_FL_CANCELING);
1259 LDLM_DEBUG_NOLOCK("lock is already being destroyed\n");
1263 rc = ldlm_cli_cancel_local(lock);
1264 if (rc < 0 || rc == LDLM_FL_LOCAL_ONLY) {
1265 LDLM_LOCK_RELEASE(lock);
1266 RETURN(rc < 0 ? rc : 0);
1268 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1269 * rpc which goes to canceld portal, so we can cancel other lru locks
1270 * here and send them all as one LDLM_CANCEL rpc. */
1271 LASSERT(cfs_list_empty(&lock->l_bl_ast));
1272 cfs_list_add(&lock->l_bl_ast, &cancels);
1274 exp = lock->l_conn_export;
1275 if (exp_connect_cancelset(exp)) {
1276 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1281 ns = ldlm_lock_to_ns(lock);
1282 flags = ns_connect_lru_resize(ns) ?
1283 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
1284 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1287 ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1291 /* XXX until we will have compound requests and can cut cancels from generic rpc
1292 * we need send cancels with LDLM_FL_BL_AST flag as separate rpc */
1293 int ldlm_cli_cancel_list_local(cfs_list_t *cancels, int count,
1294 ldlm_cancel_flags_t flags)
1296 CFS_LIST_HEAD(head);
1297 struct ldlm_lock *lock, *next;
1298 int left = 0, bl_ast = 0, rc;
1301 cfs_list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1305 if (flags & LCF_LOCAL) {
1306 rc = LDLM_FL_LOCAL_ONLY;
1307 ldlm_lock_cancel(lock);
1309 rc = ldlm_cli_cancel_local(lock);
1311 if (!(flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1312 LDLM_DEBUG(lock, "Cancel lock separately");
1313 cfs_list_del_init(&lock->l_bl_ast);
1314 cfs_list_add(&lock->l_bl_ast, &head);
1318 if (rc == LDLM_FL_LOCAL_ONLY) {
1319 /* CANCEL RPC should not be sent to server. */
1320 cfs_list_del_init(&lock->l_bl_ast);
1321 LDLM_LOCK_RELEASE(lock);
1328 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1335 * Cancel as many locks as possible w/o sending any rpcs (e.g. to write back
1336 * dirty data, to close a file, ...) or waiting for any rpcs in-flight (e.g.
1337 * readahead requests, ...)
1339 static ldlm_policy_res_t ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns,
1340 struct ldlm_lock *lock,
1341 int unused, int added,
1344 ldlm_policy_res_t result = LDLM_POLICY_CANCEL_LOCK;
1345 ldlm_cancel_for_recovery cb = ns->ns_cancel_for_recovery;
1346 lock_res_and_lock(lock);
1348 /* don't check added & count since we want to process all locks
1349 * from unused list */
1350 switch (lock->l_resource->lr_type) {
1356 result = LDLM_POLICY_SKIP_LOCK;
1357 lock->l_flags |= LDLM_FL_SKIPPED;
1361 unlock_res_and_lock(lock);
1366 * Callback function for lru-resize policy. Makes decision whether to keep
1367 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1368 * \a added and number of locks to be preferably canceled \a count.
1370 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1372 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1374 static ldlm_policy_res_t ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1375 struct ldlm_lock *lock,
1376 int unused, int added,
1379 cfs_time_t cur = cfs_time_current();
1380 struct ldlm_pool *pl = &ns->ns_pool;
1385 * Stop lru processing when we reached passed @count or checked all
1388 if (count && added >= count)
1389 return LDLM_POLICY_KEEP_LOCK;
1391 slv = ldlm_pool_get_slv(pl);
1392 lvf = ldlm_pool_get_lvf(pl);
1393 la = cfs_duration_sec(cfs_time_sub(cur,
1394 lock->l_last_used));
1397 * Stop when slv is not yet come from server or lv is smaller than
1400 lv = lvf * la * unused;
1403 * Inform pool about current CLV to see it via proc.
1405 ldlm_pool_set_clv(pl, lv);
1406 return (slv == 0 || lv < slv) ?
1407 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1411 * Callback function for proc used policy. Makes decision whether to keep
1412 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1413 * \a added and number of locks to be preferably canceled \a count.
1415 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1417 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1419 static ldlm_policy_res_t ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1420 struct ldlm_lock *lock,
1421 int unused, int added,
1425 * Stop lru processing when we reached passed @count or checked all
1428 return (added >= count) ?
1429 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1433 * Callback function for aged policy. Makes decision whether to keep
1434 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1435 * \a added and number of locks to be preferably canceled \a count.
1437 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1439 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1441 static ldlm_policy_res_t ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1442 struct ldlm_lock *lock,
1443 int unused, int added,
1447 * Stop lru processing if young lock is found and we reached passed
1450 return ((added >= count) &&
1451 cfs_time_before(cfs_time_current(),
1452 cfs_time_add(lock->l_last_used,
1454 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1458 * Callback function for default policy. Makes decision whether to keep
1459 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1460 * \a added and number of locks to be preferably canceled \a count.
1462 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1464 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1466 static ldlm_policy_res_t ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1467 struct ldlm_lock *lock,
1468 int unused, int added,
1472 * Stop lru processing when we reached passed @count or checked all
1475 return (added >= count) ?
1476 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1479 typedef ldlm_policy_res_t (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *,
1480 struct ldlm_lock *, int,
1483 static ldlm_cancel_lru_policy_t
1484 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, int flags)
1486 if (flags & LDLM_CANCEL_NO_WAIT)
1487 return ldlm_cancel_no_wait_policy;
1489 if (ns_connect_lru_resize(ns)) {
1490 if (flags & LDLM_CANCEL_SHRINK)
1491 /* We kill passed number of old locks. */
1492 return ldlm_cancel_passed_policy;
1493 else if (flags & LDLM_CANCEL_LRUR)
1494 return ldlm_cancel_lrur_policy;
1495 else if (flags & LDLM_CANCEL_PASSED)
1496 return ldlm_cancel_passed_policy;
1498 if (flags & LDLM_CANCEL_AGED)
1499 return ldlm_cancel_aged_policy;
1502 return ldlm_cancel_default_policy;
1505 /* - Free space in lru for @count new locks,
1506 * redundant unused locks are canceled locally;
1507 * - also cancel locally unused aged locks;
1508 * - do not cancel more than @max locks;
1509 * - GET the found locks and add them into the @cancels list.
1511 * A client lock can be added to the l_bl_ast list only when it is
1512 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing CANCEL.
1513 * There are the following use cases: ldlm_cancel_resource_local(),
1514 * ldlm_cancel_lru_local() and ldlm_cli_cancel(), which check&set this
1515 * flag properly. As any attempt to cancel a lock rely on this flag,
1516 * l_bl_ast list is accessed later without any special locking.
1518 * Calling policies for enabled lru resize:
1519 * ----------------------------------------
1520 * flags & LDLM_CANCEL_LRUR - use lru resize policy (SLV from server) to
1521 * cancel not more than @count locks;
1523 * flags & LDLM_CANCEL_PASSED - cancel @count number of old locks (located at
1524 * the beginning of lru list);
1526 * flags & LDLM_CANCEL_SHRINK - cancel not more than @count locks according to
1527 * memory pressre policy function;
1529 * flags & LDLM_CANCEL_AGED - cancel alocks according to "aged policy".
1531 * flags & LDLM_CANCEL_NO_WAIT - cancel as many unused locks as possible
1532 * (typically before replaying locks) w/o
1533 * sending any rpcs or waiting for any
1534 * outstanding rpc to complete.
1536 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns, cfs_list_t *cancels,
1537 int count, int max, int flags)
1539 ldlm_cancel_lru_policy_t pf;
1540 struct ldlm_lock *lock, *next;
1541 int added = 0, unused, remained;
1544 cfs_spin_lock(&ns->ns_lock);
1545 unused = ns->ns_nr_unused;
1548 if (!ns_connect_lru_resize(ns))
1549 count += unused - ns->ns_max_unused;
1551 pf = ldlm_cancel_lru_policy(ns, flags);
1552 LASSERT(pf != NULL);
1554 while (!cfs_list_empty(&ns->ns_unused_list)) {
1555 ldlm_policy_res_t result;
1557 /* all unused locks */
1558 if (remained-- <= 0)
1561 /* For any flags, stop scanning if @max is reached. */
1562 if (max && added >= max)
1565 cfs_list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1567 /* No locks which got blocking requests. */
1568 LASSERT(!(lock->l_flags & LDLM_FL_BL_AST));
1570 if (flags & LDLM_CANCEL_NO_WAIT &&
1571 lock->l_flags & LDLM_FL_SKIPPED)
1572 /* already processed */
1575 /* Somebody is already doing CANCEL. No need in this
1576 * lock in lru, do not traverse it again. */
1577 if (!(lock->l_flags & LDLM_FL_CANCELING))
1580 ldlm_lock_remove_from_lru_nolock(lock);
1582 if (&lock->l_lru == &ns->ns_unused_list)
1585 LDLM_LOCK_GET(lock);
1586 cfs_spin_unlock(&ns->ns_lock);
1587 lu_ref_add(&lock->l_reference, __FUNCTION__, cfs_current());
1589 /* Pass the lock through the policy filter and see if it
1590 * should stay in lru.
1592 * Even for shrinker policy we stop scanning if
1593 * we find a lock that should stay in the cache.
1594 * We should take into account lock age anyway
1595 * as new lock even if it is small of weight is
1596 * valuable resource.
1598 * That is, for shrinker policy we drop only
1599 * old locks, but additionally chose them by
1600 * their weight. Big extent locks will stay in
1602 result = pf(ns, lock, unused, added, count);
1603 if (result == LDLM_POLICY_KEEP_LOCK) {
1604 lu_ref_del(&lock->l_reference,
1605 __FUNCTION__, cfs_current());
1606 LDLM_LOCK_RELEASE(lock);
1607 cfs_spin_lock(&ns->ns_lock);
1610 if (result == LDLM_POLICY_SKIP_LOCK) {
1611 lu_ref_del(&lock->l_reference,
1612 __FUNCTION__, cfs_current());
1613 LDLM_LOCK_RELEASE(lock);
1614 cfs_spin_lock(&ns->ns_lock);
1618 lock_res_and_lock(lock);
1619 /* Check flags again under the lock. */
1620 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1621 (ldlm_lock_remove_from_lru(lock) == 0)) {
1622 /* other thread is removing lock from lru or
1623 * somebody is already doing CANCEL or
1624 * there is a blocking request which will send
1625 * cancel by itseft or the lock is matched
1626 * is already not unused. */
1627 unlock_res_and_lock(lock);
1628 lu_ref_del(&lock->l_reference,
1629 __FUNCTION__, cfs_current());
1630 LDLM_LOCK_RELEASE(lock);
1631 cfs_spin_lock(&ns->ns_lock);
1634 LASSERT(!lock->l_readers && !lock->l_writers);
1636 /* If we have chosen to cancel this lock voluntarily, we
1637 * better send cancel notification to server, so that it
1638 * frees appropriate state. This might lead to a race
1639 * where while we are doing cancel here, server is also
1640 * silently cancelling this lock. */
1641 lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
1643 /* Setting the CBPENDING flag is a little misleading,
1644 * but prevents an important race; namely, once
1645 * CBPENDING is set, the lock can accumulate no more
1646 * readers/writers. Since readers and writers are
1647 * already zero here, ldlm_lock_decref() won't see
1648 * this flag and call l_blocking_ast */
1649 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1651 /* We can't re-add to l_lru as it confuses the
1652 * refcounting in ldlm_lock_remove_from_lru() if an AST
1653 * arrives after we drop lr_lock below. We use l_bl_ast
1654 * and can't use l_pending_chain as it is used both on
1655 * server and client nevertheless bug 5666 says it is
1656 * used only on server */
1657 LASSERT(cfs_list_empty(&lock->l_bl_ast));
1658 cfs_list_add(&lock->l_bl_ast, cancels);
1659 unlock_res_and_lock(lock);
1660 lu_ref_del(&lock->l_reference, __FUNCTION__, cfs_current());
1661 cfs_spin_lock(&ns->ns_lock);
1665 cfs_spin_unlock(&ns->ns_lock);
1669 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, cfs_list_t *cancels,
1670 int count, int max, ldlm_cancel_flags_t cancel_flags,
1674 added = ldlm_prepare_lru_list(ns, cancels, count, max, flags);
1677 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1680 /* when called with LDLM_ASYNC the blocking callback will be handled
1681 * in a thread and this function will return after the thread has been
1682 * asked to call the callback. when called with LDLM_SYNC the blocking
1683 * callback will be performed in this function. */
1684 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr, ldlm_sync_t mode,
1687 CFS_LIST_HEAD(cancels);
1692 mode = LDLM_SYNC; /* force to be sync in user space */
1694 /* Just prepare the list of locks, do not actually cancel them yet.
1695 * Locks are cancelled later in a separate thread. */
1696 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, flags);
1697 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, mode);
1704 /* Find and cancel locally unused locks found on resource, matched to the
1705 * given policy, mode. GET the found locks and add them into the @cancels
1707 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1708 cfs_list_t *cancels,
1709 ldlm_policy_data_t *policy,
1710 ldlm_mode_t mode, int lock_flags,
1711 ldlm_cancel_flags_t cancel_flags, void *opaque)
1713 struct ldlm_lock *lock;
1718 cfs_list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1719 if (opaque != NULL && lock->l_ast_data != opaque) {
1720 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1721 lock->l_ast_data, opaque);
1726 if (lock->l_readers || lock->l_writers)
1729 /* If somebody is already doing CANCEL, or blocking ast came,
1730 * skip this lock. */
1731 if (lock->l_flags & LDLM_FL_BL_AST ||
1732 lock->l_flags & LDLM_FL_CANCELING)
1735 if (lockmode_compat(lock->l_granted_mode, mode))
1738 /* If policy is given and this is IBITS lock, add to list only
1739 * those locks that match by policy. */
1740 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1741 !(lock->l_policy_data.l_inodebits.bits &
1742 policy->l_inodebits.bits))
1745 /* See CBPENDING comment in ldlm_cancel_lru */
1746 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1749 LASSERT(cfs_list_empty(&lock->l_bl_ast));
1750 cfs_list_add(&lock->l_bl_ast, cancels);
1751 LDLM_LOCK_GET(lock);
1756 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1759 /* If @req is NULL, send CANCEL request to server with handles of locks
1760 * in the @cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1761 * separately per lock.
1762 * If @req is not NULL, put handles of locks in @cancels into the request
1763 * buffer at the offset @off.
1764 * Destroy @cancels at the end. */
1765 int ldlm_cli_cancel_list(cfs_list_t *cancels, int count,
1766 struct ptlrpc_request *req, ldlm_cancel_flags_t flags)
1768 struct ldlm_lock *lock;
1772 if (cfs_list_empty(cancels) || count == 0)
1775 /* XXX: requests (both batched and not) could be sent in parallel.
1776 * Usually it is enough to have just 1 RPC, but it is possible that
1777 * there are to many locks to be cancelled in LRU or on a resource.
1778 * It would also speed up the case when the server does not support
1781 LASSERT(!cfs_list_empty(cancels));
1782 lock = cfs_list_entry(cancels->next, struct ldlm_lock,
1784 LASSERT(lock->l_conn_export);
1786 if (exp_connect_cancelset(lock->l_conn_export)) {
1789 ldlm_cancel_pack(req, cancels, count);
1791 res = ldlm_cli_cancel_req(lock->l_conn_export,
1795 res = ldlm_cli_cancel_req(lock->l_conn_export,
1800 CERROR("ldlm_cli_cancel_list: %d\n", res);
1805 ldlm_lock_list_put(cancels, l_bl_ast, res);
1807 LASSERT(count == 0);
1811 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1812 const struct ldlm_res_id *res_id,
1813 ldlm_policy_data_t *policy,
1815 ldlm_cancel_flags_t flags,
1818 struct ldlm_resource *res;
1819 CFS_LIST_HEAD(cancels);
1824 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1826 /* This is not a problem. */
1827 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1831 LDLM_RESOURCE_ADDREF(res);
1832 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1833 0, flags | LCF_BL_AST, opaque);
1834 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1836 CERROR("ldlm_cli_cancel_unused_resource: %d\n", rc);
1838 LDLM_RESOURCE_DELREF(res);
1839 ldlm_resource_putref(res);
1843 struct ldlm_cli_cancel_arg {
1848 static int ldlm_cli_hash_cancel_unused(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1849 cfs_hlist_node_t *hnode, void *arg)
1851 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1852 struct ldlm_cli_cancel_arg *lc = arg;
1855 rc = ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
1857 lc->lc_flags, lc->lc_opaque);
1859 CERROR("ldlm_cli_cancel_unused ("LPU64"): %d\n",
1860 res->lr_name.name[0], rc);
1862 /* must return 0 for hash iteration */
1866 /* Cancel all locks on a namespace (or a specific resource, if given)
1867 * that have 0 readers/writers.
1869 * If flags & LCF_LOCAL, throw the locks away without trying
1870 * to notify the server. */
1871 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
1872 const struct ldlm_res_id *res_id,
1873 ldlm_cancel_flags_t flags, void *opaque)
1875 struct ldlm_cli_cancel_arg arg = {
1877 .lc_opaque = opaque,
1885 if (res_id != NULL) {
1886 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
1890 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1891 ldlm_cli_hash_cancel_unused, &arg);
1896 /* Lock iterators. */
1898 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1901 cfs_list_t *tmp, *next;
1902 struct ldlm_lock *lock;
1903 int rc = LDLM_ITER_CONTINUE;
1908 RETURN(LDLM_ITER_CONTINUE);
1911 cfs_list_for_each_safe(tmp, next, &res->lr_granted) {
1912 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1914 if (iter(lock, closure) == LDLM_ITER_STOP)
1915 GOTO(out, rc = LDLM_ITER_STOP);
1918 cfs_list_for_each_safe(tmp, next, &res->lr_converting) {
1919 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1921 if (iter(lock, closure) == LDLM_ITER_STOP)
1922 GOTO(out, rc = LDLM_ITER_STOP);
1925 cfs_list_for_each_safe(tmp, next, &res->lr_waiting) {
1926 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1928 if (iter(lock, closure) == LDLM_ITER_STOP)
1929 GOTO(out, rc = LDLM_ITER_STOP);
1936 struct iter_helper_data {
1937 ldlm_iterator_t iter;
1941 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1943 struct iter_helper_data *helper = closure;
1944 return helper->iter(lock, helper->closure);
1947 static int ldlm_res_iter_helper(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1948 cfs_hlist_node_t *hnode, void *arg)
1951 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1953 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
1957 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
1958 ldlm_iterator_t iter, void *closure)
1961 struct iter_helper_data helper = { iter: iter, closure: closure };
1963 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1964 ldlm_res_iter_helper, &helper);
1968 /* non-blocking function to manipulate a lock whose cb_data is being put away.
1969 * return 0: find no resource
1970 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
1973 int ldlm_resource_iterate(struct ldlm_namespace *ns,
1974 const struct ldlm_res_id *res_id,
1975 ldlm_iterator_t iter, void *data)
1977 struct ldlm_resource *res;
1982 CERROR("must pass in namespace\n");
1986 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1990 LDLM_RESOURCE_ADDREF(res);
1991 rc = ldlm_resource_foreach(res, iter, data);
1992 LDLM_RESOURCE_DELREF(res);
1993 ldlm_resource_putref(res);
1999 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2001 cfs_list_t *list = closure;
2003 /* we use l_pending_chain here, because it's unused on clients. */
2004 LASSERTF(cfs_list_empty(&lock->l_pending_chain),
2005 "lock %p next %p prev %p\n",
2006 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2007 /* bug 9573: don't replay locks left after eviction, or
2008 * bug 17614: locks being actively cancelled. Get a reference
2009 * on a lock so that it does not disapear under us (e.g. due to cancel)
2011 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_CANCELING))) {
2012 cfs_list_add(&lock->l_pending_chain, list);
2013 LDLM_LOCK_GET(lock);
2016 return LDLM_ITER_CONTINUE;
2019 static int replay_lock_interpret(const struct lu_env *env,
2020 struct ptlrpc_request *req,
2021 struct ldlm_async_args *aa, int rc)
2023 struct ldlm_lock *lock;
2024 struct ldlm_reply *reply;
2025 struct obd_export *exp;
2028 cfs_atomic_dec(&req->rq_import->imp_replay_inflight);
2033 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2035 GOTO(out, rc = -EPROTO);
2037 lock = ldlm_handle2lock(&aa->lock_handle);
2039 CERROR("received replay ack for unknown local cookie "LPX64
2040 " remote cookie "LPX64 " from server %s id %s\n",
2041 aa->lock_handle.cookie, reply->lock_handle.cookie,
2042 req->rq_export->exp_client_uuid.uuid,
2043 libcfs_id2str(req->rq_peer));
2044 GOTO(out, rc = -ESTALE);
2047 /* Key change rehash lock in per-export hash with new key */
2048 exp = req->rq_export;
2049 if (exp && exp->exp_lock_hash) {
2050 cfs_hash_rehash_key(exp->exp_lock_hash,
2051 &lock->l_remote_handle,
2052 &reply->lock_handle,
2055 lock->l_remote_handle = reply->lock_handle;
2058 LDLM_DEBUG(lock, "replayed lock:");
2059 ptlrpc_import_recovery_state_machine(req->rq_import);
2060 LDLM_LOCK_PUT(lock);
2063 ptlrpc_connect_import(req->rq_import);
2068 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2070 struct ptlrpc_request *req;
2071 struct ldlm_async_args *aa;
2072 struct ldlm_request *body;
2077 /* Bug 11974: Do not replay a lock which is actively being canceled */
2078 if (lock->l_flags & LDLM_FL_CANCELING) {
2079 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2083 /* If this is reply-less callback lock, we cannot replay it, since
2084 * server might have long dropped it, but notification of that event was
2085 * lost by network. (and server granted conflicting lock already) */
2086 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
2087 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2088 ldlm_lock_cancel(lock);
2092 * If granted mode matches the requested mode, this lock is granted.
2094 * If they differ, but we have a granted mode, then we were granted
2095 * one mode and now want another: ergo, converting.
2097 * If we haven't been granted anything and are on a resource list,
2098 * then we're blocked/waiting.
2100 * If we haven't been granted anything and we're NOT on a resource list,
2101 * then we haven't got a reply yet and don't have a known disposition.
2102 * This happens whenever a lock enqueue is the request that triggers
2105 if (lock->l_granted_mode == lock->l_req_mode)
2106 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2107 else if (lock->l_granted_mode)
2108 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2109 else if (!cfs_list_empty(&lock->l_res_link))
2110 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2112 flags = LDLM_FL_REPLAY;
2114 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2115 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2119 /* We're part of recovery, so don't wait for it. */
2120 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2122 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2123 ldlm_lock2desc(lock, &body->lock_desc);
2124 body->lock_flags = flags;
2126 ldlm_lock2handle(lock, &body->lock_handle[0]);
2127 if (lock->l_lvb_len != 0) {
2128 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2129 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2132 ptlrpc_request_set_replen(req);
2133 /* notify the server we've replayed all requests.
2134 * also, we mark the request to be put on a dedicated
2135 * queue to be processed after all request replayes.
2137 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2139 LDLM_DEBUG(lock, "replaying lock:");
2141 cfs_atomic_inc(&req->rq_import->imp_replay_inflight);
2142 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2143 aa = ptlrpc_req_async_args(req);
2144 aa->lock_handle = body->lock_handle[0];
2145 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2146 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2152 * Cancel as many unused locks as possible before replay. since we are
2153 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2156 * Called only in recovery before replaying locks. there is no need to
2157 * replay locks that are unused. since the clients may hold thousands of
2158 * cached unused locks, dropping the unused locks can greatly reduce the
2159 * load on the servers at recovery time.
2161 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2164 CFS_LIST_HEAD(cancels);
2166 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2167 "replay for namespace %s (%d)\n",
2168 ldlm_ns_name(ns), ns->ns_nr_unused);
2170 /* We don't need to care whether or not LRU resize is enabled
2171 * because the LDLM_CANCEL_NO_WAIT policy doesn't use the
2172 * count parameter */
2173 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2174 LCF_LOCAL, LDLM_CANCEL_NO_WAIT);
2176 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2177 canceled, ldlm_ns_name(ns));
2180 int ldlm_replay_locks(struct obd_import *imp)
2182 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2183 CFS_LIST_HEAD(list);
2184 struct ldlm_lock *lock, *next;
2189 LASSERT(cfs_atomic_read(&imp->imp_replay_inflight) == 0);
2191 /* don't replay locks if import failed recovery */
2192 if (imp->imp_vbr_failed)
2195 /* ensure this doesn't fall to 0 before all have been queued */
2196 cfs_atomic_inc(&imp->imp_replay_inflight);
2198 if (ldlm_cancel_unused_locks_before_replay)
2199 ldlm_cancel_unused_locks_for_replay(ns);
2201 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2203 cfs_list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2204 cfs_list_del_init(&lock->l_pending_chain);
2206 LDLM_LOCK_RELEASE(lock);
2207 continue; /* or try to do the rest? */
2209 rc = replay_one_lock(imp, lock);
2210 LDLM_LOCK_RELEASE(lock);
2213 cfs_atomic_dec(&imp->imp_replay_inflight);