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 %lu, %lus ago); "
65 "not entering recovery in server code, just going "
66 "back to sleep", lock->l_enqueued_time.tv_sec,
67 CURRENT_SECONDS - lock->l_enqueued_time.tv_sec);
68 if (cfs_time_after(cfs_time_current(), next_dump)) {
69 last_dump = next_dump;
70 next_dump = cfs_time_shift(300);
71 ldlm_namespace_dump(D_DLMTRACE,
72 lock->l_resource->lr_namespace);
74 libcfs_debug_dumplog();
79 obd = lock->l_conn_export->exp_obd;
80 imp = obd->u.cli.cl_import;
81 ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
82 LDLM_ERROR(lock, "lock timed out (enqueued at %lu, %lus ago), entering "
83 "recovery for %s@%s", lock->l_enqueued_time.tv_sec,
84 CURRENT_SECONDS - lock->l_enqueued_time.tv_sec,
85 obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
90 static int is_granted_or_cancelled(struct ldlm_lock *lock)
94 lock_res_and_lock(lock);
95 if (((lock->l_req_mode == lock->l_granted_mode) &&
96 !(lock->l_flags & LDLM_FL_CP_REQD)) ||
97 (lock->l_flags & LDLM_FL_FAILED))
99 unlock_res_and_lock(lock);
104 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
106 /* XXX ALLOCATE - 160 bytes */
107 struct lock_wait_data lwd;
108 struct obd_device *obd;
109 struct obd_import *imp = NULL;
110 struct l_wait_info lwi;
114 if (flags == LDLM_FL_WAIT_NOREPROC) {
115 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
119 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
120 LDLM_FL_BLOCK_CONV))) {
121 cfs_waitq_signal(&lock->l_waitq);
125 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
127 ldlm_lock_dump(D_OTHER, lock, 0);
128 ldlm_reprocess_all(lock->l_resource);
132 obd = class_exp2obd(lock->l_conn_export);
134 /* if this is a local lock, then there is no import */
136 imp = obd->u.cli.cl_import;
140 if (lock->l_flags & LDLM_FL_NO_TIMEOUT) {
141 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
142 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
144 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
145 ldlm_expired_completion_wait,
146 interrupted_completion_wait, &lwd);
150 spin_lock(&imp->imp_lock);
151 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
152 spin_unlock(&imp->imp_lock);
155 /* Go to sleep until the lock is granted or cancelled. */
156 rc = l_wait_event(lock->l_waitq, is_granted_or_cancelled(lock), &lwi);
158 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
159 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
164 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
169 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
174 * ->l_blocking_ast() callback for LDLM locks acquired by server-side OBDs.
176 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
177 void *data, int flag)
182 if (flag == LDLM_CB_CANCELING) {
183 /* Don't need to do anything here. */
187 lock_res_and_lock(lock);
188 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
189 * that ldlm_blocking_ast is called just before intent_policy method
190 * takes the ns_lock, then by the time we get the lock, we might not
191 * be the correct blocking function anymore. So check, and return
193 if (lock->l_blocking_ast != ldlm_blocking_ast) {
194 unlock_res_and_lock(lock);
198 lock->l_flags |= LDLM_FL_CBPENDING;
199 do_ast = (!lock->l_readers && !lock->l_writers);
200 unlock_res_and_lock(lock);
203 struct lustre_handle lockh;
206 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
207 ldlm_lock2handle(lock, &lockh);
208 rc = ldlm_cli_cancel(&lockh);
210 CERROR("ldlm_cli_cancel: %d\n", rc);
212 LDLM_DEBUG(lock, "Lock still has references, will be "
219 * ->l_glimpse_ast() for DLM extent locks acquired on the server-side. See
220 * comment in filter_intent_policy() on why you may need this.
222 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
225 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for
226 * that is rather subtle: with OST-side locking, it may so happen that
227 * _all_ extent locks are held by the OST. If client wants to obtain
228 * current file size it calls ll{,u}_glimpse_size(), and (as locks are
229 * on the server), dummy glimpse callback fires and does
230 * nothing. Client still receives correct file size due to the
231 * following fragment in filter_intent_policy():
233 * rc = l->l_glimpse_ast(l, NULL); // this will update the LVB
234 * if (rc != 0 && res->lr_namespace->ns_lvbo &&
235 * res->lr_namespace->ns_lvbo->lvbo_update) {
236 * res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
239 * that is, after glimpse_ast() fails, filter_lvbo_update() runs, and
240 * returns correct file size to the client.
242 return -ELDLM_NO_LOCK_DATA;
245 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
246 const struct ldlm_res_id *res_id,
247 ldlm_type_t type, ldlm_policy_data_t *policy,
248 ldlm_mode_t mode, int *flags,
249 ldlm_blocking_callback blocking,
250 ldlm_completion_callback completion,
251 ldlm_glimpse_callback glimpse,
252 void *data, __u32 lvb_len, void *lvb_swabber,
253 struct lustre_handle *lockh)
255 struct ldlm_lock *lock;
259 LASSERT(!(*flags & LDLM_FL_REPLAY));
260 if (unlikely(ns_is_client(ns))) {
261 CERROR("Trying to enqueue local lock in a shadow namespace\n");
265 lock = ldlm_lock_create(ns, res_id, type, mode, blocking,
266 completion, glimpse, data, lvb_len);
268 GOTO(out_nolock, err = -ENOMEM);
269 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
271 ldlm_lock_addref_internal(lock, mode);
272 ldlm_lock2handle(lock, lockh);
273 lock_res_and_lock(lock);
274 lock->l_flags |= LDLM_FL_LOCAL;
275 if (*flags & LDLM_FL_ATOMIC_CB)
276 lock->l_flags |= LDLM_FL_ATOMIC_CB;
277 lock->l_lvb_swabber = lvb_swabber;
278 unlock_res_and_lock(lock);
280 lock->l_policy_data = *policy;
281 if (type == LDLM_EXTENT)
282 lock->l_req_extent = policy->l_extent;
284 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
285 if (unlikely(err != ELDLM_OK))
289 *policy = lock->l_policy_data;
291 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
294 if (lock->l_completion_ast)
295 lock->l_completion_ast(lock, *flags, NULL);
297 LDLM_DEBUG(lock, "client-side local enqueue END");
305 static void failed_lock_cleanup(struct ldlm_namespace *ns,
306 struct ldlm_lock *lock,
307 struct lustre_handle *lockh, int mode)
309 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
310 lock_res_and_lock(lock);
311 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
312 unlock_res_and_lock(lock);
313 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
315 ldlm_lock_decref_and_cancel(lockh, mode);
317 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
318 * from llite/file.c/ll_file_flock(). */
319 if (lock->l_resource->lr_type == LDLM_FLOCK) {
320 ldlm_lock_destroy(lock);
324 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
325 ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
326 int *flags, void *lvb, __u32 lvb_len,
327 void *lvb_swabber, struct lustre_handle *lockh,int rc)
329 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
330 int is_replay = *flags & LDLM_FL_REPLAY;
331 struct ldlm_lock *lock;
332 struct ldlm_reply *reply;
333 int cleanup_phase = 1;
336 lock = ldlm_handle2lock(lockh);
337 /* ldlm_cli_enqueue is holding a reference on this lock. */
339 LASSERT(type == LDLM_FLOCK);
343 if (rc != ELDLM_OK) {
345 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
346 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
347 if (rc == ELDLM_LOCK_ABORTED) {
348 /* Before we return, swab the reply */
349 reply = req_capsule_server_get(&req->rq_pill,
354 struct ost_lvb *tmplvb;
356 req_capsule_set_size(&req->rq_pill,
357 &RMF_DLM_LVB, RCL_SERVER,
359 tmplvb = req_capsule_server_swab_get(&req->rq_pill,
363 GOTO(cleanup, rc = -EPROTO);
365 memcpy(lvb, tmplvb, lvb_len);
371 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
373 GOTO(cleanup, rc = -EPROTO);
375 /* lock enqueued on the server */
378 lock_res_and_lock(lock);
379 lock->l_remote_handle = reply->lock_handle;
380 *flags = reply->lock_flags;
381 lock->l_flags |= reply->lock_flags & LDLM_INHERIT_FLAGS;
382 /* move NO_TIMEOUT flag to the lock to force ldlm_lock_match()
383 * to wait with no timeout as well */
384 lock->l_flags |= reply->lock_flags & LDLM_FL_NO_TIMEOUT;
385 unlock_res_and_lock(lock);
387 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
388 lock, reply->lock_handle.cookie, *flags);
390 /* If enqueue returned a blocked lock but the completion handler has
391 * already run, then it fixed up the resource and we don't need to do it
393 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
394 int newmode = reply->lock_desc.l_req_mode;
396 if (newmode && newmode != lock->l_req_mode) {
397 LDLM_DEBUG(lock, "server returned different mode %s",
398 ldlm_lockname[newmode]);
399 lock->l_req_mode = newmode;
402 if (memcmp(reply->lock_desc.l_resource.lr_name.name,
403 lock->l_resource->lr_name.name,
404 sizeof(struct ldlm_res_id))) {
405 CDEBUG(D_INFO, "remote intent success, locking "
406 "(%ld,%ld,%ld) instead of "
408 (long)reply->lock_desc.l_resource.lr_name.name[0],
409 (long)reply->lock_desc.l_resource.lr_name.name[1],
410 (long)reply->lock_desc.l_resource.lr_name.name[2],
411 (long)lock->l_resource->lr_name.name[0],
412 (long)lock->l_resource->lr_name.name[1],
413 (long)lock->l_resource->lr_name.name[2]);
415 ldlm_lock_change_resource(ns, lock,
416 &reply->lock_desc.l_resource.lr_name);
417 if (lock->l_resource == NULL) {
419 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(struct obd_export *exp,
490 int *size, int bufcount,
493 int avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512);
494 int old_size = size[bufoff];
496 size[bufoff] = sizeof(struct ldlm_request);
497 avail -= lustre_msg_size(class_exp2cliimp(exp)->imp_msg_magic,
499 avail /= sizeof(struct lustre_handle);
500 avail += LDLM_LOCKREQ_HANDLES - off;
501 size[bufoff] = old_size;
506 static inline int ldlm_cancel_handles_avail(struct obd_export *exp)
508 int size[2] = { sizeof(struct ptlrpc_body),
509 sizeof(struct ldlm_request) };
510 return ldlm_req_handles_avail(exp, size, 2, DLM_LOCKREQ_OFF, 0);
513 /* Cancel lru locks and pack them into the enqueue request. Pack there the given
514 * @count locks in @cancels. */
515 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
516 int version, int opc, int canceloff,
517 struct list_head *cancels, int count)
519 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
520 struct req_capsule *pill = &req->rq_pill;
521 struct ldlm_request *dlm = NULL;
522 int flags, avail, to_free, bufcount, pack = 0;
527 LASSERT(cancels != NULL);
529 if (exp_connect_cancelset(exp)) {
530 /* Estimate the amount of available space in the request. */
531 bufcount = req_capsule_filled_sizes(pill, RCL_CLIENT);
532 avail = ldlm_req_handles_avail(exp, pill->rc_area[RCL_CLIENT],
533 bufcount, bufcount - 1, canceloff);
534 flags = ns_connect_lru_resize(ns) ?
535 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
536 to_free = !ns_connect_lru_resize(ns) &&
537 opc == LDLM_ENQUEUE ? 1 : 0;
539 /* Cancel lru locks here _only_ if the server supports
540 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
541 * rpc, what will make us slower. */
543 count += ldlm_cancel_lru_local(ns, cancels, to_free,
544 avail - count, 0, flags);
549 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
550 ldlm_request_bufsize(count, opc));
553 rc = ptlrpc_request_pack(req, version, opc);
555 ldlm_lock_list_put(cancels, l_bl_ast, count);
559 if (exp_connect_cancelset(exp)) {
561 dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
563 /* Skip first lock handler in ldlm_request_pack(),
564 * this method will incrment @lock_count according
565 * to the lock handle amount actually written to
567 dlm->lock_count = canceloff;
569 /* Pack into the request @pack lock handles. */
570 ldlm_cli_cancel_list(cancels, pack, req, 0);
571 /* Prepare and send separate cancel rpc for others. */
572 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
574 ldlm_lock_list_put(cancels, l_bl_ast, count);
579 int ldlm_prep_enqueue_req(struct obd_export *exp,
580 struct ptlrpc_request *req,
581 struct list_head *cancels,
584 return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
585 LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
588 /* If a request has some specific initialisation it is passed in @reqp,
589 * otherwise it is created in ldlm_cli_enqueue.
591 * Supports sync and async requests, pass @async flag accordingly. If a
592 * request was created in ldlm_cli_enqueue and it is the async request,
593 * pass it to the caller in @reqp. */
594 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
595 struct ldlm_enqueue_info *einfo,
596 const struct ldlm_res_id *res_id,
597 ldlm_policy_data_t *policy, int *flags,
598 void *lvb, __u32 lvb_len, void *lvb_swabber,
599 struct lustre_handle *lockh, int async)
601 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
602 struct ldlm_lock *lock;
603 struct ldlm_request *body;
604 int is_replay = *flags & LDLM_FL_REPLAY;
605 int req_passed_in = 1;
607 struct ptlrpc_request *req;
610 LASSERT(exp != NULL);
612 /* If we're replaying this lock, just check some invariants.
613 * If we're creating a new lock, get everything all setup nice. */
615 lock = ldlm_handle2lock(lockh);
616 LASSERT(lock != NULL);
617 LDLM_DEBUG(lock, "client-side enqueue START");
618 LASSERT(exp == lock->l_conn_export);
620 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
621 einfo->ei_mode, einfo->ei_cb_bl,
622 einfo->ei_cb_cp, einfo->ei_cb_gl,
623 einfo->ei_cbdata, lvb_len);
626 /* for the local lock, add the reference */
627 ldlm_lock_addref_internal(lock, einfo->ei_mode);
628 ldlm_lock2handle(lock, lockh);
629 lock->l_lvb_swabber = lvb_swabber;
630 if (policy != NULL) {
631 /* INODEBITS_INTEROP: If the server does not support
632 * inodebits, we will request a plain lock in the
633 * descriptor (ldlm_lock2desc() below) but use an
634 * inodebits lock internally with both bits set.
636 if (einfo->ei_type == LDLM_IBITS &&
637 !(exp->exp_connect_flags & OBD_CONNECT_IBITS))
638 lock->l_policy_data.l_inodebits.bits =
639 MDS_INODELOCK_LOOKUP |
640 MDS_INODELOCK_UPDATE;
642 lock->l_policy_data = *policy;
645 if (einfo->ei_type == LDLM_EXTENT)
646 lock->l_req_extent = policy->l_extent;
647 LDLM_DEBUG(lock, "client-side enqueue START");
650 /* lock not sent to server yet */
652 if (reqp == NULL || *reqp == NULL) {
653 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
658 failed_lock_cleanup(ns, lock, lockh, einfo->ei_mode);
669 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
671 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
672 DLM_LOCKREQ_OFF, len, sizeof(*body));
675 lock->l_conn_export = exp;
676 lock->l_export = NULL;
677 lock->l_blocking_ast = einfo->ei_cb_bl;
679 /* Dump lock data into the request buffer */
680 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
681 ldlm_lock2desc(lock, &body->lock_desc);
682 body->lock_flags = *flags;
683 body->lock_handle[0] = *lockh;
685 /* Continue as normal. */
686 if (!req_passed_in) {
688 req_capsule_extend(&req->rq_pill,
689 &RQF_LDLM_ENQUEUE_LVB);
690 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
691 RCL_SERVER, lvb_len);
693 ptlrpc_request_set_replen(req);
697 * Liblustre client doesn't get extent locks, except for O_APPEND case
698 * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
699 * [i_size, OBD_OBJECT_EOF] lock is taken.
701 LASSERT(ergo(LIBLUSTRE_CLIENT, einfo->ei_type != LDLM_EXTENT ||
702 policy->l_extent.end == OBD_OBJECT_EOF));
705 LASSERT(reqp != NULL);
709 LDLM_DEBUG(lock, "sending request");
710 rc = ptlrpc_queue_wait(req);
711 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
712 einfo->ei_mode, flags, lvb, lvb_len,
713 lvb_swabber, lockh, rc);
715 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
716 * one reference that we took */
722 if (!req_passed_in && req != NULL) {
723 ptlrpc_req_finished(req);
731 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
734 struct ldlm_resource *res;
737 if (ns_is_client(lock->l_resource->lr_namespace)) {
738 CERROR("Trying to cancel local lock\n");
741 LDLM_DEBUG(lock, "client-side local convert");
743 res = ldlm_lock_convert(lock, new_mode, flags);
745 ldlm_reprocess_all(res);
750 LDLM_DEBUG(lock, "client-side local convert handler END");
755 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
756 * conversion of locks which are on the waiting or converting queue */
757 /* Caller of this code is supposed to take care of lock readers/writers
759 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, __u32 *flags)
761 struct ldlm_request *body;
762 struct ldlm_reply *reply;
763 struct ldlm_lock *lock;
764 struct ldlm_resource *res;
765 struct ptlrpc_request *req;
769 lock = ldlm_handle2lock(lockh);
776 if (lock->l_conn_export == NULL)
777 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
779 LDLM_DEBUG(lock, "client-side convert");
781 req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
782 &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
789 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
790 body->lock_handle[0] = lock->l_remote_handle;
792 body->lock_desc.l_req_mode = new_mode;
793 body->lock_flags = *flags;
796 ptlrpc_request_set_replen(req);
797 rc = ptlrpc_queue_wait(req);
801 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
803 GOTO(out, rc = -EPROTO);
806 GOTO(out, rc = req->rq_status);
808 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
810 ldlm_reprocess_all(res);
811 /* Go to sleep until the lock is granted. */
812 /* FIXME: or cancelled. */
813 if (lock->l_completion_ast) {
814 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
825 ptlrpc_req_finished(req);
829 /* Cancel locks locally.
831 * LDLM_FL_LOCAL_ONLY if tere is no need in a CANCEL rpc to the server;
832 * LDLM_FL_CANCELING otherwise;
833 * LDLM_FL_BL_AST if there is a need in a separate CANCEL rpc. */
834 static int ldlm_cli_cancel_local(struct ldlm_lock *lock)
836 int rc = LDLM_FL_LOCAL_ONLY;
839 if (lock->l_conn_export) {
842 LDLM_DEBUG(lock, "client-side cancel");
843 /* Set this flag to prevent others from getting new references*/
844 lock_res_and_lock(lock);
845 lock->l_flags |= LDLM_FL_CBPENDING;
846 local_only = (lock->l_flags &
847 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
848 ldlm_cancel_callback(lock);
849 rc = (lock->l_flags & LDLM_FL_BL_AST) ?
850 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
851 unlock_res_and_lock(lock);
854 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
856 rc = LDLM_FL_LOCAL_ONLY;
858 ldlm_lock_cancel(lock);
860 if (ns_is_client(lock->l_resource->lr_namespace)) {
861 LDLM_ERROR(lock, "Trying to cancel local lock");
864 LDLM_DEBUG(lock, "server-side local cancel");
865 ldlm_lock_cancel(lock);
866 ldlm_reprocess_all(lock->l_resource);
867 LDLM_DEBUG(lock, "server-side local cancel handler END");
873 /* Pack @count locks in @head into ldlm_request buffer at the offset @off,
874 of the request @req. */
875 static void ldlm_cancel_pack(struct ptlrpc_request *req,
876 struct list_head *head, int count)
878 struct ldlm_request *dlm;
879 struct ldlm_lock *lock;
883 dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
884 LASSERT(dlm != NULL);
886 /* Check the room in the request buffer. */
887 max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
888 sizeof(struct ldlm_request);
889 max /= sizeof(struct lustre_handle);
890 max += LDLM_LOCKREQ_HANDLES;
891 LASSERT(max >= dlm->lock_count + count);
893 /* XXX: it would be better to pack lock handles grouped by resource.
894 * so that the server cancel would call filter_lvbo_update() less
896 list_for_each_entry(lock, head, l_bl_ast) {
899 LASSERT(lock->l_conn_export);
900 /* Pack the lock handle to the given request buffer. */
901 LDLM_DEBUG(lock, "packing");
902 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
905 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
909 /* Prepare and send a batched cancel rpc, it will include count lock handles
910 * of locks given in @head. */
911 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
912 int count, int flags)
914 struct ptlrpc_request *req = NULL;
915 struct obd_import *imp;
920 LASSERT(exp != NULL);
923 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
928 struct req_capsule *pill;
929 imp = class_exp2cliimp(exp);
930 if (imp == NULL || imp->imp_invalid) {
932 "skipping cancel on invalid import %p\n", imp);
936 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
938 GOTO(out, rc = -ENOMEM);
940 pill = &req->rq_pill;
941 bufcount = req_capsule_filled_sizes(pill, RCL_CLIENT);
943 free = ldlm_req_handles_avail(exp, pill->rc_area[RCL_CLIENT],
944 bufcount, bufcount, 0);
948 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
949 ldlm_request_bufsize(count, LDLM_CANCEL));
951 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
953 ptlrpc_request_free(req);
956 req->rq_no_resend = 1;
957 req->rq_no_delay = 1;
959 /* XXX FIXME bug 249 */
960 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
961 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
963 ldlm_cancel_pack(req, cancels, count);
965 ptlrpc_request_set_replen(req);
966 if (flags & LDLM_FL_ASYNC) {
967 ptlrpcd_add_req(req);
971 rc = ptlrpc_queue_wait(req);
974 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
975 "out of sync -- not fatal\n",
976 libcfs_nid2str(req->rq_import->
977 imp_connection->c_peer.nid));
979 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
980 req->rq_import_generation == imp->imp_generation) {
981 ptlrpc_req_finished(req);
983 } else if (rc != ELDLM_OK) {
984 CERROR("Got rc %d from cancel RPC: canceling "
992 ptlrpc_req_finished(req);
995 return sent ? sent : rc;
998 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1000 LASSERT(imp != NULL);
1001 return &imp->imp_obd->obd_namespace->ns_pool;
1004 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1006 __u64 old_slv, new_slv;
1007 struct ldlm_pool *pl;
1011 if (!imp_connect_lru_resize(req->rq_import))
1014 /* In some cases RPC may contain slv and limit zeroed out. This is
1015 * the case when server does not support lru resize feature. This is
1016 * also possible in some recovery cases when server side reqs have no
1017 * ref to obd export and thus access to server side namespace is no
1019 if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1020 lustre_msg_get_limit(req->rq_repmsg) == 0) {
1021 DEBUG_REQ(D_HA, req, "zero SLV or Limit found "
1022 "(SLV: "LPU64", Limit: %u)",
1023 lustre_msg_get_slv(req->rq_repmsg),
1024 lustre_msg_get_limit(req->rq_repmsg));
1028 new_limit = lustre_msg_get_limit(req->rq_repmsg);
1029 new_slv = lustre_msg_get_slv(req->rq_repmsg);
1030 pl = ldlm_imp2pl(req->rq_import);
1032 spin_lock(&pl->pl_lock);
1033 old_slv = ldlm_pool_get_slv(pl);
1034 ldlm_pool_set_slv(pl, new_slv);
1035 ldlm_pool_set_limit(pl, new_limit);
1037 /* Check if we need to wakeup pools thread for fast SLV change.
1038 * This is only done when threads period is noticably long like
1040 #if defined(__KERNEL__) && (LDLM_POOLS_THREAD_PERIOD >= 10)
1042 __u64 fast_change = old_slv * LDLM_POOLS_FAST_SLV_CHANGE;
1043 do_div(fast_change, 100);
1045 /* Wake up pools thread only if SLV has changed more than
1046 * 50% since last update. In this case we want to react asap.
1047 * Otherwise it is no sense to wake up pools as they are
1048 * re-calculated every LDLM_POOLS_THREAD_PERIOD anyways. */
1049 if (old_slv > new_slv && old_slv - new_slv > fast_change)
1050 ldlm_pools_wakeup();
1053 spin_unlock(&pl->pl_lock);
1056 EXPORT_SYMBOL(ldlm_cli_update_pool);
1058 int ldlm_cli_cancel(struct lustre_handle *lockh)
1060 int avail, flags, count = 1, rc = 0;
1061 struct ldlm_namespace *ns;
1062 struct ldlm_lock *lock;
1063 CFS_LIST_HEAD(cancels);
1066 /* concurrent cancels on the same handle can happen */
1067 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
1069 LDLM_DEBUG_NOLOCK("lock is already being destroyed\n");
1073 rc = ldlm_cli_cancel_local(lock);
1074 if (rc < 0 || rc == LDLM_FL_LOCAL_ONLY) {
1075 LDLM_LOCK_PUT(lock);
1076 RETURN(rc < 0 ? rc : 0);
1078 /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1079 * rpc which goes to canceld portal, so we can cancel other lru locks
1080 * here and send them all as one LDLM_CANCEL rpc. */
1081 LASSERT(list_empty(&lock->l_bl_ast));
1082 list_add(&lock->l_bl_ast, &cancels);
1083 if (exp_connect_cancelset(lock->l_conn_export)) {
1084 avail = ldlm_cancel_handles_avail(lock->l_conn_export);
1087 ns = lock->l_resource->lr_namespace;
1088 flags = ns_connect_lru_resize(ns) ?
1089 LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
1090 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1091 LDLM_FL_BL_AST, flags);
1093 ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1097 /* XXX until we will have compound requests and can cut cancels from generic rpc
1098 * we need send cancels with LDLM_FL_BL_AST flag as separate rpc */
1099 static int ldlm_cancel_list(struct list_head *cancels, int count, int flags)
1101 CFS_LIST_HEAD(head);
1102 struct ldlm_lock *lock, *next;
1103 int left = 0, bl_ast = 0, rc;
1106 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1110 if (flags & LDLM_FL_LOCAL_ONLY) {
1111 rc = LDLM_FL_LOCAL_ONLY;
1112 ldlm_lock_cancel(lock);
1114 rc = ldlm_cli_cancel_local(lock);
1116 if (!(flags & LDLM_FL_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1117 LDLM_DEBUG(lock, "Cancel lock separately");
1118 list_del_init(&lock->l_bl_ast);
1119 list_add(&lock->l_bl_ast, &head);
1123 if (rc == LDLM_FL_LOCAL_ONLY) {
1124 /* CANCEL RPC should not be sent to server. */
1125 list_del_init(&lock->l_bl_ast);
1126 LDLM_LOCK_PUT(lock);
1133 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1139 /* Return 1 to stop lru processing and keep current lock cached. Return zero
1141 static ldlm_policy_res_t ldlm_cancel_shrink_policy(struct ldlm_namespace *ns,
1142 struct ldlm_lock *lock,
1143 int unused, int added,
1149 /* Stop lru processing when we reached passed @count or checked all
1151 if (count && added >= count)
1152 return LDLM_POLICY_KEEP_LOCK;
1154 if (lock->l_resource->lr_type == LDLM_EXTENT) {
1155 struct ldlm_extent *l_extent;
1157 /* For all extent locks cost is 1 + number of pages in
1159 l_extent = &lock->l_policy_data.l_extent;
1160 page_nr = (l_extent->end - l_extent->start);
1161 do_div(page_nr, CFS_PAGE_SIZE);
1164 /* XXX: In fact this is evil hack, we can't access inode
1165 * here. For doing it right we need somehow to have number
1166 * of covered by lock. This should be fixed later when 10718
1168 if (lock->l_ast_data != NULL) {
1169 struct inode *inode = lock->l_ast_data;
1170 if (page_nr > inode->i_mapping->nrpages)
1171 page_nr = inode->i_mapping->nrpages;
1174 lock_cost = 1 + page_nr;
1176 /* For all locks which are not extent ones cost is 1 */
1180 /* Keep all expensive locks in lru for the memory pressure time
1181 * cancel policy. They anyways may be canceled by lru resize
1182 * pplicy if they have not small enough CLV. */
1183 return lock_cost > ns->ns_shrink_thumb ?
1184 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1187 /* Return 1 to stop lru processing and keep current lock cached. Return zero
1189 static ldlm_policy_res_t ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1190 struct ldlm_lock *lock,
1191 int unused, int added,
1194 cfs_time_t cur = cfs_time_current();
1195 struct ldlm_pool *pl = &ns->ns_pool;
1199 /* Stop lru processing when we reached passed @count or checked all
1201 if (count && added >= count)
1202 return LDLM_POLICY_KEEP_LOCK;
1204 spin_lock(&pl->pl_lock);
1205 slv = ldlm_pool_get_slv(pl);
1206 lvf = atomic_read(&pl->pl_lock_volume_factor);
1207 spin_unlock(&pl->pl_lock);
1209 la = cfs_duration_sec(cfs_time_sub(cur,
1210 lock->l_last_used));
1212 /* Stop when slv is not yet come from server or
1213 * lv is smaller than it is. */
1214 lv = lvf * la * unused;
1215 return (slv == 1 || lv < slv) ?
1216 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1219 /* Return 1 to stop lru processing and keep current lock cached. Return zero
1221 static ldlm_policy_res_t ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1222 struct ldlm_lock *lock,
1223 int unused, int added,
1226 /* Stop lru processing when we reached passed @count or checked all
1228 return (added >= count) ?
1229 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1232 /* Return 1 to stop lru processing and keep current lock cached. Return zero
1234 static ldlm_policy_res_t ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1235 struct ldlm_lock *lock,
1236 int unused, int added,
1239 /* Stop lru processing if young lock is found and we reached passed
1241 return ((added >= count) &&
1242 cfs_time_before(cfs_time_current(),
1243 cfs_time_add(lock->l_last_used,
1245 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1248 /* Return 1 to stop lru processing and keep current lock cached. Return zero
1250 static ldlm_policy_res_t ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1251 struct ldlm_lock *lock,
1252 int unused, int added,
1255 /* Stop lru processing when we reached passed @count or checked all
1257 return (added >= count) ?
1258 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1261 typedef ldlm_policy_res_t (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *,
1262 struct ldlm_lock *, int,
1265 static ldlm_cancel_lru_policy_t
1266 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, int flags)
1268 if (ns_connect_lru_resize(ns)) {
1269 if (flags & LDLM_CANCEL_SHRINK)
1270 return ldlm_cancel_shrink_policy;
1271 else if (flags & LDLM_CANCEL_LRUR)
1272 return ldlm_cancel_lrur_policy;
1273 else if (flags & LDLM_CANCEL_PASSED)
1274 return ldlm_cancel_passed_policy;
1276 if (flags & LDLM_CANCEL_AGED)
1277 return ldlm_cancel_aged_policy;
1280 return ldlm_cancel_default_policy;
1283 /* - Free space in lru for @count new locks,
1284 * redundant unused locks are canceled locally;
1285 * - also cancel locally unused aged locks;
1286 * - do not cancel more than @max locks;
1287 * - GET the found locks and add them into the @cancels list.
1289 * A client lock can be added to the l_bl_ast list only when it is
1290 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing CANCEL.
1291 * There are the following use cases: ldlm_cancel_resource_local(),
1292 * ldlm_cancel_lru_local() and ldlm_cli_cancel(), which check&set this
1293 * flag properly. As any attempt to cancel a lock rely on this flag,
1294 * l_bl_ast list is accessed later without any special locking.
1296 * Calling policies for enabled lru resize:
1297 * ----------------------------------------
1298 * flags & LDLM_CANCEL_LRUR - use lru resize policy (SLV from server) to
1299 * cancel not more than @count locks;
1301 * flags & LDLM_CANCEL_PASSED - cancel @count number of old locks (located at
1302 * the beginning of lru list);
1304 * flags & LDLM_CANCEL_SHRINK - cancel not more than @count locks according to
1305 * memory pressre policy function;
1307 * flags & LDLM_CANCEL_AGED - cancel alocks according to "aged policy".
1309 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1310 int count, int max, int cancel_flags, int flags)
1312 ldlm_cancel_lru_policy_t pf;
1313 struct ldlm_lock *lock;
1314 int added = 0, unused;
1317 spin_lock(&ns->ns_unused_lock);
1318 unused = ns->ns_nr_unused;
1320 if (!ns_connect_lru_resize(ns))
1321 count += unused - ns->ns_max_unused;
1323 pf = ldlm_cancel_lru_policy(ns, flags);
1324 LASSERT(pf != NULL);
1326 while (!list_empty(&ns->ns_unused_list)) {
1327 /* For any flags, stop scanning if @max is reached. */
1328 if (max && added >= max)
1331 list_for_each_entry(lock, &ns->ns_unused_list, l_lru) {
1332 /* Somebody is already doing CANCEL or there is a
1333 * blocking request will send cancel. */
1334 if (!(lock->l_flags & LDLM_FL_CANCELING) &&
1335 !(lock->l_flags & LDLM_FL_BL_AST))
1338 if (&lock->l_lru == &ns->ns_unused_list)
1341 /* Pass the lock through the policy filter and see if it
1342 * should stay in lru.
1344 * Even for shrinker policy we stop scanning if
1345 * we find a lock that should stay in the cache.
1346 * We should take into account lock age anyway
1347 * as new lock even if it is small of weight is
1348 * valuable resource.
1350 * That is, for shrinker policy we drop only
1351 * old locks, but additionally chose them by
1352 * their weight. Big extent locks will stay in
1354 if (pf(ns, lock, unused, added, count) == LDLM_POLICY_KEEP_LOCK)
1357 LDLM_LOCK_GET(lock); /* dropped by bl thread */
1358 spin_unlock(&ns->ns_unused_lock);
1360 lock_res_and_lock(lock);
1361 /* Check flags again under the lock. */
1362 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1363 (lock->l_flags & LDLM_FL_BL_AST) ||
1364 (ldlm_lock_remove_from_lru(lock) == 0)) {
1365 /* other thread is removing lock from lru or
1366 * somebody is already doing CANCEL or
1367 * there is a blocking request which will send
1368 * cancel by itseft. */
1369 unlock_res_and_lock(lock);
1370 LDLM_LOCK_PUT(lock);
1371 spin_lock(&ns->ns_unused_lock);
1374 LASSERT(!lock->l_readers && !lock->l_writers);
1376 /* If we have chosen to cancel this lock voluntarily, we
1377 * better send cancel notification to server, so that it
1378 * frees appropriate state. This might lead to a race
1379 * where while we are doing cancel here, server is also
1380 * silently cancelling this lock. */
1381 lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
1383 /* Setting the CBPENDING flag is a little misleading,
1384 * but prevents an important race; namely, once
1385 * CBPENDING is set, the lock can accumulate no more
1386 * readers/writers. Since readers and writers are
1387 * already zero here, ldlm_lock_decref() won't see
1388 * this flag and call l_blocking_ast */
1389 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1391 /* We can't re-add to l_lru as it confuses the
1392 * refcounting in ldlm_lock_remove_from_lru() if an AST
1393 * arrives after we drop ns_lock below. We use l_bl_ast
1394 * and can't use l_pending_chain as it is used both on
1395 * server and client nevertheless bug 5666 says it is
1396 * used only on server */
1397 LASSERT(list_empty(&lock->l_bl_ast));
1398 list_add(&lock->l_bl_ast, cancels);
1399 unlock_res_and_lock(lock);
1400 spin_lock(&ns->ns_unused_lock);
1404 spin_unlock(&ns->ns_unused_lock);
1405 RETURN(ldlm_cancel_list(cancels, added, cancel_flags));
1408 /* Returns number of locks which could be canceled next time when
1409 * ldlm_cancel_lru() is called. Used from locks pool shrinker. */
1410 int ldlm_cancel_lru_estimate(struct ldlm_namespace *ns,
1411 int count, int max, int flags)
1413 ldlm_cancel_lru_policy_t pf;
1414 struct ldlm_lock *lock;
1415 int added = 0, unused;
1418 pf = ldlm_cancel_lru_policy(ns, flags);
1419 LASSERT(pf != NULL);
1420 spin_lock(&ns->ns_unused_lock);
1421 unused = ns->ns_nr_unused;
1423 list_for_each_entry(lock, &ns->ns_unused_list, l_lru) {
1424 /* For any flags, stop scanning if @max is reached. */
1425 if (max && added >= max)
1428 /* Somebody is already doing CANCEL or there is a
1429 * blocking request will send cancel. Let's not count
1431 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1432 (lock->l_flags & LDLM_FL_BL_AST))
1435 /* Pass the lock through the policy filter and see if it
1436 * should stay in lru. */
1437 if (pf(ns, lock, unused, added, count) == LDLM_POLICY_KEEP_LOCK)
1443 spin_unlock(&ns->ns_unused_lock);
1447 /* when called with LDLM_ASYNC the blocking callback will be handled
1448 * in a thread and this function will return after the thread has been
1449 * asked to call the callback. when called with LDLM_SYNC the blocking
1450 * callback will be performed in this function. */
1451 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr, ldlm_sync_t sync,
1454 CFS_LIST_HEAD(cancels);
1459 sync = LDLM_SYNC; /* force to be sync in user space */
1461 count = ldlm_cancel_lru_local(ns, &cancels, nr, 0, 0, flags);
1462 if (sync == LDLM_ASYNC) {
1463 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count);
1468 /* If an error occured in ASYNC mode, or
1469 * this is SYNC mode, cancel the list. */
1470 ldlm_cli_cancel_list(&cancels, count, NULL, 0);
1474 /* Find and cancel locally unused locks found on resource, matched to the
1475 * given policy, mode. GET the found locks and add them into the @cancels
1477 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1478 struct list_head *cancels,
1479 ldlm_policy_data_t *policy,
1480 ldlm_mode_t mode, int lock_flags,
1481 int cancel_flags, void *opaque)
1483 struct ldlm_lock *lock;
1488 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1489 if (opaque != NULL && lock->l_ast_data != opaque) {
1490 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1491 lock->l_ast_data, opaque);
1496 if (lock->l_readers || lock->l_writers) {
1497 if (cancel_flags & LDLM_FL_WARN) {
1498 LDLM_ERROR(lock, "lock in use");
1504 /* If somebody is already doing CANCEL, or blocking ast came,
1505 * skip this lock. */
1506 if (lock->l_flags & LDLM_FL_BL_AST ||
1507 lock->l_flags & LDLM_FL_CANCELING)
1510 if (lockmode_compat(lock->l_granted_mode, mode))
1513 /* If policy is given and this is IBITS lock, add to list only
1514 * those locks that match by policy. */
1515 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1516 !(lock->l_policy_data.l_inodebits.bits &
1517 policy->l_inodebits.bits))
1520 /* See CBPENDING comment in ldlm_cancel_lru */
1521 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1524 LASSERT(list_empty(&lock->l_bl_ast));
1525 list_add(&lock->l_bl_ast, cancels);
1526 LDLM_LOCK_GET(lock);
1531 RETURN(ldlm_cancel_list(cancels, count, cancel_flags));
1534 /* If @req is NULL, send CANCEL request to server with handles of locks
1535 * in the @cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1536 * separately per lock.
1537 * If @req is not NULL, put handles of locks in @cancels into the request
1538 * buffer at the offset @off.
1539 * Destroy @cancels at the end. */
1540 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1541 struct ptlrpc_request *req, int flags)
1543 struct ldlm_lock *lock;
1547 if (list_empty(cancels) || count == 0)
1550 /* XXX: requests (both batched and not) could be sent in parallel.
1551 * Usually it is enough to have just 1 RPC, but it is possible that
1552 * there are to many locks to be cancelled in LRU or on a resource.
1553 * It would also speed up the case when the server does not support
1556 LASSERT(!list_empty(cancels));
1557 lock = list_entry(cancels->next, struct ldlm_lock, l_bl_ast);
1558 LASSERT(lock->l_conn_export);
1560 if (exp_connect_cancelset(lock->l_conn_export)) {
1563 ldlm_cancel_pack(req, cancels, count);
1565 res = ldlm_cli_cancel_req(lock->l_conn_export,
1569 res = ldlm_cli_cancel_req(lock->l_conn_export,
1574 CERROR("ldlm_cli_cancel_list: %d\n", res);
1579 ldlm_lock_list_put(cancels, l_bl_ast, res);
1581 LASSERT(count == 0);
1585 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1586 const struct ldlm_res_id *res_id,
1587 ldlm_policy_data_t *policy,
1588 ldlm_mode_t mode, int flags, void *opaque)
1590 struct ldlm_resource *res;
1591 CFS_LIST_HEAD(cancels);
1596 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1598 /* This is not a problem. */
1599 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1603 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1605 rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1607 CERROR("ldlm_cli_cancel_unused_resource: %d\n", rc);
1609 ldlm_resource_putref(res);
1613 static inline int have_no_nsresource(struct ldlm_namespace *ns)
1615 int no_resource = 0;
1617 spin_lock(&ns->ns_hash_lock);
1618 if (ns->ns_resources == 0)
1620 spin_unlock(&ns->ns_hash_lock);
1622 RETURN(no_resource);
1625 /* Cancel all locks on a namespace (or a specific resource, if given)
1626 * that have 0 readers/writers.
1628 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1629 * to notify the server. */
1630 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
1631 const struct ldlm_res_id *res_id,
1632 int flags, void *opaque)
1641 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
1645 spin_lock(&ns->ns_hash_lock);
1646 for (i = 0; i < RES_HASH_SIZE; i++) {
1647 struct list_head *tmp;
1648 tmp = ns->ns_hash[i].next;
1649 while (tmp != &(ns->ns_hash[i])) {
1650 struct ldlm_resource *res;
1653 res = list_entry(tmp, struct ldlm_resource, lr_hash);
1654 ldlm_resource_getref(res);
1655 spin_unlock(&ns->ns_hash_lock);
1657 rc = ldlm_cli_cancel_unused_resource(ns, &res->lr_name,
1662 CERROR("ldlm_cli_cancel_unused ("LPU64"): %d\n",
1663 res->lr_name.name[0], rc);
1665 spin_lock(&ns->ns_hash_lock);
1667 ldlm_resource_putref_locked(res);
1670 spin_unlock(&ns->ns_hash_lock);
1675 /* join/split resource locks to/from lru list */
1676 int ldlm_cli_join_lru(struct ldlm_namespace *ns,
1677 const struct ldlm_res_id *res_id, int join)
1679 struct ldlm_resource *res;
1680 struct ldlm_lock *lock, *n;
1684 LASSERT(ns_is_client(ns));
1686 res = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
1689 LASSERT(res->lr_type == LDLM_EXTENT);
1695 list_for_each_entry_safe (lock, n, &res->lr_granted, l_res_link) {
1696 if (list_empty(&lock->l_lru) &&
1697 !lock->l_readers && !lock->l_writers &&
1698 !(lock->l_flags & LDLM_FL_LOCAL) &&
1699 !(lock->l_flags & LDLM_FL_CBPENDING)) {
1700 ldlm_lock_add_to_lru(lock);
1701 lock->l_flags &= ~LDLM_FL_NO_LRU;
1702 LDLM_DEBUG(lock, "join lock to lru");
1708 spin_lock(&ns->ns_unused_lock);
1709 list_for_each_entry_safe (lock, n, &ns->ns_unused_list, l_lru) {
1710 if (lock->l_resource == res) {
1711 ldlm_lock_remove_from_lru_nolock(lock);
1712 lock->l_flags |= LDLM_FL_NO_LRU;
1713 LDLM_DEBUG(lock, "split lock from lru");
1717 spin_unlock(&ns->ns_unused_lock);
1720 ldlm_resource_putref(res);
1724 /* Lock iterators. */
1726 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1729 struct list_head *tmp, *next;
1730 struct ldlm_lock *lock;
1731 int rc = LDLM_ITER_CONTINUE;
1736 RETURN(LDLM_ITER_CONTINUE);
1739 list_for_each_safe(tmp, next, &res->lr_granted) {
1740 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1742 if (iter(lock, closure) == LDLM_ITER_STOP)
1743 GOTO(out, rc = LDLM_ITER_STOP);
1746 list_for_each_safe(tmp, next, &res->lr_converting) {
1747 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1749 if (iter(lock, closure) == LDLM_ITER_STOP)
1750 GOTO(out, rc = LDLM_ITER_STOP);
1753 list_for_each_safe(tmp, next, &res->lr_waiting) {
1754 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1756 if (iter(lock, closure) == LDLM_ITER_STOP)
1757 GOTO(out, rc = LDLM_ITER_STOP);
1764 struct iter_helper_data {
1765 ldlm_iterator_t iter;
1769 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1771 struct iter_helper_data *helper = closure;
1772 return helper->iter(lock, helper->closure);
1775 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
1777 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
1780 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
1783 struct iter_helper_data helper = { iter: iter, closure: closure };
1784 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
1787 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
1788 ldlm_res_iterator_t iter, void *closure)
1790 int i, rc = LDLM_ITER_CONTINUE;
1791 struct ldlm_resource *res;
1792 struct list_head *tmp;
1795 spin_lock(&ns->ns_hash_lock);
1796 for (i = 0; i < RES_HASH_SIZE; i++) {
1797 tmp = ns->ns_hash[i].next;
1798 while (tmp != &(ns->ns_hash[i])) {
1799 res = list_entry(tmp, struct ldlm_resource, lr_hash);
1800 ldlm_resource_getref(res);
1801 spin_unlock(&ns->ns_hash_lock);
1803 rc = iter(res, closure);
1805 spin_lock(&ns->ns_hash_lock);
1807 ldlm_resource_putref_locked(res);
1808 if (rc == LDLM_ITER_STOP)
1813 spin_unlock(&ns->ns_hash_lock);
1817 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
1818 void ldlm_resource_iterate(struct ldlm_namespace *ns,
1819 const struct ldlm_res_id *res_id,
1820 ldlm_iterator_t iter, void *data)
1822 struct ldlm_resource *res;
1826 CERROR("must pass in namespace\n");
1830 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1836 ldlm_resource_foreach(res, iter, data);
1837 ldlm_resource_putref(res);
1843 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
1845 struct list_head *list = closure;
1847 /* we use l_pending_chain here, because it's unused on clients. */
1848 LASSERTF(list_empty(&lock->l_pending_chain),"lock %p next %p prev %p\n",
1849 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
1850 /* bug 9573: don't replay locks left after eviction */
1851 if (!(lock->l_flags & LDLM_FL_FAILED))
1852 list_add(&lock->l_pending_chain, list);
1853 return LDLM_ITER_CONTINUE;
1856 static int replay_lock_interpret(struct ptlrpc_request *req,
1857 struct ldlm_async_args *aa, int rc)
1859 struct ldlm_lock *lock;
1860 struct ldlm_reply *reply;
1863 atomic_dec(&req->rq_import->imp_replay_inflight);
1868 reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1870 GOTO(out, rc = -EPROTO);
1872 lock = ldlm_handle2lock(&aa->lock_handle);
1874 CERROR("received replay ack for unknown local cookie "LPX64
1875 " remote cookie "LPX64 " from server %s id %s\n",
1876 aa->lock_handle.cookie, reply->lock_handle.cookie,
1877 req->rq_export->exp_client_uuid.uuid,
1878 libcfs_id2str(req->rq_peer));
1879 GOTO(out, rc = -ESTALE);
1882 lock->l_remote_handle = reply->lock_handle;
1883 LDLM_DEBUG(lock, "replayed lock:");
1884 ptlrpc_import_recovery_state_machine(req->rq_import);
1885 LDLM_LOCK_PUT(lock);
1888 ptlrpc_connect_import(req->rq_import, NULL);
1894 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
1896 struct ptlrpc_request *req;
1897 struct ldlm_async_args *aa;
1898 struct ldlm_request *body;
1903 /* Bug 11974: Do not replay a lock which is actively being canceled */
1904 if (lock->l_flags & LDLM_FL_CANCELING) {
1905 LDLM_DEBUG(lock, "Not replaying canceled lock:");
1909 /* If this is reply-less callback lock, we cannot replay it, since
1910 * server might have long dropped it, but notification of that event was
1911 * lost by network. (and server granted conflicting lock already) */
1912 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1913 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
1914 ldlm_lock_cancel(lock);
1918 * If granted mode matches the requested mode, this lock is granted.
1920 * If they differ, but we have a granted mode, then we were granted
1921 * one mode and now want another: ergo, converting.
1923 * If we haven't been granted anything and are on a resource list,
1924 * then we're blocked/waiting.
1926 * If we haven't been granted anything and we're NOT on a resource list,
1927 * then we haven't got a reply yet and don't have a known disposition.
1928 * This happens whenever a lock enqueue is the request that triggers
1931 if (lock->l_granted_mode == lock->l_req_mode)
1932 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
1933 else if (lock->l_granted_mode)
1934 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
1935 else if (!list_empty(&lock->l_res_link))
1936 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
1938 flags = LDLM_FL_REPLAY;
1940 req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
1941 LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
1945 /* We're part of recovery, so don't wait for it. */
1946 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
1948 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1949 ldlm_lock2desc(lock, &body->lock_desc);
1950 body->lock_flags = flags;
1952 ldlm_lock2handle(lock, &body->lock_handle[0]);
1953 if (lock->l_lvb_len != 0) {
1954 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
1955 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
1958 ptlrpc_request_set_replen(req);
1959 /* notify the server we've replayed all requests.
1960 * also, we mark the request to be put on a dedicated
1961 * queue to be processed after all request replayes.
1963 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
1965 LDLM_DEBUG(lock, "replaying lock:");
1967 atomic_inc(&req->rq_import->imp_replay_inflight);
1968 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1969 aa = (struct ldlm_async_args *)&req->rq_async_args;
1970 aa->lock_handle = body->lock_handle[0];
1971 req->rq_interpret_reply = replay_lock_interpret;
1972 ptlrpcd_add_req(req);
1977 int ldlm_replay_locks(struct obd_import *imp)
1979 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
1980 struct list_head list;
1981 struct ldlm_lock *lock, *next;
1985 CFS_INIT_LIST_HEAD(&list);
1987 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1989 /* ensure this doesn't fall to 0 before all have been queued */
1990 atomic_inc(&imp->imp_replay_inflight);
1992 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
1994 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
1995 list_del_init(&lock->l_pending_chain);
1997 continue; /* or try to do the rest? */
1998 rc = replay_one_lock(imp, lock);
2001 atomic_dec(&imp->imp_replay_inflight);