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 struct lock_wait_data lwd;
64 unsigned long irqflags;
65 struct obd_device *obd;
66 struct obd_import *imp = NULL;
68 struct l_wait_info lwi;
70 obd = class_conn2obd(lock->l_connh);
72 /* if this is a local lock, then there is no import */
74 imp = obd->u.cli.cl_import;
78 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ, ldlm_expired_completion_wait,
79 interrupted_completion_wait, &lwd);
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);
97 ldlm_reprocess_all(lock->l_resource);
101 spin_lock_irqsave(&imp->imp_lock, irqflags);
102 lwd.lwd_generation = imp->imp_generation;
103 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
106 /* Go to sleep until the lock is granted or cancelled. */
107 rc = l_wait_event(lock->l_waitq,
108 ((lock->l_req_mode == lock->l_granted_mode) ||
109 lock->l_destroyed), &lwi);
111 if (lock->l_destroyed) {
112 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
117 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
122 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
126 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
127 struct lustre_handle *parent_lockh,
128 struct ldlm_res_id res_id,
130 void *cookie, int cookielen,
133 ldlm_completion_callback completion,
134 ldlm_blocking_callback blocking,
136 struct lustre_handle *lockh)
138 struct ldlm_lock *lock;
143 CERROR("Trying to enqueue local lock in a shadow namespace\n");
147 lock = ldlm_lock_create(ns, parent_lockh, res_id, type, mode,
150 GOTO(out_nolock, err = -ENOMEM);
151 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
153 ldlm_lock_addref_internal(lock, mode);
154 ldlm_lock2handle(lock, lockh);
155 lock->l_flags |= LDLM_FL_LOCAL;
157 err = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags,
162 if (type == LDLM_EXTENT)
163 memcpy(cookie, &lock->l_extent, sizeof(lock->l_extent));
164 if ((*flags) & LDLM_FL_LOCK_CHANGED)
165 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
167 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
170 if (lock->l_completion_ast)
171 lock->l_completion_ast(lock, *flags, NULL);
173 LDLM_DEBUG(lock, "client-side local enqueue END");
181 int ldlm_cli_enqueue(struct lustre_handle *connh,
182 struct ptlrpc_request *req,
183 struct ldlm_namespace *ns,
184 struct lustre_handle *parent_lock_handle,
185 struct ldlm_res_id res_id,
187 void *cookie, int cookielen,
190 ldlm_completion_callback completion,
191 ldlm_blocking_callback blocking,
193 struct lustre_handle *lockh)
195 struct ldlm_lock *lock;
196 struct ldlm_request *body;
197 struct ldlm_reply *reply;
198 int rc, size = sizeof(*body), req_passed_in = 1, is_replay;
201 is_replay = *flags & LDLM_FL_REPLAY;
202 LASSERT(connh != NULL || !is_replay);
205 rc = ldlm_cli_enqueue_local(ns, parent_lock_handle, res_id,
206 type, cookie, cookielen, mode,
207 flags, completion, blocking, data,
212 /* If we're replaying this lock, just check some invariants.
213 * If we're creating a new lock, get everything all setup nice. */
215 lock = ldlm_handle2lock(lockh);
216 LDLM_DEBUG(lock, "client-side enqueue START");
217 LASSERT(connh == lock->l_connh);
219 lock = ldlm_lock_create(ns, parent_lock_handle, res_id, type,
220 mode, blocking, data);
222 GOTO(out_nolock, rc = -ENOMEM);
223 /* ugh. I set this early (instead of waiting for _enqueue)
224 * because the completion AST might arrive early, and we need
225 * (in just this one case) to run the completion_cb even if it
226 * arrives before the reply. */
227 lock->l_completion_ast = completion;
228 LDLM_DEBUG(lock, "client-side enqueue START");
229 /* for the local lock, add the reference */
230 ldlm_lock_addref_internal(lock, mode);
231 ldlm_lock2handle(lock, lockh);
232 if (type == LDLM_EXTENT)
233 memcpy(&lock->l_extent, cookie,
234 sizeof(body->lock_desc.l_extent));
238 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_ENQUEUE, 1,
241 GOTO(out, rc = -ENOMEM);
243 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
246 /* Dump lock data into the request buffer */
247 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
248 ldlm_lock2desc(lock, &body->lock_desc);
249 body->lock_flags = *flags;
251 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
252 if (parent_lock_handle)
253 memcpy(&body->lock_handle2, parent_lock_handle,
254 sizeof(body->lock_handle2));
256 /* Continue as normal. */
257 if (!req_passed_in) {
258 size = sizeof(*reply);
259 req->rq_replen = lustre_msg_size(1, &size);
261 lock->l_connh = connh;
262 lock->l_export = NULL;
263 lock->l_blocking_ast = blocking;
265 LDLM_DEBUG(lock, "sending request");
266 rc = ptlrpc_queue_wait(req);
268 if (rc != ELDLM_OK) {
270 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
271 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
272 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
273 l_lock(&ns->ns_lock);
274 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
275 l_unlock(&ns->ns_lock);
277 ldlm_lock_decref_and_cancel(lockh, mode);
279 if (rc == ELDLM_LOCK_ABORTED) {
280 /* caller expects reply buffer 0 to have been swabbed */
281 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
282 lustre_swab_ldlm_reply);
284 CERROR ("Can't unpack ldlm_reply\n");
285 GOTO (out_req, rc = -EPROTO);
291 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
292 lustre_swab_ldlm_reply);
294 CERROR ("Can't unpack ldlm_reply\n");
295 GOTO (out_req, rc = -EPROTO);
298 memcpy(&lock->l_remote_handle, &reply->lock_handle,
299 sizeof(lock->l_remote_handle));
300 *flags = reply->lock_flags;
302 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: %d\n", lock,
303 reply->lock_handle.cookie, *flags);
304 if (type == LDLM_EXTENT) {
305 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got "
306 "extent "LPU64" -> "LPU64"\n",
307 body->lock_desc.l_extent.start,
308 body->lock_desc.l_extent.end,
309 reply->lock_extent.start, reply->lock_extent.end);
311 if ((reply->lock_extent.end & ~PAGE_MASK) != ~PAGE_MASK) {
312 /* XXX Old versions of BA OST code have a fencepost bug
313 * which will cause them to grant a lock that's one
314 * byte too large. This can be safely removed after BA
315 * ships their next release -phik (02 Apr 2003) */
316 reply->lock_extent.end--;
317 } else if ((reply->lock_extent.start & ~PAGE_MASK) ==
319 reply->lock_extent.start++;
322 cookie = &reply->lock_extent; /* FIXME bug 267 */
323 cookielen = sizeof(reply->lock_extent);
326 /* If enqueue returned a blocked lock but the completion handler has
327 * already run, then it fixed up the resource and we don't need to do it
329 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
330 int newmode = reply->lock_mode;
332 if (newmode && newmode != lock->l_req_mode) {
333 LDLM_DEBUG(lock, "server returned different mode %s",
334 ldlm_lockname[newmode]);
335 lock->l_req_mode = newmode;
338 if (reply->lock_resource_name.name[0] !=
339 lock->l_resource->lr_name.name[0]) {
340 CDEBUG(D_INFO, "remote intent success, locking %ld "
342 (long)reply->lock_resource_name.name[0],
343 (long)lock->l_resource->lr_name.name[0]);
345 ldlm_lock_change_resource(ns, lock,
346 reply->lock_resource_name);
347 if (lock->l_resource == NULL) {
349 GOTO(out_req, rc = -ENOMEM);
351 LDLM_DEBUG(lock, "client-side enqueue, new resource");
356 l_lock(&ns->ns_lock);
357 lock->l_completion_ast = NULL;
358 rc = ldlm_lock_enqueue(ns, &lock, cookie, cookielen, flags,
360 l_unlock(&ns->ns_lock);
361 if (lock->l_completion_ast)
362 lock->l_completion_ast(lock, *flags, NULL);
365 LDLM_DEBUG(lock, "client-side enqueue END");
369 ptlrpc_req_finished(req);
376 int ldlm_match_or_enqueue(struct lustre_handle *connh,
377 struct ptlrpc_request *req,
378 struct ldlm_namespace *ns,
379 struct lustre_handle *parent_lock_handle,
380 struct ldlm_res_id res_id,
382 void *cookie, int cookielen,
385 ldlm_completion_callback completion,
386 ldlm_blocking_callback blocking,
388 struct lustre_handle *lockh)
393 /* Just to make sure that I understand things --phil */
394 LASSERT(*flags & LDLM_FL_LOCAL_ONLY);
397 LDLM_DEBUG_NOLOCK("resource "LPU64"/"LPU64, res_id.name[0],
399 rc = ldlm_lock_match(ns, *flags, &res_id, type, cookie, cookielen, mode,
402 rc = ldlm_cli_enqueue(connh, req, ns, parent_lock_handle,
403 res_id, type, cookie, cookielen, mode,
404 flags, completion, blocking, data,
407 CERROR("ldlm_cli_enqueue: err: %d\n", rc);
413 int ldlm_cli_replay_enqueue(struct ldlm_lock *lock)
415 struct lustre_handle lockh;
416 struct ldlm_res_id junk;
417 int flags = LDLM_FL_REPLAY;
418 ldlm_lock2handle(lock, &lockh);
419 return ldlm_cli_enqueue(lock->l_connh, NULL, NULL, NULL, junk,
420 lock->l_resource->lr_type, NULL, 0, -1, &flags,
421 NULL, NULL, NULL, &lockh);
424 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
428 if (lock->l_resource->lr_namespace->ns_client) {
429 CERROR("Trying to cancel local lock\n");
432 LDLM_DEBUG(lock, "client-side local convert");
434 ldlm_lock_convert(lock, new_mode, flags);
435 ldlm_reprocess_all(lock->l_resource);
437 LDLM_DEBUG(lock, "client-side local convert handler END");
442 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
443 * conversion of locks which are on the waiting or converting queue */
444 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
446 struct ldlm_request *body;
447 struct lustre_handle *connh;
448 struct ldlm_reply *reply;
449 struct ldlm_lock *lock;
450 struct ldlm_resource *res;
451 struct ptlrpc_request *req;
452 int rc, size = sizeof(*body);
455 lock = ldlm_handle2lock(lockh);
461 connh = lock->l_connh;
464 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
466 LDLM_DEBUG(lock, "client-side convert");
468 req = ptlrpc_prep_req(class_conn2cliimp(connh), LDLM_CONVERT, 1, &size,
471 GOTO(out, rc = -ENOMEM);
473 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
474 memcpy(&body->lock_handle1, &lock->l_remote_handle,
475 sizeof(body->lock_handle1));
477 body->lock_desc.l_req_mode = new_mode;
478 body->lock_flags = *flags;
480 size = sizeof(*reply);
481 req->rq_replen = lustre_msg_size(1, &size);
483 rc = ptlrpc_queue_wait(req);
487 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
488 lustre_swab_ldlm_reply);
490 CERROR ("Can't unpack ldlm_reply\n");
491 GOTO (out, rc = -EPROTO);
494 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
496 ldlm_reprocess_all(res);
497 /* Go to sleep until the lock is granted. */
498 /* FIXME: or cancelled. */
499 if (lock->l_completion_ast)
500 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
504 ptlrpc_req_finished(req);
508 int ldlm_cli_cancel(struct lustre_handle *lockh)
510 struct ptlrpc_request *req;
511 struct ldlm_lock *lock;
512 struct ldlm_request *body;
513 int rc = 0, size = sizeof(*body);
516 /* concurrent cancels on the same handle can happen */
517 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
523 struct obd_import *imp;
525 LDLM_DEBUG(lock, "client-side cancel");
526 /* Set this flag to prevent others from getting new references*/
527 l_lock(&lock->l_resource->lr_namespace->ns_lock);
528 lock->l_flags |= LDLM_FL_CBPENDING;
529 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
530 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
531 ldlm_cancel_callback(lock);
534 CDEBUG(D_INFO, "not sending request (at caller's "
539 imp = class_conn2cliimp(lock->l_connh);
540 if (imp == NULL || imp->imp_invalid) {
541 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
546 req = ptlrpc_prep_req(imp, LDLM_CANCEL, 1, &size, NULL);
548 GOTO(out, rc = -ENOMEM);
550 /* XXX FIXME bug 249 */
551 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
552 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
554 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
555 memcpy(&body->lock_handle1, &lock->l_remote_handle,
556 sizeof(body->lock_handle1));
558 req->rq_replen = lustre_msg_size(0, NULL);
560 rc = ptlrpc_queue_wait(req);
563 CERROR("client/server (nid "LPU64") out of sync--not "
565 req->rq_import->imp_connection->c_peer.peer_nid);
566 else if (rc != ELDLM_OK)
567 CERROR("Got rc %d from cancel RPC: canceling "
570 ptlrpc_req_finished(req);
572 ldlm_lock_cancel(lock);
574 LDLM_DEBUG(lock, "client-side local cancel");
575 if (lock->l_resource->lr_namespace->ns_client) {
576 CERROR("Trying to cancel local lock\n");
579 ldlm_lock_cancel(lock);
580 ldlm_reprocess_all(lock->l_resource);
581 LDLM_DEBUG(lock, "client-side local cancel handler END");
590 int ldlm_cancel_lru(struct ldlm_namespace *ns)
592 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
594 struct ldlm_ast_work *w;
597 l_lock(&ns->ns_lock);
598 count = ns->ns_nr_unused - ns->ns_max_unused;
601 l_unlock(&ns->ns_lock);
605 list_for_each_safe(tmp, next, &ns->ns_unused_list) {
606 struct ldlm_lock *lock;
607 lock = list_entry(tmp, struct ldlm_lock, l_lru);
609 LASSERT(!lock->l_readers && !lock->l_writers);
611 /* Setting the CBPENDING flag is a little misleading, but
612 * prevents an important race; namely, once CBPENDING is set,
613 * the lock can accumulate no more readers/writers. Since
614 * readers and writers are already zero here, ldlm_lock_decref
615 * won't see this flag and call l_blocking_ast */
616 lock->l_flags |= LDLM_FL_CBPENDING;
618 OBD_ALLOC(w, sizeof(*w));
621 w->w_lock = LDLM_LOCK_GET(lock);
622 list_add(&w->w_list, &list);
623 ldlm_lock_remove_from_lru(lock);
628 l_unlock(&ns->ns_lock);
630 list_for_each_safe(tmp, next, &list) {
631 struct lustre_handle lockh;
633 w = list_entry(tmp, struct ldlm_ast_work, w_list);
635 ldlm_lock2handle(w->w_lock, &lockh);
636 rc = ldlm_cli_cancel(&lockh);
638 CDEBUG(D_INFO, "ldlm_cli_cancel: %d\n", rc);
640 list_del(&w->w_list);
641 LDLM_LOCK_PUT(w->w_lock);
642 OBD_FREE(w, sizeof(*w));
648 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
649 struct ldlm_res_id res_id, int flags,
652 struct ldlm_resource *res;
653 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
654 struct ldlm_ast_work *w;
657 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
659 /* This is not a problem. */
660 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
664 l_lock(&ns->ns_lock);
665 list_for_each(tmp, &res->lr_granted) {
666 struct ldlm_lock *lock;
667 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
669 if (lock->l_readers || lock->l_writers) {
670 if (flags & LDLM_FL_WARN) {
671 LDLM_ERROR(lock, "lock in use");
675 if (opaque != NULL && lock->l_data != opaque) {
676 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
677 lock->l_data, opaque);
681 /* See CBPENDING comment in ldlm_cancel_lru */
682 lock->l_flags |= LDLM_FL_CBPENDING;
684 OBD_ALLOC(w, sizeof(*w));
687 w->w_lock = LDLM_LOCK_GET(lock);
689 /* Prevent the cancel callback from being called by setting
690 * LDLM_FL_CANCEL in the lock. Very sneaky. -p */
691 if (flags & LDLM_FL_NO_CALLBACK)
692 w->w_lock->l_flags |= LDLM_FL_CANCEL;
694 list_add(&w->w_list, &list);
696 l_unlock(&ns->ns_lock);
698 list_for_each_safe(tmp, next, &list) {
699 struct lustre_handle lockh;
701 w = list_entry(tmp, struct ldlm_ast_work, w_list);
703 if (flags & LDLM_FL_LOCAL_ONLY) {
704 ldlm_lock_cancel(w->w_lock);
706 ldlm_lock2handle(w->w_lock, &lockh);
707 rc = ldlm_cli_cancel(&lockh);
709 CERROR("ldlm_cli_cancel: %d\n", rc);
711 list_del(&w->w_list);
712 LDLM_LOCK_PUT(w->w_lock);
713 OBD_FREE(w, sizeof(*w));
716 ldlm_resource_putref(res);
721 /* Cancel all locks on a namespace (or a specific resource, if given)
722 * that have 0 readers/writers.
724 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
725 * to notify the server.
726 * If flags & LDLM_FL_NO_CALLBACK, don't run the cancel callback.
727 * If flags & LDLM_FL_WARN, print a warning if some locks are still in use. */
728 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
729 struct ldlm_res_id *res_id, int flags, void *opaque)
738 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
741 l_lock(&ns->ns_lock);
742 for (i = 0; i < RES_HASH_SIZE; i++) {
743 struct list_head *tmp, *pos;
744 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
746 struct ldlm_resource *res;
747 res = list_entry(tmp, struct ldlm_resource, lr_hash);
748 ldlm_resource_getref(res);
750 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
754 CERROR("cancel_unused_res ("LPU64"): %d\n",
755 res->lr_name.name[0], rc);
756 ldlm_resource_putref(res);
759 l_unlock(&ns->ns_lock);
764 /* Lock iterators. */
766 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
769 struct list_head *tmp, *next;
770 struct ldlm_lock *lock;
771 int rc = LDLM_ITER_CONTINUE;
772 struct ldlm_namespace *ns = res->lr_namespace;
777 RETURN(LDLM_ITER_CONTINUE);
779 l_lock(&ns->ns_lock);
780 list_for_each_safe(tmp, next, &res->lr_granted) {
781 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
783 if (iter(lock, closure) == LDLM_ITER_STOP)
784 GOTO(out, rc = LDLM_ITER_STOP);
787 list_for_each_safe(tmp, next, &res->lr_converting) {
788 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
790 if (iter(lock, closure) == LDLM_ITER_STOP)
791 GOTO(out, rc = LDLM_ITER_STOP);
794 list_for_each_safe(tmp, next, &res->lr_waiting) {
795 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
797 if (iter(lock, closure) == LDLM_ITER_STOP)
798 GOTO(out, rc = LDLM_ITER_STOP);
801 l_unlock(&ns->ns_lock);
805 struct iter_helper_data {
806 ldlm_iterator_t iter;
810 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
812 struct iter_helper_data *helper = closure;
813 return helper->iter(lock, helper->closure);
816 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
818 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
821 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
824 struct iter_helper_data helper = { iter: iter, closure: closure };
825 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
828 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
829 ldlm_res_iterator_t iter, void *closure)
831 int i, rc = LDLM_ITER_CONTINUE;
833 l_lock(&ns->ns_lock);
834 for (i = 0; i < RES_HASH_SIZE; i++) {
835 struct list_head *tmp, *next;
836 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
837 struct ldlm_resource *res =
838 list_entry(tmp, struct ldlm_resource, lr_hash);
840 ldlm_resource_getref(res);
841 rc = iter(res, closure);
842 ldlm_resource_putref(res);
843 if (rc == LDLM_ITER_STOP)
848 l_unlock(&ns->ns_lock);
854 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
856 struct list_head *list = closure;
858 /* we use l_pending_chain here, because it's unused on clients. */
859 list_add(&lock->l_pending_chain, list);
860 return LDLM_ITER_CONTINUE;
863 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
865 struct ptlrpc_request *req;
866 struct ldlm_request *body;
867 struct ldlm_reply *reply;
872 * If granted mode matches the requested mode, this lock is granted.
874 * If they differ, but we have a granted mode, then we were granted
875 * one mode and now want another: ergo, converting.
877 * If we haven't been granted anything and are on a resource list,
878 * then we're blocked/waiting.
880 * If we haven't been granted anything and we're NOT on a resource list,
881 * then we haven't got a reply yet and don't have a known disposition.
882 * This happens whenever a lock enqueue is the request that triggers
885 if (lock->l_granted_mode == lock->l_req_mode)
886 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
887 else if (lock->l_granted_mode)
888 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
889 else if (!list_empty(&lock->l_res_link))
890 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
892 flags = LDLM_FL_REPLAY;
894 size = sizeof(*body);
895 req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, &size, NULL);
899 /* We're part of recovery, so don't wait for it. */
900 req->rq_level = LUSTRE_CONN_RECOVD;
902 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
903 ldlm_lock2desc(lock, &body->lock_desc);
904 body->lock_flags = flags;
906 ldlm_lock2handle(lock, &body->lock_handle1);
907 size = sizeof(*reply);
908 req->rq_replen = lustre_msg_size(1, &size);
910 LDLM_DEBUG(lock, "replaying lock:");
911 rc = ptlrpc_queue_wait(req);
915 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
916 lustre_swab_ldlm_reply);
918 CERROR("Can't unpack ldlm_reply\n");
919 GOTO (out, rc = -EPROTO);
922 memcpy(&lock->l_remote_handle, &reply->lock_handle,
923 sizeof(lock->l_remote_handle));
924 LDLM_DEBUG(lock, "replayed lock:");
926 ptlrpc_req_finished(req);
930 int ldlm_replay_locks(struct obd_import *imp)
932 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
933 struct list_head list, *pos, *next;
934 struct ldlm_lock *lock;
938 INIT_LIST_HEAD(&list);
940 l_lock(&ns->ns_lock);
941 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
943 list_for_each_safe(pos, next, &list) {
944 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
945 rc = replay_one_lock(imp, lock);
947 break; /* or try to do the rest? */
949 l_unlock(&ns->ns_lock);