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");
558 * Called with namespace lock held.
560 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
562 if (lock->l_export == NULL) {
563 /* We don't have a "waiting locks list" on clients. */
564 LDLM_DEBUG(lock, "client lock: no-op");
568 cfs_spin_lock_bh(&waiting_locks_spinlock);
570 if (cfs_list_empty(&lock->l_pending_chain)) {
571 cfs_spin_unlock_bh(&waiting_locks_spinlock);
572 LDLM_DEBUG(lock, "wasn't waiting");
576 /* we remove/add the lock to the waiting list, so no needs to
577 * release/take a lock reference */
578 __ldlm_del_waiting_lock(lock);
579 __ldlm_add_waiting_lock(lock, timeout);
580 cfs_spin_unlock_bh(&waiting_locks_spinlock);
582 LDLM_DEBUG(lock, "refreshed");
586 #else /* !HAVE_SERVER_SUPPORT || !__KERNEL__ */
588 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
593 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
598 # ifdef HAVE_SERVER_SUPPORT
599 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
601 LASSERT(lock->l_res_locked);
602 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
607 #endif /* HAVE_SERVER_SUPPORT && __KERNEL__ */
609 #ifdef HAVE_SERVER_SUPPORT
611 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
612 const char *ast_type)
614 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
615 "to a lock %s callback time out: rc %d\n",
616 lock->l_export->exp_obd->obd_name,
617 obd_export_nid2str(lock->l_export), ast_type, rc);
619 if (obd_dump_on_timeout)
620 libcfs_debug_dumplog();
622 cfs_spin_lock_bh(&waiting_locks_spinlock);
623 if (__ldlm_del_waiting_lock(lock) == 0)
624 /* the lock was not in any list, grab an extra ref before adding
625 * the lock to the expired list */
627 cfs_list_add(&lock->l_pending_chain,
628 &expired_lock_thread.elt_expired_locks);
629 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
630 cfs_spin_unlock_bh(&waiting_locks_spinlock);
632 class_fail_export(lock->l_export);
636 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
637 struct ptlrpc_request *req, int rc,
638 const char *ast_type)
640 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
642 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
643 LASSERT(lock->l_export);
644 if (lock->l_export->exp_libclient) {
645 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
646 " timeout, just cancelling lock", ast_type,
647 libcfs_nid2str(peer.nid));
648 ldlm_lock_cancel(lock);
650 } else if (lock->l_flags & LDLM_FL_CANCEL) {
651 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
652 "cancel was received (AST reply lost?)",
653 ast_type, libcfs_nid2str(peer.nid));
654 ldlm_lock_cancel(lock);
657 ldlm_del_waiting_lock(lock);
658 ldlm_failed_ast(lock, rc, ast_type);
662 struct ldlm_resource *res = lock->l_resource;
663 LDLM_DEBUG(lock, "client (nid %s) returned %d"
664 " from %s AST - normal race",
665 libcfs_nid2str(peer.nid),
667 lustre_msg_get_status(req->rq_repmsg) : -1,
670 /* update lvbo to return proper attributes.
672 ldlm_resource_getref(res);
673 ldlm_res_lvbo_update(res, NULL, 1);
674 ldlm_resource_putref(res);
678 LDLM_ERROR(lock, "client (nid %s) returned %d "
679 "from %s AST", libcfs_nid2str(peer.nid),
680 (req->rq_repmsg != NULL) ?
681 lustre_msg_get_status(req->rq_repmsg) : 0,
684 ldlm_lock_cancel(lock);
685 /* Server-side AST functions are called from ldlm_reprocess_all,
686 * which needs to be told to please restart its reprocessing. */
693 static int ldlm_cb_interpret(const struct lu_env *env,
694 struct ptlrpc_request *req, void *data, int rc)
696 struct ldlm_cb_async_args *ca = data;
697 struct ldlm_lock *lock = ca->ca_lock;
698 struct ldlm_cb_set_arg *arg = ca->ca_set_arg;
701 LASSERT(lock != NULL);
704 case LDLM_GL_CALLBACK:
705 /* Update the LVB from disk if the AST failed
706 * (this is a legal race)
708 * - Glimpse callback of local lock just returns
709 * -ELDLM_NO_LOCK_DATA.
710 * - Glimpse callback of remote lock might return
711 * -ELDLM_NO_LOCK_DATA when inode is cleared. LU-274
713 if (rc == -ELDLM_NO_LOCK_DATA) {
714 LDLM_DEBUG(lock, "lost race - client has a lock but no "
716 ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
717 } else if (rc != 0) {
718 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
720 rc = ldlm_res_lvbo_update(lock->l_resource, req, 1);
723 case LDLM_BL_CALLBACK:
725 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
727 case LDLM_CP_CALLBACK:
729 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
732 LDLM_ERROR(lock, "invalid opcode for lock callback %d",
737 /* release extra reference taken in ldlm_ast_fini() */
738 LDLM_LOCK_RELEASE(lock);
741 cfs_atomic_inc(&arg->restart);
746 static inline int ldlm_ast_fini(struct ptlrpc_request *req,
747 struct ldlm_cb_set_arg *arg,
748 struct ldlm_lock *lock,
754 if (unlikely(instant_cancel)) {
755 rc = ptl_send_rpc(req, 1);
756 ptlrpc_req_finished(req);
758 cfs_atomic_inc(&arg->restart);
761 ptlrpc_set_add_req(arg->set, req);
768 * Check if there are requests in the export request list which prevent
769 * the lock canceling and make these requests high priority ones.
771 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
773 struct ptlrpc_request *req;
776 if (lock->l_export == NULL) {
777 LDLM_DEBUG(lock, "client lock: no-op");
781 cfs_spin_lock_bh(&lock->l_export->exp_rpc_lock);
782 cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
784 /* Do not process requests that were not yet added to there
785 * incoming queue or were already removed from there for
787 if (!req->rq_hp && !cfs_list_empty(&req->rq_list) &&
788 req->rq_ops->hpreq_lock_match &&
789 req->rq_ops->hpreq_lock_match(req, lock))
790 ptlrpc_hpreq_reorder(req);
792 cfs_spin_unlock_bh(&lock->l_export->exp_rpc_lock);
797 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
798 * enqueued server lock conflicts with given one.
800 * Sends blocking ast rpc to the client owning that lock; arms timeout timer
801 * to wait for client response.
803 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
804 struct ldlm_lock_desc *desc,
805 void *data, int flag)
807 struct ldlm_cb_async_args *ca;
808 struct ldlm_cb_set_arg *arg = data;
809 struct ldlm_request *body;
810 struct ptlrpc_request *req;
811 int instant_cancel = 0;
815 if (flag == LDLM_CB_CANCELING)
816 /* Don't need to do anything here. */
820 LASSERT(data != NULL);
821 if (lock->l_export->exp_obd->obd_recovering != 0)
822 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
824 ldlm_lock_reorder_req(lock);
826 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
827 &RQF_LDLM_BL_CALLBACK,
828 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
832 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
833 ca = ptlrpc_req_async_args(req);
834 ca->ca_set_arg = arg;
837 req->rq_interpret_reply = ldlm_cb_interpret;
838 req->rq_no_resend = 1;
840 lock_res_and_lock(lock);
841 if (lock->l_granted_mode != lock->l_req_mode) {
842 /* this blocking AST will be communicated as part of the
843 * completion AST instead */
844 unlock_res_and_lock(lock);
846 ptlrpc_req_finished(req);
847 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
851 if (lock->l_destroyed) {
852 /* What's the point? */
853 unlock_res_and_lock(lock);
854 ptlrpc_req_finished(req);
858 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
861 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
862 body->lock_handle[0] = lock->l_remote_handle;
863 body->lock_desc = *desc;
864 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
866 LDLM_DEBUG(lock, "server preparing blocking AST");
868 ptlrpc_request_set_replen(req);
869 if (instant_cancel) {
870 unlock_res_and_lock(lock);
871 ldlm_lock_cancel(lock);
873 LASSERT(lock->l_granted_mode == lock->l_req_mode);
874 ldlm_add_waiting_lock(lock);
875 unlock_res_and_lock(lock);
878 req->rq_send_state = LUSTRE_IMP_FULL;
879 /* ptlrpc_request_alloc_pack already set timeout */
881 req->rq_timeout = ldlm_get_rq_timeout();
883 if (lock->l_export && lock->l_export->exp_nid_stats &&
884 lock->l_export->exp_nid_stats->nid_ldlm_stats)
885 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
886 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
888 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
893 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
895 struct ldlm_cb_set_arg *arg = data;
896 struct ldlm_request *body;
897 struct ptlrpc_request *req;
898 struct ldlm_cb_async_args *ca;
899 long total_enqueue_wait;
900 int instant_cancel = 0;
905 LASSERT(lock != NULL);
906 LASSERT(data != NULL);
908 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
909 lock->l_last_activity);
911 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
912 &RQF_LDLM_CP_CALLBACK);
916 /* server namespace, doesn't need lock */
917 lvb_len = ldlm_lvbo_size(lock);
919 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT,
922 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
924 ptlrpc_request_free(req);
928 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
929 ca = ptlrpc_req_async_args(req);
930 ca->ca_set_arg = arg;
933 req->rq_interpret_reply = ldlm_cb_interpret;
934 req->rq_no_resend = 1;
935 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
937 body->lock_handle[0] = lock->l_remote_handle;
938 body->lock_flags = flags;
939 ldlm_lock2desc(lock, &body->lock_desc);
941 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
943 lvb_len = ldlm_lvbo_fill(lock, lvb, lvb_len);
944 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB,
945 lvb_len, RCL_CLIENT);
948 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
951 /* Server-side enqueue wait time estimate, used in
952 __ldlm_add_waiting_lock to set future enqueue timers */
953 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
954 at_measured(ldlm_lock_to_ns_at(lock),
957 /* bz18618. Don't add lock enqueue time we spend waiting for a
958 previous callback to fail. Locks waiting legitimately will
959 get extended by ldlm_refresh_waiting_lock regardless of the
960 estimate, so it's okay to underestimate here. */
961 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
962 "It is likely that a previous callback timed out.",
964 at_get(ldlm_lock_to_ns_at(lock)));
966 ptlrpc_request_set_replen(req);
968 req->rq_send_state = LUSTRE_IMP_FULL;
969 /* ptlrpc_request_pack already set timeout */
971 req->rq_timeout = ldlm_get_rq_timeout();
973 /* We only send real blocking ASTs after the lock is granted */
974 lock_res_and_lock(lock);
975 if (lock->l_flags & LDLM_FL_AST_SENT) {
976 body->lock_flags |= LDLM_FL_AST_SENT;
977 /* copy ast flags like LDLM_FL_DISCARD_DATA */
978 body->lock_flags |= (lock->l_flags & LDLM_AST_FLAGS);
980 /* We might get here prior to ldlm_handle_enqueue setting
981 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
982 * into waiting list, but this is safe and similar code in
983 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
984 * that would not only cancel the lock, but will also remove
985 * it from waiting list */
986 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
987 unlock_res_and_lock(lock);
988 ldlm_lock_cancel(lock);
990 lock_res_and_lock(lock);
992 /* start the lock-timeout clock */
993 ldlm_add_waiting_lock(lock);
996 unlock_res_and_lock(lock);
998 if (lock->l_export && lock->l_export->exp_nid_stats &&
999 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1000 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1001 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
1003 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
1008 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
1010 struct ldlm_cb_set_arg *arg = data;
1011 struct ldlm_request *body;
1012 struct ptlrpc_request *req;
1013 struct ldlm_cb_async_args *ca;
1015 struct req_format *req_fmt;
1018 LASSERT(lock != NULL);
1020 if (arg->gl_desc != NULL)
1021 /* There is a glimpse descriptor to pack */
1022 req_fmt = &RQF_LDLM_GL_DESC_CALLBACK;
1024 req_fmt = &RQF_LDLM_GL_CALLBACK;
1026 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
1027 req_fmt, LUSTRE_DLM_VERSION,
1033 if (arg->gl_desc != NULL) {
1034 /* copy the GL descriptor */
1035 union ldlm_gl_desc *desc;
1036 desc = req_capsule_client_get(&req->rq_pill, &RMF_DLM_GL_DESC);
1037 *desc = *arg->gl_desc;
1040 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1041 body->lock_handle[0] = lock->l_remote_handle;
1042 ldlm_lock2desc(lock, &body->lock_desc);
1044 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
1045 ca = ptlrpc_req_async_args(req);
1046 ca->ca_set_arg = arg;
1049 /* server namespace, doesn't need lock */
1050 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
1051 ldlm_lvbo_size(lock));
1052 ptlrpc_request_set_replen(req);
1054 req->rq_send_state = LUSTRE_IMP_FULL;
1055 /* ptlrpc_request_alloc_pack already set timeout */
1057 req->rq_timeout = ldlm_get_rq_timeout();
1059 req->rq_interpret_reply = ldlm_cb_interpret;
1061 if (lock->l_export && lock->l_export->exp_nid_stats &&
1062 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1063 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1064 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
1066 rc = ldlm_ast_fini(req, arg, lock, 0);
1071 int ldlm_glimpse_locks(struct ldlm_resource *res, cfs_list_t *gl_work_list)
1076 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), gl_work_list,
1078 if (rc == -ERESTART)
1079 ldlm_reprocess_all(res);
1084 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1085 struct lprocfs_stats *srv_stats)
1087 int lock_type = 0, op = 0;
1089 lock_type = dlm_req->lock_desc.l_resource.lr_type;
1091 switch (lock_type) {
1093 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1096 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
1097 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
1099 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1102 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1105 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1113 lprocfs_counter_incr(srv_stats, op);
1119 * Main server-side entry point into LDLM. This is called by ptlrpc service
1120 * threads to carry out client lock enqueueing requests.
1122 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1123 struct ptlrpc_request *req,
1124 const struct ldlm_request *dlm_req,
1125 const struct ldlm_callback_suite *cbs)
1127 struct ldlm_reply *dlm_rep;
1129 ldlm_error_t err = ELDLM_OK;
1130 struct ldlm_lock *lock = NULL;
1131 void *cookie = NULL;
1135 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1137 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1138 flags = dlm_req->lock_flags;
1140 LASSERT(req->rq_export);
1142 if (ptlrpc_req2svc(req)->srv_stats != NULL)
1143 ldlm_svc_get_eopc(dlm_req, ptlrpc_req2svc(req)->srv_stats);
1145 if (req->rq_export && req->rq_export->exp_nid_stats &&
1146 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1147 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1148 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1150 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1151 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1152 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1153 dlm_req->lock_desc.l_resource.lr_type);
1154 GOTO(out, rc = -EFAULT);
1157 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1158 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1159 dlm_req->lock_desc.l_req_mode &
1160 (dlm_req->lock_desc.l_req_mode-1))) {
1161 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1162 dlm_req->lock_desc.l_req_mode);
1163 GOTO(out, rc = -EFAULT);
1166 if (req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) {
1167 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1169 DEBUG_REQ(D_ERROR, req,
1170 "PLAIN lock request from IBITS client?");
1171 GOTO(out, rc = -EPROTO);
1173 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1175 DEBUG_REQ(D_ERROR, req,
1176 "IBITS lock request from unaware client?");
1177 GOTO(out, rc = -EPROTO);
1181 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1182 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1183 ibits for mgc/mgs) */
1185 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1186 * inodebits lock if client does not support them. */
1187 if (!(req->rq_export->exp_connect_flags & OBD_CONNECT_IBITS) &&
1188 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1189 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1190 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1191 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1192 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1193 dlm_req->lock_desc.l_req_mode = LCK_CR;
1197 if (unlikely(flags & LDLM_FL_REPLAY)) {
1198 /* Find an existing lock in the per-export lock hash */
1199 /* In the function below, .hs_keycmp resolves to
1200 * ldlm_export_lock_keycmp() */
1201 /* coverity[overrun-buffer-val] */
1202 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1203 (void *)&dlm_req->lock_handle[0]);
1205 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1206 LPX64, lock->l_handle.h_cookie);
1207 GOTO(existing_lock, rc = 0);
1211 /* The lock's callback data might be set in the policy function */
1212 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1213 dlm_req->lock_desc.l_resource.lr_type,
1214 dlm_req->lock_desc.l_req_mode,
1218 GOTO(out, rc = -ENOMEM);
1220 lock->l_last_activity = cfs_time_current_sec();
1221 lock->l_remote_handle = dlm_req->lock_handle[0];
1222 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1224 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1225 /* Don't enqueue a lock onto the export if it is been disonnected
1226 * due to eviction (bug 3822) or server umount (bug 24324).
1227 * Cancel it now instead. */
1228 if (req->rq_export->exp_disconnected) {
1229 LDLM_ERROR(lock, "lock on disconnected export %p",
1231 GOTO(out, rc = -ENOTCONN);
1234 lock->l_export = class_export_lock_get(req->rq_export, lock);
1235 if (lock->l_export->exp_lock_hash)
1236 cfs_hash_add(lock->l_export->exp_lock_hash,
1237 &lock->l_remote_handle,
1242 if (flags & LDLM_FL_HAS_INTENT) {
1243 /* In this case, the reply buffer is allocated deep in
1244 * local_lock_enqueue by the policy function. */
1247 /* based on the assumption that lvb size never changes during
1248 * resource life time otherwise it need resource->lr_lock's
1250 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1251 RCL_SERVER, ldlm_lvbo_size(lock));
1253 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1254 GOTO(out, rc = -ENOMEM);
1256 rc = req_capsule_server_pack(&req->rq_pill);
1261 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1262 ldlm_convert_policy_to_local(req->rq_export,
1263 dlm_req->lock_desc.l_resource.lr_type,
1264 &dlm_req->lock_desc.l_policy_data,
1265 &lock->l_policy_data);
1266 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1267 lock->l_req_extent = lock->l_policy_data.l_extent;
1269 err = ldlm_lock_enqueue(ns, &lock, cookie, (int *)&flags);
1273 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1274 dlm_rep->lock_flags = flags;
1276 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1277 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1279 /* We never send a blocking AST until the lock is granted, but
1280 * we can tell it right now */
1281 lock_res_and_lock(lock);
1283 /* Now take into account flags to be inherited from original lock
1284 request both in reply to client and in our own lock flags. */
1285 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1286 lock->l_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1288 /* Don't move a pending lock onto the export if it has already been
1289 * disconnected due to eviction (bug 5683) or server umount (bug 24324).
1290 * Cancel it now instead. */
1291 if (unlikely(req->rq_export->exp_disconnected ||
1292 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1293 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1295 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1296 dlm_rep->lock_flags |= LDLM_FL_AST_SENT;
1297 if (lock->l_granted_mode == lock->l_req_mode) {
1299 * Only cancel lock if it was granted, because it would
1300 * be destroyed immediately and would never be granted
1301 * in the future, causing timeouts on client. Not
1302 * granted lock will be cancelled immediately after
1303 * sending completion AST.
1305 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1306 unlock_res_and_lock(lock);
1307 ldlm_lock_cancel(lock);
1308 lock_res_and_lock(lock);
1310 ldlm_add_waiting_lock(lock);
1313 /* Make sure we never ever grant usual metadata locks to liblustre
1315 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1316 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1317 req->rq_export->exp_libclient) {
1318 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1319 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1320 CERROR("Granting sync lock to libclient. "
1321 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1322 dlm_req->lock_flags, dlm_rep->lock_flags,
1324 LDLM_ERROR(lock, "sync lock");
1325 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1326 struct ldlm_intent *it;
1328 it = req_capsule_client_get(&req->rq_pill,
1331 CERROR("This is intent %s ("LPU64")\n",
1332 ldlm_it2str(it->opc), it->opc);
1338 unlock_res_and_lock(lock);
1342 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1343 if (!req->rq_packed_final) {
1344 err = lustre_pack_reply(req, 1, NULL, NULL);
1349 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1350 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1352 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1353 "(err=%d, rc=%d)", err, rc);
1356 int lvb_len = ldlm_lvbo_size(lock);
1362 buf = req_capsule_server_get(&req->rq_pill,
1364 LASSERTF(buf != NULL, "req %p, lock %p\n",
1366 buflen = req_capsule_get_size(&req->rq_pill,
1367 &RMF_DLM_LVB, RCL_SERVER);
1368 buflen = ldlm_lvbo_fill(lock, buf, buflen);
1369 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB,
1370 buflen, RCL_SERVER);
1373 lock_res_and_lock(lock);
1374 ldlm_resource_unlink_lock(lock);
1375 ldlm_lock_destroy_nolock(lock);
1376 unlock_res_and_lock(lock);
1379 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1380 ldlm_reprocess_all(lock->l_resource);
1382 LDLM_LOCK_RELEASE(lock);
1385 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1391 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1392 ldlm_completion_callback completion_callback,
1393 ldlm_blocking_callback blocking_callback,
1394 ldlm_glimpse_callback glimpse_callback)
1396 struct ldlm_request *dlm_req;
1397 struct ldlm_callback_suite cbs = {
1398 .lcs_completion = completion_callback,
1399 .lcs_blocking = blocking_callback,
1400 .lcs_glimpse = glimpse_callback
1404 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1405 if (dlm_req != NULL) {
1406 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1407 req, dlm_req, &cbs);
1414 int ldlm_handle_convert0(struct ptlrpc_request *req,
1415 const struct ldlm_request *dlm_req)
1417 struct ldlm_reply *dlm_rep;
1418 struct ldlm_lock *lock;
1422 if (req->rq_export && req->rq_export->exp_nid_stats &&
1423 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1424 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1425 LDLM_CONVERT - LDLM_FIRST_OPC);
1427 rc = req_capsule_server_pack(&req->rq_pill);
1431 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1432 dlm_rep->lock_flags = dlm_req->lock_flags;
1434 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1436 req->rq_status = EINVAL;
1440 LDLM_DEBUG(lock, "server-side convert handler START");
1442 lock->l_last_activity = cfs_time_current_sec();
1443 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1444 &dlm_rep->lock_flags);
1446 if (ldlm_del_waiting_lock(lock))
1447 LDLM_DEBUG(lock, "converted waiting lock");
1450 req->rq_status = EDEADLOCK;
1455 if (!req->rq_status)
1456 ldlm_reprocess_all(lock->l_resource);
1457 LDLM_DEBUG(lock, "server-side convert handler END");
1458 LDLM_LOCK_PUT(lock);
1460 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1465 int ldlm_handle_convert(struct ptlrpc_request *req)
1468 struct ldlm_request *dlm_req;
1470 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1471 if (dlm_req != NULL) {
1472 rc = ldlm_handle_convert0(req, dlm_req);
1474 CERROR ("Can't unpack dlm_req\n");
1480 /* Cancel all the locks whos handles are packed into ldlm_request */
1481 int ldlm_request_cancel(struct ptlrpc_request *req,
1482 const struct ldlm_request *dlm_req, int first)
1484 struct ldlm_resource *res, *pres = NULL;
1485 struct ldlm_lock *lock;
1486 int i, count, done = 0;
1489 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1493 /* There is no lock on the server at the replay time,
1494 * skip lock cancelling to make replay tests to pass. */
1495 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1498 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1499 "starting at %d", count, first);
1501 for (i = first; i < count; i++) {
1502 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1504 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1505 "lock (cookie "LPU64")",
1506 dlm_req->lock_handle[i].cookie);
1510 res = lock->l_resource;
1515 ldlm_reprocess_all(pres);
1516 LDLM_RESOURCE_DELREF(pres);
1517 ldlm_resource_putref(pres);
1520 ldlm_resource_getref(res);
1521 LDLM_RESOURCE_ADDREF(res);
1522 ldlm_res_lvbo_update(res, NULL, 1);
1526 ldlm_lock_cancel(lock);
1527 LDLM_LOCK_PUT(lock);
1530 ldlm_reprocess_all(pres);
1531 LDLM_RESOURCE_DELREF(pres);
1532 ldlm_resource_putref(pres);
1534 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1538 int ldlm_handle_cancel(struct ptlrpc_request *req)
1540 struct ldlm_request *dlm_req;
1544 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1545 if (dlm_req == NULL) {
1546 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1550 if (req->rq_export && req->rq_export->exp_nid_stats &&
1551 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1552 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1553 LDLM_CANCEL - LDLM_FIRST_OPC);
1555 rc = req_capsule_server_pack(&req->rq_pill);
1559 if (!ldlm_request_cancel(req, dlm_req, 0))
1560 req->rq_status = ESTALE;
1562 RETURN(ptlrpc_reply(req));
1564 #endif /* HAVE_SERVER_SUPPORT */
1566 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1567 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1572 LDLM_DEBUG(lock, "client blocking AST callback handler");
1574 lock_res_and_lock(lock);
1575 lock->l_flags |= LDLM_FL_CBPENDING;
1577 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1578 lock->l_flags |= LDLM_FL_CANCEL;
1580 do_ast = (!lock->l_readers && !lock->l_writers);
1581 unlock_res_and_lock(lock);
1584 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1585 lock, lock->l_blocking_ast);
1586 if (lock->l_blocking_ast != NULL)
1587 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1590 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1594 LDLM_DEBUG(lock, "client blocking callback handler END");
1595 LDLM_LOCK_RELEASE(lock);
1599 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1600 struct ldlm_namespace *ns,
1601 struct ldlm_request *dlm_req,
1602 struct ldlm_lock *lock)
1605 CFS_LIST_HEAD(ast_list);
1608 LDLM_DEBUG(lock, "client completion callback handler START");
1610 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1611 int to = cfs_time_seconds(1);
1613 cfs_schedule_timeout_and_set_state(
1614 CFS_TASK_INTERRUPTIBLE, to);
1615 if (lock->l_granted_mode == lock->l_req_mode ||
1621 lvb_len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT);
1623 if (lock->l_lvb_len > 0) {
1624 /* for extent lock, lvb contains ost_lvb{}. */
1625 LASSERT(lock->l_lvb_data != NULL);
1626 LASSERTF(lock->l_lvb_len == lvb_len,
1627 "preallocated %d, actual %d.\n",
1628 lock->l_lvb_len, lvb_len);
1629 } else { /* for layout lock, lvb has variable length */
1632 OBD_ALLOC(lvb_data, lvb_len);
1633 if (lvb_data == NULL)
1634 LDLM_ERROR(lock, "no memory.\n");
1636 lock_res_and_lock(lock);
1637 if (lvb_data == NULL) {
1638 lock->l_flags |= LDLM_FL_FAILED;
1640 LASSERT(lock->l_lvb_data == NULL);
1641 lock->l_lvb_data = lvb_data;
1642 lock->l_lvb_len = lvb_len;
1644 unlock_res_and_lock(lock);
1648 lock_res_and_lock(lock);
1649 if (lock->l_destroyed ||
1650 lock->l_granted_mode == lock->l_req_mode) {
1651 /* bug 11300: the lock has already been granted */
1652 unlock_res_and_lock(lock);
1653 LDLM_DEBUG(lock, "Double grant race happened");
1654 LDLM_LOCK_RELEASE(lock);
1659 /* If we receive the completion AST before the actual enqueue returned,
1660 * then we might need to switch lock modes, resources, or extents. */
1661 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1662 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1663 LDLM_DEBUG(lock, "completion AST, new lock mode");
1666 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1667 ldlm_convert_policy_to_local(req->rq_export,
1668 dlm_req->lock_desc.l_resource.lr_type,
1669 &dlm_req->lock_desc.l_policy_data,
1670 &lock->l_policy_data);
1671 LDLM_DEBUG(lock, "completion AST, new policy data");
1674 ldlm_resource_unlink_lock(lock);
1675 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1676 &lock->l_resource->lr_name,
1677 sizeof(lock->l_resource->lr_name)) != 0) {
1678 unlock_res_and_lock(lock);
1679 if (ldlm_lock_change_resource(ns, lock,
1680 &dlm_req->lock_desc.l_resource.lr_name) != 0) {
1681 LDLM_ERROR(lock, "Failed to allocate resource");
1682 LDLM_LOCK_RELEASE(lock);
1686 LDLM_DEBUG(lock, "completion AST, new resource");
1687 CERROR("change resource!\n");
1688 lock_res_and_lock(lock);
1691 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1692 /* BL_AST locks are not needed in lru.
1693 * let ldlm_cancel_lru() be fast. */
1694 ldlm_lock_remove_from_lru(lock);
1695 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1696 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1699 if (lock->l_lvb_len) {
1700 if (req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB,
1701 RCL_CLIENT) < lock->l_lvb_len) {
1702 LDLM_ERROR(lock, "completion AST did not contain "
1705 void *lvb = req_capsule_client_get(&req->rq_pill,
1707 memcpy(lock->l_lvb_data, lvb, lock->l_lvb_len);
1711 ldlm_grant_lock(lock, &ast_list);
1712 unlock_res_and_lock(lock);
1714 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1716 /* Let Enqueue to call osc_lock_upcall() and initialize
1718 OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1720 ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
1722 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1724 LDLM_LOCK_RELEASE(lock);
1728 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1729 struct ldlm_namespace *ns,
1730 struct ldlm_request *dlm_req,
1731 struct ldlm_lock *lock)
1736 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1738 if (lock->l_glimpse_ast != NULL)
1739 rc = lock->l_glimpse_ast(lock, req);
1741 if (req->rq_repmsg != NULL) {
1744 req->rq_status = rc;
1748 lock_res_and_lock(lock);
1749 if (lock->l_granted_mode == LCK_PW &&
1750 !lock->l_readers && !lock->l_writers &&
1751 cfs_time_after(cfs_time_current(),
1752 cfs_time_add(lock->l_last_used,
1753 cfs_time_seconds(10)))) {
1754 unlock_res_and_lock(lock);
1755 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1756 ldlm_handle_bl_callback(ns, NULL, lock);
1761 unlock_res_and_lock(lock);
1762 LDLM_LOCK_RELEASE(lock);
1766 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1768 if (req->rq_no_reply)
1771 req->rq_status = rc;
1772 if (!req->rq_packed_final) {
1773 rc = lustre_pack_reply(req, 1, NULL, NULL);
1777 return ptlrpc_reply(req);
1781 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi, int mode)
1783 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1786 cfs_spin_lock(&blp->blp_lock);
1787 if (blwi->blwi_lock && blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1788 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1789 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1791 /* other blocking callbacks are added to the regular list */
1792 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1794 cfs_spin_unlock(&blp->blp_lock);
1796 cfs_waitq_signal(&blp->blp_waitq);
1798 /* can not use blwi->blwi_mode as blwi could be already freed in
1800 if (mode == LDLM_SYNC)
1801 cfs_wait_for_completion(&blwi->blwi_comp);
1806 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1807 struct ldlm_namespace *ns,
1808 struct ldlm_lock_desc *ld,
1809 cfs_list_t *cancels, int count,
1810 struct ldlm_lock *lock,
1813 cfs_init_completion(&blwi->blwi_comp);
1814 CFS_INIT_LIST_HEAD(&blwi->blwi_head);
1816 if (cfs_memory_pressure_get())
1817 blwi->blwi_mem_pressure = 1;
1820 blwi->blwi_mode = mode;
1822 blwi->blwi_ld = *ld;
1824 cfs_list_add(&blwi->blwi_head, cancels);
1825 cfs_list_del_init(cancels);
1826 blwi->blwi_count = count;
1828 blwi->blwi_lock = lock;
1832 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1833 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1834 cfs_list_t *cancels, int count, int mode)
1838 if (cancels && count == 0)
1841 if (mode == LDLM_SYNC) {
1842 /* if it is synchronous call do minimum mem alloc, as it could
1843 * be triggered from kernel shrinker
1845 struct ldlm_bl_work_item blwi;
1846 memset(&blwi, 0, sizeof(blwi));
1847 init_blwi(&blwi, ns, ld, cancels, count, lock, LDLM_SYNC);
1848 RETURN(__ldlm_bl_to_thread(&blwi, LDLM_SYNC));
1850 struct ldlm_bl_work_item *blwi;
1851 OBD_ALLOC(blwi, sizeof(*blwi));
1854 init_blwi(blwi, ns, ld, cancels, count, lock, LDLM_ASYNC);
1856 RETURN(__ldlm_bl_to_thread(blwi, LDLM_ASYNC));
1862 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1863 struct ldlm_lock *lock)
1866 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
1872 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1873 cfs_list_t *cancels, int count, int mode)
1876 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
1882 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
1883 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
1885 struct obd_device *obd = req->rq_export->exp_obd;
1892 DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
1894 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
1896 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
1898 DEBUG_REQ(D_IOCTL, req, "no set_info key");
1901 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
1903 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
1905 DEBUG_REQ(D_IOCTL, req, "no set_info val");
1908 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
1911 /* We are responsible for swabbing contents of val */
1913 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
1914 /* Pass it on to mdc (the "export" in this case) */
1915 rc = obd_set_info_async(req->rq_svc_thread->t_env,
1917 sizeof(KEY_HSM_COPYTOOL_SEND),
1918 KEY_HSM_COPYTOOL_SEND,
1921 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
1926 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
1927 const char *msg, int rc,
1928 struct lustre_handle *handle)
1930 DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
1931 "%s: [nid %s] [rc %d] [lock "LPX64"]",
1932 msg, libcfs_id2str(req->rq_peer), rc,
1933 handle ? handle->cookie : 0);
1934 if (req->rq_no_reply)
1935 CWARN("No reply was sent, maybe cause bug 21636.\n");
1937 CWARN("Send reply failed, maybe cause bug 21636.\n");
1940 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
1941 static int ldlm_callback_handler(struct ptlrpc_request *req)
1943 struct ldlm_namespace *ns;
1944 struct ldlm_request *dlm_req;
1945 struct ldlm_lock *lock;
1949 /* Requests arrive in sender's byte order. The ptlrpc service
1950 * handler has already checked and, if necessary, byte-swapped the
1951 * incoming request message body, but I am responsible for the
1952 * message buffers. */
1954 /* do nothing for sec context finalize */
1955 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
1958 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
1960 if (req->rq_export == NULL) {
1961 rc = ldlm_callback_reply(req, -ENOTCONN);
1962 ldlm_callback_errmsg(req, "Operate on unconnected server",
1967 LASSERT(req->rq_export != NULL);
1968 LASSERT(req->rq_export->exp_obd != NULL);
1970 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
1971 case LDLM_BL_CALLBACK:
1972 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK))
1975 case LDLM_CP_CALLBACK:
1976 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK))
1979 case LDLM_GL_CALLBACK:
1980 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK))
1984 rc = ldlm_handle_setinfo(req);
1985 ldlm_callback_reply(req, rc);
1987 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
1988 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
1989 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
1990 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
1992 rc = llog_origin_handle_cancel(req);
1993 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
1995 ldlm_callback_reply(req, rc);
1997 case LLOG_ORIGIN_HANDLE_CREATE:
1998 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
1999 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2001 rc = llog_origin_handle_open(req);
2002 ldlm_callback_reply(req, rc);
2004 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2005 req_capsule_set(&req->rq_pill,
2006 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2007 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2009 rc = llog_origin_handle_next_block(req);
2010 ldlm_callback_reply(req, rc);
2012 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2013 req_capsule_set(&req->rq_pill,
2014 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2015 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2017 rc = llog_origin_handle_read_header(req);
2018 ldlm_callback_reply(req, rc);
2020 case LLOG_ORIGIN_HANDLE_CLOSE:
2021 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2023 rc = llog_origin_handle_close(req);
2024 ldlm_callback_reply(req, rc);
2027 CERROR("unknown opcode %u\n",
2028 lustre_msg_get_opc(req->rq_reqmsg));
2029 ldlm_callback_reply(req, -EPROTO);
2033 ns = req->rq_export->exp_obd->obd_namespace;
2034 LASSERT(ns != NULL);
2036 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2038 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2039 if (dlm_req == NULL) {
2040 rc = ldlm_callback_reply(req, -EPROTO);
2041 ldlm_callback_errmsg(req, "Operate without parameter", rc,
2046 /* Force a known safe race, send a cancel to the server for a lock
2047 * which the server has already started a blocking callback on. */
2048 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
2049 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2050 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
2052 CERROR("ldlm_cli_cancel: %d\n", rc);
2055 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
2057 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
2058 "disappeared\n", dlm_req->lock_handle[0].cookie);
2059 rc = ldlm_callback_reply(req, -EINVAL);
2060 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
2061 &dlm_req->lock_handle[0]);
2065 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
2066 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
2067 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
2069 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
2070 lock_res_and_lock(lock);
2071 lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
2072 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2073 /* If somebody cancels lock and cache is already dropped,
2074 * or lock is failed before cp_ast received on client,
2075 * we can tell the server we have no lock. Otherwise, we
2076 * should send cancel after dropping the cache. */
2077 if (((lock->l_flags & LDLM_FL_CANCELING) &&
2078 (lock->l_flags & LDLM_FL_BL_DONE)) ||
2079 (lock->l_flags & LDLM_FL_FAILED)) {
2080 LDLM_DEBUG(lock, "callback on lock "
2081 LPX64" - lock disappeared\n",
2082 dlm_req->lock_handle[0].cookie);
2083 unlock_res_and_lock(lock);
2084 LDLM_LOCK_RELEASE(lock);
2085 rc = ldlm_callback_reply(req, -EINVAL);
2086 ldlm_callback_errmsg(req, "Operate on stale lock", rc,
2087 &dlm_req->lock_handle[0]);
2090 /* BL_AST locks are not needed in lru.
2091 * let ldlm_cancel_lru() be fast. */
2092 ldlm_lock_remove_from_lru(lock);
2093 lock->l_flags |= LDLM_FL_BL_AST;
2095 unlock_res_and_lock(lock);
2097 /* We want the ost thread to get this reply so that it can respond
2098 * to ost requests (write cache writeback) that might be triggered
2101 * But we'd also like to be able to indicate in the reply that we're
2102 * cancelling right now, because it's unused, or have an intent result
2103 * in the reply, so we might have to push the responsibility for sending
2104 * the reply down into the AST handlers, alas. */
2106 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2107 case LDLM_BL_CALLBACK:
2108 CDEBUG(D_INODE, "blocking ast\n");
2109 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
2110 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
2111 rc = ldlm_callback_reply(req, 0);
2112 if (req->rq_no_reply || rc)
2113 ldlm_callback_errmsg(req, "Normal process", rc,
2114 &dlm_req->lock_handle[0]);
2116 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
2117 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
2119 case LDLM_CP_CALLBACK:
2120 CDEBUG(D_INODE, "completion ast\n");
2121 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
2122 ldlm_callback_reply(req, 0);
2123 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
2125 case LDLM_GL_CALLBACK:
2126 CDEBUG(D_INODE, "glimpse ast\n");
2127 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
2128 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
2131 LBUG(); /* checked above */
2137 #ifdef HAVE_SERVER_SUPPORT
2138 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2143 /* Requests arrive in sender's byte order. The ptlrpc service
2144 * handler has already checked and, if necessary, byte-swapped the
2145 * incoming request message body, but I am responsible for the
2146 * message buffers. */
2148 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2150 if (req->rq_export == NULL) {
2151 struct ldlm_request *dlm_req;
2153 CERROR("%s from %s arrived at %lu with bad export cookie "
2155 ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2156 libcfs_nid2str(req->rq_peer.nid),
2157 req->rq_arrival_time.tv_sec,
2158 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2160 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2161 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2162 dlm_req = req_capsule_client_get(&req->rq_pill,
2164 if (dlm_req != NULL)
2165 ldlm_lock_dump_handle(D_ERROR,
2166 &dlm_req->lock_handle[0]);
2168 ldlm_callback_reply(req, -ENOTCONN);
2172 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2174 /* XXX FIXME move this back to mds/handler.c, bug 249 */
2176 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2177 CDEBUG(D_INODE, "cancel\n");
2178 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL))
2180 rc = ldlm_handle_cancel(req);
2184 case OBD_LOG_CANCEL:
2185 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2186 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2188 rc = llog_origin_handle_cancel(req);
2189 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2191 ldlm_callback_reply(req, rc);
2194 CERROR("invalid opcode %d\n",
2195 lustre_msg_get_opc(req->rq_reqmsg));
2196 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2197 ldlm_callback_reply(req, -EINVAL);
2203 static int ldlm_cancel_hpreq_lock_match(struct ptlrpc_request *req,
2204 struct ldlm_lock *lock)
2206 struct ldlm_request *dlm_req;
2207 struct lustre_handle lockh;
2212 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2213 if (dlm_req == NULL)
2216 ldlm_lock2handle(lock, &lockh);
2217 for (i = 0; i < dlm_req->lock_count; i++) {
2218 if (lustre_handle_equal(&dlm_req->lock_handle[i],
2220 DEBUG_REQ(D_RPCTRACE, req,
2221 "Prio raised by lock "LPX64".", lockh.cookie);
2232 static int ldlm_cancel_hpreq_check(struct ptlrpc_request *req)
2234 struct ldlm_request *dlm_req;
2239 /* no prolong in recovery */
2240 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2243 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2244 if (dlm_req == NULL)
2247 for (i = 0; i < dlm_req->lock_count; i++) {
2248 struct ldlm_lock *lock;
2250 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
2254 rc = !!(lock->l_flags & LDLM_FL_AST_SENT);
2256 LDLM_DEBUG(lock, "hpreq cancel lock");
2257 LDLM_LOCK_PUT(lock);
2266 static struct ptlrpc_hpreq_ops ldlm_cancel_hpreq_ops = {
2267 .hpreq_lock_match = ldlm_cancel_hpreq_lock_match,
2268 .hpreq_check = ldlm_cancel_hpreq_check,
2272 static int ldlm_hpreq_handler(struct ptlrpc_request *req)
2276 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2278 if (req->rq_export == NULL)
2281 if (LDLM_CANCEL == lustre_msg_get_opc(req->rq_reqmsg)) {
2282 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2283 req->rq_ops = &ldlm_cancel_hpreq_ops;
2288 int ldlm_revoke_lock_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
2289 cfs_hlist_node_t *hnode, void *data)
2292 cfs_list_t *rpc_list = data;
2293 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
2295 lock_res_and_lock(lock);
2297 if (lock->l_req_mode != lock->l_granted_mode) {
2298 unlock_res_and_lock(lock);
2302 LASSERT(lock->l_resource);
2303 if (lock->l_resource->lr_type != LDLM_IBITS &&
2304 lock->l_resource->lr_type != LDLM_PLAIN) {
2305 unlock_res_and_lock(lock);
2309 if (lock->l_flags & LDLM_FL_AST_SENT) {
2310 unlock_res_and_lock(lock);
2314 LASSERT(lock->l_blocking_ast);
2315 LASSERT(!lock->l_blocking_lock);
2317 lock->l_flags |= LDLM_FL_AST_SENT;
2318 if (lock->l_export && lock->l_export->exp_lock_hash) {
2319 /* NB: it's safe to call cfs_hash_del() even lock isn't
2320 * in exp_lock_hash. */
2321 /* In the function below, .hs_keycmp resolves to
2322 * ldlm_export_lock_keycmp() */
2323 /* coverity[overrun-buffer-val] */
2324 cfs_hash_del(lock->l_export->exp_lock_hash,
2325 &lock->l_remote_handle, &lock->l_exp_hash);
2328 cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2329 LDLM_LOCK_GET(lock);
2331 unlock_res_and_lock(lock);
2335 void ldlm_revoke_export_locks(struct obd_export *exp)
2337 cfs_list_t rpc_list;
2340 CFS_INIT_LIST_HEAD(&rpc_list);
2341 cfs_hash_for_each_empty(exp->exp_lock_hash,
2342 ldlm_revoke_lock_cb, &rpc_list);
2343 ldlm_run_ast_work(exp->exp_obd->obd_namespace, &rpc_list,
2344 LDLM_WORK_REVOKE_AST);
2348 #endif /* HAVE_SERVER_SUPPORT */
2351 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2353 struct ldlm_bl_work_item *blwi = NULL;
2354 static unsigned int num_bl = 0;
2356 cfs_spin_lock(&blp->blp_lock);
2357 /* process a request from the blp_list at least every blp_num_threads */
2358 if (!cfs_list_empty(&blp->blp_list) &&
2359 (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2360 blwi = cfs_list_entry(blp->blp_list.next,
2361 struct ldlm_bl_work_item, blwi_entry);
2363 if (!cfs_list_empty(&blp->blp_prio_list))
2364 blwi = cfs_list_entry(blp->blp_prio_list.next,
2365 struct ldlm_bl_work_item,
2369 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2371 cfs_list_del(&blwi->blwi_entry);
2373 cfs_spin_unlock(&blp->blp_lock);
2378 /* This only contains temporary data until the thread starts */
2379 struct ldlm_bl_thread_data {
2380 char bltd_name[CFS_CURPROC_COMM_MAX];
2381 struct ldlm_bl_pool *bltd_blp;
2382 cfs_completion_t bltd_comp;
2386 static int ldlm_bl_thread_main(void *arg);
2388 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2390 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2393 cfs_init_completion(&bltd.bltd_comp);
2394 rc = cfs_create_thread(ldlm_bl_thread_main, &bltd, 0);
2396 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2397 cfs_atomic_read(&blp->blp_num_threads), rc);
2400 cfs_wait_for_completion(&bltd.bltd_comp);
2405 static int ldlm_bl_thread_main(void *arg)
2407 struct ldlm_bl_pool *blp;
2411 struct ldlm_bl_thread_data *bltd = arg;
2413 blp = bltd->bltd_blp;
2416 cfs_atomic_inc_return(&blp->blp_num_threads) - 1;
2417 cfs_atomic_inc(&blp->blp_busy_threads);
2419 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2420 "ldlm_bl_%02d", bltd->bltd_num);
2421 cfs_daemonize(bltd->bltd_name);
2423 cfs_complete(&bltd->bltd_comp);
2424 /* cannot use bltd after this, it is only on caller's stack */
2428 struct l_wait_info lwi = { 0 };
2429 struct ldlm_bl_work_item *blwi = NULL;
2432 blwi = ldlm_bl_get_work(blp);
2435 cfs_atomic_dec(&blp->blp_busy_threads);
2436 l_wait_event_exclusive(blp->blp_waitq,
2437 (blwi = ldlm_bl_get_work(blp)) != NULL,
2439 busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2441 busy = cfs_atomic_read(&blp->blp_busy_threads);
2444 if (blwi->blwi_ns == NULL)
2445 /* added by ldlm_cleanup() */
2448 /* Not fatal if racy and have a few too many threads */
2449 if (unlikely(busy < blp->blp_max_threads &&
2450 busy >= cfs_atomic_read(&blp->blp_num_threads) &&
2451 !blwi->blwi_mem_pressure))
2452 /* discard the return value, we tried */
2453 ldlm_bl_thread_start(blp);
2455 if (blwi->blwi_mem_pressure)
2456 cfs_memory_pressure_set();
2458 if (blwi->blwi_count) {
2460 /* The special case when we cancel locks in lru
2461 * asynchronously, we pass the list of locks here.
2462 * Thus locks are marked LDLM_FL_CANCELING, but NOT
2463 * canceled locally yet. */
2464 count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2467 ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL, 0);
2469 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2472 if (blwi->blwi_mem_pressure)
2473 cfs_memory_pressure_clr();
2475 if (blwi->blwi_mode == LDLM_ASYNC)
2476 OBD_FREE(blwi, sizeof(*blwi));
2478 cfs_complete(&blwi->blwi_comp);
2481 cfs_atomic_dec(&blp->blp_busy_threads);
2482 cfs_atomic_dec(&blp->blp_num_threads);
2483 cfs_complete(&blp->blp_comp);
2489 static int ldlm_setup(void);
2490 static int ldlm_cleanup(void);
2492 int ldlm_get_ref(void)
2496 cfs_mutex_lock(&ldlm_ref_mutex);
2497 if (++ldlm_refcount == 1) {
2502 cfs_mutex_unlock(&ldlm_ref_mutex);
2507 void ldlm_put_ref(void)
2510 cfs_mutex_lock(&ldlm_ref_mutex);
2511 if (ldlm_refcount == 1) {
2512 int rc = ldlm_cleanup();
2514 CERROR("ldlm_cleanup failed: %d\n", rc);
2520 cfs_mutex_unlock(&ldlm_ref_mutex);
2526 * Export handle<->lock hash operations.
2529 ldlm_export_lock_hash(cfs_hash_t *hs, const void *key, unsigned mask)
2531 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2535 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2537 struct ldlm_lock *lock;
2539 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2540 return &lock->l_remote_handle;
2544 ldlm_export_lock_keycpy(cfs_hlist_node_t *hnode, void *key)
2546 struct ldlm_lock *lock;
2548 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2549 lock->l_remote_handle = *(struct lustre_handle *)key;
2553 ldlm_export_lock_keycmp(const void *key, cfs_hlist_node_t *hnode)
2555 return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
2559 ldlm_export_lock_object(cfs_hlist_node_t *hnode)
2561 return cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2565 ldlm_export_lock_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2567 struct ldlm_lock *lock;
2569 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2570 LDLM_LOCK_GET(lock);
2574 ldlm_export_lock_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2576 struct ldlm_lock *lock;
2578 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2579 LDLM_LOCK_RELEASE(lock);
2582 static cfs_hash_ops_t ldlm_export_lock_ops = {
2583 .hs_hash = ldlm_export_lock_hash,
2584 .hs_key = ldlm_export_lock_key,
2585 .hs_keycmp = ldlm_export_lock_keycmp,
2586 .hs_keycpy = ldlm_export_lock_keycpy,
2587 .hs_object = ldlm_export_lock_object,
2588 .hs_get = ldlm_export_lock_get,
2589 .hs_put = ldlm_export_lock_put,
2590 .hs_put_locked = ldlm_export_lock_put,
2593 int ldlm_init_export(struct obd_export *exp)
2597 exp->exp_lock_hash =
2598 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2599 HASH_EXP_LOCK_CUR_BITS,
2600 HASH_EXP_LOCK_MAX_BITS,
2601 HASH_EXP_LOCK_BKT_BITS, 0,
2602 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
2603 &ldlm_export_lock_ops,
2604 CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
2605 CFS_HASH_NBLK_CHANGE);
2607 if (!exp->exp_lock_hash)
2612 EXPORT_SYMBOL(ldlm_init_export);
2614 void ldlm_destroy_export(struct obd_export *exp)
2617 cfs_hash_putref(exp->exp_lock_hash);
2618 exp->exp_lock_hash = NULL;
2620 ldlm_destroy_flock_export(exp);
2623 EXPORT_SYMBOL(ldlm_destroy_export);
2625 static int ldlm_setup(void)
2627 static struct ptlrpc_service_conf conf;
2628 struct ldlm_bl_pool *blp = NULL;
2635 if (ldlm_state != NULL)
2638 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2639 if (ldlm_state == NULL)
2643 rc = ldlm_proc_setup();
2648 memset(&conf, 0, sizeof(conf));
2649 conf = (typeof(conf)) {
2650 .psc_name = "ldlm_cbd",
2651 .psc_watchdog_factor = 2,
2653 .bc_nbufs = LDLM_NBUFS,
2654 .bc_buf_size = LDLM_BUFSIZE,
2655 .bc_req_max_size = LDLM_MAXREQSIZE,
2656 .bc_rep_max_size = LDLM_MAXREPSIZE,
2657 .bc_req_portal = LDLM_CB_REQUEST_PORTAL,
2658 .bc_rep_portal = LDLM_CB_REPLY_PORTAL,
2661 .tc_thr_name = "ldlm_cb",
2662 .tc_thr_factor = LDLM_THR_FACTOR,
2663 .tc_nthrs_init = LDLM_NTHRS_INIT,
2664 .tc_nthrs_base = LDLM_NTHRS_BASE,
2665 .tc_nthrs_max = LDLM_NTHRS_MAX,
2666 .tc_nthrs_user = ldlm_num_threads,
2667 .tc_cpu_affinity = 1,
2668 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD,
2671 .cc_pattern = ldlm_cpts,
2674 .so_req_handler = ldlm_callback_handler,
2677 ldlm_state->ldlm_cb_service = \
2678 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2679 if (IS_ERR(ldlm_state->ldlm_cb_service)) {
2680 CERROR("failed to start service\n");
2681 rc = PTR_ERR(ldlm_state->ldlm_cb_service);
2682 ldlm_state->ldlm_cb_service = NULL;
2686 #ifdef HAVE_SERVER_SUPPORT
2687 memset(&conf, 0, sizeof(conf));
2688 conf = (typeof(conf)) {
2689 .psc_name = "ldlm_canceld",
2690 .psc_watchdog_factor = 6,
2692 .bc_nbufs = LDLM_NBUFS,
2693 .bc_buf_size = LDLM_BUFSIZE,
2694 .bc_req_max_size = LDLM_MAXREQSIZE,
2695 .bc_rep_max_size = LDLM_MAXREPSIZE,
2696 .bc_req_portal = LDLM_CANCEL_REQUEST_PORTAL,
2697 .bc_rep_portal = LDLM_CANCEL_REPLY_PORTAL,
2701 .tc_thr_name = "ldlm_cn",
2702 .tc_thr_factor = LDLM_THR_FACTOR,
2703 .tc_nthrs_init = LDLM_NTHRS_INIT,
2704 .tc_nthrs_base = LDLM_NTHRS_BASE,
2705 .tc_nthrs_max = LDLM_NTHRS_MAX,
2706 .tc_nthrs_user = ldlm_num_threads,
2707 .tc_cpu_affinity = 1,
2708 .tc_ctx_tags = LCT_MD_THREAD | \
2713 .cc_pattern = ldlm_cpts,
2716 .so_req_handler = ldlm_cancel_handler,
2717 .so_hpreq_handler = ldlm_hpreq_handler,
2720 ldlm_state->ldlm_cancel_service = \
2721 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2722 if (IS_ERR(ldlm_state->ldlm_cancel_service)) {
2723 CERROR("failed to start service\n");
2724 rc = PTR_ERR(ldlm_state->ldlm_cancel_service);
2725 ldlm_state->ldlm_cancel_service = NULL;
2730 OBD_ALLOC(blp, sizeof(*blp));
2732 GOTO(out, rc = -ENOMEM);
2733 ldlm_state->ldlm_bl_pool = blp;
2735 cfs_spin_lock_init(&blp->blp_lock);
2736 CFS_INIT_LIST_HEAD(&blp->blp_list);
2737 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2738 cfs_waitq_init(&blp->blp_waitq);
2739 cfs_atomic_set(&blp->blp_num_threads, 0);
2740 cfs_atomic_set(&blp->blp_busy_threads, 0);
2743 if (ldlm_num_threads == 0) {
2744 blp->blp_min_threads = LDLM_NTHRS_INIT;
2745 blp->blp_max_threads = LDLM_NTHRS_MAX;
2747 blp->blp_min_threads = blp->blp_max_threads = \
2748 min_t(int, LDLM_NTHRS_MAX, max_t(int, LDLM_NTHRS_INIT,
2752 for (i = 0; i < blp->blp_min_threads; i++) {
2753 rc = ldlm_bl_thread_start(blp);
2758 # ifdef HAVE_SERVER_SUPPORT
2759 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2760 expired_lock_thread.elt_state = ELT_STOPPED;
2761 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2763 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2764 cfs_spin_lock_init(&waiting_locks_spinlock);
2765 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2767 rc = cfs_create_thread(expired_lock_main, NULL, CFS_DAEMON_FLAGS);
2769 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2773 cfs_wait_event(expired_lock_thread.elt_waitq,
2774 expired_lock_thread.elt_state == ELT_READY);
2775 # endif /* HAVE_SERVER_SUPPORT */
2777 rc = ldlm_pools_init();
2779 CERROR("Failed to initialize LDLM pools: %d\n", rc);
2790 static int ldlm_cleanup(void)
2794 if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2795 !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2796 CERROR("ldlm still has namespaces; clean these up first.\n");
2797 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2798 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2805 if (ldlm_state->ldlm_bl_pool != NULL) {
2806 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2808 while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2809 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2811 cfs_init_completion(&blp->blp_comp);
2813 cfs_spin_lock(&blp->blp_lock);
2814 cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2815 cfs_waitq_signal(&blp->blp_waitq);
2816 cfs_spin_unlock(&blp->blp_lock);
2818 cfs_wait_for_completion(&blp->blp_comp);
2821 OBD_FREE(blp, sizeof(*blp));
2823 #endif /* __KERNEL__ */
2825 if (ldlm_state->ldlm_cb_service != NULL)
2826 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2827 # ifdef HAVE_SERVER_SUPPORT
2828 if (ldlm_state->ldlm_cancel_service != NULL)
2829 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
2833 ldlm_proc_cleanup();
2835 # ifdef HAVE_SERVER_SUPPORT
2836 if (expired_lock_thread.elt_state != ELT_STOPPED) {
2837 expired_lock_thread.elt_state = ELT_TERMINATE;
2838 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
2839 cfs_wait_event(expired_lock_thread.elt_waitq,
2840 expired_lock_thread.elt_state == ELT_STOPPED);
2843 #endif /* __KERNEL__ */
2845 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
2853 cfs_mutex_init(&ldlm_ref_mutex);
2854 cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
2855 cfs_mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
2856 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
2857 sizeof(struct ldlm_resource), 0,
2858 CFS_SLAB_HWCACHE_ALIGN);
2859 if (ldlm_resource_slab == NULL)
2862 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
2863 sizeof(struct ldlm_lock), 0,
2864 CFS_SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU);
2865 if (ldlm_lock_slab == NULL) {
2866 cfs_mem_cache_destroy(ldlm_resource_slab);
2870 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
2871 sizeof(struct ldlm_interval),
2872 0, CFS_SLAB_HWCACHE_ALIGN);
2873 if (ldlm_interval_slab == NULL) {
2874 cfs_mem_cache_destroy(ldlm_resource_slab);
2875 cfs_mem_cache_destroy(ldlm_lock_slab);
2878 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2879 class_export_dump_hook = ldlm_dump_export_locks;
2884 void ldlm_exit(void)
2888 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
2889 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
2890 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
2892 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
2893 * synchronize_rcu() to wait a grace period elapsed, so that
2894 * ldlm_lock_free() get a chance to be called. */
2897 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
2898 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
2899 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
2900 LASSERTF(rc == 0, "couldn't free interval node slab\n");
2904 EXPORT_SYMBOL(ldlm_extent_shift_kms);
2907 #ifdef HAVE_SERVER_SUPPORT
2908 EXPORT_SYMBOL(ldlm_get_processing_policy);
2910 EXPORT_SYMBOL(ldlm_lock2desc);
2911 EXPORT_SYMBOL(ldlm_register_intent);
2912 EXPORT_SYMBOL(ldlm_lockname);
2913 EXPORT_SYMBOL(ldlm_typename);
2914 EXPORT_SYMBOL(ldlm_lock2handle);
2915 EXPORT_SYMBOL(__ldlm_handle2lock);
2916 EXPORT_SYMBOL(ldlm_lock_get);
2917 EXPORT_SYMBOL(ldlm_lock_put);
2918 EXPORT_SYMBOL(ldlm_lock_match);
2919 EXPORT_SYMBOL(ldlm_lock_cancel);
2920 EXPORT_SYMBOL(ldlm_lock_addref);
2921 EXPORT_SYMBOL(ldlm_lock_addref_try);
2922 EXPORT_SYMBOL(ldlm_lock_decref);
2923 EXPORT_SYMBOL(ldlm_lock_decref_and_cancel);
2924 EXPORT_SYMBOL(ldlm_lock_change_resource);
2925 EXPORT_SYMBOL(ldlm_it2str);
2926 EXPORT_SYMBOL(ldlm_lock_dump_handle);
2927 EXPORT_SYMBOL(ldlm_reprocess_all_ns);
2928 EXPORT_SYMBOL(ldlm_lock_allow_match_locked);
2929 EXPORT_SYMBOL(ldlm_lock_allow_match);
2930 EXPORT_SYMBOL(ldlm_lock_downgrade);
2931 EXPORT_SYMBOL(ldlm_lock_convert);
2933 /* ldlm_request.c */
2934 EXPORT_SYMBOL(ldlm_completion_ast_async);
2935 EXPORT_SYMBOL(ldlm_blocking_ast_nocheck);
2936 EXPORT_SYMBOL(ldlm_completion_ast);
2937 EXPORT_SYMBOL(ldlm_blocking_ast);
2938 EXPORT_SYMBOL(ldlm_glimpse_ast);
2939 EXPORT_SYMBOL(ldlm_expired_completion_wait);
2940 EXPORT_SYMBOL(ldlm_prep_enqueue_req);
2941 EXPORT_SYMBOL(ldlm_prep_elc_req);
2942 EXPORT_SYMBOL(ldlm_cli_convert);
2943 EXPORT_SYMBOL(ldlm_cli_enqueue);
2944 EXPORT_SYMBOL(ldlm_cli_enqueue_fini);
2945 EXPORT_SYMBOL(ldlm_cli_enqueue_local);
2946 EXPORT_SYMBOL(ldlm_cli_cancel);
2947 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
2948 EXPORT_SYMBOL(ldlm_cli_cancel_unused_resource);
2949 EXPORT_SYMBOL(ldlm_cli_cancel_req);
2950 EXPORT_SYMBOL(ldlm_replay_locks);
2951 EXPORT_SYMBOL(ldlm_resource_foreach);
2952 EXPORT_SYMBOL(ldlm_namespace_foreach);
2953 EXPORT_SYMBOL(ldlm_resource_iterate);
2954 EXPORT_SYMBOL(ldlm_cancel_resource_local);
2955 EXPORT_SYMBOL(ldlm_cli_cancel_list_local);
2956 EXPORT_SYMBOL(ldlm_cli_cancel_list);
2959 #ifdef HAVE_SERVER_SUPPORT
2960 EXPORT_SYMBOL(ldlm_server_blocking_ast);
2961 EXPORT_SYMBOL(ldlm_server_completion_ast);
2962 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
2963 EXPORT_SYMBOL(ldlm_glimpse_locks);
2964 EXPORT_SYMBOL(ldlm_handle_enqueue);
2965 EXPORT_SYMBOL(ldlm_handle_enqueue0);
2966 EXPORT_SYMBOL(ldlm_handle_cancel);
2967 EXPORT_SYMBOL(ldlm_request_cancel);
2968 EXPORT_SYMBOL(ldlm_handle_convert);
2969 EXPORT_SYMBOL(ldlm_handle_convert0);
2970 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2972 EXPORT_SYMBOL(ldlm_del_waiting_lock);
2973 EXPORT_SYMBOL(ldlm_get_ref);
2974 EXPORT_SYMBOL(ldlm_put_ref);
2975 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
2977 /* ldlm_resource.c */
2978 EXPORT_SYMBOL(ldlm_namespace_new);
2979 EXPORT_SYMBOL(ldlm_namespace_cleanup);
2980 EXPORT_SYMBOL(ldlm_namespace_free);
2981 EXPORT_SYMBOL(ldlm_namespace_dump);
2982 EXPORT_SYMBOL(ldlm_dump_all_namespaces);
2983 EXPORT_SYMBOL(ldlm_resource_get);
2984 EXPORT_SYMBOL(ldlm_resource_putref);
2985 EXPORT_SYMBOL(ldlm_resource_unlink_lock);
2988 EXPORT_SYMBOL(client_import_add_conn);
2989 EXPORT_SYMBOL(client_import_del_conn);
2990 EXPORT_SYMBOL(client_obd_setup);
2991 EXPORT_SYMBOL(client_obd_cleanup);
2992 EXPORT_SYMBOL(client_connect_import);
2993 EXPORT_SYMBOL(client_disconnect_export);
2994 EXPORT_SYMBOL(target_send_reply);
2995 EXPORT_SYMBOL(target_pack_pool_reply);
2997 #ifdef HAVE_SERVER_SUPPORT
2998 EXPORT_SYMBOL(server_disconnect_export);
2999 EXPORT_SYMBOL(target_stop_recovery_thread);
3000 EXPORT_SYMBOL(target_handle_connect);
3001 EXPORT_SYMBOL(target_cleanup_recovery);
3002 EXPORT_SYMBOL(target_destroy_export);
3003 EXPORT_SYMBOL(target_cancel_recovery_timer);
3004 EXPORT_SYMBOL(target_queue_recovery_request);
3005 EXPORT_SYMBOL(target_handle_ping);
3006 EXPORT_SYMBOL(target_handle_disconnect);
3010 EXPORT_SYMBOL(lock_res_and_lock);
3011 EXPORT_SYMBOL(unlock_res_and_lock);