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, last_dump = 0;
53 if (ptlrpc_check_suspend())
56 LDLM_ERROR(lock, "lock timed out; not entering recovery in "
57 "server code, just going back to sleep");
58 if (time_after(jiffies, next_dump)) {
59 last_dump = next_dump;
60 next_dump = jiffies + 300 * HZ;
61 ldlm_namespace_dump(D_DLMTRACE,
62 lock->l_resource->lr_namespace);
64 portals_debug_dumplog();
69 obd = lock->l_conn_export->exp_obd;
70 imp = obd->u.cli.cl_import;
71 ptlrpc_fail_import(imp, lwd->lwd_generation);
72 LDLM_ERROR(lock, "lock timed out, entering recovery for %s@%s",
73 imp->imp_target_uuid.uuid,
74 imp->imp_connection->c_remote_uuid.uuid);
79 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
81 /* XXX ALLOCATE - 160 bytes */
82 struct lock_wait_data lwd;
83 unsigned long irqflags;
84 struct obd_device *obd;
85 struct obd_import *imp = NULL;
86 struct l_wait_info lwi;
90 if (flags == LDLM_FL_WAIT_NOREPROC)
93 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
94 LDLM_FL_BLOCK_CONV))) {
95 wake_up(&lock->l_waitq);
99 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
101 ldlm_lock_dump(D_OTHER, lock, 0);
102 ldlm_reprocess_all(lock->l_resource);
106 obd = class_exp2obd(lock->l_conn_export);
108 /* if this is a local lock, then there is no import */
110 imp = obd->u.cli.cl_import;
114 if (flags & LDLM_FL_NO_TIMEOUT) {
115 LDLM_DEBUG(lock, "waiting indefinitely for group lock\n");
116 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
118 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ,
119 ldlm_expired_completion_wait,
120 interrupted_completion_wait, &lwd);
124 spin_lock_irqsave(&imp->imp_lock, irqflags);
125 lwd.lwd_generation = imp->imp_generation;
126 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
129 /* Go to sleep until the lock is granted or cancelled. */
130 rc = l_wait_event(lock->l_waitq,
131 ((lock->l_req_mode == lock->l_granted_mode) ||
132 (lock->l_flags & LDLM_FL_FAILED)), &lwi);
134 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
135 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
140 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
145 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
149 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
150 struct ldlm_res_id res_id,
152 ldlm_policy_data_t *policy,
155 ldlm_blocking_callback blocking,
156 ldlm_completion_callback completion,
157 ldlm_glimpse_callback glimpse,
158 void *data, __u32 lvb_len,
160 struct lustre_handle *lockh)
162 struct ldlm_lock *lock;
167 CERROR("Trying to enqueue local lock in a shadow namespace\n");
171 lock = ldlm_lock_create(ns, NULL, res_id, type, mode, blocking,
172 completion, glimpse, data, lvb_len);
174 GOTO(out_nolock, err = -ENOMEM);
175 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
177 ldlm_lock_addref_internal(lock, mode);
178 ldlm_lock2handle(lock, lockh);
179 lock->l_flags |= LDLM_FL_LOCAL;
180 if (*flags & LDLM_FL_ATOMIC_CB)
181 lock->l_flags |= LDLM_FL_ATOMIC_CB;
182 lock->l_lvb_swabber = lvb_swabber;
184 memcpy(&lock->l_policy_data, policy, sizeof(*policy));
185 if (type == LDLM_EXTENT)
186 memcpy(&lock->l_req_extent, &policy->l_extent,
187 sizeof(policy->l_extent));
189 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
194 memcpy(policy, &lock->l_policy_data, sizeof(*policy));
195 if ((*flags) & LDLM_FL_LOCK_CHANGED)
196 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
198 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
201 if (lock->l_completion_ast)
202 lock->l_completion_ast(lock, *flags, NULL);
204 LDLM_DEBUG(lock, "client-side local enqueue END");
212 static void failed_lock_cleanup(struct ldlm_namespace *ns,
213 struct ldlm_lock *lock,
214 struct lustre_handle *lockh, int mode)
216 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
217 lock_res_and_lock(lock);
218 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
219 unlock_res_and_lock(lock);
220 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
222 ldlm_lock_decref_and_cancel(lockh, mode);
224 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
225 * from llite/file.c/ll_file_flock(). */
226 if (lock->l_resource->lr_type == LDLM_FLOCK) {
227 ldlm_lock_destroy(lock);
231 int ldlm_cli_enqueue(struct obd_export *exp,
232 struct ptlrpc_request *req,
233 struct ldlm_namespace *ns,
234 struct ldlm_res_id res_id,
236 ldlm_policy_data_t *policy,
239 ldlm_blocking_callback blocking,
240 ldlm_completion_callback completion,
241 ldlm_glimpse_callback glimpse,
246 struct lustre_handle *lockh)
248 struct ldlm_lock *lock;
249 struct ldlm_request *body;
250 struct ldlm_reply *reply;
251 int rc, size[3] = {0, sizeof(*body), lvb_len}, req_passed_in = 1;
252 int is_replay = *flags & LDLM_FL_REPLAY;
253 int cleanup_phase = 0;
258 rc = ldlm_cli_enqueue_local(ns, res_id, type, policy, mode,
259 flags, blocking, completion,
260 glimpse, data, lvb_len, lvb_swabber,
265 /* If we're replaying this lock, just check some invariants.
266 * If we're creating a new lock, get everything all setup nice. */
268 lock = ldlm_handle2lock(lockh);
269 LDLM_DEBUG(lock, "client-side enqueue START");
270 LASSERT(exp == lock->l_conn_export);
272 lock = ldlm_lock_create(ns, NULL, res_id, type, mode, blocking,
273 completion, glimpse, data, lvb_len);
276 /* for the local lock, add the reference */
277 ldlm_lock_addref_internal(lock, mode);
278 ldlm_lock2handle(lock, lockh);
279 lock->l_lvb_swabber = lvb_swabber;
281 memcpy(&lock->l_policy_data, policy, sizeof(*policy));
282 if (type == LDLM_EXTENT)
283 memcpy(&lock->l_req_extent, &policy->l_extent,
284 sizeof(policy->l_extent));
285 LDLM_DEBUG(lock, "client-side enqueue START");
288 /* lock not sent to server yet */
292 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
293 LDLM_ENQUEUE, 2, size, NULL);
295 GOTO(cleanup, rc = -ENOMEM);
299 LASSERTF(req->rq_reqmsg->buflens[MDS_REQ_INTENT_LOCKREQ_OFF] ==
300 sizeof(*body), "buflen[%d] = %d, not %d\n",
301 MDS_REQ_INTENT_LOCKREQ_OFF,
302 req->rq_reqmsg->buflens[MDS_REQ_INTENT_LOCKREQ_OFF],
305 /* Dump lock data into the request buffer */
306 body = lustre_msg_buf(req->rq_reqmsg, MDS_REQ_INTENT_LOCKREQ_OFF,
308 ldlm_lock2desc(lock, &body->lock_desc);
309 body->lock_flags = *flags;
311 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
313 /* Continue as normal. */
314 if (!req_passed_in) {
315 size[0] = sizeof(*reply);
316 req->rq_replen = lustre_msg_size(1 + (lvb_len > 0), size);
318 lock->l_conn_export = exp;
319 lock->l_export = NULL;
320 lock->l_blocking_ast = blocking;
322 LDLM_DEBUG(lock, "sending request");
323 rc = ptlrpc_queue_wait(req);
325 if (rc != ELDLM_OK) {
327 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
328 rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
329 if (rc == ELDLM_LOCK_ABORTED) {
330 /* Before we return, swab the reply */
331 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
332 lustre_swab_ldlm_reply);
334 CERROR("Can't unpack ldlm_reply\n");
339 tmplvb = lustre_swab_repbuf(req, 1, lvb_len,
342 GOTO(cleanup, rc = -EPROTO);
344 memcpy(lvb, tmplvb, lvb_len);
350 reply = lustre_swab_repbuf(req, 0, sizeof(*reply),
351 lustre_swab_ldlm_reply);
353 CERROR("Can't unpack ldlm_reply\n");
354 GOTO(cleanup, rc = -EPROTO);
357 /* lock enqueued on the server */
360 do_gettimeofday(&lock->l_enqueued_time);
361 memcpy(&lock->l_remote_handle, &reply->lock_handle,
362 sizeof(lock->l_remote_handle));
363 *flags = reply->lock_flags;
365 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
366 lock, reply->lock_handle.cookie, *flags);
368 /* If enqueue returned a blocked lock but the completion handler has
369 * already run, then it fixed up the resource and we don't need to do it
371 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
372 int newmode = reply->lock_desc.l_req_mode;
374 if (newmode && newmode != lock->l_req_mode) {
375 LDLM_DEBUG(lock, "server returned different mode %s",
376 ldlm_lockname[newmode]);
377 lock->l_req_mode = newmode;
380 if (reply->lock_desc.l_resource.lr_name.name[0] !=
381 lock->l_resource->lr_name.name[0] ||
382 reply->lock_desc.l_resource.lr_name.name[1] !=
383 lock->l_resource->lr_name.name[1]) {
384 CDEBUG(D_INFO, "remote intent success, locking %ld "
386 (long)reply->lock_desc.l_resource.lr_name.name[0],
387 (long)lock->l_resource->lr_name.name[0]);
389 ldlm_lock_change_resource(ns, lock,
390 reply->lock_desc.l_resource.lr_name);
391 if (lock->l_resource == NULL) {
393 GOTO(cleanup, rc = -ENOMEM);
395 LDLM_DEBUG(lock, "client-side enqueue, new resource");
398 memcpy(&lock->l_policy_data,
399 &reply->lock_desc.l_policy_data,
400 sizeof(reply->lock_desc.l_policy_data));
401 if (type != LDLM_PLAIN)
402 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
405 if ((*flags) & LDLM_FL_AST_SENT) {
406 lock_res_and_lock(lock);
407 lock->l_flags |= LDLM_FL_CBPENDING;
408 unlock_res_and_lock(lock);
409 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
412 /* If the lock has already been granted by a completion AST, don't
413 * clobber the LVB with an older one. */
414 if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
416 tmplvb = lustre_swab_repbuf(req, 1, lvb_len, lvb_swabber);
417 if (tmplvb == NULL) {
419 GOTO(cleanup, rc = -EPROTO);
421 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
425 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
426 if (lock->l_completion_ast != NULL) {
427 int err = lock->l_completion_ast(lock, *flags, NULL);
435 if (lvb_len && lvb != NULL) {
436 /* Copy the LVB here, and not earlier, because the completion
437 * AST (if any) can override what we got in the reply */
438 memcpy(lvb, lock->l_lvb_data, lvb_len);
441 LDLM_DEBUG(lock, "client-side enqueue END");
444 switch (cleanup_phase) {
447 failed_lock_cleanup(ns, lock, lockh, mode);
449 if (!req_passed_in && req != NULL)
450 ptlrpc_req_finished(req);
457 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
460 struct ldlm_resource *res;
463 if (lock->l_resource->lr_namespace->ns_client) {
464 CERROR("Trying to cancel local lock\n");
467 LDLM_DEBUG(lock, "client-side local convert");
469 res = ldlm_lock_convert(lock, new_mode, flags);
471 ldlm_reprocess_all(res);
476 LDLM_DEBUG(lock, "client-side local convert handler END");
481 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
482 * conversion of locks which are on the waiting or converting queue */
483 /* Caller of this code is supposed to take care of lock readers/writers
485 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
487 struct ldlm_request *body;
488 struct ldlm_reply *reply;
489 struct ldlm_lock *lock;
490 struct ldlm_resource *res;
491 struct ptlrpc_request *req = NULL;
492 int rc, size = sizeof(*body);
495 lock = ldlm_handle2lock(lockh);
502 if (lock->l_conn_export == NULL)
503 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
505 LDLM_DEBUG(lock, "client-side convert");
507 req = ptlrpc_prep_req(class_exp2cliimp(lock->l_conn_export),
508 LUSTRE_DLM_VERSION, LDLM_CONVERT, 1, &size, NULL);
510 GOTO(out, rc = -ENOMEM);
512 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
513 memcpy(&body->lock_handle1, &lock->l_remote_handle,
514 sizeof(body->lock_handle1));
516 body->lock_desc.l_req_mode = new_mode;
517 body->lock_flags = *flags;
519 size = sizeof(*reply);
520 req->rq_replen = lustre_msg_size(1, &size);
522 rc = ptlrpc_queue_wait(req);
526 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
527 lustre_swab_ldlm_reply);
529 CERROR ("Can't unpack ldlm_reply\n");
530 GOTO (out, rc = -EPROTO);
534 GOTO(out, rc = req->rq_status);
536 res = ldlm_lock_convert(lock, new_mode,
537 (int *)&reply->lock_flags);
539 ldlm_reprocess_all(res);
540 /* Go to sleep until the lock is granted. */
541 /* FIXME: or cancelled. */
542 if (lock->l_completion_ast) {
543 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
554 ptlrpc_req_finished(req);
558 int ldlm_cli_cancel(struct lustre_handle *lockh)
560 struct ptlrpc_request *req;
561 struct ldlm_lock *lock;
562 struct ldlm_request *body;
563 int rc = 0, size = sizeof(*body);
566 /* concurrent cancels on the same handle can happen */
567 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
571 if (lock->l_conn_export) {
573 struct obd_import *imp;
575 LDLM_DEBUG(lock, "client-side cancel");
576 /* Set this flag to prevent others from getting new references*/
577 lock_res_and_lock(lock);
578 lock->l_flags |= LDLM_FL_CBPENDING;
579 local_only = lock->l_flags & LDLM_FL_LOCAL_ONLY;
580 ldlm_cancel_callback(lock);
581 unlock_res_and_lock(lock);
584 CDEBUG(D_INFO, "not sending request (at caller's "
590 imp = class_exp2cliimp(lock->l_conn_export);
591 if (imp == NULL || imp->imp_invalid) {
592 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
597 req = ptlrpc_prep_req(imp, LUSTRE_DLM_VERSION, LDLM_CANCEL,
600 GOTO(out, rc = -ENOMEM);
601 req->rq_no_resend = 1;
603 /* XXX FIXME bug 249 */
604 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
605 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
607 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
608 memcpy(&body->lock_handle1, &lock->l_remote_handle,
609 sizeof(body->lock_handle1));
611 req->rq_replen = lustre_msg_size(0, NULL);
613 rc = ptlrpc_queue_wait(req);
616 char str[PTL_NALFMT_SIZE];
617 CERROR("client/server (nid %s) out of sync"
619 ptlrpc_peernid2str(&req->rq_import->
620 imp_connection->c_peer, str));
621 } else if (rc == -ETIMEDOUT) {
622 ptlrpc_req_finished(req);
624 } else if (rc != ELDLM_OK) {
625 CERROR("Got rc %d from cancel RPC: canceling "
629 ptlrpc_req_finished(req);
631 ldlm_lock_cancel(lock);
633 if (lock->l_resource->lr_namespace->ns_client) {
634 LDLM_ERROR(lock, "Trying to cancel local lock\n");
637 LDLM_DEBUG(lock, "client-side local cancel");
638 ldlm_lock_cancel(lock);
639 ldlm_reprocess_all(lock->l_resource);
640 LDLM_DEBUG(lock, "client-side local cancel handler END");
649 /* when called with LDLM_ASYNC the blocking callback will be handled
650 * in a thread and this function will return after the thread has been
651 * asked to call the callback. when called with LDLM_SYNC the blocking
652 * callback will be performed in this function. */
653 int ldlm_cancel_lru(struct ldlm_namespace *ns, ldlm_sync_t sync)
655 struct ldlm_lock *lock, *next;
661 sync = LDLM_SYNC; /* force to be sync in user space */
664 spin_lock(&ns->ns_unused_lock);
665 count = ns->ns_nr_unused - ns->ns_max_unused;
668 spin_unlock(&ns->ns_unused_lock);
672 while (!list_empty(&ns->ns_unused_list)) {
673 struct list_head *tmp = ns->ns_unused_list.next;
674 lock = list_entry(tmp, struct ldlm_lock, l_lru);
675 LASSERT(!lock->l_readers && !lock->l_writers);
677 LDLM_LOCK_GET(lock); /* dropped by bl thread */
678 spin_unlock(&ns->ns_unused_lock);
680 lock_res_and_lock(lock);
681 ldlm_lock_remove_from_lru(lock);
683 /* Setting the CBPENDING flag is a little misleading, but
684 * prevents an important race; namely, once CBPENDING is set,
685 * the lock can accumulate no more readers/writers. Since
686 * readers and writers are already zero here, ldlm_lock_decref
687 * won't see this flag and call l_blocking_ast */
688 lock->l_flags |= LDLM_FL_CBPENDING;
690 /* We can't re-add to l_lru as it confuses the refcounting in
691 * ldlm_lock_remove_from_lru() if an AST arrives after we drop
692 * ns_lock below. We use l_tmp and can't use l_pending_chain as
693 * it is used both on server and client nevertheles bug 5666
694 * says it is used only on server. --umka */
695 if (sync != LDLM_ASYNC || ldlm_bl_to_thread(ns, NULL, lock))
696 list_add(&lock->l_tmp, &cblist);
698 unlock_res_and_lock(lock);
700 spin_lock(&ns->ns_unused_lock);
705 spin_unlock(&ns->ns_unused_lock);
707 list_for_each_entry_safe(lock, next, &cblist, l_tmp) {
708 list_del_init(&lock->l_tmp);
709 ldlm_handle_bl_callback(ns, NULL, lock);
715 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
716 struct ldlm_res_id res_id, int flags,
719 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
720 struct ldlm_resource *res;
721 struct ldlm_lock *lock;
724 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
726 /* This is not a problem. */
727 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
732 list_for_each(tmp, &res->lr_granted) {
733 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
735 if (opaque != NULL && lock->l_ast_data != opaque) {
736 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
737 lock->l_ast_data, opaque);
741 if (lock->l_readers || lock->l_writers) {
742 if (flags & LDLM_FL_CONFIG_CHANGE)
743 lock->l_flags |= LDLM_FL_CBPENDING;
744 else if (flags & LDLM_FL_WARN)
745 LDLM_ERROR(lock, "lock in use");
749 /* See CBPENDING comment in ldlm_cancel_lru */
750 lock->l_flags |= LDLM_FL_CBPENDING;
752 LASSERT(list_empty(&lock->l_bl_ast));
753 list_add(&lock->l_bl_ast, &list);
758 list_for_each_safe(tmp, next, &list) {
759 struct lustre_handle lockh;
761 lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
763 if (flags & LDLM_FL_LOCAL_ONLY) {
764 ldlm_lock_cancel(lock);
766 ldlm_lock2handle(lock, &lockh);
767 rc = ldlm_cli_cancel(&lockh);
769 CERROR("ldlm_cli_cancel: %d\n", rc);
771 list_del_init(&lock->l_bl_ast);
775 ldlm_resource_putref(res);
780 static inline int have_no_nsresource(struct ldlm_namespace *ns)
784 spin_lock(&ns->ns_hash_lock);
785 if (ns->ns_resources == 0)
787 spin_unlock(&ns->ns_hash_lock);
792 /* Cancel all locks on a namespace (or a specific resource, if given)
793 * that have 0 readers/writers.
795 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
796 * to notify the server.
797 * If flags & LDLM_FL_NO_CALLBACK, don't run the cancel callback.
798 * If flags & LDLM_FL_WARN, print a warning if some locks are still in use.
799 * If flags & LDLM_FL_CONFIG_CHANGE, mark all locks as having a pending callback
801 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
802 struct ldlm_res_id *res_id, int flags, void *opaque)
805 struct l_wait_info lwi = { 0 };
812 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
815 spin_lock(&ns->ns_hash_lock);
816 for (i = 0; i < RES_HASH_SIZE; i++) {
817 struct list_head *tmp;
818 tmp = ns->ns_hash[i].next;
819 while (tmp != &(ns->ns_hash[i])) {
820 struct ldlm_resource *res;
823 res = list_entry(tmp, struct ldlm_resource, lr_hash);
824 ldlm_resource_getref(res);
825 spin_unlock(&ns->ns_hash_lock);
827 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
830 CERROR("cancel_unused_res ("LPU64"): %d\n",
831 res->lr_name.name[0], rc);
833 spin_lock(&ns->ns_hash_lock);
835 ldlm_resource_putref_locked(res);
838 spin_unlock(&ns->ns_hash_lock);
840 if (flags & LDLM_FL_CONFIG_CHANGE)
841 l_wait_event(ns->ns_waitq, have_no_nsresource(ns), &lwi);
846 /* Lock iterators. */
848 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
851 struct list_head *tmp, *next;
852 struct ldlm_lock *lock;
853 int rc = LDLM_ITER_CONTINUE;
858 RETURN(LDLM_ITER_CONTINUE);
861 list_for_each_safe(tmp, next, &res->lr_granted) {
862 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
864 if (iter(lock, closure) == LDLM_ITER_STOP)
865 GOTO(out, rc = LDLM_ITER_STOP);
868 list_for_each_safe(tmp, next, &res->lr_converting) {
869 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
871 if (iter(lock, closure) == LDLM_ITER_STOP)
872 GOTO(out, rc = LDLM_ITER_STOP);
875 list_for_each_safe(tmp, next, &res->lr_waiting) {
876 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
878 if (iter(lock, closure) == LDLM_ITER_STOP)
879 GOTO(out, rc = LDLM_ITER_STOP);
886 struct iter_helper_data {
887 ldlm_iterator_t iter;
891 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
893 struct iter_helper_data *helper = closure;
894 return helper->iter(lock, helper->closure);
897 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
899 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
902 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
905 struct iter_helper_data helper = { iter: iter, closure: closure };
906 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
909 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
910 ldlm_res_iterator_t iter, void *closure)
912 int i, rc = LDLM_ITER_CONTINUE;
913 struct ldlm_resource *res;
914 struct list_head *tmp;
916 spin_lock(&ns->ns_hash_lock);
917 for (i = 0; i < RES_HASH_SIZE; i++) {
918 tmp = ns->ns_hash[i].next;
919 while (tmp != &(ns->ns_hash[i])) {
920 res = list_entry(tmp, struct ldlm_resource, lr_hash);
921 ldlm_resource_getref(res);
922 spin_unlock(&ns->ns_hash_lock);
924 rc = iter(res, closure);
926 spin_lock(&ns->ns_hash_lock);
928 ldlm_resource_putref_locked(res);
929 if (rc == LDLM_ITER_STOP)
934 spin_unlock(&ns->ns_hash_lock);
938 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
939 void ldlm_change_cbdata(struct ldlm_namespace *ns,
940 struct ldlm_res_id *res_id,
941 ldlm_iterator_t iter,
944 struct ldlm_resource *res;
948 CERROR("must pass in namespace\n");
952 res = ldlm_resource_get(ns, NULL, *res_id, 0, 0);
958 ldlm_resource_foreach(res, iter, data);
959 ldlm_resource_putref(res);
965 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
967 struct list_head *list = closure;
969 /* we use l_pending_chain here, because it's unused on clients. */
970 list_add(&lock->l_pending_chain, list);
971 return LDLM_ITER_CONTINUE;
974 static int replay_lock_interpret(struct ptlrpc_request *req,
977 struct ldlm_lock *lock;
978 struct ldlm_reply *reply;
980 atomic_dec(&req->rq_import->imp_replay_inflight);
984 lock = req->rq_async_args.pointer_arg[0];
985 LASSERT(lock != NULL);
987 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
988 lustre_swab_ldlm_reply);
990 CERROR("Can't unpack ldlm_reply\n");
991 GOTO (out, rc = -EPROTO);
994 memcpy(&lock->l_remote_handle, &reply->lock_handle,
995 sizeof(lock->l_remote_handle));
996 LDLM_DEBUG(lock, "replayed lock:");
997 ptlrpc_import_recovery_state_machine(req->rq_import);
1000 ptlrpc_connect_import(req->rq_import, NULL);
1005 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
1007 struct ptlrpc_request *req;
1008 struct ldlm_request *body;
1009 struct ldlm_reply *reply;
1011 int size[2] = {0, sizeof(*body)};
1015 * If granted mode matches the requested mode, this lock is granted.
1017 * If they differ, but we have a granted mode, then we were granted
1018 * one mode and now want another: ergo, converting.
1020 * If we haven't been granted anything and are on a resource list,
1021 * then we're blocked/waiting.
1023 * If we haven't been granted anything and we're NOT on a resource list,
1024 * then we haven't got a reply yet and don't have a known disposition.
1025 * This happens whenever a lock enqueue is the request that triggers
1028 if (lock->l_granted_mode == lock->l_req_mode)
1029 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
1030 else if (lock->l_granted_mode)
1031 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
1032 else if (!list_empty(&lock->l_res_link))
1033 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
1035 flags = LDLM_FL_REPLAY;
1037 req = ptlrpc_prep_req(imp, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
1042 /* We're part of recovery, so don't wait for it. */
1043 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
1045 body = lustre_msg_buf(req->rq_reqmsg, 1, sizeof (*body));
1046 ldlm_lock2desc(lock, &body->lock_desc);
1047 body->lock_flags = flags;
1049 ldlm_lock2handle(lock, &body->lock_handle1);
1050 size[0] = sizeof(*reply);
1051 if (lock->l_lvb_len != 0) {
1053 size[1] = lock->l_lvb_len;
1055 req->rq_replen = lustre_msg_size(buffers, size);
1056 /* notify the server we've replayed all requests.
1057 * also, we mark the request to be put on a dedicated
1058 * queue to be processed after all request replayes.
1060 lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
1062 LDLM_DEBUG(lock, "replaying lock:");
1064 imp->imp_locks_replayed++;
1065 atomic_inc(&req->rq_import->imp_replay_inflight);
1066 req->rq_async_args.pointer_arg[0] = lock;
1067 req->rq_interpret_reply = replay_lock_interpret;
1068 ptlrpcd_add_req(req);
1073 int ldlm_replay_locks(struct obd_import *imp)
1075 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
1076 struct list_head list, *pos, *next;
1077 struct ldlm_lock *lock;
1081 INIT_LIST_HEAD(&list);
1083 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1084 LASSERT(ns != NULL);
1086 /* ensure this doesn't fall to 0 before all have been queued */
1087 atomic_inc(&imp->imp_replay_inflight);
1089 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
1091 list_for_each_safe(pos, next, &list) {
1092 lock = list_entry(pos, struct ldlm_lock, l_pending_chain);
1093 rc = replay_one_lock(imp, lock);
1095 break; /* or try to do the rest? */
1098 atomic_dec(&imp->imp_replay_inflight);