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 static unsigned long next_dump = 0;
46 struct lock_wait_data *lwd = data;
47 struct ldlm_lock *lock = lwd->lwd_lock;
48 struct obd_import *imp;
49 struct obd_device *obd;
51 if (lock->l_conn_export == NULL) {
52 LDLM_ERROR(lock, "lock timed out; not entering recovery in "
53 "server code, just going back to sleep");
57 obd = lock->l_conn_export->exp_obd;
58 imp = obd->u.cli.cl_import;
59 ptlrpc_fail_import(imp, lwd->lwd_generation);
60 LDLM_ERROR(lock, "lock timed out, entering recovery for %s@%s",
61 imp->imp_target_uuid.uuid,
62 imp->imp_connection->c_remote_uuid.uuid);
63 if (time_after(jiffies, next_dump)) {
64 next_dump = jiffies + 300 * HZ;
65 ldlm_namespace_dump(lock->l_resource->lr_namespace);
71 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
73 /* XXX ALLOCATE - 160 mytes */
74 struct lock_wait_data lwd;
75 unsigned long irqflags;
76 struct obd_device *obd;
77 struct obd_import *imp = NULL;
79 struct l_wait_info lwi;
82 if (flags == LDLM_FL_WAIT_NOREPROC)
86 wake_up(&lock->l_waitq);
90 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
94 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
96 ldlm_lock_dump(D_OTHER, lock, 0);
97 ldlm_reprocess_all(lock->l_resource);
101 obd = class_exp2obd(lock->l_conn_export);
103 /* if this is a local lock, then there is no import */
105 imp = obd->u.cli.cl_import;
109 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
110 interrupted_completion_wait, &lwd);
112 spin_lock_irqsave(&imp->imp_lock, irqflags);
113 lwd.lwd_generation = imp->imp_generation;
114 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
117 /* Go to sleep until the lock is granted or cancelled. */
118 rc = l_wait_event(lock->l_waitq,
119 ((lock->l_req_mode == lock->l_granted_mode) ||
120 lock->l_destroyed), &lwi);
122 if (lock->l_destroyed) {
123 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
128 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
133 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
137 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
138 struct lustre_handle *parent_lockh,
139 struct ldlm_res_id res_id,
141 void *cookie, int cookielen,
144 ldlm_completion_callback completion,
145 ldlm_blocking_callback blocking,
147 struct lustre_handle *lockh)
149 struct ldlm_lock *lock;
154 CERROR("Trying to enqueue local lock in a shadow namespace\n");
158 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode,
159 blocking, completion, data);
161 GOTO(out_nolock, err = -ENOMEM);
162 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
164 ldlm_lock_addref_internal(lock, mode);
165 ldlm_lock2handle(lock, lockh);
166 lock->l_flags |= LDLM_FL_LOCAL;
168 err = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags);
172 if (type != LDLM_PLAIN)
173 memcpy(cookie, &lock->l_policy_data, cookielen);
174 if ((*flags) & LDLM_FL_LOCK_CHANGED)
175 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
177 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
180 if (lock->l_completion_ast)
181 lock->l_completion_ast(lock, *flags, NULL);
183 LDLM_DEBUG(lock, "client-side local enqueue END");
191 static void failed_lock_cleanup(struct ldlm_namespace *ns,
192 struct ldlm_lock *lock,
193 struct lustre_handle *lockh, int mode)
195 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
196 l_lock(&ns->ns_lock);
197 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
198 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
199 l_unlock(&ns->ns_lock);
201 ldlm_lock_decref_and_cancel(lockh, mode);
204 int ldlm_cli_enqueue(struct obd_export *exp,
205 struct ptlrpc_request *req,
206 struct ldlm_namespace *ns,
207 struct lustre_handle *parent_lock_handle,
208 struct ldlm_res_id res_id,
210 void *cookie, int cookielen,
213 ldlm_completion_callback completion,
214 ldlm_blocking_callback blocking,
216 struct lustre_handle *lockh)
218 struct ldlm_lock *lock;
219 struct ldlm_request *body;
220 struct ldlm_reply *reply;
221 int rc, size = sizeof(*body), req_passed_in = 1, is_replay;
224 is_replay = *flags & LDLM_FL_REPLAY;
225 LASSERT(exp != NULL || !is_replay);
228 rc = ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
229 type, cookie, cookielen, mode,
230 flags, completion, blocking, data,
235 /* If we're replaying this lock, just check some invariants.
236 * If we're creating a new lock, get everything all setup nice. */
238 lock = ldlm_handle2lock(lockh);
239 LDLM_DEBUG(lock, "client-side enqueue START");
240 LASSERT(exp == lock->l_conn_export);
242 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type,
243 mode, blocking, completion, data);
245 GOTO(out_nolock, rc = -ENOMEM);
246 /* for the local lock, add the reference */
247 ldlm_lock_addref_internal(lock, mode);
248 ldlm_lock2handle(lock, lockh);
249 if (type != LDLM_PLAIN)
250 memcpy(&lock->l_policy_data, cookie, cookielen);
251 LDLM_DEBUG(lock, "client-side enqueue START");
255 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 1,
258 GOTO(out, rc = -ENOMEM);
260 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
263 /* Dump lock data into the request buffer */
264 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
265 ldlm_lock2desc(lock, &body->lock_desc);
266 body->lock_flags = *flags;
268 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
269 if (parent_lock_handle)
270 memcpy(&body->lock_handle2, parent_lock_handle,
271 sizeof(body->lock_handle2));
273 /* Continue as normal. */
274 if (!req_passed_in) {
275 size = sizeof(*reply);
276 req->rq_replen = lustre_msg_size(1, &size);
278 lock->l_conn_export = exp;
279 lock->l_export = NULL;
280 lock->l_blocking_ast = blocking;
282 LDLM_DEBUG(lock, "sending request");
283 rc = ptlrpc_queue_wait(req);
285 if (rc != ELDLM_OK) {
287 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
288 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
289 failed_lock_cleanup(ns, lock, lockh, mode);
290 if (rc == ELDLM_LOCK_ABORTED) {
291 /* Before we return, swab the reply */
292 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
293 lustre_swab_ldlm_reply);
295 CERROR("Can't unpack ldlm_reply\n");
296 GOTO(out_req, rc = -EPROTO);
302 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
303 lustre_swab_ldlm_reply);
305 CERROR("Can't unpack ldlm_reply\n");
306 GOTO(out_req, rc = -EPROTO);
309 memcpy(&lock->l_remote_handle, &reply->lock_handle,
310 sizeof(lock->l_remote_handle));
311 *flags = reply->lock_flags;
313 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
314 lock, reply->lock_handle.cookie, *flags);
315 if (type == LDLM_EXTENT) {
316 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got "
317 "extent "LPU64" -> "LPU64"\n",
318 body->lock_desc.l_policy_data.l_extent.start,
319 body->lock_desc.l_policy_data.l_extent.end,
320 reply->lock_policy_data.l_extent.start,
321 reply->lock_policy_data.l_extent.end);
323 cookie = &reply->lock_policy_data; /* FIXME bug 267 */
324 cookielen = sizeof(struct ldlm_extent);
325 } else if (type == LDLM_FLOCK) {
326 cookie = &reply->lock_policy_data;
327 cookielen = sizeof(struct ldlm_flock);
330 /* If enqueue returned a blocked lock but the completion handler has
331 * already run, then it fixed up the resource and we don't need to do it
333 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
334 int newmode = reply->lock_mode;
336 if (newmode && newmode != lock->l_req_mode) {
337 LDLM_DEBUG(lock, "server returned different mode %s",
338 ldlm_lockname[newmode]);
339 lock->l_req_mode = newmode;
342 if (reply->lock_resource_name.name[0] !=
343 lock->l_resource->lr_name.name[0]) {
344 CDEBUG(D_INFO, "remote intent success, locking %ld "
346 (long)reply->lock_resource_name.name[0],
347 (long)lock->l_resource->lr_name.name[0]);
349 ldlm_lock_change_resource(ns, lock,
350 reply->lock_resource_name);
351 if (lock->l_resource == NULL) {
353 GOTO(out_req, rc = -ENOMEM);
355 LDLM_DEBUG(lock, "client-side enqueue, new resource");
358 if ((*flags) & LDLM_FL_AST_SENT) {
359 l_lock(&ns->ns_lock);
360 lock->l_flags |= LDLM_FL_CBPENDING;
361 l_unlock(&ns->ns_lock);
362 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
366 rc = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags);
367 if (lock->l_completion_ast != NULL) {
368 int err = lock->l_completion_ast(lock, *flags, NULL);
370 failed_lock_cleanup(ns, lock, lockh, mode);
376 LDLM_DEBUG(lock, "client-side enqueue END");
380 ptlrpc_req_finished(req);
387 int ldlm_cli_replay_enqueue(struct ldlm_lock *lock)
389 struct lustre_handle lockh;
390 struct ldlm_res_id junk;
391 int flags = LDLM_FL_REPLAY;
392 ldlm_lock2handle(lock, &lockh);
393 return ldlm_cli_enqueue(lock->l_conn_export, NULL, NULL, NULL, junk,
394 lock->l_resource->lr_type, NULL, 0, -1, &flags,
395 NULL, NULL, NULL, &lockh);
398 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
402 if (lock->l_resource->lr_namespace->ns_client) {
403 CERROR("Trying to cancel local lock\n");
406 LDLM_DEBUG(lock, "client-side local convert");
408 ldlm_lock_convert(lock, new_mode, flags);
409 ldlm_reprocess_all(lock->l_resource);
411 LDLM_DEBUG(lock, "client-side local convert handler END");
416 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
417 * conversion of locks which are on the waiting or converting queue */
418 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
420 struct ldlm_request *body;
421 struct ldlm_reply *reply;
422 struct ldlm_lock *lock;
423 struct ldlm_resource *res;
424 struct ptlrpc_request *req;
425 int rc, size = sizeof(*body);
428 lock = ldlm_handle2lock(lockh);
435 if (lock->l_conn_export == NULL)
436 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
438 LDLM_DEBUG(lock, "client-side convert");
440 req = ptlrpc_prep_req(class_exp2cliimp(lock->l_conn_export),
441 LDLM_CONVERT, 1, &size, NULL);
443 GOTO(out, rc = -ENOMEM);
445 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
446 memcpy(&body->lock_handle1, &lock->l_remote_handle,
447 sizeof(body->lock_handle1));
449 body->lock_desc.l_req_mode = new_mode;
450 body->lock_flags = *flags;
452 size = sizeof(*reply);
453 req->rq_replen = lustre_msg_size(1, &size);
455 rc = ptlrpc_queue_wait(req);
459 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
460 lustre_swab_ldlm_reply);
462 CERROR ("Can't unpack ldlm_reply\n");
463 GOTO (out, rc = -EPROTO);
466 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
468 ldlm_reprocess_all(res);
469 /* Go to sleep until the lock is granted. */
470 /* FIXME: or cancelled. */
471 if (lock->l_completion_ast)
472 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
476 ptlrpc_req_finished(req);
480 int ldlm_cli_cancel(struct lustre_handle *lockh)
482 struct ptlrpc_request *req;
483 struct ldlm_lock *lock;
484 struct ldlm_request *body;
485 int rc = 0, size = sizeof(*body);
488 /* concurrent cancels on the same handle can happen */
489 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
493 if (lock->l_conn_export) {
495 struct obd_import *imp;
497 LDLM_DEBUG(lock, "client-side cancel");
498 /* Set this flag to prevent others from getting new references*/
499 l_lock(&lock->l_resource->lr_namespace->ns_lock);
500 lock->l_flags |= LDLM_FL_CBPENDING;
501 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
502 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
503 ldlm_cancel_callback(lock);
506 CDEBUG(D_INFO, "not sending request (at caller's "
511 imp = class_exp2cliimp(lock->l_conn_export);
512 if (imp == NULL || imp->imp_invalid) {
513 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
518 req = ptlrpc_prep_req(imp, LDLM_CANCEL, 1, &size, NULL);
520 GOTO(out, rc = -ENOMEM);
522 /* XXX FIXME bug 249 */
523 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
524 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
526 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
527 memcpy(&body->lock_handle1, &lock->l_remote_handle,
528 sizeof(body->lock_handle1));
530 req->rq_replen = lustre_msg_size(0, NULL);
532 rc = ptlrpc_queue_wait(req);
535 CERROR("client/server (nid "LPU64") out of sync--not "
537 req->rq_import->imp_connection->c_peer.peer_nid);
538 else if (rc != ELDLM_OK)
539 CERROR("Got rc %d from cancel RPC: canceling "
542 ptlrpc_req_finished(req);
544 ldlm_lock_cancel(lock);
546 if (lock->l_resource->lr_namespace->ns_client) {
547 LDLM_ERROR(lock, "Trying to cancel local lock\n");
550 LDLM_DEBUG(lock, "client-side local cancel");
551 ldlm_lock_cancel(lock);
552 ldlm_reprocess_all(lock->l_resource);
553 LDLM_DEBUG(lock, "client-side local cancel handler END");
562 int ldlm_cancel_lru(struct ldlm_namespace *ns)
564 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
566 struct ldlm_ast_work *w;
569 l_lock(&ns->ns_lock);
570 count = ns->ns_nr_unused - ns->ns_max_unused;
573 l_unlock(&ns->ns_lock);
577 list_for_each_safe(tmp, next, &ns->ns_unused_list) {
578 struct ldlm_lock *lock;
579 lock = list_entry(tmp, struct ldlm_lock, l_lru);
581 LASSERT(!lock->l_readers && !lock->l_writers);
583 /* Setting the CBPENDING flag is a little misleading, but
584 * prevents an important race; namely, once CBPENDING is set,
585 * the lock can accumulate no more readers/writers. Since
586 * readers and writers are already zero here, ldlm_lock_decref
587 * won't see this flag and call l_blocking_ast */
588 lock->l_flags |= LDLM_FL_CBPENDING;
590 OBD_ALLOC(w, sizeof(*w));
593 w->w_lock = LDLM_LOCK_GET(lock);
594 list_add(&w->w_list, &list);
595 ldlm_lock_remove_from_lru(lock);
600 l_unlock(&ns->ns_lock);
602 list_for_each_safe(tmp, next, &list) {
603 struct lustre_handle lockh;
605 w = list_entry(tmp, struct ldlm_ast_work, w_list);
607 ldlm_lock2handle(w->w_lock, &lockh);
608 rc = ldlm_cli_cancel(&lockh);
610 CDEBUG(D_INFO, "ldlm_cli_cancel: %d\n", rc);
612 list_del(&w->w_list);
613 LDLM_LOCK_PUT(w->w_lock);
614 OBD_FREE(w, sizeof(*w));
620 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
621 struct ldlm_res_id res_id, int flags,
624 struct ldlm_resource *res;
625 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
626 struct ldlm_ast_work *w;
629 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
631 /* This is not a problem. */
632 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
636 l_lock(&ns->ns_lock);
637 list_for_each(tmp, &res->lr_granted) {
638 struct ldlm_lock *lock;
639 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
641 if (opaque != NULL && lock->l_ast_data != opaque) {
642 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
643 lock->l_ast_data, opaque);
648 if (lock->l_readers || lock->l_writers) {
649 if (flags & LDLM_FL_WARN) {
650 LDLM_ERROR(lock, "lock in use");
656 /* See CBPENDING comment in ldlm_cancel_lru */
657 lock->l_flags |= LDLM_FL_CBPENDING;
659 OBD_ALLOC(w, sizeof(*w));
662 w->w_lock = LDLM_LOCK_GET(lock);
664 /* Prevent the cancel callback from being called by setting
665 * LDLM_FL_CANCEL in the lock. Very sneaky. -p */
666 if (flags & LDLM_FL_NO_CALLBACK)
667 w->w_lock->l_flags |= LDLM_FL_CANCEL;
669 list_add(&w->w_list, &list);
671 l_unlock(&ns->ns_lock);
673 list_for_each_safe(tmp, next, &list) {
674 struct lustre_handle lockh;
676 w = list_entry(tmp, struct ldlm_ast_work, w_list);
678 if (flags & LDLM_FL_LOCAL_ONLY) {
679 ldlm_lock_cancel(w->w_lock);
681 ldlm_lock2handle(w->w_lock, &lockh);
682 rc = ldlm_cli_cancel(&lockh);
684 CERROR("ldlm_cli_cancel: %d\n", rc);
686 list_del(&w->w_list);
687 LDLM_LOCK_PUT(w->w_lock);
688 OBD_FREE(w, sizeof(*w));
691 ldlm_resource_putref(res);
696 /* Cancel all locks on a namespace (or a specific resource, if given)
697 * that have 0 readers/writers.
699 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
700 * to notify the server.
701 * If flags & LDLM_FL_NO_CALLBACK, don't run the cancel callback.
702 * If flags & LDLM_FL_WARN, print a warning if some locks are still in use. */
703 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
704 struct ldlm_res_id *res_id, int flags, void *opaque)
713 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
716 l_lock(&ns->ns_lock);
717 for (i = 0; i < RES_HASH_SIZE; i++) {
718 struct list_head *tmp, *pos;
719 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
721 struct ldlm_resource *res;
722 res = list_entry(tmp, struct ldlm_resource, lr_hash);
723 ldlm_resource_getref(res);
725 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
729 CERROR("cancel_unused_res ("LPU64"): %d\n",
730 res->lr_name.name[0], rc);
731 ldlm_resource_putref(res);
734 l_unlock(&ns->ns_lock);
739 /* Lock iterators. */
741 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
744 struct list_head *tmp, *next;
745 struct ldlm_lock *lock;
746 int rc = LDLM_ITER_CONTINUE;
747 struct ldlm_namespace *ns = res->lr_namespace;
752 RETURN(LDLM_ITER_CONTINUE);
754 l_lock(&ns->ns_lock);
755 list_for_each_safe(tmp, next, &res->lr_granted) {
756 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
758 if (iter(lock, closure) == LDLM_ITER_STOP)
759 GOTO(out, rc = LDLM_ITER_STOP);
762 list_for_each_safe(tmp, next, &res->lr_converting) {
763 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
765 if (iter(lock, closure) == LDLM_ITER_STOP)
766 GOTO(out, rc = LDLM_ITER_STOP);
769 list_for_each_safe(tmp, next, &res->lr_waiting) {
770 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
772 if (iter(lock, closure) == LDLM_ITER_STOP)
773 GOTO(out, rc = LDLM_ITER_STOP);
776 l_unlock(&ns->ns_lock);
780 struct iter_helper_data {
781 ldlm_iterator_t iter;
785 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
787 struct iter_helper_data *helper = closure;
788 return helper->iter(lock, helper->closure);
791 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
793 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
796 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
799 struct iter_helper_data helper = { iter: iter, closure: closure };
800 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
803 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
804 ldlm_res_iterator_t iter, void *closure)
806 int i, rc = LDLM_ITER_CONTINUE;
808 l_lock(&ns->ns_lock);
809 for (i = 0; i < RES_HASH_SIZE; i++) {
810 struct list_head *tmp, *next;
811 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
812 struct ldlm_resource *res =
813 list_entry(tmp, struct ldlm_resource, lr_hash);
815 ldlm_resource_getref(res);
816 rc = iter(res, closure);
817 ldlm_resource_putref(res);
818 if (rc == LDLM_ITER_STOP)
823 l_unlock(&ns->ns_lock);
827 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
828 void ldlm_change_cbdata(struct ldlm_namespace *ns,
829 struct ldlm_res_id *res_id,
830 ldlm_iterator_t iter,
833 struct ldlm_resource *res;
837 CERROR("must pass in namespace");
841 res = ldlm_resource_get(ns, NULL, *res_id, 0, 0);
847 l_lock(&ns->ns_lock);
848 ldlm_resource_foreach(res, iter, data);
849 l_unlock(&ns->ns_lock);
850 ldlm_resource_putref(res);
856 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
858 struct list_head *list = closure;
860 /* we use l_pending_chain here, because it's unused on clients. */
861 list_add(&lock->l_pending_chain, list);
862 return LDLM_ITER_CONTINUE;
865 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
867 struct ptlrpc_request *req;
868 struct ldlm_request *body;
869 struct ldlm_reply *reply;
874 * If granted mode matches the requested mode, this lock is granted.
876 * If they differ, but we have a granted mode, then we were granted
877 * one mode and now want another: ergo, converting.
879 * If we haven't been granted anything and are on a resource list,
880 * then we're blocked/waiting.
882 * If we haven't been granted anything and we're NOT on a resource list,
883 * then we haven't got a reply yet and don't have a known disposition.
884 * This happens whenever a lock enqueue is the request that triggers
887 if (lock->l_granted_mode == lock->l_req_mode)
888 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
889 else if (lock->l_granted_mode)
890 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
891 else if (!list_empty(&lock->l_res_link))
892 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
894 flags = LDLM_FL_REPLAY;
896 size = sizeof(*body);
897 req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, &size, NULL);
901 /* We're part of recovery, so don't wait for it. */
902 req->rq_send_state = LUSTRE_IMP_REPLAY;
904 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
905 ldlm_lock2desc(lock, &body->lock_desc);
906 body->lock_flags = flags;
908 ldlm_lock2handle(lock, &body->lock_handle1);
909 size = sizeof(*reply);
910 req->rq_replen = lustre_msg_size(1, &size);
912 LDLM_DEBUG(lock, "replaying lock:");
913 rc = ptlrpc_queue_wait(req);
917 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
918 lustre_swab_ldlm_reply);
920 CERROR("Can't unpack ldlm_reply\n");
921 GOTO (out, rc = -EPROTO);
924 memcpy(&lock->l_remote_handle, &reply->lock_handle,
925 sizeof(lock->l_remote_handle));
926 LDLM_DEBUG(lock, "replayed lock:");
928 ptlrpc_req_finished(req);
932 int ldlm_replay_locks(struct obd_import *imp)
934 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
935 struct list_head list, *pos, *next;
936 struct ldlm_lock *lock;
940 INIT_LIST_HEAD(&list);
942 l_lock(&ns->ns_lock);
943 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
945 list_for_each_safe(pos, next, &list) {
946 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
947 rc = replay_one_lock(imp, lock);
949 break; /* or try to do the rest? */
951 l_unlock(&ns->ns_lock);