4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ldlm/ldlm_lockd.c
38 * Author: Peter Braam <braam@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
42 #define DEBUG_SUBSYSTEM S_LDLM
45 # include <libcfs/libcfs.h>
47 # include <liblustre.h>
50 #include <lustre_dlm.h>
51 #include <obd_class.h>
52 #include <libcfs/list.h>
53 #include "ldlm_internal.h"
55 static int ldlm_num_threads;
56 CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
57 "number of DLM service threads to start");
59 static char *ldlm_cpts;
60 CFS_MODULE_PARM(ldlm_cpts, "s", charp, 0444,
61 "CPU partitions ldlm threads should run on");
63 extern cfs_mem_cache_t *ldlm_resource_slab;
64 extern cfs_mem_cache_t *ldlm_lock_slab;
65 static cfs_mutex_t ldlm_ref_mutex;
66 static int ldlm_refcount;
68 struct ldlm_cb_async_args {
69 struct ldlm_cb_set_arg *ca_set_arg;
70 struct ldlm_lock *ca_lock;
75 static struct ldlm_state *ldlm_state;
77 inline cfs_time_t round_timeout(cfs_time_t timeout)
79 return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
82 /* timeout for initial callback (AST) reply (bz10399) */
83 static inline unsigned int ldlm_get_rq_timeout(void)
86 unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
88 return timeout < 1 ? 1 : timeout;
93 #define ELT_TERMINATE 2
96 cfs_spinlock_t blp_lock;
99 * blp_prio_list is used for callbacks that should be handled
100 * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
103 cfs_list_t blp_prio_list;
106 * blp_list is used for all other callbacks which are likely
107 * to take longer to process.
111 cfs_waitq_t blp_waitq;
112 cfs_completion_t blp_comp;
113 cfs_atomic_t blp_num_threads;
114 cfs_atomic_t blp_busy_threads;
119 struct ldlm_bl_work_item {
120 cfs_list_t blwi_entry;
121 struct ldlm_namespace *blwi_ns;
122 struct ldlm_lock_desc blwi_ld;
123 struct ldlm_lock *blwi_lock;
124 cfs_list_t blwi_head;
126 cfs_completion_t blwi_comp;
128 int blwi_mem_pressure;
131 #if defined(HAVE_SERVER_SUPPORT) && defined(__KERNEL__)
133 /* w_l_spinlock protects both waiting_locks_list and expired_lock_thread */
134 static cfs_spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
135 static cfs_list_t waiting_locks_list;
136 static cfs_timer_t waiting_locks_timer;
138 static struct expired_lock_thread {
139 cfs_waitq_t elt_waitq;
142 cfs_list_t elt_expired_locks;
143 } expired_lock_thread;
145 static inline int have_expired_locks(void)
150 cfs_spin_lock_bh(&waiting_locks_spinlock);
151 need_to_run = !cfs_list_empty(&expired_lock_thread.elt_expired_locks);
152 cfs_spin_unlock_bh(&waiting_locks_spinlock);
157 static int expired_lock_main(void *arg)
159 cfs_list_t *expired = &expired_lock_thread.elt_expired_locks;
160 struct l_wait_info lwi = { 0 };
164 cfs_daemonize("ldlm_elt");
166 expired_lock_thread.elt_state = ELT_READY;
167 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
170 l_wait_event(expired_lock_thread.elt_waitq,
171 have_expired_locks() ||
172 expired_lock_thread.elt_state == ELT_TERMINATE,
175 cfs_spin_lock_bh(&waiting_locks_spinlock);
176 if (expired_lock_thread.elt_dump) {
177 struct libcfs_debug_msg_data msgdata = {
178 .msg_file = __FILE__,
179 .msg_fn = "waiting_locks_callback",
180 .msg_line = expired_lock_thread.elt_dump };
181 cfs_spin_unlock_bh(&waiting_locks_spinlock);
183 /* from waiting_locks_callback, but not in timer */
184 libcfs_debug_dumplog();
185 libcfs_run_lbug_upcall(&msgdata);
187 cfs_spin_lock_bh(&waiting_locks_spinlock);
188 expired_lock_thread.elt_dump = 0;
193 while (!cfs_list_empty(expired)) {
194 struct obd_export *export;
195 struct ldlm_lock *lock;
197 lock = cfs_list_entry(expired->next, struct ldlm_lock,
199 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
200 (void *)lock >= LP_POISON) {
201 cfs_spin_unlock_bh(&waiting_locks_spinlock);
202 CERROR("free lock on elt list %p\n", lock);
205 cfs_list_del_init(&lock->l_pending_chain);
206 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
207 (void *)lock->l_export >= LP_POISON) {
208 CERROR("lock with free export on elt list %p\n",
210 lock->l_export = NULL;
211 LDLM_ERROR(lock, "free export");
212 /* release extra ref grabbed by
213 * ldlm_add_waiting_lock() or
214 * ldlm_failed_ast() */
215 LDLM_LOCK_RELEASE(lock);
219 if (lock->l_destroyed) {
220 /* release the lock refcount where
221 * waiting_locks_callback() founds */
222 LDLM_LOCK_RELEASE(lock);
225 export = class_export_lock_get(lock->l_export, lock);
226 cfs_spin_unlock_bh(&waiting_locks_spinlock);
229 class_fail_export(export);
230 class_export_lock_put(export, lock);
232 /* release extra ref grabbed by ldlm_add_waiting_lock()
233 * or ldlm_failed_ast() */
234 LDLM_LOCK_RELEASE(lock);
236 cfs_spin_lock_bh(&waiting_locks_spinlock);
238 cfs_spin_unlock_bh(&waiting_locks_spinlock);
240 if (do_dump && obd_dump_on_eviction) {
241 CERROR("dump the log upon eviction\n");
242 libcfs_debug_dumplog();
245 if (expired_lock_thread.elt_state == ELT_TERMINATE)
249 expired_lock_thread.elt_state = ELT_STOPPED;
250 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
254 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
255 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds);
258 * Check if there is a request in the export request list
259 * which prevents the lock canceling.
261 static int ldlm_lock_busy(struct ldlm_lock *lock)
263 struct ptlrpc_request *req;
267 if (lock->l_export == NULL)
270 cfs_spin_lock_bh(&lock->l_export->exp_rpc_lock);
271 cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
273 if (req->rq_ops->hpreq_lock_match) {
274 match = req->rq_ops->hpreq_lock_match(req, lock);
279 cfs_spin_unlock_bh(&lock->l_export->exp_rpc_lock);
283 /* This is called from within a timer interrupt and cannot schedule */
284 static void waiting_locks_callback(unsigned long unused)
286 struct ldlm_lock *lock;
289 cfs_spin_lock_bh(&waiting_locks_spinlock);
290 while (!cfs_list_empty(&waiting_locks_list)) {
291 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
293 if (cfs_time_after(lock->l_callback_timeout,
294 cfs_time_current()) ||
295 (lock->l_req_mode == LCK_GROUP))
298 if (ptlrpc_check_suspend()) {
299 /* there is a case when we talk to one mds, holding
300 * lock from another mds. this way we easily can get
301 * here, if second mds is being recovered. so, we
302 * suspend timeouts. bug 6019 */
304 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
305 lock->l_export->exp_client_uuid.uuid,
306 lock->l_export->exp_connection->c_remote_uuid.uuid,
307 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
309 cfs_list_del_init(&lock->l_pending_chain);
310 if (lock->l_destroyed) {
311 /* relay the lock refcount decrease to
312 * expired lock thread */
313 cfs_list_add(&lock->l_pending_chain,
314 &expired_lock_thread.elt_expired_locks);
316 __ldlm_add_waiting_lock(lock,
317 ldlm_get_enq_timeout(lock));
322 /* if timeout overlaps the activation time of suspended timeouts
323 * then extend it to give a chance for client to reconnect */
324 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
325 cfs_time_seconds(obd_timeout)/2),
326 ptlrpc_suspend_wakeup_time())) {
327 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
328 lock->l_export->exp_client_uuid.uuid,
329 lock->l_export->exp_connection->c_remote_uuid.uuid,
330 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
332 cfs_list_del_init(&lock->l_pending_chain);
333 if (lock->l_destroyed) {
334 /* relay the lock refcount decrease to
335 * expired lock thread */
336 cfs_list_add(&lock->l_pending_chain,
337 &expired_lock_thread.elt_expired_locks);
339 __ldlm_add_waiting_lock(lock,
340 ldlm_get_enq_timeout(lock));
345 /* Check if we need to prolong timeout */
346 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
347 ldlm_lock_busy(lock)) {
350 if (lock->l_pending_chain.next == &waiting_locks_list)
355 cfs_spin_unlock_bh(&waiting_locks_spinlock);
356 LDLM_DEBUG(lock, "prolong the busy lock");
357 ldlm_refresh_waiting_lock(lock,
358 ldlm_get_enq_timeout(lock));
359 cfs_spin_lock_bh(&waiting_locks_spinlock);
362 LDLM_LOCK_RELEASE(lock);
366 LDLM_LOCK_RELEASE(lock);
369 ldlm_lock_to_ns(lock)->ns_timeouts++;
370 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
371 "evicting client at %s ",
372 cfs_time_current_sec()- lock->l_last_activity,
374 lock->l_export->exp_connection->c_peer.nid));
376 /* no needs to take an extra ref on the lock since it was in
377 * the waiting_locks_list and ldlm_add_waiting_lock()
378 * already grabbed a ref */
379 cfs_list_del(&lock->l_pending_chain);
380 cfs_list_add(&lock->l_pending_chain,
381 &expired_lock_thread.elt_expired_locks);
385 if (!cfs_list_empty(&expired_lock_thread.elt_expired_locks)) {
386 if (obd_dump_on_timeout && need_dump)
387 expired_lock_thread.elt_dump = __LINE__;
389 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
393 * Make sure the timer will fire again if we have any locks
396 if (!cfs_list_empty(&waiting_locks_list)) {
397 cfs_time_t timeout_rounded;
398 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
400 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
401 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
403 cfs_spin_unlock_bh(&waiting_locks_spinlock);
407 * Indicate that we're waiting for a client to call us back cancelling a given
408 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
409 * timer to fire appropriately. (We round up to the next second, to avoid
410 * floods of timer firings during periods of high lock contention and traffic).
411 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
412 * if it has been added to the waiting list (1 is returned).
414 * Called with the namespace lock held.
416 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
419 cfs_time_t timeout_rounded;
421 if (!cfs_list_empty(&lock->l_pending_chain))
424 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
425 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
428 timeout = cfs_time_shift(seconds);
429 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
430 lock->l_callback_timeout = timeout;
432 timeout_rounded = round_timeout(lock->l_callback_timeout);
434 if (cfs_time_before(timeout_rounded,
435 cfs_timer_deadline(&waiting_locks_timer)) ||
436 !cfs_timer_is_armed(&waiting_locks_timer)) {
437 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
439 /* if the new lock has a shorter timeout than something earlier on
440 the list, we'll wait the longer amount of time; no big deal. */
442 cfs_list_add_tail(&lock->l_pending_chain, &waiting_locks_list);
446 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
449 int timeout = ldlm_get_enq_timeout(lock);
451 /* NB: must be called with hold of lock_res_and_lock() */
452 LASSERT(lock->l_res_locked);
455 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
457 cfs_spin_lock_bh(&waiting_locks_spinlock);
458 if (lock->l_destroyed) {
459 static cfs_time_t next;
460 cfs_spin_unlock_bh(&waiting_locks_spinlock);
461 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
462 if (cfs_time_after(cfs_time_current(), next)) {
463 next = cfs_time_shift(14400);
464 libcfs_debug_dumpstack(NULL);
469 ret = __ldlm_add_waiting_lock(lock, timeout);
471 /* grab ref on the lock if it has been added to the
475 cfs_spin_unlock_bh(&waiting_locks_spinlock);
478 cfs_spin_lock_bh(&lock->l_export->exp_bl_list_lock);
479 if (cfs_list_empty(&lock->l_exp_list))
480 cfs_list_add(&lock->l_exp_list,
481 &lock->l_export->exp_bl_list);
482 cfs_spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
485 LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
486 ret == 0 ? "not re-" : "", timeout,
487 AT_OFF ? "off" : "on");
492 * Remove a lock from the pending list, likely because it had its cancellation
493 * callback arrive without incident. This adjusts the lock-timeout timer if
494 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
495 * As done by ldlm_del_waiting_lock(), the caller must release the lock
496 * reference when the lock is removed from any list (1 is returned).
498 * Called with namespace lock held.
500 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
502 cfs_list_t *list_next;
504 if (cfs_list_empty(&lock->l_pending_chain))
507 list_next = lock->l_pending_chain.next;
508 if (lock->l_pending_chain.prev == &waiting_locks_list) {
509 /* Removing the head of the list, adjust timer. */
510 if (list_next == &waiting_locks_list) {
511 /* No more, just cancel. */
512 cfs_timer_disarm(&waiting_locks_timer);
514 struct ldlm_lock *next;
515 next = cfs_list_entry(list_next, struct ldlm_lock,
517 cfs_timer_arm(&waiting_locks_timer,
518 round_timeout(next->l_callback_timeout));
521 cfs_list_del_init(&lock->l_pending_chain);
526 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
530 if (lock->l_export == NULL) {
531 /* We don't have a "waiting locks list" on clients. */
532 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
536 cfs_spin_lock_bh(&waiting_locks_spinlock);
537 ret = __ldlm_del_waiting_lock(lock);
538 cfs_spin_unlock_bh(&waiting_locks_spinlock);
540 /* remove the lock out of export blocking list */
541 cfs_spin_lock_bh(&lock->l_export->exp_bl_list_lock);
542 cfs_list_del_init(&lock->l_exp_list);
543 cfs_spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
546 /* release lock ref if it has indeed been removed
548 LDLM_LOCK_RELEASE(lock);
551 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
554 EXPORT_SYMBOL(ldlm_del_waiting_lock);
559 * Called with namespace lock held.
561 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
563 if (lock->l_export == NULL) {
564 /* We don't have a "waiting locks list" on clients. */
565 LDLM_DEBUG(lock, "client lock: no-op");
569 cfs_spin_lock_bh(&waiting_locks_spinlock);
571 if (cfs_list_empty(&lock->l_pending_chain)) {
572 cfs_spin_unlock_bh(&waiting_locks_spinlock);
573 LDLM_DEBUG(lock, "wasn't waiting");
577 /* we remove/add the lock to the waiting list, so no needs to
578 * release/take a lock reference */
579 __ldlm_del_waiting_lock(lock);
580 __ldlm_add_waiting_lock(lock, timeout);
581 cfs_spin_unlock_bh(&waiting_locks_spinlock);
583 LDLM_DEBUG(lock, "refreshed");
586 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
588 #else /* !HAVE_SERVER_SUPPORT || !__KERNEL__ */
590 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
595 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
600 # ifdef HAVE_SERVER_SUPPORT
601 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
603 LASSERT(lock->l_res_locked);
604 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
609 #endif /* HAVE_SERVER_SUPPORT && __KERNEL__ */
611 #ifdef HAVE_SERVER_SUPPORT
613 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
614 const char *ast_type)
616 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
617 "to a lock %s callback time out: rc %d\n",
618 lock->l_export->exp_obd->obd_name,
619 obd_export_nid2str(lock->l_export), ast_type, rc);
621 if (obd_dump_on_timeout)
622 libcfs_debug_dumplog();
624 cfs_spin_lock_bh(&waiting_locks_spinlock);
625 if (__ldlm_del_waiting_lock(lock) == 0)
626 /* the lock was not in any list, grab an extra ref before adding
627 * the lock to the expired list */
629 cfs_list_add(&lock->l_pending_chain,
630 &expired_lock_thread.elt_expired_locks);
631 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
632 cfs_spin_unlock_bh(&waiting_locks_spinlock);
634 class_fail_export(lock->l_export);
638 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
639 struct ptlrpc_request *req, int rc,
640 const char *ast_type)
642 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
644 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
645 LASSERT(lock->l_export);
646 if (lock->l_export->exp_libclient) {
647 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
648 " timeout, just cancelling lock", ast_type,
649 libcfs_nid2str(peer.nid));
650 ldlm_lock_cancel(lock);
652 } else if (lock->l_flags & LDLM_FL_CANCEL) {
653 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
654 "cancel was received (AST reply lost?)",
655 ast_type, libcfs_nid2str(peer.nid));
656 ldlm_lock_cancel(lock);
659 ldlm_del_waiting_lock(lock);
660 ldlm_failed_ast(lock, rc, ast_type);
664 struct ldlm_resource *res = lock->l_resource;
665 LDLM_DEBUG(lock, "client (nid %s) returned %d"
666 " from %s AST - normal race",
667 libcfs_nid2str(peer.nid),
669 lustre_msg_get_status(req->rq_repmsg) : -1,
672 /* update lvbo to return proper attributes.
674 ldlm_resource_getref(res);
675 ldlm_res_lvbo_update(res, NULL, 1);
676 ldlm_resource_putref(res);
680 LDLM_ERROR(lock, "client (nid %s) returned %d "
681 "from %s AST", libcfs_nid2str(peer.nid),
682 (req->rq_repmsg != NULL) ?
683 lustre_msg_get_status(req->rq_repmsg) : 0,
686 ldlm_lock_cancel(lock);
687 /* Server-side AST functions are called from ldlm_reprocess_all,
688 * which needs to be told to please restart its reprocessing. */
695 static int ldlm_cb_interpret(const struct lu_env *env,
696 struct ptlrpc_request *req, void *data, int rc)
698 struct ldlm_cb_async_args *ca = data;
699 struct ldlm_lock *lock = ca->ca_lock;
700 struct ldlm_cb_set_arg *arg = ca->ca_set_arg;
703 LASSERT(lock != NULL);
706 case LDLM_GL_CALLBACK:
707 /* Update the LVB from disk if the AST failed
708 * (this is a legal race)
710 * - Glimpse callback of local lock just returns
711 * -ELDLM_NO_LOCK_DATA.
712 * - Glimpse callback of remote lock might return
713 * -ELDLM_NO_LOCK_DATA when inode is cleared. LU-274
715 if (rc == -ELDLM_NO_LOCK_DATA) {
716 LDLM_DEBUG(lock, "lost race - client has a lock but no "
718 ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
719 } else if (rc != 0) {
720 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
722 rc = ldlm_res_lvbo_update(lock->l_resource, req, 1);
725 case LDLM_BL_CALLBACK:
727 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
729 case LDLM_CP_CALLBACK:
731 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
734 LDLM_ERROR(lock, "invalid opcode for lock callback %d",
739 /* release extra reference taken in ldlm_ast_fini() */
740 LDLM_LOCK_RELEASE(lock);
743 cfs_atomic_inc(&arg->restart);
748 static inline int ldlm_ast_fini(struct ptlrpc_request *req,
749 struct ldlm_cb_set_arg *arg,
750 struct ldlm_lock *lock,
756 if (unlikely(instant_cancel)) {
757 rc = ptl_send_rpc(req, 1);
758 ptlrpc_req_finished(req);
760 cfs_atomic_inc(&arg->restart);
763 ptlrpc_set_add_req(arg->set, req);
770 * Check if there are requests in the export request list which prevent
771 * the lock canceling and make these requests high priority ones.
773 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
775 struct ptlrpc_request *req;
778 if (lock->l_export == NULL) {
779 LDLM_DEBUG(lock, "client lock: no-op");
783 cfs_spin_lock_bh(&lock->l_export->exp_rpc_lock);
784 cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
786 /* Do not process requests that were not yet added to there
787 * incoming queue or were already removed from there for
789 if (!req->rq_hp && !cfs_list_empty(&req->rq_list) &&
790 req->rq_ops->hpreq_lock_match &&
791 req->rq_ops->hpreq_lock_match(req, lock))
792 ptlrpc_hpreq_reorder(req);
794 cfs_spin_unlock_bh(&lock->l_export->exp_rpc_lock);
799 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
800 * enqueued server lock conflicts with given one.
802 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
803 * to wait for client response.
805 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
806 struct ldlm_lock_desc *desc,
807 void *data, int flag)
809 struct ldlm_cb_async_args *ca;
810 struct ldlm_cb_set_arg *arg = data;
811 struct ldlm_request *body;
812 struct ptlrpc_request *req;
813 int instant_cancel = 0;
817 if (flag == LDLM_CB_CANCELING)
818 /* Don't need to do anything here. */
822 LASSERT(data != NULL);
823 if (lock->l_export->exp_obd->obd_recovering != 0)
824 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
826 ldlm_lock_reorder_req(lock);
828 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
829 &RQF_LDLM_BL_CALLBACK,
830 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
834 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
835 ca = ptlrpc_req_async_args(req);
836 ca->ca_set_arg = arg;
839 req->rq_interpret_reply = ldlm_cb_interpret;
840 req->rq_no_resend = 1;
842 lock_res_and_lock(lock);
843 if (lock->l_granted_mode != lock->l_req_mode) {
844 /* this blocking AST will be communicated as part of the
845 * completion AST instead */
846 unlock_res_and_lock(lock);
848 ptlrpc_req_finished(req);
849 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
853 if (lock->l_destroyed) {
854 /* What's the point? */
855 unlock_res_and_lock(lock);
856 ptlrpc_req_finished(req);
860 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
863 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
864 body->lock_handle[0] = lock->l_remote_handle;
865 body->lock_desc = *desc;
866 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags & LDLM_AST_FLAGS);
868 LDLM_DEBUG(lock, "server preparing blocking AST");
870 ptlrpc_request_set_replen(req);
871 if (instant_cancel) {
872 unlock_res_and_lock(lock);
873 ldlm_lock_cancel(lock);
875 LASSERT(lock->l_granted_mode == lock->l_req_mode);
876 ldlm_add_waiting_lock(lock);
877 unlock_res_and_lock(lock);
880 req->rq_send_state = LUSTRE_IMP_FULL;
881 /* ptlrpc_request_alloc_pack already set timeout */
883 req->rq_timeout = ldlm_get_rq_timeout();
885 if (lock->l_export && lock->l_export->exp_nid_stats &&
886 lock->l_export->exp_nid_stats->nid_ldlm_stats)
887 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
888 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
890 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
894 EXPORT_SYMBOL(ldlm_server_blocking_ast);
896 int ldlm_server_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
898 struct ldlm_cb_set_arg *arg = data;
899 struct ldlm_request *body;
900 struct ptlrpc_request *req;
901 struct ldlm_cb_async_args *ca;
902 long total_enqueue_wait;
903 int instant_cancel = 0;
908 LASSERT(lock != NULL);
909 LASSERT(data != NULL);
911 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
912 lock->l_last_activity);
914 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
915 &RQF_LDLM_CP_CALLBACK);
919 /* server namespace, doesn't need lock */
920 lvb_len = ldlm_lvbo_size(lock);
922 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
925 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
927 ptlrpc_request_free(req);
931 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
932 ca = ptlrpc_req_async_args(req);
933 ca->ca_set_arg = arg;
936 req->rq_interpret_reply = ldlm_cb_interpret;
937 req->rq_no_resend = 1;
938 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
940 body->lock_handle[0] = lock->l_remote_handle;
941 body->lock_flags = ldlm_flags_to_wire(flags);
942 ldlm_lock2desc(lock, &body->lock_desc);
944 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
946 lvb_len = ldlm_lvbo_fill(lock, lvb, lvb_len);
947 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB,
948 lvb_len, RCL_CLIENT);
951 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
954 /* Server-side enqueue wait time estimate, used in
955 __ldlm_add_waiting_lock to set future enqueue timers */
956 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
957 at_measured(ldlm_lock_to_ns_at(lock),
960 /* bz18618. Don't add lock enqueue time we spend waiting for a
961 previous callback to fail. Locks waiting legitimately will
962 get extended by ldlm_refresh_waiting_lock regardless of the
963 estimate, so it's okay to underestimate here. */
964 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
965 "It is likely that a previous callback timed out.",
967 at_get(ldlm_lock_to_ns_at(lock)));
969 ptlrpc_request_set_replen(req);
971 req->rq_send_state = LUSTRE_IMP_FULL;
972 /* ptlrpc_request_pack already set timeout */
974 req->rq_timeout = ldlm_get_rq_timeout();
976 /* We only send real blocking ASTs after the lock is granted */
977 lock_res_and_lock(lock);
978 if (lock->l_flags & LDLM_FL_AST_SENT) {
979 body->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
980 /* copy ast flags like LDLM_FL_DISCARD_DATA */
981 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags &
984 /* We might get here prior to ldlm_handle_enqueue setting
985 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
986 * into waiting list, but this is safe and similar code in
987 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
988 * that would not only cancel the lock, but will also remove
989 * it from waiting list */
990 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
991 unlock_res_and_lock(lock);
992 ldlm_lock_cancel(lock);
994 lock_res_and_lock(lock);
996 /* start the lock-timeout clock */
997 ldlm_add_waiting_lock(lock);
1000 unlock_res_and_lock(lock);
1002 if (lock->l_export && lock->l_export->exp_nid_stats &&
1003 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1004 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1005 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
1007 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
1011 EXPORT_SYMBOL(ldlm_server_completion_ast);
1013 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
1015 struct ldlm_cb_set_arg *arg = data;
1016 struct ldlm_request *body;
1017 struct ptlrpc_request *req;
1018 struct ldlm_cb_async_args *ca;
1020 struct req_format *req_fmt;
1023 LASSERT(lock != NULL);
1025 if (arg->gl_desc != NULL)
1026 /* There is a glimpse descriptor to pack */
1027 req_fmt = &RQF_LDLM_GL_DESC_CALLBACK;
1029 req_fmt = &RQF_LDLM_GL_CALLBACK;
1031 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
1032 req_fmt, LUSTRE_DLM_VERSION,
1038 if (arg->gl_desc != NULL) {
1039 /* copy the GL descriptor */
1040 union ldlm_gl_desc *desc;
1041 desc = req_capsule_client_get(&req->rq_pill, &RMF_DLM_GL_DESC);
1042 *desc = *arg->gl_desc;
1045 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1046 body->lock_handle[0] = lock->l_remote_handle;
1047 ldlm_lock2desc(lock, &body->lock_desc);
1049 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
1050 ca = ptlrpc_req_async_args(req);
1051 ca->ca_set_arg = arg;
1054 /* server namespace, doesn't need lock */
1055 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
1056 ldlm_lvbo_size(lock));
1057 ptlrpc_request_set_replen(req);
1059 req->rq_send_state = LUSTRE_IMP_FULL;
1060 /* ptlrpc_request_alloc_pack already set timeout */
1062 req->rq_timeout = ldlm_get_rq_timeout();
1064 req->rq_interpret_reply = ldlm_cb_interpret;
1066 if (lock->l_export && lock->l_export->exp_nid_stats &&
1067 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1068 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1069 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
1071 rc = ldlm_ast_fini(req, arg, lock, 0);
1075 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1077 int ldlm_glimpse_locks(struct ldlm_resource *res, cfs_list_t *gl_work_list)
1082 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), gl_work_list,
1084 if (rc == -ERESTART)
1085 ldlm_reprocess_all(res);
1089 EXPORT_SYMBOL(ldlm_glimpse_locks);
1091 /* return ldlm lock associated with a lock callback request */
1092 struct ldlm_lock *ldlm_request_lock(struct ptlrpc_request *req)
1094 struct ldlm_cb_async_args *ca;
1095 struct ldlm_lock *lock;
1098 ca = ptlrpc_req_async_args(req);
1101 RETURN(ERR_PTR(-EFAULT));
1105 EXPORT_SYMBOL(ldlm_request_lock);
1107 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1108 struct lprocfs_stats *srv_stats)
1110 int lock_type = 0, op = 0;
1112 lock_type = dlm_req->lock_desc.l_resource.lr_type;
1114 switch (lock_type) {
1116 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1119 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
1120 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
1122 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1125 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1128 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1136 lprocfs_counter_incr(srv_stats, op);
1142 * Main server-side entry point into LDLM. This is called by ptlrpc service
1143 * threads to carry out client lock enqueueing requests.
1145 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1146 struct ptlrpc_request *req,
1147 const struct ldlm_request *dlm_req,
1148 const struct ldlm_callback_suite *cbs)
1150 struct ldlm_reply *dlm_rep;
1152 ldlm_error_t err = ELDLM_OK;
1153 struct ldlm_lock *lock = NULL;
1154 void *cookie = NULL;
1158 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1160 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1161 flags = ldlm_flags_from_wire(dlm_req->lock_flags);
1163 LASSERT(req->rq_export);
1165 if (ptlrpc_req2svc(req)->srv_stats != NULL)
1166 ldlm_svc_get_eopc(dlm_req, ptlrpc_req2svc(req)->srv_stats);
1168 if (req->rq_export && req->rq_export->exp_nid_stats &&
1169 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1170 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1171 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1173 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1174 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1175 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1176 dlm_req->lock_desc.l_resource.lr_type);
1177 GOTO(out, rc = -EFAULT);
1180 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1181 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1182 dlm_req->lock_desc.l_req_mode &
1183 (dlm_req->lock_desc.l_req_mode-1))) {
1184 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1185 dlm_req->lock_desc.l_req_mode);
1186 GOTO(out, rc = -EFAULT);
1189 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1190 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1192 DEBUG_REQ(D_ERROR, req,
1193 "PLAIN lock request from IBITS client?");
1194 GOTO(out, rc = -EPROTO);
1196 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1198 DEBUG_REQ(D_ERROR, req,
1199 "IBITS lock request from unaware client?");
1200 GOTO(out, rc = -EPROTO);
1204 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1205 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1206 ibits for mgc/mgs) */
1208 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1209 * inodebits lock if client does not support them. */
1210 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1211 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1212 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1213 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1214 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1215 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1216 dlm_req->lock_desc.l_req_mode = LCK_CR;
1220 if (unlikely(flags & LDLM_FL_REPLAY)) {
1221 /* Find an existing lock in the per-export lock hash */
1222 /* In the function below, .hs_keycmp resolves to
1223 * ldlm_export_lock_keycmp() */
1224 /* coverity[overrun-buffer-val] */
1225 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1226 (void *)&dlm_req->lock_handle[0]);
1228 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1229 LPX64, lock->l_handle.h_cookie);
1230 GOTO(existing_lock, rc = 0);
1234 /* The lock's callback data might be set in the policy function */
1235 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1236 dlm_req->lock_desc.l_resource.lr_type,
1237 dlm_req->lock_desc.l_req_mode,
1241 GOTO(out, rc = -ENOMEM);
1243 lock->l_last_activity = cfs_time_current_sec();
1244 lock->l_remote_handle = dlm_req->lock_handle[0];
1245 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1247 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1248 /* Don't enqueue a lock onto the export if it is been disonnected
1249 * due to eviction (bug 3822) or server umount (bug 24324).
1250 * Cancel it now instead. */
1251 if (req->rq_export->exp_disconnected) {
1252 LDLM_ERROR(lock, "lock on disconnected export %p",
1254 GOTO(out, rc = -ENOTCONN);
1257 lock->l_export = class_export_lock_get(req->rq_export, lock);
1258 if (lock->l_export->exp_lock_hash)
1259 cfs_hash_add(lock->l_export->exp_lock_hash,
1260 &lock->l_remote_handle,
1265 if (flags & LDLM_FL_HAS_INTENT) {
1266 /* In this case, the reply buffer is allocated deep in
1267 * local_lock_enqueue by the policy function. */
1270 /* based on the assumption that lvb size never changes during
1271 * resource life time otherwise it need resource->lr_lock's
1273 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1274 RCL_SERVER, ldlm_lvbo_size(lock));
1276 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1277 GOTO(out, rc = -ENOMEM);
1279 rc = req_capsule_server_pack(&req->rq_pill);
1284 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1285 ldlm_convert_policy_to_local(req->rq_export,
1286 dlm_req->lock_desc.l_resource.lr_type,
1287 &dlm_req->lock_desc.l_policy_data,
1288 &lock->l_policy_data);
1289 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1290 lock->l_req_extent = lock->l_policy_data.l_extent;
1292 err = ldlm_lock_enqueue(ns, &lock, cookie, &flags);
1296 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1297 dlm_rep->lock_flags = ldlm_flags_to_wire(flags);
1299 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1300 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1302 /* We never send a blocking AST until the lock is granted, but
1303 * we can tell it right now */
1304 lock_res_and_lock(lock);
1306 /* Now take into account flags to be inherited from original lock
1307 request both in reply to client and in our own lock flags. */
1308 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1309 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
1310 LDLM_INHERIT_FLAGS);
1312 /* Don't move a pending lock onto the export if it has already been
1313 * disconnected due to eviction (bug 5683) or server umount (bug 24324).
1314 * Cancel it now instead. */
1315 if (unlikely(req->rq_export->exp_disconnected ||
1316 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1317 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1319 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1320 dlm_rep->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
1321 if (lock->l_granted_mode == lock->l_req_mode) {
1323 * Only cancel lock if it was granted, because it would
1324 * be destroyed immediately and would never be granted
1325 * in the future, causing timeouts on client. Not
1326 * granted lock will be cancelled immediately after
1327 * sending completion AST.
1329 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1330 unlock_res_and_lock(lock);
1331 ldlm_lock_cancel(lock);
1332 lock_res_and_lock(lock);
1334 ldlm_add_waiting_lock(lock);
1337 /* Make sure we never ever grant usual metadata locks to liblustre
1339 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1340 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1341 req->rq_export->exp_libclient) {
1342 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1343 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1344 CERROR("Granting sync lock to libclient. "
1345 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1346 dlm_req->lock_flags, dlm_rep->lock_flags,
1348 LDLM_ERROR(lock, "sync lock");
1349 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1350 struct ldlm_intent *it;
1352 it = req_capsule_client_get(&req->rq_pill,
1355 CERROR("This is intent %s ("LPU64")\n",
1356 ldlm_it2str(it->opc), it->opc);
1362 unlock_res_and_lock(lock);
1366 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1367 if (!req->rq_packed_final) {
1368 err = lustre_pack_reply(req, 1, NULL, NULL);
1373 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1374 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1376 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1377 "(err=%d, rc=%d)", err, rc);
1380 int lvb_len = ldlm_lvbo_size(lock);
1386 buf = req_capsule_server_get(&req->rq_pill,
1388 LASSERTF(buf != NULL, "req %p, lock %p\n",
1390 buflen = req_capsule_get_size(&req->rq_pill,
1391 &RMF_DLM_LVB, RCL_SERVER);
1392 buflen = ldlm_lvbo_fill(lock, buf, buflen);
1393 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB,
1394 buflen, RCL_SERVER);
1397 lock_res_and_lock(lock);
1398 ldlm_resource_unlink_lock(lock);
1399 ldlm_lock_destroy_nolock(lock);
1400 unlock_res_and_lock(lock);
1403 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1404 ldlm_reprocess_all(lock->l_resource);
1406 LDLM_LOCK_RELEASE(lock);
1409 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1414 EXPORT_SYMBOL(ldlm_handle_enqueue0);
1416 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1417 ldlm_completion_callback completion_callback,
1418 ldlm_blocking_callback blocking_callback,
1419 ldlm_glimpse_callback glimpse_callback)
1421 struct ldlm_request *dlm_req;
1422 struct ldlm_callback_suite cbs = {
1423 .lcs_completion = completion_callback,
1424 .lcs_blocking = blocking_callback,
1425 .lcs_glimpse = glimpse_callback
1429 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1430 if (dlm_req != NULL) {
1431 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1432 req, dlm_req, &cbs);
1438 EXPORT_SYMBOL(ldlm_handle_enqueue);
1440 int ldlm_handle_convert0(struct ptlrpc_request *req,
1441 const struct ldlm_request *dlm_req)
1443 struct ldlm_reply *dlm_rep;
1444 struct ldlm_lock *lock;
1448 if (req->rq_export && req->rq_export->exp_nid_stats &&
1449 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1450 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1451 LDLM_CONVERT - LDLM_FIRST_OPC);
1453 rc = req_capsule_server_pack(&req->rq_pill);
1457 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1458 dlm_rep->lock_flags = dlm_req->lock_flags;
1460 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1462 req->rq_status = EINVAL;
1466 LDLM_DEBUG(lock, "server-side convert handler START");
1468 lock->l_last_activity = cfs_time_current_sec();
1469 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1470 &dlm_rep->lock_flags);
1472 if (ldlm_del_waiting_lock(lock))
1473 LDLM_DEBUG(lock, "converted waiting lock");
1476 req->rq_status = EDEADLOCK;
1481 if (!req->rq_status)
1482 ldlm_reprocess_all(lock->l_resource);
1483 LDLM_DEBUG(lock, "server-side convert handler END");
1484 LDLM_LOCK_PUT(lock);
1486 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1490 EXPORT_SYMBOL(ldlm_handle_convert0);
1492 int ldlm_handle_convert(struct ptlrpc_request *req)
1495 struct ldlm_request *dlm_req;
1497 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1498 if (dlm_req != NULL) {
1499 rc = ldlm_handle_convert0(req, dlm_req);
1501 CERROR ("Can't unpack dlm_req\n");
1506 EXPORT_SYMBOL(ldlm_handle_convert);
1508 /* Cancel all the locks whos handles are packed into ldlm_request */
1509 int ldlm_request_cancel(struct ptlrpc_request *req,
1510 const struct ldlm_request *dlm_req, int first)
1512 struct ldlm_resource *res, *pres = NULL;
1513 struct ldlm_lock *lock;
1514 int i, count, done = 0;
1517 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1521 if (count == 1 && dlm_req->lock_handle[0].cookie == 0)
1524 /* There is no lock on the server at the replay time,
1525 * skip lock cancelling to make replay tests to pass. */
1526 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1529 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1530 "starting at %d", count, first);
1532 for (i = first; i < count; i++) {
1533 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1535 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1536 "lock (cookie "LPU64")",
1537 dlm_req->lock_handle[i].cookie);
1541 res = lock->l_resource;
1546 ldlm_reprocess_all(pres);
1547 LDLM_RESOURCE_DELREF(pres);
1548 ldlm_resource_putref(pres);
1551 ldlm_resource_getref(res);
1552 LDLM_RESOURCE_ADDREF(res);
1553 ldlm_res_lvbo_update(res, NULL, 1);
1557 ldlm_lock_cancel(lock);
1558 LDLM_LOCK_PUT(lock);
1561 ldlm_reprocess_all(pres);
1562 LDLM_RESOURCE_DELREF(pres);
1563 ldlm_resource_putref(pres);
1565 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1568 EXPORT_SYMBOL(ldlm_request_cancel);
1570 int ldlm_handle_cancel(struct ptlrpc_request *req)
1572 struct ldlm_request *dlm_req;
1576 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1577 if (dlm_req == NULL) {
1578 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1582 if (req->rq_export && req->rq_export->exp_nid_stats &&
1583 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1584 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1585 LDLM_CANCEL - LDLM_FIRST_OPC);
1587 rc = req_capsule_server_pack(&req->rq_pill);
1591 if (!ldlm_request_cancel(req, dlm_req, 0))
1592 req->rq_status = ESTALE;
1594 RETURN(ptlrpc_reply(req));
1596 EXPORT_SYMBOL(ldlm_handle_cancel);
1597 #endif /* HAVE_SERVER_SUPPORT */
1599 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1600 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1605 LDLM_DEBUG(lock, "client blocking AST callback handler");
1607 lock_res_and_lock(lock);
1608 lock->l_flags |= LDLM_FL_CBPENDING;
1610 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1611 lock->l_flags |= LDLM_FL_CANCEL;
1613 do_ast = (!lock->l_readers && !lock->l_writers);
1614 unlock_res_and_lock(lock);
1617 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1618 lock, lock->l_blocking_ast);
1619 if (lock->l_blocking_ast != NULL)
1620 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1623 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1627 LDLM_DEBUG(lock, "client blocking callback handler END");
1628 LDLM_LOCK_RELEASE(lock);
1632 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1633 struct ldlm_namespace *ns,
1634 struct ldlm_request *dlm_req,
1635 struct ldlm_lock *lock)
1638 CFS_LIST_HEAD(ast_list);
1641 LDLM_DEBUG(lock, "client completion callback handler START");
1643 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1644 int to = cfs_time_seconds(1);
1646 cfs_schedule_timeout_and_set_state(
1647 CFS_TASK_INTERRUPTIBLE, to);
1648 if (lock->l_granted_mode == lock->l_req_mode ||
1654 lvb_len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT);
1656 if (lock->l_lvb_len > 0) {
1657 /* for extent lock, lvb contains ost_lvb{}. */
1658 LASSERT(lock->l_lvb_data != NULL);
1659 LASSERTF(lock->l_lvb_len == lvb_len,
1660 "preallocated %d, actual %d.\n",
1661 lock->l_lvb_len, lvb_len);
1662 } else { /* for layout lock, lvb has variable length */
1665 OBD_ALLOC(lvb_data, lvb_len);
1666 if (lvb_data == NULL)
1667 LDLM_ERROR(lock, "no memory.\n");
1669 lock_res_and_lock(lock);
1670 if (lvb_data == NULL) {
1671 lock->l_flags |= LDLM_FL_FAILED;
1673 LASSERT(lock->l_lvb_data == NULL);
1674 lock->l_lvb_data = lvb_data;
1675 lock->l_lvb_len = lvb_len;
1677 unlock_res_and_lock(lock);
1681 lock_res_and_lock(lock);
1682 if (lock->l_destroyed ||
1683 lock->l_granted_mode == lock->l_req_mode) {
1684 /* bug 11300: the lock has already been granted */
1685 unlock_res_and_lock(lock);
1686 LDLM_DEBUG(lock, "Double grant race happened");
1687 LDLM_LOCK_RELEASE(lock);
1692 /* If we receive the completion AST before the actual enqueue returned,
1693 * then we might need to switch lock modes, resources, or extents. */
1694 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1695 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1696 LDLM_DEBUG(lock, "completion AST, new lock mode");
1699 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1700 ldlm_convert_policy_to_local(req->rq_export,
1701 dlm_req->lock_desc.l_resource.lr_type,
1702 &dlm_req->lock_desc.l_policy_data,
1703 &lock->l_policy_data);
1704 LDLM_DEBUG(lock, "completion AST, new policy data");
1707 ldlm_resource_unlink_lock(lock);
1708 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1709 &lock->l_resource->lr_name,
1710 sizeof(lock->l_resource->lr_name)) != 0) {
1711 unlock_res_and_lock(lock);
1712 if (ldlm_lock_change_resource(ns, lock,
1713 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1714 LDLM_ERROR(lock, "Failed to allocate resource");
1715 LDLM_LOCK_RELEASE(lock);
1719 LDLM_DEBUG(lock, "completion AST, new resource");
1720 CERROR("change resource!\n");
1721 lock_res_and_lock(lock);
1724 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1725 /* BL_AST locks are not needed in lru.
1726 * let ldlm_cancel_lru() be fast. */
1727 ldlm_lock_remove_from_lru(lock);
1728 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1729 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1732 if (lock->l_lvb_len) {
1733 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1734 RCL_CLIENT) < lock->l_lvb_len) {
1735 LDLM_ERROR(lock, "completion AST did not contain "
1738 void *lvb = req_capsule_client_get(&req->rq_pill,
1740 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1744 ldlm_grant_lock(lock, &ast_list);
1745 unlock_res_and_lock(lock);
1747 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1749 /* Let Enqueue to call osc_lock_upcall() and initialize
1751 OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1753 ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
1755 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1757 LDLM_LOCK_RELEASE(lock);
1761 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1762 struct ldlm_namespace *ns,
1763 struct ldlm_request *dlm_req,
1764 struct ldlm_lock *lock)
1769 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1771 if (lock->l_glimpse_ast != NULL)
1772 rc = lock->l_glimpse_ast(lock, req);
1774 if (req->rq_repmsg != NULL) {
1777 req->rq_status = rc;
1781 lock_res_and_lock(lock);
1782 if (lock->l_granted_mode == LCK_PW &&
1783 !lock->l_readers && !lock->l_writers &&
1784 cfs_time_after(cfs_time_current(),
1785 cfs_time_add(lock->l_last_used,
1786 cfs_time_seconds(10)))) {
1787 unlock_res_and_lock(lock);
1788 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1789 ldlm_handle_bl_callback(ns, NULL, lock);
1794 unlock_res_and_lock(lock);
1795 LDLM_LOCK_RELEASE(lock);
1799 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1801 if (req->rq_no_reply)
1804 req->rq_status = rc;
1805 if (!req->rq_packed_final) {
1806 rc = lustre_pack_reply(req, 1, NULL, NULL);
1810 return ptlrpc_reply(req);
1814 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi, int mode)
1816 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1819 cfs_spin_lock(&blp->blp_lock);
1820 if (blwi->blwi_lock && blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1821 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1822 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1824 /* other blocking callbacks are added to the regular list */
1825 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1827 cfs_spin_unlock(&blp->blp_lock);
1829 cfs_waitq_signal(&blp->blp_waitq);
1831 /* can not use blwi->blwi_mode as blwi could be already freed in
1833 if (mode == LDLM_SYNC)
1834 cfs_wait_for_completion(&blwi->blwi_comp);
1839 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1840 struct ldlm_namespace *ns,
1841 struct ldlm_lock_desc *ld,
1842 cfs_list_t *cancels, int count,
1843 struct ldlm_lock *lock,
1846 cfs_init_completion(&blwi->blwi_comp);
1847 CFS_INIT_LIST_HEAD(&blwi->blwi_head);
1849 if (cfs_memory_pressure_get())
1850 blwi->blwi_mem_pressure = 1;
1853 blwi->blwi_mode = mode;
1855 blwi->blwi_ld = *ld;
1857 cfs_list_add(&blwi->blwi_head, cancels);
1858 cfs_list_del_init(cancels);
1859 blwi->blwi_count = count;
1861 blwi->blwi_lock = lock;
1865 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1866 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1867 cfs_list_t *cancels, int count, int mode)
1871 if (cancels && count == 0)
1874 if (mode == LDLM_SYNC) {
1875 /* if it is synchronous call do minimum mem alloc, as it could
1876 * be triggered from kernel shrinker
1878 struct ldlm_bl_work_item blwi;
1879 memset(&blwi, 0, sizeof(blwi));
1880 init_blwi(&blwi, ns, ld, cancels, count, lock, LDLM_SYNC);
1881 RETURN(__ldlm_bl_to_thread(&blwi, LDLM_SYNC));
1883 struct ldlm_bl_work_item *blwi;
1884 OBD_ALLOC(blwi, sizeof(*blwi));
1887 init_blwi(blwi, ns, ld, cancels, count, lock, LDLM_ASYNC);
1889 RETURN(__ldlm_bl_to_thread(blwi, LDLM_ASYNC));
1895 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1896 struct ldlm_lock *lock)
1899 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
1905 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1906 cfs_list_t *cancels, int count, int mode)
1909 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
1915 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
1916 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
1918 struct obd_device *obd = req->rq_export->exp_obd;
1925 DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
1927 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
1929 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1931 DEBUG_REQ(D_IOCTL, req, "no set_info key");
1934 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1936 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1938 DEBUG_REQ(D_IOCTL, req, "no set_info val");
1941 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1944 /* We are responsible for swabbing contents of val */
1946 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
1947 /* Pass it on to mdc (the "export" in this case) */
1948 rc = obd_set_info_async(req->rq_svc_thread->t_env,
1950 sizeof(KEY_HSM_COPYTOOL_SEND),
1951 KEY_HSM_COPYTOOL_SEND,
1954 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
1959 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
1960 const char *msg, int rc,
1961 struct lustre_handle *handle)
1963 DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
1964 "%s: [nid %s] [rc %d] [lock "LPX64"]",
1965 msg, libcfs_id2str(req->rq_peer), rc,
1966 handle ? handle->cookie : 0);
1967 if (req->rq_no_reply)
1968 CWARN("No reply was sent, maybe cause bug 21636.\n");
1970 CWARN("Send reply failed, maybe cause bug 21636.\n");
1973 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1974 static int ldlm_callback_handler(struct ptlrpc_request *req)
1976 struct ldlm_namespace *ns;
1977 struct ldlm_request *dlm_req;
1978 struct ldlm_lock *lock;
1982 /* Requests arrive in sender's byte order. The ptlrpc service
1983 * handler has already checked and, if necessary, byte-swapped the
1984 * incoming request message body, but I am responsible for the
1985 * message buffers. */
1987 /* do nothing for sec context finalize */
1988 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1991 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1993 if (req->rq_export == NULL) {
1994 rc = ldlm_callback_reply(req, -ENOTCONN);
1995 ldlm_callback_errmsg(req, "Operate on unconnected server",
2000 LASSERT(req->rq_export != NULL);
2001 LASSERT(req->rq_export->exp_obd != NULL);
2003 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2004 case LDLM_BL_CALLBACK:
2005 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
2008 case LDLM_CP_CALLBACK:
2009 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK_NET))
2012 case LDLM_GL_CALLBACK:
2013 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK_NET))
2017 rc = ldlm_handle_setinfo(req);
2018 ldlm_callback_reply(req, rc);
2020 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
2021 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
2022 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2023 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2025 rc = llog_origin_handle_cancel(req);
2026 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2028 ldlm_callback_reply(req, rc);
2030 case LLOG_ORIGIN_HANDLE_CREATE:
2031 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2032 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2034 rc = llog_origin_handle_open(req);
2035 ldlm_callback_reply(req, rc);
2037 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2038 req_capsule_set(&req->rq_pill,
2039 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2040 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2042 rc = llog_origin_handle_next_block(req);
2043 ldlm_callback_reply(req, rc);
2045 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2046 req_capsule_set(&req->rq_pill,
2047 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2048 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2050 rc = llog_origin_handle_read_header(req);
2051 ldlm_callback_reply(req, rc);
2053 case LLOG_ORIGIN_HANDLE_CLOSE:
2054 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2056 rc = llog_origin_handle_close(req);
2057 ldlm_callback_reply(req, rc);
2060 CERROR("unknown opcode %u\n",
2061 lustre_msg_get_opc(req->rq_reqmsg));
2062 ldlm_callback_reply(req, -EPROTO);
2066 ns = req->rq_export->exp_obd->obd_namespace;
2067 LASSERT(ns != NULL);
2069 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2071 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2072 if (dlm_req == NULL) {
2073 rc = ldlm_callback_reply(req, -EPROTO);
2074 ldlm_callback_errmsg(req, "Operate without parameter", rc,
2079 /* Force a known safe race, send a cancel to the server for a lock
2080 * which the server has already started a blocking callback on. */
2081 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
2082 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2083 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
2085 CERROR("ldlm_cli_cancel: %d\n", rc);
2088 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
2090 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
2091 "disappeared\n", dlm_req->lock_handle[0].cookie);
2092 rc = ldlm_callback_reply(req, -EINVAL);
2093 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
2094 &dlm_req->lock_handle[0]);
2098 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
2099 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
2100 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
2102 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
2103 lock_res_and_lock(lock);
2104 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
2106 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2107 /* If somebody cancels lock and cache is already dropped,
2108 * or lock is failed before cp_ast received on client,
2109 * we can tell the server we have no lock. Otherwise, we
2110 * should send cancel after dropping the cache. */
2111 if (((lock->l_flags & LDLM_FL_CANCELING) &&
2112 (lock->l_flags & LDLM_FL_BL_DONE)) ||
2113 (lock->l_flags & LDLM_FL_FAILED)) {
2114 LDLM_DEBUG(lock, "callback on lock "
2115 LPX64" - lock disappeared\n",
2116 dlm_req->lock_handle[0].cookie);
2117 unlock_res_and_lock(lock);
2118 LDLM_LOCK_RELEASE(lock);
2119 rc = ldlm_callback_reply(req, -EINVAL);
2120 ldlm_callback_errmsg(req, "Operate on stale lock", rc,
2121 &dlm_req->lock_handle[0]);
2124 /* BL_AST locks are not needed in lru.
2125 * let ldlm_cancel_lru() be fast. */
2126 ldlm_lock_remove_from_lru(lock);
2127 lock->l_flags |= LDLM_FL_BL_AST;
2129 unlock_res_and_lock(lock);
2131 /* We want the ost thread to get this reply so that it can respond
2132 * to ost requests (write cache writeback) that might be triggered
2135 * But we'd also like to be able to indicate in the reply that we're
2136 * cancelling right now, because it's unused, or have an intent result
2137 * in the reply, so we might have to push the responsibility for sending
2138 * the reply down into the AST handlers, alas. */
2140 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2141 case LDLM_BL_CALLBACK:
2142 CDEBUG(D_INODE, "blocking ast\n");
2143 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
2144 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
2145 rc = ldlm_callback_reply(req, 0);
2146 if (req->rq_no_reply || rc)
2147 ldlm_callback_errmsg(req, "Normal process", rc,
2148 &dlm_req->lock_handle[0]);
2150 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
2151 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
2153 case LDLM_CP_CALLBACK:
2154 CDEBUG(D_INODE, "completion ast\n");
2155 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
2156 ldlm_callback_reply(req, 0);
2157 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
2159 case LDLM_GL_CALLBACK:
2160 CDEBUG(D_INODE, "glimpse ast\n");
2161 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
2162 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
2165 LBUG(); /* checked above */
2171 #ifdef HAVE_SERVER_SUPPORT
2172 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2177 /* Requests arrive in sender's byte order. The ptlrpc service
2178 * handler has already checked and, if necessary, byte-swapped the
2179 * incoming request message body, but I am responsible for the
2180 * message buffers. */
2182 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2184 if (req->rq_export == NULL) {
2185 struct ldlm_request *dlm_req;
2187 CERROR("%s from %s arrived at %lu with bad export cookie "
2189 ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2190 libcfs_nid2str(req->rq_peer.nid),
2191 req->rq_arrival_time.tv_sec,
2192 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2194 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2195 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2196 dlm_req = req_capsule_client_get(&req->rq_pill,
2198 if (dlm_req != NULL)
2199 ldlm_lock_dump_handle(D_ERROR,
2200 &dlm_req->lock_handle[0]);
2202 ldlm_callback_reply(req, -ENOTCONN);
2206 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2208 /* XXX FIXME move this back to mds/handler.c, bug 249 */
2210 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2211 CDEBUG(D_INODE, "cancel\n");
2212 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_NET))
2214 rc = ldlm_handle_cancel(req);
2218 case OBD_LOG_CANCEL:
2219 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2220 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2222 rc = llog_origin_handle_cancel(req);
2223 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2225 ldlm_callback_reply(req, rc);
2228 CERROR("invalid opcode %d\n",
2229 lustre_msg_get_opc(req->rq_reqmsg));
2230 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2231 ldlm_callback_reply(req, -EINVAL);
2237 static int ldlm_cancel_hpreq_lock_match(struct ptlrpc_request *req,
2238 struct ldlm_lock *lock)
2240 struct ldlm_request *dlm_req;
2241 struct lustre_handle lockh;
2246 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2247 if (dlm_req == NULL)
2250 ldlm_lock2handle(lock, &lockh);
2251 for (i = 0; i < dlm_req->lock_count; i++) {
2252 if (lustre_handle_equal(&dlm_req->lock_handle[i],
2254 DEBUG_REQ(D_RPCTRACE, req,
2255 "Prio raised by lock "LPX64".", lockh.cookie);
2266 static int ldlm_cancel_hpreq_check(struct ptlrpc_request *req)
2268 struct ldlm_request *dlm_req;
2273 /* no prolong in recovery */
2274 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2277 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2278 if (dlm_req == NULL)
2281 for (i = 0; i < dlm_req->lock_count; i++) {
2282 struct ldlm_lock *lock;
2284 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
2288 rc = !!(lock->l_flags & LDLM_FL_AST_SENT);
2290 LDLM_DEBUG(lock, "hpreq cancel lock");
2291 LDLM_LOCK_PUT(lock);
2300 static struct ptlrpc_hpreq_ops ldlm_cancel_hpreq_ops = {
2301 .hpreq_lock_match = ldlm_cancel_hpreq_lock_match,
2302 .hpreq_check = ldlm_cancel_hpreq_check,
2306 static int ldlm_hpreq_handler(struct ptlrpc_request *req)
2310 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2312 if (req->rq_export == NULL)
2315 if (LDLM_CANCEL == lustre_msg_get_opc(req->rq_reqmsg)) {
2316 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2317 req->rq_ops = &ldlm_cancel_hpreq_ops;
2322 int ldlm_revoke_lock_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
2323 cfs_hlist_node_t *hnode, void *data)
2326 cfs_list_t *rpc_list = data;
2327 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
2329 lock_res_and_lock(lock);
2331 if (lock->l_req_mode != lock->l_granted_mode) {
2332 unlock_res_and_lock(lock);
2336 LASSERT(lock->l_resource);
2337 if (lock->l_resource->lr_type != LDLM_IBITS &&
2338 lock->l_resource->lr_type != LDLM_PLAIN) {
2339 unlock_res_and_lock(lock);
2343 if (lock->l_flags & LDLM_FL_AST_SENT) {
2344 unlock_res_and_lock(lock);
2348 LASSERT(lock->l_blocking_ast);
2349 LASSERT(!lock->l_blocking_lock);
2351 lock->l_flags |= LDLM_FL_AST_SENT;
2352 if (lock->l_export && lock->l_export->exp_lock_hash) {
2353 /* NB: it's safe to call cfs_hash_del() even lock isn't
2354 * in exp_lock_hash. */
2355 /* In the function below, .hs_keycmp resolves to
2356 * ldlm_export_lock_keycmp() */
2357 /* coverity[overrun-buffer-val] */
2358 cfs_hash_del(lock->l_export->exp_lock_hash,
2359 &lock->l_remote_handle, &lock->l_exp_hash);
2362 cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2363 LDLM_LOCK_GET(lock);
2365 unlock_res_and_lock(lock);
2369 void ldlm_revoke_export_locks(struct obd_export *exp)
2371 cfs_list_t rpc_list;
2374 CFS_INIT_LIST_HEAD(&rpc_list);
2375 cfs_hash_for_each_empty(exp->exp_lock_hash,
2376 ldlm_revoke_lock_cb, &rpc_list);
2377 ldlm_run_ast_work(exp->exp_obd->obd_namespace, &rpc_list,
2378 LDLM_WORK_REVOKE_AST);
2382 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2383 #endif /* HAVE_SERVER_SUPPORT */
2386 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2388 struct ldlm_bl_work_item *blwi = NULL;
2389 static unsigned int num_bl = 0;
2391 cfs_spin_lock(&blp->blp_lock);
2392 /* process a request from the blp_list at least every blp_num_threads */
2393 if (!cfs_list_empty(&blp->blp_list) &&
2394 (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2395 blwi = cfs_list_entry(blp->blp_list.next,
2396 struct ldlm_bl_work_item, blwi_entry);
2398 if (!cfs_list_empty(&blp->blp_prio_list))
2399 blwi = cfs_list_entry(blp->blp_prio_list.next,
2400 struct ldlm_bl_work_item,
2404 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2406 cfs_list_del(&blwi->blwi_entry);
2408 cfs_spin_unlock(&blp->blp_lock);
2413 /* This only contains temporary data until the thread starts */
2414 struct ldlm_bl_thread_data {
2415 char bltd_name[CFS_CURPROC_COMM_MAX];
2416 struct ldlm_bl_pool *bltd_blp;
2417 cfs_completion_t bltd_comp;
2421 static int ldlm_bl_thread_main(void *arg);
2423 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2425 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2428 cfs_init_completion(&bltd.bltd_comp);
2429 rc = cfs_create_thread(ldlm_bl_thread_main, &bltd, 0);
2431 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2432 cfs_atomic_read(&blp->blp_num_threads), rc);
2435 cfs_wait_for_completion(&bltd.bltd_comp);
2440 static int ldlm_bl_thread_main(void *arg)
2442 struct ldlm_bl_pool *blp;
2446 struct ldlm_bl_thread_data *bltd = arg;
2448 blp = bltd->bltd_blp;
2451 cfs_atomic_inc_return(&blp->blp_num_threads) - 1;
2452 cfs_atomic_inc(&blp->blp_busy_threads);
2454 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2455 "ldlm_bl_%02d", bltd->bltd_num);
2456 cfs_daemonize(bltd->bltd_name);
2458 cfs_complete(&bltd->bltd_comp);
2459 /* cannot use bltd after this, it is only on caller's stack */
2463 struct l_wait_info lwi = { 0 };
2464 struct ldlm_bl_work_item *blwi = NULL;
2467 blwi = ldlm_bl_get_work(blp);
2470 cfs_atomic_dec(&blp->blp_busy_threads);
2471 l_wait_event_exclusive(blp->blp_waitq,
2472 (blwi = ldlm_bl_get_work(blp)) != NULL,
2474 busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2476 busy = cfs_atomic_read(&blp->blp_busy_threads);
2479 if (blwi->blwi_ns == NULL)
2480 /* added by ldlm_cleanup() */
2483 /* Not fatal if racy and have a few too many threads */
2484 if (unlikely(busy < blp->blp_max_threads &&
2485 busy >= cfs_atomic_read(&blp->blp_num_threads) &&
2486 !blwi->blwi_mem_pressure))
2487 /* discard the return value, we tried */
2488 ldlm_bl_thread_start(blp);
2490 if (blwi->blwi_mem_pressure)
2491 cfs_memory_pressure_set();
2493 if (blwi->blwi_count) {
2495 /* The special case when we cancel locks in lru
2496 * asynchronously, we pass the list of locks here.
2497 * Thus locks are marked LDLM_FL_CANCELING, but NOT
2498 * canceled locally yet. */
2499 count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2502 ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL, 0);
2504 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2507 if (blwi->blwi_mem_pressure)
2508 cfs_memory_pressure_clr();
2510 if (blwi->blwi_mode == LDLM_ASYNC)
2511 OBD_FREE(blwi, sizeof(*blwi));
2513 cfs_complete(&blwi->blwi_comp);
2516 cfs_atomic_dec(&blp->blp_busy_threads);
2517 cfs_atomic_dec(&blp->blp_num_threads);
2518 cfs_complete(&blp->blp_comp);
2524 static int ldlm_setup(void);
2525 static int ldlm_cleanup(void);
2527 int ldlm_get_ref(void)
2531 cfs_mutex_lock(&ldlm_ref_mutex);
2532 if (++ldlm_refcount == 1) {
2537 cfs_mutex_unlock(&ldlm_ref_mutex);
2541 EXPORT_SYMBOL(ldlm_get_ref);
2543 void ldlm_put_ref(void)
2546 cfs_mutex_lock(&ldlm_ref_mutex);
2547 if (ldlm_refcount == 1) {
2548 int rc = ldlm_cleanup();
2550 CERROR("ldlm_cleanup failed: %d\n", rc);
2556 cfs_mutex_unlock(&ldlm_ref_mutex);
2560 EXPORT_SYMBOL(ldlm_put_ref);
2563 * Export handle<->lock hash operations.
2566 ldlm_export_lock_hash(cfs_hash_t *hs, const void *key, unsigned mask)
2568 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2572 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2574 struct ldlm_lock *lock;
2576 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2577 return &lock->l_remote_handle;
2581 ldlm_export_lock_keycpy(cfs_hlist_node_t *hnode, void *key)
2583 struct ldlm_lock *lock;
2585 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2586 lock->l_remote_handle = *(struct lustre_handle *)key;
2590 ldlm_export_lock_keycmp(const void *key, cfs_hlist_node_t *hnode)
2592 return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
2596 ldlm_export_lock_object(cfs_hlist_node_t *hnode)
2598 return cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2602 ldlm_export_lock_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2604 struct ldlm_lock *lock;
2606 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2607 LDLM_LOCK_GET(lock);
2611 ldlm_export_lock_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2613 struct ldlm_lock *lock;
2615 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2616 LDLM_LOCK_RELEASE(lock);
2619 static cfs_hash_ops_t ldlm_export_lock_ops = {
2620 .hs_hash = ldlm_export_lock_hash,
2621 .hs_key = ldlm_export_lock_key,
2622 .hs_keycmp = ldlm_export_lock_keycmp,
2623 .hs_keycpy = ldlm_export_lock_keycpy,
2624 .hs_object = ldlm_export_lock_object,
2625 .hs_get = ldlm_export_lock_get,
2626 .hs_put = ldlm_export_lock_put,
2627 .hs_put_locked = ldlm_export_lock_put,
2630 int ldlm_init_export(struct obd_export *exp)
2634 exp->exp_lock_hash =
2635 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2636 HASH_EXP_LOCK_CUR_BITS,
2637 HASH_EXP_LOCK_MAX_BITS,
2638 HASH_EXP_LOCK_BKT_BITS, 0,
2639 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
2640 &ldlm_export_lock_ops,
2641 CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
2642 CFS_HASH_NBLK_CHANGE);
2644 if (!exp->exp_lock_hash)
2649 EXPORT_SYMBOL(ldlm_init_export);
2651 void ldlm_destroy_export(struct obd_export *exp)
2654 cfs_hash_putref(exp->exp_lock_hash);
2655 exp->exp_lock_hash = NULL;
2657 ldlm_destroy_flock_export(exp);
2660 EXPORT_SYMBOL(ldlm_destroy_export);
2662 static int ldlm_setup(void)
2664 static struct ptlrpc_service_conf conf;
2665 struct ldlm_bl_pool *blp = NULL;
2672 if (ldlm_state != NULL)
2675 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2676 if (ldlm_state == NULL)
2680 rc = ldlm_proc_setup();
2685 memset(&conf, 0, sizeof(conf));
2686 conf = (typeof(conf)) {
2687 .psc_name = "ldlm_cbd",
2688 .psc_watchdog_factor = 2,
2690 .bc_nbufs = LDLM_NBUFS,
2691 .bc_buf_size = LDLM_BUFSIZE,
2692 .bc_req_max_size = LDLM_MAXREQSIZE,
2693 .bc_rep_max_size = LDLM_MAXREPSIZE,
2694 .bc_req_portal = LDLM_CB_REQUEST_PORTAL,
2695 .bc_rep_portal = LDLM_CB_REPLY_PORTAL,
2698 .tc_thr_name = "ldlm_cb",
2699 .tc_thr_factor = LDLM_THR_FACTOR,
2700 .tc_nthrs_init = LDLM_NTHRS_INIT,
2701 .tc_nthrs_base = LDLM_NTHRS_BASE,
2702 .tc_nthrs_max = LDLM_NTHRS_MAX,
2703 .tc_nthrs_user = ldlm_num_threads,
2704 .tc_cpu_affinity = 1,
2705 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD,
2708 .cc_pattern = ldlm_cpts,
2711 .so_req_handler = ldlm_callback_handler,
2714 ldlm_state->ldlm_cb_service = \
2715 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2716 if (IS_ERR(ldlm_state->ldlm_cb_service)) {
2717 CERROR("failed to start service\n");
2718 rc = PTR_ERR(ldlm_state->ldlm_cb_service);
2719 ldlm_state->ldlm_cb_service = NULL;
2723 #ifdef HAVE_SERVER_SUPPORT
2724 memset(&conf, 0, sizeof(conf));
2725 conf = (typeof(conf)) {
2726 .psc_name = "ldlm_canceld",
2727 .psc_watchdog_factor = 6,
2729 .bc_nbufs = LDLM_NBUFS,
2730 .bc_buf_size = LDLM_BUFSIZE,
2731 .bc_req_max_size = LDLM_MAXREQSIZE,
2732 .bc_rep_max_size = LDLM_MAXREPSIZE,
2733 .bc_req_portal = LDLM_CANCEL_REQUEST_PORTAL,
2734 .bc_rep_portal = LDLM_CANCEL_REPLY_PORTAL,
2738 .tc_thr_name = "ldlm_cn",
2739 .tc_thr_factor = LDLM_THR_FACTOR,
2740 .tc_nthrs_init = LDLM_NTHRS_INIT,
2741 .tc_nthrs_base = LDLM_NTHRS_BASE,
2742 .tc_nthrs_max = LDLM_NTHRS_MAX,
2743 .tc_nthrs_user = ldlm_num_threads,
2744 .tc_cpu_affinity = 1,
2745 .tc_ctx_tags = LCT_MD_THREAD | \
2750 .cc_pattern = ldlm_cpts,
2753 .so_req_handler = ldlm_cancel_handler,
2754 .so_hpreq_handler = ldlm_hpreq_handler,
2757 ldlm_state->ldlm_cancel_service = \
2758 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2759 if (IS_ERR(ldlm_state->ldlm_cancel_service)) {
2760 CERROR("failed to start service\n");
2761 rc = PTR_ERR(ldlm_state->ldlm_cancel_service);
2762 ldlm_state->ldlm_cancel_service = NULL;
2767 OBD_ALLOC(blp, sizeof(*blp));
2769 GOTO(out, rc = -ENOMEM);
2770 ldlm_state->ldlm_bl_pool = blp;
2772 cfs_spin_lock_init(&blp->blp_lock);
2773 CFS_INIT_LIST_HEAD(&blp->blp_list);
2774 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2775 cfs_waitq_init(&blp->blp_waitq);
2776 cfs_atomic_set(&blp->blp_num_threads, 0);
2777 cfs_atomic_set(&blp->blp_busy_threads, 0);
2780 if (ldlm_num_threads == 0) {
2781 blp->blp_min_threads = LDLM_NTHRS_INIT;
2782 blp->blp_max_threads = LDLM_NTHRS_MAX;
2784 blp->blp_min_threads = blp->blp_max_threads = \
2785 min_t(int, LDLM_NTHRS_MAX, max_t(int, LDLM_NTHRS_INIT,
2789 for (i = 0; i < blp->blp_min_threads; i++) {
2790 rc = ldlm_bl_thread_start(blp);
2795 # ifdef HAVE_SERVER_SUPPORT
2796 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2797 expired_lock_thread.elt_state = ELT_STOPPED;
2798 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2800 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2801 cfs_spin_lock_init(&waiting_locks_spinlock);
2802 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2804 rc = cfs_create_thread(expired_lock_main, NULL, CFS_DAEMON_FLAGS);
2806 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2810 cfs_wait_event(expired_lock_thread.elt_waitq,
2811 expired_lock_thread.elt_state == ELT_READY);
2812 # endif /* HAVE_SERVER_SUPPORT */
2814 rc = ldlm_pools_init();
2816 CERROR("Failed to initialize LDLM pools: %d\n", rc);
2827 static int ldlm_cleanup(void)
2831 if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2832 !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2833 CERROR("ldlm still has namespaces; clean these up first.\n");
2834 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2835 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2842 if (ldlm_state->ldlm_bl_pool != NULL) {
2843 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2845 while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2846 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2848 cfs_init_completion(&blp->blp_comp);
2850 cfs_spin_lock(&blp->blp_lock);
2851 cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2852 cfs_waitq_signal(&blp->blp_waitq);
2853 cfs_spin_unlock(&blp->blp_lock);
2855 cfs_wait_for_completion(&blp->blp_comp);
2858 OBD_FREE(blp, sizeof(*blp));
2860 #endif /* __KERNEL__ */
2862 if (ldlm_state->ldlm_cb_service != NULL)
2863 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2864 # ifdef HAVE_SERVER_SUPPORT
2865 if (ldlm_state->ldlm_cancel_service != NULL)
2866 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2870 ldlm_proc_cleanup();
2872 # ifdef HAVE_SERVER_SUPPORT
2873 if (expired_lock_thread.elt_state != ELT_STOPPED) {
2874 expired_lock_thread.elt_state = ELT_TERMINATE;
2875 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2876 cfs_wait_event(expired_lock_thread.elt_waitq,
2877 expired_lock_thread.elt_state == ELT_STOPPED);
2880 #endif /* __KERNEL__ */
2882 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2890 cfs_mutex_init(&ldlm_ref_mutex);
2891 cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2892 cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2893 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2894 sizeof(struct ldlm_resource), 0,
2895 CFS_SLAB_HWCACHE_ALIGN);
2896 if (ldlm_resource_slab == NULL)
2899 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2900 sizeof(struct ldlm_lock), 0,
2901 CFS_SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU);
2902 if (ldlm_lock_slab == NULL) {
2903 cfs_mem_cache_destroy(ldlm_resource_slab);
2907 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2908 sizeof(struct ldlm_interval),
2909 0, CFS_SLAB_HWCACHE_ALIGN);
2910 if (ldlm_interval_slab == NULL) {
2911 cfs_mem_cache_destroy(ldlm_resource_slab);
2912 cfs_mem_cache_destroy(ldlm_lock_slab);
2915 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2916 class_export_dump_hook = ldlm_dump_export_locks;
2921 void ldlm_exit(void)
2925 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2926 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2927 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2929 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2930 * synchronize_rcu() to wait a grace period elapsed, so that
2931 * ldlm_lock_free() get a chance to be called. */
2934 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2935 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2936 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2937 LASSERTF(rc == 0, "couldn't free interval node slab\n");