Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / ldlm / ldlm_lockd.c
index af90be5..ef065d5 100644 (file)
 #include <libcfs/list.h>
 #include "ldlm_internal.h"
 
+#ifdef __KERNEL__
+static int ldlm_num_threads;
+CFS_MODULE_PARM(ldlm_num_threads, "i", int, 0444,
+                "number of DLM service threads to start");
+#endif
+
 extern cfs_mem_cache_t *ldlm_resource_slab;
 extern cfs_mem_cache_t *ldlm_lock_slab;
-extern struct list_head ldlm_namespace_list;
-
-extern struct semaphore ldlm_namespace_lock;
 static struct semaphore ldlm_ref_sem;
 static int ldlm_refcount;
 
@@ -88,8 +91,11 @@ struct ldlm_bl_pool {
         spinlock_t              blp_lock;
         struct list_head        blp_list;
         cfs_waitq_t             blp_waitq;
-        atomic_t                blp_num_threads;
         struct completion       blp_comp;
+        atomic_t                blp_num_threads;
+        atomic_t                blp_busy_threads;
+        int                     blp_min_threads;
+        int                     blp_max_threads;
 };
 
 struct ldlm_bl_work_item {
@@ -97,7 +103,8 @@ struct ldlm_bl_work_item {
         struct ldlm_namespace   *blwi_ns;
         struct ldlm_lock_desc   blwi_ld;
         struct ldlm_lock        *blwi_lock;
-        int                     blwi_flags;
+        struct list_head        blwi_head;
+        int                     blwi_count;
 };
 
 #ifdef __KERNEL__
@@ -438,11 +445,11 @@ static void ldlm_failed_ast(struct ldlm_lock *lock, int rc,
         char                     *str = libcfs_nid2str(conn->c_peer.nid);
 
         LCONSOLE_ERROR_MSG(0x138, "A client on nid %s was evicted from "
-                           "service %s.\n", str, 
+                           "service %s.\n", str,
                            lock->l_export->exp_obd->obd_name);
 
         LCONSOLE_ERROR_MSG(0x012, "Lock %s callback to %s timed out for "
-                           "resource %d\n", ast_type, 
+                           "resource %d\n", ast_type,
                            obd_export_nid2str(lock->l_export), rc);
 
         if (obd_dump_on_timeout)
@@ -497,15 +504,77 @@ static int ldlm_handle_ast_error(struct ldlm_lock *lock,
         return rc;
 }
 
+static int ldlm_cb_interpret(struct ptlrpc_request *req, void *data, int rc)
+{
+        struct ldlm_cb_set_arg *arg;
+        struct ldlm_lock *lock;
+        ENTRY;
+
+        LASSERT(data != NULL);
+
+        arg = req->rq_async_args.pointer_arg[0];
+        lock = req->rq_async_args.pointer_arg[1];
+        LASSERT(lock != NULL);
+        if (rc != 0) {
+                /* If client canceled the lock but the cancel has not 
+                 * been recieved yet, we need to update lvbo to have the
+                 * proper attributes cached. */
+                if (rc == -EINVAL && arg->type == LDLM_BL_CALLBACK)
+                        ldlm_res_lvbo_update(lock->l_resource, NULL, 
+                                             0, 1);
+                rc = ldlm_handle_ast_error(lock, req, rc, 
+                                           arg->type == LDLM_BL_CALLBACK
+                                           ? "blocking" : "completion");
+        }                
+
+        LDLM_LOCK_PUT(lock);
+
+        if (rc == -ERESTART)
+                atomic_set(&arg->restart, 1);
+
+        RETURN(0);
+}
+
+static inline int ldlm_bl_and_cp_ast_fini(struct ptlrpc_request *req,
+                                          struct ldlm_cb_set_arg *arg,
+                                          struct ldlm_lock *lock,
+                                          int instant_cancel)
+{
+        int rc = 0;
+        ENTRY;
+
+        if (unlikely(instant_cancel)) {
+                rc = ptl_send_rpc(req, 1);
+                ptlrpc_req_finished(req);
+                if (rc == 0)
+                        /* If we cancelled the lock, we need to restart
+                         * ldlm_reprocess_queue */
+                        atomic_set(&arg->restart, 1);
+        } else {
+                LDLM_LOCK_GET(lock);
+                ptlrpc_set_add_req(arg->set, req);
+        }       
+
+        RETURN(rc);
+}
+
+/*
+ * ->l_blocking_ast() method for server-side locks. This is invoked when newly
+ * enqueued server lock conflicts with given one.
+ *
+ * Sends blocking ast rpc to the client owning that lock; arms timeout timer
+ * to wait for client response.
+ */
 int ldlm_server_blocking_ast(struct ldlm_lock *lock,
                              struct ldlm_lock_desc *desc,
                              void *data, int flag)
 {
+        struct ldlm_cb_set_arg *arg = data;
         struct ldlm_request *body;
         struct ptlrpc_request *req;
         int size[] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
                        [DLM_LOCKREQ_OFF]     = sizeof(*body) };
-        int instant_cancel = 0, rc = 0;
+        int instant_cancel = 0, rc;
         ENTRY;
 
         if (flag == LDLM_CB_CANCELING) {
@@ -514,6 +583,7 @@ int ldlm_server_blocking_ast(struct ldlm_lock *lock,
         }
 
         LASSERT(lock);
+        LASSERT(data != NULL);
         if (lock->l_export->exp_obd->obd_recovering != 0) {
                 LDLM_ERROR(lock, "BUG 6063: lock collide during recovery");
                 ldlm_lock_dump(D_ERROR, lock, 0);
@@ -525,6 +595,11 @@ int ldlm_server_blocking_ast(struct ldlm_lock *lock,
         if (req == NULL)
                 RETURN(-ENOMEM);
 
+        req->rq_async_args.pointer_arg[0] = arg;
+        req->rq_async_args.pointer_arg[1] = lock;
+        req->rq_interpret_reply = ldlm_cb_interpret;
+        req->rq_no_resend = 1;
+
         lock_res(lock->l_resource);
         if (lock->l_granted_mode != lock->l_req_mode) {
                 /* this blocking AST will be communicated as part of the
@@ -556,7 +631,8 @@ int ldlm_server_blocking_ast(struct ldlm_lock *lock,
         if (instant_cancel) {
                 unlock_res(lock->l_resource);
                 ldlm_lock_cancel(lock);
-        } else if (lock->l_granted_mode == lock->l_req_mode) {
+        } else {
+                LASSERT(lock->l_granted_mode == lock->l_req_mode);
                 ldlm_add_waiting_lock(lock);
                 unlock_res(lock->l_resource);
         }
@@ -568,37 +644,25 @@ int ldlm_server_blocking_ast(struct ldlm_lock *lock,
                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
                                      LDLM_BL_CALLBACK - LDLM_FIRST_OPC);
 
-        if (unlikely(instant_cancel)) {
-                rc = ptl_send_rpc(req, 1);
-        } else {
-                rc = ptlrpc_queue_wait(req);
-                OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
-        }
-
-        if (rc != 0)
-                rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
-
-        ptlrpc_req_finished(req);
-
-        /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
-        if (!rc && instant_cancel)
-                rc = -ERESTART;
+        rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
 
         RETURN(rc);
 }
 
 int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
 {
+        struct ldlm_cb_set_arg *arg = data;
         struct ldlm_request *body;
         struct ptlrpc_request *req;
         struct timeval granted_time;
         long total_enqueue_wait;
         int size[3] = { [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
                         [DLM_LOCKREQ_OFF]     = sizeof(*body) };
-        int rc = 0, buffers = 2, instant_cancel = 0;
+        int rc, buffers = 2, instant_cancel = 0;
         ENTRY;
 
         LASSERT(lock != NULL);
+        LASSERT(data != NULL);
 
         do_gettimeofday(&granted_time);
         total_enqueue_wait = cfs_timeval_sub(&granted_time,
@@ -621,6 +685,11 @@ int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
         if (req == NULL)
                 RETURN(-ENOMEM);
 
+        req->rq_async_args.pointer_arg[0] = arg;
+        req->rq_async_args.pointer_arg[1] = lock;
+        req->rq_interpret_reply = ldlm_cb_interpret;
+        req->rq_no_resend = 1;
+
         body = lustre_msg_buf(req->rq_reqmsg, DLM_LOCKREQ_OFF, sizeof(*body));
         body->lock_handle[0] = lock->l_remote_handle;
         body->lock_flags = flags;
@@ -639,7 +708,7 @@ int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
 
         LDLM_DEBUG(lock, "server preparing completion AST (after %ldus wait)",
                    total_enqueue_wait);
-        
+
         ptlrpc_req_set_repsize(req, 1, NULL);
 
         req->rq_send_state = LUSTRE_IMP_FULL;
@@ -672,15 +741,7 @@ int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags, void *data)
                 lprocfs_counter_incr(lock->l_export->exp_ldlm_stats,
                                      LDLM_CP_CALLBACK - LDLM_FIRST_OPC);
 
-        rc = ptlrpc_queue_wait(req);
-        if (rc != 0)
-                rc = ldlm_handle_ast_error(lock, req, rc, "completion");
-
-        ptlrpc_req_finished(req);
-
-        /* If we cancelled the lock, we need to restart ldlm_reprocess_queue */
-        if (!rc && instant_cancel)
-                rc = -ERESTART;
+        rc = ldlm_bl_and_cp_ast_fini(req, arg, lock, instant_cancel);
 
         RETURN(rc);
 }
@@ -726,8 +787,8 @@ int ldlm_server_glimpse_ast(struct ldlm_lock *lock, void *data)
         else if (rc != 0)
                 rc = ldlm_handle_ast_error(lock, req, rc, "glimpse");
         else
-                rc = res->lr_namespace->ns_lvbo->lvbo_update
-                        (res, req->rq_repmsg, REPLY_REC_OFF, 1);
+                rc = ldlm_res_lvbo_update(res, req->rq_repmsg,
+                                          REPLY_REC_OFF, 1);
         ptlrpc_req_finished(req);
         RETURN(rc);
 }
@@ -839,8 +900,8 @@ int ldlm_handle_enqueue0(struct ldlm_namespace *ns,
                 lock = find_existing_lock(req->rq_export,
                                           &dlm_req->lock_handle[0]);
                 if (lock != NULL) {
-                        DEBUG_REQ(D_HA, req, "found existing lock cookie "LPX64,
-                                  lock->l_handle.h_cookie);
+                        DEBUG_REQ(D_DLMTRACE, req, "found existing lock cookie "
+                                  LPX64, lock->l_handle.h_cookie);
                         GOTO(existing_lock, rc = 0);
                 }
         }
@@ -941,7 +1002,7 @@ existing_lock:
                                 unlock_res_and_lock(lock);
                                 ldlm_lock_cancel(lock);
                                 lock_res_and_lock(lock);
-                        } else if (lock->l_granted_mode == lock->l_req_mode)
+                        } else
                                 ldlm_add_waiting_lock(lock);
                 }
         }
@@ -988,8 +1049,8 @@ existing_lock:
                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
                            "(err=%d, rc=%d)", err, rc);
 
+                lock_res_and_lock(lock);
                 if (rc == 0) {
-                        lock_res_and_lock(lock);
                         size[DLM_REPLY_REC_OFF] = lock->l_resource->lr_lvb_len;
                         if (size[DLM_REPLY_REC_OFF] > 0) {
                                 void *lvb = lustre_msg_buf(req->rq_repmsg,
@@ -1001,13 +1062,11 @@ existing_lock:
                                 memcpy(lvb, lock->l_resource->lr_lvb_data,
                                        size[DLM_REPLY_REC_OFF]);
                         }
-                        unlock_res_and_lock(lock);
                 } else {
-                        lock_res_and_lock(lock);
                         ldlm_resource_unlink_lock(lock);
                         ldlm_lock_destroy_nolock(lock);
-                        unlock_res_and_lock(lock);
                 }
+                unlock_res_and_lock(lock);
 
                 if (!err && dlm_req->lock_desc.l_resource.lr_type != LDLM_FLOCK)
                         ldlm_reprocess_all(lock->l_resource);
@@ -1062,10 +1121,9 @@ int ldlm_handle_convert0(struct ptlrpc_request *req,
                                      LDLM_CONVERT - LDLM_FIRST_OPC);
 
         rc = lustre_pack_reply(req, 2, size, NULL);
-        if (rc) {
-                CERROR("out of memory\n");
-                RETURN(-ENOMEM);
-        }
+        if (rc)
+                RETURN(rc);
+
         dlm_rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF,
                                  sizeof(*dlm_rep));
         dlm_rep->lock_flags = dlm_req->lock_flags;
@@ -1135,8 +1193,9 @@ int ldlm_request_cancel(struct ptlrpc_request *req,
         if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY)
                 RETURN(0);
 
-        LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks",
-                          count - first);
+        LDLM_DEBUG_NOLOCK("server-side cancel handler START: %d locks, "
+                          "starting at %d", count, first);
+
         for (i = first; i < count; i++) {
                 lock = ldlm_handle2lock(&dlm_req->lock_handle[i]);
                 if (!lock) {
@@ -1148,32 +1207,22 @@ int ldlm_request_cancel(struct ptlrpc_request *req,
 
                 res = lock->l_resource;
                 done++;
-                ldlm_lock_cancel(lock);
-                if (ldlm_del_waiting_lock(lock))
-                        CDEBUG(D_DLMTRACE, "cancelled waiting lock %p\n", lock);
 
                 if (res != pres) {
                         if (pres != NULL) {
-                                if (pres->lr_namespace->ns_lvbo &&
-                                    pres->lr_namespace->ns_lvbo->lvbo_update) {
-                                        (void)pres->lr_namespace->ns_lvbo->
-                                                lvbo_update(pres, NULL, 0, 1);
-                                }
                                 ldlm_reprocess_all(pres);
                                 ldlm_resource_putref(pres);
                         }
-                        if (res != NULL)
+                        if (res != NULL) {
                                 ldlm_resource_getref(res);
+                                ldlm_res_lvbo_update(res, NULL, 0, 1);
+                        }
                         pres = res;
                 }
+                ldlm_lock_cancel(lock);
                 LDLM_LOCK_PUT(lock);
         }
         if (pres != NULL) {
-                if (pres->lr_namespace->ns_lvbo &&
-                    pres->lr_namespace->ns_lvbo->lvbo_update) {
-                        (void)pres->lr_namespace->ns_lvbo->
-                                lvbo_update(pres, NULL, 0, 1);
-                }
                 ldlm_reprocess_all(pres);
                 ldlm_resource_putref(pres);
         }
@@ -1199,11 +1248,9 @@ int ldlm_handle_cancel(struct ptlrpc_request *req)
                                      LDLM_CANCEL - LDLM_FIRST_OPC);
 
         rc = lustre_pack_reply(req, 1, NULL, NULL);
-        if (rc) {
-                CERROR("out of memory\n");
-                RETURN(-ENOMEM);
-        }
-        
+        if (rc)
+                RETURN(rc);
+
         if (!ldlm_request_cancel(req, dlm_req, 0))
                 req->rq_status = ESTALE;
 
@@ -1339,7 +1386,7 @@ static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
                            cfs_time_add(lock->l_last_used,
                                         cfs_time_seconds(10)))) {
                 unlock_res_and_lock(lock);
-                if (ldlm_bl_to_thread(ns, NULL, lock, 0))
+                if (ldlm_bl_to_thread_lock(ns, NULL, lock))
                         ldlm_handle_bl_callback(ns, NULL, lock);
 
                 EXIT;
@@ -1361,14 +1408,18 @@ static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
         return ptlrpc_reply(req);
 }
 
-int ldlm_bl_to_thread(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
-                      struct ldlm_lock *lock, int flags)
-{
 #ifdef __KERNEL__
+static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
+                             struct ldlm_lock_desc *ld, struct ldlm_lock *lock,
+                             struct list_head *cancels, int count)
+{
         struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
         struct ldlm_bl_work_item *blwi;
         ENTRY;
 
+        if (cancels && count == 0)
+                RETURN(0);
+
         OBD_ALLOC(blwi, sizeof(*blwi));
         if (blwi == NULL)
                 RETURN(-ENOMEM);
@@ -1376,15 +1427,37 @@ int ldlm_bl_to_thread(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
         blwi->blwi_ns = ns;
         if (ld != NULL)
                 blwi->blwi_ld = *ld;
-        blwi->blwi_lock = lock;
-        blwi->blwi_flags = flags;
-
+        if (count) {
+                list_add(&blwi->blwi_head, cancels);
+                list_del_init(cancels);
+                blwi->blwi_count = count;
+        } else {
+                blwi->blwi_lock = lock;
+        }
         spin_lock(&blp->blp_lock);
         list_add_tail(&blwi->blwi_entry, &blp->blp_list);
         cfs_waitq_signal(&blp->blp_waitq);
         spin_unlock(&blp->blp_lock);
 
         RETURN(0);
+}
+#endif
+
+int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
+                           struct ldlm_lock *lock)
+{
+#ifdef __KERNEL__
+        RETURN(ldlm_bl_to_thread(ns, ld, lock, NULL, 0));
+#else
+        RETURN(-ENOSYS);
+#endif
+}
+
+int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
+                           struct list_head *cancels, int count)
+{
+#ifdef __KERNEL__
+        RETURN(ldlm_bl_to_thread(ns, ld, NULL, cancels, count));
 #else
         RETURN(-ENOSYS);
 #endif
@@ -1472,6 +1545,9 @@ static int ldlm_callback_handler(struct ptlrpc_request *req)
                 rc = llog_origin_handle_close(req);
                 ldlm_callback_reply(req, rc);
                 RETURN(0);
+        case SEC_CTX_FINI:
+                /* do nothing */
+                RETURN(0);
         default:
                 CERROR("unknown opcode %u\n",
                        lustre_msg_get_opc(req->rq_reqmsg));
@@ -1492,8 +1568,8 @@ static int ldlm_callback_handler(struct ptlrpc_request *req)
 
         lock = ldlm_handle2lock_ns(ns, &dlm_req->lock_handle[0]);
         if (!lock) {
-                CDEBUG(D_INODE, "callback on lock "LPX64" - lock disappeared\n",
-                       dlm_req->lock_handle[0].cookie);
+                CDEBUG(D_DLMTRACE, "callback on lock "LPX64" - lock "
+                       "disappeared\n", dlm_req->lock_handle[0].cookie);
                 ldlm_callback_reply(req, -EINVAL);
                 RETURN(0);
         }
@@ -1501,6 +1577,22 @@ static int ldlm_callback_handler(struct ptlrpc_request *req)
         /* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
         lock_res_and_lock(lock);
         lock->l_flags |= (dlm_req->lock_flags & LDLM_AST_FLAGS);
+        if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
+                /* If somebody cancels locks and cache is already droped,
+                 * we can tell the server we have no lock. Otherwise, we
+                 * should send cancel after dropping the cache. */
+                if ((lock->l_flags & LDLM_FL_CANCELING) &&
+                    (lock->l_flags & LDLM_FL_BL_DONE)) {
+                        LDLM_DEBUG(lock, "callback on lock "
+                                   LPX64" - lock disappeared\n",
+                                   dlm_req->lock_handle[0].cookie);
+                        unlock_res_and_lock(lock);
+                        LDLM_LOCK_PUT(lock);
+                        ldlm_callback_reply(req, -EINVAL);
+                        RETURN(0);
+                }
+                lock->l_flags |= LDLM_FL_BL_AST;
+        }
         unlock_res_and_lock(lock);
 
         /* We want the ost thread to get this reply so that it can respond
@@ -1517,7 +1609,7 @@ static int ldlm_callback_handler(struct ptlrpc_request *req)
                 CDEBUG(D_INODE, "blocking ast\n");
                 if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK))
                         ldlm_callback_reply(req, 0);
-                if (ldlm_bl_to_thread(ns, &dlm_req->lock_desc, lock, 0))
+                if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
                         ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
                 break;
         case LDLM_CP_CALLBACK:
@@ -1663,50 +1755,91 @@ static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
         return blwi;
 }
 
+/* This only contains temporary data until the thread starts */
 struct ldlm_bl_thread_data {
-        int                     bltd_num;
+        char                    bltd_name[CFS_CURPROC_COMM_MAX];
         struct ldlm_bl_pool     *bltd_blp;
+        struct completion       bltd_comp;
+        int                     bltd_num;
 };
 
+static int ldlm_bl_thread_main(void *arg);
+
+static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
+{
+        struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
+        int rc;
+
+        init_completion(&bltd.bltd_comp);
+        rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
+        if (rc < 0) {
+                CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %d\n",
+                       atomic_read(&blp->blp_num_threads), rc);
+                return rc;
+        }
+        wait_for_completion(&bltd.bltd_comp);
+
+        return 0;
+}
+
 static int ldlm_bl_thread_main(void *arg)
 {
-        struct ldlm_bl_thread_data *bltd = arg;
-        struct ldlm_bl_pool *blp = bltd->bltd_blp;
+        struct ldlm_bl_pool *blp;
         ENTRY;
 
         {
-                char name[CFS_CURPROC_COMM_MAX];
-                snprintf(name, sizeof(name) - 1, "ldlm_bl_%02d",
-                         bltd->bltd_num);
-                cfs_daemonize(name);
-        }
+                struct ldlm_bl_thread_data *bltd = arg;
 
-        atomic_inc(&blp->blp_num_threads);
-        complete(&blp->blp_comp);
+                blp = bltd->bltd_blp;
+
+                bltd->bltd_num = atomic_inc_return(&blp->blp_num_threads) - 1;
+                atomic_inc(&blp->blp_busy_threads);
+
+                snprintf(bltd->bltd_name, sizeof(bltd->bltd_name) - 1,
+                        "ldlm_bl_%02d", bltd->bltd_num);
+                cfs_daemonize(bltd->bltd_name);
+
+                complete(&bltd->bltd_comp);
+                /* cannot use bltd after this, it is only on caller's stack */
+        }
 
-        while(1) {
+        while (1) {
                 struct l_wait_info lwi = { 0 };
                 struct ldlm_bl_work_item *blwi = NULL;
 
-                l_wait_event_exclusive(blp->blp_waitq,
-                                       (blwi = ldlm_bl_get_work(blp)) != NULL,
-                                       &lwi);
+                blwi = ldlm_bl_get_work(blp);
 
-                if (blwi->blwi_ns == NULL)
-                        break;
+                if (blwi == NULL) {
+                        int busy;
+
+                        atomic_dec(&blp->blp_busy_threads);
+                        l_wait_event_exclusive(blp->blp_waitq,
+                                         (blwi = ldlm_bl_get_work(blp)) != NULL,
+                                         &lwi);
+                        busy = atomic_inc_return(&blp->blp_busy_threads);
+
+                        if (blwi->blwi_ns == NULL)
+                                /* added by ldlm_cleanup() */
+                                break;
 
-                if (blwi->blwi_flags == LDLM_FL_CANCELING) {
+                        /* Not fatal if racy and have a few too many threads */
+                        if (unlikely(busy < blp->blp_max_threads &&
+                                    busy >= atomic_read(&blp->blp_num_threads)))
+                                /* discard the return value, we tried */
+                                ldlm_bl_thread_start(blp);
+                } else {
+                        if (blwi->blwi_ns == NULL)
+                                /* added by ldlm_cleanup() */
+                                break;
+                }
+
+                if (blwi->blwi_count) {
                         /* The special case when we cancel locks in lru
-                         * asynchronously, then we first remove the lock from
-                         * l_bl_ast explicitely in ldlm_cancel_lru before
-                         * sending it to this thread. Thus lock is marked
-                         * LDLM_FL_CANCELING, and already cancelled locally. */
-                        CFS_LIST_HEAD(head);
-                        LASSERT(list_empty(&blwi->blwi_lock->l_bl_ast));
-                        list_add(&blwi->blwi_lock->l_bl_ast, &head);
-                        ldlm_cli_cancel_req(blwi->blwi_lock->l_conn_export,
-                                            &head, 1, 0);
-                        LDLM_LOCK_PUT(blwi->blwi_lock);
+                         * asynchronously, we pass the list of locks here.
+                         * Thus lock is marked LDLM_FL_CANCELING, and already
+                         * canceled locally. */
+                        ldlm_cli_cancel_list(&blwi->blwi_head,
+                                             blwi->blwi_count, NULL, 0, 0);
                 } else {
                         ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
                                                 blwi->blwi_lock);
@@ -1714,6 +1847,7 @@ static int ldlm_bl_thread_main(void *arg)
                 OBD_FREE(blwi, sizeof(*blwi));
         }
 
+        atomic_dec(&blp->blp_busy_threads);
         atomic_dec(&blp->blp_num_threads);
         complete(&blp->blp_comp);
         RETURN(0);
@@ -1761,6 +1895,8 @@ static int ldlm_setup(void)
 {
         struct ldlm_bl_pool *blp;
         int rc = 0;
+        int ldlm_min_threads = LDLM_THREADS_AUTO_MIN;
+        int ldlm_max_threads = LDLM_THREADS_AUTO_MAX;
 #ifdef __KERNEL__
         int i;
 #endif
@@ -1779,13 +1915,24 @@ static int ldlm_setup(void)
                 GOTO(out_free, rc);
 #endif
 
+#ifdef __KERNEL__
+        if (ldlm_num_threads) {
+                /* If ldlm_num_threads is set, it is the min and the max. */
+                if (ldlm_num_threads > LDLM_THREADS_AUTO_MAX)
+                        ldlm_num_threads = LDLM_THREADS_AUTO_MAX;
+                if (ldlm_num_threads < LDLM_THREADS_AUTO_MIN)
+                        ldlm_num_threads = LDLM_THREADS_AUTO_MIN;
+                ldlm_min_threads = ldlm_max_threads = ldlm_num_threads;
+        }
+#endif
+
         ldlm_state->ldlm_cb_service =
                 ptlrpc_init_svc(LDLM_NBUFS, LDLM_BUFSIZE, LDLM_MAXREQSIZE,
                                 LDLM_MAXREPSIZE, LDLM_CB_REQUEST_PORTAL,
                                 LDLM_CB_REPLY_PORTAL, ldlm_timeout * 900,
                                 ldlm_callback_handler, "ldlm_cbd",
                                 ldlm_svc_proc_dir, NULL,
-                                LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX,
+                                ldlm_min_threads, ldlm_max_threads,
                                 "ldlm_cb",
                                 LCT_MD_THREAD|LCT_DT_THREAD);
 
@@ -1800,7 +1947,7 @@ static int ldlm_setup(void)
                                 LDLM_CANCEL_REPLY_PORTAL, ldlm_timeout * 6000,
                                 ldlm_cancel_handler, "ldlm_canceld",
                                 ldlm_svc_proc_dir, NULL,
-                                LDLM_THREADS_AUTO_MIN, LDLM_THREADS_AUTO_MAX,
+                                ldlm_min_threads, ldlm_max_threads,
                                 "ldlm_cn",
                                 LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD);
 
@@ -1814,25 +1961,19 @@ static int ldlm_setup(void)
                 GOTO(out_proc, rc = -ENOMEM);
         ldlm_state->ldlm_bl_pool = blp;
 
-        atomic_set(&blp->blp_num_threads, 0);
-        cfs_waitq_init(&blp->blp_waitq);
         spin_lock_init(&blp->blp_lock);
-
         CFS_INIT_LIST_HEAD(&blp->blp_list);
+        cfs_waitq_init(&blp->blp_waitq);
+        atomic_set(&blp->blp_num_threads, 0);
+        atomic_set(&blp->blp_busy_threads, 0);
+        blp->blp_min_threads = ldlm_min_threads;
+        blp->blp_max_threads = ldlm_max_threads;
 
 #ifdef __KERNEL__
-        for (i = 0; i < LDLM_BL_THREADS; i++) {
-                struct ldlm_bl_thread_data bltd = {
-                        .bltd_num = i,
-                        .bltd_blp = blp,
-                };
-                init_completion(&blp->blp_comp);
-                rc = cfs_kernel_thread(ldlm_bl_thread_main, &bltd, 0);
-                if (rc < 0) {
-                        CERROR("cannot start LDLM thread #%d: rc %d\n", i, rc);
+        for (i = 0; i < blp->blp_min_threads; i++) {
+                rc = ldlm_bl_thread_start(blp);
+                if (rc < 0)
                         GOTO(out_thread, rc);
-                }
-                wait_for_completion(&blp->blp_comp);
         }
 
         rc = ptlrpc_start_threads(NULL, ldlm_state->ldlm_cancel_service);
@@ -1861,6 +2002,11 @@ static int ldlm_setup(void)
                    expired_lock_thread.elt_state == ELT_READY);
 #endif
 
+#ifdef __KERNEL__
+        rc = ldlm_pools_init();
+        if (rc)
+                GOTO(out_thread, rc);
+#endif
         RETURN(0);
 
 #ifdef __KERNEL__
@@ -1886,13 +2032,19 @@ static int ldlm_cleanup(int force)
 #endif
         ENTRY;
 
-        if (!list_empty(&ldlm_namespace_list)) {
+        if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) || 
+            !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
                 CERROR("ldlm still has namespaces; clean these up first.\n");
-                ldlm_dump_all_namespaces(D_DLMTRACE);
+                ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
+                ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
                 RETURN(-EBUSY);
         }
 
 #ifdef __KERNEL__
+        ldlm_pools_fini();
+#endif
+
+#ifdef __KERNEL__
         while (atomic_read(&blp->blp_num_threads) > 0) {
                 struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
 
@@ -1929,7 +2081,8 @@ static int ldlm_cleanup(int force)
 int __init ldlm_init(void)
 {
         init_mutex(&ldlm_ref_sem);
-        init_mutex(&ldlm_namespace_lock);
+        init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
+        init_mutex(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
         ldlm_resource_slab = cfs_mem_cache_create("ldlm_resources",
                                                sizeof(struct ldlm_resource), 0,
                                                SLAB_HWCACHE_ALIGN);
@@ -2050,6 +2203,7 @@ EXPORT_SYMBOL(target_cancel_recovery_timer);
 EXPORT_SYMBOL(target_send_reply);
 EXPORT_SYMBOL(target_queue_recovery_request);
 EXPORT_SYMBOL(target_handle_ping);
+EXPORT_SYMBOL(target_pack_pool_reply);
 EXPORT_SYMBOL(target_handle_disconnect);
 
 /* l_lock.c */