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