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