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) 2010, 2012, Intel Corporation.
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 struct mutex 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
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 struct completion 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 struct completion blwi_comp;
128 int blwi_mem_pressure;
131 #if defined(HAVE_SERVER_SUPPORT) && defined(__KERNEL__)
134 * Protects both waiting_locks_list and expired_lock_thread.
136 static spinlock_t waiting_locks_spinlock; /* BH lock (timer) */
139 * List for contended locks.
141 * As soon as a lock is contended, it gets placed on this list and
142 * expected time to get a response is filled in the lock. A special
143 * thread walks the list looking for locks that should be released and
144 * schedules client evictions for those that have not been released in
147 * All access to it should be under waiting_locks_spinlock.
149 static cfs_list_t waiting_locks_list;
150 static cfs_timer_t waiting_locks_timer;
152 static struct expired_lock_thread {
153 cfs_waitq_t elt_waitq;
156 cfs_list_t elt_expired_locks;
157 } expired_lock_thread;
159 static inline int have_expired_locks(void)
164 spin_lock_bh(&waiting_locks_spinlock);
165 need_to_run = !cfs_list_empty(&expired_lock_thread.elt_expired_locks);
166 spin_unlock_bh(&waiting_locks_spinlock);
172 * Check expired lock list for expired locks and time them out.
174 static int expired_lock_main(void *arg)
176 cfs_list_t *expired = &expired_lock_thread.elt_expired_locks;
177 struct l_wait_info lwi = { 0 };
181 cfs_daemonize("ldlm_elt");
183 expired_lock_thread.elt_state = ELT_READY;
184 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
187 l_wait_event(expired_lock_thread.elt_waitq,
188 have_expired_locks() ||
189 expired_lock_thread.elt_state == ELT_TERMINATE,
192 spin_lock_bh(&waiting_locks_spinlock);
193 if (expired_lock_thread.elt_dump) {
194 struct libcfs_debug_msg_data msgdata = {
195 .msg_file = __FILE__,
196 .msg_fn = "waiting_locks_callback",
197 .msg_line = expired_lock_thread.elt_dump };
198 spin_unlock_bh(&waiting_locks_spinlock);
200 /* from waiting_locks_callback, but not in timer */
201 libcfs_debug_dumplog();
202 libcfs_run_lbug_upcall(&msgdata);
204 spin_lock_bh(&waiting_locks_spinlock);
205 expired_lock_thread.elt_dump = 0;
210 while (!cfs_list_empty(expired)) {
211 struct obd_export *export;
212 struct ldlm_lock *lock;
214 lock = cfs_list_entry(expired->next, struct ldlm_lock,
216 if ((void *)lock < LP_POISON + CFS_PAGE_SIZE &&
217 (void *)lock >= LP_POISON) {
218 spin_unlock_bh(&waiting_locks_spinlock);
219 CERROR("free lock on elt list %p\n", lock);
222 cfs_list_del_init(&lock->l_pending_chain);
223 if ((void *)lock->l_export < LP_POISON + CFS_PAGE_SIZE &&
224 (void *)lock->l_export >= LP_POISON) {
225 CERROR("lock with free export on elt list %p\n",
227 lock->l_export = NULL;
228 LDLM_ERROR(lock, "free export");
229 /* release extra ref grabbed by
230 * ldlm_add_waiting_lock() or
231 * ldlm_failed_ast() */
232 LDLM_LOCK_RELEASE(lock);
236 if (lock->l_destroyed) {
237 /* release the lock refcount where
238 * waiting_locks_callback() founds */
239 LDLM_LOCK_RELEASE(lock);
242 export = class_export_lock_get(lock->l_export, lock);
243 spin_unlock_bh(&waiting_locks_spinlock);
246 class_fail_export(export);
247 class_export_lock_put(export, lock);
249 /* release extra ref grabbed by ldlm_add_waiting_lock()
250 * or ldlm_failed_ast() */
251 LDLM_LOCK_RELEASE(lock);
253 spin_lock_bh(&waiting_locks_spinlock);
255 spin_unlock_bh(&waiting_locks_spinlock);
257 if (do_dump && obd_dump_on_eviction) {
258 CERROR("dump the log upon eviction\n");
259 libcfs_debug_dumplog();
262 if (expired_lock_thread.elt_state == ELT_TERMINATE)
266 expired_lock_thread.elt_state = ELT_STOPPED;
267 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
271 static int ldlm_add_waiting_lock(struct ldlm_lock *lock);
272 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds);
275 * Check if there is a request in the export request list
276 * which prevents the lock canceling.
278 static int ldlm_lock_busy(struct ldlm_lock *lock)
280 struct ptlrpc_request *req;
284 if (lock->l_export == NULL)
287 spin_lock_bh(&lock->l_export->exp_rpc_lock);
288 cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
290 if (req->rq_ops->hpreq_lock_match) {
291 match = req->rq_ops->hpreq_lock_match(req, lock);
296 spin_unlock_bh(&lock->l_export->exp_rpc_lock);
300 /* This is called from within a timer interrupt and cannot schedule */
301 static void waiting_locks_callback(unsigned long unused)
303 struct ldlm_lock *lock;
306 spin_lock_bh(&waiting_locks_spinlock);
307 while (!cfs_list_empty(&waiting_locks_list)) {
308 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
310 if (cfs_time_after(lock->l_callback_timeout,
311 cfs_time_current()) ||
312 (lock->l_req_mode == LCK_GROUP))
315 if (ptlrpc_check_suspend()) {
316 /* there is a case when we talk to one mds, holding
317 * lock from another mds. this way we easily can get
318 * here, if second mds is being recovered. so, we
319 * suspend timeouts. bug 6019 */
321 LDLM_ERROR(lock, "recharge timeout: %s@%s nid %s ",
322 lock->l_export->exp_client_uuid.uuid,
323 lock->l_export->exp_connection->c_remote_uuid.uuid,
324 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
326 cfs_list_del_init(&lock->l_pending_chain);
327 if (lock->l_destroyed) {
328 /* relay the lock refcount decrease to
329 * expired lock thread */
330 cfs_list_add(&lock->l_pending_chain,
331 &expired_lock_thread.elt_expired_locks);
333 __ldlm_add_waiting_lock(lock,
334 ldlm_get_enq_timeout(lock));
339 /* if timeout overlaps the activation time of suspended timeouts
340 * then extend it to give a chance for client to reconnect */
341 if (cfs_time_before(cfs_time_sub(lock->l_callback_timeout,
342 cfs_time_seconds(obd_timeout)/2),
343 ptlrpc_suspend_wakeup_time())) {
344 LDLM_ERROR(lock, "extend timeout due to recovery: %s@%s nid %s ",
345 lock->l_export->exp_client_uuid.uuid,
346 lock->l_export->exp_connection->c_remote_uuid.uuid,
347 libcfs_nid2str(lock->l_export->exp_connection->c_peer.nid));
349 cfs_list_del_init(&lock->l_pending_chain);
350 if (lock->l_destroyed) {
351 /* relay the lock refcount decrease to
352 * expired lock thread */
353 cfs_list_add(&lock->l_pending_chain,
354 &expired_lock_thread.elt_expired_locks);
356 __ldlm_add_waiting_lock(lock,
357 ldlm_get_enq_timeout(lock));
362 /* Check if we need to prolong timeout */
363 if (!OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT) &&
364 ldlm_lock_busy(lock)) {
367 if (lock->l_pending_chain.next == &waiting_locks_list)
372 spin_unlock_bh(&waiting_locks_spinlock);
373 LDLM_DEBUG(lock, "prolong the busy lock");
374 ldlm_refresh_waiting_lock(lock,
375 ldlm_get_enq_timeout(lock));
376 spin_lock_bh(&waiting_locks_spinlock);
379 LDLM_LOCK_RELEASE(lock);
383 LDLM_LOCK_RELEASE(lock);
386 ldlm_lock_to_ns(lock)->ns_timeouts++;
387 LDLM_ERROR(lock, "lock callback timer expired after %lds: "
388 "evicting client at %s ",
389 cfs_time_current_sec()- lock->l_last_activity,
391 lock->l_export->exp_connection->c_peer.nid));
393 /* no needs to take an extra ref on the lock since it was in
394 * the waiting_locks_list and ldlm_add_waiting_lock()
395 * already grabbed a ref */
396 cfs_list_del(&lock->l_pending_chain);
397 cfs_list_add(&lock->l_pending_chain,
398 &expired_lock_thread.elt_expired_locks);
402 if (!cfs_list_empty(&expired_lock_thread.elt_expired_locks)) {
403 if (obd_dump_on_timeout && need_dump)
404 expired_lock_thread.elt_dump = __LINE__;
406 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
410 * Make sure the timer will fire again if we have any locks
413 if (!cfs_list_empty(&waiting_locks_list)) {
414 cfs_time_t timeout_rounded;
415 lock = cfs_list_entry(waiting_locks_list.next, struct ldlm_lock,
417 timeout_rounded = (cfs_time_t)round_timeout(lock->l_callback_timeout);
418 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
420 spin_unlock_bh(&waiting_locks_spinlock);
424 * Add lock to the list of contended locks.
426 * Indicate that we're waiting for a client to call us back cancelling a given
427 * lock. We add it to the pending-callback chain, and schedule the lock-timeout
428 * timer to fire appropriately. (We round up to the next second, to avoid
429 * floods of timer firings during periods of high lock contention and traffic).
430 * As done by ldlm_add_waiting_lock(), the caller must grab a lock reference
431 * if it has been added to the waiting list (1 is returned).
433 * Called with the namespace lock held.
435 static int __ldlm_add_waiting_lock(struct ldlm_lock *lock, int seconds)
438 cfs_time_t timeout_rounded;
440 if (!cfs_list_empty(&lock->l_pending_chain))
443 if (OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_NOTIMEOUT) ||
444 OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_HPREQ_TIMEOUT))
447 timeout = cfs_time_shift(seconds);
448 if (likely(cfs_time_after(timeout, lock->l_callback_timeout)))
449 lock->l_callback_timeout = timeout;
451 timeout_rounded = round_timeout(lock->l_callback_timeout);
453 if (cfs_time_before(timeout_rounded,
454 cfs_timer_deadline(&waiting_locks_timer)) ||
455 !cfs_timer_is_armed(&waiting_locks_timer)) {
456 cfs_timer_arm(&waiting_locks_timer, timeout_rounded);
458 /* if the new lock has a shorter timeout than something earlier on
459 the list, we'll wait the longer amount of time; no big deal. */
461 cfs_list_add_tail(&lock->l_pending_chain, &waiting_locks_list);
465 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
468 int timeout = ldlm_get_enq_timeout(lock);
470 /* NB: must be called with hold of lock_res_and_lock() */
471 LASSERT(lock->l_res_locked);
474 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
476 spin_lock_bh(&waiting_locks_spinlock);
477 if (lock->l_destroyed) {
478 static cfs_time_t next;
479 spin_unlock_bh(&waiting_locks_spinlock);
480 LDLM_ERROR(lock, "not waiting on destroyed lock (bug 5653)");
481 if (cfs_time_after(cfs_time_current(), next)) {
482 next = cfs_time_shift(14400);
483 libcfs_debug_dumpstack(NULL);
488 ret = __ldlm_add_waiting_lock(lock, timeout);
490 /* grab ref on the lock if it has been added to the
494 spin_unlock_bh(&waiting_locks_spinlock);
497 spin_lock_bh(&lock->l_export->exp_bl_list_lock);
498 if (cfs_list_empty(&lock->l_exp_list))
499 cfs_list_add(&lock->l_exp_list,
500 &lock->l_export->exp_bl_list);
501 spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
504 LDLM_DEBUG(lock, "%sadding to wait list(timeout: %d, AT: %s)",
505 ret == 0 ? "not re-" : "", timeout,
506 AT_OFF ? "off" : "on");
511 * Remove a lock from the pending list, likely because it had its cancellation
512 * callback arrive without incident. This adjusts the lock-timeout timer if
513 * needed. Returns 0 if the lock wasn't pending after all, 1 if it was.
514 * As done by ldlm_del_waiting_lock(), the caller must release the lock
515 * reference when the lock is removed from any list (1 is returned).
517 * Called with namespace lock held.
519 static int __ldlm_del_waiting_lock(struct ldlm_lock *lock)
521 cfs_list_t *list_next;
523 if (cfs_list_empty(&lock->l_pending_chain))
526 list_next = lock->l_pending_chain.next;
527 if (lock->l_pending_chain.prev == &waiting_locks_list) {
528 /* Removing the head of the list, adjust timer. */
529 if (list_next == &waiting_locks_list) {
530 /* No more, just cancel. */
531 cfs_timer_disarm(&waiting_locks_timer);
533 struct ldlm_lock *next;
534 next = cfs_list_entry(list_next, struct ldlm_lock,
536 cfs_timer_arm(&waiting_locks_timer,
537 round_timeout(next->l_callback_timeout));
540 cfs_list_del_init(&lock->l_pending_chain);
545 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
549 if (lock->l_export == NULL) {
550 /* We don't have a "waiting locks list" on clients. */
551 CDEBUG(D_DLMTRACE, "Client lock %p : no-op\n", lock);
555 spin_lock_bh(&waiting_locks_spinlock);
556 ret = __ldlm_del_waiting_lock(lock);
557 spin_unlock_bh(&waiting_locks_spinlock);
559 /* remove the lock out of export blocking list */
560 spin_lock_bh(&lock->l_export->exp_bl_list_lock);
561 cfs_list_del_init(&lock->l_exp_list);
562 spin_unlock_bh(&lock->l_export->exp_bl_list_lock);
565 /* release lock ref if it has indeed been removed
567 LDLM_LOCK_RELEASE(lock);
570 LDLM_DEBUG(lock, "%s", ret == 0 ? "wasn't waiting" : "removed");
573 EXPORT_SYMBOL(ldlm_del_waiting_lock);
576 * Prolong the contended lock waiting time.
578 * Called with namespace lock held.
580 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
582 if (lock->l_export == NULL) {
583 /* We don't have a "waiting locks list" on clients. */
584 LDLM_DEBUG(lock, "client lock: no-op");
588 spin_lock_bh(&waiting_locks_spinlock);
590 if (cfs_list_empty(&lock->l_pending_chain)) {
591 spin_unlock_bh(&waiting_locks_spinlock);
592 LDLM_DEBUG(lock, "wasn't waiting");
596 /* we remove/add the lock to the waiting list, so no needs to
597 * release/take a lock reference */
598 __ldlm_del_waiting_lock(lock);
599 __ldlm_add_waiting_lock(lock, timeout);
600 spin_unlock_bh(&waiting_locks_spinlock);
602 LDLM_DEBUG(lock, "refreshed");
605 EXPORT_SYMBOL(ldlm_refresh_waiting_lock);
607 #else /* !HAVE_SERVER_SUPPORT || !__KERNEL__ */
609 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
614 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
619 # ifdef HAVE_SERVER_SUPPORT
620 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
622 LASSERT(lock->l_res_locked);
623 LASSERT(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK));
628 #endif /* HAVE_SERVER_SUPPORT && __KERNEL__ */
630 #ifdef HAVE_SERVER_SUPPORT
633 * Perform lock cleanup if AST sending failed.
635 static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
636 const char *ast_type)
638 LCONSOLE_ERROR_MSG(0x138, "%s: A client on nid %s was evicted due "
639 "to a lock %s callback time out: rc %d\n",
640 lock->l_export->exp_obd->obd_name,
641 obd_export_nid2str(lock->l_export), ast_type, rc);
643 if (obd_dump_on_timeout)
644 libcfs_debug_dumplog();
646 spin_lock_bh(&waiting_locks_spinlock);
647 if (__ldlm_del_waiting_lock(lock) == 0)
648 /* the lock was not in any list, grab an extra ref before adding
649 * the lock to the expired list */
651 cfs_list_add(&lock->l_pending_chain,
652 &expired_lock_thread.elt_expired_locks);
653 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
654 spin_unlock_bh(&waiting_locks_spinlock);
656 class_fail_export(lock->l_export);
661 * Perform lock cleanup if AST reply came with error.
663 static int ldlm_handle_ast_error(struct ldlm_lock *lock,
664 struct ptlrpc_request *req, int rc,
665 const char *ast_type)
667 lnet_process_id_t peer = req->rq_import->imp_connection->c_peer;
669 if (rc == -ETIMEDOUT || rc == -EINTR || rc == -ENOTCONN) {
670 LASSERT(lock->l_export);
671 if (lock->l_export->exp_libclient) {
672 LDLM_DEBUG(lock, "%s AST to liblustre client (nid %s)"
673 " timeout, just cancelling lock", ast_type,
674 libcfs_nid2str(peer.nid));
675 ldlm_lock_cancel(lock);
677 } else if (lock->l_flags & LDLM_FL_CANCEL) {
678 LDLM_DEBUG(lock, "%s AST timeout from nid %s, but "
679 "cancel was received (AST reply lost?)",
680 ast_type, libcfs_nid2str(peer.nid));
681 ldlm_lock_cancel(lock);
684 ldlm_del_waiting_lock(lock);
685 ldlm_failed_ast(lock, rc, ast_type);
689 struct ldlm_resource *res = lock->l_resource;
690 LDLM_DEBUG(lock, "client (nid %s) returned %d"
691 " from %s AST - normal race",
692 libcfs_nid2str(peer.nid),
694 lustre_msg_get_status(req->rq_repmsg) : -1,
697 /* update lvbo to return proper attributes.
699 ldlm_resource_getref(res);
700 ldlm_res_lvbo_update(res, NULL, 1);
701 ldlm_resource_putref(res);
705 LDLM_ERROR(lock, "client (nid %s) returned %d "
706 "from %s AST", libcfs_nid2str(peer.nid),
707 (req->rq_repmsg != NULL) ?
708 lustre_msg_get_status(req->rq_repmsg) : 0,
711 ldlm_lock_cancel(lock);
712 /* Server-side AST functions are called from ldlm_reprocess_all,
713 * which needs to be told to please restart its reprocessing. */
720 static int ldlm_cb_interpret(const struct lu_env *env,
721 struct ptlrpc_request *req, void *data, int rc)
723 struct ldlm_cb_async_args *ca = data;
724 struct ldlm_lock *lock = ca->ca_lock;
725 struct ldlm_cb_set_arg *arg = ca->ca_set_arg;
728 LASSERT(lock != NULL);
731 case LDLM_GL_CALLBACK:
732 /* Update the LVB from disk if the AST failed
733 * (this is a legal race)
735 * - Glimpse callback of local lock just returns
736 * -ELDLM_NO_LOCK_DATA.
737 * - Glimpse callback of remote lock might return
738 * -ELDLM_NO_LOCK_DATA when inode is cleared. LU-274
740 if (rc == -ELDLM_NO_LOCK_DATA) {
741 LDLM_DEBUG(lock, "lost race - client has a lock but no "
743 ldlm_res_lvbo_update(lock->l_resource, NULL, 1);
744 } else if (rc != 0) {
745 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
747 rc = ldlm_res_lvbo_update(lock->l_resource, req, 1);
750 case LDLM_BL_CALLBACK:
752 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
754 case LDLM_CP_CALLBACK:
756 rc = ldlm_handle_ast_error(lock, req, rc, "completion");
759 LDLM_ERROR(lock, "invalid opcode for lock callback %d",
764 /* release extra reference taken in ldlm_ast_fini() */
765 LDLM_LOCK_RELEASE(lock);
768 cfs_atomic_inc(&arg->restart);
773 static inline int ldlm_ast_fini(struct ptlrpc_request *req,
774 struct ldlm_cb_set_arg *arg,
775 struct ldlm_lock *lock,
781 if (unlikely(instant_cancel)) {
782 rc = ptl_send_rpc(req, 1);
783 ptlrpc_req_finished(req);
785 cfs_atomic_inc(&arg->restart);
788 ptlrpc_set_add_req(arg->set, req);
795 * Check if there are requests in the export request list which prevent
796 * the lock canceling and make these requests high priority ones.
798 static void ldlm_lock_reorder_req(struct ldlm_lock *lock)
800 struct ptlrpc_request *req;
803 if (lock->l_export == NULL) {
804 LDLM_DEBUG(lock, "client lock: no-op");
808 spin_lock_bh(&lock->l_export->exp_rpc_lock);
809 cfs_list_for_each_entry(req, &lock->l_export->exp_hp_rpcs,
811 /* Do not process requests that were not yet added to there
812 * incoming queue or were already removed from there for
813 * processing. We evaluate ptlrpc_request_reorderable() without
814 * holding svcpt->scp_req_lock, and then redo the checks with
815 * the lock held once we need to obtain a reliable result.
817 if (ptlrpc_nrs_req_can_move(req) &&
818 req->rq_ops->hpreq_lock_match &&
819 req->rq_ops->hpreq_lock_match(req, lock))
820 ptlrpc_nrs_req_hp_move(req);
822 spin_unlock_bh(&lock->l_export->exp_rpc_lock);
827 * ->l_blocking_ast() method for server-side locks. This is invoked when newly
828 * enqueued server lock conflicts with given one.
830 * Sends blocking AST RPC to the client owning that lock; arms timeout timer
831 * to wait for client response.
833 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
834 struct ldlm_lock_desc *desc,
835 void *data, int flag)
837 struct ldlm_cb_async_args *ca;
838 struct ldlm_cb_set_arg *arg = data;
839 struct ldlm_request *body;
840 struct ptlrpc_request *req;
841 int instant_cancel = 0;
845 if (flag == LDLM_CB_CANCELING)
846 /* Don't need to do anything here. */
850 LASSERT(data != NULL);
851 if (lock->l_export->exp_obd->obd_recovering != 0)
852 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
854 ldlm_lock_reorder_req(lock);
856 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
857 &RQF_LDLM_BL_CALLBACK,
858 LUSTRE_DLM_VERSION, LDLM_BL_CALLBACK);
862 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
863 ca = ptlrpc_req_async_args(req);
864 ca->ca_set_arg = arg;
867 req->rq_interpret_reply = ldlm_cb_interpret;
868 req->rq_no_resend = 1;
870 lock_res_and_lock(lock);
871 if (lock->l_granted_mode != lock->l_req_mode) {
872 /* this blocking AST will be communicated as part of the
873 * completion AST instead */
874 unlock_res_and_lock(lock);
876 ptlrpc_req_finished(req);
877 LDLM_DEBUG(lock, "lock not granted, not sending blocking AST");
881 if (lock->l_destroyed) {
882 /* What's the point? */
883 unlock_res_and_lock(lock);
884 ptlrpc_req_finished(req);
888 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
891 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
892 body->lock_handle[0] = lock->l_remote_handle;
893 body->lock_desc = *desc;
894 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags & LDLM_AST_FLAGS);
896 LDLM_DEBUG(lock, "server preparing blocking AST");
898 ptlrpc_request_set_replen(req);
899 if (instant_cancel) {
900 unlock_res_and_lock(lock);
901 ldlm_lock_cancel(lock);
903 LASSERT(lock->l_granted_mode == lock->l_req_mode);
904 ldlm_add_waiting_lock(lock);
905 unlock_res_and_lock(lock);
908 req->rq_send_state = LUSTRE_IMP_FULL;
909 /* ptlrpc_request_alloc_pack already set timeout */
911 req->rq_timeout = ldlm_get_rq_timeout();
913 if (lock->l_export && lock->l_export->exp_nid_stats &&
914 lock->l_export->exp_nid_stats->nid_ldlm_stats)
915 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
916 LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
918 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
922 EXPORT_SYMBOL(ldlm_server_blocking_ast);
925 * ->l_completion_ast callback for a remote lock in server namespace.
927 * Sends AST to the client notifying it of lock granting. If initial
928 * lock response was not sent yet, instead of sending another RPC, just
929 * mark the lock as granted and client will understand
931 int ldlm_server_completion_ast(struct ldlm_lock *lock, __u64 flags, void *data)
933 struct ldlm_cb_set_arg *arg = data;
934 struct ldlm_request *body;
935 struct ptlrpc_request *req;
936 struct ldlm_cb_async_args *ca;
937 long total_enqueue_wait;
938 int instant_cancel = 0;
943 LASSERT(lock != NULL);
944 LASSERT(data != NULL);
946 total_enqueue_wait = cfs_time_sub(cfs_time_current_sec(),
947 lock->l_last_activity);
949 req = ptlrpc_request_alloc(lock->l_export->exp_imp_reverse,
950 &RQF_LDLM_CP_CALLBACK);
954 /* server namespace, doesn't need lock */
955 lvb_len = ldlm_lvbo_size(lock);
956 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT, lvb_len);
957 rc = ptlrpc_request_pack(req, LUSTRE_DLM_VERSION, LDLM_CP_CALLBACK);
959 ptlrpc_request_free(req);
963 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
964 ca = ptlrpc_req_async_args(req);
965 ca->ca_set_arg = arg;
968 req->rq_interpret_reply = ldlm_cb_interpret;
969 req->rq_no_resend = 1;
970 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
972 body->lock_handle[0] = lock->l_remote_handle;
973 body->lock_flags = ldlm_flags_to_wire(flags);
974 ldlm_lock2desc(lock, &body->lock_desc);
976 void *lvb = req_capsule_client_get(&req->rq_pill, &RMF_DLM_LVB);
978 lvb_len = ldlm_lvbo_fill(lock, lvb, lvb_len);
979 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB,
980 lvb_len, RCL_CLIENT);
983 LDLM_DEBUG(lock, "server preparing completion AST (after %lds wait)",
986 /* Server-side enqueue wait time estimate, used in
987 __ldlm_add_waiting_lock to set future enqueue timers */
988 if (total_enqueue_wait < ldlm_get_enq_timeout(lock))
989 at_measured(ldlm_lock_to_ns_at(lock),
992 /* bz18618. Don't add lock enqueue time we spend waiting for a
993 previous callback to fail. Locks waiting legitimately will
994 get extended by ldlm_refresh_waiting_lock regardless of the
995 estimate, so it's okay to underestimate here. */
996 LDLM_DEBUG(lock, "lock completed after %lus; estimate was %ds. "
997 "It is likely that a previous callback timed out.",
999 at_get(ldlm_lock_to_ns_at(lock)));
1001 ptlrpc_request_set_replen(req);
1003 req->rq_send_state = LUSTRE_IMP_FULL;
1004 /* ptlrpc_request_pack already set timeout */
1006 req->rq_timeout = ldlm_get_rq_timeout();
1008 /* We only send real blocking ASTs after the lock is granted */
1009 lock_res_and_lock(lock);
1010 if (lock->l_flags & LDLM_FL_AST_SENT) {
1011 body->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
1012 /* Copy AST flags like LDLM_FL_DISCARD_DATA. */
1013 body->lock_flags |= ldlm_flags_to_wire(lock->l_flags &
1016 /* We might get here prior to ldlm_handle_enqueue setting
1017 * LDLM_FL_CANCEL_ON_BLOCK flag. Then we will put this lock
1018 * into waiting list, but this is safe and similar code in
1019 * ldlm_handle_enqueue will call ldlm_lock_cancel() still,
1020 * that would not only cancel the lock, but will also remove
1021 * it from waiting list */
1022 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1023 unlock_res_and_lock(lock);
1024 ldlm_lock_cancel(lock);
1026 lock_res_and_lock(lock);
1028 /* start the lock-timeout clock */
1029 ldlm_add_waiting_lock(lock);
1032 unlock_res_and_lock(lock);
1034 if (lock->l_export && lock->l_export->exp_nid_stats &&
1035 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1036 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1037 LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
1039 rc = ldlm_ast_fini(req, arg, lock, instant_cancel);
1043 EXPORT_SYMBOL(ldlm_server_completion_ast);
1046 * Server side ->l_glimpse_ast handler for client locks.
1048 * Sends glimpse AST to the client and waits for reply. Then updates
1049 * lvbo with the result.
1051 int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
1053 struct ldlm_cb_set_arg *arg = data;
1054 struct ldlm_request *body;
1055 struct ptlrpc_request *req;
1056 struct ldlm_cb_async_args *ca;
1058 struct req_format *req_fmt;
1061 LASSERT(lock != NULL);
1063 if (arg->gl_desc != NULL)
1064 /* There is a glimpse descriptor to pack */
1065 req_fmt = &RQF_LDLM_GL_DESC_CALLBACK;
1067 req_fmt = &RQF_LDLM_GL_CALLBACK;
1069 req = ptlrpc_request_alloc_pack(lock->l_export->exp_imp_reverse,
1070 req_fmt, LUSTRE_DLM_VERSION,
1076 if (arg->gl_desc != NULL) {
1077 /* copy the GL descriptor */
1078 union ldlm_gl_desc *desc;
1079 desc = req_capsule_client_get(&req->rq_pill, &RMF_DLM_GL_DESC);
1080 *desc = *arg->gl_desc;
1083 body = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1084 body->lock_handle[0] = lock->l_remote_handle;
1085 ldlm_lock2desc(lock, &body->lock_desc);
1087 CLASSERT(sizeof(*ca) <= sizeof(req->rq_async_args));
1088 ca = ptlrpc_req_async_args(req);
1089 ca->ca_set_arg = arg;
1092 /* server namespace, doesn't need lock */
1093 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER,
1094 ldlm_lvbo_size(lock));
1095 ptlrpc_request_set_replen(req);
1097 req->rq_send_state = LUSTRE_IMP_FULL;
1098 /* ptlrpc_request_alloc_pack already set timeout */
1100 req->rq_timeout = ldlm_get_rq_timeout();
1102 req->rq_interpret_reply = ldlm_cb_interpret;
1104 if (lock->l_export && lock->l_export->exp_nid_stats &&
1105 lock->l_export->exp_nid_stats->nid_ldlm_stats)
1106 lprocfs_counter_incr(lock->l_export->exp_nid_stats->nid_ldlm_stats,
1107 LDLM_GL_CALLBACK - LDLM_FIRST_OPC);
1109 rc = ldlm_ast_fini(req, arg, lock, 0);
1113 EXPORT_SYMBOL(ldlm_server_glimpse_ast);
1115 int ldlm_glimpse_locks(struct ldlm_resource *res, cfs_list_t *gl_work_list)
1120 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), gl_work_list,
1122 if (rc == -ERESTART)
1123 ldlm_reprocess_all(res);
1127 EXPORT_SYMBOL(ldlm_glimpse_locks);
1129 /* return LDLM lock associated with a lock callback request */
1130 struct ldlm_lock *ldlm_request_lock(struct ptlrpc_request *req)
1132 struct ldlm_cb_async_args *ca;
1133 struct ldlm_lock *lock;
1136 ca = ptlrpc_req_async_args(req);
1139 RETURN(ERR_PTR(-EFAULT));
1143 EXPORT_SYMBOL(ldlm_request_lock);
1145 static void ldlm_svc_get_eopc(const struct ldlm_request *dlm_req,
1146 struct lprocfs_stats *srv_stats)
1148 int lock_type = 0, op = 0;
1150 lock_type = dlm_req->lock_desc.l_resource.lr_type;
1152 switch (lock_type) {
1154 op = PTLRPC_LAST_CNTR + LDLM_PLAIN_ENQUEUE;
1157 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT)
1158 op = PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE;
1160 op = PTLRPC_LAST_CNTR + LDLM_EXTENT_ENQUEUE;
1163 op = PTLRPC_LAST_CNTR + LDLM_FLOCK_ENQUEUE;
1166 op = PTLRPC_LAST_CNTR + LDLM_IBITS_ENQUEUE;
1174 lprocfs_counter_incr(srv_stats, op);
1180 * Main server-side entry point into LDLM for enqueue. This is called by ptlrpc
1181 * service threads to carry out client lock enqueueing requests.
1183 int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
1184 struct ptlrpc_request *req,
1185 const struct ldlm_request *dlm_req,
1186 const struct ldlm_callback_suite *cbs)
1188 struct ldlm_reply *dlm_rep;
1190 ldlm_error_t err = ELDLM_OK;
1191 struct ldlm_lock *lock = NULL;
1192 void *cookie = NULL;
1196 LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
1198 ldlm_request_cancel(req, dlm_req, LDLM_ENQUEUE_CANCEL_OFF);
1199 flags = ldlm_flags_from_wire(dlm_req->lock_flags);
1201 LASSERT(req->rq_export);
1203 if (ptlrpc_req2svc(req)->srv_stats != NULL)
1204 ldlm_svc_get_eopc(dlm_req, ptlrpc_req2svc(req)->srv_stats);
1206 if (req->rq_export && req->rq_export->exp_nid_stats &&
1207 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1208 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1209 LDLM_ENQUEUE - LDLM_FIRST_OPC);
1211 if (unlikely(dlm_req->lock_desc.l_resource.lr_type < LDLM_MIN_TYPE ||
1212 dlm_req->lock_desc.l_resource.lr_type >= LDLM_MAX_TYPE)) {
1213 DEBUG_REQ(D_ERROR, req, "invalid lock request type %d",
1214 dlm_req->lock_desc.l_resource.lr_type);
1215 GOTO(out, rc = -EFAULT);
1218 if (unlikely(dlm_req->lock_desc.l_req_mode <= LCK_MINMODE ||
1219 dlm_req->lock_desc.l_req_mode >= LCK_MAXMODE ||
1220 dlm_req->lock_desc.l_req_mode &
1221 (dlm_req->lock_desc.l_req_mode-1))) {
1222 DEBUG_REQ(D_ERROR, req, "invalid lock request mode %d",
1223 dlm_req->lock_desc.l_req_mode);
1224 GOTO(out, rc = -EFAULT);
1227 if (exp_connect_flags(req->rq_export) & OBD_CONNECT_IBITS) {
1228 if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1230 DEBUG_REQ(D_ERROR, req,
1231 "PLAIN lock request from IBITS client?");
1232 GOTO(out, rc = -EPROTO);
1234 } else if (unlikely(dlm_req->lock_desc.l_resource.lr_type ==
1236 DEBUG_REQ(D_ERROR, req,
1237 "IBITS lock request from unaware client?");
1238 GOTO(out, rc = -EPROTO);
1242 /* FIXME this makes it impossible to use LDLM_PLAIN locks -- check
1243 against server's _CONNECT_SUPPORTED flags? (I don't want to use
1244 ibits for mgc/mgs) */
1246 /* INODEBITS_INTEROP: Perform conversion from plain lock to
1247 * inodebits lock if client does not support them. */
1248 if (!(exp_connect_flags(req->rq_export) & OBD_CONNECT_IBITS) &&
1249 (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN)) {
1250 dlm_req->lock_desc.l_resource.lr_type = LDLM_IBITS;
1251 dlm_req->lock_desc.l_policy_data.l_inodebits.bits =
1252 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE;
1253 if (dlm_req->lock_desc.l_req_mode == LCK_PR)
1254 dlm_req->lock_desc.l_req_mode = LCK_CR;
1258 if (unlikely(flags & LDLM_FL_REPLAY)) {
1259 /* Find an existing lock in the per-export lock hash */
1260 /* In the function below, .hs_keycmp resolves to
1261 * ldlm_export_lock_keycmp() */
1262 /* coverity[overrun-buffer-val] */
1263 lock = cfs_hash_lookup(req->rq_export->exp_lock_hash,
1264 (void *)&dlm_req->lock_handle[0]);
1266 DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
1267 LPX64, lock->l_handle.h_cookie);
1268 GOTO(existing_lock, rc = 0);
1272 /* The lock's callback data might be set in the policy function */
1273 lock = ldlm_lock_create(ns, &dlm_req->lock_desc.l_resource.lr_name,
1274 dlm_req->lock_desc.l_resource.lr_type,
1275 dlm_req->lock_desc.l_req_mode,
1276 cbs, NULL, 0, LVB_T_NONE);
1278 GOTO(out, rc = -ENOMEM);
1280 lock->l_last_activity = cfs_time_current_sec();
1281 lock->l_remote_handle = dlm_req->lock_handle[0];
1282 LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
1284 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_ENQUEUE_BLOCKED, obd_timeout * 2);
1285 /* Don't enqueue a lock onto the export if it is been disonnected
1286 * due to eviction (bug 3822) or server umount (bug 24324).
1287 * Cancel it now instead. */
1288 if (req->rq_export->exp_disconnected) {
1289 LDLM_ERROR(lock, "lock on disconnected export %p",
1291 GOTO(out, rc = -ENOTCONN);
1294 lock->l_export = class_export_lock_get(req->rq_export, lock);
1295 if (lock->l_export->exp_lock_hash)
1296 cfs_hash_add(lock->l_export->exp_lock_hash,
1297 &lock->l_remote_handle,
1302 if (flags & LDLM_FL_HAS_INTENT) {
1303 /* In this case, the reply buffer is allocated deep in
1304 * local_lock_enqueue by the policy function. */
1307 /* based on the assumption that lvb size never changes during
1308 * resource life time otherwise it need resource->lr_lock's
1310 req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB,
1311 RCL_SERVER, ldlm_lvbo_size(lock));
1313 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_EXTENT_ERR))
1314 GOTO(out, rc = -ENOMEM);
1316 rc = req_capsule_server_pack(&req->rq_pill);
1321 if (dlm_req->lock_desc.l_resource.lr_type != LDLM_PLAIN)
1322 ldlm_convert_policy_to_local(req->rq_export,
1323 dlm_req->lock_desc.l_resource.lr_type,
1324 &dlm_req->lock_desc.l_policy_data,
1325 &lock->l_policy_data);
1326 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
1327 lock->l_req_extent = lock->l_policy_data.l_extent;
1329 err = ldlm_lock_enqueue(ns, &lock, cookie, &flags);
1333 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1334 dlm_rep->lock_flags = ldlm_flags_to_wire(flags);
1336 ldlm_lock2desc(lock, &dlm_rep->lock_desc);
1337 ldlm_lock2handle(lock, &dlm_rep->lock_handle);
1339 /* We never send a blocking AST until the lock is granted, but
1340 * we can tell it right now */
1341 lock_res_and_lock(lock);
1343 /* Now take into account flags to be inherited from original lock
1344 request both in reply to client and in our own lock flags. */
1345 dlm_rep->lock_flags |= dlm_req->lock_flags & LDLM_INHERIT_FLAGS;
1346 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
1347 LDLM_INHERIT_FLAGS);
1349 /* Don't move a pending lock onto the export if it has already been
1350 * disconnected due to eviction (bug 5683) or server umount (bug 24324).
1351 * Cancel it now instead. */
1352 if (unlikely(req->rq_export->exp_disconnected ||
1353 OBD_FAIL_CHECK(OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT))) {
1354 LDLM_ERROR(lock, "lock on destroyed export %p", req->rq_export);
1356 } else if (lock->l_flags & LDLM_FL_AST_SENT) {
1357 dlm_rep->lock_flags |= ldlm_flags_to_wire(LDLM_FL_AST_SENT);
1358 if (lock->l_granted_mode == lock->l_req_mode) {
1360 * Only cancel lock if it was granted, because it would
1361 * be destroyed immediately and would never be granted
1362 * in the future, causing timeouts on client. Not
1363 * granted lock will be cancelled immediately after
1364 * sending completion AST.
1366 if (dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK) {
1367 unlock_res_and_lock(lock);
1368 ldlm_lock_cancel(lock);
1369 lock_res_and_lock(lock);
1371 ldlm_add_waiting_lock(lock);
1374 /* Make sure we never ever grant usual metadata locks to liblustre
1376 if ((dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN ||
1377 dlm_req->lock_desc.l_resource.lr_type == LDLM_IBITS) &&
1378 req->rq_export->exp_libclient) {
1379 if (unlikely(!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK) ||
1380 !(dlm_rep->lock_flags & LDLM_FL_CANCEL_ON_BLOCK))){
1381 CERROR("Granting sync lock to libclient. "
1382 "req fl %d, rep fl %d, lock fl "LPX64"\n",
1383 dlm_req->lock_flags, dlm_rep->lock_flags,
1385 LDLM_ERROR(lock, "sync lock");
1386 if (dlm_req->lock_flags & LDLM_FL_HAS_INTENT) {
1387 struct ldlm_intent *it;
1389 it = req_capsule_client_get(&req->rq_pill,
1392 CERROR("This is intent %s ("LPU64")\n",
1393 ldlm_it2str(it->opc), it->opc);
1399 unlock_res_and_lock(lock);
1403 req->rq_status = rc ?: err; /* return either error - bug 11190 */
1404 if (!req->rq_packed_final) {
1405 err = lustre_pack_reply(req, 1, NULL, NULL);
1410 /* The LOCK_CHANGED code in ldlm_lock_enqueue depends on this
1411 * ldlm_reprocess_all. If this moves, revisit that code. -phil */
1413 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
1414 "(err=%d, rc=%d)", err, rc);
1417 if (req_capsule_has_field(&req->rq_pill, &RMF_DLM_LVB,
1419 ldlm_lvbo_size(lock) > 0) {
1423 buf = req_capsule_server_get(&req->rq_pill,
1425 LASSERTF(buf != NULL, "req %p, lock %p\n",
1427 buflen = req_capsule_get_size(&req->rq_pill,
1428 &RMF_DLM_LVB, RCL_SERVER);
1429 buflen = ldlm_lvbo_fill(lock, buf, buflen);
1430 req_capsule_shrink(&req->rq_pill, &RMF_DLM_LVB,
1431 buflen, RCL_SERVER);
1434 lock_res_and_lock(lock);
1435 ldlm_resource_unlink_lock(lock);
1436 ldlm_lock_destroy_nolock(lock);
1437 unlock_res_and_lock(lock);
1440 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
1441 ldlm_reprocess_all(lock->l_resource);
1443 LDLM_LOCK_RELEASE(lock);
1446 LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p, rc %d)",
1451 EXPORT_SYMBOL(ldlm_handle_enqueue0);
1454 * Old-style LDLM main entry point for server code enqueue.
1456 int ldlm_handle_enqueue(struct ptlrpc_request *req,
1457 ldlm_completion_callback completion_callback,
1458 ldlm_blocking_callback blocking_callback,
1459 ldlm_glimpse_callback glimpse_callback)
1461 struct ldlm_request *dlm_req;
1462 struct ldlm_callback_suite cbs = {
1463 .lcs_completion = completion_callback,
1464 .lcs_blocking = blocking_callback,
1465 .lcs_glimpse = glimpse_callback
1469 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1470 if (dlm_req != NULL) {
1471 rc = ldlm_handle_enqueue0(req->rq_export->exp_obd->obd_namespace,
1472 req, dlm_req, &cbs);
1478 EXPORT_SYMBOL(ldlm_handle_enqueue);
1481 * Main LDLM entry point for server code to process lock conversion requests.
1483 int ldlm_handle_convert0(struct ptlrpc_request *req,
1484 const struct ldlm_request *dlm_req)
1486 struct ldlm_reply *dlm_rep;
1487 struct ldlm_lock *lock;
1491 if (req->rq_export && req->rq_export->exp_nid_stats &&
1492 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1493 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1494 LDLM_CONVERT - LDLM_FIRST_OPC);
1496 rc = req_capsule_server_pack(&req->rq_pill);
1500 dlm_rep = req_capsule_server_get(&req->rq_pill, &RMF_DLM_REP);
1501 dlm_rep->lock_flags = dlm_req->lock_flags;
1503 lock = ldlm_handle2lock(&dlm_req->lock_handle[0]);
1505 req->rq_status = EINVAL;
1509 LDLM_DEBUG(lock, "server-side convert handler START");
1511 lock->l_last_activity = cfs_time_current_sec();
1512 res = ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
1513 &dlm_rep->lock_flags);
1515 if (ldlm_del_waiting_lock(lock))
1516 LDLM_DEBUG(lock, "converted waiting lock");
1519 req->rq_status = EDEADLOCK;
1524 if (!req->rq_status)
1525 ldlm_reprocess_all(lock->l_resource);
1526 LDLM_DEBUG(lock, "server-side convert handler END");
1527 LDLM_LOCK_PUT(lock);
1529 LDLM_DEBUG_NOLOCK("server-side convert handler END");
1533 EXPORT_SYMBOL(ldlm_handle_convert0);
1536 * Old-style main LDLM entry point for server code to process lock conversion
1539 int ldlm_handle_convert(struct ptlrpc_request *req)
1542 struct ldlm_request *dlm_req;
1544 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1545 if (dlm_req != NULL) {
1546 rc = ldlm_handle_convert0(req, dlm_req);
1548 CERROR ("Can't unpack dlm_req\n");
1553 EXPORT_SYMBOL(ldlm_handle_convert);
1556 * Cancel all the locks whose handles are packed into ldlm_request
1558 * Called by server code expecting such combined cancel activity
1561 int ldlm_request_cancel(struct ptlrpc_request *req,
1562 const struct ldlm_request *dlm_req, int first)
1564 struct ldlm_resource *res, *pres = NULL;
1565 struct ldlm_lock *lock;
1566 int i, count, done = 0;
1569 count = dlm_req->lock_count ? dlm_req->lock_count : 1;
1573 if (count == 1 && dlm_req->lock_handle[0].cookie == 0)
1576 /* There is no lock on the server at the replay time,
1577 * skip lock cancelling to make replay tests to pass. */
1578 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
1581 LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
1582 "starting at %d", count, first);
1584 for (i = first; i < count; i++) {
1585 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
1587 LDLM_DEBUG_NOLOCK("server-side cancel handler stale "
1588 "lock (cookie "LPU64")",
1589 dlm_req->lock_handle[i].cookie);
1593 res = lock->l_resource;
1596 /* This code is an optimization to only attempt lock
1597 * granting on the resource (that could be CPU-expensive)
1598 * after we are done cancelling lock in that resource. */
1601 ldlm_reprocess_all(pres);
1602 LDLM_RESOURCE_DELREF(pres);
1603 ldlm_resource_putref(pres);
1606 ldlm_resource_getref(res);
1607 LDLM_RESOURCE_ADDREF(res);
1608 ldlm_res_lvbo_update(res, NULL, 1);
1612 ldlm_lock_cancel(lock);
1613 LDLM_LOCK_PUT(lock);
1616 ldlm_reprocess_all(pres);
1617 LDLM_RESOURCE_DELREF(pres);
1618 ldlm_resource_putref(pres);
1620 LDLM_DEBUG_NOLOCK("server-side cancel handler END");
1623 EXPORT_SYMBOL(ldlm_request_cancel);
1626 * Main LDLM entry point for server code to cancel locks.
1628 * Typically gets called from service handler on LDLM_CANCEL opc.
1630 int ldlm_handle_cancel(struct ptlrpc_request *req)
1632 struct ldlm_request *dlm_req;
1636 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
1637 if (dlm_req == NULL) {
1638 CDEBUG(D_INFO, "bad request buffer for cancel\n");
1642 if (req->rq_export && req->rq_export->exp_nid_stats &&
1643 req->rq_export->exp_nid_stats->nid_ldlm_stats)
1644 lprocfs_counter_incr(req->rq_export->exp_nid_stats->nid_ldlm_stats,
1645 LDLM_CANCEL - LDLM_FIRST_OPC);
1647 rc = req_capsule_server_pack(&req->rq_pill);
1651 if (!ldlm_request_cancel(req, dlm_req, 0))
1652 req->rq_status = ESTALE;
1654 RETURN(ptlrpc_reply(req));
1656 EXPORT_SYMBOL(ldlm_handle_cancel);
1657 #endif /* HAVE_SERVER_SUPPORT */
1660 * Callback handler for receiving incoming blocking ASTs.
1662 * This can only happen on client side.
1664 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
1665 struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
1670 LDLM_DEBUG(lock, "client blocking AST callback handler");
1672 lock_res_and_lock(lock);
1673 lock->l_flags |= LDLM_FL_CBPENDING;
1675 if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
1676 lock->l_flags |= LDLM_FL_CANCEL;
1678 do_ast = (!lock->l_readers && !lock->l_writers);
1679 unlock_res_and_lock(lock);
1682 CDEBUG(D_DLMTRACE, "Lock %p already unused, calling callback (%p)\n",
1683 lock, lock->l_blocking_ast);
1684 if (lock->l_blocking_ast != NULL)
1685 lock->l_blocking_ast(lock, ld, lock->l_ast_data,
1688 CDEBUG(D_DLMTRACE, "Lock %p is referenced, will be cancelled later\n",
1692 LDLM_DEBUG(lock, "client blocking callback handler END");
1693 LDLM_LOCK_RELEASE(lock);
1698 * Callback handler for receiving incoming completion ASTs.
1700 * This only can happen on client side.
1702 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
1703 struct ldlm_namespace *ns,
1704 struct ldlm_request *dlm_req,
1705 struct ldlm_lock *lock)
1708 CFS_LIST_HEAD(ast_list);
1712 LDLM_DEBUG(lock, "client completion callback handler START");
1714 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
1715 int to = cfs_time_seconds(1);
1717 cfs_schedule_timeout_and_set_state(
1718 CFS_TASK_INTERRUPTIBLE, to);
1719 if (lock->l_granted_mode == lock->l_req_mode ||
1725 lvb_len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT);
1727 LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", lvb_len);
1728 GOTO(out, rc = lvb_len);
1729 } else if (lvb_len > 0) {
1730 if (lock->l_lvb_len > 0) {
1731 /* for extent lock, lvb contains ost_lvb{}. */
1732 LASSERT(lock->l_lvb_data != NULL);
1734 if (unlikely(lock->l_lvb_len < lvb_len)) {
1735 LDLM_ERROR(lock, "Replied LVB is larger than "
1736 "expectation, expected = %d, "
1738 lock->l_lvb_len, lvb_len);
1739 GOTO(out, rc = -EINVAL);
1741 } else if (ldlm_has_layout(lock)) { /* for layout lock, lvb has
1742 * variable length */
1745 OBD_ALLOC(lvb_data, lvb_len);
1746 if (lvb_data == NULL) {
1747 LDLM_ERROR(lock, "No memory: %d.\n", lvb_len);
1748 GOTO(out, rc = -ENOMEM);
1751 lock_res_and_lock(lock);
1752 LASSERT(lock->l_lvb_data == NULL);
1753 lock->l_lvb_data = lvb_data;
1754 lock->l_lvb_len = lvb_len;
1755 unlock_res_and_lock(lock);
1759 lock_res_and_lock(lock);
1760 if (lock->l_destroyed ||
1761 lock->l_granted_mode == lock->l_req_mode) {
1762 /* bug 11300: the lock has already been granted */
1763 unlock_res_and_lock(lock);
1764 LDLM_DEBUG(lock, "Double grant race happened");
1768 /* If we receive the completion AST before the actual enqueue returned,
1769 * then we might need to switch lock modes, resources, or extents. */
1770 if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
1771 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
1772 LDLM_DEBUG(lock, "completion AST, new lock mode");
1775 if (lock->l_resource->lr_type != LDLM_PLAIN) {
1776 ldlm_convert_policy_to_local(req->rq_export,
1777 dlm_req->lock_desc.l_resource.lr_type,
1778 &dlm_req->lock_desc.l_policy_data,
1779 &lock->l_policy_data);
1780 LDLM_DEBUG(lock, "completion AST, new policy data");
1783 ldlm_resource_unlink_lock(lock);
1784 if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
1785 &lock->l_resource->lr_name,
1786 sizeof(lock->l_resource->lr_name)) != 0) {
1787 unlock_res_and_lock(lock);
1788 rc = ldlm_lock_change_resource(ns, lock,
1789 &dlm_req->lock_desc.l_resource.lr_name);
1791 LDLM_ERROR(lock, "Failed to allocate resource");
1794 LDLM_DEBUG(lock, "completion AST, new resource");
1795 CERROR("change resource!\n");
1796 lock_res_and_lock(lock);
1799 if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
1800 /* BL_AST locks are not needed in LRU.
1801 * Let ldlm_cancel_lru() be fast. */
1802 ldlm_lock_remove_from_lru(lock);
1803 lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
1804 LDLM_DEBUG(lock, "completion AST includes blocking AST");
1807 if (lock->l_lvb_len > 0) {
1808 rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_CLIENT,
1809 lock->l_lvb_data, lvb_len);
1811 unlock_res_and_lock(lock);
1816 ldlm_grant_lock(lock, &ast_list);
1817 unlock_res_and_lock(lock);
1819 LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
1821 /* Let Enqueue to call osc_lock_upcall() and initialize
1823 OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
1825 ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
1827 LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
1833 lock_res_and_lock(lock);
1834 lock->l_flags |= LDLM_FL_FAILED;
1835 unlock_res_and_lock(lock);
1836 cfs_waitq_signal(&lock->l_waitq);
1838 LDLM_LOCK_RELEASE(lock);
1842 * Callback handler for receiving incoming glimpse ASTs.
1844 * This only can happen on client side. After handling the glimpse AST
1845 * we also consider dropping the lock here if it is unused locally for a
1848 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
1849 struct ldlm_namespace *ns,
1850 struct ldlm_request *dlm_req,
1851 struct ldlm_lock *lock)
1856 LDLM_DEBUG(lock, "client glimpse AST callback handler");
1858 if (lock->l_glimpse_ast != NULL)
1859 rc = lock->l_glimpse_ast(lock, req);
1861 if (req->rq_repmsg != NULL) {
1864 req->rq_status = rc;
1868 lock_res_and_lock(lock);
1869 if (lock->l_granted_mode == LCK_PW &&
1870 !lock->l_readers && !lock->l_writers &&
1871 cfs_time_after(cfs_time_current(),
1872 cfs_time_add(lock->l_last_used,
1873 cfs_time_seconds(10)))) {
1874 unlock_res_and_lock(lock);
1875 if (ldlm_bl_to_thread_lock(ns, NULL, lock))
1876 ldlm_handle_bl_callback(ns, NULL, lock);
1881 unlock_res_and_lock(lock);
1882 LDLM_LOCK_RELEASE(lock);
1886 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
1888 if (req->rq_no_reply)
1891 req->rq_status = rc;
1892 if (!req->rq_packed_final) {
1893 rc = lustre_pack_reply(req, 1, NULL, NULL);
1897 return ptlrpc_reply(req);
1901 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi, int mode)
1903 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1906 spin_lock(&blp->blp_lock);
1907 if (blwi->blwi_lock &&
1908 blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
1909 /* add LDLM_FL_DISCARD_DATA requests to the priority list */
1910 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
1912 /* other blocking callbacks are added to the regular list */
1913 cfs_list_add_tail(&blwi->blwi_entry, &blp->blp_list);
1915 spin_unlock(&blp->blp_lock);
1917 cfs_waitq_signal(&blp->blp_waitq);
1919 /* can not use blwi->blwi_mode as blwi could be already freed in
1921 if (mode == LDLM_SYNC)
1922 wait_for_completion(&blwi->blwi_comp);
1927 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
1928 struct ldlm_namespace *ns,
1929 struct ldlm_lock_desc *ld,
1930 cfs_list_t *cancels, int count,
1931 struct ldlm_lock *lock,
1934 init_completion(&blwi->blwi_comp);
1935 CFS_INIT_LIST_HEAD(&blwi->blwi_head);
1937 if (cfs_memory_pressure_get())
1938 blwi->blwi_mem_pressure = 1;
1941 blwi->blwi_mode = mode;
1943 blwi->blwi_ld = *ld;
1945 cfs_list_add(&blwi->blwi_head, cancels);
1946 cfs_list_del_init(cancels);
1947 blwi->blwi_count = count;
1949 blwi->blwi_lock = lock;
1954 * Queues a list of locks \a cancels containing \a count locks
1955 * for later processing by a blocking thread. If \a count is zero,
1956 * then the lock referenced as \a lock is queued instead.
1958 * The blocking thread would then call ->l_blocking_ast callback in the lock.
1959 * If list addition fails an error is returned and caller is supposed to
1960 * call ->l_blocking_ast itself.
1962 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
1963 struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
1964 cfs_list_t *cancels, int count, int mode)
1968 if (cancels && count == 0)
1971 if (mode == LDLM_SYNC) {
1972 /* if it is synchronous call do minimum mem alloc, as it could
1973 * be triggered from kernel shrinker
1975 struct ldlm_bl_work_item blwi;
1976 memset(&blwi, 0, sizeof(blwi));
1977 init_blwi(&blwi, ns, ld, cancels, count, lock, LDLM_SYNC);
1978 RETURN(__ldlm_bl_to_thread(&blwi, LDLM_SYNC));
1980 struct ldlm_bl_work_item *blwi;
1981 OBD_ALLOC(blwi, sizeof(*blwi));
1984 init_blwi(blwi, ns, ld, cancels, count, lock, LDLM_ASYNC);
1986 RETURN(__ldlm_bl_to_thread(blwi, LDLM_ASYNC));
1992 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
1993 struct ldlm_lock *lock)
1996 RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LDLM_ASYNC));
2002 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
2003 cfs_list_t *cancels, int count, int mode)
2006 RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count, mode));
2012 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
2013 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
2015 struct obd_device *obd = req->rq_export->exp_obd;
2022 DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
2024 req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
2026 key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
2028 DEBUG_REQ(D_IOCTL, req, "no set_info key");
2031 keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
2033 val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
2035 DEBUG_REQ(D_IOCTL, req, "no set_info val");
2038 vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
2041 /* We are responsible for swabbing contents of val */
2043 if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
2044 /* Pass it on to mdc (the "export" in this case) */
2045 rc = obd_set_info_async(req->rq_svc_thread->t_env,
2047 sizeof(KEY_HSM_COPYTOOL_SEND),
2048 KEY_HSM_COPYTOOL_SEND,
2051 DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
2056 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
2057 const char *msg, int rc,
2058 struct lustre_handle *handle)
2060 DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
2061 "%s: [nid %s] [rc %d] [lock "LPX64"]",
2062 msg, libcfs_id2str(req->rq_peer), rc,
2063 handle ? handle->cookie : 0);
2064 if (req->rq_no_reply)
2065 CWARN("No reply was sent, maybe cause bug 21636.\n");
2067 CWARN("Send reply failed, maybe cause bug 21636.\n");
2070 static int ldlm_handle_qc_callback(struct ptlrpc_request *req)
2072 struct obd_quotactl *oqctl;
2073 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
2075 oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2076 if (oqctl == NULL) {
2077 CERROR("Can't unpack obd_quotactl\n");
2081 cli->cl_qchk_stat = oqctl->qc_stat;
2085 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
2086 static int ldlm_callback_handler(struct ptlrpc_request *req)
2088 struct ldlm_namespace *ns;
2089 struct ldlm_request *dlm_req;
2090 struct ldlm_lock *lock;
2094 /* Requests arrive in sender's byte order. The ptlrpc service
2095 * handler has already checked and, if necessary, byte-swapped the
2096 * incoming request message body, but I am responsible for the
2097 * message buffers. */
2099 /* do nothing for sec context finalize */
2100 if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
2103 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2105 if (req->rq_export == NULL) {
2106 rc = ldlm_callback_reply(req, -ENOTCONN);
2107 ldlm_callback_errmsg(req, "Operate on unconnected server",
2112 LASSERT(req->rq_export != NULL);
2113 LASSERT(req->rq_export->exp_obd != NULL);
2115 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2116 case LDLM_BL_CALLBACK:
2117 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
2120 case LDLM_CP_CALLBACK:
2121 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK_NET))
2124 case LDLM_GL_CALLBACK:
2125 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK_NET))
2129 rc = ldlm_handle_setinfo(req);
2130 ldlm_callback_reply(req, rc);
2132 case OBD_LOG_CANCEL: /* remove this eventually - for 1.4.0 compat */
2133 CERROR("shouldn't be handling OBD_LOG_CANCEL on DLM thread\n");
2134 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2135 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2137 rc = llog_origin_handle_cancel(req);
2138 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2140 ldlm_callback_reply(req, rc);
2142 case LLOG_ORIGIN_HANDLE_CREATE:
2143 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
2144 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2146 rc = llog_origin_handle_open(req);
2147 ldlm_callback_reply(req, rc);
2149 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
2150 req_capsule_set(&req->rq_pill,
2151 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
2152 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2154 rc = llog_origin_handle_next_block(req);
2155 ldlm_callback_reply(req, rc);
2157 case LLOG_ORIGIN_HANDLE_READ_HEADER:
2158 req_capsule_set(&req->rq_pill,
2159 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
2160 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2162 rc = llog_origin_handle_read_header(req);
2163 ldlm_callback_reply(req, rc);
2165 case LLOG_ORIGIN_HANDLE_CLOSE:
2166 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOGD_NET))
2168 rc = llog_origin_handle_close(req);
2169 ldlm_callback_reply(req, rc);
2171 case OBD_QC_CALLBACK:
2172 req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
2173 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
2175 rc = ldlm_handle_qc_callback(req);
2176 ldlm_callback_reply(req, rc);
2179 CERROR("unknown opcode %u\n",
2180 lustre_msg_get_opc(req->rq_reqmsg));
2181 ldlm_callback_reply(req, -EPROTO);
2185 ns = req->rq_export->exp_obd->obd_namespace;
2186 LASSERT(ns != NULL);
2188 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2190 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2191 if (dlm_req == NULL) {
2192 rc = ldlm_callback_reply(req, -EPROTO);
2193 ldlm_callback_errmsg(req, "Operate without parameter", rc,
2198 /* Force a known safe race, send a cancel to the server for a lock
2199 * which the server has already started a blocking callback on. */
2200 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
2201 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2202 rc = ldlm_cli_cancel(&dlm_req->lock_handle[0]);
2204 CERROR("ldlm_cli_cancel: %d\n", rc);
2207 lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
2209 CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
2210 "disappeared\n", dlm_req->lock_handle[0].cookie);
2211 rc = ldlm_callback_reply(req, -EINVAL);
2212 ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
2213 &dlm_req->lock_handle[0]);
2217 if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
2218 lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
2219 OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
2221 /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
2222 lock_res_and_lock(lock);
2223 lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
2225 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
2226 /* If somebody cancels lock and cache is already dropped,
2227 * or lock is failed before cp_ast received on client,
2228 * we can tell the server we have no lock. Otherwise, we
2229 * should send cancel after dropping the cache. */
2230 if (((lock->l_flags & LDLM_FL_CANCELING) &&
2231 (lock->l_flags & LDLM_FL_BL_DONE)) ||
2232 (lock->l_flags & LDLM_FL_FAILED)) {
2233 LDLM_DEBUG(lock, "callback on lock "
2234 LPX64" - lock disappeared\n",
2235 dlm_req->lock_handle[0].cookie);
2236 unlock_res_and_lock(lock);
2237 LDLM_LOCK_RELEASE(lock);
2238 rc = ldlm_callback_reply(req, -EINVAL);
2239 ldlm_callback_errmsg(req, "Operate on stale lock", rc,
2240 &dlm_req->lock_handle[0]);
2243 /* BL_AST locks are not needed in LRU.
2244 * Let ldlm_cancel_lru() be fast. */
2245 ldlm_lock_remove_from_lru(lock);
2246 lock->l_flags |= LDLM_FL_BL_AST;
2248 unlock_res_and_lock(lock);
2250 /* We want the ost thread to get this reply so that it can respond
2251 * to ost requests (write cache writeback) that might be triggered
2254 * But we'd also like to be able to indicate in the reply that we're
2255 * cancelling right now, because it's unused, or have an intent result
2256 * in the reply, so we might have to push the responsibility for sending
2257 * the reply down into the AST handlers, alas. */
2259 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2260 case LDLM_BL_CALLBACK:
2261 CDEBUG(D_INODE, "blocking ast\n");
2262 req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
2263 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
2264 rc = ldlm_callback_reply(req, 0);
2265 if (req->rq_no_reply || rc)
2266 ldlm_callback_errmsg(req, "Normal process", rc,
2267 &dlm_req->lock_handle[0]);
2269 if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
2270 ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
2272 case LDLM_CP_CALLBACK:
2273 CDEBUG(D_INODE, "completion ast\n");
2274 req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
2275 ldlm_callback_reply(req, 0);
2276 ldlm_handle_cp_callback(req, ns, dlm_req, lock);
2278 case LDLM_GL_CALLBACK:
2279 CDEBUG(D_INODE, "glimpse ast\n");
2280 req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
2281 ldlm_handle_gl_callback(req, ns, dlm_req, lock);
2284 LBUG(); /* checked above */
2290 #ifdef HAVE_SERVER_SUPPORT
2292 * Main handler for canceld thread.
2294 * Separated into its own thread to avoid deadlocks.
2296 static int ldlm_cancel_handler(struct ptlrpc_request *req)
2301 /* Requests arrive in sender's byte order. The ptlrpc service
2302 * handler has already checked and, if necessary, byte-swapped the
2303 * incoming request message body, but I am responsible for the
2304 * message buffers. */
2306 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2308 if (req->rq_export == NULL) {
2309 struct ldlm_request *dlm_req;
2311 CERROR("%s from %s arrived at %lu with bad export cookie "
2313 ll_opcode2str(lustre_msg_get_opc(req->rq_reqmsg)),
2314 libcfs_nid2str(req->rq_peer.nid),
2315 req->rq_arrival_time.tv_sec,
2316 lustre_msg_get_handle(req->rq_reqmsg)->cookie);
2318 if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_CANCEL) {
2319 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2320 dlm_req = req_capsule_client_get(&req->rq_pill,
2322 if (dlm_req != NULL)
2323 ldlm_lock_dump_handle(D_ERROR,
2324 &dlm_req->lock_handle[0]);
2326 ldlm_callback_reply(req, -ENOTCONN);
2330 switch (lustre_msg_get_opc(req->rq_reqmsg)) {
2332 /* XXX FIXME move this back to mds/handler.c, bug 249 */
2334 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2335 CDEBUG(D_INODE, "cancel\n");
2336 if (CFS_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_NET) ||
2337 CFS_FAIL_CHECK(OBD_FAIL_PTLRPC_CANCEL_RESEND))
2339 rc = ldlm_handle_cancel(req);
2343 case OBD_LOG_CANCEL:
2344 req_capsule_set(&req->rq_pill, &RQF_LOG_CANCEL);
2345 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_NET))
2347 rc = llog_origin_handle_cancel(req);
2348 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LOG_CANCEL_REP))
2350 ldlm_callback_reply(req, rc);
2353 CERROR("invalid opcode %d\n",
2354 lustre_msg_get_opc(req->rq_reqmsg));
2355 req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
2356 ldlm_callback_reply(req, -EINVAL);
2362 static int ldlm_cancel_hpreq_lock_match(struct ptlrpc_request *req,
2363 struct ldlm_lock *lock)
2365 struct ldlm_request *dlm_req;
2366 struct lustre_handle lockh;
2371 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2372 if (dlm_req == NULL)
2375 ldlm_lock2handle(lock, &lockh);
2376 for (i = 0; i < dlm_req->lock_count; i++) {
2377 if (lustre_handle_equal(&dlm_req->lock_handle[i],
2379 DEBUG_REQ(D_RPCTRACE, req,
2380 "Prio raised by lock "LPX64".", lockh.cookie);
2391 static int ldlm_cancel_hpreq_check(struct ptlrpc_request *req)
2393 struct ldlm_request *dlm_req;
2398 /* no prolong in recovery */
2399 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
2402 dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
2403 if (dlm_req == NULL)
2406 for (i = 0; i < dlm_req->lock_count; i++) {
2407 struct ldlm_lock *lock;
2409 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
2413 rc = !!(lock->l_flags & LDLM_FL_AST_SENT);
2415 LDLM_DEBUG(lock, "hpreq cancel lock");
2416 LDLM_LOCK_PUT(lock);
2425 static struct ptlrpc_hpreq_ops ldlm_cancel_hpreq_ops = {
2426 .hpreq_lock_match = ldlm_cancel_hpreq_lock_match,
2427 .hpreq_check = ldlm_cancel_hpreq_check,
2431 static int ldlm_hpreq_handler(struct ptlrpc_request *req)
2435 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
2437 if (req->rq_export == NULL)
2440 if (LDLM_CANCEL == lustre_msg_get_opc(req->rq_reqmsg)) {
2441 req_capsule_set(&req->rq_pill, &RQF_LDLM_CANCEL);
2442 req->rq_ops = &ldlm_cancel_hpreq_ops;
2447 int ldlm_revoke_lock_cb(cfs_hash_t *hs, cfs_hash_bd_t *bd,
2448 cfs_hlist_node_t *hnode, void *data)
2451 cfs_list_t *rpc_list = data;
2452 struct ldlm_lock *lock = cfs_hash_object(hs, hnode);
2454 lock_res_and_lock(lock);
2456 if (lock->l_req_mode != lock->l_granted_mode) {
2457 unlock_res_and_lock(lock);
2461 LASSERT(lock->l_resource);
2462 if (lock->l_resource->lr_type != LDLM_IBITS &&
2463 lock->l_resource->lr_type != LDLM_PLAIN) {
2464 unlock_res_and_lock(lock);
2468 if (lock->l_flags & LDLM_FL_AST_SENT) {
2469 unlock_res_and_lock(lock);
2473 LASSERT(lock->l_blocking_ast);
2474 LASSERT(!lock->l_blocking_lock);
2476 lock->l_flags |= LDLM_FL_AST_SENT;
2477 if (lock->l_export && lock->l_export->exp_lock_hash) {
2478 /* NB: it's safe to call cfs_hash_del() even lock isn't
2479 * in exp_lock_hash. */
2480 /* In the function below, .hs_keycmp resolves to
2481 * ldlm_export_lock_keycmp() */
2482 /* coverity[overrun-buffer-val] */
2483 cfs_hash_del(lock->l_export->exp_lock_hash,
2484 &lock->l_remote_handle, &lock->l_exp_hash);
2487 cfs_list_add_tail(&lock->l_rk_ast, rpc_list);
2488 LDLM_LOCK_GET(lock);
2490 unlock_res_and_lock(lock);
2494 void ldlm_revoke_export_locks(struct obd_export *exp)
2496 cfs_list_t rpc_list;
2499 CFS_INIT_LIST_HEAD(&rpc_list);
2500 cfs_hash_for_each_empty(exp->exp_lock_hash,
2501 ldlm_revoke_lock_cb, &rpc_list);
2502 ldlm_run_ast_work(exp->exp_obd->obd_namespace, &rpc_list,
2503 LDLM_WORK_REVOKE_AST);
2507 EXPORT_SYMBOL(ldlm_revoke_export_locks);
2508 #endif /* HAVE_SERVER_SUPPORT */
2511 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
2513 struct ldlm_bl_work_item *blwi = NULL;
2514 static unsigned int num_bl = 0;
2516 spin_lock(&blp->blp_lock);
2517 /* process a request from the blp_list at least every blp_num_threads */
2518 if (!cfs_list_empty(&blp->blp_list) &&
2519 (cfs_list_empty(&blp->blp_prio_list) || num_bl == 0))
2520 blwi = cfs_list_entry(blp->blp_list.next,
2521 struct ldlm_bl_work_item, blwi_entry);
2523 if (!cfs_list_empty(&blp->blp_prio_list))
2524 blwi = cfs_list_entry(blp->blp_prio_list.next,
2525 struct ldlm_bl_work_item,
2529 if (++num_bl >= cfs_atomic_read(&blp->blp_num_threads))
2531 cfs_list_del(&blwi->blwi_entry);
2533 spin_unlock(&blp->blp_lock);
2538 /* This only contains temporary data until the thread starts */
2539 struct ldlm_bl_thread_data {
2540 char bltd_name[CFS_CURPROC_COMM_MAX];
2541 struct ldlm_bl_pool *bltd_blp;
2542 struct completion bltd_comp;
2546 static int ldlm_bl_thread_main(void *arg);
2548 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
2550 struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
2553 init_completion(&bltd.bltd_comp);
2554 rc = cfs_create_thread(ldlm_bl_thread_main, &bltd, 0);
2556 CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
2557 cfs_atomic_read(&blp->blp_num_threads), rc);
2560 wait_for_completion(&bltd.bltd_comp);
2566 * Main blocking requests processing thread.
2568 * Callers put locks into its queue by calling ldlm_bl_to_thread.
2569 * This thread in the end ends up doing actual call to ->l_blocking_ast
2572 static int ldlm_bl_thread_main(void *arg)
2574 struct ldlm_bl_pool *blp;
2578 struct ldlm_bl_thread_data *bltd = arg;
2580 blp = bltd->bltd_blp;
2583 cfs_atomic_inc_return(&blp->blp_num_threads) - 1;
2584 cfs_atomic_inc(&blp->blp_busy_threads);
2586 snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
2587 "ldlm_bl_%02d", bltd->bltd_num);
2588 cfs_daemonize(bltd->bltd_name);
2590 complete(&bltd->bltd_comp);
2591 /* cannot use bltd after this, it is only on caller's stack */
2595 struct l_wait_info lwi = { 0 };
2596 struct ldlm_bl_work_item *blwi = NULL;
2599 blwi = ldlm_bl_get_work(blp);
2602 cfs_atomic_dec(&blp->blp_busy_threads);
2603 l_wait_event_exclusive(blp->blp_waitq,
2604 (blwi = ldlm_bl_get_work(blp)) != NULL,
2606 busy = cfs_atomic_inc_return(&blp->blp_busy_threads);
2608 busy = cfs_atomic_read(&blp->blp_busy_threads);
2611 if (blwi->blwi_ns == NULL)
2612 /* added by ldlm_cleanup() */
2615 /* Not fatal if racy and have a few too many threads */
2616 if (unlikely(busy < blp->blp_max_threads &&
2617 busy >= cfs_atomic_read(&blp->blp_num_threads) &&
2618 !blwi->blwi_mem_pressure))
2619 /* discard the return value, we tried */
2620 ldlm_bl_thread_start(blp);
2622 if (blwi->blwi_mem_pressure)
2623 cfs_memory_pressure_set();
2625 if (blwi->blwi_count) {
2627 /* The special case when we cancel locks in LRU
2628 * asynchronously, we pass the list of locks here.
2629 * Thus locks are marked LDLM_FL_CANCELING, but NOT
2630 * canceled locally yet. */
2631 count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
2634 ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL, 0);
2636 ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
2639 if (blwi->blwi_mem_pressure)
2640 cfs_memory_pressure_clr();
2642 if (blwi->blwi_mode == LDLM_ASYNC)
2643 OBD_FREE(blwi, sizeof(*blwi));
2645 complete(&blwi->blwi_comp);
2648 cfs_atomic_dec(&blp->blp_busy_threads);
2649 cfs_atomic_dec(&blp->blp_num_threads);
2650 complete(&blp->blp_comp);
2656 static int ldlm_setup(void);
2657 static int ldlm_cleanup(void);
2659 int ldlm_get_ref(void)
2663 mutex_lock(&ldlm_ref_mutex);
2664 if (++ldlm_refcount == 1) {
2669 mutex_unlock(&ldlm_ref_mutex);
2673 EXPORT_SYMBOL(ldlm_get_ref);
2675 void ldlm_put_ref(void)
2678 mutex_lock(&ldlm_ref_mutex);
2679 if (ldlm_refcount == 1) {
2680 int rc = ldlm_cleanup();
2682 CERROR("ldlm_cleanup failed: %d\n", rc);
2688 mutex_unlock(&ldlm_ref_mutex);
2692 EXPORT_SYMBOL(ldlm_put_ref);
2695 * Export handle<->lock hash operations.
2698 ldlm_export_lock_hash(cfs_hash_t *hs, const void *key, unsigned mask)
2700 return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
2704 ldlm_export_lock_key(cfs_hlist_node_t *hnode)
2706 struct ldlm_lock *lock;
2708 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2709 return &lock->l_remote_handle;
2713 ldlm_export_lock_keycpy(cfs_hlist_node_t *hnode, void *key)
2715 struct ldlm_lock *lock;
2717 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2718 lock->l_remote_handle = *(struct lustre_handle *)key;
2722 ldlm_export_lock_keycmp(const void *key, cfs_hlist_node_t *hnode)
2724 return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
2728 ldlm_export_lock_object(cfs_hlist_node_t *hnode)
2730 return cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2734 ldlm_export_lock_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2736 struct ldlm_lock *lock;
2738 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2739 LDLM_LOCK_GET(lock);
2743 ldlm_export_lock_put(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
2745 struct ldlm_lock *lock;
2747 lock = cfs_hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
2748 LDLM_LOCK_RELEASE(lock);
2751 static cfs_hash_ops_t ldlm_export_lock_ops = {
2752 .hs_hash = ldlm_export_lock_hash,
2753 .hs_key = ldlm_export_lock_key,
2754 .hs_keycmp = ldlm_export_lock_keycmp,
2755 .hs_keycpy = ldlm_export_lock_keycpy,
2756 .hs_object = ldlm_export_lock_object,
2757 .hs_get = ldlm_export_lock_get,
2758 .hs_put = ldlm_export_lock_put,
2759 .hs_put_locked = ldlm_export_lock_put,
2762 int ldlm_init_export(struct obd_export *exp)
2766 exp->exp_lock_hash =
2767 cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
2768 HASH_EXP_LOCK_CUR_BITS,
2769 HASH_EXP_LOCK_MAX_BITS,
2770 HASH_EXP_LOCK_BKT_BITS, 0,
2771 CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
2772 &ldlm_export_lock_ops,
2773 CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
2774 CFS_HASH_NBLK_CHANGE);
2776 if (!exp->exp_lock_hash)
2781 EXPORT_SYMBOL(ldlm_init_export);
2783 void ldlm_destroy_export(struct obd_export *exp)
2786 cfs_hash_putref(exp->exp_lock_hash);
2787 exp->exp_lock_hash = NULL;
2789 ldlm_destroy_flock_export(exp);
2792 EXPORT_SYMBOL(ldlm_destroy_export);
2794 static int ldlm_setup(void)
2796 static struct ptlrpc_service_conf conf;
2797 struct ldlm_bl_pool *blp = NULL;
2804 if (ldlm_state != NULL)
2807 OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
2808 if (ldlm_state == NULL)
2812 rc = ldlm_proc_setup();
2817 memset(&conf, 0, sizeof(conf));
2818 conf = (typeof(conf)) {
2819 .psc_name = "ldlm_cbd",
2820 .psc_watchdog_factor = 2,
2822 .bc_nbufs = LDLM_NBUFS,
2823 .bc_buf_size = LDLM_BUFSIZE,
2824 .bc_req_max_size = LDLM_MAXREQSIZE,
2825 .bc_rep_max_size = LDLM_MAXREPSIZE,
2826 .bc_req_portal = LDLM_CB_REQUEST_PORTAL,
2827 .bc_rep_portal = LDLM_CB_REPLY_PORTAL,
2830 .tc_thr_name = "ldlm_cb",
2831 .tc_thr_factor = LDLM_THR_FACTOR,
2832 .tc_nthrs_init = LDLM_NTHRS_INIT,
2833 .tc_nthrs_base = LDLM_NTHRS_BASE,
2834 .tc_nthrs_max = LDLM_NTHRS_MAX,
2835 .tc_nthrs_user = ldlm_num_threads,
2836 .tc_cpu_affinity = 1,
2837 .tc_ctx_tags = LCT_MD_THREAD | LCT_DT_THREAD,
2840 .cc_pattern = ldlm_cpts,
2843 .so_req_handler = ldlm_callback_handler,
2846 ldlm_state->ldlm_cb_service = \
2847 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2848 if (IS_ERR(ldlm_state->ldlm_cb_service)) {
2849 CERROR("failed to start service\n");
2850 rc = PTR_ERR(ldlm_state->ldlm_cb_service);
2851 ldlm_state->ldlm_cb_service = NULL;
2855 #ifdef HAVE_SERVER_SUPPORT
2856 memset(&conf, 0, sizeof(conf));
2857 conf = (typeof(conf)) {
2858 .psc_name = "ldlm_canceld",
2859 .psc_watchdog_factor = 6,
2861 .bc_nbufs = LDLM_NBUFS,
2862 .bc_buf_size = LDLM_BUFSIZE,
2863 .bc_req_max_size = LDLM_MAXREQSIZE,
2864 .bc_rep_max_size = LDLM_MAXREPSIZE,
2865 .bc_req_portal = LDLM_CANCEL_REQUEST_PORTAL,
2866 .bc_rep_portal = LDLM_CANCEL_REPLY_PORTAL,
2870 .tc_thr_name = "ldlm_cn",
2871 .tc_thr_factor = LDLM_THR_FACTOR,
2872 .tc_nthrs_init = LDLM_NTHRS_INIT,
2873 .tc_nthrs_base = LDLM_NTHRS_BASE,
2874 .tc_nthrs_max = LDLM_NTHRS_MAX,
2875 .tc_nthrs_user = ldlm_num_threads,
2876 .tc_cpu_affinity = 1,
2877 .tc_ctx_tags = LCT_MD_THREAD | \
2882 .cc_pattern = ldlm_cpts,
2885 .so_req_handler = ldlm_cancel_handler,
2886 .so_hpreq_handler = ldlm_hpreq_handler,
2889 ldlm_state->ldlm_cancel_service = \
2890 ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
2891 if (IS_ERR(ldlm_state->ldlm_cancel_service)) {
2892 CERROR("failed to start service\n");
2893 rc = PTR_ERR(ldlm_state->ldlm_cancel_service);
2894 ldlm_state->ldlm_cancel_service = NULL;
2899 OBD_ALLOC(blp, sizeof(*blp));
2901 GOTO(out, rc = -ENOMEM);
2902 ldlm_state->ldlm_bl_pool = blp;
2904 spin_lock_init(&blp->blp_lock);
2905 CFS_INIT_LIST_HEAD(&blp->blp_list);
2906 CFS_INIT_LIST_HEAD(&blp->blp_prio_list);
2907 cfs_waitq_init(&blp->blp_waitq);
2908 cfs_atomic_set(&blp->blp_num_threads, 0);
2909 cfs_atomic_set(&blp->blp_busy_threads, 0);
2912 if (ldlm_num_threads == 0) {
2913 blp->blp_min_threads = LDLM_NTHRS_INIT;
2914 blp->blp_max_threads = LDLM_NTHRS_MAX;
2916 blp->blp_min_threads = blp->blp_max_threads = \
2917 min_t(int, LDLM_NTHRS_MAX, max_t(int, LDLM_NTHRS_INIT,
2921 for (i = 0; i < blp->blp_min_threads; i++) {
2922 rc = ldlm_bl_thread_start(blp);
2927 # ifdef HAVE_SERVER_SUPPORT
2928 CFS_INIT_LIST_HEAD(&expired_lock_thread.elt_expired_locks);
2929 expired_lock_thread.elt_state = ELT_STOPPED;
2930 cfs_waitq_init(&expired_lock_thread.elt_waitq);
2932 CFS_INIT_LIST_HEAD(&waiting_locks_list);
2933 spin_lock_init(&waiting_locks_spinlock);
2934 cfs_timer_init(&waiting_locks_timer, waiting_locks_callback, 0);
2936 rc = cfs_create_thread(expired_lock_main, NULL, CFS_DAEMON_FLAGS);
2938 CERROR("Cannot start ldlm expired-lock thread: %d\n", rc);
2942 cfs_wait_event(expired_lock_thread.elt_waitq,
2943 expired_lock_thread.elt_state == ELT_READY);
2944 # endif /* HAVE_SERVER_SUPPORT */
2946 rc = ldlm_pools_init();
2948 CERROR("Failed to initialize LDLM pools: %d\n", rc);
2959 static int ldlm_cleanup(void)
2963 if (!cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
2964 !cfs_list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
2965 CERROR("ldlm still has namespaces; clean these up first.\n");
2966 ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
2967 ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
2974 if (ldlm_state->ldlm_bl_pool != NULL) {
2975 struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
2977 while (cfs_atomic_read(&blp->blp_num_threads) > 0) {
2978 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
2980 init_completion(&blp->blp_comp);
2982 spin_lock(&blp->blp_lock);
2983 cfs_list_add_tail(&blwi.blwi_entry, &blp->blp_list);
2984 cfs_waitq_signal(&blp->blp_waitq);
2985 spin_unlock(&blp->blp_lock);
2987 wait_for_completion(&blp->blp_comp);
2990 OBD_FREE(blp, sizeof(*blp));
2992 #endif /* __KERNEL__ */
2994 if (ldlm_state->ldlm_cb_service != NULL)
2995 ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
2996 # ifdef HAVE_SERVER_SUPPORT
2997 if (ldlm_state->ldlm_cancel_service != NULL)
2998 ptlrpc_unregister_service(ldlm_state->ldlm_cancel_service);
3002 ldlm_proc_cleanup();
3004 # ifdef HAVE_SERVER_SUPPORT
3005 if (expired_lock_thread.elt_state != ELT_STOPPED) {
3006 expired_lock_thread.elt_state = ELT_TERMINATE;
3007 cfs_waitq_signal(&expired_lock_thread.elt_waitq);
3008 cfs_wait_event(expired_lock_thread.elt_waitq,
3009 expired_lock_thread.elt_state == ELT_STOPPED);
3012 #endif /* __KERNEL__ */
3014 OBD_FREE(ldlm_state, sizeof(*ldlm_state));
3022 mutex_init(&ldlm_ref_mutex);
3023 mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
3024 mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
3025 ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
3026 sizeof(struct ldlm_resource), 0,
3027 CFS_SLAB_HWCACHE_ALIGN);
3028 if (ldlm_resource_slab == NULL)
3031 ldlm_lock_slab = cfs_mem_cache_create("ldlm_locks",
3032 sizeof(struct ldlm_lock), 0,
3033 CFS_SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU);
3034 if (ldlm_lock_slab == NULL) {
3035 cfs_mem_cache_destroy(ldlm_resource_slab);
3039 ldlm_interval_slab = cfs_mem_cache_create("interval_node",
3040 sizeof(struct ldlm_interval),
3041 0, CFS_SLAB_HWCACHE_ALIGN);
3042 if (ldlm_interval_slab == NULL) {
3043 cfs_mem_cache_destroy(ldlm_resource_slab);
3044 cfs_mem_cache_destroy(ldlm_lock_slab);
3047 #if LUSTRE_TRACKS_LOCK_EXP_REFS
3048 class_export_dump_hook = ldlm_dump_export_locks;
3053 void ldlm_exit(void)
3057 CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
3058 rc = cfs_mem_cache_destroy(ldlm_resource_slab);
3059 LASSERTF(rc == 0, "couldn't free ldlm resource slab\n");
3061 /* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
3062 * synchronize_rcu() to wait a grace period elapsed, so that
3063 * ldlm_lock_free() get a chance to be called. */
3066 rc = cfs_mem_cache_destroy(ldlm_lock_slab);
3067 LASSERTF(rc == 0, "couldn't free ldlm lock slab\n");
3068 rc = cfs_mem_cache_destroy(ldlm_interval_slab);
3069 LASSERTF(rc == 0, "couldn't free interval node slab\n");