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 #include "ldlm_internal.h"
34 static void interrupted_completion_wait(void *data)
38 struct lock_wait_data {
39 struct ldlm_lock *lwd_lock;
43 int ldlm_expired_completion_wait(void *data)
45 struct lock_wait_data *lwd = data;
46 struct ldlm_lock *lock = lwd->lwd_lock;
47 struct obd_device *obd = class_conn2obd(lock->l_connh);
50 LDLM_ERROR(lock, "lock timed out; mot entering recovery in "
51 "server code, just going back to sleep");
53 struct obd_import *imp = obd->u.cli.cl_import;
54 ptlrpc_fail_import(imp, lwd->lwd_generation);
55 LDLM_ERROR(lock, "lock timed out, entering recovery for %s@%s",
56 imp->imp_target_uuid.uuid,
57 imp->imp_connection->c_remote_uuid.uuid);
63 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
65 /* XXX ALLOCATE - 160 mytes */
66 struct lock_wait_data lwd;
67 unsigned long irqflags;
68 struct obd_device *obd;
69 struct obd_import *imp = NULL;
71 struct l_wait_info lwi;
73 obd = class_conn2obd(lock->l_connh);
75 /* if this is a local lock, then there is no import */
77 imp = obd->u.cli.cl_import;
81 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
82 interrupted_completion_wait, &lwd);
85 if (flags == LDLM_FL_WAIT_NOREPROC)
89 wake_up(&lock->l_waitq);
93 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
97 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
99 ldlm_lock_dump(D_OTHER, lock, 0);
100 ldlm_reprocess_all(lock->l_resource);
104 spin_lock_irqsave(&imp->imp_lock, irqflags);
105 lwd.lwd_generation = imp->imp_generation;
106 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
109 /* Go to sleep until the lock is granted or cancelled. */
110 rc = l_wait_event(lock->l_waitq,
111 ((lock->l_req_mode == lock->l_granted_mode) ||
112 lock->l_destroyed), &lwi);
114 if (lock->l_destroyed) {
115 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
120 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
125 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
129 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
130 struct lustre_handle *parent_lockh,
131 struct ldlm_res_id res_id,
133 void *cookie, int cookielen,
136 ldlm_completion_callback completion,
137 ldlm_blocking_callback blocking,
139 struct lustre_handle *lockh)
141 struct ldlm_lock *lock;
146 CERROR("Trying to enqueue local lock in a shadow namespace\n");
150 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode,
151 blocking, completion, data);
153 GOTO(out_nolock, err = -ENOMEM);
154 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
156 ldlm_lock_addref_internal(lock, mode);
157 ldlm_lock2handle(lock, lockh);
158 lock->l_flags |= LDLM_FL_LOCAL;
160 err = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags);
164 if (type == LDLM_EXTENT)
165 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
166 if ((*flags) & LDLM_FL_LOCK_CHANGED)
167 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
169 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
172 if (lock->l_completion_ast)
173 lock->l_completion_ast(lock, *flags, NULL);
175 LDLM_DEBUG(lock, "client-side local enqueue END");
183 int ldlm_cli_enqueue(struct lustre_handle *connh,
184 struct ptlrpc_request *req,
185 struct ldlm_namespace *ns,
186 struct lustre_handle *parent_lock_handle,
187 struct ldlm_res_id res_id,
189 void *cookie, int cookielen,
192 ldlm_completion_callback completion,
193 ldlm_blocking_callback blocking,
195 struct lustre_handle *lockh)
197 struct ldlm_lock *lock;
198 struct ldlm_request *body;
199 struct ldlm_reply *reply;
200 int rc, size = sizeof(*body), req_passed_in = 1, is_replay;
203 is_replay = *flags & LDLM_FL_REPLAY;
204 LASSERT(connh != NULL || !is_replay);
207 rc = ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
208 type, cookie, cookielen, mode,
209 flags, completion, blocking, data,
214 /* If we're replaying this lock, just check some invariants.
215 * If we're creating a new lock, get everything all setup nice. */
217 lock = ldlm_handle2lock(lockh);
218 LDLM_DEBUG(lock, "client-side enqueue START");
219 LASSERT(connh == lock->l_connh);
221 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type,
222 mode, blocking, completion, data);
224 GOTO(out_nolock, rc = -ENOMEM);
225 LDLM_DEBUG(lock, "client-side enqueue START");
226 /* for the local lock, add the reference */
227 ldlm_lock_addref_internal(lock, mode);
228 ldlm_lock2handle(lock, lockh);
229 if (type == LDLM_EXTENT)
230 memcpy(&lock->l_extent, cookie,
231 sizeof(body->lock_desc.l_extent));
235 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
238 GOTO(out, rc = -ENOMEM);
240 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
243 /* Dump lock data into the request buffer */
244 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
245 ldlm_lock2desc(lock, &body->lock_desc);
246 body->lock_flags = *flags;
248 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
249 if (parent_lock_handle)
250 memcpy(&body->lock_handle2, parent_lock_handle,
251 sizeof(body->lock_handle2));
253 /* Continue as normal. */
254 if (!req_passed_in) {
255 size = sizeof(*reply);
256 req->rq_replen = lustre_msg_size(1, &size);
258 lock->l_connh = connh;
259 lock->l_export = NULL;
260 lock->l_blocking_ast = blocking;
262 LDLM_DEBUG(lock, "sending request");
263 rc = ptlrpc_queue_wait(req);
265 if (rc != ELDLM_OK) {
267 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
268 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
269 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
270 l_lock(&ns->ns_lock);
271 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
272 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
273 l_unlock(&ns->ns_lock);
275 ldlm_lock_decref_and_cancel(lockh, mode);
277 if (rc == ELDLM_LOCK_ABORTED) {
278 /* caller expects reply buffer 0 to have been swabbed */
279 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
280 lustre_swab_ldlm_reply);
282 CERROR ("Can't unpack ldlm_reply\n");
283 GOTO (out_req, rc = -EPROTO);
289 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
290 lustre_swab_ldlm_reply);
292 CERROR ("Can't unpack ldlm_reply\n");
293 GOTO (out_req, rc = -EPROTO);
296 memcpy(&lock->l_remote_handle, &reply->lock_handle,
297 sizeof(lock->l_remote_handle));
298 *flags = reply->lock_flags;
300 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: %d\n", lock,
301 reply->lock_handle.cookie, *flags);
302 if (type == LDLM_EXTENT) {
303 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got "
304 "extent "LPU64" -> "LPU64"\n",
305 body->lock_desc.l_extent.start,
306 body->lock_desc.l_extent.end,
307 reply->lock_extent.start, reply->lock_extent.end);
309 cookie = &reply->lock_extent; /* FIXME bug 267 */
310 cookielen = sizeof(reply->lock_extent);
313 /* If enqueue returned a blocked lock but the completion handler has
314 * already run, then it fixed up the resource and we don't need to do it
316 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
317 int newmode = reply->lock_mode;
319 if (newmode && newmode != lock->l_req_mode) {
320 LDLM_DEBUG(lock, "server returned different mode %s",
321 ldlm_lockname[newmode]);
322 lock->l_req_mode = newmode;
325 if (reply->lock_resource_name.name[0] !=
326 lock->l_resource->lr_name.name[0]) {
327 CDEBUG(D_INFO, "remote intent success, locking %ld "
329 (long)reply->lock_resource_name.name[0],
330 (long)lock->l_resource->lr_name.name[0]);
332 ldlm_lock_change_resource(ns, lock,
333 reply->lock_resource_name);
334 if (lock->l_resource == NULL) {
336 GOTO(out_req, rc = -ENOMEM);
338 LDLM_DEBUG(lock, "client-side enqueue, new resource");
343 rc = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags);
344 if (lock->l_completion_ast)
345 lock->l_completion_ast(lock, *flags, NULL);
348 LDLM_DEBUG(lock, "client-side enqueue END");
352 ptlrpc_req_finished(req);
359 int ldlm_cli_replay_enqueue(struct ldlm_lock *lock)
361 struct lustre_handle lockh;
362 struct ldlm_res_id junk;
363 int flags = LDLM_FL_REPLAY;
364 ldlm_lock2handle(lock, &lockh);
365 return ldlm_cli_enqueue(lock->l_connh, NULL, NULL, NULL, junk,
366 lock->l_resource->lr_type, NULL, 0, -1, &flags,
367 NULL, NULL, NULL, &lockh);
370 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
374 if (lock->l_resource->lr_namespace->ns_client) {
375 CERROR("Trying to cancel local lock\n");
378 LDLM_DEBUG(lock, "client-side local convert");
380 ldlm_lock_convert(lock, new_mode, flags);
381 ldlm_reprocess_all(lock->l_resource);
383 LDLM_DEBUG(lock, "client-side local convert handler END");
388 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
389 * conversion of locks which are on the waiting or converting queue */
390 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
392 struct ldlm_request *body;
393 struct lustre_handle *connh;
394 struct ldlm_reply *reply;
395 struct ldlm_lock *lock;
396 struct ldlm_resource *res;
397 struct ptlrpc_request *req;
398 int rc, size = sizeof(*body);
401 lock = ldlm_handle2lock(lockh);
407 connh = lock->l_connh;
410 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
412 LDLM_DEBUG(lock, "client-side convert");
414 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
417 GOTO(out, rc = -ENOMEM);
419 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
420 memcpy(&body->lock_handle1, &lock->l_remote_handle,
421 sizeof(body->lock_handle1));
423 body->lock_desc.l_req_mode = new_mode;
424 body->lock_flags = *flags;
426 size = sizeof(*reply);
427 req->rq_replen = lustre_msg_size(1, &size);
429 rc = ptlrpc_queue_wait(req);
433 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
434 lustre_swab_ldlm_reply);
436 CERROR ("Can't unpack ldlm_reply\n");
437 GOTO (out, rc = -EPROTO);
440 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
442 ldlm_reprocess_all(res);
443 /* Go to sleep until the lock is granted. */
444 /* FIXME: or cancelled. */
445 if (lock->l_completion_ast)
446 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
450 ptlrpc_req_finished(req);
454 int ldlm_cli_cancel(struct lustre_handle *lockh)
456 struct ptlrpc_request *req;
457 struct ldlm_lock *lock;
458 struct ldlm_request *body;
459 int rc = 0, size = sizeof(*body);
462 /* concurrent cancels on the same handle can happen */
463 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
469 struct obd_import *imp;
471 LDLM_DEBUG(lock, "client-side cancel");
472 /* Set this flag to prevent others from getting new references*/
473 l_lock(&lock->l_resource->lr_namespace->ns_lock);
474 lock->l_flags |= LDLM_FL_CBPENDING;
475 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
476 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
477 ldlm_cancel_callback(lock);
480 CDEBUG(D_INFO, "not sending request (at caller's "
485 imp = class_conn2cliimp(lock->l_connh);
486 if (imp == NULL || imp->imp_invalid) {
487 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
492 req = ptlrpc_prep_req(imp, LDLM_CANCEL, 1, &size, NULL);
494 GOTO(out, rc = -ENOMEM);
496 /* XXX FIXME bug 249 */
497 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
498 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
500 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
501 memcpy(&body->lock_handle1, &lock->l_remote_handle,
502 sizeof(body->lock_handle1));
504 req->rq_replen = lustre_msg_size(0, NULL);
506 rc = ptlrpc_queue_wait(req);
509 CERROR("client/server (nid "LPU64") out of sync--not "
511 req->rq_import->imp_connection->c_peer.peer_nid);
512 else if (rc != ELDLM_OK)
513 CERROR("Got rc %d from cancel RPC: canceling "
516 ptlrpc_req_finished(req);
518 ldlm_lock_cancel(lock);
520 if (lock->l_resource->lr_namespace->ns_client) {
521 LDLM_ERROR(lock, "Trying to cancel local lock\n");
524 LDLM_DEBUG(lock, "client-side local cancel");
525 ldlm_lock_cancel(lock);
526 ldlm_reprocess_all(lock->l_resource);
527 LDLM_DEBUG(lock, "client-side local cancel handler END");
536 int ldlm_cancel_lru(struct ldlm_namespace *ns)
538 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
540 struct ldlm_ast_work *w;
543 l_lock(&ns->ns_lock);
544 count = ns->ns_nr_unused - ns->ns_max_unused;
547 l_unlock(&ns->ns_lock);
551 list_for_each_safe(tmp, next, &ns->ns_unused_list) {
552 struct ldlm_lock *lock;
553 lock = list_entry(tmp, struct ldlm_lock, l_lru);
555 LASSERT(!lock->l_readers && !lock->l_writers);
557 /* Setting the CBPENDING flag is a little misleading, but
558 * prevents an important race; namely, once CBPENDING is set,
559 * the lock can accumulate no more readers/writers. Since
560 * readers and writers are already zero here, ldlm_lock_decref
561 * won't see this flag and call l_blocking_ast */
562 lock->l_flags |= LDLM_FL_CBPENDING;
564 OBD_ALLOC(w, sizeof(*w));
567 w->w_lock = LDLM_LOCK_GET(lock);
568 list_add(&w->w_list, &list);
569 ldlm_lock_remove_from_lru(lock);
574 l_unlock(&ns->ns_lock);
576 list_for_each_safe(tmp, next, &list) {
577 struct lustre_handle lockh;
579 w = list_entry(tmp, struct ldlm_ast_work, w_list);
581 ldlm_lock2handle(w->w_lock, &lockh);
582 rc = ldlm_cli_cancel(&lockh);
584 CDEBUG(D_INFO, "ldlm_cli_cancel: %d\n", rc);
586 list_del(&w->w_list);
587 LDLM_LOCK_PUT(w->w_lock);
588 OBD_FREE(w, sizeof(*w));
594 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
595 struct ldlm_res_id res_id, int flags,
598 struct ldlm_resource *res;
599 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
600 struct ldlm_ast_work *w;
603 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
605 /* This is not a problem. */
606 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
610 l_lock(&ns->ns_lock);
611 list_for_each(tmp, &res->lr_granted) {
612 struct ldlm_lock *lock;
613 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
615 if (opaque != NULL && lock->l_data != opaque) {
616 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
617 lock->l_data, opaque);
622 if (lock->l_readers || lock->l_writers) {
623 if (flags & LDLM_FL_WARN) {
624 LDLM_ERROR(lock, "lock in use");
630 /* See CBPENDING comment in ldlm_cancel_lru */
631 lock->l_flags |= LDLM_FL_CBPENDING;
633 OBD_ALLOC(w, sizeof(*w));
636 w->w_lock = LDLM_LOCK_GET(lock);
638 /* Prevent the cancel callback from being called by setting
639 * LDLM_FL_CANCEL in the lock. Very sneaky. -p */
640 if (flags & LDLM_FL_NO_CALLBACK)
641 w->w_lock->l_flags |= LDLM_FL_CANCEL;
643 list_add(&w->w_list, &list);
645 l_unlock(&ns->ns_lock);
647 list_for_each_safe(tmp, next, &list) {
648 struct lustre_handle lockh;
650 w = list_entry(tmp, struct ldlm_ast_work, w_list);
652 if (flags & LDLM_FL_LOCAL_ONLY) {
653 ldlm_lock_cancel(w->w_lock);
655 ldlm_lock2handle(w->w_lock, &lockh);
656 rc = ldlm_cli_cancel(&lockh);
658 CERROR("ldlm_cli_cancel: %d\n", rc);
660 list_del(&w->w_list);
661 LDLM_LOCK_PUT(w->w_lock);
662 OBD_FREE(w, sizeof(*w));
665 ldlm_resource_putref(res);
670 /* Cancel all locks on a namespace (or a specific resource, if given)
671 * that have 0 readers/writers.
673 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
674 * to notify the server.
675 * If flags & LDLM_FL_NO_CALLBACK, don't run the cancel callback.
676 * If flags & LDLM_FL_WARN, print a warning if some locks are still in use. */
677 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
678 struct ldlm_res_id *res_id, int flags, void *opaque)
687 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
690 l_lock(&ns->ns_lock);
691 for (i = 0; i < RES_HASH_SIZE; i++) {
692 struct list_head *tmp, *pos;
693 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
695 struct ldlm_resource *res;
696 res = list_entry(tmp, struct ldlm_resource, lr_hash);
697 ldlm_resource_getref(res);
699 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
703 CERROR("cancel_unused_res ("LPU64"): %d\n",
704 res->lr_name.name[0], rc);
705 ldlm_resource_putref(res);
708 l_unlock(&ns->ns_lock);
713 /* Lock iterators. */
715 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
718 struct list_head *tmp, *next;
719 struct ldlm_lock *lock;
720 int rc = LDLM_ITER_CONTINUE;
721 struct ldlm_namespace *ns = res->lr_namespace;
726 RETURN(LDLM_ITER_CONTINUE);
728 l_lock(&ns->ns_lock);
729 list_for_each_safe(tmp, next, &res->lr_granted) {
730 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
732 if (iter(lock, closure) == LDLM_ITER_STOP)
733 GOTO(out, rc = LDLM_ITER_STOP);
736 list_for_each_safe(tmp, next, &res->lr_converting) {
737 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
739 if (iter(lock, closure) == LDLM_ITER_STOP)
740 GOTO(out, rc = LDLM_ITER_STOP);
743 list_for_each_safe(tmp, next, &res->lr_waiting) {
744 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
746 if (iter(lock, closure) == LDLM_ITER_STOP)
747 GOTO(out, rc = LDLM_ITER_STOP);
750 l_unlock(&ns->ns_lock);
754 struct iter_helper_data {
755 ldlm_iterator_t iter;
759 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
761 struct iter_helper_data *helper = closure;
762 return helper->iter(lock, helper->closure);
765 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
767 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
770 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
773 struct iter_helper_data helper = { iter: iter, closure: closure };
774 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
777 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
778 ldlm_res_iterator_t iter, void *closure)
780 int i, rc = LDLM_ITER_CONTINUE;
782 l_lock(&ns->ns_lock);
783 for (i = 0; i < RES_HASH_SIZE; i++) {
784 struct list_head *tmp, *next;
785 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
786 struct ldlm_resource *res =
787 list_entry(tmp, struct ldlm_resource, lr_hash);
789 ldlm_resource_getref(res);
790 rc = iter(res, closure);
791 ldlm_resource_putref(res);
792 if (rc == LDLM_ITER_STOP)
797 l_unlock(&ns->ns_lock);
801 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
802 void ldlm_change_cbdata(struct ldlm_namespace *ns,
803 struct ldlm_res_id *res_id,
804 ldlm_iterator_t iter,
807 struct ldlm_resource *res;
811 CERROR("must pass in namespace");
815 res = ldlm_resource_get(ns, NULL, *res_id, 0, 0);
821 l_lock(&ns->ns_lock);
822 ldlm_resource_foreach(res, iter, data);
823 l_unlock(&ns->ns_lock);
824 ldlm_resource_putref(res);
830 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
832 struct list_head *list = closure;
834 /* we use l_pending_chain here, because it's unused on clients. */
835 list_add(&lock->l_pending_chain, list);
836 return LDLM_ITER_CONTINUE;
839 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
841 struct ptlrpc_request *req;
842 struct ldlm_request *body;
843 struct ldlm_reply *reply;
848 * If granted mode matches the requested mode, this lock is granted.
850 * If they differ, but we have a granted mode, then we were granted
851 * one mode and now want another: ergo, converting.
853 * If we haven't been granted anything and are on a resource list,
854 * then we're blocked/waiting.
856 * If we haven't been granted anything and we're NOT on a resource list,
857 * then we haven't got a reply yet and don't have a known disposition.
858 * This happens whenever a lock enqueue is the request that triggers
861 if (lock->l_granted_mode == lock->l_req_mode)
862 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
863 else if (lock->l_granted_mode)
864 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
865 else if (!list_empty(&lock->l_res_link))
866 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
868 flags = LDLM_FL_REPLAY;
870 size = sizeof(*body);
871 req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, &size, NULL);
875 /* We're part of recovery, so don't wait for it. */
876 req->rq_level = LUSTRE_CONN_RECOVER;
878 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
879 ldlm_lock2desc(lock, &body->lock_desc);
880 body->lock_flags = flags;
882 ldlm_lock2handle(lock, &body->lock_handle1);
883 size = sizeof(*reply);
884 req->rq_replen = lustre_msg_size(1, &size);
886 LDLM_DEBUG(lock, "replaying lock:");
887 rc = ptlrpc_queue_wait(req);
891 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
892 lustre_swab_ldlm_reply);
894 CERROR("Can't unpack ldlm_reply\n");
895 GOTO (out, rc = -EPROTO);
898 memcpy(&lock->l_remote_handle, &reply->lock_handle,
899 sizeof(lock->l_remote_handle));
900 LDLM_DEBUG(lock, "replayed lock:");
902 ptlrpc_req_finished(req);
906 int ldlm_replay_locks(struct obd_import *imp)
908 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
909 struct list_head list, *pos, *next;
910 struct ldlm_lock *lock;
914 INIT_LIST_HEAD(&list);
916 l_lock(&ns->ns_lock);
917 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
919 list_for_each_safe(pos, next, &list) {
920 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
921 rc = replay_one_lock(imp, lock);
923 break; /* or try to do the rest? */
925 l_unlock(&ns->ns_lock);