Whamcloud - gitweb
LU-1267 lfsck: framework (3) for MDT-OST consistency
[fs/lustre-release.git] / lustre / osp / osp_object.c
index d2bcde5..f474f43 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, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
  * Author: Mikhail Pershin <mike.tappro@intel.com>
  */
 
-#ifndef EXPORT_SYMTAB
-# define EXPORT_SYMTAB
-#endif
 #define DEBUG_SUBSYSTEM S_MDS
 
 #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,
@@ -99,9 +91,10 @@ static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
        if (attr == NULL)
                RETURN(0);
 
-       if (attr->la_valid & LA_SIZE && attr->la_size > 0) {
+       if (attr->la_valid & LA_SIZE && attr->la_size > 0 &&
+           fid_is_zero(lu_object_fid(&o->opo_obj.do_lu))) {
                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);
@@ -177,7 +170,7 @@ static int osp_declare_object_create(const struct lu_env *env,
        if (OBD_FAIL_CHECK(OBD_FAIL_MDS_OSC_CREATE_FAIL) && d->opd_index == 1)
                RETURN(-ENOSPC);
 
-       LASSERT(d->opd_last_used_file);
+       LASSERT(d->opd_last_used_oid_file);
        fid = lu_object_fid(&dt->do_lu);
 
        /*
@@ -191,7 +184,7 @@ static int osp_declare_object_create(const struct lu_env *env,
        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);
@@ -215,12 +208,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);
@@ -234,25 +227,37 @@ 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_object %p is insane"DFID"!\n",
+                o, 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);
+               LASSERT(d->opd_pre != NULL);
+               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_object %p is "DFID"\n", o, 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
@@ -263,20 +268,22 @@ 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);
+               LASSERT(d->opd_pre != NULL);
+               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;
+                       ostid_set_id(&osi->osi_oi,
+                                    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);
                }
        }
 
@@ -284,10 +291,17 @@ static int osp_object_create(const struct lu_env *env, struct dt_object *dt,
         * initializing attributes needs no logging */
        o->opo_new = 1;
 
-       osp_objid_buf_prep(osi, d, d->opd_index);
-       rc = dt_record_write(env, d->opd_last_used_file, &osi->osi_lb,
+       /* 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);
 }
 
@@ -322,7 +336,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);
 }
@@ -336,14 +350,36 @@ 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;
+               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)
@@ -368,14 +404,23 @@ static void osp_object_release(const struct lu_env *env, struct lu_object *o)
         * this may require lu_object_put() in LOD
         */
        if (unlikely(po->opo_reserved)) {
+               LASSERT(d->opd_pre != NULL);
                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);
        }
+
+       if (is_ost_obj(o))
+               /* XXX: Currently, NOT cache OST-object on MDT because:
+                *      1. it is not often accessed on MDT.
+                *      2. avoid up layer (such as LFSCK) to load too many
+                *         once-used OST-objects. */
+               set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
+
        EXIT;
 }