Whamcloud - gitweb
LU-1302 llog: pass lu_env as parametr in llog functions
[fs/lustre-release.git] / lustre / ptlrpc / recov_thread.c
index 48e5261..710afd7 100644 (file)
@@ -1,6 +1,4 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
  * GPL HEADER START
  *
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, Whamcloud, Inc.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #define DEBUG_SUBSYSTEM S_LOG
 
-#ifndef EXPORT_SYMTAB
-# define EXPORT_SYMTAB
-#endif
-
 #ifdef __KERNEL__
 # include <libcfs/libcfs.h>
 #else
@@ -67,7 +63,7 @@
 #include <lustre_log.h>
 #include "ptlrpc_internal.h"
 
-static atomic_t                   llcd_count = ATOMIC_INIT(0);
+static cfs_atomic_t               llcd_count = CFS_ATOMIC_INIT(0);
 static cfs_mem_cache_t           *llcd_cache = NULL;
 
 #ifdef __KERNEL__
@@ -76,8 +72,12 @@ enum {
         LLOG_LCM_FL_EXIT        = 1 << 1
 };
 
-static void llcd_print(struct llog_canceld_ctxt *llcd, 
-                       const char *func, int line) 
+struct llcd_async_args {
+        struct llog_canceld_ctxt *la_ctxt;
+};
+
+static void llcd_print(struct llog_canceld_ctxt *llcd,
+                       const char *func, int line)
 {
         CDEBUG(D_RPCTRACE, "Llcd (%p) at %s:%d:\n", llcd, func, line);
         CDEBUG(D_RPCTRACE, "  size: %d\n", llcd->llcd_size);
@@ -104,7 +104,7 @@ static struct llog_canceld_ctxt *llcd_alloc(struct llog_commit_master *lcm)
          */
         size = CFS_PAGE_SIZE - lustre_msg_size(LUSTRE_MSG_MAGIC_V2, 1, NULL);
         overhead =  offsetof(struct llog_canceld_ctxt, llcd_cookies);
-        OBD_SLAB_ALLOC(llcd, llcd_cache, CFS_ALLOC_STD, size + overhead);
+       OBD_SLAB_ALLOC_GFP(llcd, llcd_cache, size + overhead, CFS_ALLOC_STD);
         if (!llcd)
                 return NULL;
 
@@ -112,15 +112,15 @@ static struct llog_canceld_ctxt *llcd_alloc(struct llog_commit_master *lcm)
         llcd->llcd_cookiebytes = 0;
         llcd->llcd_size = size;
 
-        spin_lock(&lcm->lcm_lock);
+        cfs_spin_lock(&lcm->lcm_lock);
         llcd->llcd_lcm = lcm;
-        atomic_inc(&lcm->lcm_count);
-        list_add_tail(&llcd->llcd_list, &lcm->lcm_llcds);
-        spin_unlock(&lcm->lcm_lock);
-        atomic_inc(&llcd_count);
+        cfs_atomic_inc(&lcm->lcm_count);
+        cfs_list_add_tail(&llcd->llcd_list, &lcm->lcm_llcds);
+        cfs_spin_unlock(&lcm->lcm_lock);
+        cfs_atomic_inc(&llcd_count);
 
         CDEBUG(D_RPCTRACE, "Alloc llcd %p on lcm %p (%d)\n",
-               llcd, lcm, atomic_read(&lcm->lcm_count));
+               llcd, lcm, cfs_atomic_read(&lcm->lcm_count));
 
         return llcd;
 }
@@ -134,25 +134,25 @@ static void llcd_free(struct llog_canceld_ctxt *llcd)
         int size;
 
         if (lcm) {
-                if (atomic_read(&lcm->lcm_count) == 0) {
+                if (cfs_atomic_read(&lcm->lcm_count) == 0) {
                         CERROR("Invalid llcd free %p\n", llcd);
                         llcd_print(llcd, __FUNCTION__, __LINE__);
                         LBUG();
                 }
-                spin_lock(&lcm->lcm_lock);
-                LASSERT(!list_empty(&llcd->llcd_list));
-                list_del_init(&llcd->llcd_list);
-                atomic_dec(&lcm->lcm_count);
-                spin_unlock(&lcm->lcm_lock);
-
-                CDEBUG(D_RPCTRACE, "Free llcd %p on lcm %p (%d)\n", 
-                       llcd, lcm, atomic_read(&lcm->lcm_count));
+                cfs_spin_lock(&lcm->lcm_lock);
+                LASSERT(!cfs_list_empty(&llcd->llcd_list));
+                cfs_list_del_init(&llcd->llcd_list);
+                cfs_atomic_dec(&lcm->lcm_count);
+                cfs_spin_unlock(&lcm->lcm_lock);
+
+                CDEBUG(D_RPCTRACE, "Free llcd %p on lcm %p (%d)\n",
+                       llcd, lcm, cfs_atomic_read(&lcm->lcm_count));
         }
 
-        LASSERT(atomic_read(&llcd_count) > 0);
-        atomic_dec(&llcd_count);
+        LASSERT(cfs_atomic_read(&llcd_count) > 0);
+        cfs_atomic_dec(&llcd_count);
 
-        size = offsetof(struct llog_canceld_ctxt, llcd_cookies) + 
+        size = offsetof(struct llog_canceld_ctxt, llcd_cookies) +
             llcd->llcd_size;
         OBD_SLAB_FREE(llcd, llcd_cache, size);
 }
@@ -161,7 +161,7 @@ static void llcd_free(struct llog_canceld_ctxt *llcd)
  * Checks if passed cookie fits into llcd free space buffer. Returns
  * 1 if yes and 0 otherwise.
  */
-static inline int 
+static inline int
 llcd_fit(struct llog_canceld_ctxt *llcd, struct llog_cookie *cookies)
 {
         return (llcd->llcd_size - llcd->llcd_cookiebytes >= sizeof(*cookies));
@@ -170,11 +170,11 @@ llcd_fit(struct llog_canceld_ctxt *llcd, struct llog_cookie *cookies)
 /**
  * Copy passed @cookies to @llcd.
  */
-static inline void 
+static inline void
 llcd_copy(struct llog_canceld_ctxt *llcd, struct llog_cookie *cookies)
 {
         LASSERT(llcd_fit(llcd, cookies));
-        memcpy((char *)llcd->llcd_cookies + llcd->llcd_cookiebytes, 
+        memcpy((char *)llcd->llcd_cookies + llcd->llcd_cookiebytes,
               cookies, sizeof(*cookies));
         llcd->llcd_cookiebytes += sizeof(*cookies);
 }
@@ -186,9 +186,11 @@ llcd_copy(struct llog_canceld_ctxt *llcd, struct llog_cookie *cookies)
  */
 static int
 llcd_interpret(const struct lu_env *env,
-               struct ptlrpc_request *req, void *noused, int rc)
+               struct ptlrpc_request *req, void *args, int rc)
 {
-        struct llog_canceld_ctxt *llcd = req->rq_async_args.pointer_arg[0];
+        struct llcd_async_args *la = args;
+        struct llog_canceld_ctxt *llcd = la->la_ctxt;
+
         CDEBUG(D_RPCTRACE, "Sent llcd %p (%d) - killing it\n", llcd, rc);
         llcd_free(llcd);
         return 0;
@@ -204,6 +206,7 @@ static int llcd_send(struct llog_canceld_ctxt *llcd)
         char *bufs[2] = { NULL, (char *)llcd->llcd_cookies };
         struct obd_import *import = NULL;
         struct llog_commit_master *lcm;
+        struct llcd_async_args *la;
         struct ptlrpc_request *req;
         struct llog_ctxt *ctxt;
         int rc;
@@ -216,7 +219,7 @@ static int llcd_send(struct llog_canceld_ctxt *llcd)
                 llcd_print(llcd, __FUNCTION__, __LINE__);
                 LBUG();
         }
-        LASSERT_SEM_LOCKED(&ctxt->loc_sem);
+        LASSERT_MUTEX_LOCKED(&ctxt->loc_mutex);
 
         if (llcd->llcd_cookiebytes == 0)
                 GOTO(exit, rc = 0);
@@ -227,7 +230,7 @@ static int llcd_send(struct llog_canceld_ctxt *llcd)
          * Check if we're in exit stage. Do not send llcd in
          * this case.
          */
-        if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
+        if (cfs_test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
                 GOTO(exit, rc = -ENODEV);
 
         CDEBUG(D_RPCTRACE, "Sending llcd %p\n", llcd);
@@ -268,13 +271,18 @@ static int llcd_send(struct llog_canceld_ctxt *llcd)
         /* bug 5515 */
         req->rq_request_portal = LDLM_CANCEL_REQUEST_PORTAL;
         req->rq_reply_portal = LDLM_CANCEL_REPLY_PORTAL;
+
         req->rq_interpret_reply = (ptlrpc_interpterer_t)llcd_interpret;
-        req->rq_async_args.pointer_arg[0] = llcd;
-        rc = ptlrpc_set_add_new_req(&lcm->lcm_pc, req);
-        if (rc) {
-                ptlrpc_request_free(req);
-                GOTO(exit, rc);
-        }
+
+        CLASSERT(sizeof(*la) <= sizeof(req->rq_async_args));
+        la = ptlrpc_req_async_args(req);
+        la->la_ctxt = llcd;
+
+        /* llog cancels will be replayed after reconnect so this will do twice
+         * first from replay llog, second for resended rpc */
+        req->rq_no_delay = req->rq_no_resend = 1;
+
+        ptlrpc_set_add_new_req(&lcm->lcm_pc, req);
         RETURN(0);
 exit:
         CDEBUG(D_RPCTRACE, "Refused llcd %p\n", llcd);
@@ -290,7 +298,7 @@ static int
 llcd_attach(struct llog_ctxt *ctxt, struct llog_canceld_ctxt *llcd)
 {
         LASSERT(ctxt != NULL && llcd != NULL);
-        LASSERT_SEM_LOCKED(&ctxt->loc_sem);
+        LASSERT_MUTEX_LOCKED(&ctxt->loc_mutex);
         LASSERT(ctxt->loc_llcd == NULL);
         llcd->llcd_ctxt = llog_ctxt_get(ctxt);
         ctxt->loc_llcd = llcd;
@@ -310,13 +318,13 @@ static struct llog_canceld_ctxt *llcd_detach(struct llog_ctxt *ctxt)
         struct llog_canceld_ctxt *llcd;
 
         LASSERT(ctxt != NULL);
-        LASSERT_SEM_LOCKED(&ctxt->loc_sem);
+        LASSERT_MUTEX_LOCKED(&ctxt->loc_mutex);
 
         llcd = ctxt->loc_llcd;
         if (!llcd)
                 return NULL;
 
-        CDEBUG(D_RPCTRACE, "Detach llcd %p from ctxt %p\n", 
+        CDEBUG(D_RPCTRACE, "Detach llcd %p from ctxt %p\n",
                llcd, ctxt);
 
         ctxt->loc_llcd = NULL;
@@ -331,7 +339,7 @@ static struct llog_canceld_ctxt *llcd_detach(struct llog_ctxt *ctxt)
 static struct llog_canceld_ctxt *llcd_get(struct llog_ctxt *ctxt)
 {
         struct llog_canceld_ctxt *llcd;
-
+        LASSERT(ctxt);
         llcd = llcd_alloc(ctxt->loc_lcm);
         if (!llcd) {
                 CERROR("Can't alloc an llcd for ctxt %p\n", ctxt);
@@ -389,7 +397,7 @@ int llog_recov_thread_start(struct llog_commit_master *lcm)
         int rc;
         ENTRY;
 
-        rc = ptlrpcd_start(lcm->lcm_name, &lcm->lcm_pc);
+        rc = ptlrpcd_start(-1, 1, lcm->lcm_name, &lcm->lcm_pc);
         if (rc) {
                 CERROR("Error %d while starting recovery thread %s\n",
                        rc, lcm->lcm_name);
@@ -410,7 +418,7 @@ void llog_recov_thread_stop(struct llog_commit_master *lcm, int force)
          * Let all know that we're stopping. This will also make
          * llcd_send() refuse any new llcds.
          */
-        set_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags);
+        cfs_set_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags);
 
         /*
          * Stop processing thread. No new rpcs will be accepted for
@@ -423,21 +431,21 @@ void llog_recov_thread_stop(struct llog_commit_master *lcm, int force)
          * those forgotten in sync may still be attached to ctxt. Let's
          * print them.
          */
-        if (atomic_read(&lcm->lcm_count) != 0) {
+        if (cfs_atomic_read(&lcm->lcm_count) != 0) {
                 struct llog_canceld_ctxt *llcd;
-                struct list_head         *tmp;
+                cfs_list_t               *tmp;
 
-                CERROR("Busy llcds found (%d) on lcm %p\n", 
-                       atomic_read(&lcm->lcm_count) == 0, lcm);
+                CERROR("Busy llcds found (%d) on lcm %p\n",
+                       cfs_atomic_read(&lcm->lcm_count), lcm);
 
-                spin_lock(&lcm->lcm_lock);
-                list_for_each(tmp, &lcm->lcm_llcds) {
-                        llcd = list_entry(tmp, struct llog_canceld_ctxt,
-                                          llcd_list);
+                cfs_spin_lock(&lcm->lcm_lock);
+                cfs_list_for_each(tmp, &lcm->lcm_llcds) {
+                        llcd = cfs_list_entry(tmp, struct llog_canceld_ctxt,
+                                              llcd_list);
                         llcd_print(llcd, __FUNCTION__, __LINE__);
                 }
-                spin_unlock(&lcm->lcm_lock);
-                
+                cfs_spin_unlock(&lcm->lcm_lock);
+
                 /*
                  * No point to go further with busy llcds at this point
                  * as this is clear bug. It might mean we got hanging
@@ -471,11 +479,11 @@ struct llog_commit_master *llog_recov_thread_init(char *name)
          * Try to create threads with unique names.
          */
         snprintf(lcm->lcm_name, sizeof(lcm->lcm_name),
-                 "ll_log_commit_%s", name);
+                 "lcm_%s", name);
 
-        strncpy(lcm->lcm_name, name, sizeof(lcm->lcm_name));
-        atomic_set(&lcm->lcm_count, 0);
-        spin_lock_init(&lcm->lcm_lock);
+        cfs_atomic_set(&lcm->lcm_count, 0);
+        cfs_atomic_set(&lcm->lcm_refcount, 1);
+        cfs_spin_lock_init(&lcm->lcm_lock);
         CFS_INIT_LIST_HEAD(&lcm->lcm_llcds);
         rc = llog_recov_thread_start(lcm);
         if (rc) {
@@ -496,7 +504,7 @@ void llog_recov_thread_fini(struct llog_commit_master *lcm, int force)
 {
         ENTRY;
         llog_recov_thread_stop(lcm, force);
-        OBD_FREE_PTR(lcm);
+        lcm_put(lcm);
         EXIT;
 }
 EXPORT_SYMBOL(llog_recov_thread_fini);
@@ -530,8 +538,7 @@ static int llog_recov_thread_replay(struct llog_ctxt *ctxt,
                 OBD_FREE_PTR(lpca);
                 RETURN(-ENODEV);
         }
-        rc = cfs_kernel_thread(llog_cat_process_thread, lpca,
-                               CLONE_VM | CLONE_FILES);
+        rc = cfs_create_thread(llog_cat_process_thread, lpca, CFS_DAEMON_FLAGS);
         if (rc < 0) {
                 CERROR("Error starting llog_cat_process_thread(): %d\n", rc);
                 OBD_FREE_PTR(lpca);
@@ -555,15 +562,15 @@ int llog_obd_repl_connect(struct llog_ctxt *ctxt,
          * Send back cached llcd from llog before recovery if we have any.
          * This is void is nothing cached is found there.
          */
-        llog_sync(ctxt, NULL);
+       llog_sync(ctxt, NULL, 0);
 
         /*
          * Start recovery in separate thread.
          */
-        mutex_down(&ctxt->loc_sem);
+        cfs_mutex_lock(&ctxt->loc_mutex);
         ctxt->loc_gen = *gen;
         rc = llog_recov_thread_replay(ctxt, ctxt->llog_proc_cb, logid);
-        mutex_up(&ctxt->loc_sem);
+        cfs_mutex_unlock(&ctxt->loc_mutex);
 
         RETURN(rc);
 }
@@ -574,9 +581,9 @@ EXPORT_SYMBOL(llog_obd_repl_connect);
  * log record for the deletion. The commit callback calls this
  * function.
  */
-int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
-                         struct lov_stripe_md *lsm, int count,
-                         struct llog_cookie *cookies, int flags)
+int llog_obd_repl_cancel(const struct lu_env *env, struct llog_ctxt *ctxt,
+                        struct lov_stripe_md *lsm, int count,
+                        struct llog_cookie *cookies, int flags)
 {
         struct llog_commit_master *lcm;
         struct llog_canceld_ctxt *llcd;
@@ -585,19 +592,29 @@ int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
 
         LASSERT(ctxt != NULL);
 
-        mutex_down(&ctxt->loc_sem);
+        cfs_mutex_lock(&ctxt->loc_mutex);
+        if (!ctxt->loc_lcm) {
+                CDEBUG(D_RPCTRACE, "No lcm for ctxt %p\n", ctxt);
+                GOTO(out, rc = -ENODEV);
+        }
         lcm = ctxt->loc_lcm;
+        CDEBUG(D_INFO, "cancel on lsm %p\n", lcm);
 
         /*
          * Let's check if we have all structures alive. We also check for
          * possible shutdown. Do nothing if we're stopping.
          */
-        if (ctxt->loc_imp == NULL) {
+       if (ctxt->loc_flags & LLOG_CTXT_FLAG_STOP) {
+               CDEBUG(D_RPCTRACE, "Last sync was done for ctxt %p\n", ctxt);
+               GOTO(out, rc = -ENODEV);
+       }
+
+       if (ctxt->loc_imp == NULL) {
                 CDEBUG(D_RPCTRACE, "No import for ctxt %p\n", ctxt);
                 GOTO(out, rc = -ENODEV);
         }
 
-        if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags)) {
+        if (cfs_test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags)) {
                 CDEBUG(D_RPCTRACE, "Commit thread is stopping for ctxt %p\n",
                        ctxt);
                 GOTO(out, rc = -ENODEV);
@@ -617,7 +634,7 @@ int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
                          * Allocation is successful, let's check for stop
                          * flag again to fall back as soon as possible.
                          */
-                        if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
+                        if (cfs_test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
                                 GOTO(out, rc = -ENODEV);
                 }
 
@@ -636,7 +653,7 @@ int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
                          * Allocation is successful, let's check for stop
                          * flag again to fall back as soon as possible.
                          */
-                        if (test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
+                        if (cfs_test_bit(LLOG_LCM_FL_EXIT, &lcm->lcm_flags))
                                 GOTO(out, rc = -ENODEV);
                 }
 
@@ -661,20 +678,25 @@ int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
 out:
         if (rc)
                 llcd_put(ctxt);
-        mutex_up(&ctxt->loc_sem);
+
+       if (flags & OBD_LLOG_FL_EXIT)
+               ctxt->loc_flags = LLOG_CTXT_FLAG_STOP;
+
+        cfs_mutex_unlock(&ctxt->loc_mutex);
         return rc;
 }
 EXPORT_SYMBOL(llog_obd_repl_cancel);
 
-int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
+int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp,
+                      int flags)
 {
         int rc = 0;
         ENTRY;
 
-        /* 
-         * Flush any remaining llcd. 
+        /*
+         * Flush any remaining llcd.
          */
-        mutex_down(&ctxt->loc_sem);
+        cfs_mutex_lock(&ctxt->loc_mutex);
         if (exp && (ctxt->loc_imp == exp->exp_imp_reverse)) {
                 /*
                  * This is ost->mds connection, we can't be sure that mds
@@ -682,16 +704,21 @@ int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
                  */
                 CDEBUG(D_RPCTRACE, "Kill cached llcd\n");
                 llcd_put(ctxt);
-                mutex_up(&ctxt->loc_sem);
+
+               if (flags & OBD_LLOG_FL_EXIT)
+                       ctxt->loc_flags = LLOG_CTXT_FLAG_STOP;
+
+                cfs_mutex_unlock(&ctxt->loc_mutex);
         } else {
-                /* 
+                /*
                  * This is either llog_sync() from generic llog code or sync
                  * on client disconnect. In either way let's do it and send
-                 * llcds to the target with waiting for completion. 
+                 * llcds to the target with waiting for completion.
                  */
                 CDEBUG(D_RPCTRACE, "Sync cached llcd\n");
-                mutex_up(&ctxt->loc_sem);
-                rc = llog_cancel(ctxt, NULL, 0, NULL, OBD_LLOG_FL_SENDNOW);
+                cfs_mutex_unlock(&ctxt->loc_mutex);
+               rc = llog_cancel(NULL, ctxt, NULL, 0, NULL,
+                                OBD_LLOG_FL_SENDNOW | flags);
         }
         RETURN(rc);
 }
@@ -699,7 +726,7 @@ EXPORT_SYMBOL(llog_obd_repl_sync);
 
 #else /* !__KERNEL__ */
 
-int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
+int llog_obd_repl_cancel(const struct lu_env *env, struct llog_ctxt *ctxt,
                          struct lov_stripe_md *lsm, int count,
                          struct llog_cookie *cookies, int flags)
 {
@@ -739,9 +766,9 @@ void llog_recov_fini(void)
                  * In 2.6.22 cfs_mem_cache_destroy() will not return error
                  * for busy resources. Let's check it another way.
                  */
-                LASSERTF(atomic_read(&llcd_count) == 0,
+                LASSERTF(cfs_atomic_read(&llcd_count) == 0,
                          "Can't destroy llcd cache! Number of "
-                         "busy llcds: %d\n", atomic_read(&llcd_count));
+                         "busy llcds: %d\n", cfs_atomic_read(&llcd_count));
                 cfs_mem_cache_destroy(llcd_cache);
                 llcd_cache = NULL;
         }