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 void interrupted_completion_wait(void *data)
36 struct lock_wait_data {
37 struct ldlm_lock *lwd_lock;
41 int ldlm_expired_completion_wait(void *data)
43 struct lock_wait_data *lwd = data;
44 struct ldlm_lock *lock = lwd->lwd_lock;
45 struct obd_device *obd = class_conn2obd(lock->l_connh);
48 LDLM_ERROR(lock, "lock timed out; mot entering recovery in "
49 "server code, just going back to sleep");
51 struct obd_import *imp = obd->u.cli.cl_import;
52 ptlrpc_fail_import(imp, lwd->lwd_generation);
53 LDLM_ERROR(lock, "lock timed out, entering recovery for %s@%s",
54 imp->imp_target_uuid.uuid,
55 imp->imp_connection->c_remote_uuid.uuid);
61 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
63 /* XXX ALLOCATE - 160 mytes */
64 struct lock_wait_data lwd;
65 unsigned long irqflags;
66 struct obd_device *obd;
67 struct obd_import *imp = NULL;
69 struct l_wait_info lwi;
71 obd = class_conn2obd(lock->l_connh);
73 /* if this is a local lock, then there is no import */
75 imp = obd->u.cli.cl_import;
79 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
80 interrupted_completion_wait, &lwd);
83 if (flags == LDLM_FL_WAIT_NOREPROC)
87 wake_up(&lock->l_waitq);
91 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
95 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
97 ldlm_lock_dump(D_OTHER, lock);
98 ldlm_reprocess_all(lock->l_resource);
102 spin_lock_irqsave(&imp->imp_lock, irqflags);
103 lwd.lwd_generation = imp->imp_generation;
104 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
107 /* Go to sleep until the lock is granted or cancelled. */
108 rc = l_wait_event(lock->l_waitq,
109 ((lock->l_req_mode == lock->l_granted_mode) ||
110 lock->l_destroyed), &lwi);
112 if (lock->l_destroyed) {
113 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
118 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
123 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
127 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
128 struct lustre_handle *parent_lockh,
129 struct ldlm_res_id res_id,
131 void *cookie, int cookielen,
134 ldlm_completion_callback completion,
135 ldlm_blocking_callback blocking,
137 struct lustre_handle *lockh)
139 struct ldlm_lock *lock;
144 CERROR("Trying to enqueue local lock in a shadow namespace\n");
148 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode,
151 GOTO(out_nolock, err = -ENOMEM);
152 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
154 ldlm_lock_addref_internal(lock, mode);
155 ldlm_lock2handle(lock, lockh);
156 lock->l_flags |= LDLM_FL_LOCAL;
158 err = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags,
163 if (type == LDLM_EXTENT)
164 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
165 if ((*flags) & LDLM_FL_LOCK_CHANGED)
166 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
168 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
171 if (lock->l_completion_ast)
172 lock->l_completion_ast(lock, *flags, NULL);
174 LDLM_DEBUG(lock, "client-side local enqueue END");
182 int ldlm_cli_enqueue(struct lustre_handle *connh,
183 struct ptlrpc_request *req,
184 struct ldlm_namespace *ns,
185 struct lustre_handle *parent_lock_handle,
186 struct ldlm_res_id res_id,
188 void *cookie, int cookielen,
191 ldlm_completion_callback completion,
192 ldlm_blocking_callback blocking,
194 struct lustre_handle *lockh)
196 struct ldlm_lock *lock;
197 struct ldlm_request *body;
198 struct ldlm_reply *reply;
199 int rc, size = sizeof(*body), req_passed_in = 1, is_replay;
202 is_replay = *flags & LDLM_FL_REPLAY;
203 LASSERT(connh != NULL || !is_replay);
206 rc = ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
207 type, cookie, cookielen, mode,
208 flags, completion, blocking, data,
213 /* If we're replaying this lock, just check some invariants.
214 * If we're creating a new lock, get everything all setup nice. */
216 lock = ldlm_handle2lock(lockh);
217 LDLM_DEBUG(lock, "client-side enqueue START");
218 LASSERT(connh == lock->l_connh);
220 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type,
221 mode, blocking, data);
223 GOTO(out_nolock, rc = -ENOMEM);
224 /* ugh. I set this early (instead of waiting for _enqueue)
225 * because the completion AST might arrive early, and we need
226 * (in just this one case) to run the completion_cb even if it
227 * arrives before the reply. */
228 lock->l_completion_ast = completion;
229 LDLM_DEBUG(lock, "client-side enqueue START");
230 /* for the local lock, add the reference */
231 ldlm_lock_addref_internal(lock, mode);
232 ldlm_lock2handle(lock, lockh);
233 if (type == LDLM_EXTENT)
234 memcpy(&lock->l_extent, cookie,
235 sizeof(body->lock_desc.l_extent));
239 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
242 GOTO(out, rc = -ENOMEM);
244 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
247 /* Dump lock data into the request buffer */
248 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
249 ldlm_lock2desc(lock, &body->lock_desc);
250 body->lock_flags = *flags;
252 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
253 if (parent_lock_handle)
254 memcpy(&body->lock_handle2, parent_lock_handle,
255 sizeof(body->lock_handle2));
257 /* Continue as normal. */
258 if (!req_passed_in) {
259 size = sizeof(*reply);
260 req->rq_replen = lustre_msg_size(1, &size);
262 lock->l_connh = connh;
263 lock->l_export = NULL;
264 lock->l_blocking_ast = blocking;
266 LDLM_DEBUG(lock, "sending request");
267 rc = ptlrpc_queue_wait(req);
269 if (rc != ELDLM_OK) {
271 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
272 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
273 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
274 l_lock(&ns->ns_lock);
275 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
276 l_unlock(&ns->ns_lock);
278 ldlm_lock_decref_and_cancel(lockh, mode);
280 if (rc == ELDLM_LOCK_ABORTED) {
281 /* caller expects reply buffer 0 to have been swabbed */
282 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
283 lustre_swab_ldlm_reply);
285 CERROR ("Can't unpack ldlm_reply\n");
286 GOTO (out_req, rc = -EPROTO);
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);
299 memcpy(&lock->l_remote_handle, &reply->lock_handle,
300 sizeof(lock->l_remote_handle));
301 *flags = reply->lock_flags;
303 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: %d\n", lock,
304 reply->lock_handle.cookie, *flags);
305 if (type == LDLM_EXTENT) {
306 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got "
307 "extent "LPU64" -> "LPU64"\n",
308 body->lock_desc.l_extent.start,
309 body->lock_desc.l_extent.end,
310 reply->lock_extent.start, reply->lock_extent.end);
312 if ((reply->lock_extent.end & ~PAGE_MASK) != ~PAGE_MASK) {
313 /* XXX Old versions of BA OST code have a fencepost bug
314 * which will cause them to grant a lock that's one
315 * byte too large. This can be safely removed after BA
316 * ships their next release -phik (02 Apr 2003) */
317 reply->lock_extent.end--;
318 } else if ((reply->lock_extent.start & ~PAGE_MASK) ==
320 reply->lock_extent.start++;
323 cookie = &reply->lock_extent; /* FIXME bug 267 */
324 cookielen = sizeof(reply->lock_extent);
327 /* If enqueue returned a blocked lock but the completion handler has
328 * already run, then it fixed up the resource and we don't need to do it
330 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
331 int newmode = reply->lock_mode;
333 if (newmode && newmode != lock->l_req_mode) {
334 LDLM_DEBUG(lock, "server returned different mode %s",
335 ldlm_lockname[newmode]);
336 lock->l_req_mode = newmode;
339 if (reply->lock_resource_name.name[0] !=
340 lock->l_resource->lr_name.name[0]) {
341 CDEBUG(D_INFO, "remote intent success, locking %ld "
343 (long)reply->lock_resource_name.name[0],
344 (long)lock->l_resource->lr_name.name[0]);
346 ldlm_lock_change_resource(ns, lock,
347 reply->lock_resource_name);
348 if (lock->l_resource == NULL) {
350 GOTO(out_req, rc = -ENOMEM);
352 LDLM_DEBUG(lock, "client-side enqueue, new resource");
357 l_lock(&ns->ns_lock);
358 lock->l_completion_ast = NULL;
359 rc = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags,
361 l_unlock(&ns->ns_lock);
362 if (lock->l_completion_ast)
363 lock->l_completion_ast(lock, *flags, NULL);
366 LDLM_DEBUG(lock, "client-side enqueue END");
370 ptlrpc_req_finished(req);
377 int ldlm_cli_replay_enqueue(struct ldlm_lock *lock)
379 struct lustre_handle lockh;
380 struct ldlm_res_id junk;
381 int flags = LDLM_FL_REPLAY;
382 ldlm_lock2handle(lock, &lockh);
383 return ldlm_cli_enqueue(lock->l_connh, NULL, NULL, NULL, junk,
384 lock->l_resource->lr_type, NULL, 0, -1, &flags,
385 NULL, NULL, NULL, &lockh);
388 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
392 if (lock->l_resource->lr_namespace->ns_client) {
393 CERROR("Trying to cancel local lock\n");
396 LDLM_DEBUG(lock, "client-side local convert");
398 ldlm_lock_convert(lock, new_mode, flags);
399 ldlm_reprocess_all(lock->l_resource);
401 LDLM_DEBUG(lock, "client-side local convert handler END");
406 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
407 * conversion of locks which are on the waiting or converting queue */
408 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
410 struct ldlm_request *body;
411 struct lustre_handle *connh;
412 struct ldlm_reply *reply;
413 struct ldlm_lock *lock;
414 struct ldlm_resource *res;
415 struct ptlrpc_request *req;
416 int rc, size = sizeof(*body);
419 lock = ldlm_handle2lock(lockh);
425 connh = lock->l_connh;
428 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
430 LDLM_DEBUG(lock, "client-side convert");
432 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
435 GOTO(out, rc = -ENOMEM);
437 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
438 memcpy(&body->lock_handle1, &lock->l_remote_handle,
439 sizeof(body->lock_handle1));
441 body->lock_desc.l_req_mode = new_mode;
442 body->lock_flags = *flags;
444 size = sizeof(*reply);
445 req->rq_replen = lustre_msg_size(1, &size);
447 rc = ptlrpc_queue_wait(req);
451 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
452 lustre_swab_ldlm_reply);
454 CERROR ("Can't unpack ldlm_reply\n");
455 GOTO (out, rc = -EPROTO);
458 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
460 ldlm_reprocess_all(res);
461 /* Go to sleep until the lock is granted. */
462 /* FIXME: or cancelled. */
463 if (lock->l_completion_ast)
464 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
468 ptlrpc_req_finished(req);
472 int ldlm_cli_cancel(struct lustre_handle *lockh)
474 struct ptlrpc_request *req;
475 struct ldlm_lock *lock;
476 struct ldlm_request *body;
477 int rc = 0, size = sizeof(*body);
480 /* concurrent cancels on the same handle can happen */
481 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
487 struct obd_import *imp;
489 LDLM_DEBUG(lock, "client-side cancel");
490 /* Set this flag to prevent others from getting new references*/
491 l_lock(&lock->l_resource->lr_namespace->ns_lock);
492 lock->l_flags |= LDLM_FL_CBPENDING;
493 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
494 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
495 ldlm_cancel_callback(lock);
498 CDEBUG(D_INFO, "not sending request (at caller's "
503 imp = class_conn2cliimp(lock->l_connh);
504 if (imp == NULL || imp->imp_invalid) {
505 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
510 req = ptlrpc_prep_req(imp, LDLM_CANCEL, 1, &size, NULL);
512 GOTO(out, rc = -ENOMEM);
514 /* XXX FIXME bug 249 */
515 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
516 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
518 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
519 memcpy(&body->lock_handle1, &lock->l_remote_handle,
520 sizeof(body->lock_handle1));
522 req->rq_replen = lustre_msg_size(0, NULL);
524 rc = ptlrpc_queue_wait(req);
527 CERROR("client/server (nid "LPU64") out of sync--not "
529 req->rq_import->imp_connection->c_peer.peer_nid);
530 else if (rc != ELDLM_OK)
531 CERROR("Got rc %d from cancel RPC: canceling "
534 ptlrpc_req_finished(req);
536 ldlm_lock_cancel(lock);
538 LDLM_DEBUG(lock, "client-side local cancel");
539 if (lock->l_resource->lr_namespace->ns_client) {
540 CERROR("Trying to cancel local lock\n");
543 ldlm_lock_cancel(lock);
544 ldlm_reprocess_all(lock->l_resource);
545 LDLM_DEBUG(lock, "client-side local cancel handler END");
554 int ldlm_cancel_lru(struct ldlm_namespace *ns)
556 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
558 struct ldlm_ast_work *w;
561 l_lock(&ns->ns_lock);
562 count = ns->ns_nr_unused - ns->ns_max_unused;
565 l_unlock(&ns->ns_lock);
569 list_for_each_safe(tmp, next, &ns->ns_unused_list) {
570 struct ldlm_lock *lock;
571 lock = list_entry(tmp, struct ldlm_lock, l_lru);
573 LASSERT(!lock->l_readers && !lock->l_writers);
575 /* Setting the CBPENDING flag is a little misleading, but
576 * prevents an important race; namely, once CBPENDING is set,
577 * the lock can accumulate no more readers/writers. Since
578 * readers and writers are already zero here, ldlm_lock_decref
579 * won't see this flag and call l_blocking_ast */
580 lock->l_flags |= LDLM_FL_CBPENDING;
582 OBD_ALLOC(w, sizeof(*w));
585 w->w_lock = LDLM_LOCK_GET(lock);
586 list_add(&w->w_list, &list);
587 ldlm_lock_remove_from_lru(lock);
592 l_unlock(&ns->ns_lock);
594 list_for_each_safe(tmp, next, &list) {
595 struct lustre_handle lockh;
597 w = list_entry(tmp, struct ldlm_ast_work, w_list);
599 ldlm_lock2handle(w->w_lock, &lockh);
600 rc = ldlm_cli_cancel(&lockh);
602 CDEBUG(D_INFO, "ldlm_cli_cancel: %d\n", rc);
604 list_del(&w->w_list);
605 LDLM_LOCK_PUT(w->w_lock);
606 OBD_FREE(w, sizeof(*w));
612 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
613 struct ldlm_res_id res_id, int flags,
616 struct ldlm_resource *res;
617 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
618 struct ldlm_ast_work *w;
621 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
623 /* This is not a problem. */
624 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
628 l_lock(&ns->ns_lock);
629 list_for_each(tmp, &res->lr_granted) {
630 struct ldlm_lock *lock;
631 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
633 if (opaque != NULL && lock->l_data != opaque) {
634 LDLM_ERROR(lock, "data %p doesn't match opaque %p res"
635 LPU64":"LPU64, lock->l_data, opaque,
636 res_id.name[0], res_id.name[1]);
641 if (lock->l_readers || lock->l_writers) {
642 if (flags & LDLM_FL_WARN) {
643 LDLM_ERROR(lock, "lock in use");
649 /* See CBPENDING comment in ldlm_cancel_lru */
650 lock->l_flags |= LDLM_FL_CBPENDING;
652 OBD_ALLOC(w, sizeof(*w));
655 w->w_lock = LDLM_LOCK_GET(lock);
657 /* Prevent the cancel callback from being called by setting
658 * LDLM_FL_CANCEL in the lock. Very sneaky. -p */
659 if (flags & LDLM_FL_NO_CALLBACK)
660 w->w_lock->l_flags |= LDLM_FL_CANCEL;
662 list_add(&w->w_list, &list);
664 l_unlock(&ns->ns_lock);
666 list_for_each_safe(tmp, next, &list) {
667 struct lustre_handle lockh;
669 w = list_entry(tmp, struct ldlm_ast_work, w_list);
671 if (flags & LDLM_FL_LOCAL_ONLY) {
672 ldlm_lock_cancel(w->w_lock);
674 ldlm_lock2handle(w->w_lock, &lockh);
675 rc = ldlm_cli_cancel(&lockh);
677 CERROR("ldlm_cli_cancel: %d\n", rc);
679 list_del(&w->w_list);
680 LDLM_LOCK_PUT(w->w_lock);
681 OBD_FREE(w, sizeof(*w));
684 ldlm_resource_putref(res);
689 /* Cancel all locks on a namespace (or a specific resource, if given)
690 * that have 0 readers/writers.
692 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
693 * to notify the server.
694 * If flags & LDLM_FL_NO_CALLBACK, don't run the cancel callback.
695 * If flags & LDLM_FL_WARN, print a warning if some locks are still in use. */
696 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
697 struct ldlm_res_id *res_id, int flags, void *opaque)
706 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
709 l_lock(&ns->ns_lock);
710 for (i = 0; i < RES_HASH_SIZE; i++) {
711 struct list_head *tmp, *pos;
712 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
714 struct ldlm_resource *res;
715 res = list_entry(tmp, struct ldlm_resource, lr_hash);
716 ldlm_resource_getref(res);
718 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
722 CERROR("cancel_unused_res ("LPU64"): %d\n",
723 res->lr_name.name[0], rc);
724 ldlm_resource_putref(res);
727 l_unlock(&ns->ns_lock);
732 /* Lock iterators. */
734 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
737 struct list_head *tmp, *next;
738 struct ldlm_lock *lock;
739 int rc = LDLM_ITER_CONTINUE;
740 struct ldlm_namespace *ns = res->lr_namespace;
745 RETURN(LDLM_ITER_CONTINUE);
747 l_lock(&ns->ns_lock);
748 list_for_each_safe(tmp, next, &res->lr_granted) {
749 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
751 if (iter(lock, closure) == LDLM_ITER_STOP)
752 GOTO(out, rc = LDLM_ITER_STOP);
755 list_for_each_safe(tmp, next, &res->lr_converting) {
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_waiting) {
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 l_unlock(&ns->ns_lock);
773 struct iter_helper_data {
774 ldlm_iterator_t iter;
778 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
780 struct iter_helper_data *helper = closure;
781 return helper->iter(lock, helper->closure);
784 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
786 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
789 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
792 struct iter_helper_data helper = { iter: iter, closure: closure };
793 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
796 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
797 ldlm_res_iterator_t iter, void *closure)
799 int i, rc = LDLM_ITER_CONTINUE;
801 l_lock(&ns->ns_lock);
802 for (i = 0; i < RES_HASH_SIZE; i++) {
803 struct list_head *tmp, *next;
804 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
805 struct ldlm_resource *res =
806 list_entry(tmp, struct ldlm_resource, lr_hash);
808 ldlm_resource_getref(res);
809 rc = iter(res, closure);
810 ldlm_resource_putref(res);
811 if (rc == LDLM_ITER_STOP)
816 l_unlock(&ns->ns_lock);
822 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
824 struct list_head *list = closure;
826 /* we use l_pending_chain here, because it's unused on clients. */
827 list_add(&lock->l_pending_chain, list);
828 return LDLM_ITER_CONTINUE;
831 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
833 struct ptlrpc_request *req;
834 struct ldlm_request *body;
835 struct ldlm_reply *reply;
840 * If granted mode matches the requested mode, this lock is granted.
842 * If they differ, but we have a granted mode, then we were granted
843 * one mode and now want another: ergo, converting.
845 * If we haven't been granted anything and are on a resource list,
846 * then we're blocked/waiting.
848 * If we haven't been granted anything and we're NOT on a resource list,
849 * then we haven't got a reply yet and don't have a known disposition.
850 * This happens whenever a lock enqueue is the request that triggers
853 if (lock->l_granted_mode == lock->l_req_mode)
854 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
855 else if (lock->l_granted_mode)
856 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
857 else if (!list_empty(&lock->l_res_link))
858 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
860 flags = LDLM_FL_REPLAY;
862 size = sizeof(*body);
863 req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, &size, NULL);
867 /* We're part of recovery, so don't wait for it. */
868 req->rq_level = LUSTRE_CONN_RECOVD;
870 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
871 ldlm_lock2desc(lock, &body->lock_desc);
872 body->lock_flags = flags;
874 ldlm_lock2handle(lock, &body->lock_handle1);
875 size = sizeof(*reply);
876 req->rq_replen = lustre_msg_size(1, &size);
878 LDLM_DEBUG(lock, "replaying lock:");
879 rc = ptlrpc_queue_wait(req);
883 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
884 lustre_swab_ldlm_reply);
886 CERROR("Can't unpack ldlm_reply\n");
887 GOTO (out, rc = -EPROTO);
890 memcpy(&lock->l_remote_handle, &reply->lock_handle,
891 sizeof(lock->l_remote_handle));
892 LDLM_DEBUG(lock, "replayed lock:");
894 ptlrpc_req_finished(req);
898 int ldlm_replay_locks(struct obd_import *imp)
900 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
901 struct list_head list, *pos, *next;
902 struct ldlm_lock *lock;
906 INIT_LIST_HEAD(&list);
908 l_lock(&ns->ns_lock);
909 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
911 list_for_each_safe(pos, next, &list) {
912 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
913 rc = replay_one_lock(imp, lock);
915 break; /* or try to do the rest? */
917 l_unlock(&ns->ns_lock);