Whamcloud - gitweb
LU-1081 fid: define special FID for BFL
[fs/lustre-release.git] / lustre / fid / fid_store.c
index ae88f59..9ef70c6 100644 (file)
@@ -16,8 +16,8 @@
  * in the LICENSE file that accompanied this code).
  *
  * You should have received a copy of the GNU General Public License
- * version 2 along with this program; If not, see [sun.com URL with a
- * copy of GPLv2].
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  * CA 95054 USA or visit www.sun.com if you need additional information or
  * GPL HEADER END
  */
 /*
- * Copyright  2008 Sun Microsystems, Inc. All rights reserved
+ * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2012, Whamcloud, Inc.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -62,9 +64,6 @@
 #include "fid_internal.h"
 
 #ifdef __KERNEL__
-enum {
-        SEQ_TXN_STORE_CREDITS = 20
-};
 
 static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
 {
@@ -76,47 +75,159 @@ static struct lu_buf *seq_store_buf(struct seq_thread_info *info)
         return buf;
 }
 
+struct seq_update_callback {
+        struct dt_txn_commit_cb suc_cb;
+        struct lu_server_seq   *suc_seq;
+};
+
+void seq_update_cb(struct lu_env *env, struct thandle *th,
+                   struct dt_txn_commit_cb *cb, int err)
+{
+        struct seq_update_callback *ccb;
+        ccb = container_of0(cb, struct seq_update_callback, suc_cb);
+        ccb->suc_seq->lss_need_sync = 0;
+        cfs_list_del(&ccb->suc_cb.dcb_linkage);
+        OBD_FREE_PTR(ccb);
+}
+
+struct thandle *seq_store_trans_create(struct lu_server_seq *seq,
+                                       const struct lu_env *env)
+{
+        struct dt_device *dt_dev;
+
+        dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
+        return dt_trans_create(env, dt_dev);
+}
+
+int seq_store_trans_start(struct lu_server_seq *seq, const struct lu_env *env,
+                          struct thandle *th)
+{
+        struct dt_device *dt_dev;
+        ENTRY;
+
+        dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
+
+        return dt_trans_start(env, dt_dev, th);
+}
+
+int seq_update_cb_add(struct thandle *th, struct lu_server_seq *seq)
+{
+        struct seq_update_callback *ccb;
+        int rc;
+        OBD_ALLOC_PTR(ccb);
+        if (ccb == NULL)
+                return -ENOMEM;
+
+        ccb->suc_cb.dcb_func = seq_update_cb;
+        CFS_INIT_LIST_HEAD(&ccb->suc_cb.dcb_linkage);
+        ccb->suc_seq = seq;
+        seq->lss_need_sync = 1;
+        rc = dt_trans_cb_add(th, &ccb->suc_cb);
+        if (rc)
+                OBD_FREE_PTR(ccb);
+        return rc;
+}
+
+int seq_declare_store_write(struct lu_server_seq *seq,
+                            const struct lu_env *env,
+                            struct thandle *th)
+{
+        struct dt_object *dt_obj = seq->lss_obj;
+        int rc;
+        ENTRY;
+
+        rc = dt_obj->do_body_ops->dbo_declare_write(env, dt_obj,
+                                                    sizeof(struct lu_seq_range),
+                                                    0, th);
+        return rc;
+}
+
 /* This function implies that caller takes care about locking. */
 int seq_store_write(struct lu_server_seq *seq,
-                    const struct lu_env *env)
+                    const struct lu_env *env,
+                    struct thandle *th)
 {
         struct dt_object *dt_obj = seq->lss_obj;
         struct seq_thread_info *info;
-        struct dt_device *dt_dev;
-        struct thandle *th;
         loff_t pos = 0;
-       int rc;
-       ENTRY;
+        int rc;
+        ENTRY;
 
-        dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
         LASSERT(info != NULL);
 
-        /* Stub here, will fix it later. */
-        txn_param_init(&info->sti_txn, SEQ_TXN_STORE_CREDITS);
-
-        th = dt_dev->dd_ops->dt_trans_start(env, dt_dev, &info->sti_txn);
-        if (!IS_ERR(th)) {
-                /* Store ranges in le format. */
-                range_cpu_to_le(&info->sti_space, &seq->lss_space);
-
-                rc = dt_obj->do_body_ops->dbo_write(env, dt_obj,
-                                                    seq_store_buf(info),
-                                                    &pos, th, BYPASS_CAPA);
-                if (rc == sizeof(info->sti_space)) {
-                        CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
-                               seq->lss_name, PRANGE(&seq->lss_space));
-                        rc = 0;
-                } else if (rc >= 0) {
-                        rc = -EIO;
-                }
+        /* Store ranges in le format. */
+        range_cpu_to_le(&info->sti_space, &seq->lss_space);
 
-                dt_dev->dd_ops->dt_trans_stop(env, th);
-        } else {
-                rc = PTR_ERR(th);
+        rc = dt_obj->do_body_ops->dbo_write(env, dt_obj,
+                                            seq_store_buf(info),
+                                            &pos, th, BYPASS_CAPA, 1);
+        if (rc == sizeof(info->sti_space)) {
+                CDEBUG(D_INFO, "%s: Space - "DRANGE"\n",
+                       seq->lss_name, PRANGE(&seq->lss_space));
+                rc = 0;
+        } else if (rc >= 0) {
+                rc = -EIO;
+        }
+
+
+        RETURN(rc);
+}
+
+int seq_store_update(const struct lu_env *env, struct lu_server_seq *seq,
+                     struct lu_seq_range *out, int sync)
+{
+        struct dt_device *dt_dev;
+        struct thandle *th;
+        int rc;
+        ENTRY;
+
+        dt_dev = lu2dt_dev(seq->lss_obj->do_lu.lo_dev);
+
+        th = seq_store_trans_create(seq, env);
+        if (IS_ERR(th))
+                RETURN(PTR_ERR(th));
+
+        rc = seq_declare_store_write(seq, env, th);
+        if (rc)
+                GOTO(exit, rc);
+
+        if (out != NULL) {
+                rc = fld_declare_server_create(seq->lss_site->ms_server_fld,
+                                               env, th);
+                if (rc)
+                        GOTO(exit, rc);
+        }
+
+        rc = seq_store_trans_start(seq, env, th);
+        if (rc)
+                GOTO(exit, rc);
+
+        rc = seq_store_write(seq, env, th);
+        if (rc) {
+                CERROR("%s: Can't write space data, rc %d\n",
+                       seq->lss_name, rc);
+                GOTO(exit,rc);
+        } else if (out != NULL) {
+                rc = fld_server_create(seq->lss_site->ms_server_fld,
+                                       env, out, th);
+                if (rc) {
+                        CERROR("%s: Can't Update fld database, rc %d\n",
+                               seq->lss_name, rc);
+                        GOTO(exit,rc);
+                }
         }
-       
-       RETURN(rc);
+
+        /* next sequence update will need sync until this update is committed
+         * in case of sync operation this is not needed obviously */
+        if (!sync)
+                /* if callback can't be added then sync always */
+                sync = !!seq_update_cb_add(th, seq);
+
+        th->th_sync |= sync;
+exit:
+        dt_trans_stop(env, dt_dev, th);
+        return rc;
 }
 
 /*
@@ -129,8 +240,8 @@ int seq_store_read(struct lu_server_seq *seq,
         struct dt_object *dt_obj = seq->lss_obj;
         struct seq_thread_info *info;
         loff_t pos = 0;
-       int rc;
-       ENTRY;
+        int rc;
+        ENTRY;
 
         info = lu_context_key_get(&env->le_ctx, &seq_thread_key);
         LASSERT(info != NULL);
@@ -151,7 +262,7 @@ int seq_store_read(struct lu_server_seq *seq,
                 rc = -EIO;
         }
 
-       RETURN(rc);
+        RETURN(rc);
 }
 
 int seq_store_init(struct lu_server_seq *seq,
@@ -167,13 +278,13 @@ int seq_store_init(struct lu_server_seq *seq,
         name = seq->lss_type == LUSTRE_SEQ_SERVER ?
                 LUSTRE_SEQ_SRV_NAME : LUSTRE_SEQ_CTL_NAME;
 
-        dt_obj = dt_store_open(env, dt, name, &fid);
+        dt_obj = dt_store_open(env, dt, "", name, &fid);
         if (!IS_ERR(dt_obj)) {
                 seq->lss_obj = dt_obj;
-               rc = 0;
+                rc = 0;
         } else {
                 CERROR("%s: Can't find \"%s\" obj %d\n",
-                      seq->lss_name, name, (int)PTR_ERR(dt_obj));
+                       seq->lss_name, name, (int)PTR_ERR(dt_obj));
                 rc = PTR_ERR(dt_obj);
         }