Whamcloud - gitweb
LU-1187 mdt: Add pre_cleanup phase in MDT stack cleanup
[fs/lustre-release.git] / lustre / osp / osp_object.c
index 89bbf9f..695a31e 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2012, Intel, Inc.
+ * Copyright (c) 2012, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #include "osp_internal.h"
 
-static __u64 osp_object_assign_id(const struct lu_env *env,
-                                 struct osp_device *d, struct osp_object *o)
+static void osp_object_assign_fid(const struct lu_env *env,
+                                struct osp_device *d, struct osp_object *o)
 {
-       struct osp_thread_info  *osi = osp_env_info(env);
-       const struct lu_fid     *f = lu_object_fid(&o->opo_obj.do_lu);
+       struct osp_thread_info *osi = osp_env_info(env);
 
-       LASSERT(fid_is_zero(f));
+       LASSERT(fid_is_zero(lu_object_fid(&o->opo_obj.do_lu)));
        LASSERT(o->opo_reserved);
        o->opo_reserved = 0;
 
-       /* assign fid to anonymous object */
-       osi->osi_oi.oi_id = osp_precreate_get_id(d);
-       osi->osi_oi.oi_seq = FID_SEQ_OST_MDT0;
-       fid_ostid_unpack(&osi->osi_fid, &osi->osi_oi, d->opd_index);
-       lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
+       osp_precreate_get_fid(env, d, &osi->osi_fid);
 
-       return osi->osi_oi.oi_id;
+       lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
 }
 
 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
@@ -93,15 +88,25 @@ static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
         *
         * 2) send synchronous truncate RPC with just assigned id
         */
-       LASSERT(attr != NULL);
+
+       /* there are few places in MDD code still passing NULL
+        * XXX: to be fixed soon */
+       if (attr == NULL)
+               RETURN(0);
+
        if (attr->la_valid & LA_SIZE && attr->la_size > 0) {
                LASSERT(!dt_object_exists(dt));
-               osp_object_assign_id(env, d, o);
+               osp_object_assign_fid(env, d, o);
                rc = osp_object_truncate(env, dt, attr->la_size);
                if (rc)
                        RETURN(rc);
        }
 
+       if (o->opo_new) {
+               /* no need in logging for new objects being created */
+               RETURN(0);
+       }
+
        if (!(attr->la_valid & (LA_UID | LA_GID)))
                RETURN(0);
 
@@ -126,6 +131,15 @@ static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
        if (!(attr->la_valid & (LA_UID | LA_GID)))
                RETURN(0);
 
+       /* new object, the very first ->attr_set()
+        * initializing attributes needs no logging
+        * all subsequent one are subject to the
+        * logging and synchronization with OST */
+       if (o->opo_new) {
+               o->opo_new = 0;
+               RETURN(0);
+       }
+
        /*
         * once transaction is committed put proper command on
         * the queue going to our OST
@@ -152,18 +166,27 @@ static int osp_declare_object_create(const struct lu_env *env,
 
        ENTRY;
 
-       LASSERT(d->opd_last_used_file);
+       /* should happen to non-0 OSP only so that at least one object
+        * has been already declared in the scenario and LOD should
+        * cleanup that */
+       if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
+               RETURN(-ENOSPC);
+
+       LASSERT(d->opd_last_used_oid_file);
        fid = lu_object_fid(&dt->do_lu);
 
        /*
         * There can be gaps in precreated ids and record to unlink llog
+        * XXX: we do not handle gaps yet, implemented before solution
+        *      was found to be racy, so we disabled that. there is no
+        *      point in making useless but expensive llog declaration.
         */
-       rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th);
+       /* rc = osp_sync_declare_add(env, o, MDS_UNLINK64_REC, th); */
 
        if (unlikely(!fid_is_zero(fid))) {
                /* replay case: caller knows fid */
                osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
-               rc = dt_declare_record_write(env, d->opd_last_used_file,
+               rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
                                             sizeof(osi->osi_id), osi->osi_off,
                                             th);
                RETURN(rc);
@@ -187,12 +210,12 @@ static int osp_declare_object_create(const struct lu_env *env,
 
                /* common for all OSPs file hystorically */
                osi->osi_off = sizeof(osi->osi_id) * d->opd_index;
-               rc = dt_declare_record_write(env, d->opd_last_used_file,
+               rc = dt_declare_record_write(env, d->opd_last_used_oid_file,
                                             sizeof(osi->osi_id), osi->osi_off,
                                             th);
        } else {
                /* not needed in the cache anymore */
-               cfs_set_bit(LU_OBJECT_HEARD_BANSHEE,
+               set_bit(LU_OBJECT_HEARD_BANSHEE,
                            &dt->do_lu.lo_header->loh_flags);
        }
        RETURN(rc);
@@ -206,25 +229,36 @@ static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
        struct osp_thread_info  *osi = osp_env_info(env);
        struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
        struct osp_object       *o = dt2osp_obj(dt);
-       int                      rc = 0;
-
+       int                     rc = 0;
+       struct lu_fid           *fid = &osi->osi_fid;
        ENTRY;
 
        if (o->opo_reserved) {
-               /* regular case, id is assigned holding transaction open */
-               osi->osi_id = osp_object_assign_id(env, d, o);
-       } else {
+               /* regular case, fid is assigned holding trunsaction open */
+                osp_object_assign_fid(env, d, o);
+       }
+
+       memcpy(fid, lu_object_fid(&dt->do_lu), sizeof(*fid));
+
+       LASSERTF(fid_is_sane(fid), "fid for osp_obj %p is insane"DFID"!\n",
+                osp_obj, PFID(fid));
+
+       if (!o->opo_reserved) {
                /* special case, id was assigned outside of transaction
                 * see comments in osp_declare_attr_set */
-               rc = fid_ostid_pack(lu_object_fid(&dt->do_lu), &osi->osi_oi);
-               LASSERT(rc == 0);
-               osi->osi_id = ostid_id(&osi->osi_oi);
-               cfs_spin_lock(&d->opd_pre_lock);
-               osp_update_last_id(d, osi->osi_id);
-               cfs_spin_unlock(&d->opd_pre_lock);
+               spin_lock(&d->opd_pre_lock);
+               osp_update_last_fid(d, fid);
+               spin_unlock(&d->opd_pre_lock);
        }
 
-       LASSERT(osi->osi_id);
+       CDEBUG(D_INODE, "fid for osp_obj %p is "DFID"!\n", osp_obj, PFID(fid));
+
+       /* If the precreate ends, it means it will be ready to rollover to
+        * the new sequence soon, all the creation should be synchronized,
+        * otherwise during replay, the replay fid will be inconsistent with
+        * last_used/create fid */
+       if (osp_precreate_end_seq(env, d) && osp_is_fid_client(d))
+               th->th_sync = 1;
 
        /*
         * it's OK if the import is inactive by this moment - id was created
@@ -235,27 +269,37 @@ static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
 
        /* we might have lost precreated objects */
        if (unlikely(d->opd_gap_count) > 0) {
-               cfs_spin_lock(&d->opd_pre_lock);
+               spin_lock(&d->opd_pre_lock);
                if (d->opd_gap_count > 0) {
                        int count = d->opd_gap_count;
-
-                       osi->osi_oi.oi_id = d->opd_gap_start;
+                       osi->osi_oi.oi_id = fid_oid(&d->opd_gap_start_fid);
                        d->opd_gap_count = 0;
-                       cfs_spin_unlock(&d->opd_pre_lock);
+                       spin_unlock(&d->opd_pre_lock);
 
-                       CDEBUG(D_HA, "Found gap "LPU64"+%d in objids\n",
-                              d->opd_gap_start, count);
+                       CDEBUG(D_HA, "Writting gap "DFID"+%d in llog\n",
+                              PFID(&d->opd_gap_start_fid), count);
                        /* real gap handling is disabled intil ORI-692 will be
                         * fixed, now we only report gaps */
                } else {
-                       cfs_spin_unlock(&d->opd_pre_lock);
+                       spin_unlock(&d->opd_pre_lock);
                }
        }
 
-       osp_objid_buf_prep(osi, d, d->opd_index);
-       rc = dt_record_write(env, d->opd_last_used_file, &osi->osi_lb,
+       /* new object, the very first ->attr_set()
+        * initializing attributes needs no logging */
+       o->opo_new = 1;
+
+       /* Only need update last_used oid file, seq file will only be update
+        * during seq rollover */
+       osp_objid_buf_prep(&osi->osi_lb, &osi->osi_off,
+                          &d->opd_last_used_fid.f_oid, d->opd_index);
+
+       rc = dt_record_write(env, d->opd_last_used_oid_file, &osi->osi_lb,
                             &osi->osi_off, th);
 
+       CDEBUG(D_HA, "%s: Wrote last used FID: "DFID", index %d: %d\n",
+              d->opd_obd->obd_name, PFID(fid), d->opd_index, rc);
+
        RETURN(rc);
 }
 
@@ -290,7 +334,7 @@ static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
        rc = osp_sync_add(env, o, MDS_UNLINK64_REC, th, NULL);
 
        /* not needed in cache any more */
-       cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
+       set_bit(LU_OBJECT_HEARD_BANSHEE, &dt->do_lu.lo_header->loh_flags);
 
        RETURN(rc);
 }
@@ -304,14 +348,39 @@ struct dt_object_operations osp_obj_ops = {
        .do_destroy             = osp_object_destroy,
 };
 
-static int osp_object_init(const struct lu_env *env, struct lu_object *o,
-                          const struct lu_object_conf *unused)
+static int is_ost_obj(struct lu_object *lo)
 {
-       struct osp_object *po = lu2osp_obj(o);
+       struct osp_device  *osp  = lu2osp_dev(lo->lo_dev);
+
+       return !osp->opd_connect_mdt;
+}
 
-       po->opo_obj.do_ops = &osp_obj_ops;
+static int osp_object_init(const struct lu_env *env, struct lu_object *o,
+                          const struct lu_object_conf *conf)
+{
+       struct osp_object       *po = lu2osp_obj(o);
+       int                     rc = 0;
+       ENTRY;
 
-       return 0;
+       if (is_ost_obj(o)) {
+               po->opo_obj.do_ops = &osp_obj_ops;
+       } else {
+               struct lu_attr          *la = &osp_env_info(env)->osi_attr;
+
+               po->opo_obj.do_ops = &osp_md_obj_ops;
+               o->lo_header->loh_attr |= LOHA_REMOTE;
+               /* Do not need get attr for new object */
+               if (!(conf != NULL && (conf->loc_flags & LOC_F_NEW) != 0)) {
+                       rc = po->opo_obj.do_ops->do_attr_get(env, lu2dt_obj(o),
+                                                            la, NULL);
+                       if (rc == 0)
+                               o->lo_header->loh_attr |=
+                                       LOHA_EXISTS | (la->la_mode & S_IFMT);
+                       if (rc == -ENOENT)
+                               rc = 0;
+               }
+       }
+       RETURN(rc);
 }
 
 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
@@ -337,12 +406,12 @@ static void osp_object_release(const struct lu_env *env, struct lu_object *o)
         */
        if (unlikely(po->opo_reserved)) {
                LASSERT(d->opd_pre_reserved > 0);
-               cfs_spin_lock(&d->opd_pre_lock);
+               spin_lock(&d->opd_pre_lock);
                d->opd_pre_reserved--;
-               cfs_spin_unlock(&d->opd_pre_lock);
+               spin_unlock(&d->opd_pre_lock);
 
                /* not needed in cache any more */
-               cfs_set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
+               set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
        }
        EXIT;
 }