Whamcloud - gitweb
2b3447391ab566f5eac8294c7969685b20b39795
[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 struct ldlm_async_args {
47         struct lustre_handle lock_handle;
48 };
49
50 int ldlm_expired_completion_wait(void *data)
51 {
52         struct lock_wait_data *lwd = data;
53         struct ldlm_lock *lock = lwd->lwd_lock;
54         struct obd_import *imp;
55         struct obd_device *obd;
56
57         ENTRY;
58         if (lock->l_conn_export == NULL) {
59                 static cfs_time_t next_dump = 0, last_dump = 0;
60
61                 if (ptlrpc_check_suspend())
62                         RETURN(0);
63
64                 LDLM_ERROR(lock, "lock timed out (enqueued at %lu, %lus ago); "
65                            "not entering recovery in server code, just going "
66                            "back to sleep", lock->l_enqueued_time.tv_sec,
67                            CURRENT_SECONDS - lock->l_enqueued_time.tv_sec);
68                 if (cfs_time_after(cfs_time_current(), next_dump)) {
69                         last_dump = next_dump;
70                         next_dump = cfs_time_shift(300);
71                         ldlm_namespace_dump(D_DLMTRACE,
72                                             lock->l_resource->lr_namespace);
73                         if (last_dump == 0)
74                                 libcfs_debug_dumplog();
75                 }
76                 RETURN(0);
77         }
78
79         obd = lock->l_conn_export->exp_obd;
80         imp = obd->u.cli.cl_import;
81         ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
82         LDLM_ERROR(lock, "lock timed out (enqueued at %lu, %lus ago), entering "
83                    "recovery for %s@%s", lock->l_enqueued_time.tv_sec,
84                    CURRENT_SECONDS - lock->l_enqueued_time.tv_sec,
85                    obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
86
87         RETURN(0);
88 }
89
90 int ldlm_completion_ast(struct ldlm_lock *lock, int flags, void *data)
91 {
92         /* XXX ALLOCATE - 160 bytes */
93         struct lock_wait_data lwd;
94         struct obd_device *obd;
95         struct obd_import *imp = NULL;
96         struct l_wait_info lwi;
97         int rc = 0;
98         ENTRY;
99
100         if (flags == LDLM_FL_WAIT_NOREPROC) {
101                 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
102                 goto noreproc;
103         }
104
105         if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
106                        LDLM_FL_BLOCK_CONV))) {
107                 cfs_waitq_signal(&lock->l_waitq);
108                 RETURN(0);
109         }
110
111         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
112                    "sleeping");
113         ldlm_lock_dump(D_OTHER, lock, 0);
114         ldlm_reprocess_all(lock->l_resource);
115
116 noreproc:
117
118         obd = class_exp2obd(lock->l_conn_export);
119
120         /* if this is a local lock, then there is no import */
121         if (obd != NULL)
122                 imp = obd->u.cli.cl_import;
123
124         lwd.lwd_lock = lock;
125
126         if (lock->l_flags & LDLM_FL_NO_TIMEOUT) {
127                 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
128                 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
129         } else {
130                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(obd_timeout),
131                                        ldlm_expired_completion_wait,
132                                        interrupted_completion_wait, &lwd);
133         }
134
135         if (imp != NULL) {
136                 spin_lock(&imp->imp_lock);
137                 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
138                 spin_unlock(&imp->imp_lock);
139         }
140
141         /* Go to sleep until the lock is granted or cancelled. */
142         rc = l_wait_event(lock->l_waitq,
143                           ((lock->l_req_mode == lock->l_granted_mode) ||
144                            (lock->l_flags & LDLM_FL_FAILED)), &lwi);
145
146         if (lock->l_destroyed || lock->l_flags & LDLM_FL_FAILED) {
147                 LDLM_DEBUG(lock, "client-side enqueue waking up: destroyed");
148                 RETURN(-EIO);
149         }
150
151         if (rc) {
152                 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
153                            rc);
154                 RETURN(rc);
155         }
156
157         LDLM_DEBUG(lock, "client-side enqueue waking up: granted");
158         RETURN(0);
159 }
160
161 /*
162  * ->l_blocking_ast() callback for LDLM locks acquired by server-side OBDs.
163  */
164 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
165                       void *data, int flag)
166 {
167         int do_ast;
168         ENTRY;
169
170         if (flag == LDLM_CB_CANCELING) {
171                 /* Don't need to do anything here. */
172                 RETURN(0);
173         }
174
175         lock_res_and_lock(lock);
176         /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
177          * that ldlm_blocking_ast is called just before intent_policy method
178          * takes the ns_lock, then by the time we get the lock, we might not
179          * be the correct blocking function anymore.  So check, and return
180          * early, if so. */
181         if (lock->l_blocking_ast != ldlm_blocking_ast) {
182                 unlock_res_and_lock(lock);
183                 RETURN(0);
184         }
185
186         lock->l_flags |= LDLM_FL_CBPENDING;
187         do_ast = (!lock->l_readers && !lock->l_writers);
188         unlock_res_and_lock(lock);
189
190         if (do_ast) {
191                 struct lustre_handle lockh;
192                 int rc;
193
194                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
195                 ldlm_lock2handle(lock, &lockh);
196                 rc = ldlm_cli_cancel(&lockh);
197                 if (rc < 0)
198                         CERROR("ldlm_cli_cancel: %d\n", rc);
199         } else {
200                 LDLM_DEBUG(lock, "Lock still has references, will be "
201                            "cancelled later");
202         }
203         RETURN(0);
204 }
205
206 /*
207  * ->l_glimpse_ast() for DLM extent locks acquired on the server-side. See
208  * comment in filter_intent_policy() on why you may need this.
209  */
210 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
211 {
212         /*
213          * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for
214          * that is rather subtle: with OST-side locking, it may so happen that
215          * _all_ extent locks are held by the OST. If client wants to obtain
216          * current file size it calls ll{,u}_glimpse_size(), and (as locks are
217          * on the server), dummy glimpse callback fires and does
218          * nothing. Client still receives correct file size due to the
219          * following fragment in filter_intent_policy():
220          *
221          * rc = l->l_glimpse_ast(l, NULL); // this will update the LVB
222          * if (rc != 0 && res->lr_namespace->ns_lvbo &&
223          *     res->lr_namespace->ns_lvbo->lvbo_update) {
224          *         res->lr_namespace->ns_lvbo->lvbo_update(res, NULL, 0, 1);
225          * }
226          *
227          * that is, after glimpse_ast() fails, filter_lvbo_update() runs, and
228          * returns correct file size to the client.
229          */
230         return -ELDLM_NO_LOCK_DATA;
231 }
232
233 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
234                            const struct ldlm_res_id *res_id,
235                            ldlm_type_t type, ldlm_policy_data_t *policy,
236                            ldlm_mode_t mode, int *flags,
237                            ldlm_blocking_callback blocking,
238                            ldlm_completion_callback completion,
239                            ldlm_glimpse_callback glimpse,
240                            void *data, __u32 lvb_len, void *lvb_swabber,
241                            struct lustre_handle *lockh)
242 {
243         struct ldlm_lock *lock;
244         int err;
245         ENTRY;
246
247         LASSERT(!(*flags & LDLM_FL_REPLAY));
248         if (unlikely(ns_is_client(ns))) {
249                 CERROR("Trying to enqueue local lock in a shadow namespace\n");
250                 LBUG();
251         }
252
253         lock = ldlm_lock_create(ns, res_id, type, mode, blocking,
254                                 completion, glimpse, data, lvb_len);
255         if (unlikely(!lock))
256                 GOTO(out_nolock, err = -ENOMEM);
257         LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
258
259         ldlm_lock_addref_internal(lock, mode);
260         ldlm_lock2handle(lock, lockh);
261         lock_res_and_lock(lock);
262         lock->l_flags |= LDLM_FL_LOCAL;
263         if (*flags & LDLM_FL_ATOMIC_CB)
264                 lock->l_flags |= LDLM_FL_ATOMIC_CB;
265         lock->l_lvb_swabber = lvb_swabber;
266         unlock_res_and_lock(lock);
267         if (policy != NULL)
268                 lock->l_policy_data = *policy;
269         if (type == LDLM_EXTENT)
270                 lock->l_req_extent = policy->l_extent;
271
272         err = ldlm_lock_enqueue(ns, &lock, policy, flags);
273         if (unlikely(err != ELDLM_OK))
274                 GOTO(out, err);
275
276         if (policy != NULL)
277                 *policy = lock->l_policy_data;
278
279         LDLM_DEBUG_NOLOCK("client-side local enqueue handler END (lock %p)",
280                           lock);
281
282         if (lock->l_completion_ast)
283                 lock->l_completion_ast(lock, *flags, NULL);
284
285         LDLM_DEBUG(lock, "client-side local enqueue END");
286         EXIT;
287  out:
288         LDLM_LOCK_PUT(lock);
289  out_nolock:
290         return err;
291 }
292
293 static void failed_lock_cleanup(struct ldlm_namespace *ns,
294                                 struct ldlm_lock *lock,
295                                 struct lustre_handle *lockh, int mode)
296 {
297         /* Set a flag to prevent us from sending a CANCEL (bug 407) */
298         lock_res_and_lock(lock);
299         lock->l_flags |= LDLM_FL_LOCAL_ONLY;
300         unlock_res_and_lock(lock);
301         LDLM_DEBUG(lock, "setting FL_LOCAL_ONLY");
302
303         ldlm_lock_decref_and_cancel(lockh, mode);
304
305         /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
306          *       from llite/file.c/ll_file_flock(). */
307         if (lock->l_resource->lr_type == LDLM_FLOCK) {
308                 ldlm_lock_destroy(lock);
309         }
310 }
311
312 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
313                           ldlm_type_t type, __u8 with_policy, ldlm_mode_t mode,
314                           int *flags, void *lvb, __u32 lvb_len,
315                           void *lvb_swabber, struct lustre_handle *lockh,int rc)
316 {
317         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
318         int is_replay = *flags & LDLM_FL_REPLAY;
319         struct ldlm_lock *lock;
320         struct ldlm_reply *reply;
321         int cleanup_phase = 1;
322         ENTRY;
323
324         lock = ldlm_handle2lock(lockh);
325         /* ldlm_cli_enqueue is holding a reference on this lock. */
326         if (!lock) {
327                 LASSERT(type == LDLM_FLOCK);
328                 RETURN(-ENOLCK);
329         }
330
331         if (rc != ELDLM_OK) {
332                 LASSERT(!is_replay);
333                 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
334                            rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
335                 if (rc == ELDLM_LOCK_ABORTED) {
336                         /* Before we return, swab the reply */
337                         reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF,
338                                                    sizeof(*reply),
339                                                    lustre_swab_ldlm_reply);
340                         if (reply == NULL) {
341                                 CERROR("Can't unpack ldlm_reply\n");
342                                 rc = -EPROTO;
343                         }
344                         if (lvb_len) {
345                                 void *tmplvb;
346                                 tmplvb = lustre_swab_repbuf(req,
347                                                             DLM_REPLY_REC_OFF,
348                                                             lvb_len,
349                                                             lvb_swabber);
350                                 if (tmplvb == NULL)
351                                         GOTO(cleanup, rc = -EPROTO);
352                                 if (lvb != NULL)
353                                         memcpy(lvb, tmplvb, lvb_len);
354                         }
355                 }
356                 GOTO(cleanup, rc);
357         }
358
359         reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF, sizeof(*reply),
360                                    lustre_swab_ldlm_reply);
361         if (reply == NULL) {
362                 CERROR("Can't unpack ldlm_reply\n");
363                 GOTO(cleanup, rc = -EPROTO);
364         }
365
366         /* lock enqueued on the server */
367         cleanup_phase = 0;
368
369         lock_res_and_lock(lock);
370         lock->l_remote_handle = reply->lock_handle;
371         *flags = reply->lock_flags;
372         lock->l_flags |= reply->lock_flags & LDLM_INHERIT_FLAGS;
373         /* move NO_TIMEOUT flag to the lock to force ldlm_lock_match()
374          * to wait with no timeout as well */
375         lock->l_flags |= reply->lock_flags & LDLM_FL_NO_TIMEOUT;
376         unlock_res_and_lock(lock);
377
378         CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: 0x%x\n",
379                lock, reply->lock_handle.cookie, *flags);
380
381         /* If enqueue returned a blocked lock but the completion handler has
382          * already run, then it fixed up the resource and we don't need to do it
383          * again. */
384         if ((*flags) & LDLM_FL_LOCK_CHANGED) {
385                 int newmode = reply->lock_desc.l_req_mode;
386                 LASSERT(!is_replay);
387                 if (newmode && newmode != lock->l_req_mode) {
388                         LDLM_DEBUG(lock, "server returned different mode %s",
389                                    ldlm_lockname[newmode]);
390                         lock->l_req_mode = newmode;
391                 }
392
393                 if (memcmp(reply->lock_desc.l_resource.lr_name.name,
394                           lock->l_resource->lr_name.name,
395                           sizeof(struct ldlm_res_id))) {
396                         CDEBUG(D_INFO, "remote intent success, locking "
397                                         "(%ld,%ld,%ld) instead of "
398                                         "(%ld,%ld,%ld)\n",
399                               (long)reply->lock_desc.l_resource.lr_name.name[0],
400                               (long)reply->lock_desc.l_resource.lr_name.name[1],
401                               (long)reply->lock_desc.l_resource.lr_name.name[2],
402                               (long)lock->l_resource->lr_name.name[0],
403                               (long)lock->l_resource->lr_name.name[1],
404                               (long)lock->l_resource->lr_name.name[2]);
405
406                         ldlm_lock_change_resource(ns, lock,
407                                           &reply->lock_desc.l_resource.lr_name);
408                         if (lock->l_resource == NULL) {
409                                 LBUG();
410                                 GOTO(cleanup, rc = -ENOMEM);
411                         }
412                         LDLM_DEBUG(lock, "client-side enqueue, new resource");
413                 }
414                 if (with_policy)
415                         if (!(type == LDLM_IBITS && !(exp->exp_connect_flags &
416                                                     OBD_CONNECT_IBITS)))
417                                 lock->l_policy_data =
418                                                  reply->lock_desc.l_policy_data;
419                 if (type != LDLM_PLAIN)
420                         LDLM_DEBUG(lock,"client-side enqueue, new policy data");
421         }
422
423         if ((*flags) & LDLM_FL_AST_SENT ||
424             /* Cancel extent locks as soon as possible on a liblustre client,
425              * because it cannot handle asynchronous ASTs robustly (see
426              * bug 7311). */
427             (LIBLUSTRE_CLIENT && type == LDLM_EXTENT)) {
428                 lock_res_and_lock(lock);
429                 lock->l_flags |= LDLM_FL_CBPENDING |  LDLM_FL_BL_AST;
430                 unlock_res_and_lock(lock);
431                 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
432         }
433
434         /* If the lock has already been granted by a completion AST, don't
435          * clobber the LVB with an older one. */
436         if (lvb_len && (lock->l_req_mode != lock->l_granted_mode)) {
437                 void *tmplvb;
438                 tmplvb = lustre_swab_repbuf(req, DLM_REPLY_REC_OFF, lvb_len,
439                                             lvb_swabber);
440                 if (tmplvb == NULL)
441                         GOTO(cleanup, rc = -EPROTO);
442                 memcpy(lock->l_lvb_data, tmplvb, lvb_len);
443         }
444
445         if (!is_replay) {
446                 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
447                 if (lock->l_completion_ast != NULL) {
448                         int err = lock->l_completion_ast(lock, *flags, NULL);
449                         if (!rc)
450                                 rc = err;
451                         if (rc && type != LDLM_FLOCK) /* bug 9425, bug 10250 */
452                                 cleanup_phase = 1;
453                 }
454         }
455
456         if (lvb_len && lvb != NULL) {
457                 /* Copy the LVB here, and not earlier, because the completion
458                  * AST (if any) can override what we got in the reply */
459                 memcpy(lvb, lock->l_lvb_data, lvb_len);
460         }
461
462         LDLM_DEBUG(lock, "client-side enqueue END");
463         EXIT;
464 cleanup:
465         if (cleanup_phase == 1 && rc)
466                 failed_lock_cleanup(ns, lock, lockh, mode);
467         /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
468         LDLM_LOCK_PUT(lock);
469         LDLM_LOCK_PUT(lock);
470         return rc;
471 }
472
473 /* PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
474  * a single page on the send/receive side. XXX: 512 should be changed
475  * to more adequate value. */
476 static inline int ldlm_req_handles_avail(struct obd_export *exp,
477                                          int *size, int bufcount, int off)
478 {
479         int avail = min_t(int, LDLM_MAXREQSIZE, PAGE_SIZE - 512);
480         int old_size = size[DLM_LOCKREQ_OFF];
481
482         size[DLM_LOCKREQ_OFF] = sizeof(struct ldlm_request);
483         avail -= lustre_msg_size(class_exp2cliimp(exp)->imp_msg_magic,
484                                  bufcount, size);
485         avail /= sizeof(struct lustre_handle);
486         avail += LDLM_LOCKREQ_HANDLES - off;
487         size[DLM_LOCKREQ_OFF] = old_size;
488
489         return avail;
490 }
491
492 static inline int ldlm_cancel_handles_avail(struct obd_export *exp)
493 {
494         int size[2] = { sizeof(struct ptlrpc_body),
495                         sizeof(struct ldlm_request) };
496         return ldlm_req_handles_avail(exp, size, 2, 0);
497 }
498
499 /* Cancel lru locks and pack them into the enqueue request. Pack there the given
500  * @count locks in @cancels. */
501 struct ptlrpc_request *ldlm_prep_enqueue_req(struct obd_export *exp,
502                                              int bufcount, int *size,
503                                              struct list_head *cancels,
504                                              int count)
505 {
506         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
507         struct ldlm_request *dlm = NULL;
508         struct ptlrpc_request *req;
509         CFS_LIST_HEAD(head);
510         ENTRY;
511
512         if (cancels == NULL)
513                 cancels = &head;
514         if (exp_connect_cancelset(exp)) {
515                 /* Estimate the amount of available space in the request. */
516                 int avail = ldlm_req_handles_avail(exp, size, bufcount,
517                                                    LDLM_ENQUEUE_CANCEL_OFF);
518                 int flags, cancel;
519
520                 LASSERT(avail >= count);
521
522                 flags = ns_connect_lru_resize(ns) ? 
523                         LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
524                 cancel = ns_connect_lru_resize(ns) ? 0 : 1;
525
526                 /* Cancel lru locks here _only_ if the server supports 
527                  * EARLY_CANCEL. Otherwise we have to send extra CANCEL
528                  * rpc right on enqueue, what will make it slower, vs. 
529                  * asynchronous rpc in blocking thread. */
530                 count += ldlm_cancel_lru_local(ns, cancels, cancel,
531                                                avail - count, flags);
532                 size[DLM_LOCKREQ_OFF] =
533                         ldlm_request_bufsize(count, LDLM_ENQUEUE);
534         }
535         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_DLM_VERSION,
536                               LDLM_ENQUEUE, bufcount, size, NULL);
537         if (exp_connect_cancelset(exp) && req) {
538                 dlm = lustre_msg_buf(req->rq_reqmsg,
539                                      DLM_LOCKREQ_OFF, sizeof(*dlm));
540                 /* Skip first lock handler in ldlm_request_pack(), this method
541                  * will incrment @lock_count according to the lock handle amount
542                  * actually written to the buffer. */
543                 dlm->lock_count = LDLM_ENQUEUE_CANCEL_OFF;
544                 ldlm_cli_cancel_list(cancels, count, req, DLM_LOCKREQ_OFF, 0);
545         } else {
546                 ldlm_lock_list_put(cancels, l_bl_ast, count);
547         }
548         RETURN(req);
549 }
550
551 /* If a request has some specific initialisation it is passed in @reqp,
552  * otherwise it is created in ldlm_cli_enqueue.
553  *
554  * Supports sync and async requests, pass @async flag accordingly. If a
555  * request was created in ldlm_cli_enqueue and it is the async request,
556  * pass it to the caller in @reqp. */
557 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
558                      struct ldlm_enqueue_info *einfo,
559                      const struct ldlm_res_id *res_id,
560                      ldlm_policy_data_t *policy, int *flags,
561                      void *lvb, __u32 lvb_len, void *lvb_swabber,
562                      struct lustre_handle *lockh, int async)
563 {
564         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
565         struct ldlm_lock *lock;
566         struct ldlm_request *body;
567         struct ldlm_reply *reply;
568         int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
569                         [DLM_LOCKREQ_OFF]     = sizeof(*body),
570                         [DLM_REPLY_REC_OFF]   = lvb_len };
571         int is_replay = *flags & LDLM_FL_REPLAY;
572         int req_passed_in = 1, rc, err;
573         struct ptlrpc_request *req;
574         ENTRY;
575
576         LASSERT(exp != NULL);
577
578         /* If we're replaying this lock, just check some invariants.
579          * If we're creating a new lock, get everything all setup nice. */
580         if (is_replay) {
581                 lock = ldlm_handle2lock(lockh);
582                 LASSERT(lock != NULL);
583                 LDLM_DEBUG(lock, "client-side enqueue START");
584                 LASSERT(exp == lock->l_conn_export);
585         } else {
586                 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
587                                         einfo->ei_mode, einfo->ei_cb_bl,
588                                         einfo->ei_cb_cp, einfo->ei_cb_gl,
589                                         einfo->ei_cbdata, lvb_len);
590                 if (lock == NULL)
591                         RETURN(-ENOMEM);
592                 /* for the local lock, add the reference */
593                 ldlm_lock_addref_internal(lock, einfo->ei_mode);
594                 ldlm_lock2handle(lock, lockh);
595                 lock->l_lvb_swabber = lvb_swabber;
596                 if (policy != NULL) {
597                         /* INODEBITS_INTEROP: If the server does not support
598                          * inodebits, we will request a plain lock in the
599                          * descriptor (ldlm_lock2desc() below) but use an
600                          * inodebits lock internally with both bits set.
601                          */
602                         if (einfo->ei_type == LDLM_IBITS &&
603                             !(exp->exp_connect_flags & OBD_CONNECT_IBITS))
604                                 lock->l_policy_data.l_inodebits.bits =
605                                         MDS_INODELOCK_LOOKUP |
606                                         MDS_INODELOCK_UPDATE;
607                         else
608                                 lock->l_policy_data = *policy;
609                 }
610
611                 if (einfo->ei_type == LDLM_EXTENT)
612                         lock->l_req_extent = policy->l_extent;
613                 LDLM_DEBUG(lock, "client-side enqueue START");
614         }
615
616         /* lock not sent to server yet */
617
618         if (reqp == NULL || *reqp == NULL) {
619                 req = ldlm_prep_enqueue_req(exp, 2, size, NULL, 0);
620                 if (req == NULL) {
621                         failed_lock_cleanup(ns, lock, lockh, einfo->ei_mode);
622                         LDLM_LOCK_PUT(lock);
623                         RETURN(-ENOMEM);
624                 }
625                 req_passed_in = 0;
626                 if (reqp)
627                         *reqp = req;
628         } else {
629                 req = *reqp;
630                 LASSERTF(lustre_msg_buflen(req->rq_reqmsg, DLM_LOCKREQ_OFF) >=
631                          sizeof(*body), "buflen[%d] = %d, not "LPSZ"\n",
632                          DLM_LOCKREQ_OFF,
633                          lustre_msg_buflen(req->rq_reqmsg, DLM_LOCKREQ_OFF),
634                          sizeof(*body));
635         }
636
637         lock->l_conn_export = exp;
638         lock->l_export = NULL;
639         lock->l_blocking_ast = einfo->ei_cb_bl;
640
641         /* Dump lock data into the request buffer */
642         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
643         ldlm_lock2desc(lock, &body->lock_desc);
644         body->lock_flags = *flags;
645         body->lock_handle[0] = *lockh;
646
647         /* Continue as normal. */
648         if (!req_passed_in) {
649                 size[DLM_LOCKREPLY_OFF] = sizeof(*reply);
650                 ptlrpc_req_set_repsize(req, 2 + (lvb_len > 0), size);
651         }
652
653         /*
654          * Liblustre client doesn't get extent locks, except for O_APPEND case
655          * where [0, OBD_OBJECT_EOF] lock is taken, or truncate, where
656          * [i_size, OBD_OBJECT_EOF] lock is taken.
657          */
658         LASSERT(ergo(LIBLUSTRE_CLIENT, einfo->ei_type != LDLM_EXTENT ||
659                      policy->l_extent.end == OBD_OBJECT_EOF));
660
661         if (async) {
662                 LASSERT(reqp != NULL);
663                 RETURN(0);
664         }
665
666         LDLM_DEBUG(lock, "sending request");
667         rc = ptlrpc_queue_wait(req);
668         err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
669                                     einfo->ei_mode, flags, lvb, lvb_len,
670                                     lvb_swabber, lockh, rc);
671
672         /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
673          * one reference that we took */
674         if (err == -ENOLCK)
675                 LDLM_LOCK_PUT(lock);
676         else
677                 rc = err;
678
679         if (!req_passed_in && req != NULL) {
680                 ptlrpc_req_finished(req);
681                 if (reqp)
682                         *reqp = NULL;
683         }
684
685         RETURN(rc);
686 }
687
688 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
689                                   int *flags)
690 {
691         struct ldlm_resource *res;
692         int rc;
693         ENTRY;
694         if (ns_is_client(lock->l_resource->lr_namespace)) {
695                 CERROR("Trying to cancel local lock\n");
696                 LBUG();
697         }
698         LDLM_DEBUG(lock, "client-side local convert");
699
700         res = ldlm_lock_convert(lock, new_mode, flags);
701         if (res) {
702                 ldlm_reprocess_all(res);
703                 rc = 0;
704         } else {
705                 rc = EDEADLOCK;
706         }
707         LDLM_DEBUG(lock, "client-side local convert handler END");
708         LDLM_LOCK_PUT(lock);
709         RETURN(rc);
710 }
711
712 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
713  * conversion of locks which are on the waiting or converting queue */
714 /* Caller of this code is supposed to take care of lock readers/writers
715    accounting */
716 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, int *flags)
717 {
718         struct ldlm_request *body;
719         struct ldlm_reply *reply;
720         struct ldlm_lock *lock;
721         struct ldlm_resource *res;
722         struct ptlrpc_request *req;
723         int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
724                         [DLM_LOCKREQ_OFF]     = sizeof(*body) };
725         int rc;
726         ENTRY;
727
728         lock = ldlm_handle2lock(lockh);
729         if (!lock) {
730                 LBUG();
731                 RETURN(-EINVAL);
732         }
733         *flags = 0;
734
735         if (lock->l_conn_export == NULL)
736                 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
737
738         LDLM_DEBUG(lock, "client-side convert");
739
740         req = ptlrpc_prep_req(class_exp2cliimp(lock->l_conn_export),
741                               LUSTRE_DLM_VERSION, LDLM_CONVERT, 2, size, NULL);
742         if (!req)
743                 GOTO(out, rc = -ENOMEM);
744
745         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
746         body->lock_handle[0] = lock->l_remote_handle;
747
748         body->lock_desc.l_req_mode = new_mode;
749         body->lock_flags = *flags;
750
751         size[DLM_LOCKREPLY_OFF] = sizeof(*reply);
752         ptlrpc_req_set_repsize(req, 2, size);
753
754         rc = ptlrpc_queue_wait(req);
755         if (rc != ELDLM_OK)
756                 GOTO(out, rc);
757
758         reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF, sizeof(*reply),
759                                    lustre_swab_ldlm_reply);
760         if (reply == NULL) {
761                 CERROR ("Can't unpack ldlm_reply\n");
762                 GOTO (out, rc = -EPROTO);
763         }
764
765         if (req->rq_status)
766                 GOTO(out, rc = req->rq_status);
767
768         res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
769         if (res != NULL) {
770                 ldlm_reprocess_all(res);
771                 /* Go to sleep until the lock is granted. */
772                 /* FIXME: or cancelled. */
773                 if (lock->l_completion_ast) {
774                         rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
775                                                     NULL);
776                         if (rc)
777                                 GOTO(out, rc);
778                 }
779         } else {
780                 rc = EDEADLOCK;
781         }
782         EXIT;
783  out:
784         LDLM_LOCK_PUT(lock);
785         ptlrpc_req_finished(req);
786         return rc;
787 }
788
789 /* Cancel locks locally.
790  * Returns:
791  * LDLM_FL_LOCAL_ONLY if tere is no need in a CANCEL rpc to the server;
792  * LDLM_FL_CANCELING otherwise;
793  * LDLM_FL_BL_AST if there is a need in a separate CANCEL rpc. */
794 static int ldlm_cli_cancel_local(struct ldlm_lock *lock)
795 {
796         int rc = LDLM_FL_LOCAL_ONLY;
797         ENTRY;
798         
799         if (lock->l_conn_export) {
800                 int local_only;
801
802                 LDLM_DEBUG(lock, "client-side cancel");
803                 /* Set this flag to prevent others from getting new references*/
804                 lock_res_and_lock(lock);
805                 lock->l_flags |= LDLM_FL_CBPENDING;
806                 local_only = (lock->l_flags &
807                               (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
808                 ldlm_cancel_callback(lock);
809                 rc = (lock->l_flags & LDLM_FL_BL_AST) ?
810                         LDLM_FL_BL_AST : LDLM_FL_CANCELING;
811                 unlock_res_and_lock(lock);
812
813                 if (local_only) {
814                         CDEBUG(D_DLMTRACE, "not sending request (at caller's "
815                                "instruction)\n");
816                         rc = LDLM_FL_LOCAL_ONLY;
817                 }
818                 ldlm_lock_cancel(lock);
819         } else {
820                 if (ns_is_client(lock->l_resource->lr_namespace)) {
821                         LDLM_ERROR(lock, "Trying to cancel local lock");
822                         LBUG();
823                 }
824                 LDLM_DEBUG(lock, "server-side local cancel");
825                 ldlm_lock_cancel(lock);
826                 ldlm_reprocess_all(lock->l_resource);
827                 LDLM_DEBUG(lock, "server-side local cancel handler END");
828         }
829
830         RETURN(rc);
831 }
832
833 /* Pack @count locks in @head into ldlm_request buffer at the offset @off,
834    of the request @req. */
835 static void ldlm_cancel_pack(struct ptlrpc_request *req, int off,
836                              struct list_head *head, int count)
837 {
838         struct ldlm_request *dlm;
839         struct ldlm_lock *lock;
840         int max, packed = 0;
841         ENTRY;
842
843         dlm = lustre_msg_buf(req->rq_reqmsg, off, sizeof(*dlm));
844         LASSERT(dlm != NULL);
845
846         /* Check the room in the request buffer. */
847         max = lustre_msg_buflen(req->rq_reqmsg, off) - 
848                 sizeof(struct ldlm_request);
849         max /= sizeof(struct lustre_handle);
850         max += LDLM_LOCKREQ_HANDLES;
851         LASSERT(max >= dlm->lock_count + count);
852
853         /* XXX: it would be better to pack lock handles grouped by resource.
854          * so that the server cancel would call filter_lvbo_update() less
855          * frequently. */
856         list_for_each_entry(lock, head, l_bl_ast) {
857                 if (!count--)
858                         break;
859                 LASSERT(lock->l_conn_export);
860                 /* Pack the lock handle to the given request buffer. */
861                 LDLM_DEBUG(lock, "packing");
862                 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
863                 packed++;
864         }
865         CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
866         EXIT;
867 }
868
869 /* Prepare and send a batched cancel rpc, it will include count lock handles
870  * of locks given in @head. */
871 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
872                         int count, int flags)
873 {
874         struct ptlrpc_request *req = NULL;
875         struct ldlm_request *body;
876         int size[2] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
877                 [DLM_LOCKREQ_OFF]     = sizeof(*body) };
878         struct obd_import *imp;
879         int free, sent = 0;
880         int rc = 0;
881         ENTRY;
882
883         LASSERT(exp != NULL);
884         LASSERT(count > 0);
885
886         if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
887                 RETURN(count);
888
889         free = ldlm_req_handles_avail(exp, size, 2, 0);
890         if (count > free)
891                 count = free;
892
893         size[DLM_LOCKREQ_OFF] = ldlm_request_bufsize(count, LDLM_CANCEL);
894         while (1) {
895                 imp = class_exp2cliimp(exp);
896                 if (imp == NULL || imp->imp_invalid) {
897                         CDEBUG(D_DLMTRACE,
898                                "skipping cancel on invalid import %p\n", imp);
899                         RETURN(count);
900                 }
901
902                 req = ptlrpc_prep_req(imp, LUSTRE_DLM_VERSION, LDLM_CANCEL, 2,
903                                       size, NULL);
904                 if (!req)
905                         GOTO(out, rc = -ENOMEM);
906
907                 req->rq_no_resend = 1;
908                 req->rq_no_delay = 1;
909
910                 /* XXX FIXME bug 249 */
911                 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
912                 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
913
914                 body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF,
915                                       sizeof(*body));
916                 ldlm_cancel_pack(req, DLM_LOCKREQ_OFF, cancels, count);
917
918                 ptlrpc_req_set_repsize(req, 1, NULL);
919                 if (flags & LDLM_FL_ASYNC) {
920                         ptlrpcd_add_req(req);
921                         sent = count;
922                         GOTO(out, 0);
923                 } else {
924                         rc = ptlrpc_queue_wait(req);
925                 }
926                 if (rc == ESTALE) {
927                         CDEBUG(D_DLMTRACE, "client/server (nid %s) "
928                                "out of sync -- not fatal\n",
929                                libcfs_nid2str(req->rq_import->
930                                               imp_connection->c_peer.nid));
931                         rc = 0;
932                 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
933                            req->rq_import_generation == imp->imp_generation) {
934                         ptlrpc_req_finished(req);
935                         continue;
936                 } else if (rc != ELDLM_OK) {
937                         CERROR("Got rc %d from cancel RPC: canceling "
938                                "anyway\n", rc);
939                         break;
940                 }
941                 sent = count;
942                 break;
943         }
944
945         ptlrpc_req_finished(req);
946         EXIT;
947 out:
948         return sent ? sent : rc;
949 }
950
951 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
952 {
953         LASSERT(imp != NULL);
954         return &imp->imp_obd->obd_namespace->ns_pool;
955 }
956
957 int ldlm_cli_update_pool(struct ptlrpc_request *req)
958 {
959         struct ldlm_pool *pl;
960         ENTRY;
961     
962         if (!imp_connect_lru_resize(req->rq_import))
963                 RETURN(0);
964
965         if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
966             lustre_msg_get_limit(req->rq_repmsg) == 0)
967                 RETURN(0);
968
969         pl = ldlm_imp2pl(req->rq_import);
970         
971         spin_lock(&pl->pl_lock);
972
973         /* Check if we need to wakeup pools thread for fast SLV change. 
974          * This is only done when threads period is noticably long like 
975          * 10s or more. */
976 #if defined(__KERNEL__) && (LDLM_POOLS_THREAD_PERIOD >= 10)
977         {
978                 __u64 old_slv, new_slv, fast_change;
979
980                 old_slv = ldlm_pool_get_slv(pl);
981                 new_slv = lustre_msg_get_slv(req->rq_repmsg);
982                 fast_change = old_slv * LDLM_POOLS_FAST_SLV_CHANGE;
983                 do_div(fast_change, 100);
984
985                 /* Wake up pools thread only if SLV has changed more than 
986                  * 50% since last update. In this case we want to react asap. 
987                  * Otherwise it is no sense to wake up pools as they are 
988                  * re-calculated every LDLM_POOLS_THREAD_PERIOD anyways. */
989                 if (old_slv > new_slv && old_slv - new_slv > fast_change)
990                         ldlm_pools_wakeup();
991         }
992 #endif
993         /* In some cases RPC may contain slv and limit zeroed out. This is 
994          * the case when server does not support lru resize feature. This is
995          * also possible in some recovery cases when server side reqs have no
996          * ref to obd export and thus access to server side namespace is no 
997          * possible. */
998         if (lustre_msg_get_slv(req->rq_repmsg) != 0 && 
999             lustre_msg_get_limit(req->rq_repmsg) != 0) {
1000                 ldlm_pool_set_slv(pl, lustre_msg_get_slv(req->rq_repmsg));
1001                 ldlm_pool_set_limit(pl, lustre_msg_get_limit(req->rq_repmsg));
1002         } else {
1003                 DEBUG_REQ(D_HA, req, "zero SLV or Limit found "
1004                           "(SLV: "LPU64", Limit: %u)", 
1005                           lustre_msg_get_slv(req->rq_repmsg), 
1006                           lustre_msg_get_limit(req->rq_repmsg));
1007         }
1008         spin_unlock(&pl->pl_lock);
1009
1010         RETURN(0);
1011 }
1012 EXPORT_SYMBOL(ldlm_cli_update_pool);
1013
1014 int ldlm_cli_cancel(struct lustre_handle *lockh)
1015 {
1016         struct ldlm_lock *lock;
1017         CFS_LIST_HEAD(cancels);
1018         int rc = 0;
1019         ENTRY;
1020
1021         /* concurrent cancels on the same handle can happen */
1022         lock = __ldlm_handle2lock(lockh, LDLM_FL_CANCELING);
1023         if (lock == NULL) {
1024                 LDLM_DEBUG_NOLOCK("lock is already being destroyed\n");
1025                 RETURN(0);
1026         }
1027
1028         rc = ldlm_cli_cancel_local(lock);
1029         list_add(&lock->l_bl_ast, &cancels);
1030
1031         if (rc == LDLM_FL_BL_AST) {
1032                 rc = ldlm_cli_cancel_req(lock->l_conn_export, &cancels, 1, 0);
1033         } else if (rc == LDLM_FL_CANCELING) {
1034                 struct ldlm_namespace *ns = lock->l_resource->lr_namespace;
1035                 int avail = ldlm_cancel_handles_avail(lock->l_conn_export);
1036                 int flags, cancel;
1037                 LASSERT(avail > 0);
1038                 
1039                 flags = ns_connect_lru_resize(ns) ? 
1040                         LDLM_CANCEL_LRUR : LDLM_CANCEL_AGED;
1041                 cancel = ns_connect_lru_resize(ns) ? 0 : 1;
1042                 
1043                 cancel += ldlm_cancel_lru_local(ns, &cancels, 0, 
1044                                                 avail - cancel, flags);
1045                 ldlm_cli_cancel_list(&cancels, cancel, NULL, 0, 0);
1046         }
1047         if (rc != LDLM_FL_CANCELING)
1048                 LDLM_LOCK_PUT(lock);
1049         RETURN(rc < 0 ? rc : 0);
1050 }
1051
1052 /* XXX until we will have compound requests and can cut cancels from generic rpc
1053  * we need send cancels with LDLM_FL_BL_AST flag as separate rpc */
1054 static int ldlm_cancel_list(struct list_head *cancels, int count)
1055 {
1056         CFS_LIST_HEAD(head);
1057         struct ldlm_lock *lock, *next;
1058         int left = 0, bl_ast = 0, rc;
1059
1060         left = count;
1061         list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1062                 if (left-- == 0)
1063                         break;
1064
1065                 rc = ldlm_cli_cancel_local(lock);
1066                 if (rc == LDLM_FL_BL_AST) {
1067                         LDLM_DEBUG(lock, "Cancel lock separately");
1068                         list_del_init(&lock->l_bl_ast);
1069                         list_add(&lock->l_bl_ast, &head);
1070                         bl_ast ++;
1071                         continue;
1072                 }
1073                 if (rc == LDLM_FL_LOCAL_ONLY) {
1074                         /* CANCEL RPC should not be sent to server. */
1075                         list_del_init(&lock->l_bl_ast);
1076                         LDLM_LOCK_PUT(lock);
1077                         count--;
1078                 }
1079
1080         }
1081         if(bl_ast > 0) {
1082                 count -= bl_ast;
1083                 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0, 0);
1084         }
1085
1086         RETURN(count);
1087 }
1088
1089
1090 /* cancel lock list without sending rpc to server*/
1091 static int ldlm_cancel_list_local(struct list_head *cancels, int count)
1092 {
1093         struct ldlm_lock *lock, *next;
1094         int left = 0;
1095
1096         left = count;
1097         list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1098                 if (left-- == 0)
1099                         break;
1100                 ldlm_lock_cancel(lock);
1101                 /* CANCEL RPC should not be sent to server. */
1102                 list_del_init(&lock->l_bl_ast);
1103                 LDLM_LOCK_PUT(lock);
1104                 count--;
1105         }
1106         RETURN(count);
1107 }
1108
1109 /* Return 1 if @lock should be canceled according to shrinker policy. 
1110  * Return zero otherwise. */
1111 static int ldlm_cancel_shrink_policy(struct ldlm_namespace *ns,
1112                                      struct ldlm_lock *lock,
1113                                      int unused, int added, 
1114                                      int asked)
1115 {
1116         int lock_cost;
1117         __u64 page_nr;
1118
1119         if (lock->l_resource->lr_type == LDLM_EXTENT) {
1120                 struct ldlm_extent *l_extent;
1121
1122                 /* For all extent locks cost is 1 + number of pages in
1123                  * their extent. */
1124                 l_extent = &lock->l_policy_data.l_extent;
1125                 page_nr = (l_extent->end - l_extent->start);
1126                 do_div(page_nr, CFS_PAGE_SIZE);
1127
1128 #ifdef __KERNEL__
1129                 /* XXX: In fact this is evil hack, we can't access inode
1130                  * here. For doing it right we need somehow to have number
1131                  * of covered by lock. This should be fixed later when 10718 
1132                  * is landed. */
1133                 if (lock->l_ast_data != NULL) {
1134                         struct inode *inode = lock->l_ast_data;
1135                         if (page_nr > inode->i_mapping->nrpages)
1136                                 page_nr = inode->i_mapping->nrpages;
1137                 }
1138 #endif
1139                 lock_cost = 1 + page_nr;
1140         } else {
1141                 /* For all locks which are not extent ones cost is 1 */
1142                 lock_cost = 1;
1143         }
1144
1145         /* Keep all expensive locks in lru for the memory pressure time
1146          * cancel policy. They anyways may be canceled by lru resize
1147          * pplicy if they have not small enough CLV. */
1148         return (lock_cost <= ns->ns_shrink_thumb);
1149 }
1150
1151 /* Return 1 if @lock should be canceled according to lru resize policy. 
1152  * Return zero otherwise. */
1153 static int ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1154                                    struct ldlm_lock *lock, 
1155                                    int unused, int added, 
1156                                    int asked)
1157 {
1158         cfs_time_t cur = cfs_time_current();
1159         struct ldlm_pool *pl = &ns->ns_pool;
1160         __u64 slv, lvf, lv;
1161         cfs_time_t la;
1162
1163         spin_lock(&pl->pl_lock);
1164         slv = ldlm_pool_get_slv(pl);
1165         lvf = atomic_read(&pl->pl_lock_volume_factor);
1166         spin_unlock(&pl->pl_lock);
1167
1168         la = cfs_duration_sec(cfs_time_sub(cur, 
1169                               lock->l_last_used));
1170
1171         /* Stop when slv is not yet come from server or 
1172          * lv is smaller than it is. */
1173         lv = lvf * la * unused;
1174         return (slv > 1 && lv >= slv);
1175 }
1176
1177 /* Return 1 if @lock should be canceled according to passed policy. 
1178  * Return zero otherwise. */
1179 static int ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1180                                      struct ldlm_lock *lock, 
1181                                      int unused, int added,
1182                                      int asked)
1183 {
1184         /* Do nothing here, we allow canceling all locks which
1185          * are passed here from upper layer logic. So that locks
1186          * number to be canceled will be limited by @count and
1187          * @max in ldlm_cancel_lru_local(). */
1188         return 1;
1189 }
1190
1191 /* Return 1 if @lock should be canceled according to aged policy. 
1192  * Return zero otherwise. */
1193 static int ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1194                                    struct ldlm_lock *lock, 
1195                                    int unused, int added,
1196                                    int asked)
1197 {
1198         /* Cancel old locks if reached asked limit. */
1199         return !((added >= asked) && 
1200                  cfs_time_before_64(cfs_time_current(),
1201                                     cfs_time_add(lock->l_last_used,
1202                                                  ns->ns_max_age)));
1203 }
1204
1205 typedef int (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *, 
1206                                         struct ldlm_lock *, int, 
1207                                         int, int);
1208
1209 static ldlm_cancel_lru_policy_t
1210 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, int flags)
1211 {
1212         if (ns_connect_lru_resize(ns)) {
1213                 if (flags & LDLM_CANCEL_SHRINK)
1214                         return ldlm_cancel_shrink_policy;
1215                 else if (flags & LDLM_CANCEL_LRUR)
1216                         return ldlm_cancel_lrur_policy;
1217                 else if (flags & LDLM_CANCEL_PASSED)
1218                         return ldlm_cancel_passed_policy;
1219         } else {
1220                 if (flags & LDLM_CANCEL_AGED)
1221                         return ldlm_cancel_aged_policy;
1222         }
1223         return NULL;
1224 }
1225  
1226 /* - Free space in lru for @count new locks,
1227  *   redundant unused locks are canceled locally;
1228  * - also cancel locally unused aged locks;
1229  * - do not cancel more than @max locks;
1230  * - GET the found locks and add them into the @cancels list.
1231  *
1232  * A client lock can be added to the l_bl_ast list only when it is
1233  * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing CANCEL.
1234  * There are the following use cases: ldlm_cancel_resource_local(),
1235  * ldlm_cancel_lru_local() and ldlm_cli_cancel(), which check&set this
1236  * flag properly. As any attempt to cancel a lock rely on this flag,
1237  * l_bl_ast list is accessed later without any special locking.
1238  *
1239  * Calling policies for enabled lru resize:
1240  * ----------------------------------------
1241  * flags & LDLM_CANCEL_LRUR - use lru resize policy (SLV from server) to
1242  *                            cancel not more than @count locks;
1243  *
1244  * flags & LDLM_CANCEL_PASSED - cancel @count number of old locks (located at
1245  *                              the beginning of lru list);
1246  *
1247  * flags & LDLM_CANCEL_SHRINK - cancel not more than @count locks according to
1248  *                              memory pressre policy function.
1249  */
1250 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1251                           int count, int max, int flags)
1252 {
1253         ldlm_cancel_lru_policy_t cancel_lru_policy_func;
1254         int added = 0, unused, cancel;
1255         struct ldlm_lock *lock, *next;
1256         ENTRY;
1257
1258         spin_lock(&ns->ns_unused_lock);
1259         unused = ns->ns_nr_unused;
1260
1261         if (!ns_connect_lru_resize(ns))
1262                 count += unused - ns->ns_max_unused;
1263
1264         cancel_lru_policy_func = ldlm_cancel_lru_policy(ns, flags);
1265      
1266         list_for_each_entry_safe(lock, next, &ns->ns_unused_list, l_lru) {
1267                 /* Make sure that we skip locks being already in cancel. */
1268                 if ((lock->l_flags & LDLM_FL_CANCELING) ||
1269                     (lock->l_flags & LDLM_FL_BL_AST))
1270                         continue;
1271
1272                 /* For any flags, stop scanning if @max or passed @count is
1273                  * reached. */
1274                 if ((max && added >= max) || (count && added >= count))
1275                         break;
1276
1277                 /* Pass the lock through the policy filter and see if it
1278                  * should stay in lru. */
1279                 if (cancel_lru_policy_func != NULL) {
1280                         cancel = cancel_lru_policy_func(ns, lock, unused, 
1281                                                         added, count);
1282                      
1283                         /* Take next lock for shrink policy, we need to check
1284                          * whole list. Stop scanning for other policies. */
1285                         if ((flags & LDLM_CANCEL_SHRINK) && !cancel)
1286                                 continue;
1287                         else if (!cancel)
1288                                 break;
1289                 }
1290
1291                 if (cancels != NULL) {
1292                         LDLM_LOCK_GET(lock); /* dropped by bl thread */
1293                         spin_unlock(&ns->ns_unused_lock);
1294
1295                         lock_res_and_lock(lock);
1296                         /* Check flags again under the lock. */
1297                         if ((lock->l_flags & LDLM_FL_CANCELING) ||
1298                             (lock->l_flags & LDLM_FL_BL_AST) ||
1299                             (ldlm_lock_remove_from_lru(lock) == 0)) {
1300                                 /* other thread is removing lock from lru or
1301                                  * somebody is already doing CANCEL or
1302                                  * there is a blocking request which will send
1303                                  * cancel by itseft. */
1304                                 unlock_res_and_lock(lock);
1305                                 LDLM_LOCK_PUT(lock);
1306                                 spin_lock(&ns->ns_unused_lock);
1307                                 continue;
1308                         }
1309                         LASSERT(!lock->l_readers && !lock->l_writers);
1310
1311                         /* If we have chosen to cancel this lock voluntarily, we
1312                          * better send cancel notification to server, so that it 
1313                          * frees appropriate state. This might lead to a race 
1314                          * where while we are doing cancel here, server is also 
1315                          * silently cancelling this lock. */
1316                         lock->l_flags &= ~LDLM_FL_CANCEL_ON_BLOCK;
1317
1318                         /* Setting the CBPENDING flag is a little misleading, but
1319                          * prevents an important race; namely, once CBPENDING is
1320                          * set, the lock can accumulate no more readers/writers.
1321                          * Since readers and writers are already zero here, 
1322                          * ldlm_lock_decref() won't see this flag and call 
1323                          * l_blocking_ast */
1324                         lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1325
1326                         /* We can't re-add to l_lru as it confuses the refcounting
1327                          * in ldlm_lock_remove_from_lru() if an AST arrives after 
1328                          * we drop ns_lock below. We use l_bl_ast and can't use 
1329                          * l_pending_chain as it is used both on server and client
1330                          * nevertheless bug 5666 says it is used only on server */
1331                         LASSERT(list_empty(&lock->l_bl_ast));
1332                         list_add(&lock->l_bl_ast, cancels);
1333                         unlock_res_and_lock(lock);
1334                         spin_lock(&ns->ns_unused_lock);
1335                 }
1336                 added++;
1337                 unused--;
1338         }
1339         spin_unlock(&ns->ns_unused_lock);
1340   
1341         if (cancels == NULL)
1342                 RETURN(added);
1343
1344         RETURN(ldlm_cancel_list(cancels, added));
1345 }
1346
1347 /* when called with LDLM_ASYNC the blocking callback will be handled
1348  * in a thread and this function will return after the thread has been
1349  * asked to call the callback.  when called with LDLM_SYNC the blocking
1350  * callback will be performed in this function. */
1351 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr, ldlm_sync_t sync, 
1352                     int flags)
1353 {
1354         CFS_LIST_HEAD(cancels);
1355         int count, rc;
1356         ENTRY;
1357
1358 #ifndef __KERNEL__
1359         sync = LDLM_SYNC; /* force to be sync in user space */
1360 #endif
1361         count = ldlm_cancel_lru_local(ns, &cancels, nr, 0, flags);
1362         if (sync == LDLM_ASYNC) {
1363                 rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count);
1364                 if (rc == 0)
1365                         RETURN(count);
1366         }
1367
1368         /* If an error occured in ASYNC mode, or
1369          * this is SYNC mode, cancel the list. */
1370         ldlm_cli_cancel_list(&cancels, count, NULL, 0, 0);
1371         RETURN(count);
1372 }
1373
1374 /* Find and cancel locally unused locks found on resource, matched to the
1375  * given policy, mode. GET the found locks and add them into the @cancels
1376  * list. */
1377 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1378                                struct list_head *cancels,
1379                                ldlm_policy_data_t *policy,
1380                                ldlm_mode_t mode, int lock_flags,
1381                                int flags, void *opaque)
1382 {
1383         struct ldlm_lock *lock;
1384         int count = 0;
1385         ENTRY;
1386
1387         lock_res(res);
1388         list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1389                 if (opaque != NULL && lock->l_ast_data != opaque) {
1390                         LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1391                                    lock->l_ast_data, opaque);
1392                         //LBUG();
1393                         continue;
1394                 }
1395
1396                 if (lock->l_readers || lock->l_writers) {
1397                         if (flags & LDLM_FL_WARN) {
1398                                 LDLM_ERROR(lock, "lock in use");
1399                                 //LBUG();
1400                         }
1401                         continue;
1402                 }
1403
1404                 /* If somebody is already doing CANCEL, or blocking ast came,
1405                  * skip this lock. */
1406                 if (lock->l_flags & LDLM_FL_BL_AST || 
1407                     lock->l_flags & LDLM_FL_CANCELING)
1408                         continue;
1409
1410                 if (lockmode_compat(lock->l_granted_mode, mode))
1411                         continue;
1412
1413                 /* If policy is given and this is IBITS lock, add to list only
1414                  * those locks that match by policy. */
1415                 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1416                     !(lock->l_policy_data.l_inodebits.bits &
1417                       policy->l_inodebits.bits))
1418                         continue;
1419
1420                 /* See CBPENDING comment in ldlm_cancel_lru */
1421                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1422                                  lock_flags;
1423
1424                 LASSERT(list_empty(&lock->l_bl_ast));
1425                 list_add(&lock->l_bl_ast, cancels);
1426                 LDLM_LOCK_GET(lock);
1427                 count++;
1428         }
1429         unlock_res(res);
1430
1431        if ((flags & LDLM_FL_LOCAL_ONLY))
1432                RETURN(ldlm_cancel_list_local(cancels, count));
1433
1434         RETURN(ldlm_cancel_list(cancels, count));
1435 }
1436
1437 /* If @req is NULL, send CANCEL request to server with handles of locks 
1438  * in the @cancels. If EARLY_CANCEL is not supported, send CANCEL requests 
1439  * separately per lock.
1440  * If @req is not NULL, put handles of locks in @cancels into the request 
1441  * buffer at the offset @off.
1442  * Destroy @cancels at the end. */
1443 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1444                          struct ptlrpc_request *req, int off, int flags)
1445 {
1446         struct ldlm_lock *lock;
1447         int res = 0;
1448         ENTRY;
1449
1450         if (list_empty(cancels) || count == 0)
1451                 RETURN(0);
1452         
1453         /* XXX: requests (both batched and not) could be sent in parallel. 
1454          * Usually it is enough to have just 1 RPC, but it is possible that
1455          * there are to many locks to be cancelled in LRU or on a resource.
1456          * It would also speed up the case when the server does not support
1457          * the feature. */
1458         while (count > 0) {
1459                 LASSERT(!list_empty(cancels));
1460                 lock = list_entry(cancels->next, struct ldlm_lock, l_bl_ast);
1461                 LASSERT(lock->l_conn_export);
1462
1463                 if (exp_connect_cancelset(lock->l_conn_export)) {
1464                         res = count;
1465                         if (req)
1466                                 ldlm_cancel_pack(req, off, cancels, count);
1467                         else
1468                                 res = ldlm_cli_cancel_req(lock->l_conn_export,
1469                                                           cancels, count,
1470                                                           flags);
1471                 } else {
1472                         res = ldlm_cli_cancel_req(lock->l_conn_export,
1473                                                   cancels, 1, flags);
1474                 }
1475
1476                 if (res < 0) {
1477                         CERROR("ldlm_cli_cancel_list: %d\n", res);
1478                         res = count;
1479                 }
1480
1481                 count -= res;
1482                 ldlm_lock_list_put(cancels, l_bl_ast, res);
1483         }
1484         LASSERT(list_empty(cancels));
1485         LASSERT(count == 0);
1486         RETURN(0);
1487 }
1488
1489 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1490                                     const struct ldlm_res_id *res_id,
1491                                     ldlm_policy_data_t *policy,
1492                                     ldlm_mode_t mode, int flags, void *opaque)
1493 {
1494         struct ldlm_resource *res;
1495         CFS_LIST_HEAD(cancels);
1496         int count;
1497         int rc;
1498         ENTRY;
1499
1500         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1501         if (res == NULL) {
1502                 /* This is not a problem. */
1503                 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1504                 RETURN(0);
1505         }
1506
1507         count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1508                                            0, flags, opaque);
1509         rc = ldlm_cli_cancel_list(&cancels, count, NULL, 0, flags);
1510         if (rc != ELDLM_OK)
1511                 CERROR("ldlm_cli_cancel_unused_resource: %d\n", rc);
1512
1513         ldlm_resource_putref(res);
1514         RETURN(0);
1515 }
1516
1517 static inline int have_no_nsresource(struct ldlm_namespace *ns)
1518 {
1519         int no_resource = 0;
1520
1521         spin_lock(&ns->ns_hash_lock);
1522         if (ns->ns_resources == 0)
1523                 no_resource = 1;
1524         spin_unlock(&ns->ns_hash_lock);
1525
1526         RETURN(no_resource);
1527 }
1528
1529 /* Cancel all locks on a namespace (or a specific resource, if given)
1530  * that have 0 readers/writers.
1531  *
1532  * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1533  * to notify the server. */
1534 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
1535                            const struct ldlm_res_id *res_id,
1536                            int flags, void *opaque)
1537 {
1538         int i;
1539         ENTRY;
1540
1541         if (ns == NULL)
1542                 RETURN(ELDLM_OK);
1543
1544         if (res_id)
1545                 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
1546                                                        LCK_MINMODE, flags,
1547                                                        opaque));
1548
1549         spin_lock(&ns->ns_hash_lock);
1550         for (i = 0; i < RES_HASH_SIZE; i++) {
1551                 struct list_head *tmp;
1552                 tmp = ns->ns_hash[i].next;
1553                 while (tmp != &(ns->ns_hash[i])) {
1554                         struct ldlm_resource *res;
1555                         int rc;
1556
1557                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
1558                         ldlm_resource_getref(res);
1559                         spin_unlock(&ns->ns_hash_lock);
1560
1561                         rc = ldlm_cli_cancel_unused_resource(ns, &res->lr_name,
1562                                                              NULL, LCK_MINMODE,
1563                                                              flags, opaque);
1564
1565                         if (rc)
1566                                 CERROR("ldlm_cli_cancel_unused ("LPU64"): %d\n",
1567                                        res->lr_name.name[0], rc);
1568
1569                         spin_lock(&ns->ns_hash_lock);
1570                         tmp = tmp->next;
1571                         ldlm_resource_putref_locked(res);
1572                 }
1573         }
1574         spin_unlock(&ns->ns_hash_lock);
1575
1576         RETURN(ELDLM_OK);
1577 }
1578
1579 /* join/split resource locks to/from lru list */
1580 int ldlm_cli_join_lru(struct ldlm_namespace *ns,
1581                       const struct ldlm_res_id *res_id, int join)
1582 {
1583         struct ldlm_resource *res;
1584         struct ldlm_lock *lock, *n;
1585         int count = 0;
1586         ENTRY;
1587
1588         LASSERT(ns_is_client(ns));
1589
1590         res = ldlm_resource_get(ns, NULL, res_id, LDLM_EXTENT, 0);
1591         if (res == NULL)
1592                 RETURN(count);
1593         LASSERT(res->lr_type == LDLM_EXTENT);
1594
1595         lock_res(res);
1596         if (!join)
1597                 goto split;
1598
1599         list_for_each_entry_safe (lock, n, &res->lr_granted, l_res_link) {
1600                 if (list_empty(&lock->l_lru) &&
1601                     !lock->l_readers && !lock->l_writers &&
1602                     !(lock->l_flags & LDLM_FL_LOCAL) &&
1603                     !(lock->l_flags & LDLM_FL_CBPENDING)) {
1604                         ldlm_lock_add_to_lru(lock);
1605                         lock->l_flags &= ~LDLM_FL_NO_LRU;
1606                         LDLM_DEBUG(lock, "join lock to lru");
1607                         count++;
1608                 }
1609         }
1610         goto unlock;
1611 split:
1612         spin_lock(&ns->ns_unused_lock);
1613         list_for_each_entry_safe (lock, n, &ns->ns_unused_list, l_lru) {
1614                 if (lock->l_resource == res) {
1615                         ldlm_lock_remove_from_lru_nolock(lock);
1616                         lock->l_flags |= LDLM_FL_NO_LRU;
1617                         LDLM_DEBUG(lock, "split lock from lru");
1618                         count++;
1619                 }
1620         }
1621         spin_unlock(&ns->ns_unused_lock);
1622 unlock:
1623         unlock_res(res);
1624         ldlm_resource_putref(res);
1625         RETURN(count);
1626 }
1627
1628 /* Lock iterators. */
1629
1630 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
1631                           void *closure)
1632 {
1633         struct list_head *tmp, *next;
1634         struct ldlm_lock *lock;
1635         int rc = LDLM_ITER_CONTINUE;
1636
1637         ENTRY;
1638
1639         if (!res)
1640                 RETURN(LDLM_ITER_CONTINUE);
1641
1642         lock_res(res);
1643         list_for_each_safe(tmp, next, &res->lr_granted) {
1644                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1645
1646                 if (iter(lock, closure) == LDLM_ITER_STOP)
1647                         GOTO(out, rc = LDLM_ITER_STOP);
1648         }
1649
1650         list_for_each_safe(tmp, next, &res->lr_converting) {
1651                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1652
1653                 if (iter(lock, closure) == LDLM_ITER_STOP)
1654                         GOTO(out, rc = LDLM_ITER_STOP);
1655         }
1656
1657         list_for_each_safe(tmp, next, &res->lr_waiting) {
1658                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
1659
1660                 if (iter(lock, closure) == LDLM_ITER_STOP)
1661                         GOTO(out, rc = LDLM_ITER_STOP);
1662         }
1663  out:
1664         unlock_res(res);
1665         RETURN(rc);
1666 }
1667
1668 struct iter_helper_data {
1669         ldlm_iterator_t iter;
1670         void *closure;
1671 };
1672
1673 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
1674 {
1675         struct iter_helper_data *helper = closure;
1676         return helper->iter(lock, helper->closure);
1677 }
1678
1679 static int ldlm_res_iter_helper(struct ldlm_resource *res, void *closure)
1680 {
1681         return ldlm_resource_foreach(res, ldlm_iter_helper, closure);
1682 }
1683
1684 int ldlm_namespace_foreach(struct ldlm_namespace *ns, ldlm_iterator_t iter,
1685                            void *closure)
1686 {
1687         struct iter_helper_data helper = { iter: iter, closure: closure };
1688         return ldlm_namespace_foreach_res(ns, ldlm_res_iter_helper, &helper);
1689 }
1690
1691 int ldlm_namespace_foreach_res(struct ldlm_namespace *ns,
1692                                ldlm_res_iterator_t iter, void *closure)
1693 {
1694         int i, rc = LDLM_ITER_CONTINUE;
1695         struct ldlm_resource *res;
1696         struct list_head *tmp;
1697
1698         ENTRY;
1699         spin_lock(&ns->ns_hash_lock);
1700         for (i = 0; i < RES_HASH_SIZE; i++) {
1701                 tmp = ns->ns_hash[i].next;
1702                 while (tmp != &(ns->ns_hash[i])) {
1703                         res = list_entry(tmp, struct ldlm_resource, lr_hash);
1704                         ldlm_resource_getref(res);
1705                         spin_unlock(&ns->ns_hash_lock);
1706
1707                         rc = iter(res, closure);
1708
1709                         spin_lock(&ns->ns_hash_lock);
1710                         tmp = tmp->next;
1711                         ldlm_resource_putref_locked(res);
1712                         if (rc == LDLM_ITER_STOP)
1713                                 GOTO(out, rc);
1714                 }
1715         }
1716  out:
1717         spin_unlock(&ns->ns_hash_lock);
1718         RETURN(rc);
1719 }
1720
1721 /* non-blocking function to manipulate a lock whose cb_data is being put away.*/
1722 void ldlm_resource_iterate(struct ldlm_namespace *ns,
1723                            const struct ldlm_res_id *res_id,
1724                            ldlm_iterator_t iter, void *data)
1725 {
1726         struct ldlm_resource *res;
1727         ENTRY;
1728
1729         if (ns == NULL) {
1730                 CERROR("must pass in namespace\n");
1731                 LBUG();
1732         }
1733
1734         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1735         if (res == NULL) {
1736                 EXIT;
1737                 return;
1738         }
1739
1740         ldlm_resource_foreach(res, iter, data);
1741         ldlm_resource_putref(res);
1742         EXIT;
1743 }
1744
1745 /* Lock replay */
1746
1747 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
1748 {
1749         struct list_head *list = closure;
1750
1751         /* we use l_pending_chain here, because it's unused on clients. */
1752         LASSERTF(list_empty(&lock->l_pending_chain),"lock %p next %p prev %p\n",
1753                  lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
1754         /* bug 9573: don't replay locks left after eviction */
1755         if (!(lock->l_flags & LDLM_FL_FAILED))
1756                 list_add(&lock->l_pending_chain, list);
1757         return LDLM_ITER_CONTINUE;
1758 }
1759
1760 static int replay_lock_interpret(struct ptlrpc_request *req,
1761                                  struct ldlm_async_args *aa, int rc)
1762 {
1763         struct ldlm_lock *lock;
1764         struct ldlm_reply *reply;
1765
1766         ENTRY;
1767         atomic_dec(&req->rq_import->imp_replay_inflight);
1768         if (rc != ELDLM_OK)
1769                 GOTO(out, rc);
1770
1771
1772         reply = lustre_swab_repbuf(req, DLM_LOCKREPLY_OFF, sizeof(*reply),
1773                                    lustre_swab_ldlm_reply);
1774         if (reply == NULL) {
1775                 CERROR("Can't unpack ldlm_reply\n");
1776                 GOTO (out, rc = -EPROTO);
1777         }
1778
1779         lock = ldlm_handle2lock(&aa->lock_handle);
1780         if (!lock) {
1781                 CERROR("received replay ack for unknown local cookie "LPX64
1782                        " remote cookie "LPX64 " from server %s id %s\n",
1783                        aa->lock_handle.cookie, reply->lock_handle.cookie,
1784                        req->rq_export->exp_client_uuid.uuid,
1785                        libcfs_id2str(req->rq_peer));
1786                 GOTO(out, rc = -ESTALE);
1787         }
1788
1789         lock->l_remote_handle = reply->lock_handle;
1790         LDLM_DEBUG(lock, "replayed lock:");
1791         ptlrpc_import_recovery_state_machine(req->rq_import);
1792         LDLM_LOCK_PUT(lock);
1793 out:
1794         if (rc != ELDLM_OK)
1795                 ptlrpc_connect_import(req->rq_import, NULL);
1796
1797
1798         RETURN(rc);
1799 }
1800
1801 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
1802 {
1803         struct ptlrpc_request *req;
1804         struct ldlm_request *body;
1805         struct ldlm_reply *reply;
1806         struct ldlm_async_args *aa;
1807         int buffers = 2;
1808         int size[3] = { sizeof(struct ptlrpc_body) };
1809         int flags;
1810         ENTRY;
1811
1812
1813         /* Bug 11974: Do not replay a lock which is actively being canceled */
1814         if (lock->l_flags & LDLM_FL_CANCELING) {
1815                 LDLM_DEBUG(lock, "Not replaying canceled lock:");
1816                 RETURN(0);
1817         }
1818
1819         /* If this is reply-less callback lock, we cannot replay it, since
1820          * server might have long dropped it, but notification of that event was
1821          * lost by network. (and server granted conflicting lock already) */
1822         if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1823                 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
1824                 ldlm_lock_cancel(lock);
1825                 RETURN(0);
1826         }
1827         /*
1828          * If granted mode matches the requested mode, this lock is granted.
1829          *
1830          * If they differ, but we have a granted mode, then we were granted
1831          * one mode and now want another: ergo, converting.
1832          *
1833          * If we haven't been granted anything and are on a resource list,
1834          * then we're blocked/waiting.
1835          *
1836          * If we haven't been granted anything and we're NOT on a resource list,
1837          * then we haven't got a reply yet and don't have a known disposition.
1838          * This happens whenever a lock enqueue is the request that triggers
1839          * recovery.
1840          */
1841         if (lock->l_granted_mode == lock->l_req_mode)
1842                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
1843         else if (lock->l_granted_mode)
1844                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
1845         else if (!list_empty(&lock->l_res_link))
1846                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
1847         else
1848                 flags = LDLM_FL_REPLAY;
1849
1850         size[DLM_LOCKREQ_OFF] = sizeof(*body);
1851         req = ptlrpc_prep_req(imp, LUSTRE_DLM_VERSION, LDLM_ENQUEUE, 2, size,
1852                               NULL);
1853         if (!req)
1854                 RETURN(-ENOMEM);
1855
1856         /* We're part of recovery, so don't wait for it. */
1857         req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
1858
1859         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
1860         ldlm_lock2desc(lock, &body->lock_desc);
1861         body->lock_flags = flags;
1862
1863         ldlm_lock2handle(lock, &body->lock_handle[0]);
1864         size[DLM_LOCKREPLY_OFF] = sizeof(*reply);
1865         if (lock->l_lvb_len != 0) {
1866                 buffers = 3;
1867                 size[DLM_REPLY_REC_OFF] = lock->l_lvb_len;
1868         }
1869         ptlrpc_req_set_repsize(req, buffers, size);
1870         /* notify the server we've replayed all requests.
1871          * also, we mark the request to be put on a dedicated
1872          * queue to be processed after all request replayes.
1873          * bug 6063 */
1874         lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
1875
1876         LDLM_DEBUG(lock, "replaying lock:");
1877
1878         atomic_inc(&req->rq_import->imp_replay_inflight);
1879         CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
1880         aa = (struct ldlm_async_args *)&req->rq_async_args;
1881         aa->lock_handle = body->lock_handle[0];
1882         req->rq_interpret_reply = replay_lock_interpret;
1883         ptlrpcd_add_req(req);
1884
1885         RETURN(0);
1886 }
1887
1888 int ldlm_replay_locks(struct obd_import *imp)
1889 {
1890         struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
1891         struct list_head list;
1892         struct ldlm_lock *lock, *next;
1893         int rc = 0;
1894
1895         ENTRY;
1896         CFS_INIT_LIST_HEAD(&list);
1897
1898         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
1899
1900         /* ensure this doesn't fall to 0 before all have been queued */
1901         atomic_inc(&imp->imp_replay_inflight);
1902
1903         (void)ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
1904
1905         list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
1906                 list_del_init(&lock->l_pending_chain);
1907                 if (rc)
1908                         continue; /* or try to do the rest? */
1909                 rc = replay_one_lock(imp, lock);
1910         }
1911
1912         atomic_dec(&imp->imp_replay_inflight);
1913
1914         RETURN(rc);
1915 }