Whamcloud - gitweb
LU-2275 mdt: Avoid setting positive dispositions too early
[fs/lustre-release.git] / lustre / mdt / mdt_capa.c
index 204adf3..b86b865 100644 (file)
@@ -1,59 +1,66 @@
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * GPL HEADER START
  *
- *  lustre/mdt/mdt_capa.c
- *  Lustre Metadata Target (mdt) capability key read/write/update.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *  Copyright (C) 2005 Cluster File Systems, Inc.
- *   Author: Lai Siyao <lsy@clusterfs.com>
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   This file is part of the Lustre file system, http://www.lustre.org
- *   Lustre is a trademark of Cluster File Systems, Inc.
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   You may have signed or agreed to another license before downloading
- *   this software.  If so, you are bound by the terms and conditions
- *   of that agreement, and the following does not apply to you.  See the
- *   LICENSE file included with this distribution for more information.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- *   If you did not agree to a different license, then this copy of Lustre
- *   is open source software; you can redistribute it and/or modify it
- *   under the terms of version 2 of the GNU General Public License as
- *   published by the Free Software Foundation.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
  *
- *   In either case, Lustre is distributed in the hope that it will be
- *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
- *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   license text for more details.
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2012, Intel Corporation.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lustre/mdt/mdt_capa.c
+ *
+ * Lustre Metadata Target (mdt) capability key read/write/update.
+ *
+ * Author: Lai Siyao <lsy@clusterfs.com>
  */
 
-#ifndef EXPORT_SYMTAB
-# define EXPORT_SYMTAB
-#endif
 #define DEBUG_SUBSYSTEM S_MDS
 
 #include "mdt_internal.h"
 
 static inline void set_capa_key_expiry(struct mdt_device *mdt)
 {
-        mdt->mdt_ck_expiry = jiffies + mdt->mdt_ck_timeout * HZ;
+        mdt->mdt_ck_expiry = jiffies + mdt->mdt_ck_timeout * CFS_HZ;
 }
 
 static void make_capa_key(struct lustre_capa_key *key,
                           mdsno_t mdsnum, int keyid)
 {
-        key->lk_mdsid = mdsnum;
+        key->lk_seq = mdsnum;
         key->lk_keyid = keyid + 1;
-        ll_get_random_bytes(key->lk_key, sizeof(key->lk_key));
+        cfs_get_random_bytes(key->lk_key, sizeof(key->lk_key));
 }
 
-enum {
-        MDT_TXN_CAPA_KEYS_WRITE_CREDITS = 1
-};
-
 static inline void lck_cpu_to_le(struct lustre_capa_key *tgt,
                                  struct lustre_capa_key *src)
 {
-        tgt->lk_mdsid   = cpu_to_le64(src->lk_mdsid);
+        tgt->lk_seq   = cpu_to_le64(src->lk_seq);
         tgt->lk_keyid   = cpu_to_le32(src->lk_keyid);
         tgt->lk_padding = cpu_to_le32(src->lk_padding);
         memcpy(tgt->lk_key, src->lk_key, sizeof(src->lk_key));
@@ -62,7 +69,7 @@ static inline void lck_cpu_to_le(struct lustre_capa_key *tgt,
 static inline void lck_le_to_cpu(struct lustre_capa_key *tgt,
                                  struct lustre_capa_key *src)
 {
-        tgt->lk_mdsid   = le64_to_cpu(src->lk_mdsid);
+        tgt->lk_seq   = le64_to_cpu(src->lk_seq);
         tgt->lk_keyid   = le32_to_cpu(src->lk_keyid);
         tgt->lk_padding = le32_to_cpu(src->lk_padding);
         memcpy(tgt->lk_key, src->lk_key, sizeof(src->lk_key));
@@ -79,24 +86,33 @@ static int write_capa_keys(const struct lu_env *env,
         int i, rc;
 
         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
+       th = dt_trans_create(env, mdt->mdt_bottom);
+       if (IS_ERR(th))
+               RETURN(PTR_ERR(th));
+
+       rc = dt_declare_record_write(env, mdt->mdt_ck_obj,
+                                    sizeof(*tmp) * 3, 0, th);
+       if (rc)
+               goto stop;
 
-        th = mdt_trans_start(env, mdt, MDT_TXN_CAPA_KEYS_WRITE_CREDITS);
-        if (IS_ERR(th))
-                RETURN(PTR_ERR(th));
+       rc = dt_trans_start_local(env, mdt->mdt_bottom, th);
+        if (rc)
+                goto stop;
 
         tmp = &mti->mti_capa_key;
 
         for (i = 0; i < 2; i++) {
                 lck_cpu_to_le(tmp, &keys[i]);
 
-                rc = mdt_record_write(env, mdt->mdt_ck_obj,
-                                      mdt_buf_const(env, tmp, sizeof(*tmp)),
-                                      &off, th);
+                rc = dt_record_write(env, mdt->mdt_ck_obj,
+                                     mdt_buf_const(env, tmp, sizeof(*tmp)),
+                                     &off, th);
                 if (rc)
                         break;
         }
 
-        mdt_trans_stop(env, mdt, th);
+stop:
+       dt_trans_stop(env, mdt->mdt_bottom, th);
 
         CDEBUG(D_INFO, "write capability keys rc = %d:\n", rc);
         return rc;
@@ -115,8 +131,8 @@ static int read_capa_keys(const struct lu_env *env,
         tmp = &mti->mti_capa_key;
 
         for (i = 0; i < 2; i++) {
-                rc = mdt_record_read(env, mdt->mdt_ck_obj,
-                                     mdt_buf(env, tmp, sizeof(*tmp)), &off);
+                rc = dt_record_read(env, mdt->mdt_ck_obj,
+                                    mdt_buf(env, tmp, sizeof(*tmp)), &off);
                 if (rc)
                         return rc;
 
@@ -138,7 +154,7 @@ int mdt_capa_keys_init(const struct lu_env *env, struct mdt_device *mdt)
         int                      rc;
         ENTRY;
 
-        mdsnum = mdt->mdt_md_dev.md_lu_dev.ld_site->ls_node_id;
+        mdsnum = mdt_md_site(mdt)->ms_node_id;
 
         mti = lu_context_key_get(&env->le_ctx, &mdt_thread_key);
         LASSERT(mti != NULL);
@@ -171,7 +187,7 @@ int mdt_capa_keys_init(const struct lu_env *env, struct mdt_device *mdt)
                 }
         }
         set_capa_key_expiry(mdt);
-        mod_timer(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
+        cfs_timer_arm(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
         CDEBUG(D_SEC, "mds_ck_timer %lu\n", mdt->mdt_ck_expiry);
         RETURN(0);
 }
@@ -182,8 +198,8 @@ void mdt_ck_timer_callback(unsigned long castmeharder)
         struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
 
         ENTRY;
-        thread->t_flags |= SVC_EVENT;
-        wake_up(&thread->t_ctl_waitq);
+        thread_add_flags(thread, SVC_EVENT);
+        cfs_waitq_signal(&thread->t_ctl_waitq);
         EXIT;
 }
 
@@ -202,34 +218,36 @@ static int mdt_ck_thread_main(void *args)
         int                     rc;
         ENTRY;
 
-        ptlrpc_daemonize("mdt_ck");
+        cfs_daemonize_ctxt("mdt_ck");
         cfs_block_allsigs();
 
-        thread->t_flags = SVC_RUNNING;
+        thread_set_flags(thread, SVC_RUNNING);
         cfs_waitq_signal(&thread->t_ctl_waitq);
 
-        rc = lu_env_init(&env, NULL, LCT_MD_THREAD);
+        rc = lu_env_init(&env, LCT_MD_THREAD|LCT_REMEMBER|LCT_NOREF);
         if (rc)
                 RETURN(rc);
 
         thread->t_env = &env;
         env.le_ctx.lc_thread = thread;
+        env.le_ctx.lc_cookie = 0x1;
 
         info = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
         LASSERT(info != NULL);
 
         tmp = &info->mti_capa_key;
-        mdsnum = mdt->mdt_md_dev.md_lu_dev.ld_site->ls_node_id;
+        mdsnum = mdt_md_site(mdt)->ms_node_id;
         while (1) {
                 l_wait_event(thread->t_ctl_waitq,
-                             thread->t_flags & (SVC_STOPPING | SVC_EVENT),
+                             thread_is_stopping(thread) ||
+                             thread_is_event(thread),
                              &lwi);
 
-                if (thread->t_flags & SVC_STOPPING)
+                if (thread_is_stopping(thread))
                         break;
-                thread->t_flags &= ~SVC_EVENT;
+                thread_clear_flags(thread, SVC_EVENT);
 
-                if (time_after(mdt->mdt_ck_expiry, jiffies))
+                if (cfs_time_before(cfs_time_current(), mdt->mdt_ck_expiry))
                         break;
 
                 *tmp = *rkey;
@@ -238,17 +256,17 @@ static int mdt_ck_thread_main(void *args)
                 next = mdt->mdt_child;
                 rc = next->md_ops->mdo_update_capa_key(&env, next, tmp);
                 if (!rc) {
-                        spin_lock(&capa_lock);
-                        *bkey = *rkey;
-                        *rkey = *tmp;
-                        spin_unlock(&capa_lock);
-
-                        rc = write_capa_keys(&env, mdt, mdt->mdt_capa_keys);
-                        if (rc) {
-                                spin_lock(&capa_lock);
-                                *rkey = *bkey;
-                                memset(bkey, 0, sizeof(*bkey));
-                                spin_unlock(&capa_lock);
+                       spin_lock(&capa_lock);
+                       *bkey = *rkey;
+                       *rkey = *tmp;
+                       spin_unlock(&capa_lock);
+
+                       rc = write_capa_keys(&env, mdt, mdt->mdt_capa_keys);
+                       if (rc) {
+                               spin_lock(&capa_lock);
+                               *rkey = *bkey;
+                               memset(bkey, 0, sizeof(*bkey));
+                               spin_unlock(&capa_lock);
                         } else {
                                 set_capa_key_expiry(mdt);
                                 DEBUG_CAPA_KEY(D_SEC, rkey, "new");
@@ -257,15 +275,15 @@ static int mdt_ck_thread_main(void *args)
                 if (rc) {
                         DEBUG_CAPA_KEY(D_ERROR, rkey, "update failed for");
                         /* next retry is in 300 sec */
-                        mdt->mdt_ck_expiry = jiffies + 300 * HZ;
+                        mdt->mdt_ck_expiry = jiffies + 300 * CFS_HZ;
                 }
 
-                mod_timer(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
+                cfs_timer_arm(&mdt->mdt_ck_timer, mdt->mdt_ck_expiry);
                 CDEBUG(D_SEC, "mdt_ck_timer %lu\n", mdt->mdt_ck_expiry);
         }
         lu_env_fini(&env);
 
-        thread->t_flags = SVC_STOPPED;
+        thread_set_flags(thread, SVC_STOPPED);
         cfs_waitq_signal(&thread->t_ctl_waitq);
         RETURN(0);
 }
@@ -276,14 +294,13 @@ int mdt_ck_thread_start(struct mdt_device *mdt)
         int rc;
 
         cfs_waitq_init(&thread->t_ctl_waitq);
-        rc = cfs_kernel_thread(mdt_ck_thread_main, mdt,
-                           (CLONE_VM | CLONE_FILES));
+        rc = cfs_create_thread(mdt_ck_thread_main, mdt, CFS_DAEMON_FLAGS);
         if (rc < 0) {
                 CERROR("cannot start mdt_ck thread, rc = %d\n", rc);
                 return rc;
         }
 
-        cfs_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
+        l_wait_condition(thread->t_ctl_waitq, thread_is_running(thread));
         return 0;
 }
 
@@ -291,12 +308,10 @@ void mdt_ck_thread_stop(struct mdt_device *mdt)
 {
         struct ptlrpc_thread *thread = &mdt->mdt_ck_thread;
 
-        if (!(thread->t_flags & SVC_RUNNING))
+        if (!thread_is_running(thread))
                 return;
 
-        thread->t_flags = SVC_STOPPING;
+        thread_set_flags(thread, SVC_STOPPING);
         cfs_waitq_signal(&thread->t_ctl_waitq);
-        cfs_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
+        l_wait_condition(thread->t_ctl_waitq, thread_is_stopped(thread));
 }
-
-