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 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
92 /* XXX ALLOCATE - 160 bytes */
93 struct lock_wait_data lwd;
94 struct obd_device *obd;
95 struct obd_import *imp = NULL;
96 struct l_wait_info lwi;
100 if (flags == LDLM_FL_WAIT_NOREPROC) {
101 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
105 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
106 LDLM_FL_BLOCK_CONV))) {
107 cfs_waitq_signal(&lock->l_waitq);
111 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
113 ldlm_lock_dump(D_OTHER, lock, 0);
114 ldlm_reprocess_all(lock->l_resource);
118 obd = class_exp2obd(lock->l_conn_export);
120 /* if this is a local lock, then there is no import */
122 imp = obd->u.cli.cl_import;
126 if (lock->l_flags & LDLM_FL_NO_TIMEOUT) {
127 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
128 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
130 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
131 ldlm_expired_completion_wait,
132 interrupted_completion_wait, &lwd);
136 spin_lock(&imp->imp_lock);
137 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
138 spin_unlock(&imp->imp_lock);
141 /* Go to sleep until the lock is granted or cancelled. */
142 rc = l_wait_event(lock->l_waitq,
143 ((lock->l_req_mode == lock->l_granted_mode) ||
144 (lock->l_flags & LDLM_FL_FAILED)), &lwi);
146 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
147 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
152 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
157 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
162 * ->l_blocking_ast() callback for LDLM locks acquired by server-side OBDs.
164 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
165 void *data, int flag)
170 if (flag == LDLM_CB_CANCELING) {
171 /* Don't need to do anything here. */
175 lock_res_and_lock(lock);
176 /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
177 * that ldlm_blocking_ast is called just before intent_policy method
178 * takes the ns_lock, then by the time we get the lock, we might not
179 * be the correct blocking function anymore. So check, and return
181 if (lock->l_blocking_ast != ldlm_blocking_ast) {
182 unlock_res_and_lock(lock);
186 lock->l_flags |= LDLM_FL_CBPENDING;
187 do_ast = (!lock->l_readers && !lock->l_writers);
188 unlock_res_and_lock(lock);
191 struct lustre_handle lockh;
194 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
195 ldlm_lock2handle(lock, &lockh);
196 rc = ldlm_cli_cancel(&lockh);
198 CERROR("ldlm_cli_cancel: %d\n", rc);
200 LDLM_DEBUG(lock, "Lock still has references, will be "
207 * ->l_glimpse_ast() for DLM extent locks acquired on the server-side. See
208 * comment in filter_intent_policy() on why you may need this.
210 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
213 * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for
214 * that is rather subtle: with OST-side locking, it may so happen that
215 * _all_ extent locks are held by the OST. If client wants to obtain
216 * current file size it calls ll{,u}_glimpse_size(), and (as locks are
217 * on the server), dummy glimpse callback fires and does
218 * nothing. Client still receives correct file size due to the
219 * following fragment in filter_intent_policy():
221 * rc = l->l_glimpse_ast(l, NULL); // this will update the LVB
222 * if (rc != 0 && res->lr_namespace->ns_lvbo &&
223 * res->lr_namespace->ns_lvbo->lvbo_update) {
224 * res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
227 * that is, after glimpse_ast() fails, filter_lvbo_update() runs, and
228 * returns correct file size to the client.
230 return -ELDLM_NO_LOCK_DATA;
233 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
234 const struct ldlm_res_id *res_id,
235 ldlm_type_t type, ldlm_policy_data_t *policy,
236 ldlm_mode_t mode, int *flags,
237 ldlm_blocking_callback blocking,
238 ldlm_completion_callback completion,
239 ldlm_glimpse_callback glimpse,
240 void *data, __u32 lvb_len, void *lvb_swabber,
241 struct lustre_handle *lockh)
243 struct ldlm_lock *lock;
247 LASSERT(!(*flags & LDLM_FL_REPLAY));
248 if (unlikely(ns->ns_client)) {
249 CERROR("Trying to enqueue local lock in a shadow namespace\n");
253 lock = ldlm_lock_create(ns, res_id, type, mode, blocking,
254 completion, glimpse, data, lvb_len);
256 GOTO(out_nolock, err = -ENOMEM);
257 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
259 ldlm_lock_addref_internal(lock, mode);
260 ldlm_lock2handle(lock, lockh);
261 lock_res_and_lock(lock);
262 lock->l_flags |= LDLM_FL_LOCAL;
263 if (*flags & LDLM_FL_ATOMIC_CB)
264 lock->l_flags |= LDLM_FL_ATOMIC_CB;
265 lock->l_lvb_swabber = lvb_swabber;
266 unlock_res_and_lock(lock);
268 lock->l_policy_data = *policy;
269 if (type == LDLM_EXTENT)
270 lock->l_req_extent = policy->l_extent;
272 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
273 if (unlikely(err != ELDLM_OK))
277 *policy = lock->l_policy_data;
279 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
282 if (lock->l_completion_ast)
283 lock->l_completion_ast(lock, *flags, NULL);
285 LDLM_DEBUG(lock, "client-side local enqueue END");
293 static void failed_lock_cleanup(struct ldlm_namespace *ns,
294 struct ldlm_lock *lock,
295 struct lustre_handle *lockh, int mode)
297 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
298 lock_res_and_lock(lock);
299 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
300 unlock_res_and_lock(lock);
301 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
303 ldlm_lock_decref_and_cancel(lockh, mode);
305 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
306 * from llite/file.c/ll_file_flock(). */
307 if (lock->l_resource->lr_type == LDLM_FLOCK) {
308 ldlm_lock_destroy(lock);
312 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
313 ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
314 int *flags, void *lvb, __u32 lvb_len,
315 void *lvb_swabber, struct lustre_handle *lockh,int rc)
317 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
318 int is_replay = *flags & LDLM_FL_REPLAY;
319 struct ldlm_lock *lock;
320 struct ldlm_reply *reply;
321 int cleanup_phase = 1;
324 lock = ldlm_handle2lock(lockh);
325 /* ldlm_cli_enqueue is holding a reference on this lock. */
327 LASSERT(type == LDLM_FLOCK);
331 if (rc != ELDLM_OK) {
333 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
334 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
335 if (rc == ELDLM_LOCK_ABORTED) {
336 /* Before we return, swab the reply */
337 reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF,
339 lustre_swab_ldlm_reply);
341 CERROR("Can't unpack ldlm_reply\n");
346 tmplvb = lustre_swab_repbuf(req,
351 GOTO(cleanup, rc = -EPROTO);
353 memcpy(lvb, tmplvb, lvb_len);
359 reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF, sizeof(*reply),
360 lustre_swab_ldlm_reply);
362 CERROR("Can't unpack ldlm_reply\n");
363 GOTO(cleanup, rc = -EPROTO);
366 /* lock enqueued on the server */
369 lock_res_and_lock(lock);
370 lock->l_remote_handle = reply->lock_handle;
371 *flags = reply->lock_flags;
372 lock->l_flags |= reply->lock_flags & LDLM_INHERIT_FLAGS;
373 /* move NO_TIMEOUT flag to the lock to force ldlm_lock_match()
374 * to wait with no timeout as well */
375 lock->l_flags |= reply->lock_flags & LDLM_FL_NO_TIMEOUT;
376 unlock_res_and_lock(lock);
378 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
379 lock, reply->lock_handle.cookie, *flags);
381 /* If enqueue returned a blocked lock but the completion handler has
382 * already run, then it fixed up the resource and we don't need to do it
384 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
385 int newmode = reply->lock_desc.l_req_mode;
387 if (newmode && newmode != lock->l_req_mode) {
388 LDLM_DEBUG(lock, "server returned different mode %s",
389 ldlm_lockname[newmode]);
390 lock->l_req_mode = newmode;
393 if (memcmp(reply->lock_desc.l_resource.lr_name.name,
394 lock->l_resource->lr_name.name,
395 sizeof(struct ldlm_res_id))) {
396 CDEBUG(D_INFO, "remote intent success, locking "
397 "(%ld,%ld,%ld) instead of "
399 (long)reply->lock_desc.l_resource.lr_name.name[0],
400 (long)reply->lock_desc.l_resource.lr_name.name[1],
401 (long)reply->lock_desc.l_resource.lr_name.name[2],
402 (long)lock->l_resource->lr_name.name[0],
403 (long)lock->l_resource->lr_name.name[1],
404 (long)lock->l_resource->lr_name.name[2]);
406 ldlm_lock_change_resource(ns, lock,
407 &reply->lock_desc.l_resource.lr_name);
408 if (lock->l_resource == NULL) {
410 GOTO(cleanup, rc = -ENOMEM);
412 LDLM_DEBUG(lock, "client-side enqueue, new resource");
415 if (!(type == LDLM_IBITS && !(exp->exp_connect_flags &
417 lock->l_policy_data =
418 reply->lock_desc.l_policy_data;
419 if (type != LDLM_PLAIN)
420 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
423 if ((*flags) & LDLM_FL_AST_SENT ||
424 /* Cancel extent locks as soon as possible on a liblustre client,
425 * because it cannot handle asynchronous ASTs robustly (see
427 (LIBLUSTRE_CLIENT && type == LDLM_EXTENT)) {
428 lock_res_and_lock(lock);
429 lock->l_flags |= LDLM_FL_CBPENDING;
430 unlock_res_and_lock(lock);
431 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
434 /* If the lock has already been granted by a completion AST, don't
435 * clobber the LVB with an older one. */
436 if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
438 tmplvb = lustre_swab_repbuf(req, DLM_REPLY_REC_OFF, lvb_len,
441 GOTO(cleanup, rc = -EPROTO);
442 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
446 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
447 if (lock->l_completion_ast != NULL) {
448 int err = lock->l_completion_ast(lock, *flags, NULL);
451 if (rc && type != LDLM_FLOCK) /* bug 9425, bug 10250 */
456 if (lvb_len && lvb != NULL) {
457 /* Copy the LVB here, and not earlier, because the completion
458 * AST (if any) can override what we got in the reply */
459 memcpy(lvb, lock->l_lvb_data, lvb_len);
462 LDLM_DEBUG(lock, "client-side enqueue END");
465 if (cleanup_phase == 1 && rc)
466 failed_lock_cleanup(ns, lock, lockh, mode);
467 /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
473 /* PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
474 * a single page on the send/receive side. XXX: 512 should be changed
475 * to more adequate value. */
476 static inline int ldlm_req_handles_avail(struct obd_export *exp,
477 int *size, int bufcount, int off)
479 int avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512);
480 int old_size = size[DLM_LOCKREQ_OFF];
482 size[DLM_LOCKREQ_OFF] = sizeof(struct ldlm_request);
483 avail -= lustre_msg_size(class_exp2cliimp(exp)->imp_msg_magic,
485 avail /= sizeof(struct lustre_handle);
486 avail += LDLM_LOCKREQ_HANDLES - off;
487 size[DLM_LOCKREQ_OFF] = old_size;
492 static inline int ldlm_cancel_handles_avail(struct obd_export *exp)
494 int size[2] = { sizeof(struct ptlrpc_body),
495 sizeof(struct ldlm_request) };
496 return ldlm_req_handles_avail(exp, size, 2, 0);
499 /* Cancel lru locks and pack them into the enqueue request. Pack there the given
500 * @count locks in @cancels. */
501 struct ptlrpc_request *ldlm_prep_enqueue_req(struct obd_export *exp,
502 int bufcount, int *size,
503 struct list_head *cancels,
506 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
507 struct ldlm_request *dlm = NULL;
508 struct ptlrpc_request *req;
514 if (exp_connect_cancelset(exp)) {
515 /* Estimate the amount of available space in the request. */
516 int avail = ldlm_req_handles_avail(exp, size, bufcount,
517 LDLM_ENQUEUE_CANCEL_OFF);
518 LASSERT(avail >= count);
520 /* Cancel lru locks here _only_ if the server supports
521 * EARLY_CANCEL. Otherwise we have to send extra CANCEL
522 * rpc right on enqueue, what will make it slower, vs.
523 * asynchronous rpc in blocking thread. */
524 count += ldlm_cancel_lru_local(ns, cancels, 1, avail - count,
526 size[DLM_LOCKREQ_OFF] =
527 ldlm_request_bufsize(count, LDLM_ENQUEUE);
529 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
530 LDLM_ENQUEUE, bufcount, size, NULL);
531 if (exp_connect_cancelset(exp) && req) {
532 dlm = lustre_msg_buf(req->rq_reqmsg,
533 DLM_LOCKREQ_OFF, sizeof(*dlm));
534 /* Skip first lock handler in ldlm_request_pack(), this method
535 * will incrment @lock_count according to the lock handle amount
536 * actually written to the buffer. */
537 dlm->lock_count = LDLM_ENQUEUE_CANCEL_OFF;
538 ldlm_cli_cancel_list(cancels, count, req, DLM_LOCKREQ_OFF, 0);
540 ldlm_lock_list_put(cancels, l_bl_ast, count);
545 /* If a request has some specific initialisation it is passed in @reqp,
546 * otherwise it is created in ldlm_cli_enqueue.
548 * Supports sync and async requests, pass @async flag accordingly. If a
549 * request was created in ldlm_cli_enqueue and it is the async request,
550 * pass it to the caller in @reqp. */
551 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
552 struct ldlm_enqueue_info *einfo,
553 const struct ldlm_res_id *res_id,
554 ldlm_policy_data_t *policy, int *flags,
555 void *lvb, __u32 lvb_len, void *lvb_swabber,
556 struct lustre_handle *lockh, int async)
558 struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
559 struct ldlm_lock *lock;
560 struct ldlm_request *body;
561 struct ldlm_reply *reply;
562 int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
563 [DLM_LOCKREQ_OFF] = sizeof(*body),
564 [DLM_REPLY_REC_OFF] = lvb_len };
565 int is_replay = *flags & LDLM_FL_REPLAY;
566 int req_passed_in = 1, rc, err;
567 struct ptlrpc_request *req;
570 LASSERT(exp != NULL);
572 /* If we're replaying this lock, just check some invariants.
573 * If we're creating a new lock, get everything all setup nice. */
575 lock = ldlm_handle2lock(lockh);
576 LASSERT(lock != NULL);
577 LDLM_DEBUG(lock, "client-side enqueue START");
578 LASSERT(exp == lock->l_conn_export);
580 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
581 einfo->ei_mode, einfo->ei_cb_bl,
582 einfo->ei_cb_cp, einfo->ei_cb_gl,
583 einfo->ei_cbdata, lvb_len);
586 /* for the local lock, add the reference */
587 ldlm_lock_addref_internal(lock, einfo->ei_mode);
588 ldlm_lock2handle(lock, lockh);
589 lock->l_lvb_swabber = lvb_swabber;
590 if (policy != NULL) {
591 /* INODEBITS_INTEROP: If the server does not support
592 * inodebits, we will request a plain lock in the
593 * descriptor (ldlm_lock2desc() below) but use an
594 * inodebits lock internally with both bits set.
596 if (einfo->ei_type == LDLM_IBITS &&
597 !(exp->exp_connect_flags & OBD_CONNECT_IBITS))
598 lock->l_policy_data.l_inodebits.bits =
599 MDS_INODELOCK_LOOKUP |
600 MDS_INODELOCK_UPDATE;
602 lock->l_policy_data = *policy;
605 if (einfo->ei_type == LDLM_EXTENT)
606 lock->l_req_extent = policy->l_extent;
607 LDLM_DEBUG(lock, "client-side enqueue START");
610 /* lock not sent to server yet */
612 if (reqp == NULL || *reqp == NULL) {
613 req = ldlm_prep_enqueue_req(exp, 2, size, NULL, 0);
615 failed_lock_cleanup(ns, lock, lockh, einfo->ei_mode);
624 LASSERTF(lustre_msg_buflen(req->rq_reqmsg, DLM_LOCKREQ_OFF) >=
625 sizeof(*body), "buflen[%d] = %d, not "LPSZ"\n",
627 lustre_msg_buflen(req->rq_reqmsg, DLM_LOCKREQ_OFF),
631 lock->l_conn_export = exp;
632 lock->l_export = NULL;
633 lock->l_blocking_ast = einfo->ei_cb_bl;
635 /* Dump lock data into the request buffer */
636 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
637 ldlm_lock2desc(lock, &body->lock_desc);
638 body->lock_flags = *flags;
639 body->lock_handle[0] = *lockh;
641 /* Continue as normal. */
642 if (!req_passed_in) {
643 size[DLM_LOCKREPLY_OFF] = sizeof(*reply);
644 ptlrpc_req_set_repsize(req, 2 + (lvb_len > 0), size);
648 * Liblustre client doesn't get extent locks, except for O_APPEND case
649 * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
650 * [i_size, OBD_OBJECT_EOF] lock is taken.
652 LASSERT(ergo(LIBLUSTRE_CLIENT, einfo->ei_type != LDLM_EXTENT ||
653 policy->l_extent.end == OBD_OBJECT_EOF));
656 LASSERT(reqp != NULL);
660 LDLM_DEBUG(lock, "sending request");
661 rc = ptlrpc_queue_wait(req);
662 err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
663 einfo->ei_mode, flags, lvb, lvb_len,
664 lvb_swabber, lockh, rc);
666 /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
667 * one reference that we took */
673 if (!req_passed_in && req != NULL) {
674 ptlrpc_req_finished(req);
682 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
685 struct ldlm_resource *res;
688 if (lock->l_resource->lr_namespace->ns_client) {
689 CERROR("Trying to cancel local lock\n");
692 LDLM_DEBUG(lock, "client-side local convert");
694 res = ldlm_lock_convert(lock, new_mode, flags);
696 ldlm_reprocess_all(res);
701 LDLM_DEBUG(lock, "client-side local convert handler END");
706 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
707 * conversion of locks which are on the waiting or converting queue */
708 /* Caller of this code is supposed to take care of lock readers/writers
710 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
712 struct ldlm_request *body;
713 struct ldlm_reply *reply;
714 struct ldlm_lock *lock;
715 struct ldlm_resource *res;
716 struct ptlrpc_request *req;
717 int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
718 [DLM_LOCKREQ_OFF] = sizeof(*body) };
722 lock = ldlm_handle2lock(lockh);
729 if (lock->l_conn_export == NULL)
730 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
732 LDLM_DEBUG(lock, "client-side convert");
734 req = ptlrpc_prep_req(class_exp2cliimp(lock->l_conn_export),
735 LUSTRE_DLM_VERSION, LDLM_CONVERT, 2, size, NULL);
737 GOTO(out, rc = -ENOMEM);
739 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
740 body->lock_handle[0] = lock->l_remote_handle;
742 body->lock_desc.l_req_mode = new_mode;
743 body->lock_flags = *flags;
745 size[DLM_LOCKREPLY_OFF] = sizeof(*reply);
746 ptlrpc_req_set_repsize(req, 2, size);
748 rc = ptlrpc_queue_wait(req);
752 reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF, sizeof(*reply),
753 lustre_swab_ldlm_reply);
755 CERROR ("Can't unpack ldlm_reply\n");
756 GOTO (out, rc = -EPROTO);
760 GOTO(out, rc = req->rq_status);
762 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
764 ldlm_reprocess_all(res);
765 /* Go to sleep until the lock is granted. */
766 /* FIXME: or cancelled. */
767 if (lock->l_completion_ast) {
768 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
779 ptlrpc_req_finished(req);
783 /* Cancel locks locally.
785 * LDLM_FL_LOCAL_ONLY if tere is no need in a CANCEL rpc to the server;
786 * LDLM_FL_CANCELING otherwise;
787 * LDLM_FL_BL_AST if there is a need in a separate CANCEL rpc. */
788 static int ldlm_cli_cancel_local(struct ldlm_lock *lock)
790 int rc = LDLM_FL_LOCAL_ONLY;
793 if (lock->l_conn_export) {
796 LDLM_DEBUG(lock, "client-side cancel");
797 /* Set this flag to prevent others from getting new references*/
798 lock_res_and_lock(lock);
799 lock->l_flags |= LDLM_FL_CBPENDING;
800 local_only = (lock->l_flags &
801 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
802 ldlm_cancel_callback(lock);
803 rc = (lock->l_flags & LDLM_FL_BL_AST) ?
804 LDLM_FL_BL_AST : LDLM_FL_CANCELING;
805 unlock_res_and_lock(lock);
808 CDEBUG(D_DLMTRACE, "not sending request (at caller's "
810 rc = LDLM_FL_LOCAL_ONLY;
812 ldlm_lock_cancel(lock);
814 if (lock->l_resource->lr_namespace->ns_client) {
815 LDLM_ERROR(lock, "Trying to cancel local lock");
818 LDLM_DEBUG(lock, "server-side local cancel");
819 ldlm_lock_cancel(lock);
820 ldlm_reprocess_all(lock->l_resource);
821 LDLM_DEBUG(lock, "server-side local cancel handler END");
827 /* Pack @count locks in @head into ldlm_request buffer at the offset @off,
828 of the request @req. */
829 static void ldlm_cancel_pack(struct ptlrpc_request *req, int off,
830 struct list_head *head, int count)
832 struct ldlm_request *dlm;
833 struct ldlm_lock *lock;
837 dlm = lustre_msg_buf(req->rq_reqmsg, off, sizeof(*dlm));
838 LASSERT(dlm != NULL);
840 /* Check the room in the request buffer. */
841 max = lustre_msg_buflen(req->rq_reqmsg, off) -
842 sizeof(struct ldlm_request);
843 max /= sizeof(struct lustre_handle);
844 max += LDLM_LOCKREQ_HANDLES;
845 LASSERT(max >= dlm->lock_count + count);
847 /* XXX: it would be better to pack lock handles grouped by resource.
848 * so that the server cancel would call filter_lvbo_update() less
850 list_for_each_entry(lock, head, l_bl_ast) {
853 LASSERT(lock->l_conn_export);
854 /* Pack the lock handle to the given request buffer. */
855 LDLM_DEBUG(lock, "packing");
856 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
859 CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
863 /* Prepare and send a batched cancel rpc, it will include count lock handles
864 * of locks given in @head. */
865 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
866 int count, int flags)
868 struct ptlrpc_request *req = NULL;
869 struct ldlm_request *body;
870 int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
871 [DLM_LOCKREQ_OFF] = sizeof(*body) };
872 struct obd_import *imp;
877 LASSERT(exp != NULL);
880 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
883 free = ldlm_req_handles_avail(exp, size, 2, 0);
887 size[DLM_LOCKREQ_OFF] = ldlm_request_bufsize(count, LDLM_CANCEL);
889 imp = class_exp2cliimp(exp);
890 if (imp == NULL || imp->imp_invalid) {
891 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
896 req = ptlrpc_prep_req(imp, LUSTRE_DLM_VERSION, LDLM_CANCEL, 2,
899 GOTO(out, rc = -ENOMEM);
901 req->rq_no_resend = 1;
902 req->rq_no_delay = 1;
904 /* XXX FIXME bug 249 */
905 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
906 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
908 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF,
910 ldlm_cancel_pack(req, DLM_LOCKREQ_OFF, cancels, count);
912 ptlrpc_req_set_repsize(req, 1, NULL);
913 if (flags & LDLM_FL_ASYNC) {
914 ptlrpcd_add_req(req);
918 rc = ptlrpc_queue_wait(req);
921 CDEBUG(D_DLMTRACE, "client/server (nid %s) "
922 "out of sync -- not fatal\n",
923 libcfs_nid2str(req->rq_import->
924 imp_connection->c_peer.nid));
926 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
927 req->rq_import_generation == imp->imp_generation) {
928 ptlrpc_req_finished(req);
930 } else if (rc != ELDLM_OK) {
931 CERROR("Got rc %d from cancel RPC: canceling "
939 ptlrpc_req_finished(req);
942 return sent ? sent : rc;
945 int ldlm_cli_cancel(struct lustre_handle *lockh)
947 struct ldlm_lock *lock;
948 CFS_LIST_HEAD(cancels);
952 /* concurrent cancels on the same handle can happen */
953 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
955 LDLM_DEBUG_NOLOCK("lock is already being destroyed\n");
959 rc = ldlm_cli_cancel_local(lock);
960 list_add(&lock->l_bl_ast, &cancels);
962 if (rc == LDLM_FL_BL_AST) {
963 rc = ldlm_cli_cancel_req(lock->l_conn_export, &cancels, 1, 0);
964 } else if (rc == LDLM_FL_CANCELING) {
965 int avail = ldlm_cancel_handles_avail(lock->l_conn_export);
968 count += ldlm_cancel_lru_local(lock->l_resource->lr_namespace,
969 &cancels, 0, avail - 1,
971 ldlm_cli_cancel_list(&cancels, count, NULL, 0, 0);
973 if (rc != LDLM_FL_CANCELING)
975 RETURN(rc < 0 ? rc : 0);
978 /* - Free space in lru for @count new locks,
979 * redundant unused locks are canceled locally;
980 * - also cancel locally unused aged locks;
981 * - do not cancel more than @max locks;
982 * - GET the found locks and add them into the @cancels list.
984 * A client lock can be added to the l_bl_ast list only when it is
985 * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing CANCEL.
986 * There are the following use cases: ldlm_cancel_resource_local(),
987 * ldlm_cancel_lru_local() and ldlm_cli_cancel(), which check&set this
988 * flag properly. As any attempt to cancel a lock rely on this flag,
989 * l_bl_ast list is accessed later without any special locking. */
990 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
991 int count, int max, int flags)
993 cfs_time_t cur = cfs_time_current();
994 struct ldlm_lock *lock, *next;
995 int rc, added = 0, left;
998 spin_lock(&ns->ns_unused_lock);
999 count += ns->ns_nr_unused - ns->ns_max_unused;
1000 while (!list_empty(&ns->ns_unused_list)) {
1001 if (max && added >= max)
1004 list_for_each_entry(lock, &ns->ns_unused_list, l_lru) {
1005 /* somebody is already doing CANCEL or there is a
1006 * blocking request will send cancel. */
1007 if (!(lock->l_flags & LDLM_FL_CANCELING) &&
1008 !(lock->l_flags & LDLM_FL_BL_AST))
1011 if (&lock->l_lru == &ns->ns_unused_list)
1014 if ((added >= count) &&
1015 (!(flags & LDLM_CANCEL_AGED) ||
1016 cfs_time_before_64(cur, (__u64)ns->ns_max_age +
1017 lock->l_last_used)))
1020 LDLM_LOCK_GET(lock); /* dropped by bl thread */
1021 spin_unlock(&ns->ns_unused_lock);
1023 lock_res_and_lock(lock);
1024 /* Check flags again under the lock. */
1025 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1026 (lock->l_flags & LDLM_FL_BL_AST) ||
1027 (ldlm_lock_remove_from_lru(lock) == 0)) {
1028 /* other thread is removing lock from lru or
1029 * somebody is already doing CANCEL or
1030 * there is a blocking request which will send
1031 * cancel by itseft. */
1032 unlock_res_and_lock(lock);
1033 LDLM_LOCK_PUT(lock);
1034 spin_lock(&ns->ns_unused_lock);
1037 LASSERT(!lock->l_readers && !lock->l_writers);
1039 /* If we have chosen to canecl this lock voluntarily, we better
1040 send cancel notification to server, so that it frees
1041 appropriate state. This might lead to a race where while
1042 we are doing cancel here, server is also silently
1043 cancelling this lock. */
1044 lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
1046 /* Setting the CBPENDING flag is a little misleading, but
1047 * prevents an important race; namely, once CBPENDING is set,
1048 * the lock can accumulate no more readers/writers. Since
1049 * readers and writers are already zero here, ldlm_lock_decref
1050 * won't see this flag and call l_blocking_ast */
1051 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1052 /* We can't re-add to l_lru as it confuses the refcounting in
1053 * ldlm_lock_remove_from_lru() if an AST arrives after we drop
1054 * ns_lock below. We use l_bl_ast and can't use l_pending_chain
1055 * as it is used both on server and client nevertheles bug 5666
1056 * says it is used only on server. --umka */
1058 LASSERT(list_empty(&lock->l_bl_ast));
1059 list_add(&lock->l_bl_ast, cancels);
1060 unlock_res_and_lock(lock);
1061 spin_lock(&ns->ns_unused_lock);
1064 spin_unlock(&ns->ns_unused_lock);
1066 /* Handle only @added inserted locks. */
1068 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1072 rc = ldlm_cli_cancel_local(lock);
1073 if (rc == LDLM_FL_BL_AST) {
1074 CFS_LIST_HEAD(head);
1076 LDLM_DEBUG(lock, "Cancel lock separately");
1077 list_del_init(&lock->l_bl_ast);
1078 list_add(&lock->l_bl_ast, &head);
1079 ldlm_cli_cancel_req(lock->l_conn_export, &head, 1, 0);
1080 rc = LDLM_FL_LOCAL_ONLY;
1082 if (rc == LDLM_FL_LOCAL_ONLY) {
1083 /* CANCEL RPC should not be sent to server. */
1084 list_del_init(&lock->l_bl_ast);
1085 LDLM_LOCK_PUT(lock);
1093 /* when called with LDLM_ASYNC the blocking callback will be handled
1094 * in a thread and this function will return after the thread has been
1095 * asked to call the callback. when called with LDLM_SYNC the blocking
1096 * callback will be performed in this function. */
1097 int ldlm_cancel_lru(struct ldlm_namespace *ns, ldlm_sync_t sync)
1099 CFS_LIST_HEAD(cancels);
1104 sync = LDLM_SYNC; /* force to be sync in user space */
1106 count = ldlm_cancel_lru_local(ns, &cancels, 0, 0, 0);
1107 if (sync == LDLM_ASYNC) {
1108 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count);
1113 /* If an error occured in ASYNC mode, or
1114 * this is SYNC mode, cancel the list. */
1115 ldlm_cli_cancel_list(&cancels, count, NULL, 0, 0);
1119 /* Find and cancel locally unused locks found on resource, matched to the
1120 * given policy, mode. GET the found locks and add them into the @cancels
1122 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1123 struct list_head *cancels,
1124 ldlm_policy_data_t *policy,
1125 ldlm_mode_t mode, int lock_flags,
1126 int flags, void *opaque)
1128 struct ldlm_lock *lock, *next;
1129 int count = 0, left;
1133 list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1134 if (opaque != NULL && lock->l_ast_data != opaque) {
1135 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1136 lock->l_ast_data, opaque);
1141 if (lock->l_readers || lock->l_writers) {
1142 if (flags & LDLM_FL_WARN) {
1143 LDLM_ERROR(lock, "lock in use");
1149 /* If somebody is already doing CANCEL, or blocking ast came,
1150 * skip this lock. */
1151 if (lock->l_flags & LDLM_FL_BL_AST ||
1152 lock->l_flags & LDLM_FL_CANCELING)
1155 if (lockmode_compat(lock->l_granted_mode, mode))
1158 /* If policy is given and this is IBITS lock, add to list only
1159 * those locks that match by policy. */
1160 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1161 !(lock->l_policy_data.l_inodebits.bits &
1162 policy->l_inodebits.bits))
1165 /* See CBPENDING comment in ldlm_cancel_lru */
1166 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1169 LASSERT(list_empty(&lock->l_bl_ast));
1170 list_add(&lock->l_bl_ast, cancels);
1171 LDLM_LOCK_GET(lock);
1176 /* Handle only @count inserted locks. */
1178 list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1179 int rc = LDLM_FL_LOCAL_ONLY;
1183 if (flags & LDLM_FL_LOCAL_ONLY)
1184 ldlm_lock_cancel(lock);
1186 rc = ldlm_cli_cancel_local(lock);
1188 if (rc == LDLM_FL_BL_AST) {
1189 CFS_LIST_HEAD(head);
1191 LDLM_DEBUG(lock, "Cancel lock separately");
1192 list_del_init(&lock->l_bl_ast);
1193 list_add(&lock->l_bl_ast, &head);
1194 ldlm_cli_cancel_req(lock->l_conn_export, &head, 1, 0);
1195 rc = LDLM_FL_LOCAL_ONLY;
1197 if (rc == LDLM_FL_LOCAL_ONLY) {
1198 /* CANCEL RPC should not be sent to server. */
1199 list_del_init(&lock->l_bl_ast);
1200 LDLM_LOCK_PUT(lock);
1207 /* If @req is NULL, send CANCEL request to server with handles of locks
1208 * in the @cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1209 * separately per lock.
1210 * If @req is not NULL, put handles of locks in @cancels into the request
1211 * buffer at the offset @off.
1212 * Destroy @cancels at the end. */
1213 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1214 struct ptlrpc_request *req, int off, int flags)
1216 struct ldlm_lock *lock;
1220 if (list_empty(cancels) || count == 0)
1223 /* XXX: requests (both batched and not) could be sent in parallel.
1224 * Usually it is enough to have just 1 RPC, but it is possible that
1225 * there are to many locks to be cancelled in LRU or on a resource.
1226 * It would also speed up the case when the server does not support
1229 LASSERT(!list_empty(cancels));
1230 lock = list_entry(cancels->next, struct ldlm_lock, l_bl_ast);
1231 LASSERT(lock->l_conn_export);
1233 if (exp_connect_cancelset(lock->l_conn_export)) {
1236 ldlm_cancel_pack(req, off, cancels, count);
1238 res = ldlm_cli_cancel_req(lock->l_conn_export,
1239 cancels, count, flags);
1241 res = ldlm_cli_cancel_req(lock->l_conn_export,
1246 CERROR("ldlm_cli_cancel_list: %d\n", res);
1251 ldlm_lock_list_put(cancels, l_bl_ast, res);
1253 LASSERT(list_empty(cancels));
1254 LASSERT(count == 0);
1258 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1259 const struct ldlm_res_id *res_id,
1260 ldlm_policy_data_t *policy,
1261 ldlm_mode_t mode, int flags, void *opaque)
1263 struct ldlm_resource *res;
1264 CFS_LIST_HEAD(cancels);
1269 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1271 /* This is not a problem. */
1272 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1276 count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1278 rc = ldlm_cli_cancel_list(&cancels, count, NULL, 0, flags);
1280 CERROR("ldlm_cli_cancel_unused_resource: %d\n", rc);
1282 ldlm_resource_putref(res);
1286 static inline int have_no_nsresource(struct ldlm_namespace *ns)
1288 int no_resource = 0;
1290 spin_lock(&ns->ns_hash_lock);
1291 if (ns->ns_resources == 0)
1293 spin_unlock(&ns->ns_hash_lock);
1295 RETURN(no_resource);
1298 /* Cancel all locks on a namespace (or a specific resource, if given)
1299 * that have 0 readers/writers.
1301 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1302 * to notify the server. */
1303 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
1304 const struct ldlm_res_id *res_id,
1305 int flags, void *opaque)
1314 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
1318 spin_lock(&ns->ns_hash_lock);
1319 for (i = 0; i < RES_HASH_SIZE; i++) {
1320 struct list_head *tmp;
1321 tmp = ns->ns_hash[i].next;
1322 while (tmp != &(ns->ns_hash[i])) {
1323 struct ldlm_resource *res;
1326 res = list_entry(tmp, struct ldlm_resource, lr_hash);
1327 ldlm_resource_getref(res);
1328 spin_unlock(&ns->ns_hash_lock);
1330 rc = ldlm_cli_cancel_unused_resource(ns, &res->lr_name,
1335 CERROR("ldlm_cli_cancel_unused ("LPU64"): %d\n",
1336 res->lr_name.name[0], rc);
1338 spin_lock(&ns->ns_hash_lock);
1340 ldlm_resource_putref_locked(res);
1343 spin_unlock(&ns->ns_hash_lock);
1348 /* join/split resource locks to/from lru list */
1349 int ldlm_cli_join_lru(struct ldlm_namespace *ns,
1350 const struct ldlm_res_id *res_id, int join)
1352 struct ldlm_resource *res;
1353 struct ldlm_lock *lock, *n;
1357 LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT);
1359 res = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
1362 LASSERT(res->lr_type == LDLM_EXTENT);
1368 list_for_each_entry_safe (lock, n, &res->lr_granted, l_res_link) {
1369 if (list_empty(&lock->l_lru) &&
1370 !lock->l_readers && !lock->l_writers &&
1371 !(lock->l_flags & LDLM_FL_LOCAL) &&
1372 !(lock->l_flags & LDLM_FL_CBPENDING)) {
1373 lock->l_last_used = cfs_time_current();
1374 spin_lock(&ns->ns_unused_lock);
1375 LASSERT(ns->ns_nr_unused >= 0);
1376 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
1378 spin_unlock(&ns->ns_unused_lock);
1379 lock->l_flags &= ~LDLM_FL_NO_LRU;
1380 LDLM_DEBUG(lock, "join lock to lru");
1386 spin_lock(&ns->ns_unused_lock);
1387 list_for_each_entry_safe (lock, n, &ns->ns_unused_list, l_lru) {
1388 if (lock->l_resource == res) {
1389 ldlm_lock_remove_from_lru_nolock(lock);
1390 lock->l_flags |= LDLM_FL_NO_LRU;
1391 LDLM_DEBUG(lock, "split lock from lru");
1395 spin_unlock(&ns->ns_unused_lock);
1398 ldlm_resource_putref(res);
1402 /* Lock iterators. */
1404 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1407 struct list_head *tmp, *next;
1408 struct ldlm_lock *lock;
1409 int rc = LDLM_ITER_CONTINUE;
1414 RETURN(LDLM_ITER_CONTINUE);
1417 list_for_each_safe(tmp, next, &res->lr_granted) {
1418 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1420 if (iter(lock, closure) == LDLM_ITER_STOP)
1421 GOTO(out, rc = LDLM_ITER_STOP);
1424 list_for_each_safe(tmp, next, &res->lr_converting) {
1425 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1427 if (iter(lock, closure) == LDLM_ITER_STOP)
1428 GOTO(out, rc = LDLM_ITER_STOP);
1431 list_for_each_safe(tmp, next, &res->lr_waiting) {
1432 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1434 if (iter(lock, closure) == LDLM_ITER_STOP)
1435 GOTO(out, rc = LDLM_ITER_STOP);
1442 struct iter_helper_data {
1443 ldlm_iterator_t iter;
1447 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1449 struct iter_helper_data *helper = closure;
1450 return helper->iter(lock, helper->closure);
1453 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
1455 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
1458 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
1461 struct iter_helper_data helper = { iter: iter, closure: closure };
1462 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
1465 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
1466 ldlm_res_iterator_t iter, void *closure)
1468 int i, rc = LDLM_ITER_CONTINUE;
1469 struct ldlm_resource *res;
1470 struct list_head *tmp;
1473 spin_lock(&ns->ns_hash_lock);
1474 for (i = 0; i < RES_HASH_SIZE; i++) {
1475 tmp = ns->ns_hash[i].next;
1476 while (tmp != &(ns->ns_hash[i])) {
1477 res = list_entry(tmp, struct ldlm_resource, lr_hash);
1478 ldlm_resource_getref(res);
1479 spin_unlock(&ns->ns_hash_lock);
1481 rc = iter(res, closure);
1483 spin_lock(&ns->ns_hash_lock);
1485 ldlm_resource_putref_locked(res);
1486 if (rc == LDLM_ITER_STOP)
1491 spin_unlock(&ns->ns_hash_lock);
1495 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
1496 void ldlm_resource_iterate(struct ldlm_namespace *ns,
1497 const struct ldlm_res_id *res_id,
1498 ldlm_iterator_t iter, void *data)
1500 struct ldlm_resource *res;
1504 CERROR("must pass in namespace\n");
1508 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1514 ldlm_resource_foreach(res, iter, data);
1515 ldlm_resource_putref(res);
1521 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
1523 struct list_head *list = closure;
1525 /* we use l_pending_chain here, because it's unused on clients. */
1526 LASSERTF(list_empty(&lock->l_pending_chain),"lock %p next %p prev %p\n",
1527 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
1528 /* bug 9573: don't replay locks left after eviction */
1529 if (!(lock->l_flags & LDLM_FL_FAILED))
1530 list_add(&lock->l_pending_chain, list);
1531 return LDLM_ITER_CONTINUE;
1534 static int replay_lock_interpret(struct ptlrpc_request *req,
1535 struct ldlm_async_args *aa, int rc)
1537 struct ldlm_lock *lock;
1538 struct ldlm_reply *reply;
1541 atomic_dec(&req->rq_import->imp_replay_inflight);
1546 reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF, sizeof(*reply),
1547 lustre_swab_ldlm_reply);
1548 if (reply == NULL) {
1549 CERROR("Can't unpack ldlm_reply\n");
1550 GOTO (out, rc = -EPROTO);
1553 lock = ldlm_handle2lock(&aa->lock_handle);
1555 CERROR("received replay ack for unknown local cookie "LPX64
1556 " remote cookie "LPX64 " from server %s id %s\n",
1557 aa->lock_handle.cookie, reply->lock_handle.cookie,
1558 req->rq_export->exp_client_uuid.uuid,
1559 libcfs_id2str(req->rq_peer));
1560 GOTO(out, rc = -ESTALE);
1563 lock->l_remote_handle = reply->lock_handle;
1564 LDLM_DEBUG(lock, "replayed lock:");
1565 ptlrpc_import_recovery_state_machine(req->rq_import);
1566 LDLM_LOCK_PUT(lock);
1569 ptlrpc_connect_import(req->rq_import, NULL);
1575 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
1577 struct ptlrpc_request *req;
1578 struct ldlm_request *body;
1579 struct ldlm_reply *reply;
1580 struct ldlm_async_args *aa;
1582 int size[3] = { sizeof(struct ptlrpc_body) };
1587 /* Bug 11974: Do not replay a lock which is actively being canceled */
1588 if (lock->l_flags & LDLM_FL_CANCELING) {
1589 LDLM_DEBUG(lock, "Not replaying canceled lock:");
1593 /* If this is reply-less callback lock, we cannot replay it, since
1594 * server might have long dropped it, but notification of that event was
1595 * lost by network. (and server granted conflicting lock already) */
1596 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1597 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
1598 ldlm_lock_cancel(lock);
1602 * If granted mode matches the requested mode, this lock is granted.
1604 * If they differ, but we have a granted mode, then we were granted
1605 * one mode and now want another: ergo, converting.
1607 * If we haven't been granted anything and are on a resource list,
1608 * then we're blocked/waiting.
1610 * If we haven't been granted anything and we're NOT on a resource list,
1611 * then we haven't got a reply yet and don't have a known disposition.
1612 * This happens whenever a lock enqueue is the request that triggers
1615 if (lock->l_granted_mode == lock->l_req_mode)
1616 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
1617 else if (lock->l_granted_mode)
1618 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
1619 else if (!list_empty(&lock->l_res_link))
1620 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
1622 flags = LDLM_FL_REPLAY;
1624 size[DLM_LOCKREQ_OFF] = sizeof(*body);
1625 req = ptlrpc_prep_req(imp, LUSTRE_DLM_VERSION, LDLM_ENQUEUE, 2, size,
1630 /* We're part of recovery, so don't wait for it. */
1631 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
1633 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
1634 ldlm_lock2desc(lock, &body->lock_desc);
1635 body->lock_flags = flags;
1637 ldlm_lock2handle(lock, &body->lock_handle[0]);
1638 size[DLM_LOCKREPLY_OFF] = sizeof(*reply);
1639 if (lock->l_lvb_len != 0) {
1641 size[DLM_REPLY_REC_OFF] = lock->l_lvb_len;
1643 ptlrpc_req_set_repsize(req, buffers, size);
1644 /* notify the server we've replayed all requests.
1645 * also, we mark the request to be put on a dedicated
1646 * queue to be processed after all request replayes.
1648 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
1650 LDLM_DEBUG(lock, "replaying lock:");
1652 atomic_inc(&req->rq_import->imp_replay_inflight);
1653 CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1654 aa = (struct ldlm_async_args *)&req->rq_async_args;
1655 aa->lock_handle = body->lock_handle[0];
1656 req->rq_interpret_reply = replay_lock_interpret;
1657 ptlrpcd_add_req(req);
1662 int ldlm_replay_locks(struct obd_import *imp)
1664 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
1665 struct list_head list;
1666 struct ldlm_lock *lock, *next;
1670 CFS_INIT_LIST_HEAD(&list);
1672 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1674 /* ensure this doesn't fall to 0 before all have been queued */
1675 atomic_inc(&imp->imp_replay_inflight);
1677 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
1679 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
1680 list_del_init(&lock->l_pending_chain);
1682 continue; /* or try to do the rest? */
1683 rc = replay_one_lock(imp, lock);
1686 atomic_dec(&imp->imp_replay_inflight);