Whamcloud - gitweb
LU-3594 lfsck: repair inconsistent owner and multiple referenced cases
[fs/lustre-release.git] / lustre / osp / osp_object.c
index d2bcde5..aaf7546 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 inline bool is_ost_obj(struct lu_object *lo)
 {
-       struct osp_thread_info  *osi = osp_env_info(env);
-       const struct lu_fid     *f = lu_object_fid(&o->opo_obj.do_lu);
+       return !lu2osp_dev(lo->lo_dev)->opd_connect_mdt;
+}
+
+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);
 
-       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);
+       osp_precreate_get_fid(env, d, &osi->osi_fid);
+
        lu_object_assign_fid(env, &o->opo_obj.do_lu, &osi->osi_fid);
+}
+
+static int osp_oac_init(struct osp_object *obj)
+{
+       struct osp_object_attr *ooa;
+
+       OBD_ALLOC_PTR(ooa);
+       if (ooa == NULL)
+               return -ENOMEM;
+
+       INIT_LIST_HEAD(&ooa->ooa_xattr_list);
+       spin_lock(&obj->opo_lock);
+       if (likely(obj->opo_ooa == NULL)) {
+               obj->opo_ooa = ooa;
+               spin_unlock(&obj->opo_lock);
+       } else {
+               spin_unlock(&obj->opo_lock);
+               OBD_FREE_PTR(ooa);
+       }
+
+       return 0;
+}
+
+static struct osp_xattr_entry *
+osp_oac_xattr_find_locked(struct osp_object_attr *ooa,
+                         const char *name, int namelen, bool unlink)
+{
+       struct osp_xattr_entry *oxe;
+
+       list_for_each_entry(oxe, &ooa->ooa_xattr_list, oxe_list) {
+               if (namelen == oxe->oxe_namelen &&
+                   strncmp(name, oxe->oxe_buf, namelen) == 0) {
+                       if (unlink)
+                               list_del_init(&oxe->oxe_list);
+                       else
+                               atomic_inc(&oxe->oxe_ref);
+
+                       return oxe;
+               }
+       }
+
+       return NULL;
+}
+
+static struct osp_xattr_entry *osp_oac_xattr_find(struct osp_object *obj,
+                                                 const char *name)
+{
+       struct osp_xattr_entry *oxe = NULL;
+
+       spin_lock(&obj->opo_lock);
+       if (obj->opo_ooa != NULL)
+               oxe = osp_oac_xattr_find_locked(obj->opo_ooa, name,
+                                               strlen(name), false);
+       spin_unlock(&obj->opo_lock);
+
+       return oxe;
+}
+
+static struct osp_xattr_entry *
+osp_oac_xattr_find_or_add(struct osp_object *obj, const char *name, int len)
+{
+       struct osp_object_attr *ooa     = obj->opo_ooa;
+       struct osp_xattr_entry *oxe;
+       struct osp_xattr_entry *tmp     = NULL;
+       int                     namelen = strlen(name);
+       int                     size    = sizeof(*oxe) + namelen + 1 + len;
+
+       LASSERT(ooa != NULL);
+
+       oxe = osp_oac_xattr_find(obj, name);
+       if (oxe != NULL)
+               return oxe;
+
+       OBD_ALLOC(oxe, size);
+       if (unlikely(oxe == NULL))
+               return NULL;
+
+       INIT_LIST_HEAD(&oxe->oxe_list);
+       oxe->oxe_buflen = size;
+       oxe->oxe_namelen = namelen;
+       memcpy(oxe->oxe_buf, name, namelen);
+       oxe->oxe_value = oxe->oxe_buf + namelen + 1;
+       /* One ref is for the caller, the other is for the entry on the list. */
+       atomic_set(&oxe->oxe_ref, 2);
+
+       spin_lock(&obj->opo_lock);
+       tmp = osp_oac_xattr_find_locked(ooa, name, namelen, false);
+       if (tmp == NULL)
+               list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
+       spin_unlock(&obj->opo_lock);
+
+       if (tmp != NULL) {
+               OBD_FREE(oxe, size);
+               oxe = tmp;
+       }
+
+       return oxe;
+}
+
+static struct osp_xattr_entry *
+osp_oac_xattr_replace(struct osp_object *obj,
+                     struct osp_xattr_entry **poxe, int len)
+{
+       struct osp_object_attr *ooa     = obj->opo_ooa;
+       struct osp_xattr_entry *old     = *poxe;
+       struct osp_xattr_entry *oxe;
+       struct osp_xattr_entry *tmp     = NULL;
+       int                     namelen = old->oxe_namelen;
+       int                     size    = sizeof(*oxe) + namelen + 1 + len;
+
+       LASSERT(ooa != NULL);
+
+       OBD_ALLOC(oxe, size);
+       if (unlikely(oxe == NULL))
+               return NULL;
+
+       INIT_LIST_HEAD(&oxe->oxe_list);
+       oxe->oxe_buflen = size;
+       oxe->oxe_namelen = namelen;
+       memcpy(oxe->oxe_buf, old->oxe_buf, namelen);
+       oxe->oxe_value = oxe->oxe_buf + namelen + 1;
+       /* One ref is for the caller, the other is for the entry on the list. */
+       atomic_set(&oxe->oxe_ref, 2);
+
+       spin_lock(&obj->opo_lock);
+       tmp = osp_oac_xattr_find_locked(ooa, oxe->oxe_buf, namelen, true);
+       list_add_tail(&oxe->oxe_list, &ooa->ooa_xattr_list);
+       spin_unlock(&obj->opo_lock);
+
+       *poxe = tmp;
+       LASSERT(tmp != NULL);
+
+       return oxe;
+}
+
+static inline void osp_oac_xattr_put(struct osp_xattr_entry *oxe)
+{
+       if (atomic_dec_and_test(&oxe->oxe_ref)) {
+               LASSERT(list_empty(&oxe->oxe_list));
+
+               OBD_FREE(oxe, oxe->oxe_buflen);
+       }
+}
+
+static int osp_get_attr_from_reply(const struct lu_env *env,
+                                  struct update_reply *reply,
+                                  struct lu_attr *attr,
+                                  struct osp_object *obj, int index)
+{
+       struct osp_thread_info  *osi    = osp_env_info(env);
+       struct lu_buf           *rbuf   = &osi->osi_lb2;
+       struct obdo             *lobdo  = &osi->osi_obdo;
+       struct obdo             *wobdo;
+       int                      rc;
+
+       rc = update_get_reply_buf(reply, rbuf, index);
+       if (rc < 0)
+               return rc;
+
+       wobdo = rbuf->lb_buf;
+       if (rbuf->lb_len != sizeof(*wobdo))
+               return -EPROTO;
+
+       obdo_le_to_cpu(wobdo, wobdo);
+       lustre_get_wire_obdo(NULL, lobdo, wobdo);
+       spin_lock(&obj->opo_lock);
+       if (obj->opo_ooa != NULL) {
+               la_from_obdo(&obj->opo_ooa->ooa_attr, lobdo, lobdo->o_valid);
+               if (attr != NULL)
+                       *attr = obj->opo_ooa->ooa_attr;
+       } else {
+               LASSERT(attr != NULL);
+
+               la_from_obdo(attr, lobdo, lobdo->o_valid);
+       }
+       spin_unlock(&obj->opo_lock);
+
+       return 0;
+}
+
+static int osp_attr_get_interpterer(const struct lu_env *env,
+                                   struct update_reply *reply,
+                                   struct osp_object *obj,
+                                   void *data, int index, int rc)
+{
+       struct lu_attr *attr = data;
+
+       LASSERT(obj->opo_ooa != NULL);
+
+       if (rc == 0) {
+               osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
+               obj->opo_non_exist = 0;
+
+               return osp_get_attr_from_reply(env, reply, NULL, obj, index);
+       } else {
+               if (rc == -ENOENT) {
+                       osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
+                       obj->opo_non_exist = 1;
+               }
+
+               spin_lock(&obj->opo_lock);
+               attr->la_valid = 0;
+               spin_unlock(&obj->opo_lock);
+       }
+
+       return 0;
+}
+
+static int osp_declare_attr_get(const struct lu_env *env, struct dt_object *dt,
+                               struct lustre_capa *capa)
+{
+       struct osp_object       *obj    = dt2osp_obj(dt);
+       struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
+       struct update_request   *update;
+       int                      rc     = 0;
+
+       if (obj->opo_ooa == NULL) {
+               rc = osp_oac_init(obj);
+               if (rc != 0)
+                       return rc;
+       }
+
+       mutex_lock(&osp->opd_async_requests_mutex);
+       update = osp_find_or_create_async_update_request(osp);
+       if (IS_ERR(update))
+               rc = PTR_ERR(update);
+       else
+               rc = osp_insert_async_update(env, update, OBJ_ATTR_GET, obj, 0,
+                                            NULL, NULL,
+                                            &obj->opo_ooa->ooa_attr,
+                                            osp_attr_get_interpterer);
+       mutex_unlock(&osp->opd_async_requests_mutex);
+
+       return rc;
+}
+
+int osp_attr_get(const struct lu_env *env, struct dt_object *dt,
+                struct lu_attr *attr, struct lustre_capa *capa)
+{
+       struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
+       struct osp_object       *obj    = dt2osp_obj(dt);
+       struct dt_device        *dev    = &osp->opd_dt_dev;
+       struct update_request   *update;
+       struct update_reply     *reply;
+       struct ptlrpc_request   *req    = NULL;
+       int                      rc     = 0;
+       ENTRY;
+
+       if (is_ost_obj(&dt->do_lu) && obj->opo_non_exist)
+               RETURN(-ENOENT);
+
+       if (obj->opo_ooa != NULL) {
+               spin_lock(&obj->opo_lock);
+               if (obj->opo_ooa->ooa_attr.la_valid != 0) {
+                       *attr = obj->opo_ooa->ooa_attr;
+                       spin_unlock(&obj->opo_lock);
+
+                       RETURN(0);
+               }
+               spin_unlock(&obj->opo_lock);
+       }
+
+       update = out_create_update_req(dev);
+       if (IS_ERR(update))
+               RETURN(PTR_ERR(update));
+
+       rc = out_insert_update(env, update, OBJ_ATTR_GET,
+                              lu_object_fid(&dt->do_lu), 0, NULL, NULL);
+       if (rc != 0) {
+               CERROR("%s: Insert update error "DFID": rc = %d\n",
+                      dev->dd_lu_dev.ld_obd->obd_name,
+                      PFID(lu_object_fid(&dt->do_lu)), rc);
+
+               GOTO(out, rc);
+       }
+
+       rc = out_remote_sync(env, osp->opd_obd->u.cli.cl_import, update, &req);
+       if (rc != 0) {
+               if (rc == -ENOENT) {
+                       osp2lu_obj(obj)->lo_header->loh_attr &= ~LOHA_EXISTS;
+                       obj->opo_non_exist = 1;
+               } else {
+                       CERROR("%s:osp_attr_get update error "DFID": rc = %d\n",
+                              dev->dd_lu_dev.ld_obd->obd_name,
+                              PFID(lu_object_fid(&dt->do_lu)), rc);
+               }
+
+               GOTO(out, rc);
+       }
+
+       osp2lu_obj(obj)->lo_header->loh_attr |= LOHA_EXISTS;
+       obj->opo_non_exist = 0;
+       reply = req_capsule_server_sized_get(&req->rq_pill, &RMF_UPDATE_REPLY,
+                                            UPDATE_BUFFER_SIZE);
+       if (reply == NULL || reply->ur_version != UPDATE_REPLY_V1)
+               GOTO(out, rc = -EPROTO);
+
+       rc = osp_get_attr_from_reply(env, reply, attr, obj, 0);
+       if (rc != 0)
+               GOTO(out, rc);
+
+       if (!is_ost_obj(&dt->do_lu)) {
+               if (attr->la_flags == 1)
+                       obj->opo_empty = 0;
+               else
+                       obj->opo_empty = 1;
+       }
+
+       GOTO(out, rc = 0);
 
-       return osi->osi_oi.oi_id;
+out:
+       if (req != NULL)
+               ptlrpc_req_finished(req);
+
+       out_destroy_update_req(update);
+
+       return rc;
 }
 
 static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
@@ -72,6 +386,7 @@ static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
 {
        struct osp_device       *d = lu2osp_dev(dt->do_lu.lo_dev);
        struct osp_object       *o = dt2osp_obj(dt);
+       struct lu_attr          *la;
        int                      rc = 0;
 
        ENTRY;
@@ -99,9 +414,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);
@@ -115,12 +431,33 @@ static int osp_declare_attr_set(const struct lu_env *env, struct dt_object *dt,
        if (!(attr->la_valid & (LA_UID | LA_GID)))
                RETURN(0);
 
-       /*
-        * track all UID/GID changes via llog
-        */
-       rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
+       if (!is_only_remote_trans(th))
+               /*
+                * track all UID/GID changes via llog
+                */
+               rc = osp_sync_declare_add(env, o, MDS_SETATTR64_REC, th);
+       else
+               /* It is for OST-object attr_set directly without updating
+                * local MDT-object attribute. It is usually used by LFSCK. */
+               rc = osp_md_declare_attr_set(env, dt, attr, th);
+
+       if (rc != 0 || o->opo_ooa == NULL)
+               RETURN(rc);
 
-       RETURN(rc);
+       la = &o->opo_ooa->ooa_attr;
+       spin_lock(&o->opo_lock);
+       if (attr->la_valid & LA_UID) {
+               la->la_uid = attr->la_uid;
+               la->la_valid |= LA_UID;
+       }
+
+       if (attr->la_valid & LA_GID) {
+               la->la_gid = attr->la_gid;
+               la->la_valid |= LA_GID;
+       }
+       spin_unlock(&o->opo_lock);
+
+       RETURN(0);
 }
 
 static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
@@ -145,17 +482,388 @@ static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
                RETURN(0);
        }
 
-       /*
-        * once transaction is committed put proper command on
-        * the queue going to our OST
-        */
-       rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
-
-       /* XXX: send new uid/gid to OST ASAP? */
+       if (!is_only_remote_trans(th))
+               /*
+                * once transaction is committed put proper command on
+                * the queue going to our OST
+                */
+               rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
+               /* XXX: send new uid/gid to OST ASAP? */
+       else
+               /* It is for OST-object attr_set directly without updating
+                * local MDT-object attribute. It is usually used by LFSCK. */
+               rc = osp_md_attr_set(env, dt, attr, th, capa);
 
        RETURN(rc);
 }
 
+static int osp_xattr_get_interpterer(const struct lu_env *env,
+                                    struct update_reply *reply,
+                                    struct osp_object *obj,
+                                    void *data, int index, int rc)
+{
+       struct osp_object_attr  *ooa  = obj->opo_ooa;
+       struct osp_xattr_entry  *oxe  = data;
+       struct lu_buf           *rbuf = &osp_env_info(env)->osi_lb2;
+
+       LASSERT(ooa != NULL);
+
+       if (rc == 0) {
+               int len = sizeof(*oxe) + oxe->oxe_namelen + 1;
+
+               rc = update_get_reply_buf(reply, rbuf, index);
+               if (rc < 0 || rbuf->lb_len > (oxe->oxe_buflen - len)) {
+                       spin_lock(&obj->opo_lock);
+                       oxe->oxe_ready = 0;
+                       spin_unlock(&obj->opo_lock);
+                       osp_oac_xattr_put(oxe);
+
+                       return rc < 0 ? rc : -ERANGE;
+               }
+
+               spin_lock(&obj->opo_lock);
+               oxe->oxe_vallen = rbuf->lb_len;
+               memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
+               oxe->oxe_exist = 1;
+               oxe->oxe_ready = 1;
+               spin_unlock(&obj->opo_lock);
+       } else if (rc == -ENOENT || rc == -ENODATA) {
+               spin_lock(&obj->opo_lock);
+               oxe->oxe_exist = 0;
+               oxe->oxe_ready = 1;
+               spin_unlock(&obj->opo_lock);
+       } else {
+               spin_lock(&obj->opo_lock);
+               oxe->oxe_ready = 0;
+               spin_unlock(&obj->opo_lock);
+       }
+
+       osp_oac_xattr_put(oxe);
+
+       return 0;
+}
+
+static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
+                                struct lu_buf *buf, const char *name,
+                                struct lustre_capa *capa)
+{
+       struct osp_object       *obj     = dt2osp_obj(dt);
+       struct osp_device       *osp     = lu2osp_dev(dt->do_lu.lo_dev);
+       struct update_request   *update;
+       struct osp_xattr_entry  *oxe;
+       int                      namelen = strlen(name);
+       int                      rc      = 0;
+
+       LASSERT(buf != NULL);
+       LASSERT(name != NULL);
+
+       /* If only for xattr size, return directly. */
+       if (unlikely(buf->lb_len == 0))
+               return 0;
+
+       if (obj->opo_ooa == NULL) {
+               rc = osp_oac_init(obj);
+               if (rc != 0)
+                       return rc;
+       }
+
+       oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
+       if (oxe == NULL)
+               return -ENOMEM;
+
+       mutex_lock(&osp->opd_async_requests_mutex);
+       update = osp_find_or_create_async_update_request(osp);
+       if (IS_ERR(update)) {
+               rc = PTR_ERR(update);
+               mutex_unlock(&osp->opd_async_requests_mutex);
+               osp_oac_xattr_put(oxe);
+       } else {
+               rc = osp_insert_async_update(env, update, OBJ_XATTR_GET, obj,
+                                            1, &namelen, &name, oxe,
+                                            osp_xattr_get_interpterer);
+               if (rc != 0) {
+                       mutex_unlock(&osp->opd_async_requests_mutex);
+                       osp_oac_xattr_put(oxe);
+               } else {
+                       /* XXX: Currently, we trigger the batched async OUT
+                        *      RPC via dt_declare_xattr_get(). It is not
+                        *      perfect solution, but works well now.
+                        *
+                        *      We will improve it in the future. */
+                       update = osp->opd_async_requests;
+                       if (update != NULL && update->ur_buf != NULL &&
+                           update->ur_buf->ub_count > 0) {
+                               osp->opd_async_requests = NULL;
+                               mutex_unlock(&osp->opd_async_requests_mutex);
+                               rc = osp_unplug_async_update(env, osp, update);
+                       } else {
+                               mutex_unlock(&osp->opd_async_requests_mutex);
+                       }
+               }
+       }
+
+       return rc;
+}
+
+int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
+                 struct lu_buf *buf, const char *name,
+                 struct lustre_capa *capa)
+{
+       struct osp_device       *osp    = lu2osp_dev(dt->do_lu.lo_dev);
+       struct osp_object       *obj    = dt2osp_obj(dt);
+       struct dt_device        *dev    = &osp->opd_dt_dev;
+       struct lu_buf           *rbuf   = &osp_env_info(env)->osi_lb2;
+       struct update_request   *update = NULL;
+       struct ptlrpc_request   *req    = NULL;
+       struct update_reply     *reply;
+       struct osp_xattr_entry  *oxe    = NULL;
+       const char              *dname  = dt->do_lu.lo_dev->ld_obd->obd_name;
+       int                      namelen;
+       int                      rc     = 0;
+       ENTRY;
+
+       LASSERT(buf != NULL);
+       LASSERT(name != NULL);
+
+       if (unlikely(obj->opo_non_exist))
+               RETURN(-ENOENT);
+
+       oxe = osp_oac_xattr_find(obj, name);
+       if (oxe != NULL) {
+               spin_lock(&obj->opo_lock);
+               if (oxe->oxe_ready) {
+                       if (!oxe->oxe_exist)
+                               GOTO(unlock, rc = -ENODATA);
+
+                       if (buf->lb_buf == NULL)
+                               GOTO(unlock, rc = oxe->oxe_vallen);
+
+                       if (buf->lb_len < oxe->oxe_vallen)
+                               GOTO(unlock, rc = -ERANGE);
+
+                       memcpy(buf->lb_buf, oxe->oxe_value, oxe->oxe_vallen);
+
+                       GOTO(unlock, rc = oxe->oxe_vallen);
+
+unlock:
+                       spin_unlock(&obj->opo_lock);
+                       osp_oac_xattr_put(oxe);
+
+                       return rc;
+               }
+               spin_unlock(&obj->opo_lock);
+       }
+
+       update = out_create_update_req(dev);
+       if (IS_ERR(update))
+               GOTO(out, rc = PTR_ERR(update));
+
+       namelen = strlen(name);
+       rc = out_insert_update(env, update, OBJ_XATTR_GET,
+                              lu_object_fid(&dt->do_lu), 1, &namelen, &name);
+       if (rc != 0) {
+               CERROR("%s: Insert update error "DFID": rc = %d\n",
+                      dname, PFID(lu_object_fid(&dt->do_lu)), rc);
+
+               GOTO(out, rc);
+       }
+
+       rc = out_remote_sync(env, osp->opd_obd->u.cli.cl_import, update, &req);
+       if (rc != 0) {
+               if (obj->opo_ooa == NULL)
+                       GOTO(out, rc);
+
+               if (oxe == NULL)
+                       oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
+
+               if (oxe == NULL) {
+                       CWARN("%s: Fail to add xattr (%s) to cache for "
+                             DFID" (1): rc = %d\n", dname, name,
+                             PFID(lu_object_fid(&dt->do_lu)), rc);
+
+                       GOTO(out, rc);
+               }
+
+               spin_lock(&obj->opo_lock);
+               if (rc == -ENOENT || rc == -ENODATA) {
+                       oxe->oxe_exist = 0;
+                       oxe->oxe_ready = 1;
+               } else {
+                       oxe->oxe_ready = 0;
+               }
+               spin_unlock(&obj->opo_lock);
+
+               GOTO(out, rc);
+       }
+
+       reply = req_capsule_server_sized_get(&req->rq_pill, &RMF_UPDATE_REPLY,
+                                           UPDATE_BUFFER_SIZE);
+       if (reply->ur_version != UPDATE_REPLY_V1) {
+               CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
+                      dname, reply->ur_version, UPDATE_REPLY_V1,
+                      PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
+
+               GOTO(out, rc = -EPROTO);
+       }
+
+       rc = update_get_reply_buf(reply, rbuf, 0);
+       if (rc < 0)
+               GOTO(out, rc);
+
+       LASSERT(rbuf->lb_len > 0 && rbuf->lb_len < PAGE_CACHE_SIZE);
+
+       if (buf->lb_buf == NULL)
+               GOTO(out, rc = rbuf->lb_len);
+
+       if (unlikely(buf->lb_len < rbuf->lb_len))
+               GOTO(out, rc = -ERANGE);
+
+       memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
+       rc = rbuf->lb_len;
+       if (obj->opo_ooa == NULL)
+               GOTO(out, rc);
+
+       if (oxe == NULL) {
+               oxe = osp_oac_xattr_find_or_add(obj, name, rbuf->lb_len);
+               if (oxe == NULL) {
+                       CWARN("%s: Fail to add xattr (%s) to "
+                             "cache for "DFID" (2): rc = %d\n",
+                             dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
+
+                       GOTO(out, rc);
+               }
+       }
+
+       if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < rbuf->lb_len) {
+               struct osp_xattr_entry *old = oxe;
+               struct osp_xattr_entry *tmp;
+
+               tmp = osp_oac_xattr_replace(obj, &old, rbuf->lb_len);
+               osp_oac_xattr_put(oxe);
+               oxe = tmp;
+               if (tmp == NULL) {
+                       CWARN("%s: Fail to update xattr (%s) to "
+                             "cache for "DFID": rc = %d\n",
+                             dname, name, PFID(lu_object_fid(&dt->do_lu)), rc);
+                       spin_lock(&obj->opo_lock);
+                       oxe->oxe_ready = 0;
+                       spin_unlock(&obj->opo_lock);
+
+                       GOTO(out, rc);
+               }
+
+               /* Drop the ref for entry on list. */
+               osp_oac_xattr_put(old);
+       }
+
+       spin_lock(&obj->opo_lock);
+       oxe->oxe_vallen = rbuf->lb_len;
+       memcpy(oxe->oxe_value, rbuf->lb_buf, rbuf->lb_len);
+       oxe->oxe_exist = 1;
+       oxe->oxe_ready = 1;
+       spin_unlock(&obj->opo_lock);
+
+       GOTO(out, rc);
+
+out:
+       if (req != NULL)
+               ptlrpc_req_finished(req);
+
+       if (update != NULL && !IS_ERR(update))
+               out_destroy_update_req(update);
+
+       if (oxe != NULL)
+               osp_oac_xattr_put(oxe);
+
+       return rc;
+}
+
+int osp_declare_xattr_set(const struct lu_env *env, struct dt_object *dt,
+                         const struct lu_buf *buf, const char *name,
+                         int flag, struct thandle *th)
+{
+       struct osp_object       *o       = dt2osp_obj(dt);
+       struct update_request   *update;
+       struct lu_fid           *fid;
+       struct osp_xattr_entry  *oxe;
+       int                     sizes[3] = {strlen(name), buf->lb_len,
+                                           sizeof(int)};
+       char                    *bufs[3] = {(char *)name, (char *)buf->lb_buf };
+       int                     rc;
+
+       LASSERT(buf->lb_len > 0 && buf->lb_buf != NULL);
+
+       update = out_find_create_update_loc(th, dt);
+       if (IS_ERR(update)) {
+               CERROR("%s: Get OSP update buf failed "DFID": rc = %d\n",
+                      dt->do_lu.lo_dev->ld_obd->obd_name,
+                      PFID(lu_object_fid(&dt->do_lu)),
+                      (int)PTR_ERR(update));
+
+               return PTR_ERR(update);
+       }
+
+       flag = cpu_to_le32(flag);
+       bufs[2] = (char *)&flag;
+
+       fid = (struct lu_fid *)lu_object_fid(&dt->do_lu);
+       rc = out_insert_update(env, update, OBJ_XATTR_SET, fid,
+                              ARRAY_SIZE(sizes), sizes, (const char **)bufs);
+       if (rc != 0 || o->opo_ooa == NULL)
+               return rc;
+
+       oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
+       if (oxe == NULL) {
+               CWARN("%s: Fail to add xattr (%s) to cache for "DFID
+                     ": rc = %d\n", dt->do_lu.lo_dev->ld_obd->obd_name,
+                     name, PFID(lu_object_fid(&dt->do_lu)), rc);
+
+               return 0;
+       }
+
+       if (oxe->oxe_buflen - oxe->oxe_namelen - 1 < buf->lb_len) {
+               struct osp_xattr_entry *old = oxe;
+               struct osp_xattr_entry *tmp;
+
+               tmp = osp_oac_xattr_replace(o, &old, buf->lb_len);
+               osp_oac_xattr_put(oxe);
+               oxe = tmp;
+               if (tmp == NULL) {
+                       CWARN("%s: Fail to update xattr (%s) to cache for "DFID
+                             ": rc = %d\n", dt->do_lu.lo_dev->ld_obd->obd_name,
+                             name, PFID(lu_object_fid(&dt->do_lu)), rc);
+                       spin_lock(&o->opo_lock);
+                       oxe->oxe_ready = 0;
+                       spin_unlock(&o->opo_lock);
+
+                       return 0;
+               }
+
+               /* Drop the ref for entry on list. */
+               osp_oac_xattr_put(old);
+       }
+
+       spin_lock(&o->opo_lock);
+       oxe->oxe_vallen = buf->lb_len;
+       memcpy(oxe->oxe_value, buf->lb_buf, buf->lb_len);
+       oxe->oxe_exist = 1;
+       oxe->oxe_ready = 1;
+       spin_unlock(&o->opo_lock);
+       osp_oac_xattr_put(oxe);
+
+       return 0;
+}
+
+int osp_xattr_set(const struct lu_env *env, struct dt_object *dt,
+                 const struct lu_buf *buf, const char *name, int fl,
+                 struct thandle *th, struct lustre_capa *capa)
+{
+       CDEBUG(D_INFO, "xattr %s set object "DFID"\n", name,
+              PFID(&dt->do_lu.lo_header->loh_fid));
+
+       return 0;
+}
+
 static int osp_declare_object_create(const struct lu_env *env,
                                     struct dt_object *dt,
                                     struct lu_attr *attr,
@@ -166,19 +874,26 @@ static int osp_declare_object_create(const struct lu_env *env,
        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);
-       const struct lu_fid     *fid;
+       const struct lu_fid     *fid = lu_object_fid(&dt->do_lu);
        int                      rc = 0;
 
        ENTRY;
 
+       if (is_only_remote_trans(th)) {
+               LASSERT(fid_is_sane(fid));
+
+               rc = osp_md_declare_object_create(env, dt, attr, hint, dof, th);
+
+               RETURN(rc);
+       }
+
        /* 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_file);
-       fid = lu_object_fid(&dt->do_lu);
+       LASSERT(d->opd_last_used_oid_file);
 
        /*
         * There can be gaps in precreated ids and record to unlink llog
@@ -191,7 +906,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 +930,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 +949,48 @@ 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 (is_only_remote_trans(th)) {
+               LASSERT(fid_is_sane(lu_object_fid(&dt->do_lu)));
+
+               rc = osp_md_object_create(env, dt, attr, hint, dof, th);
+               if (rc == 0)
+                       o->opo_non_exist = 0;
+
+               RETURN(rc);
+       }
+
+       o->opo_non_exist = 0;
        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 +1001,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,15 +1024,22 @@ 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);
 }
 
-static int osp_declare_object_destroy(const struct lu_env *env,
-                                     struct dt_object *dt, struct thandle *th)
+int osp_declare_object_destroy(const struct lu_env *env,
+                              struct dt_object *dt, struct thandle *th)
 {
        struct osp_object       *o = dt2osp_obj(dt);
        int                      rc = 0;
@@ -307,14 +1054,15 @@ static int osp_declare_object_destroy(const struct lu_env *env,
        RETURN(rc);
 }
 
-static int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
-                             struct thandle *th)
+int osp_object_destroy(const struct lu_env *env, struct dt_object *dt,
+                      struct thandle *th)
 {
        struct osp_object       *o = dt2osp_obj(dt);
        int                      rc = 0;
 
        ENTRY;
 
+       o->opo_non_exist = 1;
        /*
         * once transaction is committed put proper command on
         * the queue going to our OST
@@ -322,14 +1070,20 @@ 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);
 }
 
 struct dt_object_operations osp_obj_ops = {
+       .do_declare_attr_get    = osp_declare_attr_get,
+       .do_attr_get            = osp_attr_get,
        .do_declare_attr_set    = osp_declare_attr_set,
        .do_attr_set            = osp_attr_set,
+       .do_declare_xattr_get   = osp_declare_xattr_get,
+       .do_xattr_get           = osp_xattr_get,
+       .do_declare_xattr_set   = osp_declare_xattr_set,
+       .do_xattr_set           = osp_xattr_set,
        .do_declare_create      = osp_declare_object_create,
        .do_create              = osp_object_create,
        .do_declare_destroy     = osp_declare_object_destroy,
@@ -337,13 +1091,33 @@ struct dt_object_operations osp_obj_ops = {
 };
 
 static int osp_object_init(const struct lu_env *env, struct lu_object *o,
-                          const struct lu_object_conf *unused)
+                          const struct lu_object_conf *conf)
 {
-       struct osp_object *po = lu2osp_obj(o);
+       struct osp_object       *po = lu2osp_obj(o);
+       int                     rc = 0;
+       ENTRY;
 
-       po->opo_obj.do_ops = &osp_obj_ops;
+       spin_lock_init(&po->opo_lock);
+       o->lo_header->loh_attr |= LOHA_REMOTE;
 
-       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;
+               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) {
+                       po->opo_non_exist = 1;
+                       rc = 0;
+               }
+               init_rwsem(&po->opo_sem);
+       }
+       RETURN(rc);
 }
 
 static void osp_object_free(const struct lu_env *env, struct lu_object *o)
@@ -353,6 +1127,24 @@ static void osp_object_free(const struct lu_env *env, struct lu_object *o)
 
        dt_object_fini(&obj->opo_obj);
        lu_object_header_fini(h);
+       if (obj->opo_ooa != NULL) {
+               struct osp_xattr_entry *oxe;
+               struct osp_xattr_entry *tmp;
+               int                     count;
+
+               list_for_each_entry_safe(oxe, tmp,
+                                        &obj->opo_ooa->ooa_xattr_list,
+                                        oxe_list) {
+                       list_del(&oxe->oxe_list);
+                       count = atomic_read(&oxe->oxe_ref);
+                       LASSERTF(count == 1,
+                                "Still has %d users on the xattr entry %.*s\n",
+                                count - 1, oxe->oxe_namelen, oxe->oxe_buf);
+
+                       OBD_FREE(oxe, oxe->oxe_buflen);
+               }
+               OBD_FREE_PTR(obj->opo_ooa);
+       }
        OBD_SLAB_FREE_PTR(obj, osp_object_kmem);
 }
 
@@ -368,14 +1160,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;
 }