+ 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);
+
+ return 0;
+}
+
+/**
+ * Implement OSP layer dt_object_operations::do_attr_set() interface.
+ *
+ * Set attribute to the specified OST object.
+ *
+ * If the transaction is a remote one, then add OUT_ATTR_SET sub-request
+ * in the OUT RPC that will be flushed when the remote transaction stop.
+ * Otherwise, it will generate a MDS_SETATTR64_REC record in the llog that
+ * will be handled by a dedicated thread asynchronously.
+ *
+ * If the attribute entry exists in the OSP object attributes cache,
+ * then update the cached attribute according to given attribute.
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] dt pointer to the OSP layer dt_object
+ * \param[in] attr pointer to the attribute to be set
+ * \param[in] th pointer to the transaction handler
+ *
+ * \retval 0 for success
+ * \retval negative error number on failure
+ */
+static int osp_attr_set(const struct lu_env *env, struct dt_object *dt,
+ const struct lu_attr *attr, struct thandle *th)
+{
+ struct osp_object *o = dt2osp_obj(dt);
+ int rc = 0;
+ ENTRY;
+
+ /* we're interested in uid/gid changes only */
+ if (!(attr->la_valid & (LA_UID | LA_GID)))
+ RETURN(0);
+
+ if (!is_only_remote_trans(th)) {
+ rc = osp_sync_add(env, o, MDS_SETATTR64_REC, th, attr);
+ /* XXX: send new uid/gid to OST ASAP? */
+ } else {
+ struct lu_attr *la;
+
+ /* 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);
+ CDEBUG(D_INFO, "(1) set attr "DFID": rc = %d\n",
+ PFID(&dt->do_lu.lo_header->loh_fid), rc);
+
+ if (rc != 0)
+ RETURN(rc);
+
+ /* Update the OSP object attributes cache. */
+ la = &o->opo_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(rc);
+}
+
+/**
+ * Interpreter function for getting OSP object extended attribute asynchronously
+ *
+ * Called to interpret the result of an async mode RPC for getting the
+ * OSP object extended attribute.
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] reply pointer to the RPC reply
+ * \param[in] req pointer to the RPC request
+ * \param[in] obj pointer to the OSP object
+ * \param[out] data pointer to OSP object attributes cache
+ * \param[in] index the index of the attribute buffer in the reply
+ * \param[in] rc the result for handling the RPC
+ *
+ * \retval 0 for success
+ * \retval negative error number on failure
+ */
+static int osp_xattr_get_interpterer(const struct lu_env *env,
+ struct object_update_reply *reply,
+ struct ptlrpc_request *req,
+ struct osp_object *obj,
+ void *data, int index, int rc)
+{
+ struct osp_xattr_entry *oxe = data;
+ struct lu_buf *rbuf = &osp_env_info(env)->osi_lb2;
+
+ if (rc == 0) {
+ size_t len = sizeof(*oxe) + oxe->oxe_namelen + 1;
+
+ rc = object_update_result_data_get(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;
+}
+
+/**
+ * Implement OSP dt_object_operations::do_declare_xattr_get() interface.
+ *
+ * Declare that the caller will get extended attribute from the specified
+ * OST object.
+ *
+ * This function will add an OUT_XATTR_GET sub-request to the per OSP
+ * based shared asynchronous request queue with the interpreter function:
+ * osp_xattr_get_interpterer().
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] dt pointer to the OSP layer dt_object
+ * \param[out] buf pointer to the lu_buf to hold the extended attribute
+ * \param[in] name the name for the expected extended attribute
+ *
+ * \retval 0 for success
+ * \retval negative error number on failure
+ */
+static int osp_declare_xattr_get(const struct lu_env *env, struct dt_object *dt,
+ struct lu_buf *buf, const char *name)
+{
+ struct osp_object *obj = dt2osp_obj(dt);
+ struct osp_device *osp = lu2osp_dev(dt->do_lu.lo_dev);
+ struct osp_xattr_entry *oxe;
+ __u16 namelen;
+ int rc = 0;
+
+ LASSERT(buf != NULL);
+ LASSERT(name != NULL);
+
+ namelen = strlen(name);
+
+ /* If only for xattr size, return directly. */
+ if (unlikely(buf->lb_len == 0))
+ return 0;
+
+ oxe = osp_oac_xattr_find_or_add(obj, name, buf->lb_len);
+ if (oxe == NULL)
+ return -ENOMEM;
+
+ mutex_lock(&osp->opd_async_requests_mutex);
+ rc = osp_insert_async_request(env, OUT_XATTR_GET, obj, 1,
+ &namelen, (const void **)&name,
+ oxe, buf->lb_len,
+ osp_xattr_get_interpterer);
+ if (rc != 0) {
+ mutex_unlock(&osp->opd_async_requests_mutex);
+ osp_oac_xattr_put(oxe);
+ } else {
+ struct osp_update_request *our;
+ struct osp_update_request_sub *ours;
+
+ /* 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. */
+ our = osp->opd_async_requests;
+ ours = osp_current_object_update_request(our);
+ if (ours != NULL && ours->ours_req != NULL &&
+ ours->ours_req->ourq_count > 0) {
+ osp->opd_async_requests = NULL;
+ mutex_unlock(&osp->opd_async_requests_mutex);
+ rc = osp_unplug_async_request(env, osp, our);
+ } else {
+ mutex_unlock(&osp->opd_async_requests_mutex);
+ }
+ }
+
+ return rc;
+}
+
+/**
+ * Implement OSP layer dt_object_operations::do_xattr_get() interface.
+ *
+ * Get extended attribute from the specified MDT/OST object.
+ *
+ * If the extended attribute is in the OSP object attributes cache, then
+ * return the cached extended attribute directly. Otherwise it will get
+ * the extended attribute synchronously, if successful, add it to the OSP
+ * attributes cache. (\see lustre/osp/osp_trans.c for OUT RPC.)
+ *
+ * There is a race condition: some other thread has added the named extended
+ * attributed entry to the OSP object attributes cache during the current
+ * OUT_XATTR_GET handling. If such case happens, the OSP will replace the
+ * (just) existing extended attribute entry with the new replied one.
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] dt pointer to the OSP layer dt_object
+ * \param[out] buf pointer to the lu_buf to hold the extended attribute
+ * \param[in] name the name for the expected extended attribute
+ *
+ * \retval 0 for success
+ * \retval negative error number on failure
+ */
+int osp_xattr_get(const struct lu_env *env, struct dt_object *dt,
+ struct lu_buf *buf, const char *name)
+{
+ 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 osp_update_request *update = NULL;
+ struct ptlrpc_request *req = NULL;
+ struct object_update_reply *reply;
+ struct osp_xattr_entry *oxe = NULL;
+ const char *dname = dt->do_lu.lo_dev->ld_obd->obd_name;
+ int rc = 0;
+ ENTRY;
+
+ LASSERT(buf != NULL);
+ LASSERT(name != NULL);
+
+ if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_NETWORK) &&
+ osp->opd_index == cfs_fail_val) {
+ if (is_ost_obj(&dt->do_lu)) {
+ if (osp_dev2node(osp) == cfs_fail_val)
+ RETURN(-ENOTCONN);
+ } else {
+ if (strcmp(name, XATTR_NAME_LINK) == 0)
+ RETURN(-ENOTCONN);
+ }
+ }
+
+ if (unlikely(obj->opo_non_exist))
+ RETURN(-ENOENT);
+
+ oxe = osp_oac_xattr_find(obj, name, false);
+ 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 = osp_update_request_create(dev);
+ if (IS_ERR(update))
+ GOTO(out, rc = PTR_ERR(update));
+
+ rc = osp_update_rpc_pack(env, xattr_get, update, OUT_XATTR_GET,
+ lu_object_fid(&dt->do_lu), name, buf->lb_len);
+ 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 = osp_remote_sync(env, osp, update, &req);
+ if (rc < 0) {
+ if (rc == -ENOENT) {
+ dt->do_lu.lo_header->loh_attr &= ~LOHA_EXISTS;
+ obj->opo_non_exist = 1;
+ }
+
+ 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_OUT_UPDATE_REPLY,
+ OUT_UPDATE_REPLY_SIZE);
+ if (reply->ourp_magic != UPDATE_REPLY_MAGIC) {
+ CERROR("%s: Wrong version %x expected %x "DFID": rc = %d\n",
+ dname, reply->ourp_magic, UPDATE_REPLY_MAGIC,
+ PFID(lu_object_fid(&dt->do_lu)), -EPROTO);
+
+ GOTO(out, rc = -EPROTO);
+ }
+
+ rc = object_update_result_data_get(reply, rbuf, 0);
+ if (rc < 0)
+ GOTO(out, rc);
+
+ if (buf->lb_buf == NULL)
+ GOTO(out, rc);
+
+ if (unlikely(buf->lb_len < rbuf->lb_len))
+ GOTO(out, rc = -ERANGE);
+
+ memcpy(buf->lb_buf, rbuf->lb_buf, rbuf->lb_len);
+
+ 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);
+ old->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))
+ osp_update_request_destroy(env, update);
+
+ if (oxe != NULL)
+ osp_oac_xattr_put(oxe);
+
+ return rc;
+}
+
+/**
+ * Implement OSP layer dt_object_operations::do_declare_xattr_set() interface.
+ *
+ * Declare that the caller will set extended attribute to the specified
+ * MDT/OST object.
+ *
+ * If it is non-remote transaction, it will add an OUT_XATTR_SET sub-request
+ * to the OUT RPC that will be flushed when the transaction start. And if the
+ * OSP attributes cache is initialized, then check whether the name extended
+ * attribute entry exists in the cache or not. If yes, replace it; otherwise,
+ * add the extended attribute to the cache.
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] dt pointer to the OSP layer dt_object
+ * \param[in] buf pointer to the lu_buf to hold the extended attribute
+ * \param[in] name the name of the extended attribute to be set
+ * \param[in] flag to indicate the detailed set operation: LU_XATTR_CREATE
+ * or LU_XATTR_REPLACE or others
+ * \param[in] th pointer to the transaction handler
+ *
+ * \retval 0 for success
+ * \retval negative error number on failure
+ */
+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)
+{
+ return osp_trans_update_request_create(th);
+}
+
+/**
+ * Implement OSP layer dt_object_operations::do_xattr_set() interface.
+ *
+ * Set extended attribute to the specified MDT/OST object.
+ *
+ * Add an OUT_XATTR_SET sub-request into the OUT RPC that will be flushed in
+ * the transaction stop. And if the OSP attributes cache is initialized, then
+ * check whether the name extended attribute entry exists in the cache or not.
+ * If yes, replace it; otherwise, add the extended attribute to the cache.
+ *
+ * \param[in] env pointer to the thread context
+ * \param[in] dt pointer to the OSP layer dt_object
+ * \param[in] buf pointer to the lu_buf to hold the extended attribute
+ * \param[in] name the name of the extended attribute to be set
+ * \param[in] fl to indicate the detailed set operation: LU_XATTR_CREATE
+ * or LU_XATTR_REPLACE or others
+ * \param[in] th pointer to the transaction handler
+ *
+ * \retval 0 for success
+ * \retval negative error number on failure
+ */
+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 osp_object *o = dt2osp_obj(dt);
+ struct osp_update_request *update;
+ struct osp_xattr_entry *oxe;
+ int rc;
+ ENTRY;
+
+ update = thandle_to_osp_update_request(th);
+ LASSERT(update != NULL);
+
+ CDEBUG(D_INODE, DFID" set xattr '%s' with size %zd\n",
+ PFID(lu_object_fid(&dt->do_lu)), name, buf->lb_len);
+
+ rc = osp_update_rpc_pack(env, xattr_set, update, OUT_XATTR_SET,
+ lu_object_fid(&dt->do_lu), buf, name, fl);
+ if (rc != 0)
+ RETURN(rc);
+
+ oxe = osp_oac_xattr_find_or_add(o, name, buf->lb_len);
+ if (oxe == NULL) {
+ CWARN("%s: cannot cache xattr '%s' of "DFID"\n",
+ dt->do_lu.lo_dev->ld_obd->obd_name,
+ name, PFID(lu_object_fid(&dt->do_lu)));
+