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 static unsigned long next_dump = 0;
53 LDLM_ERROR(lock, "lock timed out; not entering recovery in "
54 "server code, just going back to sleep");
55 if (time_after(jiffies, next_dump)) {
56 ldlm_namespace_dump(lock->l_resource->lr_namespace);
58 portals_debug_dumplog();
59 next_dump = jiffies + 300 * HZ;
64 obd = lock->l_conn_export->exp_obd;
65 imp = obd->u.cli.cl_import;
66 ptlrpc_fail_import(imp, lwd->lwd_generation);
67 LDLM_ERROR(lock, "lock timed out, entering recovery for %s@%s",
68 imp->imp_target_uuid.uuid,
69 imp->imp_connection->c_remote_uuid.uuid);
74 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
76 /* XXX ALLOCATE - 160 bytes */
77 struct lock_wait_data lwd;
78 unsigned long irqflags;
79 struct obd_device *obd;
80 struct obd_import *imp = NULL;
81 struct l_wait_info lwi;
85 if (flags == LDLM_FL_WAIT_NOREPROC)
88 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
89 LDLM_FL_BLOCK_CONV))) {
90 wake_up(&lock->l_waitq);
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_flags & LDLM_FL_FAILED)), &lwi);
122 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
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 ldlm_res_id res_id,
140 ldlm_policy_data_t *policy,
143 ldlm_blocking_callback blocking,
144 ldlm_completion_callback completion,
145 ldlm_glimpse_callback glimpse,
146 void *data, __u32 lvb_len,
148 struct lustre_handle *lockh)
150 struct ldlm_lock *lock;
155 CERROR("Trying to enqueue local lock in a shadow namespace\n");
159 lock = ldlm_lock_create(ns, NULL, res_id, type, mode, blocking,
160 completion, glimpse, data, lvb_len);
162 GOTO(out_nolock, err = -ENOMEM);
163 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
165 ldlm_lock_addref_internal(lock, mode);
166 ldlm_lock2handle(lock, lockh);
167 lock->l_flags |= LDLM_FL_LOCAL;
168 lock->l_lvb_swabber = lvb_swabber;
170 memcpy(&lock->l_policy_data, policy, sizeof(*policy));
171 if (type == LDLM_EXTENT)
172 memcpy(&lock->l_req_extent, &policy->l_extent,
173 sizeof(policy->l_extent));
175 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
180 memcpy(policy, &lock->l_policy_data, sizeof(*policy));
181 if ((*flags) & LDLM_FL_LOCK_CHANGED)
182 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
184 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
187 if (lock->l_completion_ast)
188 lock->l_completion_ast(lock, *flags, NULL);
190 LDLM_DEBUG(lock, "client-side local enqueue END");
198 static void failed_lock_cleanup(struct ldlm_namespace *ns,
199 struct ldlm_lock *lock,
200 struct lustre_handle *lockh, int mode)
202 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
203 l_lock(&ns->ns_lock);
204 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
205 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
206 l_unlock(&ns->ns_lock);
208 ldlm_lock_decref_and_cancel(lockh, mode);
211 int ldlm_cli_enqueue(struct obd_export *exp,
212 struct ptlrpc_request *req,
213 struct ldlm_namespace *ns,
214 struct ldlm_res_id res_id,
216 ldlm_policy_data_t *policy,
219 ldlm_blocking_callback blocking,
220 ldlm_completion_callback completion,
221 ldlm_glimpse_callback glimpse,
226 struct lustre_handle *lockh)
228 struct ldlm_lock *lock;
229 struct ldlm_request *body;
230 struct ldlm_reply *reply;
231 int rc, size[2] = {sizeof(*body), lvb_len}, req_passed_in = 1;
232 int is_replay = *flags & LDLM_FL_REPLAY;
237 rc = ldlm_cli_enqueue_local(ns, res_id, type, policy, mode,
238 flags, blocking, completion,
239 glimpse, data, lvb_len, lvb_swabber,
244 /* If we're replaying this lock, just check some invariants.
245 * If we're creating a new lock, get everything all setup nice. */
247 lock = ldlm_handle2lock(lockh);
248 LDLM_DEBUG(lock, "client-side enqueue START");
249 LASSERT(exp == lock->l_conn_export);
251 lock = ldlm_lock_create(ns, NULL, res_id, type, mode, blocking,
252 completion, glimpse, data, lvb_len);
254 GOTO(out_nolock, rc = -ENOMEM);
255 /* for the local lock, add the reference */
256 ldlm_lock_addref_internal(lock, mode);
257 ldlm_lock2handle(lock, lockh);
258 lock->l_lvb_swabber = lvb_swabber;
260 memcpy(&lock->l_policy_data, policy, sizeof(*policy));
261 if (type == LDLM_EXTENT)
262 memcpy(&lock->l_req_extent, &policy->l_extent,
263 sizeof(policy->l_extent));
264 LDLM_DEBUG(lock, "client-side enqueue START");
268 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 1,
271 GOTO(out_lock, rc = -ENOMEM);
273 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
276 /* Dump lock data into the request buffer */
277 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
278 ldlm_lock2desc(lock, &body->lock_desc);
279 body->lock_flags = *flags;
281 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
283 /* Continue as normal. */
284 if (!req_passed_in) {
288 size[0] = sizeof(*reply);
289 req->rq_replen = lustre_msg_size(buffers, size);
291 lock->l_conn_export = exp;
292 lock->l_export = NULL;
293 lock->l_blocking_ast = blocking;
295 LDLM_DEBUG(lock, "sending request");
296 rc = ptlrpc_queue_wait(req);
298 if (rc != ELDLM_OK) {
300 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
301 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
302 if (rc == ELDLM_LOCK_ABORTED) {
303 /* Before we return, swab the reply */
304 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
305 lustre_swab_ldlm_reply);
307 CERROR("Can't unpack ldlm_reply\n");
312 tmplvb = lustre_swab_repbuf(req, 1, lvb_len,
315 GOTO(out_lock, rc = -EPROTO);
317 memcpy(lvb, tmplvb, lvb_len);
323 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
324 lustre_swab_ldlm_reply);
326 CERROR("Can't unpack ldlm_reply\n");
327 GOTO(out_lock, rc = -EPROTO);
330 memcpy(&lock->l_remote_handle, &reply->lock_handle,
331 sizeof(lock->l_remote_handle));
332 *flags = reply->lock_flags;
334 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
335 lock, reply->lock_handle.cookie, *flags);
337 /* If enqueue returned a blocked lock but the completion handler has
338 * already run, then it fixed up the resource and we don't need to do it
340 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
341 int newmode = reply->lock_desc.l_req_mode;
343 if (newmode && newmode != lock->l_req_mode) {
344 LDLM_DEBUG(lock, "server returned different mode %s",
345 ldlm_lockname[newmode]);
346 lock->l_req_mode = newmode;
349 if (reply->lock_desc.l_resource.lr_name.name[0] !=
350 lock->l_resource->lr_name.name[0]) {
351 CDEBUG(D_INFO, "remote intent success, locking %ld "
353 (long)reply->lock_desc.l_resource.lr_name.name[0],
354 (long)lock->l_resource->lr_name.name[0]);
356 ldlm_lock_change_resource(ns, lock,
357 reply->lock_desc.l_resource.lr_name);
358 if (lock->l_resource == NULL) {
360 GOTO(out_lock, rc = -ENOMEM);
362 LDLM_DEBUG(lock, "client-side enqueue, new resource");
365 memcpy(&lock->l_policy_data,
366 &reply->lock_desc.l_policy_data,
367 sizeof(reply->lock_desc.l_policy_data));
368 if (type != LDLM_PLAIN)
369 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
372 if ((*flags) & LDLM_FL_AST_SENT) {
373 l_lock(&ns->ns_lock);
374 lock->l_flags |= LDLM_FL_CBPENDING;
375 l_unlock(&ns->ns_lock);
376 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
379 /* If the lock has already been granted by a completion AST, don't
380 * clobber the LVB with an older one. */
381 if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
383 tmplvb = lustre_swab_repbuf(req, 1, lvb_len, lvb_swabber);
385 GOTO(out_lock, rc = -EPROTO);
386 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
390 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
391 if (lock->l_completion_ast != NULL) {
392 int err = lock->l_completion_ast(lock, *flags, NULL);
398 if (lvb_len && lvb != NULL) {
399 /* Copy the LVB here, and not earlier, because the completion
400 * AST (if any) can override what we got in the reply */
401 memcpy(lvb, lock->l_lvb_data, lvb_len);
404 LDLM_DEBUG(lock, "client-side enqueue END");
408 failed_lock_cleanup(ns, lock, lockh, mode);
409 if (!req_passed_in && req != NULL)
410 ptlrpc_req_finished(req);
416 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
420 if (lock->l_resource->lr_namespace->ns_client) {
421 CERROR("Trying to cancel local lock\n");
424 LDLM_DEBUG(lock, "client-side local convert");
426 ldlm_lock_convert(lock, new_mode, flags);
427 ldlm_reprocess_all(lock->l_resource);
429 LDLM_DEBUG(lock, "client-side local convert handler END");
434 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
435 * conversion of locks which are on the waiting or converting queue */
436 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
438 struct ldlm_request *body;
439 struct ldlm_reply *reply;
440 struct ldlm_lock *lock;
441 struct ldlm_resource *res;
442 struct ptlrpc_request *req;
443 int rc, size = sizeof(*body);
446 lock = ldlm_handle2lock(lockh);
453 if (lock->l_conn_export == NULL)
454 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
456 LDLM_DEBUG(lock, "client-side convert");
458 req = ptlrpc_prep_req(class_exp2cliimp(lock->l_conn_export),
459 LDLM_CONVERT, 1, &size, NULL);
461 GOTO(out, rc = -ENOMEM);
463 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
464 memcpy(&body->lock_handle1, &lock->l_remote_handle,
465 sizeof(body->lock_handle1));
467 body->lock_desc.l_req_mode = new_mode;
468 body->lock_flags = *flags;
470 size = sizeof(*reply);
471 req->rq_replen = lustre_msg_size(1, &size);
473 rc = ptlrpc_queue_wait(req);
477 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
478 lustre_swab_ldlm_reply);
480 CERROR ("Can't unpack ldlm_reply\n");
481 GOTO (out, rc = -EPROTO);
484 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
486 ldlm_reprocess_all(res);
487 /* Go to sleep until the lock is granted. */
488 /* FIXME: or cancelled. */
489 if (lock->l_completion_ast)
490 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
494 ptlrpc_req_finished(req);
498 int ldlm_cli_cancel(struct lustre_handle *lockh)
500 struct ptlrpc_request *req;
501 struct ldlm_lock *lock;
502 struct ldlm_request *body;
503 int rc = 0, size = sizeof(*body);
506 /* concurrent cancels on the same handle can happen */
507 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
511 if (lock->l_conn_export) {
513 struct obd_import *imp;
515 LDLM_DEBUG(lock, "client-side cancel");
516 /* Set this flag to prevent others from getting new references*/
517 l_lock(&lock->l_resource->lr_namespace->ns_lock);
518 lock->l_flags |= LDLM_FL_CBPENDING;
519 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
520 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
521 ldlm_cancel_callback(lock);
524 CDEBUG(D_INFO, "not sending request (at caller's "
530 imp = class_exp2cliimp(lock->l_conn_export);
531 if (imp == NULL || imp->imp_invalid) {
532 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
537 req = ptlrpc_prep_req(imp, LDLM_CANCEL, 1, &size, NULL);
539 GOTO(out, rc = -ENOMEM);
540 req->rq_no_resend = 1;
542 /* XXX FIXME bug 249 */
543 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
544 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
546 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
547 memcpy(&body->lock_handle1, &lock->l_remote_handle,
548 sizeof(body->lock_handle1));
550 req->rq_replen = lustre_msg_size(0, NULL);
552 rc = ptlrpc_queue_wait(req);
555 CERROR("client/server (nid "LPU64") out of sync--not "
557 req->rq_import->imp_connection->c_peer.peer_nid);
558 } else if (rc == -ETIMEDOUT) {
559 ptlrpc_req_finished(req);
561 } else if (rc != ELDLM_OK) {
562 CERROR("Got rc %d from cancel RPC: canceling "
566 ptlrpc_req_finished(req);
568 ldlm_lock_cancel(lock);
570 if (lock->l_resource->lr_namespace->ns_client) {
571 LDLM_ERROR(lock, "Trying to cancel local lock\n");
574 LDLM_DEBUG(lock, "client-side local cancel");
575 ldlm_lock_cancel(lock);
576 ldlm_reprocess_all(lock->l_resource);
577 LDLM_DEBUG(lock, "client-side local cancel handler END");
586 int ldlm_cancel_lru(struct ldlm_namespace *ns)
588 struct list_head *tmp, *next;
592 l_lock(&ns->ns_lock);
593 count = ns->ns_nr_unused - ns->ns_max_unused;
596 l_unlock(&ns->ns_lock);
600 list_for_each_safe(tmp, next, &ns->ns_unused_list) {
601 struct ldlm_lock *lock;
602 lock = list_entry(tmp, struct ldlm_lock, l_lru);
604 LASSERT(!lock->l_readers && !lock->l_writers);
606 /* Setting the CBPENDING flag is a little misleading, but
607 * prevents an important race; namely, once CBPENDING is set,
608 * the lock can accumulate no more readers/writers. Since
609 * readers and writers are already zero here, ldlm_lock_decref
610 * won't see this flag and call l_blocking_ast */
611 lock->l_flags |= LDLM_FL_CBPENDING;
613 LDLM_LOCK_GET(lock); /* dropped by bl thread */
614 ldlm_lock_remove_from_lru(lock);
615 ldlm_bl_to_thread(ns, NULL, lock);
620 l_unlock(&ns->ns_lock);
624 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
625 struct ldlm_res_id res_id, int flags,
628 struct ldlm_resource *res;
629 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
630 struct ldlm_ast_work *w;
633 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
635 /* This is not a problem. */
636 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
640 l_lock(&ns->ns_lock);
641 list_for_each(tmp, &res->lr_granted) {
642 struct ldlm_lock *lock;
643 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
645 if (opaque != NULL && lock->l_ast_data != opaque) {
646 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
647 lock->l_ast_data, opaque);
652 if (lock->l_readers || lock->l_writers) {
653 if (flags & LDLM_FL_WARN) {
654 LDLM_ERROR(lock, "lock in use");
660 /* See CBPENDING comment in ldlm_cancel_lru */
661 lock->l_flags |= LDLM_FL_CBPENDING;
663 OBD_ALLOC(w, sizeof(*w));
666 w->w_lock = LDLM_LOCK_GET(lock);
668 list_add(&w->w_list, &list);
670 l_unlock(&ns->ns_lock);
672 list_for_each_safe(tmp, next, &list) {
673 struct lustre_handle lockh;
675 w = list_entry(tmp, struct ldlm_ast_work, w_list);
677 if (flags & LDLM_FL_LOCAL_ONLY) {
678 ldlm_lock_cancel(w->w_lock);
680 ldlm_lock2handle(w->w_lock, &lockh);
681 rc = ldlm_cli_cancel(&lockh);
683 CERROR("ldlm_cli_cancel: %d\n", rc);
685 list_del(&w->w_list);
686 LDLM_LOCK_PUT(w->w_lock);
687 OBD_FREE(w, sizeof(*w));
690 ldlm_resource_putref(res);
695 /* Cancel all locks on a namespace (or a specific resource, if given)
696 * that have 0 readers/writers.
698 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
699 * to notify the server.
700 * If flags & LDLM_FL_WARN, print a warning if some locks are still in use. */
701 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
702 struct ldlm_res_id *res_id, int flags, void *opaque)
711 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
714 l_lock(&ns->ns_lock);
715 for (i = 0; i < RES_HASH_SIZE; i++) {
716 struct list_head *tmp, *pos;
717 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
719 struct ldlm_resource *res;
720 res = list_entry(tmp, struct ldlm_resource, lr_hash);
721 ldlm_resource_getref(res);
723 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
727 CERROR("cancel_unused_res ("LPU64"): %d\n",
728 res->lr_name.name[0], rc);
729 ldlm_resource_putref(res);
732 l_unlock(&ns->ns_lock);
737 /* Lock iterators. */
739 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
742 struct list_head *tmp, *next;
743 struct ldlm_lock *lock;
744 int rc = LDLM_ITER_CONTINUE;
745 struct ldlm_namespace *ns = res->lr_namespace;
750 RETURN(LDLM_ITER_CONTINUE);
752 l_lock(&ns->ns_lock);
753 list_for_each_safe(tmp, next, &res->lr_granted) {
754 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
756 if (iter(lock, closure) == LDLM_ITER_STOP)
757 GOTO(out, rc = LDLM_ITER_STOP);
760 list_for_each_safe(tmp, next, &res->lr_converting) {
761 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
763 if (iter(lock, closure) == LDLM_ITER_STOP)
764 GOTO(out, rc = LDLM_ITER_STOP);
767 list_for_each_safe(tmp, next, &res->lr_waiting) {
768 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
770 if (iter(lock, closure) == LDLM_ITER_STOP)
771 GOTO(out, rc = LDLM_ITER_STOP);
774 l_unlock(&ns->ns_lock);
778 struct iter_helper_data {
779 ldlm_iterator_t iter;
783 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
785 struct iter_helper_data *helper = closure;
786 return helper->iter(lock, helper->closure);
789 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
791 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
794 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
797 struct iter_helper_data helper = { iter: iter, closure: closure };
798 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
801 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
802 ldlm_res_iterator_t iter, void *closure)
804 int i, rc = LDLM_ITER_CONTINUE;
806 l_lock(&ns->ns_lock);
807 for (i = 0; i < RES_HASH_SIZE; i++) {
808 struct list_head *tmp, *next;
809 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
810 struct ldlm_resource *res =
811 list_entry(tmp, struct ldlm_resource, lr_hash);
813 ldlm_resource_getref(res);
814 rc = iter(res, closure);
815 ldlm_resource_putref(res);
816 if (rc == LDLM_ITER_STOP)
821 l_unlock(&ns->ns_lock);
825 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
826 void ldlm_change_cbdata(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
827 ldlm_iterator_t iter, void *data)
829 struct ldlm_resource *res;
833 CERROR("must pass in namespace");
837 res = ldlm_resource_get(ns, NULL, *res_id, 0, 0);
843 l_lock(&ns->ns_lock);
844 ldlm_resource_foreach(res, iter, data);
845 l_unlock(&ns->ns_lock);
846 ldlm_resource_putref(res);
852 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
854 struct list_head *list = closure;
856 /* we use l_pending_chain here, because it's unused on clients. */
857 list_add(&lock->l_pending_chain, list);
858 return LDLM_ITER_CONTINUE;
861 static int replay_lock_interpret(struct ptlrpc_request *req,
864 struct ldlm_lock *lock;
865 struct ldlm_reply *reply;
867 atomic_dec(&req->rq_import->imp_replay_inflight);
871 lock = req->rq_async_args.pointer_arg[0];
872 LASSERT(lock != NULL);
874 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
875 lustre_swab_ldlm_reply);
877 CERROR("Can't unpack ldlm_reply\n");
878 GOTO (out, rc = -EPROTO);
881 memcpy(&lock->l_remote_handle, &reply->lock_handle,
882 sizeof(lock->l_remote_handle));
883 LDLM_DEBUG(lock, "replayed lock:");
884 ptlrpc_import_recovery_state_machine(req->rq_import);
889 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
891 struct ptlrpc_request *req;
892 struct ldlm_request *body;
893 struct ldlm_reply *reply;
899 * If granted mode matches the requested mode, this lock is granted.
901 * If they differ, but we have a granted mode, then we were granted
902 * one mode and now want another: ergo, converting.
904 * If we haven't been granted anything and are on a resource list,
905 * then we're blocked/waiting.
907 * If we haven't been granted anything and we're NOT on a resource list,
908 * then we haven't got a reply yet and don't have a known disposition.
909 * This happens whenever a lock enqueue is the request that triggers
912 if (lock->l_granted_mode == lock->l_req_mode)
913 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
914 else if (lock->l_granted_mode)
915 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
916 else if (!list_empty(&lock->l_res_link))
917 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
919 flags = LDLM_FL_REPLAY;
921 size[0] = sizeof(*body);
922 req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, size, NULL);
926 /* We're part of recovery, so don't wait for it. */
927 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
929 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
930 ldlm_lock2desc(lock, &body->lock_desc);
931 body->lock_flags = flags;
933 ldlm_lock2handle(lock, &body->lock_handle1);
934 size[0] = sizeof(*reply);
935 if (lock->l_lvb_len != 0) {
937 size[1] = lock->l_lvb_len;
939 req->rq_replen = lustre_msg_size(buffers, size);
941 LDLM_DEBUG(lock, "replaying lock:");
943 atomic_inc(&req->rq_import->imp_replay_inflight);
944 req->rq_async_args.pointer_arg[0] = lock;
945 req->rq_interpret_reply = replay_lock_interpret;
946 ptlrpcd_add_req(req);
951 int ldlm_replay_locks(struct obd_import *imp)
953 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
954 struct list_head list, *pos, *next;
955 struct ldlm_lock *lock;
959 INIT_LIST_HEAD(&list);
961 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
963 /* ensure this doesn't fall to 0 before all have been queued */
964 atomic_inc(&imp->imp_replay_inflight);
966 l_lock(&ns->ns_lock);
967 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
969 list_for_each_safe(pos, next, &list) {
970 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
971 rc = replay_one_lock(imp, lock);
973 break; /* or try to do the rest? */
975 l_unlock(&ns->ns_lock);
977 atomic_dec(&imp->imp_replay_inflight);