1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
25 #define DEBUG_SUBSYSTEM S_LDLM
28 #include <liblustre.h>
31 #include <lustre_dlm.h>
32 #include <obd_class.h>
35 #include "ldlm_internal.h"
37 static void interrupted_completion_wait(void *data)
41 struct lock_wait_data {
42 struct ldlm_lock *lwd_lock;
46 struct ldlm_async_args {
47 struct lustre_handle lock_handle;
50 int ldlm_expired_completion_wait(void *data)
52 struct lock_wait_data *lwd = data;
53 struct ldlm_lock *lock = lwd->lwd_lock;
54 struct obd_import *imp;
55 struct obd_device *obd;
58 if (lock->l_conn_export == NULL) {
59 static cfs_time_t next_dump = 0, last_dump = 0;
61 if (ptlrpc_check_suspend())
64 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
65 CFS_DURATION_T"s ago); not entering recovery in "
66 "server code, just going back to sleep",
67 lock->l_enqueued_time.tv_sec,
68 cfs_time_current_sec() - lock->l_enqueued_time.tv_sec);
69 if (cfs_time_after(cfs_time_current(), next_dump)) {
70 last_dump = next_dump;
71 next_dump = cfs_time_shift(300);
72 ldlm_namespace_dump(D_DLMTRACE,
73 lock->l_resource->lr_namespace);
75 libcfs_debug_dumplog();
80 obd = lock->l_conn_export->exp_obd;
81 imp = obd->u.cli.cl_import;
82 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
83 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
84 CFS_DURATION_T"s ago), entering recovery for %s@%s",
85 lock->l_enqueued_time.tv_sec,
86 cfs_time_current_sec() - lock->l_enqueued_time.tv_sec,
87 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
92 static int is_granted_or_cancelled(struct ldlm_lock *lock)
96 lock_res_and_lock(lock);
97 if (((lock->l_req_mode == lock->l_granted_mode) &&
98 !(lock->l_flags & LDLM_FL_CP_REQD)) ||
99 (lock->l_flags & LDLM_FL_FAILED))
101 unlock_res_and_lock(lock);
106 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
108 /* XXX ALLOCATE - 160 bytes */
109 struct lock_wait_data lwd;
110 struct obd_device *obd;
111 struct obd_import *imp = NULL;
112 struct l_wait_info lwi;
116 if (flags == LDLM_FL_WAIT_NOREPROC) {
117 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
121 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
122 LDLM_FL_BLOCK_CONV))) {
123 cfs_waitq_signal(&lock->l_waitq);
127 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
129 ldlm_lock_dump(D_OTHER, lock, 0);
130 ldlm_reprocess_all(lock->l_resource);
134 obd = class_exp2obd(lock->l_conn_export);
136 /* if this is a local lock, then there is no import */
138 imp = obd->u.cli.cl_import;
142 if (lock->l_flags & LDLM_FL_NO_TIMEOUT) {
143 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
144 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
146 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
147 ldlm_expired_completion_wait,
148 interrupted_completion_wait, &lwd);
152 spin_lock(&imp->imp_lock);
153 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
154 spin_unlock(&imp->imp_lock);
157 /* Go to sleep until the lock is granted or cancelled. */
158 rc = l_wait_event(lock->l_waitq, is_granted_or_cancelled(lock), &lwi);
160 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
161 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
166 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
171 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
176 * ->l_blocking_ast() callback for LDLM locks acquired by server-side OBDs.
178 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
179 void *data, int flag)
184 if (flag == LDLM_CB_CANCELING) {
185 /* Don't need to do anything here. */
189 lock_res_and_lock(lock);
190 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
191 * that ldlm_blocking_ast is called just before intent_policy method
192 * takes the ns_lock, then by the time we get the lock, we might not
193 * be the correct blocking function anymore. So check, and return
195 if (lock->l_blocking_ast != ldlm_blocking_ast) {
196 unlock_res_and_lock(lock);
200 lock->l_flags |= LDLM_FL_CBPENDING;
201 do_ast = (!lock->l_readers && !lock->l_writers);
202 unlock_res_and_lock(lock);
205 struct lustre_handle lockh;
208 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
209 ldlm_lock2handle(lock, &lockh);
210 rc = ldlm_cli_cancel(&lockh);
212 CERROR("ldlm_cli_cancel: %d\n", rc);
214 LDLM_DEBUG(lock, "Lock still has references, will be "
221 * ->l_glimpse_ast() for DLM extent locks acquired on the server-side. See
222 * comment in filter_intent_policy() on why you may need this.
224 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
227 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for
228 * that is rather subtle: with OST-side locking, it may so happen that
229 * _all_ extent locks are held by the OST. If client wants to obtain
230 * current file size it calls ll{,u}_glimpse_size(), and (as locks are
231 * on the server), dummy glimpse callback fires and does
232 * nothing. Client still receives correct file size due to the
233 * following fragment in filter_intent_policy():
235 * rc = l->l_glimpse_ast(l, NULL); // this will update the LVB
236 * if (rc != 0 && res->lr_namespace->ns_lvbo &&
237 * res->lr_namespace->ns_lvbo->lvbo_update) {
238 * res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
241 * that is, after glimpse_ast() fails, filter_lvbo_update() runs, and
242 * returns correct file size to the client.
244 return -ELDLM_NO_LOCK_DATA;
247 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
248 const struct ldlm_res_id *res_id,
249 ldlm_type_t type, ldlm_policy_data_t *policy,
250 ldlm_mode_t mode, int *flags,
251 ldlm_blocking_callback blocking,
252 ldlm_completion_callback completion,
253 ldlm_glimpse_callback glimpse,
254 void *data, __u32 lvb_len, void *lvb_swabber,
255 struct lustre_handle *lockh)
257 struct ldlm_lock *lock;
261 LASSERT(!(*flags & LDLM_FL_REPLAY));
262 if (unlikely(ns_is_client(ns))) {
263 CERROR("Trying to enqueue local lock in a shadow namespace\n");
267 lock = ldlm_lock_create(ns, res_id, type, mode, blocking,
268 completion, glimpse, data, lvb_len);
270 GOTO(out_nolock, err = -ENOMEM);
271 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
273 ldlm_lock_addref_internal(lock, mode);
274 ldlm_lock2handle(lock, lockh);
275 lock_res_and_lock(lock);
276 lock->l_flags |= LDLM_FL_LOCAL;
277 if (*flags & LDLM_FL_ATOMIC_CB)
278 lock->l_flags |= LDLM_FL_ATOMIC_CB;
279 lock->l_lvb_swabber = lvb_swabber;
280 unlock_res_and_lock(lock);
282 lock->l_policy_data = *policy;
283 if (type == LDLM_EXTENT)
284 lock->l_req_extent = policy->l_extent;
286 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
287 if (unlikely(err != ELDLM_OK))
291 *policy = lock->l_policy_data;
293 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
296 if (lock->l_completion_ast)
297 lock->l_completion_ast(lock, *flags, NULL);
299 LDLM_DEBUG(lock, "client-side local enqueue END");
307 static void failed_lock_cleanup(struct ldlm_namespace *ns,
308 struct ldlm_lock *lock,
309 struct lustre_handle *lockh, int mode)
311 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
312 lock_res_and_lock(lock);
313 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
314 unlock_res_and_lock(lock);
315 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
317 ldlm_lock_decref_and_cancel(lockh, mode);
319 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
320 * from llite/file.c/ll_file_flock(). */
321 if (lock->l_resource->lr_type == LDLM_FLOCK) {
322 ldlm_lock_destroy(lock);
326 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
327 ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
328 int *flags, void *lvb, __u32 lvb_len,
329 void *lvb_swabber, struct lustre_handle *lockh,int rc)
331 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
332 int is_replay = *flags & LDLM_FL_REPLAY;
333 struct ldlm_lock *lock;
334 struct ldlm_reply *reply;
335 int cleanup_phase = 1;
338 lock = ldlm_handle2lock(lockh);
339 /* ldlm_cli_enqueue is holding a reference on this lock. */
341 LASSERT(type == LDLM_FLOCK);
345 if (rc != ELDLM_OK) {
347 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
348 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
349 if (rc == ELDLM_LOCK_ABORTED) {
350 /* Before we return, swab the reply */
351 reply = req_capsule_server_get(&req->rq_pill,
356 struct ost_lvb *tmplvb;
358 req_capsule_set_size(&req->rq_pill,
359 &RMF_DLM_LVB, RCL_SERVER,
361 tmplvb = req_capsule_server_swab_get(&req->rq_pill,
365 GOTO(cleanup, rc = -EPROTO);
367 memcpy(lvb, tmplvb, lvb_len);
373 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
375 GOTO(cleanup, rc = -EPROTO);
377 /* lock enqueued on the server */
380 lock_res_and_lock(lock);
381 lock->l_remote_handle = reply->lock_handle;
382 *flags = reply->lock_flags;
383 lock->l_flags |= reply->lock_flags & LDLM_INHERIT_FLAGS;
384 /* move NO_TIMEOUT flag to the lock to force ldlm_lock_match()
385 * to wait with no timeout as well */
386 lock->l_flags |= reply->lock_flags & LDLM_FL_NO_TIMEOUT;
387 unlock_res_and_lock(lock);
389 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
390 lock, reply->lock_handle.cookie, *flags);
392 /* If enqueue returned a blocked lock but the completion handler has
393 * already run, then it fixed up the resource and we don't need to do it
395 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
396 int newmode = reply->lock_desc.l_req_mode;
398 if (newmode && newmode != lock->l_req_mode) {
399 LDLM_DEBUG(lock, "server returned different mode %s",
400 ldlm_lockname[newmode]);
401 lock->l_req_mode = newmode;
404 if (memcmp(reply->lock_desc.l_resource.lr_name.name,
405 lock->l_resource->lr_name.name,
406 sizeof(struct ldlm_res_id))) {
407 CDEBUG(D_INFO, "remote intent success, locking "
408 "(%ld,%ld,%ld) instead of "
410 (long)reply->lock_desc.l_resource.lr_name.name[0],
411 (long)reply->lock_desc.l_resource.lr_name.name[1],
412 (long)reply->lock_desc.l_resource.lr_name.name[2],
413 (long)lock->l_resource->lr_name.name[0],
414 (long)lock->l_resource->lr_name.name[1],
415 (long)lock->l_resource->lr_name.name[2]);
417 rc = ldlm_lock_change_resource(ns, lock,
418 &reply->lock_desc.l_resource.lr_name);
419 if (rc || lock->l_resource == NULL)
420 GOTO(cleanup, rc = -ENOMEM);
421 LDLM_DEBUG(lock, "client-side enqueue, new resource");
424 if (!(type == LDLM_IBITS && !(exp->exp_connect_flags &
426 lock->l_policy_data =
427 reply->lock_desc.l_policy_data;
428 if (type != LDLM_PLAIN)
429 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
432 if ((*flags) & LDLM_FL_AST_SENT ||
433 /* Cancel extent locks as soon as possible on a liblustre client,
434 * because it cannot handle asynchronous ASTs robustly (see
436 (LIBLUSTRE_CLIENT && type == LDLM_EXTENT)) {
437 lock_res_and_lock(lock);
438 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
439 unlock_res_and_lock(lock);
440 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
443 /* If the lock has already been granted by a completion AST, don't
444 * clobber the LVB with an older one. */
445 if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
448 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
450 tmplvb = req_capsule_server_swab_get(&req->rq_pill,
454 GOTO(cleanup, rc = -EPROTO);
455 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
459 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
460 if (lock->l_completion_ast != NULL) {
461 int err = lock->l_completion_ast(lock, *flags, NULL);
464 if (rc && type != LDLM_FLOCK) /* bug 9425, bug 10250 */
469 if (lvb_len && lvb != NULL) {
470 /* Copy the LVB here, and not earlier, because the completion
471 * AST (if any) can override what we got in the reply */
472 memcpy(lvb, lock->l_lvb_data, lvb_len);
475 LDLM_DEBUG(lock, "client-side enqueue END");
478 if (cleanup_phase == 1 && rc)
479 failed_lock_cleanup(ns, lock, lockh, mode);
480 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
486 /* PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
487 * a single page on the send/receive side. XXX: 512 should be changed
488 * to more adequate value. */
489 static inline int ldlm_req_handles_avail(int req_size, int off)
493 avail = min_t(int, LDLM_MAXREQSIZE, CFS_PAGE_SIZE - 512) - req_size;
494 avail /= sizeof(struct lustre_handle);
495 avail += LDLM_LOCKREQ_HANDLES - off;
500 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
501 enum req_location loc,
504 int size = req_capsule_msg_size(pill, loc);
505 return ldlm_req_handles_avail(size, off);
508 static inline int ldlm_format_handles_avail(struct obd_import *imp,
509 const struct req_format *fmt,
510 enum req_location loc, int off)
512 int size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
513 return ldlm_req_handles_avail(size, off);
516 /* Cancel lru locks and pack them into the enqueue request. Pack there the given
517 * @count locks in @cancels. */
518 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
519 int version, int opc, int canceloff,
520 struct list_head *cancels, int count)
522 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
523 struct req_capsule *pill = &req->rq_pill;
524 struct ldlm_request *dlm = NULL;
525 int flags, avail, to_free, bufcount, pack = 0;
532 if (exp_connect_cancelset(exp)) {
533 /* Estimate the amount of available space in the request. */
534 bufcount = req_capsule_filled_sizes(pill, RCL_CLIENT);
535 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
537 flags = ns_connect_lru_resize(ns) ?
538 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
539 to_free = !ns_connect_lru_resize(ns) &&
540 opc == LDLM_ENQUEUE ? 1 : 0;
542 /* Cancel lru locks here _only_ if the server supports
543 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
544 * rpc, what will make us slower. */
546 count += ldlm_cancel_lru_local(ns, cancels, to_free,
547 avail - count, 0, flags);
552 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
553 ldlm_request_bufsize(pack, opc));
556 rc = ptlrpc_request_pack(req, version, opc);
558 ldlm_lock_list_put(cancels, l_bl_ast, count);
562 if (exp_connect_cancelset(exp)) {
564 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
566 /* Skip first lock handler in ldlm_request_pack(),
567 * this method will incrment @lock_count according
568 * to the lock handle amount actually written to
570 dlm->lock_count = canceloff;
572 /* Pack into the request @pack lock handles. */
573 ldlm_cli_cancel_list(cancels, pack, req, 0);
574 /* Prepare and send separate cancel rpc for others. */
575 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
577 ldlm_lock_list_put(cancels, l_bl_ast, count);
582 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
583 struct list_head *cancels, int count)
585 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
586 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
589 /* If a request has some specific initialisation it is passed in @reqp,
590 * otherwise it is created in ldlm_cli_enqueue.
592 * Supports sync and async requests, pass @async flag accordingly. If a
593 * request was created in ldlm_cli_enqueue and it is the async request,
594 * pass it to the caller in @reqp. */
595 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
596 struct ldlm_enqueue_info *einfo,
597 const struct ldlm_res_id *res_id,
598 ldlm_policy_data_t *policy, int *flags,
599 void *lvb, __u32 lvb_len, void *lvb_swabber,
600 struct lustre_handle *lockh, int async)
602 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
603 struct ldlm_lock *lock;
604 struct ldlm_request *body;
605 int is_replay = *flags & LDLM_FL_REPLAY;
606 int req_passed_in = 1;
608 struct ptlrpc_request *req;
611 LASSERT(exp != NULL);
613 /* If we're replaying this lock, just check some invariants.
614 * If we're creating a new lock, get everything all setup nice. */
616 lock = ldlm_handle2lock(lockh);
617 LASSERT(lock != NULL);
618 LDLM_DEBUG(lock, "client-side enqueue START");
619 LASSERT(exp == lock->l_conn_export);
621 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
622 einfo->ei_mode, einfo->ei_cb_bl,
623 einfo->ei_cb_cp, einfo->ei_cb_gl,
624 einfo->ei_cbdata, lvb_len);
627 /* for the local lock, add the reference */
628 ldlm_lock_addref_internal(lock, einfo->ei_mode);
629 ldlm_lock2handle(lock, lockh);
630 lock->l_lvb_swabber = lvb_swabber;
631 if (policy != NULL) {
632 /* INODEBITS_INTEROP: If the server does not support
633 * inodebits, we will request a plain lock in the
634 * descriptor (ldlm_lock2desc() below) but use an
635 * inodebits lock internally with both bits set.
637 if (einfo->ei_type == LDLM_IBITS &&
638 !(exp->exp_connect_flags & OBD_CONNECT_IBITS))
639 lock->l_policy_data.l_inodebits.bits =
640 MDS_INODELOCK_LOOKUP |
641 MDS_INODELOCK_UPDATE;
643 lock->l_policy_data = *policy;
646 if (einfo->ei_type == LDLM_EXTENT)
647 lock->l_req_extent = policy->l_extent;
648 LDLM_DEBUG(lock, "client-side enqueue START");
651 /* lock not sent to server yet */
653 if (reqp == NULL || *reqp == NULL) {
654 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
659 failed_lock_cleanup(ns, lock, lockh, einfo->ei_mode);
670 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
672 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
673 DLM_LOCKREQ_OFF, len, sizeof(*body));
676 lock->l_conn_export = exp;
677 lock->l_export = NULL;
678 lock->l_blocking_ast = einfo->ei_cb_bl;
680 /* Dump lock data into the request buffer */
681 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
682 ldlm_lock2desc(lock, &body->lock_desc);
683 body->lock_flags = *flags;
684 body->lock_handle[0] = *lockh;
686 /* Continue as normal. */
687 if (!req_passed_in) {
689 req_capsule_extend(&req->rq_pill,
690 &RQF_LDLM_ENQUEUE_LVB);
691 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
692 RCL_SERVER, lvb_len);
694 ptlrpc_request_set_replen(req);
698 * Liblustre client doesn't get extent locks, except for O_APPEND case
699 * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
700 * [i_size, OBD_OBJECT_EOF] lock is taken.
702 LASSERT(ergo(LIBLUSTRE_CLIENT, einfo->ei_type != LDLM_EXTENT ||
703 policy->l_extent.end == OBD_OBJECT_EOF));
706 LASSERT(reqp != NULL);
710 LDLM_DEBUG(lock, "sending request");
711 rc = ptlrpc_queue_wait(req);
712 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
713 einfo->ei_mode, flags, lvb, lvb_len,
714 lvb_swabber, lockh, rc);
716 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
717 * one reference that we took */
723 if (!req_passed_in && req != NULL) {
724 ptlrpc_req_finished(req);
732 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
735 struct ldlm_resource *res;
738 if (ns_is_client(lock->l_resource->lr_namespace)) {
739 CERROR("Trying to cancel local lock\n");
742 LDLM_DEBUG(lock, "client-side local convert");
744 res = ldlm_lock_convert(lock, new_mode, flags);
746 ldlm_reprocess_all(res);
751 LDLM_DEBUG(lock, "client-side local convert handler END");
756 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
757 * conversion of locks which are on the waiting or converting queue */
758 /* Caller of this code is supposed to take care of lock readers/writers
760 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, __u32 *flags)
762 struct ldlm_request *body;
763 struct ldlm_reply *reply;
764 struct ldlm_lock *lock;
765 struct ldlm_resource *res;
766 struct ptlrpc_request *req;
770 lock = ldlm_handle2lock(lockh);
777 if (lock->l_conn_export == NULL)
778 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
780 LDLM_DEBUG(lock, "client-side convert");
782 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
783 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
790 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
791 body->lock_handle[0] = lock->l_remote_handle;
793 body->lock_desc.l_req_mode = new_mode;
794 body->lock_flags = *flags;
797 ptlrpc_request_set_replen(req);
798 rc = ptlrpc_queue_wait(req);
802 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
804 GOTO(out, rc = -EPROTO);
807 GOTO(out, rc = req->rq_status);
809 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
811 ldlm_reprocess_all(res);
812 /* Go to sleep until the lock is granted. */
813 /* FIXME: or cancelled. */
814 if (lock->l_completion_ast) {
815 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
826 ptlrpc_req_finished(req);
830 /* Cancel locks locally.
832 * LDLM_FL_LOCAL_ONLY if tere is no need in a CANCEL rpc to the server;
833 * LDLM_FL_CANCELING otherwise;
834 * LDLM_FL_BL_AST if there is a need in a separate CANCEL rpc. */
835 static int ldlm_cli_cancel_local(struct ldlm_lock *lock)
837 int rc = LDLM_FL_LOCAL_ONLY;
840 if (lock->l_conn_export) {
843 LDLM_DEBUG(lock, "client-side cancel");
844 /* Set this flag to prevent others from getting new references*/
845 lock_res_and_lock(lock);
846 lock->l_flags |= LDLM_FL_CBPENDING;
847 local_only = (lock->l_flags &
848 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
849 ldlm_cancel_callback(lock);
850 rc = (lock->l_flags & LDLM_FL_BL_AST) ?
851 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
852 unlock_res_and_lock(lock);
855 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
857 rc = LDLM_FL_LOCAL_ONLY;
859 ldlm_lock_cancel(lock);
861 if (ns_is_client(lock->l_resource->lr_namespace)) {
862 LDLM_ERROR(lock, "Trying to cancel local lock");
865 LDLM_DEBUG(lock, "server-side local cancel");
866 ldlm_lock_cancel(lock);
867 ldlm_reprocess_all(lock->l_resource);
868 LDLM_DEBUG(lock, "server-side local cancel handler END");
874 /* Pack @count locks in @head into ldlm_request buffer at the offset @off,
875 of the request @req. */
876 static void ldlm_cancel_pack(struct ptlrpc_request *req,
877 struct list_head *head, int count)
879 struct ldlm_request *dlm;
880 struct ldlm_lock *lock;
884 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
885 LASSERT(dlm != NULL);
887 /* Check the room in the request buffer. */
888 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
889 sizeof(struct ldlm_request);
890 max /= sizeof(struct lustre_handle);
891 max += LDLM_LOCKREQ_HANDLES;
892 LASSERT(max >= dlm->lock_count + count);
894 /* XXX: it would be better to pack lock handles grouped by resource.
895 * so that the server cancel would call filter_lvbo_update() less
897 list_for_each_entry(lock, head, l_bl_ast) {
900 LASSERT(lock->l_conn_export);
901 /* Pack the lock handle to the given request buffer. */
902 LDLM_DEBUG(lock, "packing");
903 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
906 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
910 /* Prepare and send a batched cancel rpc, it will include count lock handles
911 * of locks given in @head. */
912 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
913 int count, int flags)
915 struct ptlrpc_request *req = NULL;
916 struct obd_import *imp;
921 LASSERT(exp != NULL);
924 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
927 free = ldlm_format_handles_avail(class_exp2cliimp(exp),
928 &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
935 imp = class_exp2cliimp(exp);
936 if (imp == NULL || imp->imp_invalid) {
938 "skipping cancel on invalid import %p\n", imp);
942 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
944 GOTO(out, rc = -ENOMEM);
946 bufcount = req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
947 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
948 ldlm_request_bufsize(count, LDLM_CANCEL));
950 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
952 ptlrpc_request_free(req);
955 req->rq_no_resend = 1;
956 req->rq_no_delay = 1;
958 /* XXX FIXME bug 249 */
959 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
960 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
962 ldlm_cancel_pack(req, cancels, count);
964 ptlrpc_request_set_replen(req);
965 if (flags & LDLM_FL_ASYNC) {
966 ptlrpcd_add_req(req);
970 rc = ptlrpc_queue_wait(req);
973 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
974 "out of sync -- not fatal\n",
975 libcfs_nid2str(req->rq_import->
976 imp_connection->c_peer.nid));
978 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
979 req->rq_import_generation == imp->imp_generation) {
980 ptlrpc_req_finished(req);
982 } else if (rc != ELDLM_OK) {
983 CERROR("Got rc %d from cancel RPC: canceling "
991 ptlrpc_req_finished(req);
994 return sent ? sent : rc;
997 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
999 LASSERT(imp != NULL);
1000 return &imp->imp_obd->obd_namespace->ns_pool;
1004 * Update client's obd pool related fields with new SLV and Limit from \a req.
1006 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1008 struct obd_device *obd;
1009 __u64 old_slv, new_slv;
1013 if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1014 !imp_connect_lru_resize(req->rq_import)))
1017 * Do nothing for corner cases.
1023 * In some cases RPC may contain slv and limit zeroed out. This is
1024 * the case when server does not support lru resize feature. This is
1025 * also possible in some recovery cases when server side reqs have no
1026 * ref to obd export and thus access to server side namespace is no
1029 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1030 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1031 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1032 "(SLV: "LPU64", Limit: %u)",
1033 lustre_msg_get_slv(req->rq_repmsg),
1034 lustre_msg_get_limit(req->rq_repmsg));
1038 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1039 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1040 obd = req->rq_import->imp_obd;
1043 * Set new SLV and Limit to obd fields to make accessible for pool
1044 * thread. We do not access obd_namespace and pool directly here
1045 * as there is no reliable way to make sure that they are still
1046 * alive in cleanup time. Evil races are possible which may cause
1047 * oops in that time.
1049 write_lock(&obd->obd_pool_lock);
1050 old_slv = obd->obd_pool_slv;
1051 obd->obd_pool_slv = new_slv;
1052 obd->obd_pool_limit = new_limit;
1053 write_unlock(&obd->obd_pool_lock);
1056 * Check if we need to wakeup pools thread for fast SLV change.
1057 * This is only done when threads period is noticably long like
1060 #if defined(__KERNEL__) && (LDLM_POOLS_THREAD_PERIOD >= 10)
1062 __u64 fast_change = old_slv * LDLM_POOLS_FAST_SLV_CHANGE;
1063 do_div(fast_change, 100);
1066 * Wake up pools thread only if SLV has changed more than
1067 * 50% since last update. In this case we want to react asap.
1068 * Otherwise it is no sense to wake up pools as they are
1069 * re-calculated every LDLM_POOLS_THREAD_PERIOD anyways.
1071 if (old_slv > new_slv && old_slv - new_slv > fast_change)
1072 ldlm_pools_wakeup();
1077 EXPORT_SYMBOL(ldlm_cli_update_pool);
1079 int ldlm_cli_cancel(struct lustre_handle *lockh)
1081 struct obd_export *exp;
1082 int avail, flags, count = 1, rc = 0;
1083 struct ldlm_namespace *ns;
1084 struct ldlm_lock *lock;
1085 CFS_LIST_HEAD(cancels);
1088 /* concurrent cancels on the same handle can happen */
1089 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
1091 LDLM_DEBUG_NOLOCK("lock is already being destroyed\n");
1095 rc = ldlm_cli_cancel_local(lock);
1096 if (rc < 0 || rc == LDLM_FL_LOCAL_ONLY) {
1097 LDLM_LOCK_PUT(lock);
1098 RETURN(rc < 0 ? rc : 0);
1100 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1101 * rpc which goes to canceld portal, so we can cancel other lru locks
1102 * here and send them all as one LDLM_CANCEL rpc. */
1103 LASSERT(list_empty(&lock->l_bl_ast));
1104 list_add(&lock->l_bl_ast, &cancels);
1106 exp = lock->l_conn_export;
1107 if (exp_connect_cancelset(exp)) {
1108 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1113 ns = lock->l_resource->lr_namespace;
1114 flags = ns_connect_lru_resize(ns) ?
1115 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
1116 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1117 LDLM_FL_BL_AST, flags);
1119 ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1123 /* XXX until we will have compound requests and can cut cancels from generic rpc
1124 * we need send cancels with LDLM_FL_BL_AST flag as separate rpc */
1125 static int ldlm_cancel_list(struct list_head *cancels, int count, int flags)
1127 CFS_LIST_HEAD(head);
1128 struct ldlm_lock *lock, *next;
1129 int left = 0, bl_ast = 0, rc;
1132 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1136 if (flags & LDLM_FL_LOCAL_ONLY) {
1137 rc = LDLM_FL_LOCAL_ONLY;
1138 ldlm_lock_cancel(lock);
1140 rc = ldlm_cli_cancel_local(lock);
1142 if (!(flags & LDLM_FL_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1143 LDLM_DEBUG(lock, "Cancel lock separately");
1144 list_del_init(&lock->l_bl_ast);
1145 list_add(&lock->l_bl_ast, &head);
1149 if (rc == LDLM_FL_LOCAL_ONLY) {
1150 /* CANCEL RPC should not be sent to server. */
1151 list_del_init(&lock->l_bl_ast);
1152 LDLM_LOCK_PUT(lock);
1159 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1166 * Callback function for shrink policy. Makes decision whether to keep
1167 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1168 * \a added and number of locks to be preferably canceled \a count.
1170 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1172 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1174 static ldlm_policy_res_t ldlm_cancel_shrink_policy(struct ldlm_namespace *ns,
1175 struct ldlm_lock *lock,
1176 int unused, int added,
1183 * Stop lru processing when we reached passed @count or checked all
1186 if (count && added >= count)
1187 return LDLM_POLICY_KEEP_LOCK;
1189 if (lock->l_resource->lr_type == LDLM_EXTENT) {
1190 struct ldlm_extent *l_extent;
1193 * For all extent locks cost is 1 + number of pages in
1196 l_extent = &lock->l_policy_data.l_extent;
1197 page_nr = (l_extent->end - l_extent->start);
1198 do_div(page_nr, CFS_PAGE_SIZE);
1202 * XXX: In fact this is evil hack, we can't access inode
1203 * here. For doing it right we need somehow to have number
1204 * of covered by lock. This should be fixed later when 10718
1207 if (lock->l_ast_data != NULL) {
1208 struct inode *inode = lock->l_ast_data;
1209 if (page_nr > inode->i_mapping->nrpages)
1210 page_nr = inode->i_mapping->nrpages;
1213 lock_cost = 1 + page_nr;
1216 * For all locks which are not extent ones cost is 1
1222 * Keep all expensive locks in lru for the memory pressure time
1223 * cancel policy. They anyways may be canceled by lru resize
1224 * pplicy if they have not small enough CLV.
1226 return lock_cost > ns->ns_shrink_thumb ?
1227 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1231 * Callback function for lru-resize policy. Makes decision whether to keep
1232 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1233 * \a added and number of locks to be preferably canceled \a count.
1235 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1237 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1239 static ldlm_policy_res_t ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1240 struct ldlm_lock *lock,
1241 int unused, int added,
1244 cfs_time_t cur = cfs_time_current();
1245 struct ldlm_pool *pl = &ns->ns_pool;
1250 * Stop lru processing when we reached passed @count or checked all
1253 if (count && added >= count)
1254 return LDLM_POLICY_KEEP_LOCK;
1256 slv = ldlm_pool_get_slv(pl);
1257 lvf = ldlm_pool_get_lvf(pl);
1258 la = cfs_duration_sec(cfs_time_sub(cur,
1259 lock->l_last_used));
1262 * Stop when slv is not yet come from server or lv is smaller than
1265 lv = lvf * la * unused;
1268 * Inform pool about current CLV to see it via proc.
1270 ldlm_pool_set_clv(pl, lv);
1271 return (slv == 1 || lv < slv) ?
1272 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1276 * Callback function for proc used policy. Makes decision whether to keep
1277 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1278 * \a added and number of locks to be preferably canceled \a count.
1280 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1282 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1284 static ldlm_policy_res_t ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1285 struct ldlm_lock *lock,
1286 int unused, int added,
1290 * Stop lru processing when we reached passed @count or checked all
1293 return (added >= count) ?
1294 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1298 * Callback function for aged policy. Makes decision whether to keep
1299 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1300 * \a added and number of locks to be preferably canceled \a count.
1302 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1304 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1306 static ldlm_policy_res_t ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1307 struct ldlm_lock *lock,
1308 int unused, int added,
1312 * Stop lru processing if young lock is found and we reached passed
1315 return ((added >= count) &&
1316 cfs_time_before(cfs_time_current(),
1317 cfs_time_add(lock->l_last_used,
1319 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1323 * Callback function for default policy. Makes decision whether to keep
1324 * \a lock in LRU for current \a LRU size \a unused, added in current scan
1325 * \a added and number of locks to be preferably canceled \a count.
1327 * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1329 * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1331 static ldlm_policy_res_t ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1332 struct ldlm_lock *lock,
1333 int unused, int added,
1337 * Stop lru processing when we reached passed @count or checked all
1340 return (added >= count) ?
1341 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1344 typedef ldlm_policy_res_t (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *,
1345 struct ldlm_lock *, int,
1348 static ldlm_cancel_lru_policy_t
1349 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, int flags)
1351 if (ns_connect_lru_resize(ns)) {
1352 if (flags & LDLM_CANCEL_SHRINK)
1353 return ldlm_cancel_shrink_policy;
1354 else if (flags & LDLM_CANCEL_LRUR)
1355 return ldlm_cancel_lrur_policy;
1356 else if (flags & LDLM_CANCEL_PASSED)
1357 return ldlm_cancel_passed_policy;
1359 if (flags & LDLM_CANCEL_AGED)
1360 return ldlm_cancel_aged_policy;
1363 return ldlm_cancel_default_policy;
1366 /* - Free space in lru for @count new locks,
1367 * redundant unused locks are canceled locally;
1368 * - also cancel locally unused aged locks;
1369 * - do not cancel more than @max locks;
1370 * - GET the found locks and add them into the @cancels list.
1372 * A client lock can be added to the l_bl_ast list only when it is
1373 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing CANCEL.
1374 * There are the following use cases: ldlm_cancel_resource_local(),
1375 * ldlm_cancel_lru_local() and ldlm_cli_cancel(), which check&set this
1376 * flag properly. As any attempt to cancel a lock rely on this flag,
1377 * l_bl_ast list is accessed later without any special locking.
1379 * Calling policies for enabled lru resize:
1380 * ----------------------------------------
1381 * flags & LDLM_CANCEL_LRUR - use lru resize policy (SLV from server) to
1382 * cancel not more than @count locks;
1384 * flags & LDLM_CANCEL_PASSED - cancel @count number of old locks (located at
1385 * the beginning of lru list);
1387 * flags & LDLM_CANCEL_SHRINK - cancel not more than @count locks according to
1388 * memory pressre policy function;
1390 * flags & LDLM_CANCEL_AGED - cancel alocks according to "aged policy".
1392 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1393 int count, int max, int cancel_flags, int flags)
1395 ldlm_cancel_lru_policy_t pf;
1396 struct ldlm_lock *lock, *next;
1397 int added = 0, unused;
1400 spin_lock(&ns->ns_unused_lock);
1401 unused = ns->ns_nr_unused;
1403 if (!ns_connect_lru_resize(ns))
1404 count += unused - ns->ns_max_unused;
1406 pf = ldlm_cancel_lru_policy(ns, flags);
1407 LASSERT(pf != NULL);
1409 while (!list_empty(&ns->ns_unused_list)) {
1410 /* For any flags, stop scanning if @max is reached. */
1411 if (max && added >= max)
1414 list_for_each_entry_safe(lock, next, &ns->ns_unused_list, l_lru){
1415 /* No locks which got blocking requests. */
1416 LASSERT(!(lock->l_flags & LDLM_FL_BL_AST));
1418 /* Somebody is already doing CANCEL. No need in this
1419 * lock in lru, do not traverse it again. */
1420 if (!(lock->l_flags & LDLM_FL_CANCELING))
1423 ldlm_lock_remove_from_lru_nolock(lock);
1425 if (&lock->l_lru == &ns->ns_unused_list)
1428 /* Pass the lock through the policy filter and see if it
1429 * should stay in lru.
1431 * Even for shrinker policy we stop scanning if
1432 * we find a lock that should stay in the cache.
1433 * We should take into account lock age anyway
1434 * as new lock even if it is small of weight is
1435 * valuable resource.
1437 * That is, for shrinker policy we drop only
1438 * old locks, but additionally chose them by
1439 * their weight. Big extent locks will stay in
1441 if (pf(ns, lock, unused, added, count) == LDLM_POLICY_KEEP_LOCK)
1444 LDLM_LOCK_GET(lock); /* dropped by bl thread */
1445 spin_unlock(&ns->ns_unused_lock);
1447 lock_res_and_lock(lock);
1448 /* Check flags again under the lock. */
1449 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1450 (ldlm_lock_remove_from_lru(lock) == 0)) {
1451 /* other thread is removing lock from lru or
1452 * somebody is already doing CANCEL or
1453 * there is a blocking request which will send
1454 * cancel by itseft or the lock is matched
1455 * is already not unused. */
1456 unlock_res_and_lock(lock);
1457 LDLM_LOCK_PUT(lock);
1458 spin_lock(&ns->ns_unused_lock);
1461 LASSERT(!lock->l_readers && !lock->l_writers);
1463 /* If we have chosen to cancel this lock voluntarily, we
1464 * better send cancel notification to server, so that it
1465 * frees appropriate state. This might lead to a race
1466 * where while we are doing cancel here, server is also
1467 * silently cancelling this lock. */
1468 lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
1470 /* Setting the CBPENDING flag is a little misleading,
1471 * but prevents an important race; namely, once
1472 * CBPENDING is set, the lock can accumulate no more
1473 * readers/writers. Since readers and writers are
1474 * already zero here, ldlm_lock_decref() won't see
1475 * this flag and call l_blocking_ast */
1476 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1478 /* We can't re-add to l_lru as it confuses the
1479 * refcounting in ldlm_lock_remove_from_lru() if an AST
1480 * arrives after we drop ns_lock below. We use l_bl_ast
1481 * and can't use l_pending_chain as it is used both on
1482 * server and client nevertheless bug 5666 says it is
1483 * used only on server */
1484 LASSERT(list_empty(&lock->l_bl_ast));
1485 list_add(&lock->l_bl_ast, cancels);
1486 unlock_res_and_lock(lock);
1487 spin_lock(&ns->ns_unused_lock);
1491 spin_unlock(&ns->ns_unused_lock);
1492 RETURN(ldlm_cancel_list(cancels, added, cancel_flags));
1495 /* Returns number of locks which could be canceled next time when
1496 * ldlm_cancel_lru() is called. Used from locks pool shrinker. */
1497 int ldlm_cancel_lru_estimate(struct ldlm_namespace *ns,
1498 int count, int max, int flags)
1500 ldlm_cancel_lru_policy_t pf;
1501 struct ldlm_lock *lock;
1502 int added = 0, unused;
1505 pf = ldlm_cancel_lru_policy(ns, flags);
1506 LASSERT(pf != NULL);
1507 spin_lock(&ns->ns_unused_lock);
1508 unused = ns->ns_nr_unused;
1510 list_for_each_entry(lock, &ns->ns_unused_list, l_lru) {
1511 /* For any flags, stop scanning if @max is reached. */
1512 if (max && added >= max)
1515 /* Somebody is already doing CANCEL or there is a
1516 * blocking request will send cancel. Let's not count
1518 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1519 (lock->l_flags & LDLM_FL_BL_AST))
1522 /* Pass the lock through the policy filter and see if it
1523 * should stay in lru. */
1524 if (pf(ns, lock, unused, added, count) == LDLM_POLICY_KEEP_LOCK)
1530 spin_unlock(&ns->ns_unused_lock);
1534 /* when called with LDLM_ASYNC the blocking callback will be handled
1535 * in a thread and this function will return after the thread has been
1536 * asked to call the callback. when called with LDLM_SYNC the blocking
1537 * callback will be performed in this function. */
1538 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr, ldlm_sync_t sync,
1541 CFS_LIST_HEAD(cancels);
1546 sync = LDLM_SYNC; /* force to be sync in user space */
1548 count = ldlm_cancel_lru_local(ns, &cancels, nr, 0, 0, flags);
1549 if (sync == LDLM_ASYNC) {
1550 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count);
1555 /* If an error occured in ASYNC mode, or
1556 * this is SYNC mode, cancel the list. */
1557 ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1561 /* Find and cancel locally unused locks found on resource, matched to the
1562 * given policy, mode. GET the found locks and add them into the @cancels
1564 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1565 struct list_head *cancels,
1566 ldlm_policy_data_t *policy,
1567 ldlm_mode_t mode, int lock_flags,
1568 int cancel_flags, void *opaque)
1570 struct ldlm_lock *lock;
1575 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1576 if (opaque != NULL && lock->l_ast_data != opaque) {
1577 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1578 lock->l_ast_data, opaque);
1583 if (lock->l_readers || lock->l_writers) {
1584 if (cancel_flags & LDLM_FL_WARN) {
1585 LDLM_ERROR(lock, "lock in use");
1591 /* If somebody is already doing CANCEL, or blocking ast came,
1592 * skip this lock. */
1593 if (lock->l_flags & LDLM_FL_BL_AST ||
1594 lock->l_flags & LDLM_FL_CANCELING)
1597 if (lockmode_compat(lock->l_granted_mode, mode))
1600 /* If policy is given and this is IBITS lock, add to list only
1601 * those locks that match by policy. */
1602 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1603 !(lock->l_policy_data.l_inodebits.bits &
1604 policy->l_inodebits.bits))
1607 /* See CBPENDING comment in ldlm_cancel_lru */
1608 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1611 LASSERT(list_empty(&lock->l_bl_ast));
1612 list_add(&lock->l_bl_ast, cancels);
1613 LDLM_LOCK_GET(lock);
1618 RETURN(ldlm_cancel_list(cancels, count, cancel_flags));
1621 /* If @req is NULL, send CANCEL request to server with handles of locks
1622 * in the @cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1623 * separately per lock.
1624 * If @req is not NULL, put handles of locks in @cancels into the request
1625 * buffer at the offset @off.
1626 * Destroy @cancels at the end. */
1627 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1628 struct ptlrpc_request *req, int flags)
1630 struct ldlm_lock *lock;
1634 if (list_empty(cancels) || count == 0)
1637 /* XXX: requests (both batched and not) could be sent in parallel.
1638 * Usually it is enough to have just 1 RPC, but it is possible that
1639 * there are to many locks to be cancelled in LRU or on a resource.
1640 * It would also speed up the case when the server does not support
1643 LASSERT(!list_empty(cancels));
1644 lock = list_entry(cancels->next, struct ldlm_lock, l_bl_ast);
1645 LASSERT(lock->l_conn_export);
1647 if (exp_connect_cancelset(lock->l_conn_export)) {
1650 ldlm_cancel_pack(req, cancels, count);
1652 res = ldlm_cli_cancel_req(lock->l_conn_export,
1656 res = ldlm_cli_cancel_req(lock->l_conn_export,
1661 CERROR("ldlm_cli_cancel_list: %d\n", res);
1666 ldlm_lock_list_put(cancels, l_bl_ast, res);
1668 LASSERT(count == 0);
1672 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1673 const struct ldlm_res_id *res_id,
1674 ldlm_policy_data_t *policy,
1675 ldlm_mode_t mode, int flags, void *opaque)
1677 struct ldlm_resource *res;
1678 CFS_LIST_HEAD(cancels);
1683 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1685 /* This is not a problem. */
1686 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1690 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1692 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1694 CERROR("ldlm_cli_cancel_unused_resource: %d\n", rc);
1696 ldlm_resource_putref(res);
1700 static inline int have_no_nsresource(struct ldlm_namespace *ns)
1702 int no_resource = 0;
1704 spin_lock(&ns->ns_hash_lock);
1705 if (ns->ns_resources == 0)
1707 spin_unlock(&ns->ns_hash_lock);
1709 RETURN(no_resource);
1712 /* Cancel all locks on a namespace (or a specific resource, if given)
1713 * that have 0 readers/writers.
1715 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1716 * to notify the server. */
1717 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
1718 const struct ldlm_res_id *res_id,
1719 int flags, void *opaque)
1728 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
1732 spin_lock(&ns->ns_hash_lock);
1733 for (i = 0; i < RES_HASH_SIZE; i++) {
1734 struct list_head *tmp;
1735 tmp = ns->ns_hash[i].next;
1736 while (tmp != &(ns->ns_hash[i])) {
1737 struct ldlm_resource *res;
1740 res = list_entry(tmp, struct ldlm_resource, lr_hash);
1741 ldlm_resource_getref(res);
1742 spin_unlock(&ns->ns_hash_lock);
1744 rc = ldlm_cli_cancel_unused_resource(ns, &res->lr_name,
1749 CERROR("ldlm_cli_cancel_unused ("LPU64"): %d\n",
1750 res->lr_name.name[0], rc);
1752 spin_lock(&ns->ns_hash_lock);
1754 ldlm_resource_putref_locked(res);
1757 spin_unlock(&ns->ns_hash_lock);
1762 /* join/split resource locks to/from lru list */
1763 int ldlm_cli_join_lru(struct ldlm_namespace *ns,
1764 const struct ldlm_res_id *res_id, int join)
1766 struct ldlm_resource *res;
1767 struct ldlm_lock *lock, *n;
1771 LASSERT(ns_is_client(ns));
1773 res = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
1776 LASSERT(res->lr_type == LDLM_EXTENT);
1782 list_for_each_entry_safe (lock, n, &res->lr_granted, l_res_link) {
1783 if (list_empty(&lock->l_lru) &&
1784 !lock->l_readers && !lock->l_writers &&
1785 !(lock->l_flags & LDLM_FL_LOCAL) &&
1786 !(lock->l_flags & LDLM_FL_CBPENDING) &&
1787 !(lock->l_flags & LDLM_FL_BL_AST)) {
1788 ldlm_lock_add_to_lru(lock);
1789 lock->l_flags &= ~LDLM_FL_NO_LRU;
1790 LDLM_DEBUG(lock, "join lock to lru");
1796 spin_lock(&ns->ns_unused_lock);
1797 list_for_each_entry_safe (lock, n, &ns->ns_unused_list, l_lru) {
1798 if (lock->l_resource == res) {
1799 ldlm_lock_remove_from_lru_nolock(lock);
1800 lock->l_flags |= LDLM_FL_NO_LRU;
1801 LDLM_DEBUG(lock, "split lock from lru");
1805 spin_unlock(&ns->ns_unused_lock);
1808 ldlm_resource_putref(res);
1812 /* Lock iterators. */
1814 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1817 struct list_head *tmp, *next;
1818 struct ldlm_lock *lock;
1819 int rc = LDLM_ITER_CONTINUE;
1824 RETURN(LDLM_ITER_CONTINUE);
1827 list_for_each_safe(tmp, next, &res->lr_granted) {
1828 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1830 if (iter(lock, closure) == LDLM_ITER_STOP)
1831 GOTO(out, rc = LDLM_ITER_STOP);
1834 list_for_each_safe(tmp, next, &res->lr_converting) {
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_waiting) {
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);
1852 struct iter_helper_data {
1853 ldlm_iterator_t iter;
1857 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1859 struct iter_helper_data *helper = closure;
1860 return helper->iter(lock, helper->closure);
1863 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
1865 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
1868 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
1871 struct iter_helper_data helper = { iter: iter, closure: closure };
1872 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
1875 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
1876 ldlm_res_iterator_t iter, void *closure)
1878 int i, rc = LDLM_ITER_CONTINUE;
1879 struct ldlm_resource *res;
1880 struct list_head *tmp;
1883 spin_lock(&ns->ns_hash_lock);
1884 for (i = 0; i < RES_HASH_SIZE; i++) {
1885 tmp = ns->ns_hash[i].next;
1886 while (tmp != &(ns->ns_hash[i])) {
1887 res = list_entry(tmp, struct ldlm_resource, lr_hash);
1888 ldlm_resource_getref(res);
1889 spin_unlock(&ns->ns_hash_lock);
1891 rc = iter(res, closure);
1893 spin_lock(&ns->ns_hash_lock);
1895 ldlm_resource_putref_locked(res);
1896 if (rc == LDLM_ITER_STOP)
1901 spin_unlock(&ns->ns_hash_lock);
1905 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
1906 void ldlm_resource_iterate(struct ldlm_namespace *ns,
1907 const struct ldlm_res_id *res_id,
1908 ldlm_iterator_t iter, void *data)
1910 struct ldlm_resource *res;
1914 CERROR("must pass in namespace\n");
1918 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1924 ldlm_resource_foreach(res, iter, data);
1925 ldlm_resource_putref(res);
1931 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
1933 struct list_head *list = closure;
1935 /* we use l_pending_chain here, because it's unused on clients. */
1936 LASSERTF(list_empty(&lock->l_pending_chain),"lock %p next %p prev %p\n",
1937 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
1938 /* bug 9573: don't replay locks left after eviction */
1939 if (!(lock->l_flags & LDLM_FL_FAILED))
1940 list_add(&lock->l_pending_chain, list);
1941 return LDLM_ITER_CONTINUE;
1944 static int replay_lock_interpret(struct ptlrpc_request *req,
1945 struct ldlm_async_args *aa, int rc)
1947 struct ldlm_lock *lock;
1948 struct ldlm_reply *reply;
1951 atomic_dec(&req->rq_import->imp_replay_inflight);
1956 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1958 GOTO(out, rc = -EPROTO);
1960 lock = ldlm_handle2lock(&aa->lock_handle);
1962 CERROR("received replay ack for unknown local cookie "LPX64
1963 " remote cookie "LPX64 " from server %s id %s\n",
1964 aa->lock_handle.cookie, reply->lock_handle.cookie,
1965 req->rq_export->exp_client_uuid.uuid,
1966 libcfs_id2str(req->rq_peer));
1967 GOTO(out, rc = -ESTALE);
1970 lock->l_remote_handle = reply->lock_handle;
1971 LDLM_DEBUG(lock, "replayed lock:");
1972 ptlrpc_import_recovery_state_machine(req->rq_import);
1973 LDLM_LOCK_PUT(lock);
1976 ptlrpc_connect_import(req->rq_import, NULL);
1982 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
1984 struct ptlrpc_request *req;
1985 struct ldlm_async_args *aa;
1986 struct ldlm_request *body;
1991 /* Bug 11974: Do not replay a lock which is actively being canceled */
1992 if (lock->l_flags & LDLM_FL_CANCELING) {
1993 LDLM_DEBUG(lock, "Not replaying canceled lock:");
1997 /* If this is reply-less callback lock, we cannot replay it, since
1998 * server might have long dropped it, but notification of that event was
1999 * lost by network. (and server granted conflicting lock already) */
2000 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
2001 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2002 ldlm_lock_cancel(lock);
2006 * If granted mode matches the requested mode, this lock is granted.
2008 * If they differ, but we have a granted mode, then we were granted
2009 * one mode and now want another: ergo, converting.
2011 * If we haven't been granted anything and are on a resource list,
2012 * then we're blocked/waiting.
2014 * If we haven't been granted anything and we're NOT on a resource list,
2015 * then we haven't got a reply yet and don't have a known disposition.
2016 * This happens whenever a lock enqueue is the request that triggers
2019 if (lock->l_granted_mode == lock->l_req_mode)
2020 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2021 else if (lock->l_granted_mode)
2022 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2023 else if (!list_empty(&lock->l_res_link))
2024 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2026 flags = LDLM_FL_REPLAY;
2028 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2029 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2033 /* We're part of recovery, so don't wait for it. */
2034 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2036 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2037 ldlm_lock2desc(lock, &body->lock_desc);
2038 body->lock_flags = flags;
2040 ldlm_lock2handle(lock, &body->lock_handle[0]);
2041 if (lock->l_lvb_len != 0) {
2042 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2043 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2046 ptlrpc_request_set_replen(req);
2047 /* notify the server we've replayed all requests.
2048 * also, we mark the request to be put on a dedicated
2049 * queue to be processed after all request replayes.
2051 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2053 LDLM_DEBUG(lock, "replaying lock:");
2055 atomic_inc(&req->rq_import->imp_replay_inflight);
2056 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2057 aa = (struct ldlm_async_args *)&req->rq_async_args;
2058 aa->lock_handle = body->lock_handle[0];
2059 req->rq_interpret_reply = replay_lock_interpret;
2060 ptlrpcd_add_req(req);
2065 int ldlm_replay_locks(struct obd_import *imp)
2067 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2068 CFS_LIST_HEAD(list);
2069 struct ldlm_lock *lock, *next;
2074 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2076 /* ensure this doesn't fall to 0 before all have been queued */
2077 atomic_inc(&imp->imp_replay_inflight);
2079 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2081 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2082 list_del_init(&lock->l_pending_chain);
2084 continue; /* or try to do the rest? */
2085 rc = replay_one_lock(imp, lock);
2088 atomic_dec(&imp->imp_replay_inflight);