Whamcloud - gitweb
LU-7791 ldlm: signal vs CP callback race
[fs/lustre-release.git] / lustre / ldlm / ldlm_request.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2010, 2015, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36 /**
37  * This file contains Asynchronous System Trap (AST) handlers and related
38  * LDLM request-processing routines.
39  *
40  * An AST is a callback issued on a lock when its state is changed. There are
41  * several different types of ASTs (callbacks) registered for each lock:
42  *
43  * - completion AST: when a lock is enqueued by some process, but cannot be
44  *   granted immediately due to other conflicting locks on the same resource,
45  *   the completion AST is sent to notify the caller when the lock is
46  *   eventually granted
47  *
48  * - blocking AST: when a lock is granted to some process, if another process
49  *   enqueues a conflicting (blocking) lock on a resource, a blocking AST is
50  *   sent to notify the holder(s) of the lock(s) of the conflicting lock
51  *   request. The lock holder(s) must release their lock(s) on that resource in
52  *   a timely manner or be evicted by the server.
53  *
54  * - glimpse AST: this is used when a process wants information about a lock
55  *   (i.e. the lock value block (LVB)) but does not necessarily require holding
56  *   the lock. If the resource is locked, the lock holder(s) are sent glimpse
57  *   ASTs and the LVB is returned to the caller, and lock holder(s) may CANCEL
58  *   their lock(s) if they are idle. If the resource is not locked, the server
59  *   may grant the lock.
60  */
61
62 #define DEBUG_SUBSYSTEM S_LDLM
63
64 #include <lustre_dlm.h>
65 #include <obd_class.h>
66 #include <obd.h>
67
68 #include "ldlm_internal.h"
69
70 unsigned int ldlm_enqueue_min = OBD_TIMEOUT_DEFAULT;
71 module_param(ldlm_enqueue_min, uint, 0644);
72 MODULE_PARM_DESC(ldlm_enqueue_min, "lock enqueue timeout minimum");
73
74 /* in client side, whether the cached locks will be canceled before replay */
75 unsigned int ldlm_cancel_unused_locks_before_replay = 1;
76
77 static void interrupted_completion_wait(void *data)
78 {
79 }
80
81 struct lock_wait_data {
82         struct ldlm_lock *lwd_lock;
83         __u32             lwd_conn_cnt;
84 };
85
86 struct ldlm_async_args {
87         struct lustre_handle lock_handle;
88 };
89
90 int ldlm_expired_completion_wait(void *data)
91 {
92         struct lock_wait_data *lwd = data;
93         struct ldlm_lock *lock = lwd->lwd_lock;
94         struct obd_import *imp;
95         struct obd_device *obd;
96
97         ENTRY;
98         if (lock->l_conn_export == NULL) {
99                 static cfs_time_t next_dump = 0, last_dump = 0;
100
101                 LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
102                            CFS_DURATION_T"s ago); not entering recovery in "
103                            "server code, just going back to sleep",
104                            lock->l_last_activity,
105                            cfs_time_sub(cfs_time_current_sec(),
106                                         lock->l_last_activity));
107                 if (cfs_time_after(cfs_time_current(), next_dump)) {
108                         last_dump = next_dump;
109                         next_dump = cfs_time_shift(300);
110                         ldlm_namespace_dump(D_DLMTRACE,
111                                             ldlm_lock_to_ns(lock));
112                         if (last_dump == 0)
113                                 libcfs_debug_dumplog();
114                 }
115                 RETURN(0);
116         }
117
118         obd = lock->l_conn_export->exp_obd;
119         imp = obd->u.cli.cl_import;
120         ptlrpc_fail_import(imp, lwd->lwd_conn_cnt);
121         LDLM_ERROR(lock, "lock timed out (enqueued at "CFS_TIME_T", "
122                   CFS_DURATION_T"s ago), entering recovery for %s@%s",
123                   lock->l_last_activity,
124                   cfs_time_sub(cfs_time_current_sec(), lock->l_last_activity),
125                   obd2cli_tgt(obd), imp->imp_connection->c_remote_uuid.uuid);
126
127         RETURN(0);
128 }
129
130 /**
131  * Calculate the Completion timeout (covering enqueue, BL AST, data flush,
132  * lock cancel, and their replies). Used for lock completion timeout on the
133  * client side.
134  *
135  * \param[in] lock        lock which is waiting the completion callback
136  *
137  * \retval            timeout in seconds to wait for the server reply
138  */
139
140 /* We use the same basis for both server side and client side functions
141    from a single node. */
142 static unsigned int ldlm_cp_timeout(struct ldlm_lock *lock)
143 {
144         unsigned int timeout;
145
146         if (AT_OFF)
147                 return obd_timeout;
148
149         /* Wait a long time for enqueue - server may have to callback a
150          * lock from another client.  Server will evict the other client if it
151          * doesn't respond reasonably, and then give us the lock. */
152         timeout = at_get(ldlm_lock_to_ns_at(lock));
153         return max(3 * timeout, ldlm_enqueue_min);
154 }
155
156 /**
157  * Helper function for ldlm_completion_ast(), updating timings when lock is
158  * actually granted.
159  */
160 static int ldlm_completion_tail(struct ldlm_lock *lock, void *data)
161 {
162         long delay;
163         int  result = 0;
164
165         if (ldlm_is_destroyed(lock) || ldlm_is_failed(lock)) {
166                 LDLM_DEBUG(lock, "client-side enqueue: destroyed");
167                 result = -EIO;
168         } else if (data == NULL) {
169                 LDLM_DEBUG(lock, "client-side enqueue: granted");
170         } else {
171                 /* Take into AT only CP RPC, not immediately granted locks */
172                 delay = cfs_time_sub(cfs_time_current_sec(),
173                                      lock->l_last_activity);
174                 LDLM_DEBUG(lock, "client-side enqueue: granted after "
175                            CFS_DURATION_T"s", delay);
176
177                 /* Update our time estimate */
178                 at_measured(ldlm_lock_to_ns_at(lock), delay);
179         }
180         return result;
181 }
182
183 /**
184  * Implementation of ->l_completion_ast() for a client, that doesn't wait
185  * until lock is granted. Suitable for locks enqueued through ptlrpcd, of
186  * other threads that cannot block for long.
187  */
188 int ldlm_completion_ast_async(struct ldlm_lock *lock, __u64 flags, void *data)
189 {
190         ENTRY;
191
192         if (flags == LDLM_FL_WAIT_NOREPROC) {
193                 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
194                 RETURN(0);
195         }
196
197         if (!(flags & LDLM_FL_BLOCKED_MASK)) {
198                 wake_up(&lock->l_waitq);
199                 RETURN(ldlm_completion_tail(lock, data));
200         }
201
202         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
203                    "going forward");
204         ldlm_reprocess_all(lock->l_resource);
205         RETURN(0);
206 }
207 EXPORT_SYMBOL(ldlm_completion_ast_async);
208
209 /**
210  * Generic LDLM "completion" AST. This is called in several cases:
211  *
212  *     - when a reply to an ENQUEUE RPC is received from the server
213  *       (ldlm_cli_enqueue_fini()). Lock might be granted or not granted at
214  *       this point (determined by flags);
215  *
216  *     - when LDLM_CP_CALLBACK RPC comes to client to notify it that lock has
217  *       been granted;
218  *
219  *     - when ldlm_lock_match(LDLM_FL_LVB_READY) is about to wait until lock
220  *       gets correct lvb;
221  *
222  *     - to force all locks when resource is destroyed (cleanup_resource());
223  *
224  *     - during lock conversion (not used currently).
225  *
226  * If lock is not granted in the first case, this function waits until second
227  * or penultimate cases happen in some other thread.
228  *
229  */
230 int ldlm_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
231 {
232         /* XXX ALLOCATE - 160 bytes */
233         struct lock_wait_data lwd;
234         struct obd_device *obd;
235         struct obd_import *imp = NULL;
236         struct l_wait_info lwi;
237         __u32 timeout;
238         int rc = 0;
239         ENTRY;
240
241         if (flags == LDLM_FL_WAIT_NOREPROC) {
242                 LDLM_DEBUG(lock, "client-side enqueue waiting on pending lock");
243                 goto noreproc;
244         }
245
246         if (!(flags & LDLM_FL_BLOCKED_MASK)) {
247                 wake_up(&lock->l_waitq);
248                 RETURN(0);
249         }
250
251         LDLM_DEBUG(lock, "client-side enqueue returned a blocked lock, "
252                    "sleeping");
253
254 noreproc:
255
256         obd = class_exp2obd(lock->l_conn_export);
257
258         /* if this is a local lock, then there is no import */
259         if (obd != NULL) {
260                 imp = obd->u.cli.cl_import;
261         }
262
263         timeout = ldlm_cp_timeout(lock);
264
265         lwd.lwd_lock = lock;
266         lock->l_last_activity = cfs_time_current_sec();
267
268         if (ldlm_is_no_timeout(lock)) {
269                 LDLM_DEBUG(lock, "waiting indefinitely because of NO_TIMEOUT");
270                 lwi = LWI_INTR(interrupted_completion_wait, &lwd);
271         } else {
272                 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(timeout),
273                                        ldlm_expired_completion_wait,
274                                        interrupted_completion_wait, &lwd);
275         }
276
277         if (imp != NULL) {
278                 spin_lock(&imp->imp_lock);
279                 lwd.lwd_conn_cnt = imp->imp_conn_cnt;
280                 spin_unlock(&imp->imp_lock);
281         }
282
283         if (ns_is_client(ldlm_lock_to_ns(lock)) &&
284             OBD_FAIL_CHECK_RESET(OBD_FAIL_LDLM_INTR_CP_AST,
285                                  OBD_FAIL_LDLM_CP_BL_RACE | OBD_FAIL_ONCE)) {
286                 ldlm_set_fail_loc(lock);
287                 rc = -EINTR;
288         } else {
289                 /* Go to sleep until the lock is granted or cancelled. */
290                 rc = l_wait_event(lock->l_waitq,
291                                   is_granted_or_cancelled(lock), &lwi);
292         }
293
294         if (rc) {
295                 LDLM_DEBUG(lock, "client-side enqueue waking up: failed (%d)",
296                            rc);
297                 RETURN(rc);
298         }
299
300         RETURN(ldlm_completion_tail(lock, data));
301 }
302 EXPORT_SYMBOL(ldlm_completion_ast);
303
304 /**
305  * A helper to build a blocking AST function
306  *
307  * Perform a common operation for blocking ASTs:
308  * defferred lock cancellation.
309  *
310  * \param lock the lock blocking or canceling AST was called on
311  * \retval 0
312  * \see mdt_blocking_ast
313  * \see ldlm_blocking_ast
314  */
315 int ldlm_blocking_ast_nocheck(struct ldlm_lock *lock)
316 {
317         int do_ast;
318         ENTRY;
319
320         ldlm_set_cbpending(lock);
321         do_ast = (!lock->l_readers && !lock->l_writers);
322         unlock_res_and_lock(lock);
323
324         if (do_ast) {
325                 struct lustre_handle lockh;
326                 int rc;
327
328                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
329                 ldlm_lock2handle(lock, &lockh);
330                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
331                 if (rc < 0)
332                         CERROR("ldlm_cli_cancel: %d\n", rc);
333         } else {
334                 LDLM_DEBUG(lock, "Lock still has references, will be "
335                            "cancelled later");
336         }
337         RETURN(0);
338 }
339 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
340
341 /**
342  * Server blocking AST
343  *
344  * ->l_blocking_ast() callback for LDLM locks acquired by server-side
345  * OBDs.
346  *
347  * \param lock the lock which blocks a request or cancelling lock
348  * \param desc unused
349  * \param data unused
350  * \param flag indicates whether this cancelling or blocking callback
351  * \retval 0
352  * \see ldlm_blocking_ast_nocheck
353  */
354 int ldlm_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
355                       void *data, int flag)
356 {
357         ENTRY;
358
359         if (flag == LDLM_CB_CANCELING) {
360                 /* Don't need to do anything here. */
361                 RETURN(0);
362         }
363
364         lock_res_and_lock(lock);
365         /* Get this: if ldlm_blocking_ast is racing with intent_policy, such
366          * that ldlm_blocking_ast is called just before intent_policy method
367          * takes the lr_lock, then by the time we get the lock, we might not
368          * be the correct blocking function anymore.  So check, and return
369          * early, if so. */
370         if (lock->l_blocking_ast != ldlm_blocking_ast) {
371                 unlock_res_and_lock(lock);
372                 RETURN(0);
373         }
374         RETURN(ldlm_blocking_ast_nocheck(lock));
375 }
376 EXPORT_SYMBOL(ldlm_blocking_ast);
377
378 /**
379  * Implements ldlm_lock::l_glimpse_ast for extent locks acquired on the server.
380  *
381  * Returning -ELDLM_NO_LOCK_DATA actually works, but the reason for that is
382  * rather subtle: with OST-side locking, it may so happen that _all_ extent
383  * locks are held by the OST. If client wants to obtain the current file size
384  * it calls ll_glimpse_size(), and (as all locks are held only on the server),
385  * this dummy glimpse callback fires and does nothing. The client still
386  * receives the correct file size due to the following fragment of code in
387  * ldlm_cb_interpret():
388  *
389  *      if (rc == -ELDLM_NO_LOCK_DATA) {
390  *              LDLM_DEBUG(lock, "lost race - client has a lock but no"
391  *                         "inode");
392  *              ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
393  *      }
394  *
395  * That is, after the glimpse returns this error, ofd_lvbo_update() is called
396  * and returns the updated file attributes from the inode to the client.
397  *
398  * See also comment in ofd_intent_policy() on why servers must set a non-NULL
399  * l_glimpse_ast when grabbing DLM locks.  Otherwise, the server will assume
400  * that the object is in the process of being destroyed.
401  *
402  * \param[in] lock      DLM lock being glimpsed, unused
403  * \param[in] reqp      pointer to ptlrpc_request, unused
404  *
405  * \retval              -ELDLM_NO_LOCK_DATA to get attributes from disk object
406  */
407 int ldlm_glimpse_ast(struct ldlm_lock *lock, void *reqp)
408 {
409         return -ELDLM_NO_LOCK_DATA;
410 }
411
412 /**
413  * Enqueue a local lock (typically on a server).
414  */
415 int ldlm_cli_enqueue_local(struct ldlm_namespace *ns,
416                            const struct ldlm_res_id *res_id,
417                            enum ldlm_type type, union ldlm_policy_data *policy,
418                            enum ldlm_mode mode, __u64 *flags,
419                            ldlm_blocking_callback blocking,
420                            ldlm_completion_callback completion,
421                            ldlm_glimpse_callback glimpse,
422                            void *data, __u32 lvb_len, enum lvb_type lvb_type,
423                            const __u64 *client_cookie,
424                            struct lustre_handle *lockh)
425 {
426         struct ldlm_lock *lock;
427         int err;
428         const struct ldlm_callback_suite cbs = { .lcs_completion = completion,
429                                                  .lcs_blocking   = blocking,
430                                                  .lcs_glimpse    = glimpse,
431         };
432         ENTRY;
433
434         LASSERT(!(*flags & LDLM_FL_REPLAY));
435         if (unlikely(ns_is_client(ns))) {
436                 CERROR("Trying to enqueue local lock in a shadow namespace\n");
437                 LBUG();
438         }
439
440         lock = ldlm_lock_create(ns, res_id, type, mode, &cbs, data, lvb_len,
441                                 lvb_type);
442         if (IS_ERR(lock))
443                 GOTO(out_nolock, err = PTR_ERR(lock));
444
445         err = ldlm_lvbo_init(lock->l_resource);
446         if (err < 0) {
447                 LDLM_ERROR(lock, "delayed lvb init failed (rc %d)", err);
448                 GOTO(out, err);
449         }
450
451         ldlm_lock2handle(lock, lockh);
452
453         /* NB: we don't have any lock now (lock_res_and_lock)
454          * because it's a new lock */
455         ldlm_lock_addref_internal_nolock(lock, mode);
456         ldlm_set_local(lock);
457         if (*flags & LDLM_FL_ATOMIC_CB)
458                 ldlm_set_atomic_cb(lock);
459
460         if (policy != NULL)
461                 lock->l_policy_data = *policy;
462         if (client_cookie != NULL)
463                 lock->l_client_cookie = *client_cookie;
464         if (type == LDLM_EXTENT) {
465                 /* extent lock without policy is a bug */
466                 if (policy == NULL)
467                         LBUG();
468
469                 lock->l_req_extent = policy->l_extent;
470         }
471
472         err = ldlm_lock_enqueue(ns, &lock, policy, flags);
473         if (unlikely(err != ELDLM_OK))
474                 GOTO(out, err);
475
476         if (policy != NULL)
477                 *policy = lock->l_policy_data;
478
479         if (lock->l_completion_ast)
480                 lock->l_completion_ast(lock, *flags, NULL);
481
482         LDLM_DEBUG(lock, "client-side local enqueue handler, new lock created");
483         EXIT;
484  out:
485         LDLM_LOCK_RELEASE(lock);
486  out_nolock:
487         return err;
488 }
489 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
490
491 static void failed_lock_cleanup(struct ldlm_namespace *ns,
492                                 struct ldlm_lock *lock, int mode)
493 {
494         int need_cancel = 0;
495
496         /* Set a flag to prevent us from sending a CANCEL (bug 407) */
497         lock_res_and_lock(lock);
498         /* Check that lock is not granted or failed, we might race. */
499         if ((lock->l_req_mode != lock->l_granted_mode) &&
500             !ldlm_is_failed(lock)) {
501                 /* Make sure that this lock will not be found by raced
502                  * bl_ast and -EINVAL reply is sent to server anyways.
503                  * b=17645*/
504                 lock->l_flags |= LDLM_FL_LOCAL_ONLY | LDLM_FL_FAILED |
505                                  LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING;
506                 need_cancel = 1;
507         }
508         unlock_res_and_lock(lock);
509
510         if (need_cancel)
511                 LDLM_DEBUG(lock,
512                            "setting FL_LOCAL_ONLY | LDLM_FL_FAILED | "
513                            "LDLM_FL_ATOMIC_CB | LDLM_FL_CBPENDING");
514         else
515                 LDLM_DEBUG(lock, "lock was granted or failed in race");
516
517         /* XXX - HACK because we shouldn't call ldlm_lock_destroy()
518          *       from llite/file.c/ll_file_flock(). */
519         /* This code makes for the fact that we do not have blocking handler on
520          * a client for flock locks. As such this is the place where we must
521          * completely kill failed locks. (interrupted and those that
522          * were waiting to be granted when server evicted us. */
523         if (lock->l_resource->lr_type == LDLM_FLOCK) {
524                 lock_res_and_lock(lock);
525                 if (!ldlm_is_destroyed(lock)) {
526                         ldlm_resource_unlink_lock(lock);
527                         ldlm_lock_decref_internal_nolock(lock, mode);
528                         ldlm_lock_destroy_nolock(lock);
529                 }
530                 unlock_res_and_lock(lock);
531         } else {
532                 ldlm_lock_decref_internal(lock, mode);
533         }
534 }
535
536 /**
537  * Finishing portion of client lock enqueue code.
538  *
539  * Called after receiving reply from server.
540  */
541 int ldlm_cli_enqueue_fini(struct obd_export *exp, struct ptlrpc_request *req,
542                           enum ldlm_type type, __u8 with_policy,
543                           enum ldlm_mode mode, __u64 *flags, void *lvb,
544                           __u32 lvb_len, struct lustre_handle *lockh, int rc)
545 {
546         struct ldlm_namespace *ns = exp->exp_obd->obd_namespace;
547         int is_replay = *flags & LDLM_FL_REPLAY;
548         struct ldlm_lock *lock;
549         struct ldlm_reply *reply;
550         int cleanup_phase = 1;
551         ENTRY;
552
553         lock = ldlm_handle2lock(lockh);
554         /* ldlm_cli_enqueue is holding a reference on this lock. */
555         if (!lock) {
556                 LASSERT(type == LDLM_FLOCK);
557                 RETURN(-ENOLCK);
558         }
559
560         LASSERTF(ergo(lvb_len != 0, lvb_len == lock->l_lvb_len),
561                  "lvb_len = %d, l_lvb_len = %d\n", lvb_len, lock->l_lvb_len);
562
563         if (rc != ELDLM_OK) {
564                 LASSERT(!is_replay);
565                 LDLM_DEBUG(lock, "client-side enqueue END (%s)",
566                            rc == ELDLM_LOCK_ABORTED ? "ABORTED" : "FAILED");
567
568                 if (rc != ELDLM_LOCK_ABORTED)
569                         GOTO(cleanup, rc);
570         }
571
572         /* Before we return, swab the reply */
573         reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
574         if (reply == NULL)
575                 GOTO(cleanup, rc = -EPROTO);
576
577         if (lvb_len > 0) {
578                 int size = 0;
579
580                 size = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
581                                             RCL_SERVER);
582                 if (size < 0) {
583                         LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", size);
584                         GOTO(cleanup, rc = size);
585                 } else if (unlikely(size > lvb_len)) {
586                         LDLM_ERROR(lock, "Replied LVB is larger than "
587                                    "expectation, expected = %d, replied = %d",
588                                    lvb_len, size);
589                         GOTO(cleanup, rc = -EINVAL);
590                 }
591                 lvb_len = size;
592         }
593
594         if (rc == ELDLM_LOCK_ABORTED) {
595                 if (lvb_len > 0 && lvb != NULL)
596                         rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
597                                            lvb, lvb_len);
598                 GOTO(cleanup, rc = rc ? : ELDLM_LOCK_ABORTED);
599         }
600
601         /* lock enqueued on the server */
602         cleanup_phase = 0;
603
604         lock_res_and_lock(lock);
605         /* Key change rehash lock in per-export hash with new key */
606         if (exp->exp_lock_hash) {
607                 /* In the function below, .hs_keycmp resolves to
608                  * ldlm_export_lock_keycmp() */
609                 /* coverity[overrun-buffer-val] */
610                 cfs_hash_rehash_key(exp->exp_lock_hash,
611                                     &lock->l_remote_handle,
612                                     &reply->lock_handle,
613                                     &lock->l_exp_hash);
614         } else {
615                 lock->l_remote_handle = reply->lock_handle;
616         }
617
618         *flags = ldlm_flags_from_wire(reply->lock_flags);
619         lock->l_flags |= ldlm_flags_from_wire(reply->lock_flags &
620                                               LDLM_FL_INHERIT_MASK);
621         unlock_res_and_lock(lock);
622
623         CDEBUG(D_INFO, "local: %p, remote cookie: "LPX64", flags: "LPX64"\n",
624                lock, reply->lock_handle.cookie, *flags);
625
626         /* If enqueue returned a blocked lock but the completion handler has
627          * already run, then it fixed up the resource and we don't need to do it
628          * again. */
629         if ((*flags) & LDLM_FL_LOCK_CHANGED) {
630                 int newmode = reply->lock_desc.l_req_mode;
631                 LASSERT(!is_replay);
632                 if (newmode && newmode != lock->l_req_mode) {
633                         LDLM_DEBUG(lock, "server returned different mode %s",
634                                    ldlm_lockname[newmode]);
635                         lock->l_req_mode = newmode;
636                 }
637
638                 if (!ldlm_res_eq(&reply->lock_desc.l_resource.lr_name,
639                                  &lock->l_resource->lr_name)) {
640                         CDEBUG(D_INFO, "remote intent success, locking "DLDLMRES
641                                        " instead of "DLDLMRES"\n",
642                                PLDLMRES(&reply->lock_desc.l_resource),
643                                PLDLMRES(lock->l_resource));
644
645                         rc = ldlm_lock_change_resource(ns, lock,
646                                         &reply->lock_desc.l_resource.lr_name);
647                         if (rc || lock->l_resource == NULL)
648                                 GOTO(cleanup, rc = -ENOMEM);
649                         LDLM_DEBUG(lock, "client-side enqueue, new resource");
650                 }
651                 if (with_policy)
652                         if (!(type == LDLM_IBITS &&
653                               !(exp_connect_flags(exp) & OBD_CONNECT_IBITS)))
654                                 /* We assume lock type cannot change on server*/
655                                 ldlm_convert_policy_to_local(exp,
656                                                 lock->l_resource->lr_type,
657                                                 &reply->lock_desc.l_policy_data,
658                                                 &lock->l_policy_data);
659                 if (type != LDLM_PLAIN)
660                         LDLM_DEBUG(lock,"client-side enqueue, new policy data");
661         }
662
663         if ((*flags) & LDLM_FL_AST_SENT) {
664                 lock_res_and_lock(lock);
665                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
666                 unlock_res_and_lock(lock);
667                 LDLM_DEBUG(lock, "enqueue reply includes blocking AST");
668         }
669
670         /* If the lock has already been granted by a completion AST, don't
671          * clobber the LVB with an older one. */
672         if (lvb_len > 0) {
673                 /* We must lock or a racing completion might update lvb without
674                  * letting us know and we'll clobber the correct value.
675                  * Cannot unlock after the check either, a that still leaves
676                  * a tiny window for completion to get in */
677                 lock_res_and_lock(lock);
678                 if (lock->l_req_mode != lock->l_granted_mode)
679                         rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_SERVER,
680                                            lock->l_lvb_data, lvb_len);
681                 unlock_res_and_lock(lock);
682                 if (rc < 0) {
683                         cleanup_phase = 1;
684                         GOTO(cleanup, rc);
685                 }
686         }
687
688         if (!is_replay) {
689                 rc = ldlm_lock_enqueue(ns, &lock, NULL, flags);
690                 if (lock->l_completion_ast != NULL) {
691                         int err = lock->l_completion_ast(lock, *flags, NULL);
692                         if (!rc)
693                                 rc = err;
694                         if (rc)
695                                 cleanup_phase = 1;
696                 }
697         }
698
699         if (lvb_len > 0 && lvb != NULL) {
700                 /* Copy the LVB here, and not earlier, because the completion
701                  * AST (if any) can override what we got in the reply */
702                 memcpy(lvb, lock->l_lvb_data, lvb_len);
703         }
704
705         LDLM_DEBUG(lock, "client-side enqueue END");
706         EXIT;
707 cleanup:
708         if (cleanup_phase == 1 && rc)
709                 failed_lock_cleanup(ns, lock, mode);
710         /* Put lock 2 times, the second reference is held by ldlm_cli_enqueue */
711         LDLM_LOCK_PUT(lock);
712         LDLM_LOCK_RELEASE(lock);
713         return rc;
714 }
715 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
716
717 /**
718  * Estimate number of lock handles that would fit into request of given
719  * size.  PAGE_SIZE-512 is to allow TCP/IP and LNET headers to fit into
720  * a single page on the send/receive side. XXX: 512 should be changed to
721  * more adequate value.
722  */
723 static inline int ldlm_req_handles_avail(int req_size, int off)
724 {
725         int avail;
726
727         avail = min_t(int, LDLM_MAXREQSIZE, PAGE_CACHE_SIZE - 512) - req_size;
728         if (likely(avail >= 0))
729                 avail /= (int)sizeof(struct lustre_handle);
730         else
731                 avail = 0;
732         avail += LDLM_LOCKREQ_HANDLES - off;
733
734         return avail;
735 }
736
737 static inline int ldlm_capsule_handles_avail(struct req_capsule *pill,
738                                              enum req_location loc,
739                                              int off)
740 {
741         __u32 size = req_capsule_msg_size(pill, loc);
742         return ldlm_req_handles_avail(size, off);
743 }
744
745 static inline int ldlm_format_handles_avail(struct obd_import *imp,
746                                             const struct req_format *fmt,
747                                             enum req_location loc, int off)
748 {
749         __u32 size = req_capsule_fmt_size(imp->imp_msg_magic, fmt, loc);
750         return ldlm_req_handles_avail(size, off);
751 }
752
753 /**
754  * Cancel LRU locks and pack them into the enqueue request. Pack there the given
755  * \a count locks in \a cancels.
756  *
757  * This is to be called by functions preparing their own requests that
758  * might contain lists of locks to cancel in addition to actual operation
759  * that needs to be performed.
760  */
761 int ldlm_prep_elc_req(struct obd_export *exp, struct ptlrpc_request *req,
762                       int version, int opc, int canceloff,
763                       struct list_head *cancels, int count)
764         {
765         struct ldlm_namespace   *ns = exp->exp_obd->obd_namespace;
766         struct req_capsule      *pill = &req->rq_pill;
767         struct ldlm_request     *dlm = NULL;
768         struct list_head        head = LIST_HEAD_INIT(head);
769         enum ldlm_lru_flags lru_flags;
770         int avail, to_free, pack = 0;
771         int rc;
772         ENTRY;
773
774         if (cancels == NULL)
775                 cancels = &head;
776         if (ns_connect_cancelset(ns)) {
777                 /* Estimate the amount of available space in the request. */
778                 req_capsule_filled_sizes(pill, RCL_CLIENT);
779                 avail = ldlm_capsule_handles_avail(pill, RCL_CLIENT, canceloff);
780
781                 lru_flags = ns_connect_lru_resize(ns) ?
782                         LDLM_LRU_FLAG_LRUR_NO_WAIT : LDLM_LRU_FLAG_AGED;
783                 to_free = !ns_connect_lru_resize(ns) &&
784                         opc == LDLM_ENQUEUE ? 1 : 0;
785
786                 /* Cancel LRU locks here _only_ if the server supports
787                  * EARLY_CANCEL. Otherwise we have to send extra CANCEL
788                  * RPC, which will make us slower. */
789                 if (avail > count)
790                         count += ldlm_cancel_lru_local(ns, cancels, to_free,
791                                                        avail - count, 0,
792                                                        lru_flags);
793                 if (avail > count)
794                         pack = count;
795                 else
796                         pack = avail;
797                 req_capsule_set_size(pill, &RMF_DLM_REQ, RCL_CLIENT,
798                                      ldlm_request_bufsize(pack, opc));
799         }
800
801         rc = ptlrpc_request_pack(req, version, opc);
802         if (rc) {
803                 ldlm_lock_list_put(cancels, l_bl_ast, count);
804                 RETURN(rc);
805         }
806
807         if (ns_connect_cancelset(ns)) {
808                 if (canceloff) {
809                         dlm = req_capsule_client_get(pill, &RMF_DLM_REQ);
810                         LASSERT(dlm);
811                         /* Skip first lock handler in ldlm_request_pack(),
812                          * this method will increment @lock_count according
813                          * to the lock handle amount actually written to
814                          * the buffer. */
815                         dlm->lock_count = canceloff;
816                 }
817                 /* Pack into the request @pack lock handles. */
818                 ldlm_cli_cancel_list(cancels, pack, req, 0);
819                 /* Prepare and send separate cancel RPC for others. */
820                 ldlm_cli_cancel_list(cancels, count - pack, NULL, 0);
821         } else {
822                 ldlm_lock_list_put(cancels, l_bl_ast, count);
823         }
824         RETURN(0);
825 }
826 EXPORT_SYMBOL(ldlm_prep_elc_req);
827
828 int ldlm_prep_enqueue_req(struct obd_export *exp, struct ptlrpc_request *req,
829                           struct list_head *cancels, int count)
830 {
831         return ldlm_prep_elc_req(exp, req, LUSTRE_DLM_VERSION, LDLM_ENQUEUE,
832                                  LDLM_ENQUEUE_CANCEL_OFF, cancels, count);
833 }
834 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
835
836 struct ptlrpc_request *ldlm_enqueue_pack(struct obd_export *exp, int lvb_len)
837 {
838         struct ptlrpc_request *req;
839         int rc;
840         ENTRY;
841
842         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LDLM_ENQUEUE);
843         if (req == NULL)
844                 RETURN(ERR_PTR(-ENOMEM));
845
846         rc = ldlm_prep_enqueue_req(exp, req, NULL, 0);
847         if (rc) {
848                 ptlrpc_request_free(req);
849                 RETURN(ERR_PTR(rc));
850         }
851
852         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, lvb_len);
853         ptlrpc_request_set_replen(req);
854         RETURN(req);
855 }
856 EXPORT_SYMBOL(ldlm_enqueue_pack);
857
858 /**
859  * Client-side lock enqueue.
860  *
861  * If a request has some specific initialisation it is passed in \a reqp,
862  * otherwise it is created in ldlm_cli_enqueue.
863  *
864  * Supports sync and async requests, pass \a async flag accordingly. If a
865  * request was created in ldlm_cli_enqueue and it is the async request,
866  * pass it to the caller in \a reqp.
867  */
868 int ldlm_cli_enqueue(struct obd_export *exp, struct ptlrpc_request **reqp,
869                      struct ldlm_enqueue_info *einfo,
870                      const struct ldlm_res_id *res_id,
871                      union ldlm_policy_data const *policy, __u64 *flags,
872                      void *lvb, __u32 lvb_len, enum lvb_type lvb_type,
873                      struct lustre_handle *lockh, int async)
874 {
875         struct ldlm_namespace *ns;
876         struct ldlm_lock      *lock;
877         struct ldlm_request   *body;
878         int                    is_replay = *flags & LDLM_FL_REPLAY;
879         int                    req_passed_in = 1;
880         int                    rc, err;
881         struct ptlrpc_request *req;
882         ENTRY;
883
884         LASSERT(exp != NULL);
885
886         ns = exp->exp_obd->obd_namespace;
887
888         /* If we're replaying this lock, just check some invariants.
889          * If we're creating a new lock, get everything all setup nice. */
890         if (is_replay) {
891                 lock = ldlm_handle2lock_long(lockh, 0);
892                 LASSERT(lock != NULL);
893                 LDLM_DEBUG(lock, "client-side enqueue START");
894                 LASSERT(exp == lock->l_conn_export);
895         } else {
896                 const struct ldlm_callback_suite cbs = {
897                         .lcs_completion = einfo->ei_cb_cp,
898                         .lcs_blocking   = einfo->ei_cb_bl,
899                         .lcs_glimpse    = einfo->ei_cb_gl
900                 };
901                 lock = ldlm_lock_create(ns, res_id, einfo->ei_type,
902                                         einfo->ei_mode, &cbs, einfo->ei_cbdata,
903                                         lvb_len, lvb_type);
904                 if (IS_ERR(lock))
905                         RETURN(PTR_ERR(lock));
906                 /* for the local lock, add the reference */
907                 ldlm_lock_addref_internal(lock, einfo->ei_mode);
908                 ldlm_lock2handle(lock, lockh);
909                 if (policy != NULL)
910                         lock->l_policy_data = *policy;
911
912                 if (einfo->ei_type == LDLM_EXTENT) {
913                         /* extent lock without policy is a bug */
914                         if (policy == NULL)
915                                 LBUG();
916
917                         lock->l_req_extent = policy->l_extent;
918                 }
919                 LDLM_DEBUG(lock, "client-side enqueue START, flags "LPX64"\n",
920                            *flags);
921         }
922
923         lock->l_conn_export = exp;
924         lock->l_export = NULL;
925         lock->l_blocking_ast = einfo->ei_cb_bl;
926         lock->l_flags |= (*flags & (LDLM_FL_NO_LRU | LDLM_FL_EXCL));
927         lock->l_last_activity = cfs_time_current_sec();
928
929         /* lock not sent to server yet */
930
931         if (reqp == NULL || *reqp == NULL) {
932                 req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
933                                                 &RQF_LDLM_ENQUEUE,
934                                                 LUSTRE_DLM_VERSION,
935                                                 LDLM_ENQUEUE);
936                 if (req == NULL) {
937                         failed_lock_cleanup(ns, lock, einfo->ei_mode);
938                         LDLM_LOCK_RELEASE(lock);
939                         RETURN(-ENOMEM);
940                 }
941                 req_passed_in = 0;
942                 if (reqp)
943                         *reqp = req;
944         } else {
945                 int len;
946
947                 req = *reqp;
948                 len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ,
949                                            RCL_CLIENT);
950                 LASSERTF(len >= sizeof(*body), "buflen[%d] = %d, not %d\n",
951                          DLM_LOCKREQ_OFF, len, (int)sizeof(*body));
952         }
953
954         /* Dump lock data into the request buffer */
955         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
956         ldlm_lock2desc(lock, &body->lock_desc);
957         body->lock_flags = ldlm_flags_to_wire(*flags);
958         body->lock_handle[0] = *lockh;
959
960         /* Continue as normal. */
961         if (!req_passed_in) {
962                 if (lvb_len > 0)
963                         req_capsule_extend(&req->rq_pill,
964                                            &RQF_LDLM_ENQUEUE_LVB);
965                 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
966                                      lvb_len);
967                 ptlrpc_request_set_replen(req);
968         }
969
970         if (async) {
971                 LASSERT(reqp != NULL);
972                 RETURN(0);
973         }
974
975         LDLM_DEBUG(lock, "sending request");
976
977         rc = ptlrpc_queue_wait(req);
978
979         err = ldlm_cli_enqueue_fini(exp, req, einfo->ei_type, policy ? 1 : 0,
980                                     einfo->ei_mode, flags, lvb, lvb_len,
981                                     lockh, rc);
982
983         /* If ldlm_cli_enqueue_fini did not find the lock, we need to free
984          * one reference that we took */
985         if (err == -ENOLCK)
986                 LDLM_LOCK_RELEASE(lock);
987         else
988                 rc = err;
989
990         if (!req_passed_in && req != NULL) {
991                 ptlrpc_req_finished(req);
992                 if (reqp)
993                         *reqp = NULL;
994         }
995
996         RETURN(rc);
997 }
998 EXPORT_SYMBOL(ldlm_cli_enqueue);
999
1000 static int ldlm_cli_convert_local(struct ldlm_lock *lock, int new_mode,
1001                                   __u32 *flags)
1002 {
1003         struct ldlm_resource *res;
1004         int rc;
1005         ENTRY;
1006         if (ns_is_client(ldlm_lock_to_ns(lock))) {
1007                 CERROR("Trying to cancel local lock\n");
1008                 LBUG();
1009         }
1010         LDLM_DEBUG(lock, "client-side local convert");
1011
1012         res = ldlm_lock_convert(lock, new_mode, flags);
1013         if (res) {
1014                 ldlm_reprocess_all(res);
1015                 rc = 0;
1016         } else {
1017                 rc = LUSTRE_EDEADLK;
1018         }
1019         LDLM_DEBUG(lock, "client-side local convert handler END");
1020         LDLM_LOCK_PUT(lock);
1021         RETURN(rc);
1022 }
1023
1024 /* FIXME: one of ldlm_cli_convert or the server side should reject attempted
1025  * conversion of locks which are on the waiting or converting queue */
1026 /* Caller of this code is supposed to take care of lock readers/writers
1027    accounting */
1028 int ldlm_cli_convert(struct lustre_handle *lockh, int new_mode, __u32 *flags)
1029 {
1030         struct ldlm_request   *body;
1031         struct ldlm_reply     *reply;
1032         struct ldlm_lock      *lock;
1033         struct ldlm_resource  *res;
1034         struct ptlrpc_request *req;
1035         int                    rc;
1036         ENTRY;
1037
1038         lock = ldlm_handle2lock(lockh);
1039         if (!lock) {
1040                 LBUG();
1041                 RETURN(-EINVAL);
1042         }
1043         *flags = 0;
1044
1045         if (lock->l_conn_export == NULL)
1046                 RETURN(ldlm_cli_convert_local(lock, new_mode, flags));
1047
1048         LDLM_DEBUG(lock, "client-side convert");
1049
1050         req = ptlrpc_request_alloc_pack(class_exp2cliimp(lock->l_conn_export),
1051                                         &RQF_LDLM_CONVERT, LUSTRE_DLM_VERSION,
1052                                         LDLM_CONVERT);
1053         if (req == NULL) {
1054                 LDLM_LOCK_PUT(lock);
1055                 RETURN(-ENOMEM);
1056         }
1057
1058         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1059         body->lock_handle[0] = lock->l_remote_handle;
1060
1061         body->lock_desc.l_req_mode = new_mode;
1062         body->lock_flags = ldlm_flags_to_wire(*flags);
1063
1064
1065         ptlrpc_request_set_replen(req);
1066         rc = ptlrpc_queue_wait(req);
1067         if (rc != ELDLM_OK)
1068                 GOTO(out, rc);
1069
1070         reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1071         if (reply == NULL)
1072                 GOTO(out, rc = -EPROTO);
1073
1074         if (req->rq_status)
1075                 GOTO(out, rc = req->rq_status);
1076
1077         res = ldlm_lock_convert(lock, new_mode, &reply->lock_flags);
1078         if (res != NULL) {
1079                 ldlm_reprocess_all(res);
1080                 /* Go to sleep until the lock is granted. */
1081                 /* FIXME: or cancelled. */
1082                 if (lock->l_completion_ast) {
1083                         rc = lock->l_completion_ast(lock, LDLM_FL_WAIT_NOREPROC,
1084                                                     NULL);
1085                         if (rc)
1086                                 GOTO(out, rc);
1087                 }
1088         } else {
1089                 rc = LUSTRE_EDEADLK;
1090         }
1091         EXIT;
1092  out:
1093         LDLM_LOCK_PUT(lock);
1094         ptlrpc_req_finished(req);
1095         return rc;
1096 }
1097
1098 /**
1099  * Cancel locks locally.
1100  * Returns:
1101  * \retval LDLM_FL_LOCAL_ONLY if there is no need for a CANCEL RPC to the server
1102  * \retval LDLM_FL_CANCELING otherwise;
1103  * \retval LDLM_FL_BL_AST if there is a need for a separate CANCEL RPC.
1104  */
1105 static __u64 ldlm_cli_cancel_local(struct ldlm_lock *lock)
1106 {
1107         __u64 rc = LDLM_FL_LOCAL_ONLY;
1108         ENTRY;
1109
1110         if (lock->l_conn_export) {
1111                 bool local_only;
1112
1113                 LDLM_DEBUG(lock, "client-side cancel");
1114                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL_LOCAL,
1115                                  cfs_fail_val);
1116
1117                 /* Set this flag to prevent others from getting new references*/
1118                 lock_res_and_lock(lock);
1119                 ldlm_set_cbpending(lock);
1120                 local_only = !!(lock->l_flags &
1121                                 (LDLM_FL_LOCAL_ONLY|LDLM_FL_CANCEL_ON_BLOCK));
1122                 ldlm_cancel_callback(lock);
1123                 rc = (ldlm_is_bl_ast(lock)) ?
1124                         LDLM_FL_BL_AST : LDLM_FL_CANCELING;
1125                 unlock_res_and_lock(lock);
1126
1127                 if (local_only) {
1128                         CDEBUG(D_DLMTRACE, "not sending request (at caller's "
1129                                "instruction)\n");
1130                         rc = LDLM_FL_LOCAL_ONLY;
1131                 }
1132                 ldlm_lock_cancel(lock);
1133         } else {
1134                 if (ns_is_client(ldlm_lock_to_ns(lock))) {
1135                         LDLM_ERROR(lock, "Trying to cancel local lock");
1136                         LBUG();
1137                 }
1138                 LDLM_DEBUG(lock, "server-side local cancel");
1139                 ldlm_lock_cancel(lock);
1140                 ldlm_reprocess_all(lock->l_resource);
1141         }
1142
1143         RETURN(rc);
1144 }
1145
1146 /**
1147  * Pack \a count locks in \a head into ldlm_request buffer of request \a req.
1148  */
1149 static void ldlm_cancel_pack(struct ptlrpc_request *req,
1150                              struct list_head *head, int count)
1151 {
1152         struct ldlm_request *dlm;
1153         struct ldlm_lock *lock;
1154         int max, packed = 0;
1155         ENTRY;
1156
1157         dlm = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1158         LASSERT(dlm != NULL);
1159
1160         /* Check the room in the request buffer. */
1161         max = req_capsule_get_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT) -
1162                 sizeof(struct ldlm_request);
1163         max /= sizeof(struct lustre_handle);
1164         max += LDLM_LOCKREQ_HANDLES;
1165         LASSERT(max >= dlm->lock_count + count);
1166
1167         /* XXX: it would be better to pack lock handles grouped by resource.
1168          * so that the server cancel would call filter_lvbo_update() less
1169          * frequently. */
1170         list_for_each_entry(lock, head, l_bl_ast) {
1171                 if (!count--)
1172                         break;
1173                 LASSERT(lock->l_conn_export);
1174                 /* Pack the lock handle to the given request buffer. */
1175                 LDLM_DEBUG(lock, "packing");
1176                 dlm->lock_handle[dlm->lock_count++] = lock->l_remote_handle;
1177                 packed++;
1178         }
1179         CDEBUG(D_DLMTRACE, "%d locks packed\n", packed);
1180         EXIT;
1181 }
1182
1183 /**
1184  * Prepare and send a batched cancel RPC. It will include \a count lock
1185  * handles of locks given in \a cancels list. */
1186 int ldlm_cli_cancel_req(struct obd_export *exp, struct list_head *cancels,
1187                         int count, enum ldlm_cancel_flags flags)
1188 {
1189         struct ptlrpc_request *req = NULL;
1190         struct obd_import *imp;
1191         int free, sent = 0;
1192         int rc = 0;
1193         ENTRY;
1194
1195         LASSERT(exp != NULL);
1196         LASSERT(count > 0);
1197
1198         CFS_FAIL_TIMEOUT(OBD_FAIL_LDLM_PAUSE_CANCEL, cfs_fail_val);
1199
1200         if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_RACE))
1201                 RETURN(count);
1202
1203         free = ldlm_format_handles_avail(class_exp2cliimp(exp),
1204                                          &RQF_LDLM_CANCEL, RCL_CLIENT, 0);
1205         if (count > free)
1206                 count = free;
1207
1208         while (1) {
1209                 imp = class_exp2cliimp(exp);
1210                 if (imp == NULL || imp->imp_invalid) {
1211                         CDEBUG(D_DLMTRACE,
1212                                "skipping cancel on invalid import %p\n", imp);
1213                         RETURN(count);
1214                 }
1215
1216                 req = ptlrpc_request_alloc(imp, &RQF_LDLM_CANCEL);
1217                 if (req == NULL)
1218                         GOTO(out, rc = -ENOMEM);
1219
1220                 req_capsule_filled_sizes(&req->rq_pill, RCL_CLIENT);
1221                 req_capsule_set_size(&req->rq_pill, &RMF_DLM_REQ, RCL_CLIENT,
1222                                      ldlm_request_bufsize(count, LDLM_CANCEL));
1223
1224                 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CANCEL);
1225                 if (rc) {
1226                         ptlrpc_request_free(req);
1227                         GOTO(out, rc);
1228                 }
1229
1230                 /* If OSP want cancel cross-MDT lock, let's not block it in
1231                  * in recovery, otherwise the lock will not released, if
1232                  * the remote target is also in recovery, and it also need
1233                  * this lock, it might cause deadlock. */
1234                 if (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS &&
1235                     exp->exp_obd->obd_lu_dev != NULL &&
1236                     exp->exp_obd->obd_lu_dev->ld_site != NULL) {
1237                         struct lu_device *top_dev;
1238
1239                         top_dev = exp->exp_obd->obd_lu_dev->ld_site->ls_top_dev;
1240                         if (top_dev != NULL &&
1241                             top_dev->ld_obd->obd_recovering)
1242                                 req->rq_allow_replay = 1;
1243                 }
1244
1245                 req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
1246                 req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
1247                 ptlrpc_at_set_req_timeout(req);
1248
1249                 ldlm_cancel_pack(req, cancels, count);
1250
1251                 ptlrpc_request_set_replen(req);
1252                 if (flags & LCF_ASYNC) {
1253                         ptlrpcd_add_req(req);
1254                         sent = count;
1255                         GOTO(out, 0);
1256                 }
1257
1258                 rc = ptlrpc_queue_wait(req);
1259                 if (rc == LUSTRE_ESTALE) {
1260                         CDEBUG(D_DLMTRACE, "client/server (nid %s) "
1261                                "out of sync -- not fatal\n",
1262                                libcfs_nid2str(req->rq_import->
1263                                               imp_connection->c_peer.nid));
1264                         rc = 0;
1265                 } else if (rc == -ETIMEDOUT && /* check there was no reconnect*/
1266                            req->rq_import_generation == imp->imp_generation) {
1267                         ptlrpc_req_finished(req);
1268                         continue;
1269                 } else if (rc != ELDLM_OK) {
1270                         /* -ESHUTDOWN is common on umount */
1271                         CDEBUG_LIMIT(rc == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1272                                      "Got rc %d from cancel RPC: "
1273                                      "canceling anyway\n", rc);
1274                         break;
1275                 }
1276                 sent = count;
1277                 break;
1278         }
1279
1280         ptlrpc_req_finished(req);
1281         EXIT;
1282 out:
1283         return sent ? sent : rc;
1284 }
1285
1286 static inline struct ldlm_pool *ldlm_imp2pl(struct obd_import *imp)
1287 {
1288         LASSERT(imp != NULL);
1289         return &imp->imp_obd->obd_namespace->ns_pool;
1290 }
1291
1292 /**
1293  * Update client's OBD pool related fields with new SLV and Limit from \a req.
1294  */
1295 int ldlm_cli_update_pool(struct ptlrpc_request *req)
1296 {
1297         struct obd_device *obd;
1298         __u64 new_slv;
1299         __u32 new_limit;
1300         ENTRY;
1301         if (unlikely(!req->rq_import || !req->rq_import->imp_obd ||
1302                      !imp_connect_lru_resize(req->rq_import)))
1303         {
1304                 /*
1305                  * Do nothing for corner cases.
1306                  */
1307                 RETURN(0);
1308         }
1309
1310         /* In some cases RPC may contain SLV and limit zeroed out. This
1311          * is the case when server does not support LRU resize feature.
1312          * This is also possible in some recovery cases when server-side
1313          * reqs have no reference to the OBD export and thus access to
1314          * server-side namespace is not possible. */
1315         if (lustre_msg_get_slv(req->rq_repmsg) == 0 ||
1316             lustre_msg_get_limit(req->rq_repmsg) == 0) {
1317                 DEBUG_REQ(D_HA, req, "Zero SLV or Limit found "
1318                           "(SLV: "LPU64", Limit: %u)",
1319                           lustre_msg_get_slv(req->rq_repmsg),
1320                           lustre_msg_get_limit(req->rq_repmsg));
1321                 RETURN(0);
1322         }
1323
1324         new_limit = lustre_msg_get_limit(req->rq_repmsg);
1325         new_slv = lustre_msg_get_slv(req->rq_repmsg);
1326         obd = req->rq_import->imp_obd;
1327
1328         /* Set new SLV and limit in OBD fields to make them accessible
1329          * to the pool thread. We do not access obd_namespace and pool
1330          * directly here as there is no reliable way to make sure that
1331          * they are still alive at cleanup time. Evil races are possible
1332          * which may cause Oops at that time. */
1333         write_lock(&obd->obd_pool_lock);
1334         obd->obd_pool_slv = new_slv;
1335         obd->obd_pool_limit = new_limit;
1336         write_unlock(&obd->obd_pool_lock);
1337
1338         RETURN(0);
1339 }
1340
1341 /**
1342  * Client side lock cancel.
1343  *
1344  * Lock must not have any readers or writers by this time.
1345  */
1346 int ldlm_cli_cancel(struct lustre_handle *lockh,
1347                     enum ldlm_cancel_flags cancel_flags)
1348 {
1349         struct obd_export *exp;
1350         enum ldlm_lru_flags lru_flags;
1351         int avail, count = 1;
1352         __u64 rc = 0;
1353         struct ldlm_namespace *ns;
1354         struct ldlm_lock *lock;
1355         struct list_head cancels = LIST_HEAD_INIT(cancels);
1356         ENTRY;
1357
1358         lock = ldlm_handle2lock_long(lockh, 0);
1359         if (lock == NULL) {
1360                 LDLM_DEBUG_NOLOCK("lock is already being destroyed");
1361                 RETURN(0);
1362         }
1363
1364         lock_res_and_lock(lock);
1365         /* Lock is being canceled and the caller doesn't want to wait */
1366         if (ldlm_is_canceling(lock) && (cancel_flags & LCF_ASYNC)) {
1367                 unlock_res_and_lock(lock);
1368                 LDLM_LOCK_RELEASE(lock);
1369                 RETURN(0);
1370         }
1371
1372         ldlm_set_canceling(lock);
1373         unlock_res_and_lock(lock);
1374
1375         rc = ldlm_cli_cancel_local(lock);
1376         if (rc == LDLM_FL_LOCAL_ONLY || cancel_flags & LCF_LOCAL) {
1377                 LDLM_LOCK_RELEASE(lock);
1378                 RETURN(0);
1379         }
1380         /* Even if the lock is marked as LDLM_FL_BL_AST, this is a LDLM_CANCEL
1381          * RPC which goes to canceld portal, so we can cancel other LRU locks
1382          * here and send them all as one LDLM_CANCEL RPC. */
1383         LASSERT(list_empty(&lock->l_bl_ast));
1384         list_add(&lock->l_bl_ast, &cancels);
1385
1386         exp = lock->l_conn_export;
1387         if (exp_connect_cancelset(exp)) {
1388                 avail = ldlm_format_handles_avail(class_exp2cliimp(exp),
1389                                                   &RQF_LDLM_CANCEL,
1390                                                   RCL_CLIENT, 0);
1391                 LASSERT(avail > 0);
1392
1393                 ns = ldlm_lock_to_ns(lock);
1394                 lru_flags = ns_connect_lru_resize(ns) ?
1395                         LDLM_LRU_FLAG_LRUR : LDLM_LRU_FLAG_AGED;
1396                 count += ldlm_cancel_lru_local(ns, &cancels, 0, avail - 1,
1397                                                LCF_BL_AST, lru_flags);
1398         }
1399         ldlm_cli_cancel_list(&cancels, count, NULL, cancel_flags);
1400         RETURN(0);
1401 }
1402 EXPORT_SYMBOL(ldlm_cli_cancel);
1403
1404 /**
1405  * Locally cancel up to \a count locks in list \a cancels.
1406  * Return the number of cancelled locks.
1407  */
1408 int ldlm_cli_cancel_list_local(struct list_head *cancels, int count,
1409                                enum ldlm_cancel_flags cancel_flags)
1410 {
1411         struct list_head head = LIST_HEAD_INIT(head);
1412         struct ldlm_lock *lock, *next;
1413         int left = 0, bl_ast = 0;
1414         __u64 rc;
1415
1416         left = count;
1417         list_for_each_entry_safe(lock, next, cancels, l_bl_ast) {
1418                 if (left-- == 0)
1419                         break;
1420
1421                 if (cancel_flags & LCF_LOCAL) {
1422                         rc = LDLM_FL_LOCAL_ONLY;
1423                         ldlm_lock_cancel(lock);
1424                 } else {
1425                         rc = ldlm_cli_cancel_local(lock);
1426                 }
1427                 /* Until we have compound requests and can send LDLM_CANCEL
1428                  * requests batched with generic RPCs, we need to send cancels
1429                  * with the LDLM_FL_BL_AST flag in a separate RPC from
1430                  * the one being generated now. */
1431                 if (!(cancel_flags & LCF_BL_AST) && (rc == LDLM_FL_BL_AST)) {
1432                         LDLM_DEBUG(lock, "Cancel lock separately");
1433                         list_del_init(&lock->l_bl_ast);
1434                         list_add(&lock->l_bl_ast, &head);
1435                         bl_ast++;
1436                         continue;
1437                 }
1438                 if (rc == LDLM_FL_LOCAL_ONLY) {
1439                         /* CANCEL RPC should not be sent to server. */
1440                         list_del_init(&lock->l_bl_ast);
1441                         LDLM_LOCK_RELEASE(lock);
1442                         count--;
1443                 }
1444         }
1445         if (bl_ast > 0) {
1446                 count -= bl_ast;
1447                 ldlm_cli_cancel_list(&head, bl_ast, NULL, 0);
1448         }
1449
1450         RETURN(count);
1451 }
1452
1453 /**
1454  * Cancel as many locks as possible w/o sending any RPCs (e.g. to write back
1455  * dirty data, to close a file, ...) or waiting for any RPCs in-flight (e.g.
1456  * readahead requests, ...)
1457  */
1458 static enum ldlm_policy_res
1459 ldlm_cancel_no_wait_policy(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1460                            int unused, int added, int count)
1461 {
1462         enum ldlm_policy_res result = LDLM_POLICY_CANCEL_LOCK;
1463
1464         /* don't check added & count since we want to process all locks
1465          * from unused list.
1466          * It's fine to not take lock to access lock->l_resource since
1467          * the lock has already been granted so it won't change. */
1468         switch (lock->l_resource->lr_type) {
1469                 case LDLM_EXTENT:
1470                 case LDLM_IBITS:
1471                         if (ns->ns_cancel != NULL && ns->ns_cancel(lock) != 0)
1472                                 break;
1473                 default:
1474                         result = LDLM_POLICY_SKIP_LOCK;
1475                         lock_res_and_lock(lock);
1476                         ldlm_set_skipped(lock);
1477                         unlock_res_and_lock(lock);
1478                         break;
1479         }
1480
1481         RETURN(result);
1482 }
1483
1484 /**
1485  * Callback function for LRU-resize policy. Decides whether to keep
1486  * \a lock in LRU for current \a LRU size \a unused, added in current
1487  * scan \a added and number of locks to be preferably canceled \a count.
1488  *
1489  * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1490  *
1491  * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1492  */
1493 static enum ldlm_policy_res ldlm_cancel_lrur_policy(struct ldlm_namespace *ns,
1494                                                     struct ldlm_lock *lock,
1495                                                     int unused, int added,
1496                                                     int count)
1497 {
1498         cfs_time_t cur = cfs_time_current();
1499         struct ldlm_pool *pl = &ns->ns_pool;
1500         __u64 slv, lvf, lv;
1501         cfs_time_t la;
1502
1503         /* Stop LRU processing when we reach past @count or have checked all
1504          * locks in LRU. */
1505         if (count && added >= count)
1506                 return LDLM_POLICY_KEEP_LOCK;
1507
1508         /* Despite of the LV, It doesn't make sense to keep the lock which
1509          * is unused for ns_max_age time. */
1510         if (cfs_time_after(cfs_time_current(),
1511                            cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1512                 return LDLM_POLICY_CANCEL_LOCK;
1513
1514         slv = ldlm_pool_get_slv(pl);
1515         lvf = ldlm_pool_get_lvf(pl);
1516         la = cfs_duration_sec(cfs_time_sub(cur,
1517                               lock->l_last_used));
1518         lv = lvf * la * unused;
1519
1520         /* Inform pool about current CLV to see it via proc. */
1521         ldlm_pool_set_clv(pl, lv);
1522
1523         /* Stop when SLV is not yet come from server or lv is smaller than
1524          * it is. */
1525         if (slv == 0 || lv < slv)
1526                 return LDLM_POLICY_KEEP_LOCK;
1527
1528         return LDLM_POLICY_CANCEL_LOCK;
1529 }
1530
1531 static enum ldlm_policy_res
1532 ldlm_cancel_lrur_no_wait_policy(struct ldlm_namespace *ns,
1533                                 struct ldlm_lock *lock,
1534                                 int unused, int added,
1535                                 int count)
1536 {
1537         enum ldlm_policy_res result;
1538
1539         result = ldlm_cancel_lrur_policy(ns, lock, unused, added, count);
1540         if (result == LDLM_POLICY_KEEP_LOCK)
1541                 return result;
1542
1543         return ldlm_cancel_no_wait_policy(ns, lock, unused, added, count);
1544 }
1545
1546 /**
1547  * Callback function for proc used policy. Makes decision whether to keep
1548  * \a lock in LRU for current \a LRU size \a unused, added in current scan \a
1549  * added and number of locks to be preferably canceled \a count.
1550  *
1551  * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1552  *
1553  * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1554  */
1555 static enum ldlm_policy_res ldlm_cancel_passed_policy(struct ldlm_namespace *ns,
1556                                                       struct ldlm_lock *lock,
1557                                                       int unused, int added,
1558                                                       int count)
1559 {
1560         /* Stop LRU processing when we reach past @count or have checked all
1561          * locks in LRU. */
1562         return (added >= count) ?
1563                 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1564 }
1565
1566 /**
1567  * Callback function for aged policy. Makes decision whether to keep \a lock in
1568  * LRU for current LRU size \a unused, added in current scan \a added and
1569  * number of locks to be preferably canceled \a count.
1570  *
1571  * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1572  *
1573  * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1574  */
1575 static enum ldlm_policy_res ldlm_cancel_aged_policy(struct ldlm_namespace *ns,
1576                                                     struct ldlm_lock *lock,
1577                                                     int unused, int added,
1578                                                     int count)
1579 {
1580         if ((added >= count) &&
1581             cfs_time_before(cfs_time_current(),
1582                             cfs_time_add(lock->l_last_used, ns->ns_max_age)))
1583                 return LDLM_POLICY_KEEP_LOCK;
1584
1585         return LDLM_POLICY_CANCEL_LOCK;
1586 }
1587
1588 /**
1589  * Callback function for default policy. Makes decision whether to keep \a lock
1590  * in LRU for current LRU size \a unused, added in current scan \a added and
1591  * number of locks to be preferably canceled \a count.
1592  *
1593  * \retval LDLM_POLICY_KEEP_LOCK keep lock in LRU in stop scanning
1594  *
1595  * \retval LDLM_POLICY_CANCEL_LOCK cancel lock from LRU
1596  */
1597 static
1598 enum ldlm_policy_res ldlm_cancel_default_policy(struct ldlm_namespace *ns,
1599                                                 struct ldlm_lock *lock,
1600                                                 int unused, int added,
1601                                                 int count)
1602 {
1603         /* Stop LRU processing when we reach past count or have checked all
1604          * locks in LRU. */
1605         return (added >= count) ?
1606                 LDLM_POLICY_KEEP_LOCK : LDLM_POLICY_CANCEL_LOCK;
1607 }
1608
1609 typedef enum ldlm_policy_res
1610 (*ldlm_cancel_lru_policy_t)(struct ldlm_namespace *ns, struct ldlm_lock *lock,
1611                             int unused, int added, int count);
1612
1613 static ldlm_cancel_lru_policy_t
1614 ldlm_cancel_lru_policy(struct ldlm_namespace *ns, enum ldlm_lru_flags lru_flags)
1615 {
1616         if (lru_flags & LDLM_LRU_FLAG_NO_WAIT)
1617                 return ldlm_cancel_no_wait_policy;
1618
1619         if (ns_connect_lru_resize(ns)) {
1620                 if (lru_flags & LDLM_LRU_FLAG_SHRINK)
1621                         /* We kill passed number of old locks. */
1622                         return ldlm_cancel_passed_policy;
1623                 if (lru_flags & LDLM_LRU_FLAG_LRUR)
1624                         return ldlm_cancel_lrur_policy;
1625                 if (lru_flags & LDLM_LRU_FLAG_PASSED)
1626                         return ldlm_cancel_passed_policy;
1627                 else if (lru_flags & LDLM_LRU_FLAG_LRUR_NO_WAIT)
1628                         return ldlm_cancel_lrur_no_wait_policy;
1629         } else {
1630                 if (lru_flags & LDLM_LRU_FLAG_AGED)
1631                         return ldlm_cancel_aged_policy;
1632         }
1633
1634         return ldlm_cancel_default_policy;
1635 }
1636
1637 /**
1638  * - Free space in LRU for \a count new locks,
1639  *   redundant unused locks are canceled locally;
1640  * - also cancel locally unused aged locks;
1641  * - do not cancel more than \a max locks;
1642  * - GET the found locks and add them into the \a cancels list.
1643  *
1644  * A client lock can be added to the l_bl_ast list only when it is
1645  * marked LDLM_FL_CANCELING. Otherwise, somebody is already doing
1646  * CANCEL.  There are the following use cases:
1647  * ldlm_cancel_resource_local(), ldlm_cancel_lru_local() and
1648  * ldlm_cli_cancel(), which check and set this flag properly. As any
1649  * attempt to cancel a lock rely on this flag, l_bl_ast list is accessed
1650  * later without any special locking.
1651  *
1652  * Calling policies for enabled LRU resize:
1653  * ----------------------------------------
1654  * flags & LDLM_LRU_FLAG_LRUR - use LRU resize policy (SLV from server) to
1655  *                              cancel not more than \a count locks;
1656  *
1657  * flags & LDLM_LRU_FLAG_PASSED - cancel \a count number of old locks (located
1658  *                              at the beginning of LRU list);
1659  *
1660  * flags & LDLM_LRU_FLAG_SHRINK - cancel not more than \a count locks according
1661  *                              to memory pressre policy function;
1662  *
1663  * flags & LDLM_LRU_FLAG_AGED - cancel \a count locks according to "aged policy"
1664  *
1665  * flags & LDLM_LRU_FLAG_NO_WAIT - cancel as many unused locks as possible
1666  *                              (typically before replaying locks) w/o
1667  *                              sending any RPCs or waiting for any
1668  *                              outstanding RPC to complete.
1669  */
1670 static int ldlm_prepare_lru_list(struct ldlm_namespace *ns,
1671                                  struct list_head *cancels, int count, int max,
1672                                  enum ldlm_lru_flags lru_flags)
1673 {
1674         ldlm_cancel_lru_policy_t pf;
1675         struct ldlm_lock *lock, *next;
1676         int added = 0, unused, remained;
1677         int no_wait = lru_flags & (LDLM_LRU_FLAG_NO_WAIT |
1678                                    LDLM_LRU_FLAG_LRUR_NO_WAIT);
1679         ENTRY;
1680
1681         spin_lock(&ns->ns_lock);
1682         unused = ns->ns_nr_unused;
1683         remained = unused;
1684
1685         if (!ns_connect_lru_resize(ns))
1686                 count += unused - ns->ns_max_unused;
1687
1688         pf = ldlm_cancel_lru_policy(ns, lru_flags);
1689         LASSERT(pf != NULL);
1690
1691         while (!list_empty(&ns->ns_unused_list)) {
1692                 enum ldlm_policy_res result;
1693                 cfs_time_t last_use = 0;
1694
1695                 /* all unused locks */
1696                 if (remained-- <= 0)
1697                         break;
1698
1699                 /* For any flags, stop scanning if @max is reached. */
1700                 if (max && added >= max)
1701                         break;
1702
1703                 list_for_each_entry_safe(lock, next, &ns->ns_unused_list,
1704                                          l_lru) {
1705                         /* No locks which got blocking requests. */
1706                         LASSERT(!ldlm_is_bl_ast(lock));
1707
1708                         if (no_wait && ldlm_is_skipped(lock))
1709                                 /* already processed */
1710                                 continue;
1711
1712                         last_use = lock->l_last_used;
1713                         if (last_use == cfs_time_current())
1714                                 continue;
1715
1716                         /* Somebody is already doing CANCEL. No need for this
1717                          * lock in LRU, do not traverse it again. */
1718                         if (!ldlm_is_canceling(lock))
1719                                 break;
1720
1721                         ldlm_lock_remove_from_lru_nolock(lock);
1722                 }
1723                 if (&lock->l_lru == &ns->ns_unused_list)
1724                         break;
1725
1726                 LDLM_LOCK_GET(lock);
1727                 spin_unlock(&ns->ns_lock);
1728                 lu_ref_add(&lock->l_reference, __FUNCTION__, current);
1729
1730                 /* Pass the lock through the policy filter and see if it
1731                  * should stay in LRU.
1732                  *
1733                  * Even for shrinker policy we stop scanning if
1734                  * we find a lock that should stay in the cache.
1735                  * We should take into account lock age anyway
1736                  * as a new lock is a valuable resource even if
1737                  * it has a low weight.
1738                  *
1739                  * That is, for shrinker policy we drop only
1740                  * old locks, but additionally choose them by
1741                  * their weight. Big extent locks will stay in
1742                  * the cache. */
1743                 result = pf(ns, lock, unused, added, count);
1744                 if (result == LDLM_POLICY_KEEP_LOCK) {
1745                         lu_ref_del(&lock->l_reference,
1746                                    __FUNCTION__, current);
1747                         LDLM_LOCK_RELEASE(lock);
1748                         spin_lock(&ns->ns_lock);
1749                         break;
1750                 }
1751                 if (result == LDLM_POLICY_SKIP_LOCK) {
1752                         lu_ref_del(&lock->l_reference,
1753                                    __func__, current);
1754                         LDLM_LOCK_RELEASE(lock);
1755                         spin_lock(&ns->ns_lock);
1756                         continue;
1757                 }
1758
1759                 lock_res_and_lock(lock);
1760                 /* Check flags again under the lock. */
1761                 if (ldlm_is_canceling(lock) ||
1762                     ldlm_lock_remove_from_lru_check(lock, last_use) == 0) {
1763                         /* Another thread is removing lock from LRU, or
1764                          * somebody is already doing CANCEL, or there
1765                          * is a blocking request which will send cancel
1766                          * by itself, or the lock is no longer unused or
1767                          * the lock has been used since the pf() call and
1768                          * pages could be put under it. */
1769                         unlock_res_and_lock(lock);
1770                         lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1771                         LDLM_LOCK_RELEASE(lock);
1772                         spin_lock(&ns->ns_lock);
1773                         continue;
1774                 }
1775                 LASSERT(!lock->l_readers && !lock->l_writers);
1776
1777                 /* If we have chosen to cancel this lock voluntarily, we
1778                  * better send cancel notification to server, so that it
1779                  * frees appropriate state. This might lead to a race
1780                  * where while we are doing cancel here, server is also
1781                  * silently cancelling this lock. */
1782                 ldlm_clear_cancel_on_block(lock);
1783
1784                 /* Setting the CBPENDING flag is a little misleading,
1785                  * but prevents an important race; namely, once
1786                  * CBPENDING is set, the lock can accumulate no more
1787                  * readers/writers. Since readers and writers are
1788                  * already zero here, ldlm_lock_decref() won't see
1789                  * this flag and call l_blocking_ast */
1790                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING;
1791
1792                 /* We can't re-add to l_lru as it confuses the
1793                  * refcounting in ldlm_lock_remove_from_lru() if an AST
1794                  * arrives after we drop lr_lock below. We use l_bl_ast
1795                  * and can't use l_pending_chain as it is used both on
1796                  * server and client nevertheless bug 5666 says it is
1797                  * used only on server */
1798                 LASSERT(list_empty(&lock->l_bl_ast));
1799                 list_add(&lock->l_bl_ast, cancels);
1800                 unlock_res_and_lock(lock);
1801                 lu_ref_del(&lock->l_reference, __FUNCTION__, current);
1802                 spin_lock(&ns->ns_lock);
1803                 added++;
1804                 unused--;
1805         }
1806         spin_unlock(&ns->ns_lock);
1807         RETURN(added);
1808 }
1809
1810 int ldlm_cancel_lru_local(struct ldlm_namespace *ns, struct list_head *cancels,
1811                           int count, int max,
1812                           enum ldlm_cancel_flags cancel_flags,
1813                           enum ldlm_lru_flags lru_flags)
1814 {
1815         int added;
1816
1817         added = ldlm_prepare_lru_list(ns, cancels, count, max, lru_flags);
1818         if (added <= 0)
1819                 return added;
1820
1821         return ldlm_cli_cancel_list_local(cancels, added, cancel_flags);
1822 }
1823
1824 /**
1825  * Cancel at least \a nr locks from given namespace LRU.
1826  *
1827  * When called with LCF_ASYNC the blocking callback will be handled
1828  * in a thread and this function will return after the thread has been
1829  * asked to call the callback.  When called with LCF_ASYNC the blocking
1830  * callback will be performed in this function.
1831  */
1832 int ldlm_cancel_lru(struct ldlm_namespace *ns, int nr,
1833                     enum ldlm_cancel_flags cancel_flags,
1834                     enum ldlm_lru_flags lru_flags)
1835 {
1836         struct list_head cancels = LIST_HEAD_INIT(cancels);
1837         int count, rc;
1838         ENTRY;
1839
1840         /* Just prepare the list of locks, do not actually cancel them yet.
1841          * Locks are cancelled later in a separate thread. */
1842         count = ldlm_prepare_lru_list(ns, &cancels, nr, 0, lru_flags);
1843         rc = ldlm_bl_to_thread_list(ns, NULL, &cancels, count, cancel_flags);
1844         if (rc == 0)
1845                 RETURN(count);
1846
1847         RETURN(0);
1848 }
1849
1850 /**
1851  * Find and cancel locally unused locks found on resource, matched to the
1852  * given policy, mode. GET the found locks and add them into the \a cancels
1853  * list.
1854  */
1855 int ldlm_cancel_resource_local(struct ldlm_resource *res,
1856                                struct list_head *cancels,
1857                                union ldlm_policy_data *policy,
1858                                enum ldlm_mode mode, __u64 lock_flags,
1859                                enum ldlm_cancel_flags cancel_flags,
1860                                void *opaque)
1861 {
1862         struct ldlm_lock *lock;
1863         int count = 0;
1864         ENTRY;
1865
1866         lock_res(res);
1867         list_for_each_entry(lock, &res->lr_granted, l_res_link) {
1868                 if (opaque != NULL && lock->l_ast_data != opaque) {
1869                         LDLM_ERROR(lock, "data %p doesn't match opaque %p",
1870                                    lock->l_ast_data, opaque);
1871                         //LBUG();
1872                         continue;
1873                 }
1874
1875                 if (lock->l_readers || lock->l_writers)
1876                         continue;
1877
1878                 /* If somebody is already doing CANCEL, or blocking AST came,
1879                  * skip this lock. */
1880                 if (ldlm_is_bl_ast(lock) || ldlm_is_canceling(lock))
1881                         continue;
1882
1883                 if (lockmode_compat(lock->l_granted_mode, mode))
1884                         continue;
1885
1886                 /* If policy is given and this is IBITS lock, add to list only
1887                  * those locks that match by policy. */
1888                 if (policy && (lock->l_resource->lr_type == LDLM_IBITS) &&
1889                     !(lock->l_policy_data.l_inodebits.bits &
1890                       policy->l_inodebits.bits))
1891                         continue;
1892
1893                 /* See CBPENDING comment in ldlm_cancel_lru */
1894                 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_CANCELING |
1895                                  lock_flags;
1896
1897                 LASSERT(list_empty(&lock->l_bl_ast));
1898                 list_add(&lock->l_bl_ast, cancels);
1899                 LDLM_LOCK_GET(lock);
1900                 count++;
1901         }
1902         unlock_res(res);
1903
1904         RETURN(ldlm_cli_cancel_list_local(cancels, count, cancel_flags));
1905 }
1906 EXPORT_SYMBOL(ldlm_cancel_resource_local);
1907
1908 /**
1909  * Cancel client-side locks from a list and send/prepare cancel RPCs to the
1910  * server.
1911  * If \a req is NULL, send CANCEL request to server with handles of locks
1912  * in the \a cancels. If EARLY_CANCEL is not supported, send CANCEL requests
1913  * separately per lock.
1914  * If \a req is not NULL, put handles of locks in \a cancels into the request
1915  * buffer at the offset \a off.
1916  * Destroy \a cancels at the end.
1917  */
1918 int ldlm_cli_cancel_list(struct list_head *cancels, int count,
1919                          struct ptlrpc_request *req,
1920                          enum ldlm_cancel_flags flags)
1921 {
1922         struct ldlm_lock *lock;
1923         int res = 0;
1924         ENTRY;
1925
1926         if (list_empty(cancels) || count == 0)
1927                 RETURN(0);
1928
1929         /* XXX: requests (both batched and not) could be sent in parallel.
1930          * Usually it is enough to have just 1 RPC, but it is possible that
1931          * there are too many locks to be cancelled in LRU or on a resource.
1932          * It would also speed up the case when the server does not support
1933          * the feature. */
1934         while (count > 0) {
1935                 LASSERT(!list_empty(cancels));
1936                 lock = list_entry(cancels->next, struct ldlm_lock,
1937                                       l_bl_ast);
1938                 LASSERT(lock->l_conn_export);
1939
1940                 if (exp_connect_cancelset(lock->l_conn_export)) {
1941                         res = count;
1942                         if (req)
1943                                 ldlm_cancel_pack(req, cancels, count);
1944                         else
1945                                 res = ldlm_cli_cancel_req(lock->l_conn_export,
1946                                                           cancels, count,
1947                                                           flags);
1948                 } else {
1949                         res = ldlm_cli_cancel_req(lock->l_conn_export,
1950                                                   cancels, 1, flags);
1951                 }
1952
1953                 if (res < 0) {
1954                         CDEBUG_LIMIT(res == -ESHUTDOWN ? D_DLMTRACE : D_ERROR,
1955                                      "ldlm_cli_cancel_list: %d\n", res);
1956                         res = count;
1957                 }
1958
1959                 count -= res;
1960                 ldlm_lock_list_put(cancels, l_bl_ast, res);
1961         }
1962         LASSERT(count == 0);
1963         RETURN(0);
1964 }
1965 EXPORT_SYMBOL(ldlm_cli_cancel_list);
1966
1967 /**
1968  * Cancel all locks on a resource that have 0 readers/writers.
1969  *
1970  * If flags & LDLM_FL_LOCAL_ONLY, throw the locks away without trying
1971  * to notify the server. */
1972 int ldlm_cli_cancel_unused_resource(struct ldlm_namespace *ns,
1973                                     const struct ldlm_res_id *res_id,
1974                                     union ldlm_policy_data *policy,
1975                                     enum ldlm_mode mode,
1976                                     enum ldlm_cancel_flags flags, void *opaque)
1977 {
1978         struct ldlm_resource *res;
1979         struct list_head cancels = LIST_HEAD_INIT(cancels);
1980         int count;
1981         int rc;
1982         ENTRY;
1983
1984         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
1985         if (IS_ERR(res)) {
1986                 /* This is not a problem. */
1987                 CDEBUG(D_INFO, "No resource "LPU64"\n", res_id->name[0]);
1988                 RETURN(0);
1989         }
1990
1991         LDLM_RESOURCE_ADDREF(res);
1992         count = ldlm_cancel_resource_local(res, &cancels, policy, mode,
1993                                            0, flags | LCF_BL_AST, opaque);
1994         rc = ldlm_cli_cancel_list(&cancels, count, NULL, flags);
1995         if (rc != ELDLM_OK)
1996                 CERROR("canceling unused lock "DLDLMRES": rc = %d\n",
1997                        PLDLMRES(res), rc);
1998
1999         LDLM_RESOURCE_DELREF(res);
2000         ldlm_resource_putref(res);
2001         RETURN(0);
2002 }
2003 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2004
2005 struct ldlm_cli_cancel_arg {
2006         int     lc_flags;
2007         void   *lc_opaque;
2008 };
2009
2010 static int
2011 ldlm_cli_hash_cancel_unused(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2012                             struct hlist_node *hnode, void *arg)
2013 {
2014         struct ldlm_resource           *res = cfs_hash_object(hs, hnode);
2015         struct ldlm_cli_cancel_arg     *lc = arg;
2016
2017         ldlm_cli_cancel_unused_resource(ldlm_res_to_ns(res), &res->lr_name,
2018                                         NULL, LCK_MINMODE, lc->lc_flags,
2019                                         lc->lc_opaque);
2020         /* must return 0 for hash iteration */
2021         return 0;
2022 }
2023
2024 /**
2025  * Cancel all locks on a namespace (or a specific resource, if given)
2026  * that have 0 readers/writers.
2027  *
2028  * If flags & LCF_LOCAL, throw the locks away without trying
2029  * to notify the server. */
2030 int ldlm_cli_cancel_unused(struct ldlm_namespace *ns,
2031                            const struct ldlm_res_id *res_id,
2032                            enum ldlm_cancel_flags flags, void *opaque)
2033 {
2034         struct ldlm_cli_cancel_arg arg = {
2035                 .lc_flags       = flags,
2036                 .lc_opaque      = opaque,
2037         };
2038
2039         ENTRY;
2040
2041         if (ns == NULL)
2042                 RETURN(ELDLM_OK);
2043
2044         if (res_id != NULL) {
2045                 RETURN(ldlm_cli_cancel_unused_resource(ns, res_id, NULL,
2046                                                        LCK_MINMODE, flags,
2047                                                        opaque));
2048         } else {
2049                 cfs_hash_for_each_nolock(ns->ns_rs_hash,
2050                                          ldlm_cli_hash_cancel_unused, &arg, 0);
2051                 RETURN(ELDLM_OK);
2052         }
2053 }
2054
2055 /* Lock iterators. */
2056
2057 int ldlm_resource_foreach(struct ldlm_resource *res, ldlm_iterator_t iter,
2058                           void *closure)
2059 {
2060         struct list_head *tmp, *next;
2061         struct ldlm_lock *lock;
2062         int rc = LDLM_ITER_CONTINUE;
2063
2064         ENTRY;
2065
2066         if (!res)
2067                 RETURN(LDLM_ITER_CONTINUE);
2068
2069         lock_res(res);
2070         list_for_each_safe(tmp, next, &res->lr_granted) {
2071                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2072
2073                 if (iter(lock, closure) == LDLM_ITER_STOP)
2074                         GOTO(out, rc = LDLM_ITER_STOP);
2075         }
2076
2077         list_for_each_safe(tmp, next, &res->lr_converting) {
2078                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2079
2080                 if (iter(lock, closure) == LDLM_ITER_STOP)
2081                         GOTO(out, rc = LDLM_ITER_STOP);
2082         }
2083
2084         list_for_each_safe(tmp, next, &res->lr_waiting) {
2085                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
2086
2087                 if (iter(lock, closure) == LDLM_ITER_STOP)
2088                         GOTO(out, rc = LDLM_ITER_STOP);
2089         }
2090  out:
2091         unlock_res(res);
2092         RETURN(rc);
2093 }
2094
2095 struct iter_helper_data {
2096         ldlm_iterator_t iter;
2097         void *closure;
2098 };
2099
2100 static int ldlm_iter_helper(struct ldlm_lock *lock, void *closure)
2101 {
2102         struct iter_helper_data *helper = closure;
2103         return helper->iter(lock, helper->closure);
2104 }
2105
2106 static int ldlm_res_iter_helper(struct cfs_hash *hs, struct cfs_hash_bd *bd,
2107                                 struct hlist_node *hnode, void *arg)
2108
2109 {
2110         struct ldlm_resource *res = cfs_hash_object(hs, hnode);
2111
2112         return ldlm_resource_foreach(res, ldlm_iter_helper, arg) ==
2113                LDLM_ITER_STOP;
2114 }
2115
2116 void ldlm_namespace_foreach(struct ldlm_namespace *ns,
2117                             ldlm_iterator_t iter, void *closure)
2118
2119 {
2120         struct iter_helper_data helper = { .iter = iter, .closure = closure };
2121
2122         cfs_hash_for_each_nolock(ns->ns_rs_hash,
2123                                  ldlm_res_iter_helper, &helper, 0);
2124
2125 }
2126
2127 /* non-blocking function to manipulate a lock whose cb_data is being put away.
2128  * return  0:  find no resource
2129  *       > 0:  must be LDLM_ITER_STOP/LDLM_ITER_CONTINUE.
2130  *       < 0:  errors
2131  */
2132 int ldlm_resource_iterate(struct ldlm_namespace *ns,
2133                           const struct ldlm_res_id *res_id,
2134                           ldlm_iterator_t iter, void *data)
2135 {
2136         struct ldlm_resource *res;
2137         int rc;
2138         ENTRY;
2139
2140         LASSERTF(ns != NULL, "must pass in namespace\n");
2141
2142         res = ldlm_resource_get(ns, NULL, res_id, 0, 0);
2143         if (IS_ERR(res))
2144                 RETURN(0);
2145
2146         LDLM_RESOURCE_ADDREF(res);
2147         rc = ldlm_resource_foreach(res, iter, data);
2148         LDLM_RESOURCE_DELREF(res);
2149         ldlm_resource_putref(res);
2150         RETURN(rc);
2151 }
2152 EXPORT_SYMBOL(ldlm_resource_iterate);
2153
2154 /* Lock replay */
2155
2156 static int ldlm_chain_lock_for_replay(struct ldlm_lock *lock, void *closure)
2157 {
2158         struct list_head *list = closure;
2159
2160         /* we use l_pending_chain here, because it's unused on clients. */
2161         LASSERTF(list_empty(&lock->l_pending_chain),
2162                  "lock %p next %p prev %p\n",
2163                  lock, &lock->l_pending_chain.next,&lock->l_pending_chain.prev);
2164         /* bug 9573: don't replay locks left after eviction, or
2165          * bug 17614: locks being actively cancelled. Get a reference
2166          * on a lock so that it does not disapear under us (e.g. due to cancel)
2167          */
2168         if (!(lock->l_flags & (LDLM_FL_FAILED|LDLM_FL_CANCELING))) {
2169                 list_add(&lock->l_pending_chain, list);
2170                 LDLM_LOCK_GET(lock);
2171         }
2172
2173         return LDLM_ITER_CONTINUE;
2174 }
2175
2176 static int replay_lock_interpret(const struct lu_env *env,
2177                                  struct ptlrpc_request *req,
2178                                  struct ldlm_async_args *aa, int rc)
2179 {
2180         struct ldlm_lock     *lock;
2181         struct ldlm_reply    *reply;
2182         struct obd_export    *exp;
2183
2184         ENTRY;
2185         atomic_dec(&req->rq_import->imp_replay_inflight);
2186         if (rc != ELDLM_OK)
2187                 GOTO(out, rc);
2188
2189         reply = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
2190         if (reply == NULL)
2191                 GOTO(out, rc = -EPROTO);
2192
2193         lock = ldlm_handle2lock(&aa->lock_handle);
2194         if (!lock) {
2195                 CERROR("received replay ack for unknown local cookie "LPX64
2196                        " remote cookie "LPX64 " from server %s id %s\n",
2197                        aa->lock_handle.cookie, reply->lock_handle.cookie,
2198                        req->rq_export->exp_client_uuid.uuid,
2199                        libcfs_id2str(req->rq_peer));
2200                 GOTO(out, rc = -ESTALE);
2201         }
2202
2203         /* Key change rehash lock in per-export hash with new key */
2204         exp = req->rq_export;
2205         if (exp && exp->exp_lock_hash) {
2206                 /* In the function below, .hs_keycmp resolves to
2207                  * ldlm_export_lock_keycmp() */
2208                 /* coverity[overrun-buffer-val] */
2209                 cfs_hash_rehash_key(exp->exp_lock_hash,
2210                                     &lock->l_remote_handle,
2211                                     &reply->lock_handle,
2212                                     &lock->l_exp_hash);
2213         } else {
2214                 lock->l_remote_handle = reply->lock_handle;
2215         }
2216
2217         LDLM_DEBUG(lock, "replayed lock:");
2218         ptlrpc_import_recovery_state_machine(req->rq_import);
2219         LDLM_LOCK_PUT(lock);
2220 out:
2221         if (rc != ELDLM_OK)
2222                 ptlrpc_connect_import(req->rq_import);
2223
2224         RETURN(rc);
2225 }
2226
2227 static int replay_one_lock(struct obd_import *imp, struct ldlm_lock *lock)
2228 {
2229         struct ptlrpc_request *req;
2230         struct ldlm_async_args *aa;
2231         struct ldlm_request   *body;
2232         int flags;
2233         ENTRY;
2234
2235
2236         /* Bug 11974: Do not replay a lock which is actively being canceled */
2237         if (ldlm_is_canceling(lock)) {
2238                 LDLM_DEBUG(lock, "Not replaying canceled lock:");
2239                 RETURN(0);
2240         }
2241
2242         /* If this is reply-less callback lock, we cannot replay it, since
2243          * server might have long dropped it, but notification of that event was
2244          * lost by network. (and server granted conflicting lock already) */
2245         if (ldlm_is_cancel_on_block(lock)) {
2246                 LDLM_DEBUG(lock, "Not replaying reply-less lock:");
2247                 ldlm_lock_cancel(lock);
2248                 RETURN(0);
2249         }
2250
2251         /*
2252          * If granted mode matches the requested mode, this lock is granted.
2253          *
2254          * If they differ, but we have a granted mode, then we were granted
2255          * one mode and now want another: ergo, converting.
2256          *
2257          * If we haven't been granted anything and are on a resource list,
2258          * then we're blocked/waiting.
2259          *
2260          * If we haven't been granted anything and we're NOT on a resource list,
2261          * then we haven't got a reply yet and don't have a known disposition.
2262          * This happens whenever a lock enqueue is the request that triggers
2263          * recovery.
2264          */
2265         if (lock->l_granted_mode == lock->l_req_mode)
2266                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_GRANTED;
2267         else if (lock->l_granted_mode)
2268                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_CONV;
2269         else if (!list_empty(&lock->l_res_link))
2270                 flags = LDLM_FL_REPLAY | LDLM_FL_BLOCK_WAIT;
2271         else
2272                 flags = LDLM_FL_REPLAY;
2273
2274         req = ptlrpc_request_alloc_pack(imp, &RQF_LDLM_ENQUEUE,
2275                                         LUSTRE_DLM_VERSION, LDLM_ENQUEUE);
2276         if (req == NULL)
2277                 RETURN(-ENOMEM);
2278
2279         /* We're part of recovery, so don't wait for it. */
2280         req->rq_send_state = LUSTRE_IMP_REPLAY_LOCKS;
2281
2282         body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2283         ldlm_lock2desc(lock, &body->lock_desc);
2284         body->lock_flags = ldlm_flags_to_wire(flags);
2285
2286         ldlm_lock2handle(lock, &body->lock_handle[0]);
2287         if (lock->l_lvb_len > 0)
2288                 req_capsule_extend(&req->rq_pill, &RQF_LDLM_ENQUEUE_LVB);
2289         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
2290                              lock->l_lvb_len);
2291         ptlrpc_request_set_replen(req);
2292         /* notify the server we've replayed all requests.
2293          * also, we mark the request to be put on a dedicated
2294          * queue to be processed after all request replayes.
2295          * bug 6063 */
2296         lustre_msg_set_flags(req->rq_reqmsg, MSG_REQ_REPLAY_DONE);
2297
2298         LDLM_DEBUG(lock, "replaying lock:");
2299
2300         atomic_inc(&req->rq_import->imp_replay_inflight);
2301         CLASSERT(sizeof(*aa) <= sizeof(req->rq_async_args));
2302         aa = ptlrpc_req_async_args(req);
2303         aa->lock_handle = body->lock_handle[0];
2304         req->rq_interpret_reply = (ptlrpc_interpterer_t)replay_lock_interpret;
2305         ptlrpcd_add_req(req);
2306
2307         RETURN(0);
2308 }
2309
2310 /**
2311  * Cancel as many unused locks as possible before replay. since we are
2312  * in recovery, we can't wait for any outstanding RPCs to send any RPC
2313  * to the server.
2314  *
2315  * Called only in recovery before replaying locks. there is no need to
2316  * replay locks that are unused. since the clients may hold thousands of
2317  * cached unused locks, dropping the unused locks can greatly reduce the
2318  * load on the servers at recovery time.
2319  */
2320 static void ldlm_cancel_unused_locks_for_replay(struct ldlm_namespace *ns)
2321 {
2322         int canceled;
2323         struct list_head cancels = LIST_HEAD_INIT(cancels);
2324
2325         CDEBUG(D_DLMTRACE, "Dropping as many unused locks as possible before"
2326                            "replay for namespace %s (%d)\n",
2327                            ldlm_ns_name(ns), ns->ns_nr_unused);
2328
2329         /* We don't need to care whether or not LRU resize is enabled
2330          * because the LDLM_LRU_FLAG_NO_WAIT policy doesn't use the
2331          * count parameter */
2332         canceled = ldlm_cancel_lru_local(ns, &cancels, ns->ns_nr_unused, 0,
2333                                          LCF_LOCAL, LDLM_LRU_FLAG_NO_WAIT);
2334
2335         CDEBUG(D_DLMTRACE, "Canceled %d unused locks from namespace %s\n",
2336                            canceled, ldlm_ns_name(ns));
2337 }
2338
2339 int ldlm_replay_locks(struct obd_import *imp)
2340 {
2341         struct ldlm_namespace *ns = imp->imp_obd->obd_namespace;
2342         struct list_head list = LIST_HEAD_INIT(list);
2343         struct ldlm_lock *lock, *next;
2344         int rc = 0;
2345
2346         ENTRY;
2347
2348         LASSERT(atomic_read(&imp->imp_replay_inflight) == 0);
2349
2350         /* don't replay locks if import failed recovery */
2351         if (imp->imp_vbr_failed)
2352                 RETURN(0);
2353
2354         /* ensure this doesn't fall to 0 before all have been queued */
2355         atomic_inc(&imp->imp_replay_inflight);
2356
2357         if (ldlm_cancel_unused_locks_before_replay)
2358                 ldlm_cancel_unused_locks_for_replay(ns);
2359
2360         ldlm_namespace_foreach(ns, ldlm_chain_lock_for_replay, &list);
2361
2362         list_for_each_entry_safe(lock, next, &list, l_pending_chain) {
2363                 list_del_init(&lock->l_pending_chain);
2364                 if (rc) {
2365                         LDLM_LOCK_RELEASE(lock);
2366                         continue; /* or try to do the rest? */
2367                 }
2368                 rc = replay_one_lock(imp, lock);
2369                 LDLM_LOCK_RELEASE(lock);
2370         }
2371
2372         atomic_dec(&imp->imp_replay_inflight);
2373
2374         RETURN(rc);
2375 }