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