Whamcloud - gitweb
land b1_5 onto HEAD
[fs/lustre-release.git] / lustre / ldlm / ldlm_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 #define DEBUG_SUBSYSTEM S_LDLM
26 #ifndef __KERNEL__
27 #include <signal.h>
28 #include <liblustre.h>
29 #endif
30
31 #include <lustre_dlm.h>
32 #include <obd_class.h>
33 #include <obd.h>
34
35 #include "ldlm_internal.h"
36
37 static void interrupted_completion_wait(void *data)
38 {
39 }
40
41 struct lock_wait_data {
42         struct ldlm_lock *lwd_lock;
43         __u32             lwd_conn_cnt;
44 };
45
46 int ldlm_expired_completion_wait(void *data)
47 {
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;
52
53         ENTRY;
54         if (lock->l_conn_export == NULL) {
55                 static cfs_time_t next_dump = 0, last_dump = 0;
56
57                 LDLM_ERROR(lock, "lock timed out (enqueued at %lu, %lus ago); "
58                            "not entering recovery in server code, just going "
59                            "back to sleep", lock->l_enqueued_time.tv_sec,
60                            CURRENT_SECONDS - lock->l_enqueued_time.tv_sec);
61                 if (cfs_time_after(cfs_time_current(), next_dump)) {
62                         last_dump = next_dump;
63                         next_dump = cfs_time_shift(300);
64                         ldlm_namespace_dump(D_DLMTRACE,
65                                             lock->l_resource->lr_namespace);
66                         if (last_dump == 0)
67                                 libcfs_debug_dumplog();
68                 }
69                 RETURN(0);
70         }
71
72         obd = lock->l_conn_export->exp_obd;
73         imp = obd->u.cli.cl_import;
74         ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
75         LDLM_ERROR(lock, "lock timed out (enqueued at %lu, %lus ago), entering "
76                    "recovery for %s@%s", lock->l_enqueued_time.tv_sec,
77                    CURRENT_SECONDS - lock->l_enqueued_time.tv_sec,
78                    obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
79
80         RETURN(0);
81 }
82
83 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
84 {
85         /* XXX ALLOCATE - 160 bytes */
86         struct lock_wait_data lwd;
87         struct obd_device *obd;
88         struct obd_import *imp = NULL;
89         struct l_wait_info lwi;
90         int rc = 0;
91         ENTRY;
92
93         if (flags == LDLM_FL_WAIT_NOREPROC) {
94                 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
95                 goto noreproc;
96         }
97
98         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
99                        LDLM_FL_BLOCK_CONV))) {
100                 cfs_waitq_signal(&lock->l_waitq);
101                 RETURN(0);
102         }
103
104         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
105                    "sleeping");
106         ldlm_lock_dump(D_OTHER, lock, 0);
107         ldlm_reprocess_all(lock->l_resource);
108
109 noreproc:
110
111         obd = class_exp2obd(lock->l_conn_export);
112
113         /* if this is a local lock, then there is no import */
114         if (obd != NULL)
115                 imp = obd->u.cli.cl_import;
116
117         lwd.lwd_lock = lock;
118
119         if (lock->l_flags & LDLM_FL_NO_TIMEOUT) {
120                 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
121                 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
122         } else {
123                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
124                                        ldlm_expired_completion_wait,
125                                        interrupted_completion_wait, &lwd);
126         }
127
128         if (imp != NULL) {
129                 spin_lock(&imp->imp_lock);
130                 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
131                 spin_unlock(&imp->imp_lock);
132         }
133
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);
138
139         if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
140                 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
141                 RETURN(-EIO);
142         }
143
144         if (rc) {
145                 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
146                            rc);
147                 RETURN(rc);
148         }
149
150         LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
151         RETURN(0);
152 }
153
154 /*
155  * ->l_blocking_ast() callback for LDLM locks acquired by server-side OBDs.
156  */
157 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
158                       void *data, int flag)
159 {
160         int do_ast;
161         ENTRY;
162
163         if (flag == LDLM_CB_CANCELING) {
164                 /* Don't need to do anything here. */
165                 RETURN(0);
166         }
167
168         lock_res_and_lock(lock);
169         /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
170          * that ldlm_blocking_ast is called just before intent_policy method
171          * takes the ns_lock, then by the time we get the lock, we might not
172          * be the correct blocking function anymore.  So check, and return
173          * early, if so. */
174         if (lock->l_blocking_ast != ldlm_blocking_ast) {
175                 unlock_res_and_lock(lock);
176                 RETURN(0);
177         }
178
179         lock->l_flags |= LDLM_FL_CBPENDING;
180         do_ast = (!lock->l_readers && !lock->l_writers);
181         unlock_res_and_lock(lock);
182
183         if (do_ast) {
184                 struct lustre_handle lockh;
185                 int rc;
186
187                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
188                 ldlm_lock2handle(lock, &lockh);
189                 rc = ldlm_cli_cancel(&lockh);
190                 if (rc < 0)
191                         CERROR("ldlm_cli_cancel: %d\n", rc);
192         } else {
193                 LDLM_DEBUG(lock, "Lock still has references, will be "
194                            "cancelled later");
195         }
196         RETURN(0);
197 }
198
199 /*
200  * ->l_glimpse_ast() for DLM extent locks acquired on the server-side. See
201  * comment in filter_intent_policy() on why you may need this.
202  */
203 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
204 {
205         /*
206          * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for
207          * that is rather subtle: with OST-side locking, it may so happen that
208          * _all_ extent locks are held by the OST. If client wants to obtain
209          * current file size it calls ll{,u}_glimpse_size(), and (as locks are
210          * on the server), dummy glimpse callback fires and does
211          * nothing. Client still receives correct file size due to the
212          * following fragment in filter_intent_policy():
213          *
214          * rc = l->l_glimpse_ast(l, NULL); // this will update the LVB
215          * if (rc != 0 && res->lr_namespace->ns_lvbo &&
216          *     res->lr_namespace->ns_lvbo->lvbo_update) {
217          *         res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
218          * }
219          *
220          * that is, after glimpse_ast() fails, filter_lvbo_update() runs, and
221          * returns correct file size to the client.
222          */
223         return -ELDLM_NO_LOCK_DATA;
224 }
225
226 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns, struct ldlm_res_id res_id,
227                            ldlm_type_t type, ldlm_policy_data_t *policy,
228                            ldlm_mode_t mode, int *flags,
229                            ldlm_blocking_callback blocking,
230                            ldlm_completion_callback completion,
231                            ldlm_glimpse_callback glimpse,
232                            void *data, __u32 lvb_len, void *lvb_swabber,
233                            struct lustre_handle *lockh)
234 {
235         struct ldlm_lock *lock;
236         int err;
237         ENTRY;
238
239         LASSERT(!(*flags & LDLM_FL_REPLAY));
240         if (ns->ns_client) {
241                 CERROR("Trying to enqueue local lock in a shadow namespace\n");
242                 LBUG();
243         }
244
245         lock = ldlm_lock_create(ns, NULL, res_id, type, mode, blocking,
246                                 completion, glimpse, data, lvb_len);
247         if (!lock)
248                 GOTO(out_nolock, err = -ENOMEM);
249         LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
250
251         ldlm_lock_addref_internal(lock, mode);
252         ldlm_lock2handle(lock, lockh);
253         lock_res_and_lock(lock);
254         lock->l_flags |= LDLM_FL_LOCAL;
255         if (*flags & LDLM_FL_ATOMIC_CB)
256                 lock->l_flags |= LDLM_FL_ATOMIC_CB;
257         lock->l_lvb_swabber = lvb_swabber;
258         unlock_res_and_lock(lock);
259         if (policy != NULL)
260                 lock->l_policy_data = *policy;
261         if (type == LDLM_EXTENT)
262                 lock->l_req_extent = policy->l_extent;
263
264         err = ldlm_lock_enqueue(ns, &lock, policy, flags);
265         if (err != ELDLM_OK)
266                 GOTO(out, err);
267
268         if (policy != NULL)
269                 *policy = lock->l_policy_data;
270         if ((*flags) & LDLM_FL_LOCK_CHANGED)
271                 res_id = lock->l_resource->lr_name;
272
273         LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
274                           lock);
275
276         if (lock->l_completion_ast)
277                 lock->l_completion_ast(lock, *flags, NULL);
278
279         LDLM_DEBUG(lock, "client-side local enqueue END");
280         EXIT;
281  out:
282         LDLM_LOCK_PUT(lock);
283  out_nolock:
284         return err;
285 }
286
287 static void failed_lock_cleanup(struct ldlm_namespace *ns,
288                                 struct ldlm_lock *lock,
289                                 struct lustre_handle *lockh, int mode)
290 {
291         /* Set a flag to prevent us from sending a CANCEL (bug 407) */
292         lock_res_and_lock(lock);
293         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
294         unlock_res_and_lock(lock);
295         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
296
297         ldlm_lock_decref_and_cancel(lockh, mode);
298
299         /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
300          *       from llite/file.c/ll_file_flock(). */
301         if (lock->l_resource->lr_type == LDLM_FLOCK) {
302                 ldlm_lock_destroy(lock);
303         }
304 }
305
306 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
307                           ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
308                           int *flags, void *lvb, __u32 lvb_len,
309                           void *lvb_swabber, struct lustre_handle *lockh,int rc)
310 {
311         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
312         int is_replay = *flags & LDLM_FL_REPLAY;
313         struct ldlm_lock *lock;
314         struct ldlm_reply *reply;
315         int cleanup_phase = 1;
316         ENTRY;
317
318         lock = ldlm_handle2lock(lockh);
319         /* ldlm_cli_enqueue is holding a reference on this lock. */
320         LASSERT(lock != NULL);
321         if (rc != ELDLM_OK) {
322                 LASSERT(!is_replay);
323                 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
324                            rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
325                 if (rc == ELDLM_LOCK_ABORTED) {
326                         /* Before we return, swab the reply */
327                         reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF,
328                                                    sizeof(*reply),
329                                                    lustre_swab_ldlm_reply);
330                         if (reply == NULL) {
331                                 CERROR("Can't unpack ldlm_reply\n");
332                                 rc = -EPROTO;
333                         }
334                         if (lvb_len) {
335                                 void *tmplvb;
336                                 tmplvb = lustre_swab_repbuf(req,
337                                                             DLM_REPLY_REC_OFF,
338                                                             lvb_len,
339                                                             lvb_swabber);
340                                 if (tmplvb == NULL)
341                                         GOTO(cleanup, rc = -EPROTO);
342                                 if (lvb != NULL)
343                                         memcpy(lvb, tmplvb, lvb_len);
344                         }
345                 }
346                 GOTO(cleanup, rc);
347         }
348
349         reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF, sizeof(*reply),
350                                    lustre_swab_ldlm_reply);
351         if (reply == NULL) {
352                 CERROR("Can't unpack ldlm_reply\n");
353                 GOTO(cleanup, rc = -EPROTO);
354         }
355
356         /* lock enqueued on the server */
357         cleanup_phase = 0;
358
359         lock_res_and_lock(lock);
360         lock->l_remote_handle = reply->lock_handle;
361         *flags = reply->lock_flags;
362         lock->l_flags |= reply->lock_flags & LDLM_INHERIT_FLAGS;
363         /* move NO_TIMEOUT flag to the lock to force ldlm_lock_match()
364          * to wait with no timeout as well */
365         lock->l_flags |= reply->lock_flags & LDLM_FL_NO_TIMEOUT;
366         unlock_res_and_lock(lock);
367
368         CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
369                lock, reply->lock_handle.cookie, *flags);
370
371         /* If enqueue returned a blocked lock but the completion handler has
372          * already run, then it fixed up the resource and we don't need to do it
373          * again. */
374         if ((*flags) & LDLM_FL_LOCK_CHANGED) {
375                 int newmode = reply->lock_desc.l_req_mode;
376                 LASSERT(!is_replay);
377                 if (newmode && newmode != lock->l_req_mode) {
378                         LDLM_DEBUG(lock, "server returned different mode %s",
379                                    ldlm_lockname[newmode]);
380                         lock->l_req_mode = newmode;
381                 }
382
383                 if (reply->lock_desc.l_resource.lr_name.name[0] !=
384                     lock->l_resource->lr_name.name[0]) {
385                         CDEBUG(D_INFO, "remote intent success, locking %ld "
386                                "instead of %ld\n",
387                               (long)reply->lock_desc.l_resource.lr_name.name[0],
388                                (long)lock->l_resource->lr_name.name[0]);
389
390                         ldlm_lock_change_resource(ns, lock,
391                                            reply->lock_desc.l_resource.lr_name);
392                         if (lock->l_resource == NULL) {
393                                 LBUG();
394                                 GOTO(cleanup, rc = -ENOMEM);
395                         }
396                         LDLM_DEBUG(lock, "client-side enqueue, new resource");
397                 }
398                 if (with_policy)
399                         if (!(type == LDLM_IBITS && !(exp->exp_connect_flags &
400                                                     OBD_CONNECT_IBITS)))
401                                 lock->l_policy_data =
402                                                  reply->lock_desc.l_policy_data;
403                 if (type != LDLM_PLAIN)
404                         LDLM_DEBUG(lock,"client-side enqueue, new policy data");
405         }
406
407         if ((*flags) & LDLM_FL_AST_SENT ||
408             /* Cancel extent locks as soon as possible on a liblustre client,
409              * because it cannot handle asynchronous ASTs robustly (see
410              * bug 7311). */
411             (LIBLUSTRE_CLIENT && type == LDLM_EXTENT)) {
412                 lock_res_and_lock(lock);
413                 lock->l_flags |= LDLM_FL_CBPENDING;
414                 unlock_res_and_lock(lock);
415                 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
416         }
417
418         /* If the lock has already been granted by a completion AST, don't
419          * clobber the LVB with an older one. */
420         if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
421                 void *tmplvb;
422                 tmplvb = lustre_swab_repbuf(req, DLM_REPLY_REC_OFF, lvb_len,
423                                             lvb_swabber);
424                 if (tmplvb == NULL)
425                         GOTO(cleanup, rc = -EPROTO);
426                 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
427         }
428
429         if (!is_replay) {
430                 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
431                 if (lock->l_completion_ast != NULL) {
432                         int err = lock->l_completion_ast(lock, *flags, NULL);
433                         if (!rc)
434                                 rc = err;
435                         if (rc && type != LDLM_FLOCK) /* bug 9425, bug 10250 */
436                                 cleanup_phase = 1;
437                 }
438         }
439
440         if (lvb_len && lvb != NULL) {
441                 /* Copy the LVB here, and not earlier, because the completion
442                  * AST (if any) can override what we got in the reply */
443                 memcpy(lvb, lock->l_lvb_data, lvb_len);
444         }
445
446         LDLM_DEBUG(lock, "client-side enqueue END");
447         EXIT;
448 cleanup:
449         if (cleanup_phase == 1 && rc)
450                 failed_lock_cleanup(ns, lock, lockh, mode);
451         /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
452         LDLM_LOCK_PUT(lock);
453         LDLM_LOCK_PUT(lock);
454         return rc;
455 }
456
457 /* If a request has some specific initialisation it is passed in @reqp,
458  * otherwise it is created in ldlm_cli_enqueue.
459  *
460  * Supports sync and async requests, pass @async flag accordingly. If a
461  * request was created in ldlm_cli_enqueue and it is the async request,
462  * pass it to the caller in @reqp. */
463 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
464                      struct ldlm_res_id res_id, ldlm_type_t type,
465                      ldlm_policy_data_t *policy, ldlm_mode_t mode, int *flags,
466                      ldlm_blocking_callback blocking,
467                      ldlm_completion_callback completion,
468                      ldlm_glimpse_callback glimpse,
469                      void *data, void *lvb, __u32 lvb_len, void *lvb_swabber,
470                      struct lustre_handle *lockh, int async)
471 {
472         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
473         struct ldlm_lock *lock;
474         struct ldlm_request *body;
475         struct ldlm_reply *reply;
476         int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
477                         [DLM_LOCKREQ_OFF]     = sizeof(*body),
478                         [DLM_REPLY_REC_OFF]   = lvb_len };
479         int is_replay = *flags & LDLM_FL_REPLAY;
480         int req_passed_in = 1, rc;
481         struct ptlrpc_request *req;
482         ENTRY;
483
484         LASSERT(exp != NULL);
485
486         /* If we're replaying this lock, just check some invariants.
487          * If we're creating a new lock, get everything all setup nice. */
488         if (is_replay) {
489                 lock = ldlm_handle2lock(lockh);
490                 LASSERT(lock != NULL);
491                 LDLM_DEBUG(lock, "client-side enqueue START");
492                 LASSERT(exp == lock->l_conn_export);
493         } else {
494                 lock = ldlm_lock_create(ns, NULL, res_id, type, mode, blocking,
495                                         completion, glimpse, data, lvb_len);
496                 if (lock == NULL)
497                         RETURN(-ENOMEM);
498                 /* for the local lock, add the reference */
499                 ldlm_lock_addref_internal(lock, mode);
500                 ldlm_lock2handle(lock, lockh);
501                 lock->l_lvb_swabber = lvb_swabber;
502                 if (policy != NULL) {
503                         /* INODEBITS_INTEROP: If the server does not support
504                          * inodebits, we will request a plain lock in the
505                          * descriptor (ldlm_lock2desc() below) but use an
506                          * inodebits lock internally with both bits set.
507                          */
508                         if (type == LDLM_IBITS && !(exp->exp_connect_flags &
509                                                     OBD_CONNECT_IBITS))
510                                 lock->l_policy_data.l_inodebits.bits =
511                                         MDS_INODELOCK_LOOKUP |
512                                         MDS_INODELOCK_UPDATE;
513                         else
514                                 lock->l_policy_data = *policy;
515                 }
516
517                 if (type == LDLM_EXTENT)
518                         lock->l_req_extent = policy->l_extent;
519                 LDLM_DEBUG(lock, "client-side enqueue START");
520         }
521
522         /* lock not sent to server yet */
523
524         if (reqp == NULL || *reqp == NULL) {
525                 req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
526                                       LDLM_ENQUEUE, 2, size, NULL);
527                 if (req == NULL) {
528                         failed_lock_cleanup(ns, lock, lockh, mode);
529                         LDLM_LOCK_PUT(lock);
530                         RETURN(-ENOMEM);
531                 }
532                 req_passed_in = 0;
533                 if (reqp)
534                         *reqp = req;
535         } else {
536                 req = *reqp;
537                 LASSERTF(lustre_msg_buflen(req->rq_reqmsg, DLM_LOCKREQ_OFF) ==
538                          sizeof(*body), "buflen[%d] = %d, not "LPSZ"\n",
539                          DLM_LOCKREQ_OFF,
540                          lustre_msg_buflen(req->rq_reqmsg, DLM_LOCKREQ_OFF),
541                          sizeof(*body));
542         }
543
544         lock->l_conn_export = exp;
545         lock->l_export = NULL;
546         lock->l_blocking_ast = blocking;
547
548         /* Dump lock data into the request buffer */
549         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
550         ldlm_lock2desc(lock, &body->lock_desc);
551         body->lock_flags = *flags;
552         body->lock_handle1 = *lockh;
553
554         /* Continue as normal. */
555         if (!req_passed_in) {
556                 size[DLM_LOCKREPLY_OFF] = sizeof(*reply);
557                 ptlrpc_req_set_repsize(req, 2 + (lvb_len > 0), size);
558         }
559
560         /*
561          * Liblustre client doesn't get extent locks, except for O_APPEND case
562          * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
563          * [i_size, OBD_OBJECT_EOF] lock is taken.
564          */
565         LASSERT(ergo(LIBLUSTRE_CLIENT, type != LDLM_EXTENT ||
566                      policy->l_extent.end == OBD_OBJECT_EOF));
567
568         if (async) {
569                 LASSERT(reqp != NULL);
570                 RETURN(0);
571         }
572
573         LDLM_DEBUG(lock, "sending request");
574         rc = ptlrpc_queue_wait(req);
575         rc = ldlm_cli_enqueue_fini(exp, req, type, policy ? 1 : 0,
576                                    mode, flags, lvb, lvb_len, lvb_swabber,
577                                    lockh, rc);
578
579         if (!req_passed_in && req != NULL) {
580                 ptlrpc_req_finished(req);
581                 if (reqp)
582                         *reqp = NULL;
583         }
584
585         RETURN(rc);
586 }
587
588 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
589                                   int *flags)
590 {
591         struct ldlm_resource *res;
592         int rc;
593         ENTRY;
594         if (lock->l_resource->lr_namespace->ns_client) {
595                 CERROR("Trying to cancel local lock\n");
596                 LBUG();
597         }
598         LDLM_DEBUG(lock, "client-side local convert");
599
600         res = ldlm_lock_convert(lock, new_mode, flags);
601         if (res) {
602                 ldlm_reprocess_all(res);
603                 rc = 0;
604         } else {
605                 rc = EDEADLOCK;
606         }
607         LDLM_DEBUG(lock, "client-side local convert handler END");
608         LDLM_LOCK_PUT(lock);
609         RETURN(rc);
610 }
611
612 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
613  * conversion of locks which are on the waiting or converting queue */
614 /* Caller of this code is supposed to take care of lock readers/writers
615    accounting */
616 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
617 {
618         struct ldlm_request *body;
619         struct ldlm_reply *reply;
620         struct ldlm_lock *lock;
621         struct ldlm_resource *res;
622         struct ptlrpc_request *req;
623         int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
624                         [DLM_LOCKREQ_OFF]     = sizeof(*body) };
625         int rc;
626         ENTRY;
627
628         lock = ldlm_handle2lock(lockh);
629         if (!lock) {
630                 LBUG();
631                 RETURN(-EINVAL);
632         }
633         *flags = 0;
634
635         if (lock->l_conn_export == NULL)
636                 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
637
638         LDLM_DEBUG(lock, "client-side convert");
639
640         req = ptlrpc_prep_req(class_exp2cliimp(lock->l_conn_export),
641                               LUSTRE_DLM_VERSION, LDLM_CONVERT, 2, size, NULL);
642         if (!req)
643                 GOTO(out, rc = -ENOMEM);
644
645         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
646         body->lock_handle1 = lock->l_remote_handle;
647
648         body->lock_desc.l_req_mode = new_mode;
649         body->lock_flags = *flags;
650
651         size[DLM_LOCKREPLY_OFF] = sizeof(*reply);
652         ptlrpc_req_set_repsize(req, 2, size);
653
654         rc = ptlrpc_queue_wait(req);
655         if (rc != ELDLM_OK)
656                 GOTO(out, rc);
657
658         reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF, sizeof(*reply),
659                                    lustre_swab_ldlm_reply);
660         if (reply == NULL) {
661                 CERROR ("Can't unpack ldlm_reply\n");
662                 GOTO (out, rc = -EPROTO);
663         }
664
665         if (req->rq_status)
666                 GOTO(out, rc = req->rq_status);
667
668         res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
669         if (res != NULL) {
670                 ldlm_reprocess_all(res);
671                 /* Go to sleep until the lock is granted. */
672                 /* FIXME: or cancelled. */
673                 if (lock->l_completion_ast) {
674                         rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
675                                                     NULL);
676                         if (rc)
677                                 GOTO(out, rc);
678                 }
679         } else {
680                 rc = EDEADLOCK;
681         }
682         EXIT;
683  out:
684         LDLM_LOCK_PUT(lock);
685         ptlrpc_req_finished(req);
686         return rc;
687 }
688
689 int ldlm_cli_cancel(struct lustre_handle *lockh)
690 {
691         struct ptlrpc_request *req;
692         struct ldlm_lock *lock;
693         struct ldlm_request *body;
694         int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
695                         [DLM_LOCKREQ_OFF]     = sizeof(*body) };
696         int rc = 0;
697         ENTRY;
698
699         /* concurrent cancels on the same handle can happen */
700         lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
701         if (lock == NULL)
702                 RETURN(0);
703
704         if (lock->l_conn_export) {
705                 int local_only;
706                 struct obd_import *imp;
707
708                 LDLM_DEBUG(lock, "client-side cancel");
709                 /* Set this flag to prevent others from getting new references*/
710                 lock_res_and_lock(lock);
711                 lock->l_flags |= LDLM_FL_CBPENDING;
712                 local_only = (lock->l_flags &
713                               (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
714                 ldlm_cancel_callback(lock);
715                 unlock_res_and_lock(lock);
716
717                 if (local_only) {
718                         CDEBUG(D_INFO, "not sending request (at caller's "
719                                "instruction)\n");
720                         goto local_cancel;
721                 }
722
723         restart:
724                 imp = class_exp2cliimp(lock->l_conn_export);
725                 if (imp == NULL || imp->imp_invalid) {
726                         CDEBUG(D_HA, "skipping cancel on invalid import %p\n",
727                                imp);
728                         goto local_cancel;
729                 }
730
731                 req = ptlrpc_prep_req(imp, LUSTRE_DLM_VERSION, LDLM_CANCEL, 2,
732                                       size, NULL);
733                 if (!req)
734                         GOTO(out, rc = -ENOMEM);
735                 req->rq_no_resend = 1;
736
737                 /* XXX FIXME bug 249 */
738                 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
739                 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
740
741                 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF,
742                                       sizeof(*body));
743                 body->lock_handle1 = lock->l_remote_handle;
744
745                 ptlrpc_req_set_repsize(req, 1, NULL);
746                 rc = ptlrpc_queue_wait(req);
747
748                 if (rc == ESTALE) {
749                         CDEBUG(D_DLMTRACE, "client/server (nid %s) out of sync "
750                                "-- not fatal, flags %x\n",
751                                libcfs_nid2str(req->rq_import->
752                                               imp_connection->c_peer.nid),
753                                lock->l_flags);
754                 } else if (rc == -ETIMEDOUT) {
755                         ptlrpc_req_finished(req);
756                         GOTO(restart, rc);
757                 } else if (rc != ELDLM_OK) {
758                         CERROR("Got rc %d from cancel RPC: canceling "
759                                "anyway\n", rc);
760                 }
761
762                 ptlrpc_req_finished(req);
763         local_cancel:
764                 ldlm_lock_cancel(lock);
765         } else {
766                 if (lock->l_resource->lr_namespace->ns_client) {
767                         LDLM_ERROR(lock, "Trying to cancel local lock");
768                         LBUG();
769                 }
770                 LDLM_DEBUG(lock, "client-side local cancel");
771                 ldlm_lock_cancel(lock);
772                 ldlm_reprocess_all(lock->l_resource);
773                 LDLM_DEBUG(lock, "client-side local cancel handler END");
774         }
775
776         EXIT;
777  out:
778         LDLM_LOCK_PUT(lock);
779         return rc;
780 }
781
782 /* when called with LDLM_ASYNC the blocking callback will be handled
783  * in a thread and this function will return after the thread has been
784  * asked to call the callback.  when called with LDLM_SYNC the blocking
785  * callback will be performed in this function. */
786 int ldlm_cancel_lru(struct ldlm_namespace *ns, ldlm_sync_t sync)
787 {
788         struct ldlm_lock *lock, *next;
789         int count, rc = 0;
790         CFS_LIST_HEAD(cblist);
791         ENTRY;
792
793 #ifndef __KERNEL__
794         sync = LDLM_SYNC; /* force to be sync in user space */
795 #endif
796
797         spin_lock(&ns->ns_unused_lock);
798         count = ns->ns_nr_unused - ns->ns_max_unused;
799
800         if (count <= 0) {
801                 spin_unlock(&ns->ns_unused_lock);
802                 RETURN(0);
803         }
804
805         while (!list_empty(&ns->ns_unused_list)) {
806                 struct list_head *tmp = ns->ns_unused_list.next;
807                 lock = list_entry(tmp, struct ldlm_lock, l_lru);
808                 LASSERT(!lock->l_readers && !lock->l_writers);
809
810                 LDLM_LOCK_GET(lock); /* dropped by bl thread */
811                 spin_unlock(&ns->ns_unused_lock);
812
813                 lock_res_and_lock(lock);
814                 if (ldlm_lock_remove_from_lru(lock) == 0) {
815                         /* other thread is removing lock from lru */
816                         unlock_res_and_lock(lock);
817                         LDLM_LOCK_PUT(lock);
818                         spin_lock(&ns->ns_unused_lock);
819                         continue;
820
821                 }
822
823                 /* If we have chosen to canecl this lock voluntarily, we better
824                    send cancel notification to server, so that it frees
825                    appropriate state. This might lead to a race where while
826                    we are doing cancel here, server is also silently
827                    cancelling this lock. */
828                 lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
829
830                 /* Setting the CBPENDING flag is a little misleading, but
831                  * prevents an important race; namely, once CBPENDING is set,
832                  * the lock can accumulate no more readers/writers.  Since
833                  * readers and writers are already zero here, ldlm_lock_decref
834                  * won't see this flag and call l_blocking_ast */
835                 lock->l_flags |= LDLM_FL_CBPENDING;
836
837                 /* We can't re-add to l_lru as it confuses the refcounting in
838                  * ldlm_lock_remove_from_lru() if an AST arrives after we drop
839                  * ns_lock below. We use l_tmp and can't use l_pending_chain as
840                  * it is used both on server and client nevertheles bug 5666
841                  * says it is used only on server. --umka */
842                 list_add(&lock->l_tmp, &cblist);
843                 unlock_res_and_lock(lock);
844
845                 LDLM_LOCK_GET(lock); /* to hold lock after bl thread */
846                 if (sync == LDLM_ASYNC && (ldlm_bl_to_thread(ns, NULL, lock) == 0)) {
847                         lock_res_and_lock(lock);
848                         list_del_init(&lock->l_tmp);
849                         unlock_res_and_lock(lock);
850                 }
851                 LDLM_LOCK_PUT(lock);
852
853                 spin_lock(&ns->ns_unused_lock);
854
855                 if (--count == 0)
856                         break;
857         }
858         spin_unlock(&ns->ns_unused_lock);
859
860         list_for_each_entry_safe(lock, next, &cblist, l_tmp) {
861                 list_del_init(&lock->l_tmp);
862                 ldlm_handle_bl_callback(ns, NULL, lock);
863         }
864
865         RETURN(rc);
866 }
867
868 static int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
869                                            struct ldlm_res_id res_id, int flags,
870                                            void *opaque)
871 {
872         struct list_head *tmp, *next, list = CFS_LIST_HEAD_INIT(list);
873         struct ldlm_resource *res;
874         struct ldlm_lock *lock;
875         ENTRY;
876
877         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
878         if (res == NULL) {
879                 /* This is not a problem. */
880                 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id.name[0]);
881                 RETURN(0);
882         }
883
884         lock_res(res);
885         list_for_each(tmp, &res->lr_granted) {
886                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
887
888                 if (opaque != NULL && lock->l_ast_data != opaque) {
889                         LDLM_ERROR(lock, "data %p doesn't match opaque %p",
890                                    lock->l_ast_data, opaque);
891                         //LBUG();
892                         continue;
893                 }
894
895                 if (lock->l_readers || lock->l_writers) {
896                         if (flags & LDLM_FL_WARN) {
897                                 LDLM_ERROR(lock, "lock in use");
898                                 //LBUG();
899                         }
900                         continue;
901                 }
902
903                 /* See CBPENDING comment in ldlm_cancel_lru */
904                 lock->l_flags |= LDLM_FL_CBPENDING;
905
906                 LASSERT(list_empty(&lock->l_bl_ast));
907                 list_add(&lock->l_bl_ast, &list);
908                 LDLM_LOCK_GET(lock);
909         }
910         unlock_res(res);
911
912         list_for_each_safe(tmp, next, &list) {
913                 struct lustre_handle lockh;
914                 int rc;
915                 lock = list_entry(tmp, struct ldlm_lock, l_bl_ast);
916
917                 if (flags & LDLM_FL_LOCAL_ONLY) {
918                         ldlm_lock_cancel(lock);
919                 } else {
920                         ldlm_lock2handle(lock, &lockh);
921                         rc = ldlm_cli_cancel(&lockh);
922                         if (rc != ELDLM_OK)
923                                 CERROR("ldlm_cli_cancel: %d\n", rc);
924                 }
925                 list_del_init(&lock->l_bl_ast);
926                 LDLM_LOCK_PUT(lock);
927         }
928
929         ldlm_resource_putref(res);
930
931         RETURN(0);
932 }
933
934 static inline int have_no_nsresource(struct ldlm_namespace *ns)
935 {
936         int no_resource = 0;
937
938         spin_lock(&ns->ns_hash_lock);
939         if (ns->ns_resources == 0)
940                 no_resource = 1;
941         spin_unlock(&ns->ns_hash_lock);
942
943         RETURN(no_resource);
944 }
945
946 /* Cancel all locks on a namespace (or a specific resource, if given)
947  * that have 0 readers/writers.
948  *
949  * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
950  * to notify the server.
951  * If flags & LDLM_FL_WARN, print a warning if some locks are still in use. */
952 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
953                            struct ldlm_res_id *res_id, int flags, void *opaque)
954 {
955         int i;
956         ENTRY;
957
958         if (ns == NULL)
959                 RETURN(ELDLM_OK);
960
961         if (res_id)
962                 RETURN(ldlm_cli_cancel_unused_resource(ns, *res_id, flags,
963                                                        opaque));
964
965         spin_lock(&ns->ns_hash_lock);
966         for (i = 0; i < RES_HASH_SIZE; i++) {
967                 struct list_head *tmp;
968                 tmp = ns->ns_hash[i].next;
969                 while (tmp != &(ns->ns_hash[i])) {
970                         struct ldlm_resource *res;
971                         int rc;
972
973                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
974                         ldlm_resource_getref(res);
975                         spin_unlock(&ns->ns_hash_lock);
976
977                         rc = ldlm_cli_cancel_unused_resource(ns, res->lr_name,
978                                                              flags, opaque);
979
980                         if (rc)
981                                 CERROR("cancel_unused_res ("LPU64"): %d\n",
982                                        res->lr_name.name[0], rc);
983
984                         spin_lock(&ns->ns_hash_lock);
985                         tmp = tmp->next;
986                         ldlm_resource_putref_locked(res);
987                 }
988         }
989         spin_unlock(&ns->ns_hash_lock);
990
991         RETURN(ELDLM_OK);
992 }
993
994 /* join/split resource locks to/from lru list */
995 int ldlm_cli_join_lru(struct ldlm_namespace *ns,
996                       struct ldlm_res_id *res_id, int join)
997 {
998         struct ldlm_resource *res;
999         struct ldlm_lock *lock, *n;
1000         int count = 0;
1001         ENTRY;
1002
1003         LASSERT(ns->ns_client == LDLM_NAMESPACE_CLIENT);
1004
1005         res = ldlm_resource_get(ns, NULL, *res_id, LDLM_EXTENT, 0);
1006         if (res == NULL)
1007                 RETURN(count);
1008         LASSERT(res->lr_type == LDLM_EXTENT);
1009
1010         lock_res(res);
1011         if (!join)
1012                 goto split;
1013
1014         list_for_each_entry_safe (lock, n, &res->lr_granted, l_res_link) {
1015                 if (list_empty(&lock->l_lru) &&
1016                     !lock->l_readers && !lock->l_writers &&
1017                     !(lock->l_flags & LDLM_FL_LOCAL) &&
1018                     !(lock->l_flags & LDLM_FL_CBPENDING)) {
1019                         spin_lock(&ns->ns_unused_lock);
1020                         LASSERT(ns->ns_nr_unused >= 0);
1021                         list_add_tail(&lock->l_lru, &ns->ns_unused_list);
1022                         ns->ns_nr_unused++;
1023                         spin_unlock(&ns->ns_unused_lock);
1024                         lock->l_flags &= ~LDLM_FL_NO_LRU;
1025                         LDLM_DEBUG(lock, "join lock to lru");
1026                         count++;
1027                 }
1028         }
1029         goto unlock;
1030 split:
1031         spin_lock(&ns->ns_unused_lock);
1032         list_for_each_entry_safe (lock, n, &ns->ns_unused_list, l_lru) {
1033                 if (lock->l_resource == res) {
1034                         ldlm_lock_remove_from_lru_nolock(lock);
1035                         lock->l_flags |= LDLM_FL_NO_LRU;
1036                         LDLM_DEBUG(lock, "split lock from lru");
1037                         count++;
1038                 }
1039         }
1040         spin_unlock(&ns->ns_unused_lock);
1041 unlock:
1042         unlock_res(res);
1043         ldlm_resource_putref(res);
1044         RETURN(count);
1045 }
1046
1047 /* Lock iterators. */
1048
1049 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1050                           void *closure)
1051 {
1052         struct list_head *tmp, *next;
1053         struct ldlm_lock *lock;
1054         int rc = LDLM_ITER_CONTINUE;
1055
1056         ENTRY;
1057
1058         if (!res)
1059                 RETURN(LDLM_ITER_CONTINUE);
1060
1061         lock_res(res);
1062         list_for_each_safe(tmp, next, &res->lr_granted) {
1063                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1064
1065                 if (iter(lock, closure) == LDLM_ITER_STOP)
1066                         GOTO(out, rc = LDLM_ITER_STOP);
1067         }
1068
1069         list_for_each_safe(tmp, next, &res->lr_converting) {
1070                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1071
1072                 if (iter(lock, closure) == LDLM_ITER_STOP)
1073                         GOTO(out, rc = LDLM_ITER_STOP);
1074         }
1075
1076         list_for_each_safe(tmp, next, &res->lr_waiting) {
1077                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1078
1079                 if (iter(lock, closure) == LDLM_ITER_STOP)
1080                         GOTO(out, rc = LDLM_ITER_STOP);
1081         }
1082  out:
1083         unlock_res(res);
1084         RETURN(rc);
1085 }
1086
1087 struct iter_helper_data {
1088         ldlm_iterator_t iter;
1089         void *closure;
1090 };
1091
1092 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1093 {
1094         struct iter_helper_data *helper = closure;
1095         return helper->iter(lock, helper->closure);
1096 }
1097
1098 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
1099 {
1100         return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
1101 }
1102
1103 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
1104                            void *closure)
1105 {
1106         struct iter_helper_data helper = { iter: iter, closure: closure };
1107         return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
1108 }
1109
1110 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
1111                                ldlm_res_iterator_t iter, void *closure)
1112 {
1113         int i, rc = LDLM_ITER_CONTINUE;
1114         struct ldlm_resource *res;
1115         struct list_head *tmp;
1116
1117         ENTRY;
1118         spin_lock(&ns->ns_hash_lock);
1119         for (i = 0; i < RES_HASH_SIZE; i++) {
1120                 tmp = ns->ns_hash[i].next;
1121                 while (tmp != &(ns->ns_hash[i])) {
1122                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
1123                         ldlm_resource_getref(res);
1124                         spin_unlock(&ns->ns_hash_lock);
1125
1126                         rc = iter(res, closure);
1127
1128                         spin_lock(&ns->ns_hash_lock);
1129                         tmp = tmp->next;
1130                         ldlm_resource_putref_locked(res);
1131                         if (rc == LDLM_ITER_STOP)
1132                                 GOTO(out, rc);
1133                 }
1134         }
1135  out:
1136         spin_unlock(&ns->ns_hash_lock);
1137         RETURN(rc);
1138 }
1139
1140 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
1141 void ldlm_resource_iterate(struct ldlm_namespace *ns, struct ldlm_res_id *res_id,
1142                            ldlm_iterator_t iter, void *data)
1143 {
1144         struct ldlm_resource *res;
1145         ENTRY;
1146
1147         if (ns == NULL) {
1148                 CERROR("must pass in namespace\n");
1149                 LBUG();
1150         }
1151
1152         res = ldlm_resource_get(ns, NULL, *res_id, 0, 0);
1153         if (res == NULL) {
1154                 EXIT;
1155                 return;
1156         }
1157
1158         ldlm_resource_foreach(res, iter, data);
1159         ldlm_resource_putref(res);
1160         EXIT;
1161 }
1162
1163 /* Lock replay */
1164
1165 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
1166 {
1167         struct list_head *list = closure;
1168
1169         /* we use l_pending_chain here, because it's unused on clients. */
1170         LASSERTF(list_empty(&lock->l_pending_chain),"lock %p next %p prev %p\n",
1171                  lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
1172         /* bug 9573: don't replay locks left after eviction */
1173         if (!(lock->l_flags & LDLM_FL_FAILED))
1174                 list_add(&lock->l_pending_chain, list);
1175         return LDLM_ITER_CONTINUE;
1176 }
1177
1178 static int replay_lock_interpret(struct ptlrpc_request *req,
1179                                     void * data, int rc)
1180 {
1181         struct ldlm_lock *lock;
1182         struct ldlm_reply *reply;
1183
1184         ENTRY;
1185         atomic_dec(&req->rq_import->imp_replay_inflight);
1186         if (rc != ELDLM_OK)
1187                 GOTO(out, rc);
1188
1189         lock = req->rq_async_args.pointer_arg[0];
1190         LASSERT(lock != NULL);
1191
1192         reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF, sizeof(*reply),
1193                                    lustre_swab_ldlm_reply);
1194         if (reply == NULL) {
1195                 CERROR("Can't unpack ldlm_reply\n");
1196                 GOTO (out, rc = -EPROTO);
1197         }
1198
1199         lock->l_remote_handle = reply->lock_handle;
1200         LDLM_DEBUG(lock, "replayed lock:");
1201         ptlrpc_import_recovery_state_machine(req->rq_import);
1202  out:
1203         if (rc != ELDLM_OK)
1204                 ptlrpc_connect_import(req->rq_import, NULL);
1205
1206
1207         RETURN(rc);
1208 }
1209
1210 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
1211 {
1212         struct ptlrpc_request *req;
1213         struct ldlm_request *body;
1214         struct ldlm_reply *reply;
1215         int buffers = 2;
1216         int size[3] = { sizeof(struct ptlrpc_body) };
1217         int flags;
1218         ENTRY;
1219
1220         /* If this is reply-less callback lock, we cannot replay it, since
1221          * server might have long dropped it, but notification of that event was
1222          * lost by network. (and server granted conflicting lock already) */
1223         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1224                 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
1225                 ldlm_lock_cancel(lock);
1226                 RETURN(0);
1227         }
1228         /*
1229          * If granted mode matches the requested mode, this lock is granted.
1230          *
1231          * If they differ, but we have a granted mode, then we were granted
1232          * one mode and now want another: ergo, converting.
1233          *
1234          * If we haven't been granted anything and are on a resource list,
1235          * then we're blocked/waiting.
1236          *
1237          * If we haven't been granted anything and we're NOT on a resource list,
1238          * then we haven't got a reply yet and don't have a known disposition.
1239          * This happens whenever a lock enqueue is the request that triggers
1240          * recovery.
1241          */
1242         if (lock->l_granted_mode == lock->l_req_mode)
1243                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
1244         else if (lock->l_granted_mode)
1245                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
1246         else if (!list_empty(&lock->l_res_link))
1247                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
1248         else
1249                 flags = LDLM_FL_REPLAY;
1250
1251         size[DLM_LOCKREQ_OFF] = sizeof(*body);
1252         req = ptlrpc_prep_req(imp, LUSTRE_DLM_VERSION, LDLM_ENQUEUE, 2, size,
1253                               NULL);
1254         if (!req)
1255                 RETURN(-ENOMEM);
1256
1257         /* We're part of recovery, so don't wait for it. */
1258         req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
1259
1260         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
1261         ldlm_lock2desc(lock, &body->lock_desc);
1262         body->lock_flags = flags;
1263
1264         ldlm_lock2handle(lock, &body->lock_handle1);
1265         size[DLM_LOCKREPLY_OFF] = sizeof(*reply);
1266         if (lock->l_lvb_len != 0) {
1267                 buffers = 3;
1268                 size[DLM_REPLY_REC_OFF] = lock->l_lvb_len;
1269         }
1270         ptlrpc_req_set_repsize(req, buffers, size);
1271
1272         LDLM_DEBUG(lock, "replaying lock:");
1273
1274         atomic_inc(&req->rq_import->imp_replay_inflight);
1275         req->rq_async_args.pointer_arg[0] = lock;
1276         req->rq_interpret_reply = replay_lock_interpret;
1277         ptlrpcd_add_req(req);
1278
1279         RETURN(0);
1280 }
1281
1282 int ldlm_replay_locks(struct obd_import *imp)
1283 {
1284         struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
1285         struct list_head list;
1286         struct ldlm_lock *lock, *next;
1287         int rc = 0;
1288
1289         ENTRY;
1290         CFS_INIT_LIST_HEAD(&list);
1291
1292         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1293
1294         /* ensure this doesn't fall to 0 before all have been queued */
1295         atomic_inc(&imp->imp_replay_inflight);
1296
1297         (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
1298
1299         list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
1300                 list_del_init(&lock->l_pending_chain);
1301                 if (rc)
1302                         continue; /* or try to do the rest? */
1303                 rc = replay_one_lock(imp, lock);
1304         }
1305
1306         atomic_dec(&imp->imp_replay_inflight);
1307
1308         RETURN(rc);
1309 }