4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LDLM
40 #include <liblustre.h>
43 #include <lustre_dlm.h>
44 #include <obd_class.h>
47 #include "ldlm_internal.h"
49 int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
50 CFS_MODULE_PARM(ldlm_enqueue_min, "i", int, 0644,
51 "lock enqueue timeout minimum");
53 /* in client side, whether the cached locks will be canceled before replay */
54 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
56 static void interrupted_completion_wait(void *data)
60 struct lock_wait_data {
61 struct ldlm_lock *lwd_lock;
65 struct ldlm_async_args {
66 struct lustre_handle lock_handle;
69 int ldlm_expired_completion_wait(void *data)
71 struct lock_wait_data *lwd = data;
72 struct ldlm_lock *lock = lwd->lwd_lock;
73 struct obd_import *imp;
74 struct obd_device *obd;
77 if (lock->l_conn_export == NULL) {
78 static cfs_time_t next_dump = 0, last_dump = 0;
80 if (ptlrpc_check_suspend())
83 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
84 CFS_DURATION_T"s ago); not entering recovery in "
85 "server code, just going back to sleep",
86 lock->l_last_activity,
87 cfs_time_sub(cfs_time_current_sec(),
88 lock->l_last_activity));
89 if (cfs_time_after(cfs_time_current(), next_dump)) {
90 last_dump = next_dump;
91 next_dump = cfs_time_shift(300);
92 ldlm_namespace_dump(D_DLMTRACE,
93 ldlm_lock_to_ns(lock));
95 libcfs_debug_dumplog();
100 obd = lock->l_conn_export->exp_obd;
101 imp = obd->u.cli.cl_import;
102 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
103 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
104 CFS_DURATION_T"s ago), entering recovery for %s@%s",
105 lock->l_last_activity,
106 cfs_time_sub(cfs_time_current_sec(), lock->l_last_activity),
107 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
112 /* We use the same basis for both server side and client side functions
113 from a single node. */
114 int ldlm_get_enq_timeout(struct ldlm_lock *lock)
116 int timeout = at_get(ldlm_lock_to_ns_at(lock));
118 return obd_timeout / 2;
119 /* Since these are non-updating timeouts, we should be conservative.
120 It would be nice to have some kind of "early reply" mechanism for
121 lock callbacks too... */
122 timeout = min_t(int, at_max, timeout + (timeout >> 1)); /* 150% */
123 return max(timeout, ldlm_enqueue_min);
125 EXPORT_SYMBOL(ldlm_get_enq_timeout);
128 * Helper function for ldlm_completion_ast(), updating timings when lock is
131 static int ldlm_completion_tail(struct ldlm_lock *lock)
136 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
137 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
140 delay = cfs_time_sub(cfs_time_current_sec(),
141 lock->l_last_activity);
142 LDLM_DEBUG(lock, "client-side enqueue: granted after "
143 CFS_DURATION_T"s", delay);
145 /* Update our time estimate */
146 at_measured(ldlm_lock_to_ns_at(lock),
154 * Implementation of ->l_completion_ast() for a client, that doesn't wait
155 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
156 * other threads that cannot block for long.
158 int ldlm_completion_ast_async(struct ldlm_lock *lock, int flags, void *data)
162 if (flags == LDLM_FL_WAIT_NOREPROC) {
163 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
167 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
168 LDLM_FL_BLOCK_CONV))) {
169 cfs_waitq_signal(&lock->l_waitq);
170 RETURN(ldlm_completion_tail(lock));
173 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
175 ldlm_lock_dump(D_OTHER, lock, 0);
176 ldlm_reprocess_all(lock->l_resource);
181 * Client side LDLM "completion" AST. This is called in several cases:
183 * - when a reply to an ENQUEUE rpc is received from the server
184 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
185 * this point (determined by flags);
187 * - when LDLM_CP_CALLBACK rpc comes to client to notify it that lock has
190 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
193 * - to force all locks when resource is destroyed (cleanup_resource());
195 * - during lock conversion (not used currently).
197 * If lock is not granted in the first case, this function waits until second
198 * or penultimate cases happen in some other thread.
201 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
203 /* XXX ALLOCATE - 160 bytes */
204 struct lock_wait_data lwd;
205 struct obd_device *obd;
206 struct obd_import *imp = NULL;
207 struct l_wait_info lwi;
212 if (flags == LDLM_FL_WAIT_NOREPROC) {
213 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
217 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
218 LDLM_FL_BLOCK_CONV))) {
219 cfs_waitq_signal(&lock->l_waitq);
223 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
225 ldlm_lock_dump(D_OTHER, lock, 0);
229 obd = class_exp2obd(lock->l_conn_export);
231 /* if this is a local lock, then there is no import */
233 imp = obd->u.cli.cl_import;
236 /* Wait a long time for enqueue - server may have to callback a
237 lock from another client. Server will evict the other client if it
238 doesn't respond reasonably, and then give us the lock. */
239 timeout = ldlm_get_enq_timeout(lock) * 2;
243 if (lock->l_flags & LDLM_FL_NO_TIMEOUT) {
244 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
245 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
247 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
248 ldlm_expired_completion_wait,
249 interrupted_completion_wait, &lwd);
253 cfs_spin_lock(&imp->imp_lock);
254 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
255 cfs_spin_unlock(&imp->imp_lock);
258 if (ns_is_client(ldlm_lock_to_ns(lock)) &&
259 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
260 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
261 lock->l_flags |= LDLM_FL_FAIL_LOC;
264 /* Go to sleep until the lock is granted or cancelled. */
265 rc = l_wait_event(lock->l_waitq,
266 is_granted_or_cancelled(lock), &lwi);
270 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
275 RETURN(ldlm_completion_tail(lock));
279 * A helper to build a blocking ast function
281 * Perform a common operation for blocking asts:
282 * defferred lock cancellation.
284 * \param lock the lock blocking or canceling ast was called on
286 * \see mdt_blocking_ast
287 * \see ldlm_blocking_ast
289 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
294 lock->l_flags |= LDLM_FL_CBPENDING;
295 do_ast = (!lock->l_readers && !lock->l_writers);
296 unlock_res_and_lock(lock);
299 struct lustre_handle lockh;
302 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
303 ldlm_lock2handle(lock, &lockh);
304 rc = ldlm_cli_cancel(&lockh);
306 CERROR("ldlm_cli_cancel: %d\n", rc);
308 LDLM_DEBUG(lock, "Lock still has references, will be "
315 * Server blocking AST
317 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
320 * \param lock the lock which blocks a request or cancelling lock
323 * \param flag indicates whether this cancelling or blocking callback
325 * \see ldlm_blocking_ast_nocheck
327 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
328 void *data, int flag)
332 if (flag == LDLM_CB_CANCELING) {
333 /* Don't need to do anything here. */
337 lock_res_and_lock(lock);
338 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
339 * that ldlm_blocking_ast is called just before intent_policy method
340 * takes the lr_lock, then by the time we get the lock, we might not
341 * be the correct blocking function anymore. So check, and return
343 if (lock->l_blocking_ast != ldlm_blocking_ast) {
344 unlock_res_and_lock(lock);
347 RETURN(ldlm_blocking_ast_nocheck(lock));
351 * ->l_glimpse_ast() for DLM extent locks acquired on the server-side. See
352 * comment in filter_intent_policy() on why you may need this.
354 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
357 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for
358 * that is rather subtle: with OST-side locking, it may so happen that
359 * _all_ extent locks are held by the OST. If client wants to obtain
360 * current file size it calls ll{,u}_glimpse_size(), and (as locks are
361 * on the server), dummy glimpse callback fires and does
362 * nothing. Client still receives correct file size due to the
363 * following fragment in filter_intent_policy():
365 * rc = l->l_glimpse_ast(l, NULL); // this will update the LVB
366 * if (rc != 0 && res->lr_namespace->ns_lvbo &&
367 * res->lr_namespace->ns_lvbo->lvbo_update) {
368 * res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
371 * that is, after glimpse_ast() fails, filter_lvbo_update() runs, and
372 * returns correct file size to the client.
374 return -ELDLM_NO_LOCK_DATA;
377 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
378 const struct ldlm_res_id *res_id,
379 ldlm_type_t type, ldlm_policy_data_t *policy,
380 ldlm_mode_t mode, int *flags,
381 ldlm_blocking_callback blocking,
382 ldlm_completion_callback completion,
383 ldlm_glimpse_callback glimpse,
384 void *data, __u32 lvb_len,
385 const __u64 *client_cookie,
386 struct lustre_handle *lockh)
388 struct ldlm_lock *lock;
390 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
391 .lcs_blocking = blocking,
392 .lcs_glimpse = glimpse,
396 LASSERT(!(*flags & LDLM_FL_REPLAY));
397 if (unlikely(ns_is_client(ns))) {
398 CERROR("Trying to enqueue local lock in a shadow namespace\n");
402 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len);
404 GOTO(out_nolock, err = -ENOMEM);
406 ldlm_lock2handle(lock, lockh);
408 /* NB: we don't have any lock now (lock_res_and_lock)
409 * because it's a new lock */
410 ldlm_lock_addref_internal_nolock(lock, mode);
411 lock->l_flags |= LDLM_FL_LOCAL;
412 if (*flags & LDLM_FL_ATOMIC_CB)
413 lock->l_flags |= LDLM_FL_ATOMIC_CB;
416 lock->l_policy_data = *policy;
417 if (client_cookie != NULL)
418 lock->l_client_cookie = *client_cookie;
419 if (type == LDLM_EXTENT)
420 lock->l_req_extent = policy->l_extent;
422 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
423 if (unlikely(err != ELDLM_OK))
427 *policy = lock->l_policy_data;
429 if (lock->l_completion_ast)
430 lock->l_completion_ast(lock, *flags, NULL);
432 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
435 LDLM_LOCK_RELEASE(lock);
440 static void failed_lock_cleanup(struct ldlm_namespace *ns,
441 struct ldlm_lock *lock, int mode)
445 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
446 lock_res_and_lock(lock);
447 /* Check that lock is not granted or failed, we might race. */
448 if ((lock->l_req_mode != lock->l_granted_mode) &&
449 !(lock->l_flags & LDLM_FL_FAILED)) {
450 /* Make sure that this lock will not be found by raced
451 * bl_ast and -EINVAL reply is sent to server anyways.
453 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
454 LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
457 unlock_res_and_lock(lock);
461 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
462 "LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
464 LDLM_DEBUG(lock, "lock was granted or failed in race");
466 ldlm_lock_decref_internal(lock, mode);
468 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
469 * from llite/file.c/ll_file_flock(). */
470 /* This code makes for the fact that we do not have blocking handler on
471 * a client for flock locks. As such this is the place where we must
472 * completely kill failed locks. (interrupted and those that
473 * were waiting to be granted when server evicted us. */
474 if (lock->l_resource->lr_type == LDLM_FLOCK) {
475 lock_res_and_lock(lock);
476 ldlm_resource_unlink_lock(lock);
477 ldlm_lock_destroy_nolock(lock);
478 unlock_res_and_lock(lock);
482 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
483 ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
484 int *flags, void *lvb, __u32 lvb_len,
485 struct lustre_handle *lockh,int rc)
487 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
488 int is_replay = *flags & LDLM_FL_REPLAY;
489 struct ldlm_lock *lock;
490 struct ldlm_reply *reply;
491 struct ost_lvb *tmplvb;
492 int cleanup_phase = 1;
495 lock = ldlm_handle2lock(lockh);
496 /* ldlm_cli_enqueue is holding a reference on this lock. */
498 LASSERT(type == LDLM_FLOCK);
502 if (rc != ELDLM_OK) {
504 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
505 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
506 if (rc == ELDLM_LOCK_ABORTED) {
507 /* Before we return, swab the reply */
508 reply = req_capsule_server_get(&req->rq_pill,
514 req_capsule_set_size(&req->rq_pill,
515 &RMF_DLM_LVB, RCL_SERVER,
517 tmplvb = req_capsule_server_get(&req->rq_pill,
520 GOTO(cleanup, rc = -EPROTO);
522 memcpy(lvb, tmplvb, lvb_len);
528 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
530 GOTO(cleanup, rc = -EPROTO);
532 /* lock enqueued on the server */
535 lock_res_and_lock(lock);
536 /* Key change rehash lock in per-export hash with new key */
537 if (exp->exp_lock_hash) {
538 cfs_hash_rehash_key(exp->exp_lock_hash,
539 &lock->l_remote_handle,
543 lock->l_remote_handle = reply->lock_handle;
546 *flags = reply->lock_flags;
547 lock->l_flags |= reply->lock_flags & LDLM_INHERIT_FLAGS;
548 /* move NO_TIMEOUT flag to the lock to force ldlm_lock_match()
549 * to wait with no timeout as well */
550 lock->l_flags |= reply->lock_flags & LDLM_FL_NO_TIMEOUT;
551 unlock_res_and_lock(lock);
553 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
554 lock, reply->lock_handle.cookie, *flags);
556 /* If enqueue returned a blocked lock but the completion handler has
557 * already run, then it fixed up the resource and we don't need to do it
559 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
560 int newmode = reply->lock_desc.l_req_mode;
562 if (newmode && newmode != lock->l_req_mode) {
563 LDLM_DEBUG(lock, "server returned different mode %s",
564 ldlm_lockname[newmode]);
565 lock->l_req_mode = newmode;
568 if (memcmp(reply->lock_desc.l_resource.lr_name.name,
569 lock->l_resource->lr_name.name,
570 sizeof(struct ldlm_res_id))) {
571 CDEBUG(D_INFO, "remote intent success, locking "
572 "(%ld,%ld,%ld) instead of "
574 (long)reply->lock_desc.l_resource.lr_name.name[0],
575 (long)reply->lock_desc.l_resource.lr_name.name[1],
576 (long)reply->lock_desc.l_resource.lr_name.name[2],
577 (long)lock->l_resource->lr_name.name[0],
578 (long)lock->l_resource->lr_name.name[1],
579 (long)lock->l_resource->lr_name.name[2]);
581 rc = ldlm_lock_change_resource(ns, lock,
582 &reply->lock_desc.l_resource.lr_name);
583 if (rc || lock->l_resource == NULL)
584 GOTO(cleanup, rc = -ENOMEM);
585 LDLM_DEBUG(lock, "client-side enqueue, new resource");
588 if (!(type == LDLM_IBITS && !(exp->exp_connect_flags &
590 /* We assume lock type cannot change on server*/
591 ldlm_convert_policy_to_local(exp,
592 lock->l_resource->lr_type,
593 &reply->lock_desc.l_policy_data,
594 &lock->l_policy_data);
595 if (type != LDLM_PLAIN)
596 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
599 if ((*flags) & LDLM_FL_AST_SENT ||
600 /* Cancel extent locks as soon as possible on a liblustre client,
601 * because it cannot handle asynchronous ASTs robustly (see
603 (LIBLUSTRE_CLIENT && type == LDLM_EXTENT)) {
604 lock_res_and_lock(lock);
605 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
606 unlock_res_and_lock(lock);
607 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
610 /* If the lock has already been granted by a completion AST, don't
611 * clobber the LVB with an older one. */
613 /* We must lock or a racing completion might update lvb
614 without letting us know and we'll clobber the correct value.
615 Cannot unlock after the check either, a that still leaves
616 a tiny window for completion to get in */
617 lock_res_and_lock(lock);
618 if (lock->l_req_mode != lock->l_granted_mode) {
620 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
621 RCL_SERVER, lvb_len);
622 tmplvb = req_capsule_server_get(&req->rq_pill,
624 if (tmplvb == NULL) {
625 unlock_res_and_lock(lock);
626 GOTO(cleanup, rc = -EPROTO);
628 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
630 unlock_res_and_lock(lock);
634 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
635 if (lock->l_completion_ast != NULL) {
636 int err = lock->l_completion_ast(lock, *flags, NULL);
644 if (lvb_len && lvb != NULL) {
645 /* Copy the LVB here, and not earlier, because the completion
646 * AST (if any) can override what we got in the reply */
647 memcpy(lvb, lock->l_lvb_data, lvb_len);
650 LDLM_DEBUG(lock, "client-side enqueue END");
653 if (cleanup_phase == 1 && rc)
654 failed_lock_cleanup(ns, lock, mode);
655 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
657 LDLM_LOCK_RELEASE(lock);
661 /* PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
662 * a single page on the send/receive side. XXX: 512 should be changed
663 * to more adequate value. */
664 static inline int ldlm_req_handles_avail(int req_size, int off)
668 avail = min_t(int, LDLM_MAXREQSIZE, CFS_PAGE_SIZE - 512) - req_size;
669 if (likely(avail >= 0))
670 avail /= (int)sizeof(struct lustre_handle);
673 avail += LDLM_LOCKREQ_HANDLES - off;
678 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
679 enum req_location loc,
682 int size = req_capsule_msg_size(pill, loc);
683 return ldlm_req_handles_avail(size, off);
686 static inline int ldlm_format_handles_avail(struct obd_import *imp,
687 const struct req_format *fmt,
688 enum req_location loc, int off)
690 int size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
691 return ldlm_req_handles_avail(size, off);
694 /* Cancel lru locks and pack them into the enqueue request. Pack there the given
695 * @count locks in @cancels. */
696 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
697 int version, int opc, int canceloff,
698 cfs_list_t *cancels, int count)
700 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
701 struct req_capsule *pill = &req->rq_pill;
702 struct ldlm_request *dlm = NULL;
703 int flags, avail, to_free, pack = 0;
710 if (exp_connect_cancelset(exp)) {
711 /* Estimate the amount of available space in the request. */
712 req_capsule_filled_sizes(pill, RCL_CLIENT);
713 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
715 flags = ns_connect_lru_resize(ns) ?
716 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
717 to_free = !ns_connect_lru_resize(ns) &&
718 opc == LDLM_ENQUEUE ? 1 : 0;
720 /* Cancel lru locks here _only_ if the server supports
721 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
722 * rpc, what will make us slower. */
724 count += ldlm_cancel_lru_local(ns, cancels, to_free,
725 avail - count, 0, flags);
730 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
731 ldlm_request_bufsize(pack, opc));
734 rc = ptlrpc_request_pack(req, version, opc);
736 ldlm_lock_list_put(cancels, l_bl_ast, count);
740 if (exp_connect_cancelset(exp)) {
742 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
744 /* Skip first lock handler in ldlm_request_pack(),
745 * this method will incrment @lock_count according
746 * to the lock handle amount actually written to
748 dlm->lock_count = canceloff;
750 /* Pack into the request @pack lock handles. */
751 ldlm_cli_cancel_list(cancels, pack, req, 0);
752 /* Prepare and send separate cancel rpc for others. */
753 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
755 ldlm_lock_list_put(cancels, l_bl_ast, count);
760 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
761 cfs_list_t *cancels, int count)
763 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
764 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
767 /* If a request has some specific initialisation it is passed in @reqp,
768 * otherwise it is created in ldlm_cli_enqueue.
770 * Supports sync and async requests, pass @async flag accordingly. If a
771 * request was created in ldlm_cli_enqueue and it is the async request,
772 * pass it to the caller in @reqp. */
773 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
774 struct ldlm_enqueue_info *einfo,
775 const struct ldlm_res_id *res_id,
776 ldlm_policy_data_t const *policy, int *flags,
777 void *lvb, __u32 lvb_len, struct lustre_handle *lockh,
780 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
781 struct ldlm_lock *lock;
782 struct ldlm_request *body;
783 int is_replay = *flags & LDLM_FL_REPLAY;
784 int req_passed_in = 1;
786 struct ptlrpc_request *req;
789 LASSERT(exp != NULL);
791 /* If we're replaying this lock, just check some invariants.
792 * If we're creating a new lock, get everything all setup nice. */
794 lock = ldlm_handle2lock_long(lockh, 0);
795 LASSERT(lock != NULL);
796 LDLM_DEBUG(lock, "client-side enqueue START");
797 LASSERT(exp == lock->l_conn_export);
799 const struct ldlm_callback_suite cbs = {
800 .lcs_completion = einfo->ei_cb_cp,
801 .lcs_blocking = einfo->ei_cb_bl,
802 .lcs_glimpse = einfo->ei_cb_gl,
803 .lcs_weigh = einfo->ei_cb_wg
805 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
806 einfo->ei_mode, &cbs, einfo->ei_cbdata,
810 /* for the local lock, add the reference */
811 ldlm_lock_addref_internal(lock, einfo->ei_mode);
812 ldlm_lock2handle(lock, lockh);
813 if (policy != NULL) {
814 /* INODEBITS_INTEROP: If the server does not support
815 * inodebits, we will request a plain lock in the
816 * descriptor (ldlm_lock2desc() below) but use an
817 * inodebits lock internally with both bits set.
819 if (einfo->ei_type == LDLM_IBITS &&
820 !(exp->exp_connect_flags & OBD_CONNECT_IBITS))
821 lock->l_policy_data.l_inodebits.bits =
822 MDS_INODELOCK_LOOKUP |
823 MDS_INODELOCK_UPDATE;
825 lock->l_policy_data = *policy;
828 if (einfo->ei_type == LDLM_EXTENT)
829 lock->l_req_extent = policy->l_extent;
830 LDLM_DEBUG(lock, "client-side enqueue START");
833 /* lock not sent to server yet */
835 if (reqp == NULL || *reqp == NULL) {
836 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
841 failed_lock_cleanup(ns, lock, einfo->ei_mode);
842 LDLM_LOCK_RELEASE(lock);
852 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
854 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
855 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
858 lock->l_conn_export = exp;
859 lock->l_export = NULL;
860 lock->l_blocking_ast = einfo->ei_cb_bl;
861 lock->l_flags |= (*flags & LDLM_FL_NO_LRU);
863 /* Dump lock data into the request buffer */
864 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
865 ldlm_lock2desc(lock, &body->lock_desc);
866 body->lock_flags = *flags;
867 body->lock_handle[0] = *lockh;
869 /* Continue as normal. */
870 if (!req_passed_in) {
872 req_capsule_extend(&req->rq_pill,
873 &RQF_LDLM_ENQUEUE_LVB);
874 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
875 RCL_SERVER, lvb_len);
877 ptlrpc_request_set_replen(req);
881 * Liblustre client doesn't get extent locks, except for O_APPEND case
882 * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
883 * [i_size, OBD_OBJECT_EOF] lock is taken.
885 LASSERT(ergo(LIBLUSTRE_CLIENT, einfo->ei_type != LDLM_EXTENT ||
886 policy->l_extent.end == OBD_OBJECT_EOF));
889 LASSERT(reqp != NULL);
893 LDLM_DEBUG(lock, "sending request");
895 rc = ptlrpc_queue_wait(req);
897 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
898 einfo->ei_mode, flags, lvb, lvb_len,
901 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
902 * one reference that we took */
904 LDLM_LOCK_RELEASE(lock);
908 if (!req_passed_in && req != NULL) {
909 ptlrpc_req_finished(req);
917 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
920 struct ldlm_resource *res;
923 if (ns_is_client(ldlm_lock_to_ns(lock))) {
924 CERROR("Trying to cancel local lock\n");
927 LDLM_DEBUG(lock, "client-side local convert");
929 res = ldlm_lock_convert(lock, new_mode, flags);
931 ldlm_reprocess_all(res);
936 LDLM_DEBUG(lock, "client-side local convert handler END");
941 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
942 * conversion of locks which are on the waiting or converting queue */
943 /* Caller of this code is supposed to take care of lock readers/writers
945 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, __u32 *flags)
947 struct ldlm_request *body;
948 struct ldlm_reply *reply;
949 struct ldlm_lock *lock;
950 struct ldlm_resource *res;
951 struct ptlrpc_request *req;
955 lock = ldlm_handle2lock(lockh);
962 if (lock->l_conn_export == NULL)
963 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
965 LDLM_DEBUG(lock, "client-side convert");
967 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
968 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
975 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
976 body->lock_handle[0] = lock->l_remote_handle;
978 body->lock_desc.l_req_mode = new_mode;
979 body->lock_flags = *flags;
982 ptlrpc_request_set_replen(req);
983 rc = ptlrpc_queue_wait(req);
987 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
989 GOTO(out, rc = -EPROTO);
992 GOTO(out, rc = req->rq_status);
994 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
996 ldlm_reprocess_all(res);
997 /* Go to sleep until the lock is granted. */
998 /* FIXME: or cancelled. */
999 if (lock->l_completion_ast) {
1000 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1010 LDLM_LOCK_PUT(lock);
1011 ptlrpc_req_finished(req);
1015 /* Cancel locks locally.
1017 * LDLM_FL_LOCAL_ONLY if tere is no need in a CANCEL rpc to the server;
1018 * LDLM_FL_CANCELING otherwise;
1019 * LDLM_FL_BL_AST if there is a need in a separate CANCEL rpc. */
1020 static int ldlm_cli_cancel_local(struct ldlm_lock *lock)
1022 int rc = LDLM_FL_LOCAL_ONLY;
1025 if (lock->l_conn_export) {
1028 LDLM_DEBUG(lock, "client-side cancel");
1029 /* Set this flag to prevent others from getting new references*/
1030 lock_res_and_lock(lock);
1031 lock->l_flags |= LDLM_FL_CBPENDING;
1032 local_only = (lock->l_flags &
1033 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1034 ldlm_cancel_callback(lock);
1035 rc = (lock->l_flags & LDLM_FL_BL_AST) ?
1036 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1037 unlock_res_and_lock(lock);
1040 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1042 rc = LDLM_FL_LOCAL_ONLY;
1044 ldlm_lock_cancel(lock);
1046 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1047 LDLM_ERROR(lock, "Trying to cancel local lock");
1050 LDLM_DEBUG(lock, "server-side local cancel");
1051 ldlm_lock_cancel(lock);
1052 ldlm_reprocess_all(lock->l_resource);
1058 /* Pack @count locks in @head into ldlm_request buffer at the offset @off,
1059 of the request @req. */
1060 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1061 cfs_list_t *head, int count)
1063 struct ldlm_request *dlm;
1064 struct ldlm_lock *lock;
1065 int max, packed = 0;
1068 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1069 LASSERT(dlm != NULL);
1071 /* Check the room in the request buffer. */
1072 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1073 sizeof(struct ldlm_request);
1074 max /= sizeof(struct lustre_handle);
1075 max += LDLM_LOCKREQ_HANDLES;
1076 LASSERT(max >= dlm->lock_count + count);
1078 /* XXX: it would be better to pack lock handles grouped by resource.
1079 * so that the server cancel would call filter_lvbo_update() less
1081 cfs_list_for_each_entry(lock, head, l_bl_ast) {
1084 LASSERT(lock->l_conn_export);
1085 /* Pack the lock handle to the given request buffer. */
1086 LDLM_DEBUG(lock, "packing");
1087 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1090 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1094 /* Prepare and send a batched cancel rpc, it will include count lock handles
1095 * of locks given in @head. */
1096 int ldlm_cli_cancel_req(struct obd_export *exp, cfs_list_t *cancels,
1097 int count, ldlm_cancel_flags_t flags)
1099 struct ptlrpc_request *req = NULL;
1100 struct obd_import *imp;
1105 LASSERT(exp != NULL);
1108 CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1110 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1113 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1114 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1119 imp = class_exp2cliimp(exp);
1120 if (imp == NULL || imp->imp_invalid) {
1122 "skipping cancel on invalid import %p\n", imp);
1126 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1128 GOTO(out, rc = -ENOMEM);
1130 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1131 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1132 ldlm_request_bufsize(count, LDLM_CANCEL));
1134 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1136 ptlrpc_request_free(req);
1139 req->rq_no_resend = 1;
1140 req->rq_no_delay = 1;
1142 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1143 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1144 ptlrpc_at_set_req_timeout(req);
1146 ldlm_cancel_pack(req, cancels, count);
1148 ptlrpc_request_set_replen(req);
1149 if (flags & LCF_ASYNC) {
1150 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
1154 rc = ptlrpc_queue_wait(req);
1157 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1158 "out of sync -- not fatal\n",
1159 libcfs_nid2str(req->rq_import->
1160 imp_connection->c_peer.nid));
1162 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1163 req->rq_import_generation == imp->imp_generation) {
1164 ptlrpc_req_finished(req);
1166 } else if (rc != ELDLM_OK) {
1167 CERROR("Got rc %d from cancel RPC: canceling "
1175 ptlrpc_req_finished(req);
1178 return sent ? sent : rc;
1181 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1183 LASSERT(imp != NULL);
1184 return &imp->imp_obd->obd_namespace->ns_pool;
1188 * Update client's obd pool related fields with new SLV and Limit from \a req.
1190 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1192 struct obd_device *obd;
1196 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1197 !imp_connect_lru_resize(req->rq_import)))
1200 * Do nothing for corner cases.
1206 * In some cases RPC may contain slv and limit zeroed out. This is
1207 * the case when server does not support lru resize feature. This is
1208 * also possible in some recovery cases when server side reqs have no
1209 * ref to obd export and thus access to server side namespace is no
1212 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1213 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1214 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1215 "(SLV: "LPU64", Limit: %u)",
1216 lustre_msg_get_slv(req->rq_repmsg),
1217 lustre_msg_get_limit(req->rq_repmsg));
1221 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1222 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1223 obd = req->rq_import->imp_obd;
1226 * Set new SLV and Limit to obd fields to make accessible for pool
1227 * thread. We do not access obd_namespace and pool directly here
1228 * as there is no reliable way to make sure that they are still
1229 * alive in cleanup time. Evil races are possible which may cause
1230 * oops in that time.
1232 cfs_write_lock(&obd->obd_pool_lock);
1233 obd->obd_pool_slv = new_slv;
1234 obd->obd_pool_limit = new_limit;
1235 cfs_write_unlock(&obd->obd_pool_lock);
1239 EXPORT_SYMBOL(ldlm_cli_update_pool);
1241 int ldlm_cli_cancel(struct lustre_handle *lockh)
1243 struct obd_export *exp;
1244 int avail, flags, count = 1, rc = 0;
1245 struct ldlm_namespace *ns;
1246 struct ldlm_lock *lock;
1247 CFS_LIST_HEAD(cancels);
1250 /* concurrent cancels on the same handle can happen */
1251 lock = ldlm_handle2lock_long(lockh, LDLM_FL_CANCELING);
1253 LDLM_DEBUG_NOLOCK("lock is already being destroyed\n");
1257 rc = ldlm_cli_cancel_local(lock);
1258 if (rc < 0 || rc == LDLM_FL_LOCAL_ONLY) {
1259 LDLM_LOCK_RELEASE(lock);
1260 RETURN(rc < 0 ? rc : 0);
1262 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1263 * rpc which goes to canceld portal, so we can cancel other lru locks
1264 * here and send them all as one LDLM_CANCEL rpc. */
1265 LASSERT(cfs_list_empty(&lock->l_bl_ast));
1266 cfs_list_add(&lock->l_bl_ast, &cancels);
1268 exp = lock->l_conn_export;
1269 if (exp_connect_cancelset(exp)) {
1270 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1275 ns = ldlm_lock_to_ns(lock);
1276 flags = ns_connect_lru_resize(ns) ?
1277 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
1278 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1281 ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1285 /* XXX until we will have compound requests and can cut cancels from generic rpc
1286 * we need send cancels with LDLM_FL_BL_AST flag as separate rpc */
1287 int ldlm_cli_cancel_list_local(cfs_list_t *cancels, int count,
1288 ldlm_cancel_flags_t flags)
1290 CFS_LIST_HEAD(head);
1291 struct ldlm_lock *lock, *next;
1292 int left = 0, bl_ast = 0, rc;
1295 cfs_list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1299 if (flags & LCF_LOCAL) {
1300 rc = LDLM_FL_LOCAL_ONLY;
1301 ldlm_lock_cancel(lock);
1303 rc = ldlm_cli_cancel_local(lock);
1305 if (!(flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1306 LDLM_DEBUG(lock, "Cancel lock separately");
1307 cfs_list_del_init(&lock->l_bl_ast);
1308 cfs_list_add(&lock->l_bl_ast, &head);
1312 if (rc == LDLM_FL_LOCAL_ONLY) {
1313 /* CANCEL RPC should not be sent to server. */
1314 cfs_list_del_init(&lock->l_bl_ast);
1315 LDLM_LOCK_RELEASE(lock);
1322 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1329 * Cancel as many locks as possible w/o sending any rpcs (e.g. to write back
1330 * dirty data, to close a file, ...) or waiting for any rpcs in-flight (e.g.
1331 * readahead requests, ...)
1333 static ldlm_policy_res_t ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns,
1334 struct ldlm_lock *lock,
1335 int unused, int added,
1338 ldlm_policy_res_t result = LDLM_POLICY_CANCEL_LOCK;
1339 ldlm_cancel_for_recovery cb = ns->ns_cancel_for_recovery;
1340 lock_res_and_lock(lock);
1342 /* don't check added & count since we want to process all locks
1343 * from unused list */
1344 switch (lock->l_resource->lr_type) {
1350 result = LDLM_POLICY_SKIP_LOCK;
1351 lock->l_flags |= LDLM_FL_SKIPPED;
1355 unlock_res_and_lock(lock);
1360 * Callback function for lru-resize policy. Makes decision whether to keep
1361 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1362 * \a added and number of locks to be preferably canceled \a count.
1364 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1366 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1368 static ldlm_policy_res_t ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1369 struct ldlm_lock *lock,
1370 int unused, int added,
1373 cfs_time_t cur = cfs_time_current();
1374 struct ldlm_pool *pl = &ns->ns_pool;
1379 * Stop lru processing when we reached passed @count or checked all
1382 if (count && added >= count)
1383 return LDLM_POLICY_KEEP_LOCK;
1385 slv = ldlm_pool_get_slv(pl);
1386 lvf = ldlm_pool_get_lvf(pl);
1387 la = cfs_duration_sec(cfs_time_sub(cur,
1388 lock->l_last_used));
1391 * Stop when slv is not yet come from server or lv is smaller than
1394 lv = lvf * la * unused;
1397 * Inform pool about current CLV to see it via proc.
1399 ldlm_pool_set_clv(pl, lv);
1400 return (slv == 0 || lv < slv) ?
1401 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1405 * Callback function for proc used policy. Makes decision whether to keep
1406 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1407 * \a added and number of locks to be preferably canceled \a count.
1409 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1411 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1413 static ldlm_policy_res_t ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1414 struct ldlm_lock *lock,
1415 int unused, int added,
1419 * Stop lru processing when we reached passed @count or checked all
1422 return (added >= count) ?
1423 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1427 * Callback function for aged policy. Makes decision whether to keep
1428 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1429 * \a added and number of locks to be preferably canceled \a count.
1431 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1433 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1435 static ldlm_policy_res_t ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1436 struct ldlm_lock *lock,
1437 int unused, int added,
1441 * Stop lru processing if young lock is found and we reached passed
1444 return ((added >= count) &&
1445 cfs_time_before(cfs_time_current(),
1446 cfs_time_add(lock->l_last_used,
1448 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1452 * Callback function for default policy. Makes decision whether to keep
1453 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1454 * \a added and number of locks to be preferably canceled \a count.
1456 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1458 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1460 static ldlm_policy_res_t ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1461 struct ldlm_lock *lock,
1462 int unused, int added,
1466 * Stop lru processing when we reached passed @count or checked all
1469 return (added >= count) ?
1470 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1473 typedef ldlm_policy_res_t (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *,
1474 struct ldlm_lock *, int,
1477 static ldlm_cancel_lru_policy_t
1478 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, int flags)
1480 if (flags & LDLM_CANCEL_NO_WAIT)
1481 return ldlm_cancel_no_wait_policy;
1483 if (ns_connect_lru_resize(ns)) {
1484 if (flags & LDLM_CANCEL_SHRINK)
1485 /* We kill passed number of old locks. */
1486 return ldlm_cancel_passed_policy;
1487 else if (flags & LDLM_CANCEL_LRUR)
1488 return ldlm_cancel_lrur_policy;
1489 else if (flags & LDLM_CANCEL_PASSED)
1490 return ldlm_cancel_passed_policy;
1492 if (flags & LDLM_CANCEL_AGED)
1493 return ldlm_cancel_aged_policy;
1496 return ldlm_cancel_default_policy;
1499 /* - Free space in lru for @count new locks,
1500 * redundant unused locks are canceled locally;
1501 * - also cancel locally unused aged locks;
1502 * - do not cancel more than @max locks;
1503 * - GET the found locks and add them into the @cancels list.
1505 * A client lock can be added to the l_bl_ast list only when it is
1506 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing CANCEL.
1507 * There are the following use cases: ldlm_cancel_resource_local(),
1508 * ldlm_cancel_lru_local() and ldlm_cli_cancel(), which check&set this
1509 * flag properly. As any attempt to cancel a lock rely on this flag,
1510 * l_bl_ast list is accessed later without any special locking.
1512 * Calling policies for enabled lru resize:
1513 * ----------------------------------------
1514 * flags & LDLM_CANCEL_LRUR - use lru resize policy (SLV from server) to
1515 * cancel not more than @count locks;
1517 * flags & LDLM_CANCEL_PASSED - cancel @count number of old locks (located at
1518 * the beginning of lru list);
1520 * flags & LDLM_CANCEL_SHRINK - cancel not more than @count locks according to
1521 * memory pressre policy function;
1523 * flags & LDLM_CANCEL_AGED - cancel alocks according to "aged policy".
1525 * flags & LDLM_CANCEL_NO_WAIT - cancel as many unused locks as possible
1526 * (typically before replaying locks) w/o
1527 * sending any rpcs or waiting for any
1528 * outstanding rpc to complete.
1530 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns, cfs_list_t *cancels,
1531 int count, int max, int flags)
1533 ldlm_cancel_lru_policy_t pf;
1534 struct ldlm_lock *lock, *next;
1535 int added = 0, unused, remained;
1538 cfs_spin_lock(&ns->ns_lock);
1539 unused = ns->ns_nr_unused;
1542 if (!ns_connect_lru_resize(ns))
1543 count += unused - ns->ns_max_unused;
1545 pf = ldlm_cancel_lru_policy(ns, flags);
1546 LASSERT(pf != NULL);
1548 while (!cfs_list_empty(&ns->ns_unused_list)) {
1549 ldlm_policy_res_t result;
1551 /* all unused locks */
1552 if (remained-- <= 0)
1555 /* For any flags, stop scanning if @max is reached. */
1556 if (max && added >= max)
1559 cfs_list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1561 /* No locks which got blocking requests. */
1562 LASSERT(!(lock->l_flags & LDLM_FL_BL_AST));
1564 if (flags & LDLM_CANCEL_NO_WAIT &&
1565 lock->l_flags & LDLM_FL_SKIPPED)
1566 /* already processed */
1569 /* Somebody is already doing CANCEL. No need in this
1570 * lock in lru, do not traverse it again. */
1571 if (!(lock->l_flags & LDLM_FL_CANCELING))
1574 ldlm_lock_remove_from_lru_nolock(lock);
1576 if (&lock->l_lru == &ns->ns_unused_list)
1579 LDLM_LOCK_GET(lock);
1580 cfs_spin_unlock(&ns->ns_lock);
1581 lu_ref_add(&lock->l_reference, __FUNCTION__, cfs_current());
1583 /* Pass the lock through the policy filter and see if it
1584 * should stay in lru.
1586 * Even for shrinker policy we stop scanning if
1587 * we find a lock that should stay in the cache.
1588 * We should take into account lock age anyway
1589 * as new lock even if it is small of weight is
1590 * valuable resource.
1592 * That is, for shrinker policy we drop only
1593 * old locks, but additionally chose them by
1594 * their weight. Big extent locks will stay in
1596 result = pf(ns, lock, unused, added, count);
1597 if (result == LDLM_POLICY_KEEP_LOCK) {
1598 lu_ref_del(&lock->l_reference,
1599 __FUNCTION__, cfs_current());
1600 LDLM_LOCK_RELEASE(lock);
1601 cfs_spin_lock(&ns->ns_lock);
1604 if (result == LDLM_POLICY_SKIP_LOCK) {
1605 lu_ref_del(&lock->l_reference,
1606 __FUNCTION__, cfs_current());
1607 LDLM_LOCK_RELEASE(lock);
1608 cfs_spin_lock(&ns->ns_lock);
1612 lock_res_and_lock(lock);
1613 /* Check flags again under the lock. */
1614 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1615 (ldlm_lock_remove_from_lru(lock) == 0)) {
1616 /* other thread is removing lock from lru or
1617 * somebody is already doing CANCEL or
1618 * there is a blocking request which will send
1619 * cancel by itseft or the lock is matched
1620 * is already not unused. */
1621 unlock_res_and_lock(lock);
1622 lu_ref_del(&lock->l_reference,
1623 __FUNCTION__, cfs_current());
1624 LDLM_LOCK_RELEASE(lock);
1625 cfs_spin_lock(&ns->ns_lock);
1628 LASSERT(!lock->l_readers && !lock->l_writers);
1630 /* If we have chosen to cancel this lock voluntarily, we
1631 * better send cancel notification to server, so that it
1632 * frees appropriate state. This might lead to a race
1633 * where while we are doing cancel here, server is also
1634 * silently cancelling this lock. */
1635 lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
1637 /* Setting the CBPENDING flag is a little misleading,
1638 * but prevents an important race; namely, once
1639 * CBPENDING is set, the lock can accumulate no more
1640 * readers/writers. Since readers and writers are
1641 * already zero here, ldlm_lock_decref() won't see
1642 * this flag and call l_blocking_ast */
1643 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1645 /* We can't re-add to l_lru as it confuses the
1646 * refcounting in ldlm_lock_remove_from_lru() if an AST
1647 * arrives after we drop lr_lock below. We use l_bl_ast
1648 * and can't use l_pending_chain as it is used both on
1649 * server and client nevertheless bug 5666 says it is
1650 * used only on server */
1651 LASSERT(cfs_list_empty(&lock->l_bl_ast));
1652 cfs_list_add(&lock->l_bl_ast, cancels);
1653 unlock_res_and_lock(lock);
1654 lu_ref_del(&lock->l_reference, __FUNCTION__, cfs_current());
1655 cfs_spin_lock(&ns->ns_lock);
1659 cfs_spin_unlock(&ns->ns_lock);
1663 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, cfs_list_t *cancels,
1664 int count, int max, ldlm_cancel_flags_t cancel_flags,
1668 added = ldlm_prepare_lru_list(ns, cancels, count, max, flags);
1671 return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1674 /* when called with LDLM_ASYNC the blocking callback will be handled
1675 * in a thread and this function will return after the thread has been
1676 * asked to call the callback. when called with LDLM_SYNC the blocking
1677 * callback will be performed in this function. */
1678 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr, ldlm_sync_t mode,
1681 CFS_LIST_HEAD(cancels);
1686 mode = LDLM_SYNC; /* force to be sync in user space */
1688 /* Just prepare the list of locks, do not actually cancel them yet.
1689 * Locks are cancelled later in a separate thread. */
1690 count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, flags);
1691 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, mode);
1698 /* Find and cancel locally unused locks found on resource, matched to the
1699 * given policy, mode. GET the found locks and add them into the @cancels
1701 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1702 cfs_list_t *cancels,
1703 ldlm_policy_data_t *policy,
1704 ldlm_mode_t mode, int lock_flags,
1705 ldlm_cancel_flags_t cancel_flags, void *opaque)
1707 struct ldlm_lock *lock;
1712 cfs_list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1713 if (opaque != NULL && lock->l_ast_data != opaque) {
1714 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1715 lock->l_ast_data, opaque);
1720 if (lock->l_readers || lock->l_writers)
1723 /* If somebody is already doing CANCEL, or blocking ast came,
1724 * skip this lock. */
1725 if (lock->l_flags & LDLM_FL_BL_AST ||
1726 lock->l_flags & LDLM_FL_CANCELING)
1729 if (lockmode_compat(lock->l_granted_mode, mode))
1732 /* If policy is given and this is IBITS lock, add to list only
1733 * those locks that match by policy. */
1734 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1735 !(lock->l_policy_data.l_inodebits.bits &
1736 policy->l_inodebits.bits))
1739 /* See CBPENDING comment in ldlm_cancel_lru */
1740 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1743 LASSERT(cfs_list_empty(&lock->l_bl_ast));
1744 cfs_list_add(&lock->l_bl_ast, cancels);
1745 LDLM_LOCK_GET(lock);
1750 RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1753 /* If @req is NULL, send CANCEL request to server with handles of locks
1754 * in the @cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1755 * separately per lock.
1756 * If @req is not NULL, put handles of locks in @cancels into the request
1757 * buffer at the offset @off.
1758 * Destroy @cancels at the end. */
1759 int ldlm_cli_cancel_list(cfs_list_t *cancels, int count,
1760 struct ptlrpc_request *req, ldlm_cancel_flags_t flags)
1762 struct ldlm_lock *lock;
1766 if (cfs_list_empty(cancels) || count == 0)
1769 /* XXX: requests (both batched and not) could be sent in parallel.
1770 * Usually it is enough to have just 1 RPC, but it is possible that
1771 * there are to many locks to be cancelled in LRU or on a resource.
1772 * It would also speed up the case when the server does not support
1775 LASSERT(!cfs_list_empty(cancels));
1776 lock = cfs_list_entry(cancels->next, struct ldlm_lock,
1778 LASSERT(lock->l_conn_export);
1780 if (exp_connect_cancelset(lock->l_conn_export)) {
1783 ldlm_cancel_pack(req, cancels, count);
1785 res = ldlm_cli_cancel_req(lock->l_conn_export,
1789 res = ldlm_cli_cancel_req(lock->l_conn_export,
1794 CERROR("ldlm_cli_cancel_list: %d\n", res);
1799 ldlm_lock_list_put(cancels, l_bl_ast, res);
1801 LASSERT(count == 0);
1805 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1806 const struct ldlm_res_id *res_id,
1807 ldlm_policy_data_t *policy,
1809 ldlm_cancel_flags_t flags,
1812 struct ldlm_resource *res;
1813 CFS_LIST_HEAD(cancels);
1818 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1820 /* This is not a problem. */
1821 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1825 LDLM_RESOURCE_ADDREF(res);
1826 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1827 0, flags | LCF_BL_AST, opaque);
1828 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1830 CERROR("ldlm_cli_cancel_unused_resource: %d\n", rc);
1832 LDLM_RESOURCE_DELREF(res);
1833 ldlm_resource_putref(res);
1837 struct ldlm_cli_cancel_arg {
1842 static int ldlm_cli_hash_cancel_unused(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1843 cfs_hlist_node_t *hnode, void *arg)
1845 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1846 struct ldlm_cli_cancel_arg *lc = arg;
1849 rc = ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
1851 lc->lc_flags, lc->lc_opaque);
1853 CERROR("ldlm_cli_cancel_unused ("LPU64"): %d\n",
1854 res->lr_name.name[0], rc);
1856 /* must return 0 for hash iteration */
1860 /* Cancel all locks on a namespace (or a specific resource, if given)
1861 * that have 0 readers/writers.
1863 * If flags & LCF_LOCAL, throw the locks away without trying
1864 * to notify the server. */
1865 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
1866 const struct ldlm_res_id *res_id,
1867 ldlm_cancel_flags_t flags, void *opaque)
1869 struct ldlm_cli_cancel_arg arg = {
1871 .lc_opaque = opaque,
1879 if (res_id != NULL) {
1880 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
1884 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1885 ldlm_cli_hash_cancel_unused, &arg);
1890 /* Lock iterators. */
1892 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1895 cfs_list_t *tmp, *next;
1896 struct ldlm_lock *lock;
1897 int rc = LDLM_ITER_CONTINUE;
1902 RETURN(LDLM_ITER_CONTINUE);
1905 cfs_list_for_each_safe(tmp, next, &res->lr_granted) {
1906 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1908 if (iter(lock, closure) == LDLM_ITER_STOP)
1909 GOTO(out, rc = LDLM_ITER_STOP);
1912 cfs_list_for_each_safe(tmp, next, &res->lr_converting) {
1913 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1915 if (iter(lock, closure) == LDLM_ITER_STOP)
1916 GOTO(out, rc = LDLM_ITER_STOP);
1919 cfs_list_for_each_safe(tmp, next, &res->lr_waiting) {
1920 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
1922 if (iter(lock, closure) == LDLM_ITER_STOP)
1923 GOTO(out, rc = LDLM_ITER_STOP);
1930 struct iter_helper_data {
1931 ldlm_iterator_t iter;
1935 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1937 struct iter_helper_data *helper = closure;
1938 return helper->iter(lock, helper->closure);
1941 static int ldlm_res_iter_helper(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1942 cfs_hlist_node_t *hnode, void *arg)
1945 struct ldlm_resource *res = cfs_hash_object(hs, hnode);
1947 return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
1951 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
1952 ldlm_iterator_t iter, void *closure)
1955 struct iter_helper_data helper = { iter: iter, closure: closure };
1957 cfs_hash_for_each_nolock(ns->ns_rs_hash,
1958 ldlm_res_iter_helper, &helper);
1962 /* non-blocking function to manipulate a lock whose cb_data is being put away.
1963 * return 0: find no resource
1964 * > 0: must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
1967 int ldlm_resource_iterate(struct ldlm_namespace *ns,
1968 const struct ldlm_res_id *res_id,
1969 ldlm_iterator_t iter, void *data)
1971 struct ldlm_resource *res;
1976 CERROR("must pass in namespace\n");
1980 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1984 LDLM_RESOURCE_ADDREF(res);
1985 rc = ldlm_resource_foreach(res, iter, data);
1986 LDLM_RESOURCE_DELREF(res);
1987 ldlm_resource_putref(res);
1993 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
1995 cfs_list_t *list = closure;
1997 /* we use l_pending_chain here, because it's unused on clients. */
1998 LASSERTF(cfs_list_empty(&lock->l_pending_chain),
1999 "lock %p next %p prev %p\n",
2000 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2001 /* bug 9573: don't replay locks left after eviction, or
2002 * bug 17614: locks being actively cancelled. Get a reference
2003 * on a lock so that it does not disapear under us (e.g. due to cancel)
2005 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_CANCELING))) {
2006 cfs_list_add(&lock->l_pending_chain, list);
2007 LDLM_LOCK_GET(lock);
2010 return LDLM_ITER_CONTINUE;
2013 static int replay_lock_interpret(const struct lu_env *env,
2014 struct ptlrpc_request *req,
2015 struct ldlm_async_args *aa, int rc)
2017 struct ldlm_lock *lock;
2018 struct ldlm_reply *reply;
2019 struct obd_export *exp;
2022 cfs_atomic_dec(&req->rq_import->imp_replay_inflight);
2027 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2029 GOTO(out, rc = -EPROTO);
2031 lock = ldlm_handle2lock(&aa->lock_handle);
2033 CERROR("received replay ack for unknown local cookie "LPX64
2034 " remote cookie "LPX64 " from server %s id %s\n",
2035 aa->lock_handle.cookie, reply->lock_handle.cookie,
2036 req->rq_export->exp_client_uuid.uuid,
2037 libcfs_id2str(req->rq_peer));
2038 GOTO(out, rc = -ESTALE);
2041 /* Key change rehash lock in per-export hash with new key */
2042 exp = req->rq_export;
2043 if (exp && exp->exp_lock_hash) {
2044 cfs_hash_rehash_key(exp->exp_lock_hash,
2045 &lock->l_remote_handle,
2046 &reply->lock_handle,
2049 lock->l_remote_handle = reply->lock_handle;
2052 LDLM_DEBUG(lock, "replayed lock:");
2053 ptlrpc_import_recovery_state_machine(req->rq_import);
2054 LDLM_LOCK_PUT(lock);
2057 ptlrpc_connect_import(req->rq_import);
2062 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2064 struct ptlrpc_request *req;
2065 struct ldlm_async_args *aa;
2066 struct ldlm_request *body;
2071 /* Bug 11974: Do not replay a lock which is actively being canceled */
2072 if (lock->l_flags & LDLM_FL_CANCELING) {
2073 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2077 /* If this is reply-less callback lock, we cannot replay it, since
2078 * server might have long dropped it, but notification of that event was
2079 * lost by network. (and server granted conflicting lock already) */
2080 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
2081 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2082 ldlm_lock_cancel(lock);
2086 * If granted mode matches the requested mode, this lock is granted.
2088 * If they differ, but we have a granted mode, then we were granted
2089 * one mode and now want another: ergo, converting.
2091 * If we haven't been granted anything and are on a resource list,
2092 * then we're blocked/waiting.
2094 * If we haven't been granted anything and we're NOT on a resource list,
2095 * then we haven't got a reply yet and don't have a known disposition.
2096 * This happens whenever a lock enqueue is the request that triggers
2099 if (lock->l_granted_mode == lock->l_req_mode)
2100 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2101 else if (lock->l_granted_mode)
2102 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2103 else if (!cfs_list_empty(&lock->l_res_link))
2104 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2106 flags = LDLM_FL_REPLAY;
2108 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2109 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2113 /* We're part of recovery, so don't wait for it. */
2114 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2116 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2117 ldlm_lock2desc(lock, &body->lock_desc);
2118 body->lock_flags = flags;
2120 ldlm_lock2handle(lock, &body->lock_handle[0]);
2121 if (lock->l_lvb_len != 0) {
2122 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2123 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2126 ptlrpc_request_set_replen(req);
2127 /* notify the server we've replayed all requests.
2128 * also, we mark the request to be put on a dedicated
2129 * queue to be processed after all request replayes.
2131 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2133 LDLM_DEBUG(lock, "replaying lock:");
2135 cfs_atomic_inc(&req->rq_import->imp_replay_inflight);
2136 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2137 aa = ptlrpc_req_async_args(req);
2138 aa->lock_handle = body->lock_handle[0];
2139 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2140 ptlrpcd_add_req(req, PDL_POLICY_LOCAL, -1);
2146 * Cancel as many unused locks as possible before replay. since we are
2147 * in recovery, we can't wait for any outstanding RPCs to send any RPC
2150 * Called only in recovery before replaying locks. there is no need to
2151 * replay locks that are unused. since the clients may hold thousands of
2152 * cached unused locks, dropping the unused locks can greatly reduce the
2153 * load on the servers at recovery time.
2155 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2158 CFS_LIST_HEAD(cancels);
2160 CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2161 "replay for namespace %s (%d)\n",
2162 ldlm_ns_name(ns), ns->ns_nr_unused);
2164 /* We don't need to care whether or not LRU resize is enabled
2165 * because the LDLM_CANCEL_NO_WAIT policy doesn't use the
2166 * count parameter */
2167 canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2168 LCF_LOCAL, LDLM_CANCEL_NO_WAIT);
2170 CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2171 canceled, ldlm_ns_name(ns));
2174 int ldlm_replay_locks(struct obd_import *imp)
2176 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2177 CFS_LIST_HEAD(list);
2178 struct ldlm_lock *lock, *next;
2183 LASSERT(cfs_atomic_read(&imp->imp_replay_inflight) == 0);
2185 /* don't replay locks if import failed recovery */
2186 if (imp->imp_vbr_failed)
2189 /* ensure this doesn't fall to 0 before all have been queued */
2190 cfs_atomic_inc(&imp->imp_replay_inflight);
2192 if (ldlm_cancel_unused_locks_before_replay)
2193 ldlm_cancel_unused_locks_for_replay(ns);
2195 ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2197 cfs_list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2198 cfs_list_del_init(&lock->l_pending_chain);
2200 LDLM_LOCK_RELEASE(lock);
2201 continue; /* or try to do the rest? */
2203 rc = replay_one_lock(imp, lock);
2204 LDLM_LOCK_RELEASE(lock);
2207 cfs_atomic_dec(&imp->imp_replay_inflight);