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_import *imp;
48 struct obd_device *obd;
50 if (lock->l_conn_export == NULL) {
51 LDLM_ERROR(lock, "lock timed out; not entering recovery in "
52 "server code, just going back to sleep");
56 obd = lock->l_conn_export->exp_obd;
57 imp = obd->u.cli.cl_import;
58 ptlrpc_fail_import(imp, lwd->lwd_generation);
59 LDLM_ERROR(lock, "lock timed out, entering recovery for %s@%s",
60 imp->imp_target_uuid.uuid,
61 imp->imp_connection->c_remote_uuid.uuid);
66 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
68 /* XXX ALLOCATE - 160 mytes */
69 struct lock_wait_data lwd;
70 unsigned long irqflags;
71 struct obd_device *obd;
72 struct obd_import *imp = NULL;
74 struct l_wait_info lwi;
77 if (flags == LDLM_FL_WAIT_NOREPROC)
81 wake_up(&lock->l_waitq);
85 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
89 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
91 ldlm_lock_dump(D_OTHER, lock, 0);
92 ldlm_reprocess_all(lock->l_resource);
96 obd = class_exp2obd(lock->l_conn_export);
98 /* if this is a local lock, then there is no import */
100 imp = obd->u.cli.cl_import;
104 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
105 interrupted_completion_wait, &lwd);
107 spin_lock_irqsave(&imp->imp_lock, irqflags);
108 lwd.lwd_generation = imp->imp_generation;
109 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
112 /* Go to sleep until the lock is granted or cancelled. */
113 rc = l_wait_event(lock->l_waitq,
114 ((lock->l_req_mode == lock->l_granted_mode) ||
115 lock->l_destroyed), &lwi);
117 if (lock->l_destroyed) {
118 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
123 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
128 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
132 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
133 struct lustre_handle *parent_lockh,
134 struct ldlm_res_id res_id,
136 void *cookie, int cookielen,
139 ldlm_completion_callback completion,
140 ldlm_blocking_callback blocking,
142 struct lustre_handle *lockh)
144 struct ldlm_lock *lock;
149 CERROR("Trying to enqueue local lock in a shadow namespace\n");
153 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode,
154 blocking, completion, data);
156 GOTO(out_nolock, err = -ENOMEM);
157 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
159 ldlm_lock_addref_internal(lock, mode);
160 ldlm_lock2handle(lock, lockh);
161 lock->l_flags |= LDLM_FL_LOCAL;
163 err = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags);
167 if (type != LDLM_PLAIN)
168 memcpy(cookie, &lock->l_policy_data, cookielen);
169 if ((*flags) & LDLM_FL_LOCK_CHANGED)
170 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
172 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
175 if (lock->l_completion_ast)
176 lock->l_completion_ast(lock, *flags, NULL);
178 LDLM_DEBUG(lock, "client-side local enqueue END");
186 static void failed_lock_cleanup(struct ldlm_namespace *ns,
187 struct ldlm_lock *lock,
188 struct lustre_handle *lockh, int mode)
190 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
191 l_lock(&ns->ns_lock);
192 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
193 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
194 l_unlock(&ns->ns_lock);
196 ldlm_lock_decref_and_cancel(lockh, mode);
199 int ldlm_cli_enqueue(struct obd_export *exp,
200 struct ptlrpc_request *req,
201 struct ldlm_namespace *ns,
202 struct lustre_handle *parent_lock_handle,
203 struct ldlm_res_id res_id,
205 void *cookie, int cookielen,
208 ldlm_completion_callback completion,
209 ldlm_blocking_callback blocking,
211 struct lustre_handle *lockh)
213 struct ldlm_lock *lock;
214 struct ldlm_request *body;
215 struct ldlm_reply *reply;
216 int rc, size = sizeof(*body), req_passed_in = 1, is_replay;
219 is_replay = *flags & LDLM_FL_REPLAY;
220 LASSERT(exp != NULL || !is_replay);
223 rc = ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
224 type, cookie, cookielen, mode,
225 flags, completion, blocking, data,
230 /* If we're replaying this lock, just check some invariants.
231 * If we're creating a new lock, get everything all setup nice. */
233 lock = ldlm_handle2lock(lockh);
234 LDLM_DEBUG(lock, "client-side enqueue START");
235 LASSERT(exp == lock->l_conn_export);
237 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type,
238 mode, blocking, completion, data);
240 GOTO(out_nolock, rc = -ENOMEM);
241 /* for the local lock, add the reference */
242 ldlm_lock_addref_internal(lock, mode);
243 ldlm_lock2handle(lock, lockh);
244 if (type != LDLM_PLAIN)
245 memcpy(&lock->l_policy_data, cookie, cookielen);
246 LDLM_DEBUG(lock, "client-side enqueue START");
250 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 1,
253 GOTO(out, rc = -ENOMEM);
255 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
258 /* Dump lock data into the request buffer */
259 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
260 ldlm_lock2desc(lock, &body->lock_desc);
261 body->lock_flags = *flags;
263 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
264 if (parent_lock_handle)
265 memcpy(&body->lock_handle2, parent_lock_handle,
266 sizeof(body->lock_handle2));
268 /* Continue as normal. */
269 if (!req_passed_in) {
270 size = sizeof(*reply);
271 req->rq_replen = lustre_msg_size(1, &size);
273 lock->l_conn_export = exp;
274 lock->l_export = NULL;
275 lock->l_blocking_ast = blocking;
277 LDLM_DEBUG(lock, "sending request");
278 rc = ptlrpc_queue_wait(req);
280 if (rc != ELDLM_OK) {
282 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
283 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
284 failed_lock_cleanup(ns, lock, lockh, mode);
285 if (rc == ELDLM_LOCK_ABORTED) {
286 /* Before we return, swab the reply */
287 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
288 lustre_swab_ldlm_reply);
290 CERROR("Can't unpack ldlm_reply\n");
291 GOTO(out_req, rc = -EPROTO);
297 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
298 lustre_swab_ldlm_reply);
300 CERROR("Can't unpack ldlm_reply\n");
301 GOTO(out_req, rc = -EPROTO);
304 memcpy(&lock->l_remote_handle, &reply->lock_handle,
305 sizeof(lock->l_remote_handle));
306 *flags = reply->lock_flags;
308 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
309 lock, reply->lock_handle.cookie, *flags);
310 if (type == LDLM_EXTENT) {
311 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got "
312 "extent "LPU64" -> "LPU64"\n",
313 body->lock_desc.l_policy_data.l_extent.start,
314 body->lock_desc.l_policy_data.l_extent.end,
315 reply->lock_policy_data.l_extent.start,
316 reply->lock_policy_data.l_extent.end);
318 cookie = &reply->lock_policy_data; /* FIXME bug 267 */
319 cookielen = sizeof(struct ldlm_extent);
320 } else if (type == LDLM_FLOCK) {
321 cookie = &reply->lock_policy_data;
322 cookielen = sizeof(struct ldlm_flock);
325 /* If enqueue returned a blocked lock but the completion handler has
326 * already run, then it fixed up the resource and we don't need to do it
328 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
329 int newmode = reply->lock_mode;
331 if (newmode && newmode != lock->l_req_mode) {
332 LDLM_DEBUG(lock, "server returned different mode %s",
333 ldlm_lockname[newmode]);
334 lock->l_req_mode = newmode;
337 if (reply->lock_resource_name.name[0] !=
338 lock->l_resource->lr_name.name[0]) {
339 CDEBUG(D_INFO, "remote intent success, locking %ld "
341 (long)reply->lock_resource_name.name[0],
342 (long)lock->l_resource->lr_name.name[0]);
344 ldlm_lock_change_resource(ns, lock,
345 reply->lock_resource_name);
346 if (lock->l_resource == NULL) {
348 GOTO(out_req, rc = -ENOMEM);
350 LDLM_DEBUG(lock, "client-side enqueue, new resource");
353 if ((*flags) & LDLM_FL_AST_SENT) {
354 l_lock(&ns->ns_lock);
355 lock->l_flags |= LDLM_FL_CBPENDING;
356 l_unlock(&ns->ns_lock);
357 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
361 rc = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags);
362 if (lock->l_completion_ast != NULL) {
363 int err = lock->l_completion_ast(lock, *flags, NULL);
365 failed_lock_cleanup(ns, lock, lockh, mode);
371 LDLM_DEBUG(lock, "client-side enqueue END");
375 ptlrpc_req_finished(req);
382 int ldlm_cli_replay_enqueue(struct ldlm_lock *lock)
384 struct lustre_handle lockh;
385 struct ldlm_res_id junk;
386 int flags = LDLM_FL_REPLAY;
387 ldlm_lock2handle(lock, &lockh);
388 return ldlm_cli_enqueue(lock->l_conn_export, NULL, NULL, NULL, junk,
389 lock->l_resource->lr_type, NULL, 0, -1, &flags,
390 NULL, NULL, NULL, &lockh);
393 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
397 if (lock->l_resource->lr_namespace->ns_client) {
398 CERROR("Trying to cancel local lock\n");
401 LDLM_DEBUG(lock, "client-side local convert");
403 ldlm_lock_convert(lock, new_mode, flags);
404 ldlm_reprocess_all(lock->l_resource);
406 LDLM_DEBUG(lock, "client-side local convert handler END");
411 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
412 * conversion of locks which are on the waiting or converting queue */
413 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
415 struct ldlm_request *body;
416 struct ldlm_reply *reply;
417 struct ldlm_lock *lock;
418 struct ldlm_resource *res;
419 struct ptlrpc_request *req;
420 int rc, size = sizeof(*body);
423 lock = ldlm_handle2lock(lockh);
430 if (lock->l_conn_export == NULL)
431 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
433 LDLM_DEBUG(lock, "client-side convert");
435 req = ptlrpc_prep_req(class_exp2cliimp(lock->l_conn_export),
436 LDLM_CONVERT, 1, &size, NULL);
438 GOTO(out, rc = -ENOMEM);
440 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
441 memcpy(&body->lock_handle1, &lock->l_remote_handle,
442 sizeof(body->lock_handle1));
444 body->lock_desc.l_req_mode = new_mode;
445 body->lock_flags = *flags;
447 size = sizeof(*reply);
448 req->rq_replen = lustre_msg_size(1, &size);
450 rc = ptlrpc_queue_wait(req);
454 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
455 lustre_swab_ldlm_reply);
457 CERROR ("Can't unpack ldlm_reply\n");
458 GOTO (out, rc = -EPROTO);
461 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
463 ldlm_reprocess_all(res);
464 /* Go to sleep until the lock is granted. */
465 /* FIXME: or cancelled. */
466 if (lock->l_completion_ast)
467 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
471 ptlrpc_req_finished(req);
475 int ldlm_cli_cancel(struct lustre_handle *lockh)
477 struct ptlrpc_request *req;
478 struct ldlm_lock *lock;
479 struct ldlm_request *body;
480 int rc = 0, size = sizeof(*body);
483 /* concurrent cancels on the same handle can happen */
484 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
488 if (lock->l_conn_export) {
490 struct obd_import *imp;
492 LDLM_DEBUG(lock, "client-side cancel");
493 /* Set this flag to prevent others from getting new references*/
494 l_lock(&lock->l_resource->lr_namespace->ns_lock);
495 lock->l_flags |= LDLM_FL_CBPENDING;
496 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
497 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
498 ldlm_cancel_callback(lock);
501 CDEBUG(D_INFO, "not sending request (at caller's "
506 imp = class_exp2cliimp(lock->l_conn_export);
507 if (imp == NULL || imp->imp_invalid) {
508 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
513 req = ptlrpc_prep_req(imp, LDLM_CANCEL, 1, &size, NULL);
515 GOTO(out, rc = -ENOMEM);
517 /* XXX FIXME bug 249 */
518 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
519 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
521 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
522 memcpy(&body->lock_handle1, &lock->l_remote_handle,
523 sizeof(body->lock_handle1));
525 req->rq_replen = lustre_msg_size(0, NULL);
527 rc = ptlrpc_queue_wait(req);
530 CERROR("client/server (nid "LPU64") out of sync--not "
532 req->rq_import->imp_connection->c_peer.peer_nid);
533 else if (rc != ELDLM_OK)
534 CERROR("Got rc %d from cancel RPC: canceling "
537 ptlrpc_req_finished(req);
539 ldlm_lock_cancel(lock);
541 if (lock->l_resource->lr_namespace->ns_client) {
542 LDLM_ERROR(lock, "Trying to cancel local lock\n");
545 LDLM_DEBUG(lock, "client-side local cancel");
546 ldlm_lock_cancel(lock);
547 ldlm_reprocess_all(lock->l_resource);
548 LDLM_DEBUG(lock, "client-side local cancel handler END");
557 int ldlm_cancel_lru(struct ldlm_namespace *ns)
559 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
561 struct ldlm_ast_work *w;
564 l_lock(&ns->ns_lock);
565 count = ns->ns_nr_unused - ns->ns_max_unused;
568 l_unlock(&ns->ns_lock);
572 list_for_each_safe(tmp, next, &ns->ns_unused_list) {
573 struct ldlm_lock *lock;
574 lock = list_entry(tmp, struct ldlm_lock, l_lru);
576 LASSERT(!lock->l_readers && !lock->l_writers);
578 /* Setting the CBPENDING flag is a little misleading, but
579 * prevents an important race; namely, once CBPENDING is set,
580 * the lock can accumulate no more readers/writers. Since
581 * readers and writers are already zero here, ldlm_lock_decref
582 * won't see this flag and call l_blocking_ast */
583 lock->l_flags |= LDLM_FL_CBPENDING;
585 OBD_ALLOC(w, sizeof(*w));
588 w->w_lock = LDLM_LOCK_GET(lock);
589 list_add(&w->w_list, &list);
590 ldlm_lock_remove_from_lru(lock);
595 l_unlock(&ns->ns_lock);
597 list_for_each_safe(tmp, next, &list) {
598 struct lustre_handle lockh;
600 w = list_entry(tmp, struct ldlm_ast_work, w_list);
602 ldlm_lock2handle(w->w_lock, &lockh);
603 rc = ldlm_cli_cancel(&lockh);
605 CDEBUG(D_INFO, "ldlm_cli_cancel: %d\n", rc);
607 list_del(&w->w_list);
608 LDLM_LOCK_PUT(w->w_lock);
609 OBD_FREE(w, sizeof(*w));
615 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
616 struct ldlm_res_id res_id, int flags,
619 struct ldlm_resource *res;
620 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
621 struct ldlm_ast_work *w;
624 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
626 /* This is not a problem. */
627 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
631 l_lock(&ns->ns_lock);
632 list_for_each(tmp, &res->lr_granted) {
633 struct ldlm_lock *lock;
634 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
636 if (opaque != NULL && lock->l_ast_data != opaque) {
637 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
638 lock->l_ast_data, opaque);
643 if (lock->l_readers || lock->l_writers) {
644 if (flags & LDLM_FL_WARN) {
645 LDLM_ERROR(lock, "lock in use");
651 /* See CBPENDING comment in ldlm_cancel_lru */
652 lock->l_flags |= LDLM_FL_CBPENDING;
654 OBD_ALLOC(w, sizeof(*w));
657 w->w_lock = LDLM_LOCK_GET(lock);
659 /* Prevent the cancel callback from being called by setting
660 * LDLM_FL_CANCEL in the lock. Very sneaky. -p */
661 if (flags & LDLM_FL_NO_CALLBACK)
662 w->w_lock->l_flags |= LDLM_FL_CANCEL;
664 list_add(&w->w_list, &list);
666 l_unlock(&ns->ns_lock);
668 list_for_each_safe(tmp, next, &list) {
669 struct lustre_handle lockh;
671 w = list_entry(tmp, struct ldlm_ast_work, w_list);
673 if (flags & LDLM_FL_LOCAL_ONLY) {
674 ldlm_lock_cancel(w->w_lock);
676 ldlm_lock2handle(w->w_lock, &lockh);
677 rc = ldlm_cli_cancel(&lockh);
679 CERROR("ldlm_cli_cancel: %d\n", rc);
681 list_del(&w->w_list);
682 LDLM_LOCK_PUT(w->w_lock);
683 OBD_FREE(w, sizeof(*w));
686 ldlm_resource_putref(res);
691 /* Cancel all locks on a namespace (or a specific resource, if given)
692 * that have 0 readers/writers.
694 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
695 * to notify the server.
696 * If flags & LDLM_FL_NO_CALLBACK, don't run the cancel callback.
697 * If flags & LDLM_FL_WARN, print a warning if some locks are still in use. */
698 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
699 struct ldlm_res_id *res_id, int flags, void *opaque)
708 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
711 l_lock(&ns->ns_lock);
712 for (i = 0; i < RES_HASH_SIZE; i++) {
713 struct list_head *tmp, *pos;
714 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
716 struct ldlm_resource *res;
717 res = list_entry(tmp, struct ldlm_resource, lr_hash);
718 ldlm_resource_getref(res);
720 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
724 CERROR("cancel_unused_res ("LPU64"): %d\n",
725 res->lr_name.name[0], rc);
726 ldlm_resource_putref(res);
729 l_unlock(&ns->ns_lock);
734 /* Lock iterators. */
736 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
739 struct list_head *tmp, *next;
740 struct ldlm_lock *lock;
741 int rc = LDLM_ITER_CONTINUE;
742 struct ldlm_namespace *ns = res->lr_namespace;
747 RETURN(LDLM_ITER_CONTINUE);
749 l_lock(&ns->ns_lock);
750 list_for_each_safe(tmp, next, &res->lr_granted) {
751 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
753 if (iter(lock, closure) == LDLM_ITER_STOP)
754 GOTO(out, rc = LDLM_ITER_STOP);
757 list_for_each_safe(tmp, next, &res->lr_converting) {
758 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
760 if (iter(lock, closure) == LDLM_ITER_STOP)
761 GOTO(out, rc = LDLM_ITER_STOP);
764 list_for_each_safe(tmp, next, &res->lr_waiting) {
765 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
767 if (iter(lock, closure) == LDLM_ITER_STOP)
768 GOTO(out, rc = LDLM_ITER_STOP);
771 l_unlock(&ns->ns_lock);
775 struct iter_helper_data {
776 ldlm_iterator_t iter;
780 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
782 struct iter_helper_data *helper = closure;
783 return helper->iter(lock, helper->closure);
786 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
788 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
791 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
794 struct iter_helper_data helper = { iter: iter, closure: closure };
795 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
798 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
799 ldlm_res_iterator_t iter, void *closure)
801 int i, rc = LDLM_ITER_CONTINUE;
803 l_lock(&ns->ns_lock);
804 for (i = 0; i < RES_HASH_SIZE; i++) {
805 struct list_head *tmp, *next;
806 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
807 struct ldlm_resource *res =
808 list_entry(tmp, struct ldlm_resource, lr_hash);
810 ldlm_resource_getref(res);
811 rc = iter(res, closure);
812 ldlm_resource_putref(res);
813 if (rc == LDLM_ITER_STOP)
818 l_unlock(&ns->ns_lock);
822 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
823 void ldlm_change_cbdata(struct ldlm_namespace *ns,
824 struct ldlm_res_id *res_id,
825 ldlm_iterator_t iter,
828 struct ldlm_resource *res;
832 CERROR("must pass in namespace");
836 res = ldlm_resource_get(ns, NULL, *res_id, 0, 0);
842 l_lock(&ns->ns_lock);
843 ldlm_resource_foreach(res, iter, data);
844 l_unlock(&ns->ns_lock);
845 ldlm_resource_putref(res);
851 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
853 struct list_head *list = closure;
855 /* we use l_pending_chain here, because it's unused on clients. */
856 list_add(&lock->l_pending_chain, list);
857 return LDLM_ITER_CONTINUE;
860 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
862 struct ptlrpc_request *req;
863 struct ldlm_request *body;
864 struct ldlm_reply *reply;
869 * If granted mode matches the requested mode, this lock is granted.
871 * If they differ, but we have a granted mode, then we were granted
872 * one mode and now want another: ergo, converting.
874 * If we haven't been granted anything and are on a resource list,
875 * then we're blocked/waiting.
877 * If we haven't been granted anything and we're NOT on a resource list,
878 * then we haven't got a reply yet and don't have a known disposition.
879 * This happens whenever a lock enqueue is the request that triggers
882 if (lock->l_granted_mode == lock->l_req_mode)
883 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
884 else if (lock->l_granted_mode)
885 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
886 else if (!list_empty(&lock->l_res_link))
887 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
889 flags = LDLM_FL_REPLAY;
891 size = sizeof(*body);
892 req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, &size, NULL);
896 /* We're part of recovery, so don't wait for it. */
897 req->rq_send_state = LUSTRE_IMP_REPLAY;
899 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
900 ldlm_lock2desc(lock, &body->lock_desc);
901 body->lock_flags = flags;
903 ldlm_lock2handle(lock, &body->lock_handle1);
904 size = sizeof(*reply);
905 req->rq_replen = lustre_msg_size(1, &size);
907 LDLM_DEBUG(lock, "replaying lock:");
908 rc = ptlrpc_queue_wait(req);
912 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
913 lustre_swab_ldlm_reply);
915 CERROR("Can't unpack ldlm_reply\n");
916 GOTO (out, rc = -EPROTO);
919 memcpy(&lock->l_remote_handle, &reply->lock_handle,
920 sizeof(lock->l_remote_handle));
921 LDLM_DEBUG(lock, "replayed lock:");
923 ptlrpc_req_finished(req);
927 int ldlm_replay_locks(struct obd_import *imp)
929 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
930 struct list_head list, *pos, *next;
931 struct ldlm_lock *lock;
935 INIT_LIST_HEAD(&list);
937 l_lock(&ns->ns_lock);
938 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
940 list_for_each_safe(pos, next, &list) {
941 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
942 rc = replay_one_lock(imp, lock);
944 break; /* or try to do the rest? */
946 l_unlock(&ns->ns_lock);