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 the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
25 #define DEBUG_SUBSYSTEM S_LDLM
28 #include <liblustre.h>
31 #include <linux/lustre_dlm.h>
32 #include <linux/obd_class.h>
33 #include <linux/obd.h>
35 #include "ldlm_internal.h"
37 static void interrupted_completion_wait(void *data)
41 struct lock_wait_data {
42 struct ldlm_lock *lwd_lock;
46 int ldlm_expired_completion_wait(void *data)
48 struct lock_wait_data *lwd = data;
49 struct ldlm_lock *lock = lwd->lwd_lock;
50 struct obd_import *imp;
51 struct obd_device *obd;
53 if (lock->l_conn_export == NULL) {
54 static unsigned long next_dump = 0, last_dump = 0;
56 LDLM_ERROR(lock, "lock timed out (enq %lus ago); not entering "
57 "recovery in server code, just going back to sleep",
58 lock->l_enqueued_time.tv_sec);
59 if (time_after(jiffies, next_dump)) {
60 last_dump = next_dump;
61 next_dump = jiffies + 300 * HZ;
62 ldlm_namespace_dump(D_DLMTRACE,
63 lock->l_resource->lr_namespace);
65 libcfs_debug_dumplog();
70 obd = lock->l_conn_export->exp_obd;
71 imp = obd->u.cli.cl_import;
72 ptlrpc_fail_import(imp, lwd->lwd_generation);
73 LDLM_ERROR(lock, "lock timed out (enqueued %lus ago), entering "
74 "recovery for %s@%s", lock->l_enqueued_time.tv_sec,
75 imp->imp_target_uuid.uuid,
76 imp->imp_connection->c_remote_uuid.uuid);
81 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
83 /* XXX ALLOCATE - 160 bytes */
84 struct lock_wait_data lwd;
85 unsigned long irqflags;
86 struct obd_device *obd;
87 struct obd_import *imp = NULL;
88 struct l_wait_info lwi;
92 if (flags == LDLM_FL_WAIT_NOREPROC) {
93 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
97 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
98 LDLM_FL_BLOCK_CONV))) {
99 wake_up(&lock->l_waitq);
103 LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
105 ldlm_lock_dump(D_OTHER, lock, 0);
106 ldlm_reprocess_all(lock->l_resource);
110 obd = class_exp2obd(lock->l_conn_export);
112 /* if this is a local lock, then there is no import */
114 imp = obd->u.cli.cl_import;
118 if (unlikely(flags & LDLM_FL_NO_TIMEOUT)) {
119 LDLM_DEBUG(lock, "waiting indefinitely because CW lock was"
121 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
123 lwi = LWI_TIMEOUT_INTR(obd_timeout * HZ,
124 ldlm_expired_completion_wait,
125 interrupted_completion_wait, &lwd);
129 spin_lock_irqsave(&imp->imp_lock, irqflags);
130 lwd.lwd_generation = imp->imp_generation;
131 spin_unlock_irqrestore(&imp->imp_lock, irqflags);
134 /* Go to sleep until the lock is granted or cancelled. */
135 rc = l_wait_event(lock->l_waitq,
136 ((lock->l_req_mode == lock->l_granted_mode) ||
137 (lock->l_flags & LDLM_FL_FAILED)), &lwi);
139 if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
140 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
145 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
150 LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
154 static int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
155 struct ldlm_res_id res_id,
157 ldlm_policy_data_t *policy,
160 ldlm_blocking_callback blocking,
161 ldlm_completion_callback completion,
162 ldlm_glimpse_callback glimpse,
163 void *data, __u32 lvb_len,
165 struct lustre_handle *lockh)
167 struct ldlm_lock *lock;
172 CERROR("Trying to enqueue local lock in a shadow namespace\n");
176 lock = ldlm_lock_create(ns, NULL, res_id, type, mode, blocking,
177 completion, glimpse, data, lvb_len);
179 GOTO(out_nolock, err = -ENOMEM);
180 LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
182 ldlm_lock_addref_internal(lock, mode);
183 ldlm_lock2handle(lock, lockh);
184 lock->l_flags |= LDLM_FL_LOCAL;
185 lock->l_lvb_swabber = lvb_swabber;
187 memcpy(&lock->l_policy_data, policy, sizeof(*policy));
188 if (type == LDLM_EXTENT)
189 memcpy(&lock->l_req_extent, &policy->l_extent,
190 sizeof(policy->l_extent));
192 err = ldlm_lock_enqueue(ns, &lock, policy, flags);
197 memcpy(policy, &lock->l_policy_data, sizeof(*policy));
198 if ((*flags) & LDLM_FL_LOCK_CHANGED)
199 memcpy(&res_id, &lock->l_resource->lr_name, sizeof(res_id));
201 LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
204 if (lock->l_completion_ast)
205 lock->l_completion_ast(lock, *flags, NULL);
207 LDLM_DEBUG(lock, "client-side local enqueue END");
215 static void failed_lock_cleanup(struct ldlm_namespace *ns,
216 struct ldlm_lock *lock,
217 struct lustre_handle *lockh, int mode)
219 /* Set a flag to prevent us from sending a CANCEL (bug 407) */
220 l_lock(&ns->ns_lock);
221 lock->l_flags |= LDLM_FL_LOCAL_ONLY;
222 LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
223 l_unlock(&ns->ns_lock);
225 ldlm_lock_decref_and_cancel(lockh, mode);
227 /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
228 * from llite/file.c/ll_file_flock(). */
229 if (lock->l_resource->lr_type == LDLM_FLOCK) {
230 ldlm_lock_destroy(lock);
234 int ldlm_cli_enqueue(struct obd_export *exp,
235 struct ptlrpc_request *req,
236 struct ldlm_namespace *ns,
237 struct ldlm_res_id res_id,
239 ldlm_policy_data_t *policy,
242 ldlm_blocking_callback blocking,
243 ldlm_completion_callback completion,
244 ldlm_glimpse_callback glimpse,
249 struct lustre_handle *lockh)
251 struct ldlm_lock *lock;
252 struct ldlm_request *body;
253 struct ldlm_reply *reply;
254 int rc, size[2] = {sizeof(*body), lvb_len}, req_passed_in = 1;
255 int is_replay = *flags & LDLM_FL_REPLAY;
256 int cleanup_phase = 0;
261 rc = ldlm_cli_enqueue_local(ns, res_id, type, policy, mode,
262 flags, blocking, completion,
263 glimpse, data, lvb_len, lvb_swabber,
268 /* If we're replaying this lock, just check some invariants.
269 * If we're creating a new lock, get everything all setup nice. */
271 lock = ldlm_handle2lock(lockh);
272 LDLM_DEBUG(lock, "client-side enqueue START");
273 LASSERT(exp == lock->l_conn_export);
275 lock = ldlm_lock_create(ns, NULL, res_id, type, mode, blocking,
276 completion, glimpse, data, lvb_len);
279 /* for the local lock, add the reference */
280 ldlm_lock_addref_internal(lock, mode);
281 ldlm_lock2handle(lock, lockh);
282 lock->l_lvb_swabber = lvb_swabber;
284 memcpy(&lock->l_policy_data, policy, sizeof(*policy));
285 if (type == LDLM_EXTENT)
286 memcpy(&lock->l_req_extent, &policy->l_extent,
287 sizeof(policy->l_extent));
288 LDLM_DEBUG(lock, "client-side enqueue START");
291 /* lock not sent to server yet */
295 req = ptlrpc_prep_req(class_exp2cliimp(exp), LDLM_ENQUEUE, 1,
298 GOTO(cleanup, rc = -ENOMEM);
300 } else if (req->rq_reqmsg->buflens[0] != sizeof(*body))
303 /* Dump lock data into the request buffer */
304 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
305 ldlm_lock2desc(lock, &body->lock_desc);
306 body->lock_flags = *flags;
308 memcpy(&body->lock_handle1, lockh, sizeof(*lockh));
310 /* Continue as normal. */
311 if (!req_passed_in) {
315 size[0] = sizeof(*reply);
316 req->rq_replen = lustre_msg_size(buffers, 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 memcpy(&lock->l_remote_handle, &reply->lock_handle,
361 sizeof(lock->l_remote_handle));
362 *flags = reply->lock_flags;
364 CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
365 lock, reply->lock_handle.cookie, *flags);
367 /* If enqueue returned a blocked lock but the completion handler has
368 * already run, then it fixed up the resource and we don't need to do it
370 if ((*flags) & LDLM_FL_LOCK_CHANGED) {
371 int newmode = reply->lock_desc.l_req_mode;
373 if (newmode && newmode != lock->l_req_mode) {
374 LDLM_DEBUG(lock, "server returned different mode %s",
375 ldlm_lockname[newmode]);
376 lock->l_req_mode = newmode;
379 if (reply->lock_desc.l_resource.lr_name.name[0] !=
380 lock->l_resource->lr_name.name[0]) {
381 CDEBUG(D_INFO, "remote intent success, locking %ld "
383 (long)reply->lock_desc.l_resource.lr_name.name[0],
384 (long)lock->l_resource->lr_name.name[0]);
386 ldlm_lock_change_resource(ns, lock,
387 reply->lock_desc.l_resource.lr_name);
388 if (lock->l_resource == NULL) {
390 GOTO(cleanup, rc = -ENOMEM);
392 LDLM_DEBUG(lock, "client-side enqueue, new resource");
395 memcpy(&lock->l_policy_data,
396 &reply->lock_desc.l_policy_data,
397 sizeof(reply->lock_desc.l_policy_data));
398 if (type != LDLM_PLAIN)
399 LDLM_DEBUG(lock,"client-side enqueue, new policy data");
402 if ((*flags) & LDLM_FL_AST_SENT) {
403 l_lock(&ns->ns_lock);
404 lock->l_flags |= LDLM_FL_CBPENDING;
405 l_unlock(&ns->ns_lock);
406 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
409 /* If the lock has already been granted by a completion AST, don't
410 * clobber the LVB with an older one. */
411 if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
413 tmplvb = lustre_swab_repbuf(req, 1, lvb_len, lvb_swabber);
415 GOTO(cleanup, rc = -EPROTO);
416 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
420 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
421 if (lock->l_completion_ast != NULL) {
422 int err = lock->l_completion_ast(lock, *flags, NULL);
430 if (lvb_len && lvb != NULL) {
431 /* Copy the LVB here, and not earlier, because the completion
432 * AST (if any) can override what we got in the reply */
433 memcpy(lvb, lock->l_lvb_data, lvb_len);
436 LDLM_DEBUG(lock, "client-side enqueue END");
439 switch (cleanup_phase) {
442 failed_lock_cleanup(ns, lock, lockh, mode);
444 if (!req_passed_in && req != NULL)
445 ptlrpc_req_finished(req);
452 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
455 struct ldlm_resource *res;
458 if (lock->l_resource->lr_namespace->ns_client) {
459 CERROR("Trying to cancel local lock\n");
462 LDLM_DEBUG(lock, "client-side local convert");
464 res = ldlm_lock_convert(lock, new_mode, flags);
466 ldlm_reprocess_all(res);
471 LDLM_DEBUG(lock, "client-side local convert handler END");
476 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
477 * conversion of locks which are on the waiting or converting queue */
478 /* Caller of this code is supposed to take care of lock readers/writers
480 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
482 struct ldlm_request *body;
483 struct ldlm_reply *reply;
484 struct ldlm_lock *lock;
485 struct ldlm_resource *res;
486 struct ptlrpc_request *req;
487 int rc, size = sizeof(*body);
490 lock = ldlm_handle2lock(lockh);
497 if (lock->l_conn_export == NULL)
498 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
500 LDLM_DEBUG(lock, "client-side convert");
502 req = ptlrpc_prep_req(class_exp2cliimp(lock->l_conn_export),
503 LDLM_CONVERT, 1, &size, NULL);
505 GOTO(out, rc = -ENOMEM);
507 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
508 memcpy(&body->lock_handle1, &lock->l_remote_handle,
509 sizeof(body->lock_handle1));
511 body->lock_desc.l_req_mode = new_mode;
512 body->lock_flags = *flags;
514 size = sizeof(*reply);
515 req->rq_replen = lustre_msg_size(1, &size);
517 rc = ptlrpc_queue_wait(req);
521 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
522 lustre_swab_ldlm_reply);
524 CERROR ("Can't unpack ldlm_reply\n");
525 GOTO (out, rc = -EPROTO);
529 GOTO(out, rc = req->rq_status);
531 res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
533 ldlm_reprocess_all(res);
534 /* Go to sleep until the lock is granted. */
535 /* FIXME: or cancelled. */
536 if (lock->l_completion_ast) {
537 rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
548 ptlrpc_req_finished(req);
552 int ldlm_cli_cancel(struct lustre_handle *lockh)
554 struct ptlrpc_request *req;
555 struct ldlm_lock *lock;
556 struct ldlm_request *body;
557 int rc = 0, size = sizeof(*body);
560 /* concurrent cancels on the same handle can happen */
561 lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
565 if (lock->l_conn_export) {
567 struct obd_import *imp;
569 LDLM_DEBUG(lock, "client-side cancel");
570 /* Set this flag to prevent others from getting new references*/
571 l_lock(&lock->l_resource->lr_namespace->ns_lock);
572 lock->l_flags |= LDLM_FL_CBPENDING;
573 local_only = (lock->l_flags & LDLM_FL_LOCAL_ONLY);
574 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
575 ldlm_cancel_callback(lock);
578 CDEBUG(D_INFO, "not sending request (at caller's "
584 imp = class_exp2cliimp(lock->l_conn_export);
585 if (imp == NULL || imp->imp_invalid) {
586 CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
591 req = ptlrpc_prep_req(imp, LDLM_CANCEL, 1, &size, NULL);
593 GOTO(out, rc = -ENOMEM);
594 req->rq_no_resend = 1;
596 /* XXX FIXME bug 249 */
597 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
598 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
600 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
601 memcpy(&body->lock_handle1, &lock->l_remote_handle,
602 sizeof(body->lock_handle1));
604 req->rq_replen = lustre_msg_size(0, NULL);
606 rc = ptlrpc_queue_wait(req);
609 CERROR("client/server (nid %s) out of sync"
611 libcfs_nid2str(req->rq_import->
612 imp_connection->c_peer.nid));
613 } else if (rc == -ETIMEDOUT) {
614 ptlrpc_req_finished(req);
616 } else if (rc != ELDLM_OK) {
617 CERROR("Got rc %d from cancel RPC: canceling "
621 ptlrpc_req_finished(req);
623 ldlm_lock_cancel(lock);
625 if (lock->l_resource->lr_namespace->ns_client) {
626 LDLM_ERROR(lock, "Trying to cancel local lock");
629 LDLM_DEBUG(lock, "client-side local cancel");
630 ldlm_lock_cancel(lock);
631 ldlm_reprocess_all(lock->l_resource);
632 LDLM_DEBUG(lock, "client-side local cancel handler END");
641 /* when called with LDLM_ASYNC the blocking callback will be handled
642 * in a thread and this function will return after the thread has been
643 * asked to call the callback. when called with LDLM_SYNC the blocking
644 * callback will be performed in this function. */
645 int ldlm_cancel_lru(struct ldlm_namespace *ns, ldlm_sync_t sync)
647 struct ldlm_lock *lock, *next;
653 sync = LDLM_SYNC; /* force to be sync in user space */
656 l_lock(&ns->ns_lock);
657 count = ns->ns_nr_unused - ns->ns_max_unused;
660 l_unlock(&ns->ns_lock);
664 list_for_each_entry_safe(lock, next, &ns->ns_unused_list, l_lru) {
665 LASSERT(!lock->l_readers && !lock->l_writers);
667 /* Setting the CBPENDING flag is a little misleading, but
668 * prevents an important race; namely, once CBPENDING is set,
669 * the lock can accumulate no more readers/writers. Since
670 * readers and writers are already zero here, ldlm_lock_decref
671 * won't see this flag and call l_blocking_ast */
672 lock->l_flags |= LDLM_FL_CBPENDING;
674 LDLM_LOCK_GET(lock); /* dropped by bl thread */
675 ldlm_lock_remove_from_lru(lock);
677 /* We can't re-add to l_lru as it confuses the refcounting in
678 * ldlm_lock_remove_from_lru() if an AST arrives after we drop
679 * ns_lock below. Use l_export_chain as that is unused on
680 * client, and lru is client-only (l_pending_chain is used by
681 * ldlm_chain_lock_for_replay() on client). bug 5666 */
682 if (sync != LDLM_ASYNC || ldlm_bl_to_thread(ns, NULL, lock)) {
683 LASSERTF(list_empty(&lock->l_export_chain),
684 "lock %p next %p prev %p\n",
685 lock, &lock->l_export_chain.next,
686 &lock->l_export_chain.prev);
687 __LDLM_DEBUG(D_INFO, lock, "adding to LRU clear list");
688 list_add(&lock->l_export_chain, &cblist);
694 l_unlock(&ns->ns_lock);
696 list_for_each_entry_safe(lock, next, &cblist, l_export_chain) {
697 list_del_init(&lock->l_export_chain);
698 ldlm_handle_bl_callback(ns, NULL, lock);
703 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
704 struct ldlm_res_id res_id, int flags,
707 struct ldlm_resource *res;
708 struct list_head *tmp, *next, list = LIST_HEAD_INIT(list);
709 struct ldlm_ast_work *w;
712 res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
714 /* This is not a problem. */
715 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
719 l_lock(&ns->ns_lock);
720 list_for_each(tmp, &res->lr_granted) {
721 struct ldlm_lock *lock;
722 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
724 if (opaque != NULL && lock->l_ast_data != opaque) {
725 LDLM_ERROR(lock, "data %p doesn't match opaque %p",
726 lock->l_ast_data, opaque);
731 if (lock->l_readers || lock->l_writers) {
732 if (flags & LDLM_FL_WARN) {
733 LDLM_ERROR(lock, "lock in use");
739 /* See CBPENDING comment in ldlm_cancel_lru */
740 lock->l_flags |= LDLM_FL_CBPENDING;
742 OBD_ALLOC(w, sizeof(*w));
745 w->w_lock = LDLM_LOCK_GET(lock);
747 list_add(&w->w_list, &list);
749 l_unlock(&ns->ns_lock);
751 list_for_each_safe(tmp, next, &list) {
752 struct lustre_handle lockh;
754 w = list_entry(tmp, struct ldlm_ast_work, w_list);
756 if (flags & LDLM_FL_LOCAL_ONLY) {
757 ldlm_lock_cancel(w->w_lock);
759 ldlm_lock2handle(w->w_lock, &lockh);
760 rc = ldlm_cli_cancel(&lockh);
762 CERROR("ldlm_cli_cancel: %d\n", rc);
764 list_del(&w->w_list);
765 LDLM_LOCK_PUT(w->w_lock);
766 OBD_FREE(w, sizeof(*w));
769 ldlm_resource_putref(res);
774 /* Cancel all locks on a namespace (or a specific resource, if given)
775 * that have 0 readers/writers.
777 * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
778 * to notify the server.
779 * If flags & LDLM_FL_WARN, print a warning if some locks are still in use. */
780 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
781 struct ldlm_res_id *res_id, int flags, void *opaque)
790 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
793 l_lock(&ns->ns_lock);
794 for (i = 0; i < RES_HASH_SIZE; i++) {
795 struct list_head *tmp, *pos;
796 list_for_each_safe(tmp, pos, &(ns->ns_hash[i])) {
798 struct ldlm_resource *res;
799 res = list_entry(tmp, struct ldlm_resource, lr_hash);
800 ldlm_resource_getref(res);
802 rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
806 CERROR("cancel_unused_res ("LPU64"): %d\n",
807 res->lr_name.name[0], rc);
808 ldlm_resource_putref(res);
811 l_unlock(&ns->ns_lock);
816 /* join/split resource locks to/from lru list */
817 int ldlm_cli_join_lru(struct ldlm_namespace *ns,
818 struct ldlm_res_id *res_id, int join)
820 struct ldlm_resource *res;
821 struct ldlm_lock *lock, *n;
825 LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT);
827 res = ldlm_resource_get(ns, NULL, *res_id, LDLM_EXTENT, 0);
830 LASSERT(res->lr_type == LDLM_EXTENT);
832 l_lock(&ns->ns_lock);
836 list_for_each_entry_safe (lock, n, &res->lr_granted, l_res_link) {
837 if (list_empty(&lock->l_lru) &&
838 !lock->l_readers && !lock->l_writers &&
839 !(lock->l_flags & LDLM_FL_LOCAL) &&
840 !(lock->l_flags & LDLM_FL_CBPENDING)) {
841 LASSERT(ns->ns_nr_unused >= 0);
842 list_add_tail(&lock->l_lru, &ns->ns_unused_list);
844 lock->l_flags &= ~LDLM_FL_NO_LRU;
845 LDLM_DEBUG(lock, "join lock to lru");
851 list_for_each_entry_safe (lock, n, &ns->ns_unused_list, l_lru) {
852 if (lock->l_resource == res) {
853 ldlm_lock_remove_from_lru(lock);
854 lock->l_flags |= LDLM_FL_NO_LRU;
855 LDLM_DEBUG(lock, "split lock from lru");
860 l_unlock(&ns->ns_lock);
861 ldlm_resource_putref(res);
865 /* Lock iterators. */
867 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
870 struct list_head *tmp, *next;
871 struct ldlm_lock *lock;
872 int rc = LDLM_ITER_CONTINUE;
873 struct ldlm_namespace *ns = res->lr_namespace;
878 RETURN(LDLM_ITER_CONTINUE);
880 l_lock(&ns->ns_lock);
881 list_for_each_safe(tmp, next, &res->lr_granted) {
882 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
884 if (iter(lock, closure) == LDLM_ITER_STOP)
885 GOTO(out, rc = LDLM_ITER_STOP);
888 list_for_each_safe(tmp, next, &res->lr_converting) {
889 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
891 if (iter(lock, closure) == LDLM_ITER_STOP)
892 GOTO(out, rc = LDLM_ITER_STOP);
895 list_for_each_safe(tmp, next, &res->lr_waiting) {
896 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
898 if (iter(lock, closure) == LDLM_ITER_STOP)
899 GOTO(out, rc = LDLM_ITER_STOP);
902 l_unlock(&ns->ns_lock);
906 struct iter_helper_data {
907 ldlm_iterator_t iter;
911 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
913 struct iter_helper_data *helper = closure;
914 return helper->iter(lock, helper->closure);
917 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
919 return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
922 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
925 struct iter_helper_data helper = { iter: iter, closure: closure };
926 return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
929 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
930 ldlm_res_iterator_t iter, void *closure)
932 int i, rc = LDLM_ITER_CONTINUE;
934 l_lock(&ns->ns_lock);
935 for (i = 0; i < RES_HASH_SIZE; i++) {
936 struct list_head *tmp, *next;
937 list_for_each_safe(tmp, next, &(ns->ns_hash[i])) {
938 struct ldlm_resource *res =
939 list_entry(tmp, struct ldlm_resource, lr_hash);
941 ldlm_resource_getref(res);
942 rc = iter(res, closure);
943 ldlm_resource_putref(res);
944 if (rc == LDLM_ITER_STOP)
949 l_unlock(&ns->ns_lock);
953 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
954 void ldlm_change_cbdata(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
955 ldlm_iterator_t iter, void *data)
957 struct ldlm_resource *res;
961 CERROR("must pass in namespace\n");
965 res = ldlm_resource_get(ns, NULL, *res_id, 0, 0);
971 l_lock(&ns->ns_lock);
972 ldlm_resource_foreach(res, iter, data);
973 l_unlock(&ns->ns_lock);
974 ldlm_resource_putref(res);
980 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
982 struct list_head *list = closure;
984 /* we use l_pending_chain here, because it's unused on clients. */
985 LASSERTF(list_empty(&lock->l_pending_chain),"lock %p next %p prev %p\n",
986 lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
987 list_add(&lock->l_pending_chain, list);
988 return LDLM_ITER_CONTINUE;
991 static int replay_lock_interpret(struct ptlrpc_request *req,
994 struct ldlm_lock *lock;
995 struct ldlm_reply *reply;
997 atomic_dec(&req->rq_import->imp_replay_inflight);
1001 lock = req->rq_async_args.pointer_arg[0];
1002 LASSERT(lock != NULL);
1004 reply = lustre_swab_repbuf(req, 0, sizeof (*reply),
1005 lustre_swab_ldlm_reply);
1006 if (reply == NULL) {
1007 CERROR("Can't unpack ldlm_reply\n");
1008 GOTO (out, rc = -EPROTO);
1011 memcpy(&lock->l_remote_handle, &reply->lock_handle,
1012 sizeof(lock->l_remote_handle));
1013 LDLM_DEBUG(lock, "replayed lock:");
1014 ptlrpc_import_recovery_state_machine(req->rq_import);
1017 ptlrpc_connect_import(req->rq_import, NULL);
1023 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
1025 struct ptlrpc_request *req;
1026 struct ldlm_request *body;
1027 struct ldlm_reply *reply;
1033 * If granted mode matches the requested mode, this lock is granted.
1035 * If they differ, but we have a granted mode, then we were granted
1036 * one mode and now want another: ergo, converting.
1038 * If we haven't been granted anything and are on a resource list,
1039 * then we're blocked/waiting.
1041 * If we haven't been granted anything and we're NOT on a resource list,
1042 * then we haven't got a reply yet and don't have a known disposition.
1043 * This happens whenever a lock enqueue is the request that triggers
1046 if (lock->l_granted_mode == lock->l_req_mode)
1047 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
1048 else if (lock->l_granted_mode)
1049 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
1050 else if (!list_empty(&lock->l_res_link))
1051 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
1053 flags = LDLM_FL_REPLAY;
1055 size[0] = sizeof(*body);
1056 req = ptlrpc_prep_req(imp, LDLM_ENQUEUE, 1, size, NULL);
1060 /* We're part of recovery, so don't wait for it. */
1061 req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
1063 body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
1064 ldlm_lock2desc(lock, &body->lock_desc);
1065 body->lock_flags = flags;
1067 ldlm_lock2handle(lock, &body->lock_handle1);
1068 size[0] = sizeof(*reply);
1069 if (lock->l_lvb_len != 0) {
1071 size[1] = lock->l_lvb_len;
1073 req->rq_replen = lustre_msg_size(buffers, size);
1075 LDLM_DEBUG(lock, "replaying lock:");
1077 atomic_inc(&req->rq_import->imp_replay_inflight);
1078 req->rq_async_args.pointer_arg[0] = lock;
1079 req->rq_interpret_reply = replay_lock_interpret;
1080 ptlrpcd_add_req(req);
1085 int ldlm_replay_locks(struct obd_import *imp)
1087 struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
1088 struct list_head list;
1089 struct ldlm_lock *lock, *next;
1093 INIT_LIST_HEAD(&list);
1095 LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1097 /* ensure this doesn't fall to 0 before all have been queued */
1098 atomic_inc(&imp->imp_replay_inflight);
1100 l_lock(&ns->ns_lock);
1101 (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
1103 list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
1104 list_del_init(&lock->l_pending_chain);
1106 continue; /* or try to do the rest? */
1107 rc = replay_one_lock(imp, lock);
1109 l_unlock(&ns->ns_lock);
1111 atomic_dec(&imp->imp_replay_inflight);