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 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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 static void interrupted_completion_wait(void *data)
57 struct lock_wait_data {
58 struct ldlm_lock *lwd_lock;
62 struct ldlm_async_args {
63 struct lustre_handle lock_handle;
66 int ldlm_expired_completion_wait(void *data)
68 struct lock_wait_data *lwd = data;
69 struct ldlm_lock *lock = lwd->lwd_lock;
70 struct obd_import *imp;
71 struct obd_device *obd;
74 if (lock->l_conn_export == NULL) {
75 static cfs_time_t next_dump = 0, last_dump = 0;
77 if (ptlrpc_check_suspend())
80 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
81 CFS_DURATION_T"s ago); not entering recovery in "
82 "server code, just going back to sleep",
83 lock->l_last_activity,
84 cfs_time_sub(cfs_time_current_sec(),
85 lock->l_last_activity));
86 if (cfs_time_after(cfs_time_current(), next_dump)) {
87 last_dump = next_dump;
88 next_dump = cfs_time_shift(300);
89 ldlm_namespace_dump(D_DLMTRACE,
90 lock->l_resource->lr_namespace);
92 libcfs_debug_dumplog();
97 obd = lock->l_conn_export->exp_obd;
98 imp = obd->u.cli.cl_import;
99 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
100 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
101 CFS_DURATION_T"s ago), entering recovery for %s@%s",
102 lock->l_last_activity,
103 cfs_time_sub(cfs_time_current_sec(), lock->l_last_activity),
104 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
109 /* We use the same basis for both server side and client side functions
110 from a single node. */
111 int ldlm_get_enq_timeout(struct ldlm_lock *lock)
113 int timeout = at_get(&lock->l_resource->lr_namespace->ns_at_estimate);
115 return obd_timeout / 2;
116 /* Since these are non-updating timeouts, we should be conservative.
117 It would be nice to have some kind of "early reply" mechanism for
118 lock callbacks too... */
119 timeout = min_t(int, at_max, timeout + (timeout >> 1)); /* 150% */
120 return max(timeout, ldlm_enqueue_min);
122 EXPORT_SYMBOL(ldlm_get_enq_timeout);
125 * Helper function for ldlm_completion_ast(), updating timings when lock is
128 static int ldlm_completion_tail(struct ldlm_lock *lock)
133 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
134 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
137 delay = cfs_time_sub(cfs_time_current_sec(),
138 lock->l_last_activity);
139 LDLM_DEBUG(lock, "client-side enqueue: granted after "
140 CFS_DURATION_T"s", delay);
142 /* Update our time estimate */
143 at_add(&lock->l_resource->lr_namespace->ns_at_estimate, delay);
150 * Implementation of ->l_completion_ast() for a client, that doesn't wait
151 * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
152 * other threads that cannot block for long.
154 int ldlm_completion_ast_async(struct ldlm_lock *lock, int flags, void *data)
158 if (flags == LDLM_FL_WAIT_NOREPROC) {
159 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
163 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
164 LDLM_FL_BLOCK_CONV))) {
165 cfs_waitq_signal(&lock->l_waitq);
166 RETURN(ldlm_completion_tail(lock));
169 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
171 ldlm_lock_dump(D_OTHER, lock, 0);
172 ldlm_reprocess_all(lock->l_resource);
177 * Client side LDLM "completion" AST. This is called in several cases:
179 * - when a reply to an ENQUEUE rpc is received from the server
180 * (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
181 * this point (determined by flags);
183 * - when LDLM_CP_CALLBACK rpc comes to client to notify it that lock has
186 * - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
189 * - to force all locks when resource is destroyed (cleanup_resource());
191 * - during lock conversion (not used currently).
193 * If lock is not granted in the first case, this function waits until second
194 * or penultimate cases happen in some other thread.
197 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
199 /* XXX ALLOCATE - 160 bytes */
200 struct lock_wait_data lwd;
201 struct obd_device *obd;
202 struct obd_import *imp = NULL;
203 struct l_wait_info lwi;
208 if (flags == LDLM_FL_WAIT_NOREPROC) {
209 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
213 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
214 LDLM_FL_BLOCK_CONV))) {
215 cfs_waitq_signal(&lock->l_waitq);
219 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
221 ldlm_lock_dump(D_OTHER, lock, 0);
225 obd = class_exp2obd(lock->l_conn_export);
227 /* if this is a local lock, then there is no import */
229 imp = obd->u.cli.cl_import;
232 /* Wait a long time for enqueue - server may have to callback a
233 lock from another client. Server will evict the other client if it
234 doesn't respond reasonably, and then give us the lock. */
235 timeout = ldlm_get_enq_timeout(lock) * 2;
239 if (lock->l_flags & LDLM_FL_NO_TIMEOUT) {
240 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
241 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
243 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
244 ldlm_expired_completion_wait,
245 interrupted_completion_wait, &lwd);
249 spin_lock(&imp->imp_lock);
250 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
251 spin_unlock(&imp->imp_lock);
254 if (ns_is_client(lock->l_resource->lr_namespace) &&
255 OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
256 OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
257 lock->l_flags |= LDLM_FL_FAIL_LOC;
260 /* Go to sleep until the lock is granted or cancelled. */
261 rc = l_wait_event(lock->l_waitq,
262 is_granted_or_cancelled(lock), &lwi);
266 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
271 RETURN(ldlm_completion_tail(lock));
275 * A helper to build a blocking ast function
277 * Perform a common operation for blocking asts:
278 * defferred lock cancellation.
280 * \param lock the lock blocking or canceling ast was called on
282 * \see mdt_blocking_ast
283 * \see ldlm_blocking_ast
285 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
290 lock->l_flags |= LDLM_FL_CBPENDING;
291 do_ast = (!lock->l_readers && !lock->l_writers);
292 unlock_res_and_lock(lock);
295 struct lustre_handle lockh;
298 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
299 ldlm_lock2handle(lock, &lockh);
300 rc = ldlm_cli_cancel(&lockh);
302 CERROR("ldlm_cli_cancel: %d\n", rc);
304 LDLM_DEBUG(lock, "Lock still has references, will be "
311 * Server blocking AST
313 * ->l_blocking_ast() callback for LDLM locks acquired by server-side
316 * \param lock the lock which blocks a request or cancelling lock
319 * \param flag indicates whether this cancelling or blocking callback
321 * \see ldlm_blocking_ast_nocheck
323 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
324 void *data, int flag)
328 if (flag == LDLM_CB_CANCELING) {
329 /* Don't need to do anything here. */
333 lock_res_and_lock(lock);
334 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
335 * that ldlm_blocking_ast is called just before intent_policy method
336 * takes the ns_lock, then by the time we get the lock, we might not
337 * be the correct blocking function anymore. So check, and return
339 if (lock->l_blocking_ast != ldlm_blocking_ast) {
340 unlock_res_and_lock(lock);
343 RETURN(ldlm_blocking_ast_nocheck(lock));
347 * ->l_glimpse_ast() for DLM extent locks acquired on the server-side. See
348 * comment in filter_intent_policy() on why you may need this.
350 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
353 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for
354 * that is rather subtle: with OST-side locking, it may so happen that
355 * _all_ extent locks are held by the OST. If client wants to obtain
356 * current file size it calls ll{,u}_glimpse_size(), and (as locks are
357 * on the server), dummy glimpse callback fires and does
358 * nothing. Client still receives correct file size due to the
359 * following fragment in filter_intent_policy():
361 * rc = l->l_glimpse_ast(l, NULL); // this will update the LVB
362 * if (rc != 0 && res->lr_namespace->ns_lvbo &&
363 * res->lr_namespace->ns_lvbo->lvbo_update) {
364 * res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
367 * that is, after glimpse_ast() fails, filter_lvbo_update() runs, and
368 * returns correct file size to the client.
370 return -ELDLM_NO_LOCK_DATA;
373 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
374 const struct ldlm_res_id *res_id,
375 ldlm_type_t type, ldlm_policy_data_t *policy,
376 ldlm_mode_t mode, int *flags,
377 ldlm_blocking_callback blocking,
378 ldlm_completion_callback completion,
379 ldlm_glimpse_callback glimpse,
380 void *data, __u32 lvb_len,
381 const __u64 *client_cookie,
382 struct lustre_handle *lockh)
384 struct ldlm_lock *lock;
386 const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
387 .lcs_blocking = blocking,
388 .lcs_glimpse = glimpse,
392 LASSERT(!(*flags & LDLM_FL_REPLAY));
393 if (unlikely(ns_is_client(ns))) {
394 CERROR("Trying to enqueue local lock in a shadow namespace\n");
398 lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len);
400 GOTO(out_nolock, err = -ENOMEM);
401 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
403 ldlm_lock_addref_internal(lock, mode);
404 ldlm_lock2handle(lock, lockh);
405 lock_res_and_lock(lock);
406 lock->l_flags |= LDLM_FL_LOCAL;
407 if (*flags & LDLM_FL_ATOMIC_CB)
408 lock->l_flags |= LDLM_FL_ATOMIC_CB;
409 unlock_res_and_lock(lock);
411 lock->l_policy_data = *policy;
412 if (client_cookie != NULL)
413 lock->l_client_cookie = *client_cookie;
414 if (type == LDLM_EXTENT)
415 lock->l_req_extent = policy->l_extent;
417 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
418 if (unlikely(err != ELDLM_OK))
422 *policy = lock->l_policy_data;
424 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
427 if (lock->l_completion_ast)
428 lock->l_completion_ast(lock, *flags, NULL);
430 LDLM_DEBUG(lock, "client-side local enqueue END");
433 LDLM_LOCK_RELEASE(lock);
438 static void failed_lock_cleanup(struct ldlm_namespace *ns,
439 struct ldlm_lock *lock,
440 struct lustre_handle *lockh, int mode)
444 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
445 lock_res_and_lock(lock);
446 /* Check that lock is not granted or failed, we might race. */
447 if ((lock->l_req_mode != lock->l_granted_mode) &&
448 !(lock->l_flags & LDLM_FL_FAILED)) {
449 /* Make sure that this lock will not be found by raced
450 * bl_ast and -EINVAL reply is sent to server anyways.
452 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
456 unlock_res_and_lock(lock);
460 "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
461 "LDLM_FL_ATOMIC_CB");
462 ldlm_lock_decref_and_cancel(lockh, mode);
464 LDLM_DEBUG(lock, "lock was granted or failed in race");
465 ldlm_lock_decref(lockh, mode);
468 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
469 * from llite/file.c/ll_file_flock(). */
470 if (lock->l_resource->lr_type == LDLM_FLOCK) {
471 ldlm_lock_destroy(lock);
475 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
476 ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
477 int *flags, void *lvb, __u32 lvb_len,
478 struct lustre_handle *lockh,int rc)
480 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
481 int is_replay = *flags & LDLM_FL_REPLAY;
482 struct lustre_handle old_hash_key;
483 struct ldlm_lock *lock;
484 struct ldlm_reply *reply;
485 int cleanup_phase = 1;
488 lock = ldlm_handle2lock(lockh);
489 /* ldlm_cli_enqueue is holding a reference on this lock. */
491 LASSERT(type == LDLM_FLOCK);
495 if (rc != ELDLM_OK) {
497 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
498 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
499 if (rc == ELDLM_LOCK_ABORTED) {
500 /* Before we return, swab the reply */
501 reply = req_capsule_server_get(&req->rq_pill,
506 struct ost_lvb *tmplvb;
508 req_capsule_set_size(&req->rq_pill,
509 &RMF_DLM_LVB, RCL_SERVER,
511 tmplvb = req_capsule_server_get(&req->rq_pill,
514 GOTO(cleanup, rc = -EPROTO);
516 memcpy(lvb, tmplvb, lvb_len);
522 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
524 GOTO(cleanup, rc = -EPROTO);
526 /* lock enqueued on the server */
529 lock_res_and_lock(lock);
530 old_hash_key = lock->l_remote_handle;
531 lock->l_remote_handle = reply->lock_handle;
533 /* Key change rehash lock in per-export hash with new key */
534 if (exp->exp_lock_hash)
535 cfs_hash_rehash_key(exp->exp_lock_hash, &old_hash_key,
536 &lock->l_remote_handle,
539 *flags = reply->lock_flags;
540 lock->l_flags |= reply->lock_flags & LDLM_INHERIT_FLAGS;
541 /* move NO_TIMEOUT flag to the lock to force ldlm_lock_match()
542 * to wait with no timeout as well */
543 lock->l_flags |= reply->lock_flags & LDLM_FL_NO_TIMEOUT;
544 unlock_res_and_lock(lock);
546 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
547 lock, reply->lock_handle.cookie, *flags);
549 /* If enqueue returned a blocked lock but the completion handler has
550 * already run, then it fixed up the resource and we don't need to do it
552 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
553 int newmode = reply->lock_desc.l_req_mode;
555 if (newmode && newmode != lock->l_req_mode) {
556 LDLM_DEBUG(lock, "server returned different mode %s",
557 ldlm_lockname[newmode]);
558 lock->l_req_mode = newmode;
561 if (memcmp(reply->lock_desc.l_resource.lr_name.name,
562 lock->l_resource->lr_name.name,
563 sizeof(struct ldlm_res_id))) {
564 CDEBUG(D_INFO, "remote intent success, locking "
565 "(%ld,%ld,%ld) instead of "
567 (long)reply->lock_desc.l_resource.lr_name.name[0],
568 (long)reply->lock_desc.l_resource.lr_name.name[1],
569 (long)reply->lock_desc.l_resource.lr_name.name[2],
570 (long)lock->l_resource->lr_name.name[0],
571 (long)lock->l_resource->lr_name.name[1],
572 (long)lock->l_resource->lr_name.name[2]);
574 rc = ldlm_lock_change_resource(ns, lock,
575 &reply->lock_desc.l_resource.lr_name);
576 if (rc || lock->l_resource == NULL)
577 GOTO(cleanup, rc = -ENOMEM);
578 LDLM_DEBUG(lock, "client-side enqueue, new resource");
581 if (!(type == LDLM_IBITS && !(exp->exp_connect_flags &
583 lock->l_policy_data =
584 reply->lock_desc.l_policy_data;
585 if (type != LDLM_PLAIN)
586 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
589 if ((*flags) & LDLM_FL_AST_SENT ||
590 /* Cancel extent locks as soon as possible on a liblustre client,
591 * because it cannot handle asynchronous ASTs robustly (see
593 (LIBLUSTRE_CLIENT && type == LDLM_EXTENT)) {
594 lock_res_and_lock(lock);
595 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
596 unlock_res_and_lock(lock);
597 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
600 /* If the lock has already been granted by a completion AST, don't
601 * clobber the LVB with an older one. */
602 if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
605 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
607 tmplvb = req_capsule_server_get(&req->rq_pill,
610 GOTO(cleanup, rc = -EPROTO);
611 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
615 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
616 if (lock->l_completion_ast != NULL) {
617 int err = lock->l_completion_ast(lock, *flags, NULL);
620 if (rc && type != LDLM_FLOCK) /* bug 9425, bug 10250 */
625 if (lvb_len && lvb != NULL) {
626 /* Copy the LVB here, and not earlier, because the completion
627 * AST (if any) can override what we got in the reply */
628 memcpy(lvb, lock->l_lvb_data, lvb_len);
631 LDLM_DEBUG(lock, "client-side enqueue END");
634 if (cleanup_phase == 1 && rc)
635 failed_lock_cleanup(ns, lock, lockh, mode);
636 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
638 LDLM_LOCK_RELEASE(lock);
642 /* PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
643 * a single page on the send/receive side. XXX: 512 should be changed
644 * to more adequate value. */
645 static inline int ldlm_req_handles_avail(int req_size, int off)
649 avail = min_t(int, LDLM_MAXREQSIZE, CFS_PAGE_SIZE - 512) - req_size;
650 if (likely(avail >= 0))
651 avail /= (int)sizeof(struct lustre_handle);
654 avail += LDLM_LOCKREQ_HANDLES - off;
659 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
660 enum req_location loc,
663 int size = req_capsule_msg_size(pill, loc);
664 return ldlm_req_handles_avail(size, off);
667 static inline int ldlm_format_handles_avail(struct obd_import *imp,
668 const struct req_format *fmt,
669 enum req_location loc, int off)
671 int size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
672 return ldlm_req_handles_avail(size, off);
675 /* Cancel lru locks and pack them into the enqueue request. Pack there the given
676 * @count locks in @cancels. */
677 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
678 int version, int opc, int canceloff,
679 struct list_head *cancels, int count)
681 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
682 struct req_capsule *pill = &req->rq_pill;
683 struct ldlm_request *dlm = NULL;
684 int flags, avail, to_free, bufcount, pack = 0;
691 if (exp_connect_cancelset(exp)) {
692 /* Estimate the amount of available space in the request. */
693 bufcount = req_capsule_filled_sizes(pill, RCL_CLIENT);
694 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
696 flags = ns_connect_lru_resize(ns) ?
697 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
698 to_free = !ns_connect_lru_resize(ns) &&
699 opc == LDLM_ENQUEUE ? 1 : 0;
701 /* Cancel lru locks here _only_ if the server supports
702 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
703 * rpc, what will make us slower. */
705 count += ldlm_cancel_lru_local(ns, cancels, to_free,
706 avail - count, 0, flags);
711 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
712 ldlm_request_bufsize(pack, opc));
715 rc = ptlrpc_request_pack(req, version, opc);
717 ldlm_lock_list_put(cancels, l_bl_ast, count);
721 if (exp_connect_cancelset(exp)) {
723 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
725 /* Skip first lock handler in ldlm_request_pack(),
726 * this method will incrment @lock_count according
727 * to the lock handle amount actually written to
729 dlm->lock_count = canceloff;
731 /* Pack into the request @pack lock handles. */
732 ldlm_cli_cancel_list(cancels, pack, req, 0);
733 /* Prepare and send separate cancel rpc for others. */
734 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
736 ldlm_lock_list_put(cancels, l_bl_ast, count);
741 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
742 struct list_head *cancels, int count)
744 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
745 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
748 /* If a request has some specific initialisation it is passed in @reqp,
749 * otherwise it is created in ldlm_cli_enqueue.
751 * Supports sync and async requests, pass @async flag accordingly. If a
752 * request was created in ldlm_cli_enqueue and it is the async request,
753 * pass it to the caller in @reqp. */
754 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
755 struct ldlm_enqueue_info *einfo,
756 const struct ldlm_res_id *res_id,
757 ldlm_policy_data_t *policy, int *flags,
758 void *lvb, __u32 lvb_len, struct lustre_handle *lockh,
761 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
762 struct ldlm_lock *lock;
763 struct ldlm_request *body;
764 int is_replay = *flags & LDLM_FL_REPLAY;
765 int req_passed_in = 1;
767 struct ptlrpc_request *req;
770 LASSERT(exp != NULL);
772 /* If we're replaying this lock, just check some invariants.
773 * If we're creating a new lock, get everything all setup nice. */
775 lock = ldlm_handle2lock_long(lockh, 0);
776 LASSERT(lock != NULL);
777 LDLM_DEBUG(lock, "client-side enqueue START");
778 LASSERT(exp == lock->l_conn_export);
780 const struct ldlm_callback_suite cbs = {
781 .lcs_completion = einfo->ei_cb_cp,
782 .lcs_blocking = einfo->ei_cb_bl,
783 .lcs_glimpse = einfo->ei_cb_gl,
784 .lcs_weigh = einfo->ei_cb_wg
786 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
787 einfo->ei_mode, &cbs, einfo->ei_cbdata,
791 /* for the local lock, add the reference */
792 ldlm_lock_addref_internal(lock, einfo->ei_mode);
793 ldlm_lock2handle(lock, lockh);
794 if (policy != NULL) {
795 /* INODEBITS_INTEROP: If the server does not support
796 * inodebits, we will request a plain lock in the
797 * descriptor (ldlm_lock2desc() below) but use an
798 * inodebits lock internally with both bits set.
800 if (einfo->ei_type == LDLM_IBITS &&
801 !(exp->exp_connect_flags & OBD_CONNECT_IBITS))
802 lock->l_policy_data.l_inodebits.bits =
803 MDS_INODELOCK_LOOKUP |
804 MDS_INODELOCK_UPDATE;
806 lock->l_policy_data = *policy;
809 if (einfo->ei_type == LDLM_EXTENT)
810 lock->l_req_extent = policy->l_extent;
811 LDLM_DEBUG(lock, "client-side enqueue START");
814 /* lock not sent to server yet */
816 if (reqp == NULL || *reqp == NULL) {
817 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
822 failed_lock_cleanup(ns, lock, lockh, einfo->ei_mode);
823 LDLM_LOCK_RELEASE(lock);
833 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
835 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
836 DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
839 lock->l_conn_export = exp;
840 lock->l_export = NULL;
841 lock->l_blocking_ast = einfo->ei_cb_bl;
843 /* Dump lock data into the request buffer */
844 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
845 ldlm_lock2desc(lock, &body->lock_desc);
846 body->lock_flags = *flags;
847 body->lock_handle[0] = *lockh;
849 /* Continue as normal. */
850 if (!req_passed_in) {
852 req_capsule_extend(&req->rq_pill,
853 &RQF_LDLM_ENQUEUE_LVB);
854 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
855 RCL_SERVER, lvb_len);
857 ptlrpc_request_set_replen(req);
861 * Liblustre client doesn't get extent locks, except for O_APPEND case
862 * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
863 * [i_size, OBD_OBJECT_EOF] lock is taken.
865 LASSERT(ergo(LIBLUSTRE_CLIENT, einfo->ei_type != LDLM_EXTENT ||
866 policy->l_extent.end == OBD_OBJECT_EOF));
869 LASSERT(reqp != NULL);
873 LDLM_DEBUG(lock, "sending request");
875 rc = ptlrpc_queue_wait(req);
877 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
878 einfo->ei_mode, flags, lvb, lvb_len,
881 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
882 * one reference that we took */
884 LDLM_LOCK_RELEASE(lock);
888 if (!req_passed_in && req != NULL) {
889 ptlrpc_req_finished(req);
897 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
900 struct ldlm_resource *res;
903 if (ns_is_client(lock->l_resource->lr_namespace)) {
904 CERROR("Trying to cancel local lock\n");
907 LDLM_DEBUG(lock, "client-side local convert");
909 res = ldlm_lock_convert(lock, new_mode, flags);
911 ldlm_reprocess_all(res);
916 LDLM_DEBUG(lock, "client-side local convert handler END");
921 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
922 * conversion of locks which are on the waiting or converting queue */
923 /* Caller of this code is supposed to take care of lock readers/writers
925 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, __u32 *flags)
927 struct ldlm_request *body;
928 struct ldlm_reply *reply;
929 struct ldlm_lock *lock;
930 struct ldlm_resource *res;
931 struct ptlrpc_request *req;
935 lock = ldlm_handle2lock(lockh);
942 if (lock->l_conn_export == NULL)
943 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
945 LDLM_DEBUG(lock, "client-side convert");
947 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
948 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
955 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
956 body->lock_handle[0] = lock->l_remote_handle;
958 body->lock_desc.l_req_mode = new_mode;
959 body->lock_flags = *flags;
962 ptlrpc_request_set_replen(req);
963 rc = ptlrpc_queue_wait(req);
967 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
969 GOTO(out, rc = -EPROTO);
972 GOTO(out, rc = req->rq_status);
974 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
976 ldlm_reprocess_all(res);
977 /* Go to sleep until the lock is granted. */
978 /* FIXME: or cancelled. */
979 if (lock->l_completion_ast) {
980 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
991 ptlrpc_req_finished(req);
995 /* Cancel locks locally.
997 * LDLM_FL_LOCAL_ONLY if tere is no need in a CANCEL rpc to the server;
998 * LDLM_FL_CANCELING otherwise;
999 * LDLM_FL_BL_AST if there is a need in a separate CANCEL rpc. */
1000 static int ldlm_cli_cancel_local(struct ldlm_lock *lock)
1002 int rc = LDLM_FL_LOCAL_ONLY;
1005 if (lock->l_conn_export) {
1008 LDLM_DEBUG(lock, "client-side cancel");
1009 /* Set this flag to prevent others from getting new references*/
1010 lock_res_and_lock(lock);
1011 lock->l_flags |= LDLM_FL_CBPENDING;
1012 local_only = (lock->l_flags &
1013 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1014 ldlm_cancel_callback(lock);
1015 rc = (lock->l_flags & LDLM_FL_BL_AST) ?
1016 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1017 unlock_res_and_lock(lock);
1020 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1022 rc = LDLM_FL_LOCAL_ONLY;
1024 ldlm_lock_cancel(lock);
1026 if (ns_is_client(lock->l_resource->lr_namespace)) {
1027 LDLM_ERROR(lock, "Trying to cancel local lock");
1030 LDLM_DEBUG(lock, "server-side local cancel");
1031 ldlm_lock_cancel(lock);
1032 ldlm_reprocess_all(lock->l_resource);
1033 LDLM_DEBUG(lock, "server-side local cancel handler END");
1039 /* Pack @count locks in @head into ldlm_request buffer at the offset @off,
1040 of the request @req. */
1041 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1042 struct list_head *head, int count)
1044 struct ldlm_request *dlm;
1045 struct ldlm_lock *lock;
1046 int max, packed = 0;
1049 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1050 LASSERT(dlm != NULL);
1052 /* Check the room in the request buffer. */
1053 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1054 sizeof(struct ldlm_request);
1055 max /= sizeof(struct lustre_handle);
1056 max += LDLM_LOCKREQ_HANDLES;
1057 LASSERT(max >= dlm->lock_count + count);
1059 /* XXX: it would be better to pack lock handles grouped by resource.
1060 * so that the server cancel would call filter_lvbo_update() less
1062 list_for_each_entry(lock, head, l_bl_ast) {
1065 LASSERT(lock->l_conn_export);
1066 /* Pack the lock handle to the given request buffer. */
1067 LDLM_DEBUG(lock, "packing");
1068 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1071 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1075 /* Prepare and send a batched cancel rpc, it will include count lock handles
1076 * of locks given in @head. */
1077 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1078 int count, int flags)
1080 struct ptlrpc_request *req = NULL;
1081 struct obd_import *imp;
1086 LASSERT(exp != NULL);
1089 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, obd_fail_val);
1091 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1094 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1095 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1102 imp = class_exp2cliimp(exp);
1103 if (imp == NULL || imp->imp_invalid) {
1105 "skipping cancel on invalid import %p\n", imp);
1109 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1111 GOTO(out, rc = -ENOMEM);
1113 bufcount = req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1114 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1115 ldlm_request_bufsize(count, LDLM_CANCEL));
1117 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1119 ptlrpc_request_free(req);
1122 req->rq_no_resend = 1;
1123 req->rq_no_delay = 1;
1125 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1126 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1127 ptlrpc_at_set_req_timeout(req);
1129 ldlm_cancel_pack(req, cancels, count);
1131 ptlrpc_request_set_replen(req);
1132 if (flags & LDLM_FL_ASYNC) {
1133 ptlrpcd_add_req(req, PSCOPE_OTHER);
1137 rc = ptlrpc_queue_wait(req);
1140 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1141 "out of sync -- not fatal\n",
1142 libcfs_nid2str(req->rq_import->
1143 imp_connection->c_peer.nid));
1145 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1146 req->rq_import_generation == imp->imp_generation) {
1147 ptlrpc_req_finished(req);
1149 } else if (rc != ELDLM_OK) {
1150 CERROR("Got rc %d from cancel RPC: canceling "
1158 ptlrpc_req_finished(req);
1161 return sent ? sent : rc;
1164 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1166 LASSERT(imp != NULL);
1167 return &imp->imp_obd->obd_namespace->ns_pool;
1171 * Update client's obd pool related fields with new SLV and Limit from \a req.
1173 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1175 struct obd_device *obd;
1176 __u64 old_slv, new_slv;
1179 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1180 !imp_connect_lru_resize(req->rq_import)))
1183 * Do nothing for corner cases.
1189 * In some cases RPC may contain slv and limit zeroed out. This is
1190 * the case when server does not support lru resize feature. This is
1191 * also possible in some recovery cases when server side reqs have no
1192 * ref to obd export and thus access to server side namespace is no
1195 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1196 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1197 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1198 "(SLV: "LPU64", Limit: %u)",
1199 lustre_msg_get_slv(req->rq_repmsg),
1200 lustre_msg_get_limit(req->rq_repmsg));
1204 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1205 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1206 obd = req->rq_import->imp_obd;
1209 * Set new SLV and Limit to obd fields to make accessible for pool
1210 * thread. We do not access obd_namespace and pool directly here
1211 * as there is no reliable way to make sure that they are still
1212 * alive in cleanup time. Evil races are possible which may cause
1213 * oops in that time.
1215 write_lock(&obd->obd_pool_lock);
1216 old_slv = obd->obd_pool_slv;
1217 obd->obd_pool_slv = new_slv;
1218 obd->obd_pool_limit = new_limit;
1219 write_unlock(&obd->obd_pool_lock);
1223 EXPORT_SYMBOL(ldlm_cli_update_pool);
1225 int ldlm_cli_cancel(struct lustre_handle *lockh)
1227 struct obd_export *exp;
1228 int avail, flags, count = 1, rc = 0;
1229 struct ldlm_namespace *ns;
1230 struct ldlm_lock *lock;
1231 CFS_LIST_HEAD(cancels);
1234 /* concurrent cancels on the same handle can happen */
1235 lock = ldlm_handle2lock_long(lockh, LDLM_FL_CANCELING);
1237 LDLM_DEBUG_NOLOCK("lock is already being destroyed\n");
1241 rc = ldlm_cli_cancel_local(lock);
1242 if (rc < 0 || rc == LDLM_FL_LOCAL_ONLY) {
1243 LDLM_LOCK_RELEASE(lock);
1244 RETURN(rc < 0 ? rc : 0);
1246 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1247 * rpc which goes to canceld portal, so we can cancel other lru locks
1248 * here and send them all as one LDLM_CANCEL rpc. */
1249 LASSERT(list_empty(&lock->l_bl_ast));
1250 list_add(&lock->l_bl_ast, &cancels);
1252 exp = lock->l_conn_export;
1253 if (exp_connect_cancelset(exp)) {
1254 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1259 ns = lock->l_resource->lr_namespace;
1260 flags = ns_connect_lru_resize(ns) ?
1261 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
1262 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1263 LDLM_FL_BL_AST, flags);
1265 ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1269 /* XXX until we will have compound requests and can cut cancels from generic rpc
1270 * we need send cancels with LDLM_FL_BL_AST flag as separate rpc */
1271 static int ldlm_cancel_list(struct list_head *cancels, int count, int flags)
1273 CFS_LIST_HEAD(head);
1274 struct ldlm_lock *lock, *next;
1275 int left = 0, bl_ast = 0, rc;
1278 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1282 if (flags & LDLM_FL_LOCAL_ONLY) {
1283 rc = LDLM_FL_LOCAL_ONLY;
1284 ldlm_lock_cancel(lock);
1286 rc = ldlm_cli_cancel_local(lock);
1288 if (!(flags & LDLM_FL_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1289 LDLM_DEBUG(lock, "Cancel lock separately");
1290 list_del_init(&lock->l_bl_ast);
1291 list_add(&lock->l_bl_ast, &head);
1295 if (rc == LDLM_FL_LOCAL_ONLY) {
1296 /* CANCEL RPC should not be sent to server. */
1297 list_del_init(&lock->l_bl_ast);
1298 LDLM_LOCK_RELEASE(lock);
1305 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1312 * Callback function for lru-resize policy. Makes decision whether to keep
1313 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1314 * \a added and number of locks to be preferably canceled \a count.
1316 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1318 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1320 static ldlm_policy_res_t ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1321 struct ldlm_lock *lock,
1322 int unused, int added,
1325 cfs_time_t cur = cfs_time_current();
1326 struct ldlm_pool *pl = &ns->ns_pool;
1331 * Stop lru processing when we reached passed @count or checked all
1334 if (count && added >= count)
1335 return LDLM_POLICY_KEEP_LOCK;
1337 slv = ldlm_pool_get_slv(pl);
1338 lvf = ldlm_pool_get_lvf(pl);
1339 la = cfs_duration_sec(cfs_time_sub(cur,
1340 lock->l_last_used));
1343 * Stop when slv is not yet come from server or lv is smaller than
1346 lv = lvf * la * unused;
1349 * Inform pool about current CLV to see it via proc.
1351 ldlm_pool_set_clv(pl, lv);
1352 return (slv == 1 || lv < slv) ?
1353 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1357 * Callback function for proc used policy. Makes decision whether to keep
1358 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1359 * \a added and number of locks to be preferably canceled \a count.
1361 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1363 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1365 static ldlm_policy_res_t ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1366 struct ldlm_lock *lock,
1367 int unused, int added,
1371 * Stop lru processing when we reached passed @count or checked all
1374 return (added >= count) ?
1375 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1379 * Callback function for aged policy. Makes decision whether to keep
1380 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1381 * \a added and number of locks to be preferably canceled \a count.
1383 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1385 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1387 static ldlm_policy_res_t ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1388 struct ldlm_lock *lock,
1389 int unused, int added,
1393 * Stop lru processing if young lock is found and we reached passed
1396 return ((added >= count) &&
1397 cfs_time_before(cfs_time_current(),
1398 cfs_time_add(lock->l_last_used,
1400 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1404 * Callback function for default policy. Makes decision whether to keep
1405 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1406 * \a added and number of locks to be preferably canceled \a count.
1408 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1410 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1412 static ldlm_policy_res_t ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1413 struct ldlm_lock *lock,
1414 int unused, int added,
1418 * Stop lru processing when we reached passed @count or checked all
1421 return (added >= count) ?
1422 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1425 typedef ldlm_policy_res_t (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *,
1426 struct ldlm_lock *, int,
1429 static ldlm_cancel_lru_policy_t
1430 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, int flags)
1432 if (ns_connect_lru_resize(ns)) {
1433 if (flags & LDLM_CANCEL_SHRINK)
1434 /* We kill passed number of old locks. */
1435 return ldlm_cancel_passed_policy;
1436 else if (flags & LDLM_CANCEL_LRUR)
1437 return ldlm_cancel_lrur_policy;
1438 else if (flags & LDLM_CANCEL_PASSED)
1439 return ldlm_cancel_passed_policy;
1441 if (flags & LDLM_CANCEL_AGED)
1442 return ldlm_cancel_aged_policy;
1445 return ldlm_cancel_default_policy;
1448 /* - Free space in lru for @count new locks,
1449 * redundant unused locks are canceled locally;
1450 * - also cancel locally unused aged locks;
1451 * - do not cancel more than @max locks;
1452 * - GET the found locks and add them into the @cancels list.
1454 * A client lock can be added to the l_bl_ast list only when it is
1455 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing CANCEL.
1456 * There are the following use cases: ldlm_cancel_resource_local(),
1457 * ldlm_cancel_lru_local() and ldlm_cli_cancel(), which check&set this
1458 * flag properly. As any attempt to cancel a lock rely on this flag,
1459 * l_bl_ast list is accessed later without any special locking.
1461 * Calling policies for enabled lru resize:
1462 * ----------------------------------------
1463 * flags & LDLM_CANCEL_LRUR - use lru resize policy (SLV from server) to
1464 * cancel not more than @count locks;
1466 * flags & LDLM_CANCEL_PASSED - cancel @count number of old locks (located at
1467 * the beginning of lru list);
1469 * flags & LDLM_CANCEL_SHRINK - cancel not more than @count locks according to
1470 * memory pressre policy function;
1472 * flags & LDLM_CANCEL_AGED - cancel alocks according to "aged policy".
1474 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1475 int count, int max, int cancel_flags, int flags)
1477 ldlm_cancel_lru_policy_t pf;
1478 struct ldlm_lock *lock, *next;
1479 int added = 0, unused;
1482 spin_lock(&ns->ns_unused_lock);
1483 unused = ns->ns_nr_unused;
1485 if (!ns_connect_lru_resize(ns))
1486 count += unused - ns->ns_max_unused;
1488 pf = ldlm_cancel_lru_policy(ns, flags);
1489 LASSERT(pf != NULL);
1491 while (!list_empty(&ns->ns_unused_list)) {
1492 /* For any flags, stop scanning if @max is reached. */
1493 if (max && added >= max)
1496 list_for_each_entry_safe(lock, next, &ns->ns_unused_list, l_lru){
1497 /* No locks which got blocking requests. */
1498 LASSERT(!(lock->l_flags & LDLM_FL_BL_AST));
1500 /* Somebody is already doing CANCEL. No need in this
1501 * lock in lru, do not traverse it again. */
1502 if (!(lock->l_flags & LDLM_FL_CANCELING))
1505 ldlm_lock_remove_from_lru_nolock(lock);
1507 if (&lock->l_lru == &ns->ns_unused_list)
1510 LDLM_LOCK_GET(lock);
1511 spin_unlock(&ns->ns_unused_lock);
1512 lu_ref_add(&lock->l_reference, __FUNCTION__, cfs_current());
1514 /* Pass the lock through the policy filter and see if it
1515 * should stay in lru.
1517 * Even for shrinker policy we stop scanning if
1518 * we find a lock that should stay in the cache.
1519 * We should take into account lock age anyway
1520 * as new lock even if it is small of weight is
1521 * valuable resource.
1523 * That is, for shrinker policy we drop only
1524 * old locks, but additionally chose them by
1525 * their weight. Big extent locks will stay in
1527 if (pf(ns, lock, unused, added, count) ==
1528 LDLM_POLICY_KEEP_LOCK) {
1529 lu_ref_del(&lock->l_reference,
1530 __FUNCTION__, cfs_current());
1531 LDLM_LOCK_RELEASE(lock);
1532 spin_lock(&ns->ns_unused_lock);
1536 lock_res_and_lock(lock);
1537 /* Check flags again under the lock. */
1538 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1539 (ldlm_lock_remove_from_lru(lock) == 0)) {
1540 /* other thread is removing lock from lru or
1541 * somebody is already doing CANCEL or
1542 * there is a blocking request which will send
1543 * cancel by itseft or the lock is matched
1544 * is already not unused. */
1545 unlock_res_and_lock(lock);
1546 lu_ref_del(&lock->l_reference,
1547 __FUNCTION__, cfs_current());
1548 LDLM_LOCK_RELEASE(lock);
1549 spin_lock(&ns->ns_unused_lock);
1552 LASSERT(!lock->l_readers && !lock->l_writers);
1554 /* If we have chosen to cancel this lock voluntarily, we
1555 * better send cancel notification to server, so that it
1556 * frees appropriate state. This might lead to a race
1557 * where while we are doing cancel here, server is also
1558 * silently cancelling this lock. */
1559 lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
1561 /* Setting the CBPENDING flag is a little misleading,
1562 * but prevents an important race; namely, once
1563 * CBPENDING is set, the lock can accumulate no more
1564 * readers/writers. Since readers and writers are
1565 * already zero here, ldlm_lock_decref() won't see
1566 * this flag and call l_blocking_ast */
1567 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1569 /* We can't re-add to l_lru as it confuses the
1570 * refcounting in ldlm_lock_remove_from_lru() if an AST
1571 * arrives after we drop ns_lock below. We use l_bl_ast
1572 * and can't use l_pending_chain as it is used both on
1573 * server and client nevertheless bug 5666 says it is
1574 * used only on server */
1575 LASSERT(list_empty(&lock->l_bl_ast));
1576 list_add(&lock->l_bl_ast, cancels);
1577 unlock_res_and_lock(lock);
1578 lu_ref_del(&lock->l_reference, __FUNCTION__, cfs_current());
1579 spin_lock(&ns->ns_unused_lock);
1583 spin_unlock(&ns->ns_unused_lock);
1584 RETURN(ldlm_cancel_list(cancels, added, cancel_flags));
1587 /* when called with LDLM_ASYNC the blocking callback will be handled
1588 * in a thread and this function will return after the thread has been
1589 * asked to call the callback. when called with LDLM_SYNC the blocking
1590 * callback will be performed in this function. */
1591 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr, ldlm_sync_t sync,
1594 CFS_LIST_HEAD(cancels);
1599 sync = LDLM_SYNC; /* force to be sync in user space */
1601 count = ldlm_cancel_lru_local(ns, &cancels, nr, 0, 0, flags);
1602 if (sync == LDLM_ASYNC) {
1603 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count);
1608 /* If an error occured in ASYNC mode, or this is SYNC mode,
1609 * cancel the list. */
1610 ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1614 /* Find and cancel locally unused locks found on resource, matched to the
1615 * given policy, mode. GET the found locks and add them into the @cancels
1617 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1618 struct list_head *cancels,
1619 ldlm_policy_data_t *policy,
1620 ldlm_mode_t mode, int lock_flags,
1621 int cancel_flags, void *opaque)
1623 struct ldlm_lock *lock;
1628 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1629 if (opaque != NULL && lock->l_ast_data != opaque) {
1630 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1631 lock->l_ast_data, opaque);
1636 if (lock->l_readers || lock->l_writers) {
1637 if (cancel_flags & LDLM_FL_WARN) {
1638 LDLM_ERROR(lock, "lock in use");
1644 /* If somebody is already doing CANCEL, or blocking ast came,
1645 * skip this lock. */
1646 if (lock->l_flags & LDLM_FL_BL_AST ||
1647 lock->l_flags & LDLM_FL_CANCELING)
1650 if (lockmode_compat(lock->l_granted_mode, mode))
1653 /* If policy is given and this is IBITS lock, add to list only
1654 * those locks that match by policy. */
1655 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1656 !(lock->l_policy_data.l_inodebits.bits &
1657 policy->l_inodebits.bits))
1660 /* See CBPENDING comment in ldlm_cancel_lru */
1661 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1664 LASSERT(list_empty(&lock->l_bl_ast));
1665 list_add(&lock->l_bl_ast, cancels);
1666 LDLM_LOCK_GET(lock);
1671 RETURN(ldlm_cancel_list(cancels, count, cancel_flags));
1674 /* If @req is NULL, send CANCEL request to server with handles of locks
1675 * in the @cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1676 * separately per lock.
1677 * If @req is not NULL, put handles of locks in @cancels into the request
1678 * buffer at the offset @off.
1679 * Destroy @cancels at the end. */
1680 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1681 struct ptlrpc_request *req, int flags)
1683 struct ldlm_lock *lock;
1687 if (list_empty(cancels) || count == 0)
1690 /* XXX: requests (both batched and not) could be sent in parallel.
1691 * Usually it is enough to have just 1 RPC, but it is possible that
1692 * there are to many locks to be cancelled in LRU or on a resource.
1693 * It would also speed up the case when the server does not support
1696 LASSERT(!list_empty(cancels));
1697 lock = list_entry(cancels->next, struct ldlm_lock, l_bl_ast);
1698 LASSERT(lock->l_conn_export);
1700 if (exp_connect_cancelset(lock->l_conn_export)) {
1703 ldlm_cancel_pack(req, cancels, count);
1705 res = ldlm_cli_cancel_req(lock->l_conn_export,
1709 res = ldlm_cli_cancel_req(lock->l_conn_export,
1714 CERROR("ldlm_cli_cancel_list: %d\n", res);
1719 ldlm_lock_list_put(cancels, l_bl_ast, res);
1721 LASSERT(count == 0);
1725 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1726 const struct ldlm_res_id *res_id,
1727 ldlm_policy_data_t *policy,
1728 ldlm_mode_t mode, int flags, void *opaque)
1730 struct ldlm_resource *res;
1731 CFS_LIST_HEAD(cancels);
1736 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1738 /* This is not a problem. */
1739 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1743 LDLM_RESOURCE_ADDREF(res);
1744 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1746 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1748 CERROR("ldlm_cli_cancel_unused_resource: %d\n", rc);
1750 LDLM_RESOURCE_DELREF(res);
1751 ldlm_resource_putref(res);
1755 static inline int have_no_nsresource(struct ldlm_namespace *ns)
1757 int no_resource = 0;
1759 spin_lock(&ns->ns_hash_lock);
1760 if (ns->ns_resources == 0)
1762 spin_unlock(&ns->ns_hash_lock);
1764 RETURN(no_resource);
1767 /* Cancel all locks on a namespace (or a specific resource, if given)
1768 * that have 0 readers/writers.
1770 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1771 * to notify the server. */
1772 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
1773 const struct ldlm_res_id *res_id,
1774 int flags, void *opaque)
1783 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
1787 spin_lock(&ns->ns_hash_lock);
1788 for (i = 0; i < RES_HASH_SIZE; i++) {
1789 struct list_head *tmp;
1790 tmp = ns->ns_hash[i].next;
1791 while (tmp != &(ns->ns_hash[i])) {
1792 struct ldlm_resource *res;
1795 res = list_entry(tmp, struct ldlm_resource, lr_hash);
1796 ldlm_resource_getref(res);
1797 spin_unlock(&ns->ns_hash_lock);
1799 LDLM_RESOURCE_ADDREF(res);
1800 rc = ldlm_cli_cancel_unused_resource(ns, &res->lr_name,
1805 CERROR("ldlm_cli_cancel_unused ("LPU64"): %d\n",
1806 res->lr_name.name[0], rc);
1808 LDLM_RESOURCE_DELREF(res);
1809 spin_lock(&ns->ns_hash_lock);
1811 ldlm_resource_putref_locked(res);
1814 spin_unlock(&ns->ns_hash_lock);
1819 /* Lock iterators. */
1821 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1824 struct list_head *tmp, *next;
1825 struct ldlm_lock *lock;
1826 int rc = LDLM_ITER_CONTINUE;
1831 RETURN(LDLM_ITER_CONTINUE);
1834 list_for_each_safe(tmp, next, &res->lr_granted) {
1835 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1837 if (iter(lock, closure) == LDLM_ITER_STOP)
1838 GOTO(out, rc = LDLM_ITER_STOP);
1841 list_for_each_safe(tmp, next, &res->lr_converting) {
1842 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1844 if (iter(lock, closure) == LDLM_ITER_STOP)
1845 GOTO(out, rc = LDLM_ITER_STOP);
1848 list_for_each_safe(tmp, next, &res->lr_waiting) {
1849 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1851 if (iter(lock, closure) == LDLM_ITER_STOP)
1852 GOTO(out, rc = LDLM_ITER_STOP);
1859 struct iter_helper_data {
1860 ldlm_iterator_t iter;
1864 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1866 struct iter_helper_data *helper = closure;
1867 return helper->iter(lock, helper->closure);
1870 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
1872 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
1875 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
1878 struct iter_helper_data helper = { iter: iter, closure: closure };
1879 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
1882 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
1883 ldlm_res_iterator_t iter, void *closure)
1885 int i, rc = LDLM_ITER_CONTINUE;
1886 struct ldlm_resource *res;
1887 struct list_head *tmp;
1890 spin_lock(&ns->ns_hash_lock);
1891 for (i = 0; i < RES_HASH_SIZE; i++) {
1892 tmp = ns->ns_hash[i].next;
1893 while (tmp != &(ns->ns_hash[i])) {
1894 res = list_entry(tmp, struct ldlm_resource, lr_hash);
1895 ldlm_resource_getref(res);
1896 spin_unlock(&ns->ns_hash_lock);
1897 LDLM_RESOURCE_ADDREF(res);
1899 rc = iter(res, closure);
1901 LDLM_RESOURCE_DELREF(res);
1902 spin_lock(&ns->ns_hash_lock);
1904 ldlm_resource_putref_locked(res);
1905 if (rc == LDLM_ITER_STOP)
1910 spin_unlock(&ns->ns_hash_lock);
1914 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
1915 void ldlm_resource_iterate(struct ldlm_namespace *ns,
1916 const struct ldlm_res_id *res_id,
1917 ldlm_iterator_t iter, void *data)
1919 struct ldlm_resource *res;
1923 CERROR("must pass in namespace\n");
1927 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1933 LDLM_RESOURCE_ADDREF(res);
1934 ldlm_resource_foreach(res, iter, data);
1935 LDLM_RESOURCE_DELREF(res);
1936 ldlm_resource_putref(res);
1942 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
1944 struct list_head *list = closure;
1946 /* we use l_pending_chain here, because it's unused on clients. */
1947 LASSERTF(list_empty(&lock->l_pending_chain),"lock %p next %p prev %p\n",
1948 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
1949 /* bug 9573: don't replay locks left after eviction, or
1950 * bug 17614: locks being actively cancelled. Get a reference
1951 * on a lock so that it does not disapear under us (e.g. due to cancel)
1953 if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_CANCELING))) {
1954 list_add(&lock->l_pending_chain, list);
1955 LDLM_LOCK_GET(lock);
1958 return LDLM_ITER_CONTINUE;
1961 static int replay_lock_interpret(const struct lu_env *env,
1962 struct ptlrpc_request *req,
1963 struct ldlm_async_args *aa, int rc)
1965 struct lustre_handle old_hash_key;
1966 struct ldlm_lock *lock;
1967 struct ldlm_reply *reply;
1968 struct obd_export *exp;
1971 atomic_dec(&req->rq_import->imp_replay_inflight);
1976 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1978 GOTO(out, rc = -EPROTO);
1980 lock = ldlm_handle2lock(&aa->lock_handle);
1982 CERROR("received replay ack for unknown local cookie "LPX64
1983 " remote cookie "LPX64 " from server %s id %s\n",
1984 aa->lock_handle.cookie, reply->lock_handle.cookie,
1985 req->rq_export->exp_client_uuid.uuid,
1986 libcfs_id2str(req->rq_peer));
1987 GOTO(out, rc = -ESTALE);
1990 old_hash_key = lock->l_remote_handle;
1991 lock->l_remote_handle = reply->lock_handle;
1993 /* Key change rehash lock in per-export hash with new key */
1994 exp = req->rq_export;
1995 if (exp && exp->exp_lock_hash)
1996 cfs_hash_rehash_key(exp->exp_lock_hash, &old_hash_key,
1997 &lock->l_remote_handle,
2000 LDLM_DEBUG(lock, "replayed lock:");
2001 ptlrpc_import_recovery_state_machine(req->rq_import);
2002 LDLM_LOCK_PUT(lock);
2005 ptlrpc_connect_import(req->rq_import, NULL);
2010 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2012 struct ptlrpc_request *req;
2013 struct ldlm_async_args *aa;
2014 struct ldlm_request *body;
2019 /* Bug 11974: Do not replay a lock which is actively being canceled */
2020 if (lock->l_flags & LDLM_FL_CANCELING) {
2021 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2025 /* If this is reply-less callback lock, we cannot replay it, since
2026 * server might have long dropped it, but notification of that event was
2027 * lost by network. (and server granted conflicting lock already) */
2028 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
2029 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2030 ldlm_lock_cancel(lock);
2034 * If granted mode matches the requested mode, this lock is granted.
2036 * If they differ, but we have a granted mode, then we were granted
2037 * one mode and now want another: ergo, converting.
2039 * If we haven't been granted anything and are on a resource list,
2040 * then we're blocked/waiting.
2042 * If we haven't been granted anything and we're NOT on a resource list,
2043 * then we haven't got a reply yet and don't have a known disposition.
2044 * This happens whenever a lock enqueue is the request that triggers
2047 if (lock->l_granted_mode == lock->l_req_mode)
2048 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2049 else if (lock->l_granted_mode)
2050 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2051 else if (!list_empty(&lock->l_res_link))
2052 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2054 flags = LDLM_FL_REPLAY;
2056 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2057 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2061 /* We're part of recovery, so don't wait for it. */
2062 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2064 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2065 ldlm_lock2desc(lock, &body->lock_desc);
2066 body->lock_flags = flags;
2068 ldlm_lock2handle(lock, &body->lock_handle[0]);
2069 if (lock->l_lvb_len != 0) {
2070 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2071 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2074 ptlrpc_request_set_replen(req);
2075 /* notify the server we've replayed all requests.
2076 * also, we mark the request to be put on a dedicated
2077 * queue to be processed after all request replayes.
2079 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2081 LDLM_DEBUG(lock, "replaying lock:");
2083 atomic_inc(&req->rq_import->imp_replay_inflight);
2084 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2085 aa = ptlrpc_req_async_args(req);
2086 aa->lock_handle = body->lock_handle[0];
2087 req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2088 ptlrpcd_add_req(req, PSCOPE_OTHER);
2093 int ldlm_replay_locks(struct obd_import *imp)
2095 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2096 CFS_LIST_HEAD(list);
2097 struct ldlm_lock *lock, *next;
2102 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2104 /* ensure this doesn't fall to 0 before all have been queued */
2105 atomic_inc(&imp->imp_replay_inflight);
2107 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2109 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2110 list_del_init(&lock->l_pending_chain);
2112 LDLM_LOCK_PUT(lock);
2113 continue; /* or try to do the rest? */
2115 rc = replay_one_lock(imp, lock);
2116 LDLM_LOCK_PUT(lock);
2119 atomic_dec(&imp->imp_replay_inflight);