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;
865 /* Dump lock data into the request buffer */
866 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
867 ldlm_lock2desc(lock, &body->lock_desc);
868 body->lock_flags = *flags;
869 body->lock_handle[0] = *lockh;
871 /* Continue as normal. */
872 if (!req_passed_in) {
874 req_capsule_extend(&req->rq_pill,
875 &RQF_LDLM_ENQUEUE_LVB);
876 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
877 RCL_SERVER, lvb_len);
879 ptlrpc_request_set_replen(req);
883 * Liblustre client doesn't get extent locks, except for O_APPEND case
884 * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
885 * [i_size, OBD_OBJECT_EOF] lock is taken.
887 LASSERT(ergo(LIBLUSTRE_CLIENT, einfo->ei_type != LDLM_EXTENT ||
888 policy->l_extent.end == OBD_OBJECT_EOF));
891 LASSERT(reqp != NULL);
895 LDLM_DEBUG(lock, "sending request");
897 rc = ptlrpc_queue_wait(req);
899 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
900 einfo->ei_mode, flags, lvb, lvb_len,
903 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
904 * one reference that we took */
906 LDLM_LOCK_RELEASE(lock);
910 if (!req_passed_in && req != NULL) {
911 ptlrpc_req_finished(req);
919 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
922 struct ldlm_resource *res;
925 if (ns_is_client(ldlm_lock_to_ns(lock))) {
926 CERROR("Trying to cancel local lock\n");
929 LDLM_DEBUG(lock, "client-side local convert");
931 res = ldlm_lock_convert(lock, new_mode, flags);
933 ldlm_reprocess_all(res);
938 LDLM_DEBUG(lock, "client-side local convert handler END");
943 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
944 * conversion of locks which are on the waiting or converting queue */
945 /* Caller of this code is supposed to take care of lock readers/writers
947 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, __u32 *flags)
949 struct ldlm_request *body;
950 struct ldlm_reply *reply;
951 struct ldlm_lock *lock;
952 struct ldlm_resource *res;
953 struct ptlrpc_request *req;
957 lock = ldlm_handle2lock(lockh);
964 if (lock->l_conn_export == NULL)
965 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
967 LDLM_DEBUG(lock, "client-side convert");
969 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
970 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
977 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
978 body->lock_handle[0] = lock->l_remote_handle;
980 body->lock_desc.l_req_mode = new_mode;
981 body->lock_flags = *flags;
984 ptlrpc_request_set_replen(req);
985 rc = ptlrpc_queue_wait(req);
989 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
991 GOTO(out, rc = -EPROTO);
994 GOTO(out, rc = req->rq_status);
996 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
998 ldlm_reprocess_all(res);
999 /* Go to sleep until the lock is granted. */
1000 /* FIXME: or cancelled. */
1001 if (lock->l_completion_ast) {
1002 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1012 LDLM_LOCK_PUT(lock);
1013 ptlrpc_req_finished(req);
1017 /* Cancel locks locally.
1019 * LDLM_FL_LOCAL_ONLY if tere is no need in a CANCEL rpc to the server;
1020 * LDLM_FL_CANCELING otherwise;
1021 * LDLM_FL_BL_AST if there is a need in a separate CANCEL rpc. */
1022 static int ldlm_cli_cancel_local(struct ldlm_lock *lock)
1024 int rc = LDLM_FL_LOCAL_ONLY;
1027 if (lock->l_conn_export) {
1030 LDLM_DEBUG(lock, "client-side cancel");
1031 /* Set this flag to prevent others from getting new references*/
1032 lock_res_and_lock(lock);
1033 lock->l_flags |= LDLM_FL_CBPENDING;
1034 local_only = (lock->l_flags &
1035 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1036 ldlm_cancel_callback(lock);
1037 rc = (lock->l_flags & LDLM_FL_BL_AST) ?
1038 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1039 unlock_res_and_lock(lock);
1042 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1044 rc = LDLM_FL_LOCAL_ONLY;
1046 ldlm_lock_cancel(lock);
1048 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1049 LDLM_ERROR(lock, "Trying to cancel local lock");
1052 LDLM_DEBUG(lock, "server-side local cancel");
1053 ldlm_lock_cancel(lock);
1054 ldlm_reprocess_all(lock->l_resource);
1060 /* Pack @count locks in @head into ldlm_request buffer at the offset @off,
1061 of the request @req. */
1062 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1063 cfs_list_t *head, int count)
1065 struct ldlm_request *dlm;
1066 struct ldlm_lock *lock;
1067 int max, packed = 0;
1070 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1071 LASSERT(dlm != NULL);
1073 /* Check the room in the request buffer. */
1074 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1075 sizeof(struct ldlm_request);
1076 max /= sizeof(struct lustre_handle);
1077 max += LDLM_LOCKREQ_HANDLES;
1078 LASSERT(max >= dlm->lock_count + count);
1080 /* XXX: it would be better to pack lock handles grouped by resource.
1081 * so that the server cancel would call filter_lvbo_update() less
1083 cfs_list_for_each_entry(lock, head, l_bl_ast) {
1086 LASSERT(lock->l_conn_export);
1087 /* Pack the lock handle to the given request buffer. */
1088 LDLM_DEBUG(lock, "packing");
1089 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1092 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1096 /* Prepare and send a batched cancel rpc, it will include count lock handles
1097 * of locks given in @head. */
1098 int ldlm_cli_cancel_req(struct obd_export *exp, cfs_list_t *cancels,
1099 int count, ldlm_cancel_flags_t flags)
1101 struct ptlrpc_request *req = NULL;
1102 struct obd_import *imp;
1107 LASSERT(exp != NULL);
1110 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1112 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1115 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1116 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1123 imp = class_exp2cliimp(exp);
1124 if (imp == NULL || imp->imp_invalid) {
1126 "skipping cancel on invalid import %p\n", imp);
1130 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1132 GOTO(out, rc = -ENOMEM);
1134 bufcount = req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1135 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1136 ldlm_request_bufsize(count, LDLM_CANCEL));
1138 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1140 ptlrpc_request_free(req);
1143 req->rq_no_resend = 1;
1144 req->rq_no_delay = 1;
1146 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1147 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1148 ptlrpc_at_set_req_timeout(req);
1150 ldlm_cancel_pack(req, cancels, count);
1152 ptlrpc_request_set_replen(req);
1153 if (flags & LCF_ASYNC) {
1154 ptlrpcd_add_req(req, PSCOPE_OTHER);
1158 rc = ptlrpc_queue_wait(req);
1161 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1162 "out of sync -- not fatal\n",
1163 libcfs_nid2str(req->rq_import->
1164 imp_connection->c_peer.nid));
1166 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1167 req->rq_import_generation == imp->imp_generation) {
1168 ptlrpc_req_finished(req);
1170 } else if (rc != ELDLM_OK) {
1171 CERROR("Got rc %d from cancel RPC: canceling "
1179 ptlrpc_req_finished(req);
1182 return sent ? sent : rc;
1185 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1187 LASSERT(imp != NULL);
1188 return &imp->imp_obd->obd_namespace->ns_pool;
1192 * Update client's obd pool related fields with new SLV and Limit from \a req.
1194 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1196 struct obd_device *obd;
1197 __u64 old_slv, new_slv;
1200 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1201 !imp_connect_lru_resize(req->rq_import)))
1204 * Do nothing for corner cases.
1210 * In some cases RPC may contain slv and limit zeroed out. This is
1211 * the case when server does not support lru resize feature. This is
1212 * also possible in some recovery cases when server side reqs have no
1213 * ref to obd export and thus access to server side namespace is no
1216 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1217 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1218 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1219 "(SLV: "LPU64", Limit: %u)",
1220 lustre_msg_get_slv(req->rq_repmsg),
1221 lustre_msg_get_limit(req->rq_repmsg));
1225 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1226 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1227 obd = req->rq_import->imp_obd;
1230 * Set new SLV and Limit to obd fields to make accessible for pool
1231 * thread. We do not access obd_namespace and pool directly here
1232 * as there is no reliable way to make sure that they are still
1233 * alive in cleanup time. Evil races are possible which may cause
1234 * oops in that time.
1236 cfs_write_lock(&obd->obd_pool_lock);
1237 old_slv = obd->obd_pool_slv;
1238 obd->obd_pool_slv = new_slv;
1239 obd->obd_pool_limit = new_limit;
1240 cfs_write_unlock(&obd->obd_pool_lock);
1244 EXPORT_SYMBOL(ldlm_cli_update_pool);
1246 int ldlm_cli_cancel(struct lustre_handle *lockh)
1248 struct obd_export *exp;
1249 int avail, flags, count = 1, rc = 0;
1250 struct ldlm_namespace *ns;
1251 struct ldlm_lock *lock;
1252 CFS_LIST_HEAD(cancels);
1255 /* concurrent cancels on the same handle can happen */
1256 lock = ldlm_handle2lock_long(lockh, LDLM_FL_CANCELING);
1258 LDLM_DEBUG_NOLOCK("lock is already being destroyed\n");
1262 rc = ldlm_cli_cancel_local(lock);
1263 if (rc < 0 || rc == LDLM_FL_LOCAL_ONLY) {
1264 LDLM_LOCK_RELEASE(lock);
1265 RETURN(rc < 0 ? rc : 0);
1267 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1268 * rpc which goes to canceld portal, so we can cancel other lru locks
1269 * here and send them all as one LDLM_CANCEL rpc. */
1270 LASSERT(cfs_list_empty(&lock->l_bl_ast));
1271 cfs_list_add(&lock->l_bl_ast, &cancels);
1273 exp = lock->l_conn_export;
1274 if (exp_connect_cancelset(exp)) {
1275 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1280 ns = ldlm_lock_to_ns(lock);
1281 flags = ns_connect_lru_resize(ns) ?
1282 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
1283 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1286 ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1290 /* XXX until we will have compound requests and can cut cancels from generic rpc
1291 * we need send cancels with LDLM_FL_BL_AST flag as separate rpc */
1292 int ldlm_cli_cancel_list_local(cfs_list_t *cancels, int count,
1293 ldlm_cancel_flags_t flags)
1295 CFS_LIST_HEAD(head);
1296 struct ldlm_lock *lock, *next;
1297 int left = 0, bl_ast = 0, rc;
1300 cfs_list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1304 if (flags & LCF_LOCAL) {
1305 rc = LDLM_FL_LOCAL_ONLY;
1306 ldlm_lock_cancel(lock);
1308 rc = ldlm_cli_cancel_local(lock);
1310 if (!(flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1311 LDLM_DEBUG(lock, "Cancel lock separately");
1312 cfs_list_del_init(&lock->l_bl_ast);
1313 cfs_list_add(&lock->l_bl_ast, &head);
1317 if (rc == LDLM_FL_LOCAL_ONLY) {
1318 /* CANCEL RPC should not be sent to server. */
1319 cfs_list_del_init(&lock->l_bl_ast);
1320 LDLM_LOCK_RELEASE(lock);
1327 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1334 * Cancel as many locks as possible w/o sending any rpcs (e.g. to write back
1335 * dirty data, to close a file, ...) or waiting for any rpcs in-flight (e.g.
1336 * readahead requests, ...)
1338 static ldlm_policy_res_t ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns,
1339 struct ldlm_lock *lock,
1340 int unused, int added,
1343 ldlm_policy_res_t result = LDLM_POLICY_CANCEL_LOCK;
1344 ldlm_cancel_for_recovery cb = ns->ns_cancel_for_recovery;
1345 lock_res_and_lock(lock);
1347 /* don't check added & count since we want to process all locks
1348 * from unused list */
1349 switch (lock->l_resource->lr_type) {
1355 result = LDLM_POLICY_SKIP_LOCK;
1356 lock->l_flags |= LDLM_FL_SKIPPED;
1360 unlock_res_and_lock(lock);
1365 * Callback function for lru-resize policy. Makes decision whether to keep
1366 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1367 * \a added and number of locks to be preferably canceled \a count.
1369 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1371 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1373 static ldlm_policy_res_t ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1374 struct ldlm_lock *lock,
1375 int unused, int added,
1378 cfs_time_t cur = cfs_time_current();
1379 struct ldlm_pool *pl = &ns->ns_pool;
1384 * Stop lru processing when we reached passed @count or checked all
1387 if (count && added >= count)
1388 return LDLM_POLICY_KEEP_LOCK;
1390 slv = ldlm_pool_get_slv(pl);
1391 lvf = ldlm_pool_get_lvf(pl);
1392 la = cfs_duration_sec(cfs_time_sub(cur,
1393 lock->l_last_used));
1396 * Stop when slv is not yet come from server or lv is smaller than
1399 lv = lvf * la * unused;
1402 * Inform pool about current CLV to see it via proc.
1404 ldlm_pool_set_clv(pl, lv);
1405 return (slv == 0 || lv < slv) ?
1406 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1410 * Callback function for proc used policy. Makes decision whether to keep
1411 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1412 * \a added and number of locks to be preferably canceled \a count.
1414 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1416 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1418 static ldlm_policy_res_t ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1419 struct ldlm_lock *lock,
1420 int unused, int added,
1424 * Stop lru processing when we reached passed @count or checked all
1427 return (added >= count) ?
1428 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1432 * Callback function for aged policy. Makes decision whether to keep
1433 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1434 * \a added and number of locks to be preferably canceled \a count.
1436 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1438 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1440 static ldlm_policy_res_t ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1441 struct ldlm_lock *lock,
1442 int unused, int added,
1446 * Stop lru processing if young lock is found and we reached passed
1449 return ((added >= count) &&
1450 cfs_time_before(cfs_time_current(),
1451 cfs_time_add(lock->l_last_used,
1453 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1457 * Callback function for default policy. Makes decision whether to keep
1458 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1459 * \a added and number of locks to be preferably canceled \a count.
1461 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1463 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1465 static ldlm_policy_res_t ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1466 struct ldlm_lock *lock,
1467 int unused, int added,
1471 * Stop lru processing when we reached passed @count or checked all
1474 return (added >= count) ?
1475 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1478 typedef ldlm_policy_res_t (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *,
1479 struct ldlm_lock *, int,
1482 static ldlm_cancel_lru_policy_t
1483 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, int flags)
1485 if (flags & LDLM_CANCEL_NO_WAIT)
1486 return ldlm_cancel_no_wait_policy;
1488 if (ns_connect_lru_resize(ns)) {
1489 if (flags & LDLM_CANCEL_SHRINK)
1490 /* We kill passed number of old locks. */
1491 return ldlm_cancel_passed_policy;
1492 else if (flags & LDLM_CANCEL_LRUR)
1493 return ldlm_cancel_lrur_policy;
1494 else if (flags & LDLM_CANCEL_PASSED)
1495 return ldlm_cancel_passed_policy;
1497 if (flags & LDLM_CANCEL_AGED)
1498 return ldlm_cancel_aged_policy;
1501 return ldlm_cancel_default_policy;
1504 /* - Free space in lru for @count new locks,
1505 * redundant unused locks are canceled locally;
1506 * - also cancel locally unused aged locks;
1507 * - do not cancel more than @max locks;
1508 * - GET the found locks and add them into the @cancels list.
1510 * A client lock can be added to the l_bl_ast list only when it is
1511 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing CANCEL.
1512 * There are the following use cases: ldlm_cancel_resource_local(),
1513 * ldlm_cancel_lru_local() and ldlm_cli_cancel(), which check&set this
1514 * flag properly. As any attempt to cancel a lock rely on this flag,
1515 * l_bl_ast list is accessed later without any special locking.
1517 * Calling policies for enabled lru resize:
1518 * ----------------------------------------
1519 * flags & LDLM_CANCEL_LRUR - use lru resize policy (SLV from server) to
1520 * cancel not more than @count locks;
1522 * flags & LDLM_CANCEL_PASSED - cancel @count number of old locks (located at
1523 * the beginning of lru list);
1525 * flags & LDLM_CANCEL_SHRINK - cancel not more than @count locks according to
1526 * memory pressre policy function;
1528 * flags & LDLM_CANCEL_AGED - cancel alocks according to "aged policy".
1530 * flags & LDLM_CANCEL_NO_WAIT - cancel as many unused locks as possible
1531 * (typically before replaying locks) w/o
1532 * sending any rpcs or waiting for any
1533 * outstanding rpc to complete.
1535 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns, cfs_list_t *cancels,
1536 int count, int max, int flags)
1538 ldlm_cancel_lru_policy_t pf;
1539 struct ldlm_lock *lock, *next;
1540 int added = 0, unused, remained;
1543 cfs_spin_lock(&ns->ns_lock);
1544 unused = ns->ns_nr_unused;
1547 if (!ns_connect_lru_resize(ns))
1548 count += unused - ns->ns_max_unused;
1550 pf = ldlm_cancel_lru_policy(ns, flags);
1551 LASSERT(pf != NULL);
1553 while (!cfs_list_empty(&ns->ns_unused_list)) {
1554 ldlm_policy_res_t result;
1556 /* all unused locks */
1557 if (remained-- <= 0)
1560 /* For any flags, stop scanning if @max is reached. */
1561 if (max && added >= max)
1564 cfs_list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1566 /* No locks which got blocking requests. */
1567 LASSERT(!(lock->l_flags & LDLM_FL_BL_AST));
1569 if (flags & LDLM_CANCEL_NO_WAIT &&
1570 lock->l_flags & LDLM_FL_SKIPPED)
1571 /* already processed */
1574 /* Somebody is already doing CANCEL. No need in this
1575 * lock in lru, do not traverse it again. */
1576 if (!(lock->l_flags & LDLM_FL_CANCELING))
1579 ldlm_lock_remove_from_lru_nolock(lock);
1581 if (&lock->l_lru == &ns->ns_unused_list)
1584 LDLM_LOCK_GET(lock);
1585 cfs_spin_unlock(&ns->ns_lock);
1586 lu_ref_add(&lock->l_reference, __FUNCTION__, cfs_current());
1588 /* Pass the lock through the policy filter and see if it
1589 * should stay in lru.
1591 * Even for shrinker policy we stop scanning if
1592 * we find a lock that should stay in the cache.
1593 * We should take into account lock age anyway
1594 * as new lock even if it is small of weight is
1595 * valuable resource.
1597 * That is, for shrinker policy we drop only
1598 * old locks, but additionally chose them by
1599 * their weight. Big extent locks will stay in
1601 result = pf(ns, lock, unused, added, count);
1602 if (result == LDLM_POLICY_KEEP_LOCK) {
1603 lu_ref_del(&lock->l_reference,
1604 __FUNCTION__, cfs_current());
1605 LDLM_LOCK_RELEASE(lock);
1606 cfs_spin_lock(&ns->ns_lock);
1609 if (result == LDLM_POLICY_SKIP_LOCK) {
1610 lu_ref_del(&lock->l_reference,
1611 __FUNCTION__, cfs_current());
1612 LDLM_LOCK_RELEASE(lock);
1613 cfs_spin_lock(&ns->ns_lock);
1617 lock_res_and_lock(lock);
1618 /* Check flags again under the lock. */
1619 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1620 (ldlm_lock_remove_from_lru(lock) == 0)) {
1621 /* other thread is removing lock from lru or
1622 * somebody is already doing CANCEL or
1623 * there is a blocking request which will send
1624 * cancel by itseft or the lock is matched
1625 * is already not unused. */
1626 unlock_res_and_lock(lock);
1627 lu_ref_del(&lock->l_reference,
1628 __FUNCTION__, cfs_current());
1629 LDLM_LOCK_RELEASE(lock);
1630 cfs_spin_lock(&ns->ns_lock);
1633 LASSERT(!lock->l_readers && !lock->l_writers);
1635 /* If we have chosen to cancel this lock voluntarily, we
1636 * better send cancel notification to server, so that it
1637 * frees appropriate state. This might lead to a race
1638 * where while we are doing cancel here, server is also
1639 * silently cancelling this lock. */
1640 lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
1642 /* Setting the CBPENDING flag is a little misleading,
1643 * but prevents an important race; namely, once
1644 * CBPENDING is set, the lock can accumulate no more
1645 * readers/writers. Since readers and writers are
1646 * already zero here, ldlm_lock_decref() won't see
1647 * this flag and call l_blocking_ast */
1648 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1650 /* We can't re-add to l_lru as it confuses the
1651 * refcounting in ldlm_lock_remove_from_lru() if an AST
1652 * arrives after we drop lr_lock below. We use l_bl_ast
1653 * and can't use l_pending_chain as it is used both on
1654 * server and client nevertheless bug 5666 says it is
1655 * used only on server */
1656 LASSERT(cfs_list_empty(&lock->l_bl_ast));
1657 cfs_list_add(&lock->l_bl_ast, cancels);
1658 unlock_res_and_lock(lock);
1659 lu_ref_del(&lock->l_reference, __FUNCTION__, cfs_current());
1660 cfs_spin_lock(&ns->ns_lock);
1664 cfs_spin_unlock(&ns->ns_lock);
1668 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, cfs_list_t *cancels,
1669 int count, int max, ldlm_cancel_flags_t cancel_flags,
1673 added = ldlm_prepare_lru_list(ns, cancels, count, max, flags);
1676 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1679 /* when called with LDLM_ASYNC the blocking callback will be handled
1680 * in a thread and this function will return after the thread has been
1681 * asked to call the callback. when called with LDLM_SYNC the blocking
1682 * callback will be performed in this function. */
1683 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr, ldlm_sync_t mode,
1686 CFS_LIST_HEAD(cancels);
1691 mode = LDLM_SYNC; /* force to be sync in user space */
1693 /* Just prepare the list of locks, do not actually cancel them yet.
1694 * Locks are cancelled later in a separate thread. */
1695 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, flags);
1696 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, mode);
1703 /* Find and cancel locally unused locks found on resource, matched to the
1704 * given policy, mode. GET the found locks and add them into the @cancels
1706 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1707 cfs_list_t *cancels,
1708 ldlm_policy_data_t *policy,
1709 ldlm_mode_t mode, int lock_flags,
1710 ldlm_cancel_flags_t cancel_flags, void *opaque)
1712 struct ldlm_lock *lock;
1717 cfs_list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1718 if (opaque != NULL && lock->l_ast_data != opaque) {
1719 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1720 lock->l_ast_data, opaque);
1725 if (lock->l_readers || lock->l_writers)
1728 /* If somebody is already doing CANCEL, or blocking ast came,
1729 * skip this lock. */
1730 if (lock->l_flags & LDLM_FL_BL_AST ||
1731 lock->l_flags & LDLM_FL_CANCELING)
1734 if (lockmode_compat(lock->l_granted_mode, mode))
1737 /* If policy is given and this is IBITS lock, add to list only
1738 * those locks that match by policy. */
1739 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1740 !(lock->l_policy_data.l_inodebits.bits &
1741 policy->l_inodebits.bits))
1744 /* See CBPENDING comment in ldlm_cancel_lru */
1745 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1748 LASSERT(cfs_list_empty(&lock->l_bl_ast));
1749 cfs_list_add(&lock->l_bl_ast, cancels);
1750 LDLM_LOCK_GET(lock);
1755 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1758 /* If @req is NULL, send CANCEL request to server with handles of locks
1759 * in the @cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1760 * separately per lock.
1761 * If @req is not NULL, put handles of locks in @cancels into the request
1762 * buffer at the offset @off.
1763 * Destroy @cancels at the end. */
1764 int ldlm_cli_cancel_list(cfs_list_t *cancels, int count,
1765 struct ptlrpc_request *req, ldlm_cancel_flags_t flags)
1767 struct ldlm_lock *lock;
1771 if (cfs_list_empty(cancels) || count == 0)
1774 /* XXX: requests (both batched and not) could be sent in parallel.
1775 * Usually it is enough to have just 1 RPC, but it is possible that
1776 * there are to many locks to be cancelled in LRU or on a resource.
1777 * It would also speed up the case when the server does not support
1780 LASSERT(!cfs_list_empty(cancels));
1781 lock = cfs_list_entry(cancels->next, struct ldlm_lock,
1783 LASSERT(lock->l_conn_export);
1785 if (exp_connect_cancelset(lock->l_conn_export)) {
1788 ldlm_cancel_pack(req, cancels, count);
1790 res = ldlm_cli_cancel_req(lock->l_conn_export,
1794 res = ldlm_cli_cancel_req(lock->l_conn_export,
1799 CERROR("ldlm_cli_cancel_list: %d\n", res);
1804 ldlm_lock_list_put(cancels, l_bl_ast, res);
1806 LASSERT(count == 0);
1810 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1811 const struct ldlm_res_id *res_id,
1812 ldlm_policy_data_t *policy,
1814 ldlm_cancel_flags_t flags,
1817 struct ldlm_resource *res;
1818 CFS_LIST_HEAD(cancels);
1823 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1825 /* This is not a problem. */
1826 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1830 LDLM_RESOURCE_ADDREF(res);
1831 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1832 0, flags | LCF_BL_AST, opaque);
1833 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1835 CERROR("ldlm_cli_cancel_unused_resource: %d\n", rc);
1837 LDLM_RESOURCE_DELREF(res);
1838 ldlm_resource_putref(res);
1842 struct ldlm_cli_cancel_arg {
1847 static int ldlm_cli_hash_cancel_unused(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1848 cfs_hlist_node_t *hnode, void *arg)
1850 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1851 struct ldlm_cli_cancel_arg *lc = arg;
1854 rc = ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
1856 lc->lc_flags, lc->lc_opaque);
1858 CERROR("ldlm_cli_cancel_unused ("LPU64"): %d\n",
1859 res->lr_name.name[0], rc);
1861 /* must return 0 for hash iteration */
1865 /* Cancel all locks on a namespace (or a specific resource, if given)
1866 * that have 0 readers/writers.
1868 * If flags & LCF_LOCAL, throw the locks away without trying
1869 * to notify the server. */
1870 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
1871 const struct ldlm_res_id *res_id,
1872 ldlm_cancel_flags_t flags, void *opaque)
1874 struct ldlm_cli_cancel_arg arg = {
1876 .lc_opaque = opaque,
1884 if (res_id != NULL) {
1885 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
1889 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1890 ldlm_cli_hash_cancel_unused, &arg);
1895 /* Lock iterators. */
1897 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1900 cfs_list_t *tmp, *next;
1901 struct ldlm_lock *lock;
1902 int rc = LDLM_ITER_CONTINUE;
1907 RETURN(LDLM_ITER_CONTINUE);
1910 cfs_list_for_each_safe(tmp, next, &res->lr_granted) {
1911 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1913 if (iter(lock, closure) == LDLM_ITER_STOP)
1914 GOTO(out, rc = LDLM_ITER_STOP);
1917 cfs_list_for_each_safe(tmp, next, &res->lr_converting) {
1918 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1920 if (iter(lock, closure) == LDLM_ITER_STOP)
1921 GOTO(out, rc = LDLM_ITER_STOP);
1924 cfs_list_for_each_safe(tmp, next, &res->lr_waiting) {
1925 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1927 if (iter(lock, closure) == LDLM_ITER_STOP)
1928 GOTO(out, rc = LDLM_ITER_STOP);
1935 struct iter_helper_data {
1936 ldlm_iterator_t iter;
1940 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1942 struct iter_helper_data *helper = closure;
1943 return helper->iter(lock, helper->closure);
1946 static int ldlm_res_iter_helper(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1947 cfs_hlist_node_t *hnode, void *arg)
1950 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1952 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
1956 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
1957 ldlm_iterator_t iter, void *closure)
1960 struct iter_helper_data helper = { iter: iter, closure: closure };
1962 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1963 ldlm_res_iter_helper, &helper);
1967 /* non-blocking function to manipulate a lock whose cb_data is being put away.
1968 * return 0: find no resource
1969 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
1972 int ldlm_resource_iterate(struct ldlm_namespace *ns,
1973 const struct ldlm_res_id *res_id,
1974 ldlm_iterator_t iter, void *data)
1976 struct ldlm_resource *res;
1981 CERROR("must pass in namespace\n");
1985 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1989 LDLM_RESOURCE_ADDREF(res);
1990 rc = ldlm_resource_foreach(res, iter, data);
1991 LDLM_RESOURCE_DELREF(res);
1992 ldlm_resource_putref(res);
1998 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2000 cfs_list_t *list = closure;
2002 /* we use l_pending_chain here, because it's unused on clients. */
2003 LASSERTF(cfs_list_empty(&lock->l_pending_chain),
2004 "lock %p next %p prev %p\n",
2005 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2006 /* bug 9573: don't replay locks left after eviction, or
2007 * bug 17614: locks being actively cancelled. Get a reference
2008 * on a lock so that it does not disapear under us (e.g. due to cancel)
2010 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_CANCELING))) {
2011 cfs_list_add(&lock->l_pending_chain, list);
2012 LDLM_LOCK_GET(lock);
2015 return LDLM_ITER_CONTINUE;
2018 static int replay_lock_interpret(const struct lu_env *env,
2019 struct ptlrpc_request *req,
2020 struct ldlm_async_args *aa, int rc)
2022 struct ldlm_lock *lock;
2023 struct ldlm_reply *reply;
2024 struct obd_export *exp;
2027 cfs_atomic_dec(&req->rq_import->imp_replay_inflight);
2032 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2034 GOTO(out, rc = -EPROTO);
2036 lock = ldlm_handle2lock(&aa->lock_handle);
2038 CERROR("received replay ack for unknown local cookie "LPX64
2039 " remote cookie "LPX64 " from server %s id %s\n",
2040 aa->lock_handle.cookie, reply->lock_handle.cookie,
2041 req->rq_export->exp_client_uuid.uuid,
2042 libcfs_id2str(req->rq_peer));
2043 GOTO(out, rc = -ESTALE);
2046 /* Key change rehash lock in per-export hash with new key */
2047 exp = req->rq_export;
2048 if (exp && exp->exp_lock_hash) {
2049 cfs_hash_rehash_key(exp->exp_lock_hash,
2050 &lock->l_remote_handle,
2051 &reply->lock_handle,
2054 lock->l_remote_handle = reply->lock_handle;
2057 LDLM_DEBUG(lock, "replayed lock:");
2058 ptlrpc_import_recovery_state_machine(req->rq_import);
2059 LDLM_LOCK_PUT(lock);
2062 ptlrpc_connect_import(req->rq_import, NULL);
2067 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2069 struct ptlrpc_request *req;
2070 struct ldlm_async_args *aa;
2071 struct ldlm_request *body;
2076 /* Bug 11974: Do not replay a lock which is actively being canceled */
2077 if (lock->l_flags & LDLM_FL_CANCELING) {
2078 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2082 /* If this is reply-less callback lock, we cannot replay it, since
2083 * server might have long dropped it, but notification of that event was
2084 * lost by network. (and server granted conflicting lock already) */
2085 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
2086 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2087 ldlm_lock_cancel(lock);
2091 * If granted mode matches the requested mode, this lock is granted.
2093 * If they differ, but we have a granted mode, then we were granted
2094 * one mode and now want another: ergo, converting.
2096 * If we haven't been granted anything and are on a resource list,
2097 * then we're blocked/waiting.
2099 * If we haven't been granted anything and we're NOT on a resource list,
2100 * then we haven't got a reply yet and don't have a known disposition.
2101 * This happens whenever a lock enqueue is the request that triggers
2104 if (lock->l_granted_mode == lock->l_req_mode)
2105 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2106 else if (lock->l_granted_mode)
2107 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2108 else if (!cfs_list_empty(&lock->l_res_link))
2109 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2111 flags = LDLM_FL_REPLAY;
2113 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2114 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2118 /* We're part of recovery, so don't wait for it. */
2119 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2121 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2122 ldlm_lock2desc(lock, &body->lock_desc);
2123 body->lock_flags = flags;
2125 ldlm_lock2handle(lock, &body->lock_handle[0]);
2126 if (lock->l_lvb_len != 0) {
2127 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2128 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2131 ptlrpc_request_set_replen(req);
2132 /* notify the server we've replayed all requests.
2133 * also, we mark the request to be put on a dedicated
2134 * queue to be processed after all request replayes.
2136 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2138 LDLM_DEBUG(lock, "replaying lock:");
2140 cfs_atomic_inc(&req->rq_import->imp_replay_inflight);
2141 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2142 aa = ptlrpc_req_async_args(req);
2143 aa->lock_handle = body->lock_handle[0];
2144 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2145 ptlrpcd_add_req(req, PSCOPE_OTHER);
2151 * Cancel as many unused locks as possible before replay. since we are
2152 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2155 * Called only in recovery before replaying locks. there is no need to
2156 * replay locks that are unused. since the clients may hold thousands of
2157 * cached unused locks, dropping the unused locks can greatly reduce the
2158 * load on the servers at recovery time.
2160 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2163 CFS_LIST_HEAD(cancels);
2165 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2166 "replay for namespace %s (%d)\n",
2167 ldlm_ns_name(ns), ns->ns_nr_unused);
2169 /* We don't need to care whether or not LRU resize is enabled
2170 * because the LDLM_CANCEL_NO_WAIT policy doesn't use the
2171 * count parameter */
2172 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2173 LCF_LOCAL, LDLM_CANCEL_NO_WAIT);
2175 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2176 canceled, ldlm_ns_name(ns));
2179 int ldlm_replay_locks(struct obd_import *imp)
2181 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2182 CFS_LIST_HEAD(list);
2183 struct ldlm_lock *lock, *next;
2188 LASSERT(cfs_atomic_read(&imp->imp_replay_inflight) == 0);
2190 /* don't replay locks if import failed recovery */
2191 if (imp->imp_vbr_failed)
2194 /* ensure this doesn't fall to 0 before all have been queued */
2195 cfs_atomic_inc(&imp->imp_replay_inflight);
2197 if (ldlm_cancel_unused_locks_before_replay)
2198 ldlm_cancel_unused_locks_for_replay(ns);
2200 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2202 cfs_list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2203 cfs_list_del_init(&lock->l_pending_chain);
2205 LDLM_LOCK_RELEASE(lock);
2206 continue; /* or try to do the rest? */
2208 rc = replay_one_lock(imp, lock);
2209 LDLM_LOCK_RELEASE(lock);
2212 cfs_atomic_dec(&imp->imp_replay_inflight);