Whamcloud - gitweb
b=13872
[fs/lustre-release.git] / lustre / ldlm / ldlm_lockd.c
index af90be5..08b2ead 100644 (file)
@@ -42,9 +42,6 @@
 
 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;
 
@@ -97,7 +94,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 +436,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,6 +495,13 @@ static int ldlm_handle_ast_error(struct ldlm_lock *lock,
         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)
@@ -556,7 +561,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);
         }
@@ -574,9 +580,14 @@ int ldlm_server_blocking_ast(struct ldlm_lock *lock,
                 rc = ptlrpc_queue_wait(req);
                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2);
         }
-
-        if (rc != 0)
+        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)
+                        ldlm_res_lvbo_update(lock->l_resource, NULL, 0, 1);
                 rc = ldlm_handle_ast_error(lock, req, rc, "blocking");
+        }
 
         ptlrpc_req_finished(req);
 
@@ -639,7 +650,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;
@@ -726,8 +737,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 +850,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 +952,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 +999,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 +1012,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);
@@ -1135,8 +1144,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 +1158,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);
         }
@@ -1203,7 +1203,7 @@ int ldlm_handle_cancel(struct ptlrpc_request *req)
                 CERROR("out of memory\n");
                 RETURN(-ENOMEM);
         }
-        
+
         if (!ldlm_request_cancel(req, dlm_req, 0))
                 req->rq_status = ESTALE;
 
@@ -1339,7 +1339,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 +1361,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 +1380,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 +1498,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 +1521,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 +1530,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 +1562,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:
@@ -1695,18 +1740,13 @@ static int ldlm_bl_thread_main(void *arg)
                 if (blwi->blwi_ns == NULL)
                         break;
 
-                if (blwi->blwi_flags == LDLM_FL_CANCELING) {
+                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);
@@ -1861,6 +1901,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 +1931,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 +1980,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 +2102,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 */