Whamcloud - gitweb
LU-812 compat: clean up mutex lock to use kernel mutex primitive
[fs/lustre-release.git] / lustre / ptlrpc / ptlrpcd.c
index 56a64ec..478f51b 100644 (file)
@@ -28,9 +28,8 @@
 /*
  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
- */
-/*
- * Copyright (c) 2011 Whamcloud, Inc.
+ *
+ * Copyright (c) 2011, 2012, Whamcloud, Inc.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -95,7 +94,7 @@ CFS_MODULE_PARM(ptlrpcd_bind_policy, "i", int, 0644,
 #endif
 static struct ptlrpcd *ptlrpcds;
 
-cfs_semaphore_t ptlrpcd_sem;
+cfs_mutex_t ptlrpcd_mutex;
 static int ptlrpcd_users = 0;
 
 void ptlrpcd_wake(struct ptlrpc_request *req)
@@ -250,6 +249,8 @@ void ptlrpcd_add_req(struct ptlrpc_request *req, pdl_policy_t policy, int idx)
                 cfs_spin_unlock(&req->rq_lock);
                 l_wait_event(req->rq_set_waitq, (req->rq_set == NULL), &lwi);
         } else if (req->rq_set) {
+                /* If we have a vaid "rq_set", just reuse it to avoid double
+                 * linked. */
                 LASSERT(req->rq_phase == RQ_PHASE_NEW);
                 LASSERT(req->rq_send_state == LUSTRE_IMP_REPLAY);
 
@@ -279,12 +280,13 @@ static inline void ptlrpc_reqset_get(struct ptlrpc_request_set *set)
  * Check if there is more work to do on ptlrpcd set.
  * Returns 1 if yes.
  */
-static int ptlrpcd_check(const struct lu_env *env, struct ptlrpcd_ctl *pc)
+static int ptlrpcd_check(struct lu_env *env, struct ptlrpcd_ctl *pc)
 {
         cfs_list_t *tmp, *pos;
         struct ptlrpc_request *req;
         struct ptlrpc_request_set *set = pc->pc_set;
         int rc = 0;
+        int rc2;
         ENTRY;
 
         if (cfs_atomic_read(&set->set_new_count)) {
@@ -303,6 +305,25 @@ static int ptlrpcd_check(const struct lu_env *env, struct ptlrpcd_ctl *pc)
                 cfs_spin_unlock(&set->set_new_req_lock);
         }
 
+        /* We should call lu_env_refill() before handling new requests to make
+         * sure that env key the requests depending on really exists.
+         */
+        rc2 = lu_env_refill(env);
+        if (rc2 != 0) {
+                /*
+                 * XXX This is very awkward situation, because
+                 * execution can neither continue (request
+                 * interpreters assume that env is set up), nor repeat
+                 * the loop (as this potentially results in a tight
+                 * loop of -ENOMEM's).
+                 *
+                 * Fortunately, refill only ever does something when
+                 * new modules are loaded, i.e., early during boot up.
+                 */
+                CERROR("Failure to refill session: %d\n", rc2);
+                RETURN(rc);
+        }
+
         if (cfs_atomic_read(&set->set_remaining))
                 rc |= ptlrpc_check_set(env, set);
 
@@ -330,6 +351,8 @@ static int ptlrpcd_check(const struct lu_env *env, struct ptlrpcd_ctl *pc)
                 rc = cfs_atomic_read(&set->set_new_count);
 
 #ifdef __KERNEL__
+                /* If we have nothing to do, check whether we can take some
+                 * work from our partner threads. */
                 if (rc == 0 && pc->pc_npartners > 0) {
                         struct ptlrpcd_ctl *partner;
                         struct ptlrpc_request_set *ps;
@@ -357,8 +380,8 @@ static int ptlrpcd_check(const struct lu_env *env, struct ptlrpcd_ctl *pc)
                                         if (rc > 0)
                                                 CDEBUG(D_RPCTRACE, "transfer %d"
                                                        " async RPCs [%d->%d]\n",
-                                                        rc, pc->pc_index,
-                                                        partner->pc_index);
+                                                        rc, partner->pc_index,
+                                                        pc->pc_index);
                                 }
                                 ptlrpc_reqset_put(ps);
                         } while (rc == 0 && pc->pc_cursor != first);
@@ -421,22 +444,6 @@ static int ptlrpcd(void *arg)
                 struct l_wait_info lwi;
                 int timeout;
 
-                rc = lu_env_refill(&env);
-                if (rc != 0) {
-                        /*
-                         * XXX This is very awkward situation, because
-                         * execution can neither continue (request
-                         * interpreters assume that env is set up), nor repeat
-                         * the loop (as this potentially results in a tight
-                         * loop of -ENOMEM's).
-                         *
-                         * Fortunately, refill only ever does something when
-                         * new modules are loaded, i.e., early during boot up.
-                         */
-                        CERROR("Failure to refill session: %d\n", rc);
-                        continue;
-                }
-
                 timeout = ptlrpc_set_next_timeout(set);
                 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
                                   ptlrpc_expired_set, set);
@@ -696,6 +703,8 @@ out:
                 if (env != 0)
                         lu_context_fini(&pc->pc_env.le_ctx);
                 cfs_clear_bit(LIOD_BIND, &pc->pc_flags);
+#else
+                SET_BUT_UNUSED(env);
 #endif
                 cfs_clear_bit(LIOD_START, &pc->pc_flags);
         }
@@ -830,18 +839,18 @@ int ptlrpcd_addref(void)
         int rc = 0;
         ENTRY;
 
-        cfs_mutex_down(&ptlrpcd_sem);
+        cfs_mutex_lock(&ptlrpcd_mutex);
         if (++ptlrpcd_users == 1)
                 rc = ptlrpcd_init();
-        cfs_mutex_up(&ptlrpcd_sem);
+        cfs_mutex_unlock(&ptlrpcd_mutex);
         RETURN(rc);
 }
 
 void ptlrpcd_decref(void)
 {
-        cfs_mutex_down(&ptlrpcd_sem);
+        cfs_mutex_lock(&ptlrpcd_mutex);
         if (--ptlrpcd_users == 0)
                 ptlrpcd_fini();
-        cfs_mutex_up(&ptlrpcd_sem);
+        cfs_mutex_unlock(&ptlrpcd_mutex);
 }
 /** @} ptlrpcd */