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