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