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_CANCEL)), &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 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);
336 if (type == LDLM_EXTENT) {
337 CDEBUG(D_INFO, "requested extent: "LPU64" -> "LPU64", got "
338 "extent "LPU64" -> "LPU64"\n",
339 body->lock_desc.l_policy_data.l_extent.start,
340 body->lock_desc.l_policy_data.l_extent.end,
341 reply->lock_desc.l_policy_data.l_extent.start,
342 reply->lock_desc.l_policy_data.l_extent.end);
345 memcpy(&lock->l_policy_data, &reply->lock_desc.l_policy_data,
346 sizeof(reply->lock_desc.l_policy_data));
348 /* If enqueue returned a blocked lock but the completion handler has
349 * already run, then it fixed up the resource and we don't need to do it
351 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
352 int newmode = reply->lock_desc.l_req_mode;
354 if (newmode && newmode != lock->l_req_mode) {
355 LDLM_DEBUG(lock, "server returned different mode %s",
356 ldlm_lockname[newmode]);
357 lock->l_req_mode = newmode;
360 if (reply->lock_desc.l_resource.lr_name.name[0] !=
361 lock->l_resource->lr_name.name[0]) {
362 CDEBUG(D_INFO, "remote intent success, locking %ld "
364 (long)reply->lock_desc.l_resource.lr_name.name[0],
365 (long)lock->l_resource->lr_name.name[0]);
367 ldlm_lock_change_resource(ns, lock,
368 reply->lock_desc.l_resource.lr_name);
369 if (lock->l_resource == NULL) {
371 GOTO(out_lock, rc = -ENOMEM);
373 LDLM_DEBUG(lock, "client-side enqueue, new resource");
376 if ((*flags) & LDLM_FL_AST_SENT) {
377 l_lock(&ns->ns_lock);
378 lock->l_flags |= LDLM_FL_CBPENDING;
379 l_unlock(&ns->ns_lock);
380 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
383 /* If the lock has already been granted by a completion AST, don't
384 * clobber the LVB with an older one. */
385 if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
387 tmplvb = lustre_swab_repbuf(req, 1, lvb_len, lvb_swabber);
389 GOTO(out_lock, rc = -EPROTO);
390 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
394 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
395 if (lock->l_completion_ast != NULL) {
396 int err = lock->l_completion_ast(lock, *flags, NULL);
402 if (lvb_len && lvb != NULL) {
403 /* Copy the LVB here, and not earlier, because the completion
404 * AST (if any) can override what we got in the reply */
405 memcpy(lvb, lock->l_lvb_data, lvb_len);
408 LDLM_DEBUG(lock, "client-side enqueue END");
412 failed_lock_cleanup(ns, lock, lockh, mode);
413 if (!req_passed_in && req != NULL)
414 ptlrpc_req_finished(req);
420 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
424 if (lock->l_resource->lr_namespace->ns_client) {
425 CERROR("Trying to cancel local lock\n");
428 LDLM_DEBUG(lock, "client-side local convert");
430 ldlm_lock_convert(lock, new_mode, flags);
431 ldlm_reprocess_all(lock->l_resource);
433 LDLM_DEBUG(lock, "client-side local convert handler END");
438 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
439 * conversion of locks which are on the waiting or converting queue */
440 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
442 struct ldlm_request *body;
443 struct ldlm_reply *reply;
444 struct ldlm_lock *lock;
445 struct ldlm_resource *res;
446 struct ptlrpc_request *req;
447 int rc, size = sizeof(*body);
450 lock = ldlm_handle2lock(lockh);
457 if (lock->l_conn_export == NULL)
458 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
460 LDLM_DEBUG(lock, "client-side convert");
462 req = ptlrpc_prep_req(class_exp2cliimp(lock->l_conn_export),
463 LDLM_CONVERT, 1, &size, NULL);
465 GOTO(out, rc = -ENOMEM);
467 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
468 memcpy(&body->lock_handle1, &lock->l_remote_handle,
469 sizeof(body->lock_handle1));
471 body->lock_desc.l_req_mode = new_mode;
472 body->lock_flags = *flags;
474 size = sizeof(*reply);
475 req->rq_replen = lustre_msg_size(1, &size);
477 rc = ptlrpc_queue_wait(req);
481 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
482 lustre_swab_ldlm_reply);
484 CERROR ("Can't unpack ldlm_reply\n");
485 GOTO (out, rc = -EPROTO);
488 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
490 ldlm_reprocess_all(res);
491 /* Go to sleep until the lock is granted. */
492 /* FIXME: or cancelled. */
493 if (lock->l_completion_ast)
494 lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC, NULL);
498 ptlrpc_req_finished(req);
502 int ldlm_cli_cancel(struct lustre_handle *lockh)
504 struct ptlrpc_request *req;
505 struct ldlm_lock *lock;
506 struct ldlm_request *body;
507 int rc = 0, size = sizeof(*body);
510 /* concurrent cancels on the same handle can happen */
511 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
515 if (lock->l_conn_export) {
517 struct obd_import *imp;
519 LDLM_DEBUG(lock, "client-side cancel");
520 /* Set this flag to prevent others from getting new references*/
521 l_lock(&lock->l_resource->lr_namespace->ns_lock);
522 lock->l_flags |= LDLM_FL_CBPENDING;
523 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
524 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
525 ldlm_cancel_callback(lock);
528 CDEBUG(D_INFO, "not sending request (at caller's "
534 imp = class_exp2cliimp(lock->l_conn_export);
535 if (imp == NULL || imp->imp_invalid) {
536 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
541 req = ptlrpc_prep_req(imp, LDLM_CANCEL, 1, &size, NULL);
543 GOTO(out, rc = -ENOMEM);
544 req->rq_no_resend = 1;
546 /* XXX FIXME bug 249 */
547 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
548 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
550 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
551 memcpy(&body->lock_handle1, &lock->l_remote_handle,
552 sizeof(body->lock_handle1));
554 req->rq_replen = lustre_msg_size(0, NULL);
556 rc = ptlrpc_queue_wait(req);
559 CERROR("client/server (nid "LPU64") out of sync--not "
561 req->rq_import->imp_connection->c_peer.peer_nid);
562 } else if (rc == -ETIMEDOUT) {
563 ptlrpc_req_finished(req);
565 } else if (rc != ELDLM_OK) {
566 CERROR("Got rc %d from cancel RPC: canceling "
570 ptlrpc_req_finished(req);
572 ldlm_lock_cancel(lock);
574 if (lock->l_resource->lr_namespace->ns_client) {
575 LDLM_ERROR(lock, "Trying to cancel local lock\n");
578 LDLM_DEBUG(lock, "client-side local cancel");
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;
596 l_lock(&ns->ns_lock);
597 count = ns->ns_nr_unused - ns->ns_max_unused;
600 l_unlock(&ns->ns_lock);
604 list_for_each_safe(tmp, next, &ns->ns_unused_list) {
605 struct ldlm_lock *lock;
606 lock = list_entry(tmp, struct ldlm_lock, l_lru);
608 LASSERT(!lock->l_readers && !lock->l_writers);
610 /* Setting the CBPENDING flag is a little misleading, but
611 * prevents an important race; namely, once CBPENDING is set,
612 * the lock can accumulate no more readers/writers. Since
613 * readers and writers are already zero here, ldlm_lock_decref
614 * won't see this flag and call l_blocking_ast */
615 lock->l_flags |= LDLM_FL_CBPENDING;
617 LDLM_LOCK_GET(lock); /* dropped by bl thread */
618 ldlm_lock_remove_from_lru(lock);
619 ldlm_bl_to_thread(ns, NULL, lock);
624 l_unlock(&ns->ns_lock);
628 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
629 struct ldlm_res_id res_id, int flags,
632 struct ldlm_resource *res;
633 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
634 struct ldlm_ast_work *w;
637 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
639 /* This is not a problem. */
640 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
644 l_lock(&ns->ns_lock);
645 list_for_each(tmp, &res->lr_granted) {
646 struct ldlm_lock *lock;
647 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
649 if (opaque != NULL && lock->l_ast_data != opaque) {
650 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
651 lock->l_ast_data, opaque);
656 if (lock->l_readers || lock->l_writers) {
657 if (flags & LDLM_FL_WARN) {
658 LDLM_ERROR(lock, "lock in use");
664 /* See CBPENDING comment in ldlm_cancel_lru */
665 lock->l_flags |= LDLM_FL_CBPENDING;
667 OBD_ALLOC(w, sizeof(*w));
670 w->w_lock = LDLM_LOCK_GET(lock);
672 /* Prevent the cancel callback from being called by setting
673 * LDLM_FL_CANCEL in the lock. Very sneaky. -p */
674 if (flags & LDLM_FL_NO_CALLBACK)
675 w->w_lock->l_flags |= LDLM_FL_CANCEL;
677 list_add(&w->w_list, &list);
679 l_unlock(&ns->ns_lock);
681 list_for_each_safe(tmp, next, &list) {
682 struct lustre_handle lockh;
684 w = list_entry(tmp, struct ldlm_ast_work, w_list);
686 if (flags & LDLM_FL_LOCAL_ONLY) {
687 ldlm_lock_cancel(w->w_lock);
689 ldlm_lock2handle(w->w_lock, &lockh);
690 rc = ldlm_cli_cancel(&lockh);
692 CERROR("ldlm_cli_cancel: %d\n", rc);
694 list_del(&w->w_list);
695 LDLM_LOCK_PUT(w->w_lock);
696 OBD_FREE(w, sizeof(*w));
699 ldlm_resource_putref(res);
704 /* Cancel all locks on a namespace (or a specific resource, if given)
705 * that have 0 readers/writers.
707 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
708 * to notify the server.
709 * If flags & LDLM_FL_NO_CALLBACK, don't run the cancel callback.
710 * If flags & LDLM_FL_WARN, print a warning if some locks are still in use. */
711 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
712 struct ldlm_res_id *res_id, int flags, void *opaque)
721 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
724 l_lock(&ns->ns_lock);
725 for (i = 0; i < RES_HASH_SIZE; i++) {
726 struct list_head *tmp, *pos;
727 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
729 struct ldlm_resource *res;
730 res = list_entry(tmp, struct ldlm_resource, lr_hash);
731 ldlm_resource_getref(res);
733 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
737 CERROR("cancel_unused_res ("LPU64"): %d\n",
738 res->lr_name.name[0], rc);
739 ldlm_resource_putref(res);
742 l_unlock(&ns->ns_lock);
747 /* Lock iterators. */
749 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
752 struct list_head *tmp, *next;
753 struct ldlm_lock *lock;
754 int rc = LDLM_ITER_CONTINUE;
755 struct ldlm_namespace *ns = res->lr_namespace;
760 RETURN(LDLM_ITER_CONTINUE);
762 l_lock(&ns->ns_lock);
763 list_for_each_safe(tmp, next, &res->lr_granted) {
764 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
766 if (iter(lock, closure) == LDLM_ITER_STOP)
767 GOTO(out, rc = LDLM_ITER_STOP);
770 list_for_each_safe(tmp, next, &res->lr_converting) {
771 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
773 if (iter(lock, closure) == LDLM_ITER_STOP)
774 GOTO(out, rc = LDLM_ITER_STOP);
777 list_for_each_safe(tmp, next, &res->lr_waiting) {
778 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
780 if (iter(lock, closure) == LDLM_ITER_STOP)
781 GOTO(out, rc = LDLM_ITER_STOP);
784 l_unlock(&ns->ns_lock);
788 struct iter_helper_data {
789 ldlm_iterator_t iter;
793 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
795 struct iter_helper_data *helper = closure;
796 return helper->iter(lock, helper->closure);
799 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
801 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
804 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
807 struct iter_helper_data helper = { iter: iter, closure: closure };
808 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
811 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
812 ldlm_res_iterator_t iter, void *closure)
814 int i, rc = LDLM_ITER_CONTINUE;
816 l_lock(&ns->ns_lock);
817 for (i = 0; i < RES_HASH_SIZE; i++) {
818 struct list_head *tmp, *next;
819 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
820 struct ldlm_resource *res =
821 list_entry(tmp, struct ldlm_resource, lr_hash);
823 ldlm_resource_getref(res);
824 rc = iter(res, closure);
825 ldlm_resource_putref(res);
826 if (rc == LDLM_ITER_STOP)
831 l_unlock(&ns->ns_lock);
835 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
836 void ldlm_change_cbdata(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
837 ldlm_iterator_t iter, void *data)
839 struct ldlm_resource *res;
843 CERROR("must pass in namespace");
847 res = ldlm_resource_get(ns, NULL, *res_id, 0, 0);
853 l_lock(&ns->ns_lock);
854 ldlm_resource_foreach(res, iter, data);
855 l_unlock(&ns->ns_lock);
856 ldlm_resource_putref(res);
862 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
864 struct list_head *list = closure;
866 /* we use l_pending_chain here, because it's unused on clients. */
867 list_add(&lock->l_pending_chain, list);
868 return LDLM_ITER_CONTINUE;
871 static int replay_lock_interpret(struct ptlrpc_request *req,
874 struct ldlm_lock *lock;
875 struct ldlm_reply *reply;
877 atomic_dec(&req->rq_import->imp_replay_inflight);
881 lock = req->rq_async_args.pointer_arg[0];
882 LASSERT(lock != NULL);
884 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
885 lustre_swab_ldlm_reply);
887 CERROR("Can't unpack ldlm_reply\n");
888 GOTO (out, rc = -EPROTO);
891 memcpy(&lock->l_remote_handle, &reply->lock_handle,
892 sizeof(lock->l_remote_handle));
893 LDLM_DEBUG(lock, "replayed lock:");
894 ptlrpc_import_recovery_state_machine(req->rq_import);
899 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
901 struct ptlrpc_request *req;
902 struct ldlm_request *body;
903 struct ldlm_reply *reply;
909 * If granted mode matches the requested mode, this lock is granted.
911 * If they differ, but we have a granted mode, then we were granted
912 * one mode and now want another: ergo, converting.
914 * If we haven't been granted anything and are on a resource list,
915 * then we're blocked/waiting.
917 * If we haven't been granted anything and we're NOT on a resource list,
918 * then we haven't got a reply yet and don't have a known disposition.
919 * This happens whenever a lock enqueue is the request that triggers
922 if (lock->l_granted_mode == lock->l_req_mode)
923 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
924 else if (lock->l_granted_mode)
925 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
926 else if (!list_empty(&lock->l_res_link))
927 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
929 flags = LDLM_FL_REPLAY;
931 size[0] = sizeof(*body);
932 req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, size, NULL);
936 /* We're part of recovery, so don't wait for it. */
937 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
939 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
940 ldlm_lock2desc(lock, &body->lock_desc);
941 body->lock_flags = flags;
943 ldlm_lock2handle(lock, &body->lock_handle1);
944 size[0] = sizeof(*reply);
945 if (lock->l_lvb_len != 0) {
947 size[1] = lock->l_lvb_len;
949 req->rq_replen = lustre_msg_size(buffers, size);
951 LDLM_DEBUG(lock, "replaying lock:");
953 atomic_inc(&req->rq_import->imp_replay_inflight);
954 req->rq_async_args.pointer_arg[0] = lock;
955 req->rq_interpret_reply = replay_lock_interpret;
956 ptlrpcd_add_req(req);
961 int ldlm_replay_locks(struct obd_import *imp)
963 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
964 struct list_head list, *pos, *next;
965 struct ldlm_lock *lock;
969 INIT_LIST_HEAD(&list);
971 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
973 /* ensure this doesn't fall to 0 before all have been queued */
974 atomic_inc(&imp->imp_replay_inflight);
976 l_lock(&ns->ns_lock);
977 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
979 list_for_each_safe(pos, next, &list) {
980 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
981 rc = replay_one_lock(imp, lock);
983 break; /* or try to do the rest? */
985 l_unlock(&ns->ns_lock);
987 atomic_dec(&imp->imp_replay_inflight);