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