Whamcloud - gitweb
LU-12616 obclass: fix MDS start/stop race
[fs/lustre-release.git] / lustre / ldlm / ldlm_inodebits.c
index 626c624..22daed5 100644 (file)
 #include "ldlm_internal.h"
 
 #ifdef HAVE_SERVER_SUPPORT
+
+/**
+ * It should iterate through all waiting locks on a given resource queue and
+ * attempt to grant them. An optimization is to check only heads waitintg
+ * locks for each inodebit type.
+ *
+ * Must be called with resource lock held.
+ */
+int ldlm_reprocess_inodebits_queue(struct ldlm_resource *res,
+                                  struct list_head *queue,
+                                  struct list_head *work_list,
+                                  enum ldlm_process_intention intention,
+                                  struct ldlm_lock *hint)
+{
+       __u64 flags;
+       int rc = LDLM_ITER_CONTINUE;
+       enum ldlm_error err;
+       struct list_head bl_ast_list = LIST_HEAD_INIT(bl_ast_list);
+       struct ldlm_ibits_queues *queues = res->lr_ibits_queues;
+       int i;
+
+       ENTRY;
+
+       check_res_locked(res);
+
+       LASSERT(res->lr_type == LDLM_IBITS);
+       LASSERT(intention == LDLM_PROCESS_RESCAN ||
+               intention == LDLM_PROCESS_RECOVERY);
+
+       if (intention == LDLM_PROCESS_RECOVERY)
+               return ldlm_reprocess_queue(res, queue, work_list, intention,
+                                           NULL);
+
+restart:
+       CDEBUG(D_DLMTRACE, "--- Reprocess resource "DLDLMRES" (%p)\n",
+              PLDLMRES(res), res);
+
+       for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
+               struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
+               struct list_head *head = &queues->liq_waiting[i];
+               struct ldlm_lock *pending;
+               struct ldlm_ibits_node *node;
+
+               if (list_empty(head))
+                       continue;
+               if (hint && !(hint->l_policy_data.l_inodebits.bits & (1 << i)))
+                       continue;
+
+               node = list_entry(head->next, struct ldlm_ibits_node,
+                                 lin_link[i]);
+
+               pending = node->lock;
+               LDLM_DEBUG(pending, "Reprocessing lock from queue %d", i);
+
+               flags = 0;
+               rc = ldlm_process_inodebits_lock(pending, &flags, intention,
+                                                &err, &rpc_list);
+               if (ldlm_is_granted(pending)) {
+                       list_splice(&rpc_list, work_list);
+                       /* Try to grant more locks from current queue */
+                       i--;
+               } else {
+                       list_splice(&rpc_list, &bl_ast_list);
+               }
+       }
+
+       if (!list_empty(&bl_ast_list)) {
+               unlock_res(res);
+
+               rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &bl_ast_list,
+                                      LDLM_WORK_BL_AST);
+
+               lock_res(res);
+               if (rc == -ERESTART)
+                       GOTO(restart, rc);
+       }
+
+       if (!list_empty(&bl_ast_list))
+               ldlm_discard_bl_list(&bl_ast_list);
+
+       RETURN(rc);
+}
+
 /**
  * Determine if the lock is compatible with all locks on the queue.
  *
@@ -209,7 +292,7 @@ int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *flags,
 
        ENTRY;
 
-       LASSERT(lock->l_granted_mode != lock->l_req_mode);
+       LASSERT(!ldlm_is_granted(lock));
        check_res_locked(res);
 
        if (intention == LDLM_PROCESS_RESCAN) {
@@ -325,7 +408,7 @@ int ldlm_inodebits_drop(struct ldlm_lock *lock, __u64 to_drop)
 
        /* Just return if there are no conflicting bits */
        if ((lock->l_policy_data.l_inodebits.bits & to_drop) == 0) {
-               LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx\n",
+               LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx",
                          lock->l_policy_data.l_inodebits.bits, to_drop);
                /* nothing to do */
                RETURN(0);
@@ -356,13 +439,19 @@ int ldlm_cli_dropbits(struct ldlm_lock *lock, __u64 drop_bits)
 
        ldlm_lock2handle(lock, &lockh);
        lock_res_and_lock(lock);
-       /* check if all bits are cancelled */
+       /* check if all bits are blocked */
        if (!(lock->l_policy_data.l_inodebits.bits & ~drop_bits)) {
                unlock_res_and_lock(lock);
                /* return error to continue with cancel */
                GOTO(exit, rc = -EINVAL);
        }
 
+       /* check if no common bits, consider this as successful convert */
+       if (!(lock->l_policy_data.l_inodebits.bits & drop_bits)) {
+               unlock_res_and_lock(lock);
+               GOTO(exit, rc = 0);
+       }
+
        /* check if there is race with cancel */
        if (ldlm_is_canceling(lock) || ldlm_is_cancel(lock)) {
                unlock_res_and_lock(lock);
@@ -408,9 +497,11 @@ int ldlm_cli_dropbits(struct ldlm_lock *lock, __u64 drop_bits)
        rc = ldlm_cli_convert(lock, &flags);
        if (rc) {
                lock_res_and_lock(lock);
-               ldlm_clear_converting(lock);
-               ldlm_set_cbpending(lock);
-               ldlm_set_bl_ast(lock);
+               if (ldlm_is_converting(lock)) {
+                       ldlm_clear_converting(lock);
+                       ldlm_set_cbpending(lock);
+                       ldlm_set_bl_ast(lock);
+               }
                unlock_res_and_lock(lock);
                GOTO(exit, rc);
        }
@@ -419,3 +510,55 @@ exit:
        LDLM_DEBUG(lock, "client lock convert END");
        return rc;
 }
+
+
+int ldlm_inodebits_alloc_lock(struct ldlm_lock *lock)
+{
+       if (ldlm_is_ns_srv(lock)) {
+               int i;
+
+               OBD_SLAB_ALLOC_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
+               if (lock->l_ibits_node == NULL)
+                       return -ENOMEM;
+               for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
+                       INIT_LIST_HEAD(&lock->l_ibits_node->lin_link[i]);
+               lock->l_ibits_node->lock = lock;
+       } else {
+               lock->l_ibits_node = NULL;
+       }
+       return 0;
+}
+
+void ldlm_inodebits_add_lock(struct ldlm_resource *res, struct list_head *head,
+                            struct ldlm_lock *lock)
+{
+       int i;
+
+       if (!ldlm_is_ns_srv(lock))
+               return;
+
+       if (head == &res->lr_waiting) {
+               for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
+                       if (lock->l_policy_data.l_inodebits.bits & (1 << i))
+                               list_add_tail(&lock->l_ibits_node->lin_link[i],
+                                       &res->lr_ibits_queues->liq_waiting[i]);
+               }
+       } else if (head == &res->lr_granted && lock->l_ibits_node != NULL) {
+               for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
+                       LASSERT(list_empty(&lock->l_ibits_node->lin_link[i]));
+               OBD_SLAB_FREE_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
+               lock->l_ibits_node = NULL;
+       }
+}
+
+void ldlm_inodebits_unlink_lock(struct ldlm_lock *lock)
+{
+       int i;
+
+       ldlm_unlink_lock_skiplist(lock);
+       if (!ldlm_is_ns_srv(lock))
+               return;
+
+       for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
+               list_del_init(&lock->l_ibits_node->lin_link[i]);
+}