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 Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define DEBUG_SUBSYSTEM S_LDLM
25 #include <liblustre.h>
28 #include <linux/lustre_dlm.h>
29 #include <linux/obd_class.h>
30 #include <linux/obd.h>
32 static int interrupted_completion_wait(void *data)
37 int ldlm_expired_completion_wait(void *data)
39 struct ldlm_lock *lock = data;
40 struct ptlrpc_connection *conn;
41 struct obd_device *obd;
44 CERROR("NULL lock\n");
45 else if (!lock->l_connh)
46 CERROR("lock %p has NULL connh\n", lock);
47 else if (!(obd = class_conn2obd(lock->l_connh)))
48 CERROR("lock %p has NULL obd\n", lock);
49 else if (!(conn = obd->u.cli.cl_import.imp_connection))
50 CERROR("lock %p has NULL connection\n", lock);
52 LDLM_DEBUG(lock, "timed out waiting for completion");
53 CERROR("lock %p timed out from %s\n", lock,
54 conn->c_remote_uuid.uuid);
55 ldlm_lock_dump(D_ERROR, lock);
56 class_signal_connection_failure(conn);
61 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
63 struct l_wait_info lwi =
64 LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
65 interrupted_completion_wait, lock);
69 if (flags == LDLM_FL_WAIT_NOREPROC)
73 wake_up(&lock->l_waitq);
77 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
81 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
83 ldlm_lock_dump(D_OTHER, lock);
84 ldlm_reprocess_all(lock->l_resource);
87 /* Go to sleep until the lock is granted or cancelled. */
88 rc = l_wait_event(lock->l_waitq,
89 ((lock->l_req_mode == lock->l_granted_mode) ||
90 lock->l_destroyed), &lwi);
92 if (lock->l_destroyed) {
93 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
98 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
103 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
107 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
108 struct lustre_handle *parent_lockh,
109 struct ldlm_res_id res_id,
111 void *cookie, int cookielen,
114 ldlm_completion_callback completion,
115 ldlm_blocking_callback blocking,
118 struct lustre_handle *lockh)
120 struct ldlm_lock *lock;
125 CERROR("Trying to enqueue local lock in a shadow namespace\n");
129 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode,
132 GOTO(out_nolock, err = -ENOMEM);
133 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
135 ldlm_lock_addref_internal(lock, mode);
136 ldlm_lock2handle(lock, lockh);
137 lock->l_flags |= LDLM_FL_LOCAL;
139 err = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags, completion,
144 if (type == LDLM_EXTENT)
145 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
146 if ((*flags) & LDLM_FL_LOCK_CHANGED)
147 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
149 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
152 if (lock->l_completion_ast)
153 lock->l_completion_ast(lock, *flags, NULL);
155 LDLM_DEBUG(lock, "client-side local enqueue END");
163 int ldlm_cli_enqueue(struct lustre_handle *connh,
164 struct ptlrpc_request *req,
165 struct ldlm_namespace *ns,
166 struct lustre_handle *parent_lock_handle,
167 struct ldlm_res_id res_id,
169 void *cookie, int cookielen,
172 ldlm_completion_callback completion,
173 ldlm_blocking_callback blocking,
176 struct lustre_handle *lockh)
178 struct ldlm_lock *lock;
179 struct ldlm_request *body;
180 struct ldlm_reply *reply;
181 int rc, size = sizeof(*body), req_passed_in = 1, is_replay;
184 is_replay = *flags & LDLM_FL_REPLAY;
185 LASSERT(connh != NULL || !is_replay);
188 rc = ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
189 type, cookie, cookielen, mode,
190 flags, completion, blocking, data,
195 /* If we're replaying this lock, just check some invariants.
196 * If we're creating a new lock, get everything all setup nice. */
198 lock = ldlm_handle2lock(lockh);
199 LDLM_DEBUG(lock, "client-side enqueue START");
200 LASSERT(connh == lock->l_connh);
202 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type,
203 mode, data, cp_data);
205 GOTO(out_nolock, rc = -ENOMEM);
206 /* ugh. I set this early (instead of waiting for _enqueue)
207 * because the completion AST might arrive early, and we need
208 * (in just this one case) to run the completion_cb even if it
209 * arrives before the reply. */
210 lock->l_completion_ast = completion;
211 LDLM_DEBUG(lock, "client-side enqueue START");
212 /* for the local lock, add the reference */
213 ldlm_lock_addref_internal(lock, mode);
214 ldlm_lock2handle(lock, lockh);
215 if (type == LDLM_EXTENT)
216 memcpy(&lock->l_extent, cookie,
217 sizeof(body->lock_desc.l_extent));
221 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
224 GOTO(out, rc = -ENOMEM);
226 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
229 /* Dump lock data into the request buffer */
230 body = lustre_msg_buf(req->rq_reqmsg, 0);
231 ldlm_lock2desc(lock, &body->lock_desc);
232 body->lock_flags = *flags;
234 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
235 if (parent_lock_handle)
236 memcpy(&body->lock_handle2, parent_lock_handle,
237 sizeof(body->lock_handle2));
239 /* Continue as normal. */
240 if (!req_passed_in) {
241 size = sizeof(*reply);
242 req->rq_replen = lustre_msg_size(1, &size);
244 lock->l_connh = connh;
245 lock->l_export = NULL;
247 LDLM_DEBUG(lock, "sending request");
248 rc = ptlrpc_queue_wait(req);
250 if (rc != ELDLM_OK) {
252 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
253 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
254 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
255 l_lock(&ns->ns_lock);
256 lock->l_flags |= LDLM_FL_CANCELING;
257 l_unlock(&ns->ns_lock);
259 ldlm_lock_decref_and_cancel(lockh, mode);
263 reply = lustre_msg_buf(req->rq_repmsg, 0);
264 memcpy(&lock->l_remote_handle, &reply->lock_handle,
265 sizeof(lock->l_remote_handle));
266 *flags = reply->lock_flags;
268 CDEBUG(D_INFO, "local: %p, remote: %p, flags: %d\n", lock,
269 (void *)(unsigned long)reply->lock_handle.addr, *flags);
270 if (type == LDLM_EXTENT) {
271 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got "
272 "extent "LPU64" -> "LPU64"\n",
273 body->lock_desc.l_extent.start,
274 body->lock_desc.l_extent.end,
275 reply->lock_extent.start, reply->lock_extent.end);
276 cookie = &reply->lock_extent; /* FIXME bug 267 */
277 cookielen = sizeof(reply->lock_extent);
280 /* If enqueue returned a blocked lock but the completion handler has
281 * already run, then it fixed up the resource and we don't need to do it
283 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
284 int newmode = reply->lock_mode;
286 if (newmode && newmode != lock->l_req_mode) {
287 LDLM_DEBUG(lock, "server returned different mode %s",
288 ldlm_lockname[newmode]);
289 lock->l_req_mode = newmode;
292 if (reply->lock_resource_name.name[0] !=
293 lock->l_resource->lr_name.name[0]) {
294 CDEBUG(D_INFO, "remote intent success, locking %ld "
296 (long)reply->lock_resource_name.name[0],
297 (long)lock->l_resource->lr_name.name[0]);
299 ldlm_lock_change_resource(ns, lock,
300 reply->lock_resource_name);
301 if (lock->l_resource == NULL) {
303 GOTO(out_req, rc = -ENOMEM);
305 LDLM_DEBUG(lock, "client-side enqueue, new resource");
310 l_lock(&ns->ns_lock);
311 lock->l_completion_ast = NULL;
312 rc = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags,
313 completion, blocking);
314 l_unlock(&ns->ns_lock);
315 if (lock->l_completion_ast)
316 lock->l_completion_ast(lock, *flags, NULL);
319 LDLM_DEBUG(lock, "client-side enqueue END");
323 ptlrpc_req_finished(req);
330 int ldlm_match_or_enqueue(struct lustre_handle *connh,
331 struct ptlrpc_request *req,
332 struct ldlm_namespace *ns,
333 struct lustre_handle *parent_lock_handle,
334 struct ldlm_res_id res_id,
336 void *cookie, int cookielen,
339 ldlm_completion_callback completion,
340 ldlm_blocking_callback blocking,
343 struct lustre_handle *lockh)
348 /* Just to make sure that I understand things --phil */
349 LASSERT(*flags & LDLM_FL_LOCAL_ONLY);
352 LDLM_DEBUG_NOLOCK("resource "LPU64"/"LPU64, res_id.name[0],
354 rc = ldlm_lock_match(ns, *flags, &res_id, type, cookie, cookielen, mode,
357 rc = ldlm_cli_enqueue(connh, req, ns, parent_lock_handle,
358 res_id, type, cookie, cookielen, mode,
359 flags, completion, blocking, data,
362 CERROR("ldlm_cli_enqueue: err: %d\n", rc);
368 int ldlm_cli_replay_enqueue(struct ldlm_lock *lock)
370 struct lustre_handle lockh;
371 struct ldlm_res_id junk;
372 int flags = LDLM_FL_REPLAY;
373 ldlm_lock2handle(lock, &lockh);
374 return ldlm_cli_enqueue(lock->l_connh, NULL, NULL, NULL, junk,
375 lock->l_resource->lr_type, NULL, 0, -1, &flags,
376 NULL, NULL, NULL, 0, &lockh);
379 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
383 if (lock->l_resource->lr_namespace->ns_client) {
384 CERROR("Trying to cancel local lock\n");
387 LDLM_DEBUG(lock, "client-side local convert");
389 ldlm_lock_convert(lock, new_mode, flags);
390 ldlm_reprocess_all(lock->l_resource);
392 LDLM_DEBUG(lock, "client-side local convert handler END");
397 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
398 * conversion of locks which are on the waiting or converting queue */
399 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
401 struct ldlm_request *body;
402 struct lustre_handle *connh;
403 struct ldlm_reply *reply;
404 struct ldlm_lock *lock;
405 struct ldlm_resource *res;
406 struct ptlrpc_request *req;
407 int rc, size = sizeof(*body);
410 lock = ldlm_handle2lock(lockh);
416 connh = lock->l_connh;
419 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
421 LDLM_DEBUG(lock, "client-side convert");
423 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
426 GOTO(out, rc = -ENOMEM);
428 body = lustre_msg_buf(req->rq_reqmsg, 0);
429 memcpy(&body->lock_handle1, &lock->l_remote_handle,
430 sizeof(body->lock_handle1));
432 body->lock_desc.l_req_mode = new_mode;
433 body->lock_flags = *flags;
435 size = sizeof(*reply);
436 req->rq_replen = lustre_msg_size(1, &size);
438 rc = ptlrpc_queue_wait(req);
442 reply = lustre_msg_buf(req->rq_repmsg, 0);
443 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
445 ldlm_reprocess_all(res);
446 /* Go to sleep until the lock is granted. */
447 /* FIXME: or cancelled. */
448 if (lock->l_completion_ast)
449 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
453 ptlrpc_req_finished(req);
457 int ldlm_cli_cancel(struct lustre_handle *lockh)
459 struct ptlrpc_request *req;
460 struct ldlm_lock *lock;
461 struct ldlm_request *body;
462 int rc = 0, size = sizeof(*body);
465 /* concurrent cancels on the same handle can happen */
466 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
473 LDLM_DEBUG(lock, "client-side cancel");
474 /* Set this flag to prevent others from getting new references*/
475 l_lock(&lock->l_resource->lr_namespace->ns_lock);
476 lock->l_flags |= LDLM_FL_CBPENDING;
477 ldlm_cancel_callback(lock);
478 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
479 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
482 CDEBUG(D_INFO, "not sending request (at caller's "
487 req = ptlrpc_prep_req(class_conn2cliimp(lock->l_connh),
488 LDLM_CANCEL, 1, &size, NULL);
490 GOTO(out, rc = -ENOMEM);
492 /* XXX FIXME bug 249 */
493 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
494 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
496 body = lustre_msg_buf(req->rq_reqmsg, 0);
497 memcpy(&body->lock_handle1, &lock->l_remote_handle,
498 sizeof(body->lock_handle1));
500 req->rq_replen = lustre_msg_size(0, NULL);
502 rc = ptlrpc_queue_wait(req);
503 ptlrpc_req_finished(req);
505 CERROR("client/server out of sync\n");
509 CERROR("Got rc %d from cancel RPC: canceling "
512 ldlm_lock_cancel(lock);
514 LDLM_DEBUG(lock, "client-side local cancel");
515 if (lock->l_resource->lr_namespace->ns_client) {
516 CERROR("Trying to cancel local lock\n");
519 ldlm_lock_cancel(lock);
520 ldlm_reprocess_all(lock->l_resource);
521 LDLM_DEBUG(lock, "client-side local cancel handler END");
530 int ldlm_cancel_lru(struct ldlm_namespace *ns)
532 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
534 struct ldlm_ast_work *w;
537 l_lock(&ns->ns_lock);
538 count = ns->ns_nr_unused - ns->ns_max_unused;
541 l_unlock(&ns->ns_lock);
545 list_for_each_safe(tmp, next, &ns->ns_unused_list) {
546 struct ldlm_lock *lock;
547 lock = list_entry(tmp, struct ldlm_lock, l_lru);
549 LASSERT(!lock->l_readers && !lock->l_writers);
551 /* Setting the CBPENDING flag is a little misleading, but
552 * prevents an important race; namely, once CBPENDING is set,
553 * the lock can accumulate no more readers/writers. Since
554 * readers and writers are already zero here, ldlm_lock_decref
555 * won't see this flag and call l_blocking_ast */
556 lock->l_flags |= LDLM_FL_CBPENDING;
558 OBD_ALLOC(w, sizeof(*w));
561 w->w_lock = LDLM_LOCK_GET(lock);
562 list_add(&w->w_list, &list);
563 ldlm_lock_remove_from_lru(lock);
568 l_unlock(&ns->ns_lock);
570 list_for_each_safe(tmp, next, &list) {
571 struct lustre_handle lockh;
573 w = list_entry(tmp, struct ldlm_ast_work, w_list);
575 ldlm_lock2handle(w->w_lock, &lockh);
576 rc = ldlm_cli_cancel(&lockh);
578 CDEBUG(D_INFO, "ldlm_cli_cancel: %d\n", rc);
580 list_del(&w->w_list);
581 LDLM_LOCK_PUT(w->w_lock);
582 OBD_FREE(w, sizeof(*w));
588 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
589 struct ldlm_res_id res_id, int flags)
591 struct ldlm_resource *res;
592 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
593 struct ldlm_ast_work *w;
596 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
598 /* This is not a problem. */
599 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
603 l_lock(&ns->ns_lock);
604 list_for_each(tmp, &res->lr_granted) {
605 struct ldlm_lock *lock;
606 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
608 if (lock->l_readers || lock->l_writers)
611 /* See CBPENDING comment in ldlm_cancel_lru */
612 lock->l_flags |= LDLM_FL_CBPENDING;
614 OBD_ALLOC(w, sizeof(*w));
617 w->w_lock = LDLM_LOCK_GET(lock);
619 /* Prevent the cancel callback from being called by setting
620 * LDLM_FL_CANCEL in the lock. Very sneaky. -p */
621 if (flags & LDLM_FL_NO_CALLBACK)
622 w->w_lock->l_flags |= LDLM_FL_CANCEL;
624 list_add(&w->w_list, &list);
626 l_unlock(&ns->ns_lock);
628 list_for_each_safe(tmp, next, &list) {
629 struct lustre_handle lockh;
631 w = list_entry(tmp, struct ldlm_ast_work, w_list);
633 if (flags & LDLM_FL_LOCAL_ONLY) {
634 ldlm_lock_cancel(w->w_lock);
636 ldlm_lock2handle(w->w_lock, &lockh);
637 rc = ldlm_cli_cancel(&lockh);
639 CERROR("ldlm_cli_cancel: %d\n", rc);
641 list_del(&w->w_list);
642 LDLM_LOCK_PUT(w->w_lock);
643 OBD_FREE(w, sizeof(*w));
646 ldlm_resource_putref(res);
651 /* Cancel all locks on a namespace (or a specific resource, if given)
652 * that have 0 readers/writers.
654 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
655 * to notify the server.
656 * If flags & LDLM_FL_NO_CALLBACK, don't run the cancel callback. */
657 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
658 struct ldlm_res_id *res_id, int flags)
667 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags));
669 l_lock(&ns->ns_lock);
670 for (i = 0; i < RES_HASH_SIZE; i++) {
671 struct list_head *tmp, *pos;
672 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
674 struct ldlm_resource *res;
675 res = list_entry(tmp, struct ldlm_resource, lr_hash);
676 ldlm_resource_getref(res);
678 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
682 CERROR("cancel_unused_res ("LPU64"): %d\n",
683 res->lr_name.name[0], rc);
684 ldlm_resource_putref(res);
687 l_unlock(&ns->ns_lock);
692 /* Lock iterators. */
694 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
697 struct list_head *tmp, *next;
698 struct ldlm_lock *lock;
699 int rc = LDLM_ITER_CONTINUE;
700 struct ldlm_namespace *ns = res->lr_namespace;
705 RETURN(LDLM_ITER_CONTINUE);
707 l_lock(&ns->ns_lock);
708 list_for_each_safe(tmp, next, &res->lr_granted) {
709 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
711 if (iter(lock, closure) == LDLM_ITER_STOP)
712 GOTO(out, rc = LDLM_ITER_STOP);
715 list_for_each_safe(tmp, next, &res->lr_converting) {
716 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
718 if (iter(lock, closure) == LDLM_ITER_STOP)
719 GOTO(out, rc = LDLM_ITER_STOP);
722 list_for_each_safe(tmp, next, &res->lr_waiting) {
723 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
725 if (iter(lock, closure) == LDLM_ITER_STOP)
726 GOTO(out, rc = LDLM_ITER_STOP);
729 l_unlock(&ns->ns_lock);
733 struct iter_helper_data {
734 ldlm_iterator_t iter;
738 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
740 struct iter_helper_data *helper = closure;
741 return helper->iter(lock, helper->closure);
744 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
746 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
749 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
752 struct iter_helper_data helper = { iter: iter, closure: closure };
753 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
756 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
757 ldlm_res_iterator_t iter, void *closure)
759 int i, rc = LDLM_ITER_CONTINUE;
761 l_lock(&ns->ns_lock);
762 for (i = 0; i < RES_HASH_SIZE; i++) {
763 struct list_head *tmp, *next;
764 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
765 struct ldlm_resource *res =
766 list_entry(tmp, struct ldlm_resource, lr_hash);
768 ldlm_resource_getref(res);
769 rc = iter(res, closure);
770 ldlm_resource_putref(res);
771 if (rc == LDLM_ITER_STOP)
776 l_unlock(&ns->ns_lock);
782 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
784 struct list_head *list = closure;
786 /* we use l_pending_chain here, because it's unused on clients. */
787 list_add(&lock->l_pending_chain, list);
788 return LDLM_ITER_CONTINUE;
791 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
793 struct ptlrpc_request *req;
794 struct ldlm_request *body;
795 struct ldlm_reply *reply;
800 * If granted mode matches the requested mode, this lock is granted.
802 * If they differ, but we have a granted mode, then we were granted
803 * one mode and now want another: ergo, converting.
805 * If we haven't been granted anything and are on a resource list,
806 * then we're blocked/waiting.
808 * If we haven't been granted anything and we're NOT on a resource list,
809 * then we haven't got a reply yet and don't have a known disposition.
810 * This happens whenever a lock enqueue is the request that triggers
813 if (lock->l_granted_mode == lock->l_req_mode)
814 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
815 else if (lock->l_granted_mode)
816 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
817 else if (!list_empty(&lock->l_res_link))
818 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
820 flags = LDLM_FL_REPLAY;
822 size = sizeof(*body);
823 req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, &size, NULL);
827 /* We're part of recovery, so don't wait for it. */
828 req->rq_level = LUSTRE_CONN_RECOVD;
830 body = lustre_msg_buf(req->rq_reqmsg, 0);
831 ldlm_lock2desc(lock, &body->lock_desc);
832 body->lock_flags = flags;
834 ldlm_lock2handle(lock, &body->lock_handle1);
835 size = sizeof(*reply);
836 req->rq_replen = lustre_msg_size(1, &size);
838 LDLM_DEBUG(lock, "replaying lock:");
839 rc = ptlrpc_queue_wait(req);
843 reply = lustre_msg_buf(req->rq_repmsg, 0);
844 memcpy(&lock->l_remote_handle, &reply->lock_handle,
845 sizeof(lock->l_remote_handle));
846 LDLM_DEBUG(lock, "replayed lock:");
848 ptlrpc_req_finished(req);
852 int ldlm_replay_locks(struct obd_import *imp)
854 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
855 struct list_head list, *pos, *next;
856 struct ldlm_lock *lock;
860 INIT_LIST_HEAD(&list);
862 l_lock(&ns->ns_lock);
863 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
865 list_for_each_safe(pos, next, &list) {
866 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
867 rc = replay_one_lock(imp, lock);
869 break; /* or try to do the rest? */
871 l_unlock(&ns->ns_lock);