+#ifdef HAVE_SERVER_SUPPORT
+static inline void echo_md_build_name(struct lu_name *lname, char *name,
+ __u64 id)
+{
+ sprintf(name, LPU64, id);
+ lname->ln_name = name;
+ lname->ln_namelen = strlen(name);
+}
+
+/* similar to mdt_attr_get_complex */
+static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
+ struct md_attr *ma)
+{
+ struct echo_thread_info *info = echo_env_info(env);
+ int rc;
+
+ ENTRY;
+
+ LASSERT(ma->ma_lmm_size > 0);
+
+ rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
+ if (rc < 0)
+ RETURN(rc);
+
+ /* big_lmm may need to be grown */
+ if (info->eti_big_lmmsize < rc) {
+ int size = size_roundup_power2(rc);
+
+ if (info->eti_big_lmmsize > 0) {
+ /* free old buffer */
+ LASSERT(info->eti_big_lmm);
+ OBD_FREE_LARGE(info->eti_big_lmm,
+ info->eti_big_lmmsize);
+ info->eti_big_lmm = NULL;
+ info->eti_big_lmmsize = 0;
+ }
+
+ OBD_ALLOC_LARGE(info->eti_big_lmm, size);
+ if (info->eti_big_lmm == NULL)
+ RETURN(-ENOMEM);
+ info->eti_big_lmmsize = size;
+ }
+ LASSERT(info->eti_big_lmmsize >= rc);
+
+ info->eti_buf.lb_buf = info->eti_big_lmm;
+ info->eti_buf.lb_len = info->eti_big_lmmsize;
+ rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
+ if (rc < 0)
+ RETURN(rc);
+
+ ma->ma_valid |= MA_LOV;
+ ma->ma_lmm = info->eti_big_lmm;
+ ma->ma_lmm_size = rc;
+
+ RETURN(0);
+}
+
+static int echo_attr_get_complex(const struct lu_env *env,
+ struct md_object *next,
+ struct md_attr *ma)
+{
+ struct echo_thread_info *info = echo_env_info(env);
+ struct lu_buf *buf = &info->eti_buf;
+ umode_t mode = lu_object_attr(&next->mo_lu);
+ int need = ma->ma_need;
+ int rc = 0, rc2;
+
+ ENTRY;
+
+ ma->ma_valid = 0;
+
+ if (need & MA_INODE) {
+ ma->ma_need = MA_INODE;
+ rc = mo_attr_get(env, next, ma);
+ if (rc)
+ GOTO(out, rc);
+ ma->ma_valid |= MA_INODE;
+ }
+
+ if (need & MA_LOV) {
+ if (S_ISREG(mode) || S_ISDIR(mode)) {
+ LASSERT(ma->ma_lmm_size > 0);
+ buf->lb_buf = ma->ma_lmm;
+ buf->lb_len = ma->ma_lmm_size;
+ rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
+ if (rc2 > 0) {
+ ma->ma_lmm_size = rc2;
+ ma->ma_valid |= MA_LOV;
+ } else if (rc2 == -ENODATA) {
+ /* no LOV EA */
+ ma->ma_lmm_size = 0;
+ } else if (rc2 == -ERANGE) {
+ rc2 = echo_big_lmm_get(env, next, ma);
+ if (rc2 < 0)
+ GOTO(out, rc = rc2);
+ } else {
+ GOTO(out, rc = rc2);
+ }
+ }
+ }
+
+#ifdef CONFIG_FS_POSIX_ACL
+ if (need & MA_ACL_DEF && S_ISDIR(mode)) {
+ buf->lb_buf = ma->ma_acl;
+ buf->lb_len = ma->ma_acl_size;
+ rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
+ if (rc2 > 0) {
+ ma->ma_acl_size = rc2;
+ ma->ma_valid |= MA_ACL_DEF;
+ } else if (rc2 == -ENODATA) {
+ /* no ACLs */
+ ma->ma_acl_size = 0;
+ } else {
+ GOTO(out, rc = rc2);
+ }
+ }
+#endif
+out:
+ ma->ma_need = need;
+ CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
+ rc, ma->ma_valid, ma->ma_lmm);
+ RETURN(rc);
+}
+
+static int
+echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
+ struct md_object *parent, struct lu_fid *fid,
+ struct lu_name *lname, struct md_op_spec *spec,
+ struct md_attr *ma)
+{
+ struct lu_object *ec_child, *child;
+ struct lu_device *ld = ed->ed_next;
+ struct echo_thread_info *info = echo_env_info(env);
+ struct lu_fid *fid2 = &info->eti_fid2;
+ struct lu_object_conf conf = { .loc_flags = LOC_F_NEW };
+ int rc;
+
+ ENTRY;
+
+ rc = mdo_lookup(env, parent, lname, fid2, spec);
+ if (rc == 0)
+ return -EEXIST;
+ else if (rc != -ENOENT)
+ return rc;
+
+ ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
+ fid, &conf);
+ if (IS_ERR(ec_child)) {
+ CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
+ PTR_ERR(ec_child));
+ RETURN(PTR_ERR(ec_child));
+ }
+
+ child = lu_object_locate(ec_child->lo_header, ld->ld_type);
+ if (child == NULL) {
+ CERROR("Can not locate the child "DFID"\n", PFID(fid));
+ GOTO(out_put, rc = -EINVAL);
+ }
+
+ CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
+ PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
+
+ /*
+ * Do not perform lookup sanity check. We know that name does not exist.
+ */
+ spec->sp_cr_lookup = 0;
+ rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
+ if (rc) {
+ CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
+ GOTO(out_put, rc);
+ }
+ CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc = %d\n",
+ PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
+ EXIT;
+out_put:
+ lu_object_put(env, ec_child);
+ return rc;
+}
+
+static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
+ struct md_attr *ma)
+{
+ struct echo_thread_info *info = echo_env_info(env);
+
+ if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
+ ma->ma_lmm = (void *)&info->eti_lmm;
+ ma->ma_lmm_size = sizeof(info->eti_lmm);
+ } else {
+ LASSERT(info->eti_big_lmmsize);
+ ma->ma_lmm = info->eti_big_lmm;
+ ma->ma_lmm_size = info->eti_big_lmmsize;
+ }
+
+ return 0;
+}
+
+static int echo_create_md_object(const struct lu_env *env,
+ struct echo_device *ed,
+ struct lu_object *ec_parent,
+ struct lu_fid *fid,
+ char *name, int namelen,
+ __u64 id, __u32 mode, int count,
+ int stripe_count, int stripe_offset)
+{
+ struct lu_object *parent;
+ struct echo_thread_info *info = echo_env_info(env);
+ struct lu_name *lname = &info->eti_lname;
+ struct md_op_spec *spec = &info->eti_spec;
+ struct md_attr *ma = &info->eti_ma;
+ struct lu_device *ld = ed->ed_next;
+ int rc = 0;
+ int i;
+
+ ENTRY;
+
+ if (ec_parent == NULL)
+ return -1;
+ parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+ if (parent == NULL)
+ RETURN(-ENXIO);
+
+ memset(ma, 0, sizeof(*ma));
+ memset(spec, 0, sizeof(*spec));
+ if (stripe_count != 0) {
+ spec->sp_cr_flags |= FMODE_WRITE;
+ echo_set_lmm_size(env, ld, ma);
+ if (stripe_count != -1) {
+ struct lov_user_md_v3 *lum = &info->eti_lum;
+
+ lum->lmm_magic = LOV_USER_MAGIC_V3;
+ lum->lmm_stripe_count = stripe_count;
+ lum->lmm_stripe_offset = stripe_offset;
+ lum->lmm_pattern = 0;
+ spec->u.sp_ea.eadata = lum;
+ spec->u.sp_ea.eadatalen = sizeof(*lum);
+ spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
+ }
+ }
+
+ ma->ma_attr.la_mode = mode;
+ ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
+ ma->ma_attr.la_ctime = cfs_time_current_64();
+
+ if (name != NULL) {
+ lname->ln_name = name;
+ lname->ln_namelen = namelen;
+ /* If name is specified, only create one object by name */
+ rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
+ spec, ma);
+ RETURN(rc);
+ }
+
+ /* Create multiple object sequenced by id */
+ for (i = 0; i < count; i++) {
+ char *tmp_name = info->eti_name;
+
+ echo_md_build_name(lname, tmp_name, id);
+
+ rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
+ spec, ma);
+ if (rc) {
+ CERROR("Can not create child %s: rc = %d\n", tmp_name,
+ rc);
+ break;
+ }
+ id++;
+ fid->f_oid++;
+ }
+
+ RETURN(rc);
+}
+
+static struct lu_object *echo_md_lookup(const struct lu_env *env,
+ struct echo_device *ed,
+ struct md_object *parent,
+ struct lu_name *lname)
+{
+ struct echo_thread_info *info = echo_env_info(env);
+ struct lu_fid *fid = &info->eti_fid;
+ struct lu_object *child;
+ int rc;
+ ENTRY;
+
+ CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
+ PFID(fid), parent);
+ rc = mdo_lookup(env, parent, lname, fid, NULL);
+ if (rc) {
+ CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
+ RETURN(ERR_PTR(rc));
+ }
+
+ /* In the function below, .hs_keycmp resolves to
+ * lu_obj_hop_keycmp() */
+ /* coverity[overrun-buffer-val] */
+ child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
+
+ RETURN(child);
+}
+
+static int echo_setattr_object(const struct lu_env *env,
+ struct echo_device *ed,
+ struct lu_object *ec_parent,
+ __u64 id, int count)
+{
+ struct lu_object *parent;
+ struct echo_thread_info *info = echo_env_info(env);
+ struct lu_name *lname = &info->eti_lname;
+ char *name = info->eti_name;
+ struct lu_device *ld = ed->ed_next;
+ struct lu_buf *buf = &info->eti_buf;
+ int rc = 0;
+ int i;
+
+ ENTRY;
+
+ if (ec_parent == NULL)
+ return -1;
+ parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+ if (parent == NULL)
+ RETURN(-ENXIO);
+
+ for (i = 0; i < count; i++) {
+ struct lu_object *ec_child, *child;
+
+ echo_md_build_name(lname, name, id);
+
+ ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
+ if (IS_ERR(ec_child)) {
+ CERROR("Can't find child %s: rc = %ld\n",
+ lname->ln_name, PTR_ERR(ec_child));
+ RETURN(PTR_ERR(ec_child));
+ }
+
+ child = lu_object_locate(ec_child->lo_header, ld->ld_type);
+ if (child == NULL) {
+ CERROR("Can not locate the child %s\n", lname->ln_name);
+ lu_object_put(env, ec_child);
+ rc = -EINVAL;
+ break;
+ }
+
+ CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
+ PFID(lu_object_fid(child)));
+
+ buf->lb_buf = info->eti_xattr_buf;
+ buf->lb_len = sizeof(info->eti_xattr_buf);
+
+ sprintf(name, "%s.test1", XATTR_USER_PREFIX);
+ rc = mo_xattr_set(env, lu2md(child), buf, name,
+ LU_XATTR_CREATE);
+ if (rc < 0) {
+ CERROR("Can not setattr child "DFID": rc = %d\n",
+ PFID(lu_object_fid(child)), rc);
+ lu_object_put(env, ec_child);
+ break;
+ }
+ CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
+ PFID(lu_object_fid(child)));
+ id++;
+ lu_object_put(env, ec_child);
+ }
+ RETURN(rc);
+}
+
+static int echo_getattr_object(const struct lu_env *env,
+ struct echo_device *ed,
+ struct lu_object *ec_parent,
+ __u64 id, int count)
+{
+ struct lu_object *parent;
+ struct echo_thread_info *info = echo_env_info(env);
+ struct lu_name *lname = &info->eti_lname;
+ char *name = info->eti_name;
+ struct md_attr *ma = &info->eti_ma;
+ struct lu_device *ld = ed->ed_next;
+ int rc = 0;
+ int i;
+
+ ENTRY;
+
+ if (ec_parent == NULL)
+ return -1;
+ parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+ if (parent == NULL)
+ RETURN(-ENXIO);
+
+ memset(ma, 0, sizeof(*ma));
+ ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
+ ma->ma_acl = info->eti_xattr_buf;
+ ma->ma_acl_size = sizeof(info->eti_xattr_buf);
+
+ for (i = 0; i < count; i++) {
+ struct lu_object *ec_child, *child;
+
+ ma->ma_valid = 0;
+ echo_md_build_name(lname, name, id);
+ echo_set_lmm_size(env, ld, ma);
+
+ ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
+ if (IS_ERR(ec_child)) {
+ CERROR("Can't find child %s: rc = %ld\n",
+ lname->ln_name, PTR_ERR(ec_child));
+ RETURN(PTR_ERR(ec_child));
+ }
+
+ child = lu_object_locate(ec_child->lo_header, ld->ld_type);
+ if (child == NULL) {
+ CERROR("Can not locate the child %s\n", lname->ln_name);
+ lu_object_put(env, ec_child);
+ RETURN(-EINVAL);
+ }
+
+ CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
+ PFID(lu_object_fid(child)));
+ rc = echo_attr_get_complex(env, lu2md(child), ma);
+ if (rc) {
+ CERROR("Can not getattr child "DFID": rc = %d\n",
+ PFID(lu_object_fid(child)), rc);
+ lu_object_put(env, ec_child);
+ break;
+ }
+ CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
+ PFID(lu_object_fid(child)));
+ id++;
+ lu_object_put(env, ec_child);
+ }
+
+ RETURN(rc);
+}
+
+static int echo_lookup_object(const struct lu_env *env,
+ struct echo_device *ed,
+ struct lu_object *ec_parent,
+ __u64 id, int count)
+{
+ struct lu_object *parent;
+ struct echo_thread_info *info = echo_env_info(env);
+ struct lu_name *lname = &info->eti_lname;
+ char *name = info->eti_name;
+ struct lu_fid *fid = &info->eti_fid;
+ struct lu_device *ld = ed->ed_next;
+ int rc = 0;
+ int i;
+
+ if (ec_parent == NULL)
+ return -1;
+ parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+ if (parent == NULL)
+ return -ENXIO;
+
+ /*prepare the requests*/
+ for (i = 0; i < count; i++) {
+ echo_md_build_name(lname, name, id);
+
+ CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
+ PFID(lu_object_fid(parent)), lname->ln_name, parent);
+
+ rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
+ if (rc) {
+ CERROR("Can not lookup child %s: rc = %d\n", name, rc);
+ break;
+ }
+ CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
+ PFID(lu_object_fid(parent)), lname->ln_name, parent);
+
+ id++;
+ }
+ return rc;
+}
+
+static int echo_md_destroy_internal(const struct lu_env *env,
+ struct echo_device *ed,
+ struct md_object *parent,
+ struct lu_name *lname,
+ struct md_attr *ma)
+{
+ struct lu_device *ld = ed->ed_next;
+ struct lu_object *ec_child;
+ struct lu_object *child;
+ int rc;
+
+ ENTRY;
+
+ ec_child = echo_md_lookup(env, ed, parent, lname);
+ if (IS_ERR(ec_child)) {
+ CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
+ PTR_ERR(ec_child));
+ RETURN(PTR_ERR(ec_child));
+ }
+
+ child = lu_object_locate(ec_child->lo_header, ld->ld_type);
+ if (child == NULL) {
+ CERROR("Can not locate the child %s\n", lname->ln_name);
+ GOTO(out_put, rc = -EINVAL);
+ }
+
+ if (lu_object_remote(child)) {
+ CERROR("Can not destroy remote object %s: rc = %d\n",
+ lname->ln_name, -EPERM);
+ GOTO(out_put, rc = -EPERM);
+ }
+ CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
+ PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
+
+ rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
+ if (rc) {
+ CERROR("Can not unlink child %s: rc = %d\n",
+ lname->ln_name, rc);
+ GOTO(out_put, rc);
+ }
+ CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
+ PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
+out_put:
+ lu_object_put(env, ec_child);
+ return rc;
+}
+
+static int echo_destroy_object(const struct lu_env *env,
+ struct echo_device *ed,
+ struct lu_object *ec_parent,
+ char *name, int namelen,
+ __u64 id, __u32 mode,
+ int count)
+{
+ struct echo_thread_info *info = echo_env_info(env);
+ struct lu_name *lname = &info->eti_lname;
+ struct md_attr *ma = &info->eti_ma;
+ struct lu_device *ld = ed->ed_next;
+ struct lu_object *parent;
+ int rc = 0;
+ int i;
+ ENTRY;
+
+ parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
+ if (parent == NULL)
+ RETURN(-EINVAL);
+
+ memset(ma, 0, sizeof(*ma));
+ ma->ma_attr.la_mode = mode;
+ ma->ma_attr.la_valid = LA_CTIME;
+ ma->ma_attr.la_ctime = cfs_time_current_64();
+ ma->ma_need = MA_INODE;
+ ma->ma_valid = 0;
+
+ if (name != NULL) {
+ lname->ln_name = name;
+ lname->ln_namelen = namelen;
+ rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
+ ma);
+ RETURN(rc);
+ }
+
+ /*prepare the requests*/
+ for (i = 0; i < count; i++) {
+ char *tmp_name = info->eti_name;
+
+ ma->ma_valid = 0;
+ echo_md_build_name(lname, tmp_name, id);
+
+ rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
+ ma);
+ if (rc) {
+ CERROR("Can not unlink child %s: rc = %d\n", name, rc);
+ break;
+ }
+ id++;
+ }
+
+ RETURN(rc);
+}
+
+static struct lu_object *echo_resolve_path(const struct lu_env *env,
+ struct echo_device *ed, char *path,
+ int path_len)
+{
+ struct lu_device *ld = ed->ed_next;
+ struct md_device *md = lu2md_dev(ld);
+ struct echo_thread_info *info = echo_env_info(env);
+ struct lu_fid *fid = &info->eti_fid;
+ struct lu_name *lname = &info->eti_lname;
+ struct lu_object *parent = NULL;
+ struct lu_object *child = NULL;
+ int rc = 0;
+ ENTRY;
+
+ /*Only support MDD layer right now*/
+ rc = md->md_ops->mdo_root_get(env, md, fid);
+ if (rc) {
+ CERROR("get root error: rc = %d\n", rc);
+ RETURN(ERR_PTR(rc));
+ }
+
+ /* In the function below, .hs_keycmp resolves to
+ * lu_obj_hop_keycmp() */
+ /* coverity[overrun-buffer-val] */
+ parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
+ if (IS_ERR(parent)) {
+ CERROR("Can not find the parent "DFID": rc = %ld\n",
+ PFID(fid), PTR_ERR(parent));
+ RETURN(parent);
+ }
+
+ while (1) {
+ struct lu_object *ld_parent;
+ char *e;
+
+ e = strsep(&path, "/");
+ if (e == NULL)
+ break;
+
+ if (e[0] == 0) {
+ if (!path || path[0] == '\0')
+ break;
+ continue;
+ }
+
+ lname->ln_name = e;
+ lname->ln_namelen = strlen(e);
+
+ ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
+ if (ld_parent == NULL) {
+ lu_object_put(env, parent);
+ rc = -EINVAL;
+ break;
+ }
+
+ child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
+ lu_object_put(env, parent);
+ if (IS_ERR(child)) {
+ rc = (int)PTR_ERR(child);
+ CERROR("lookup %s under parent "DFID": rc = %d\n",
+ lname->ln_name, PFID(lu_object_fid(ld_parent)),
+ rc);
+ break;
+ }
+ parent = child;
+ }
+ if (rc)
+ RETURN(ERR_PTR(rc));
+
+ RETURN(parent);
+}
+
+static void echo_ucred_init(struct lu_env *env)
+{
+ struct lu_ucred *ucred = lu_ucred(env);
+
+ ucred->uc_valid = UCRED_INVALID;
+
+ ucred->uc_suppgids[0] = -1;
+ ucred->uc_suppgids[1] = -1;
+
+ ucred->uc_uid = ucred->uc_o_uid =
+ from_kuid(&init_user_ns, current_uid());
+ ucred->uc_gid = ucred->uc_o_gid =
+ from_kgid(&init_user_ns, current_gid());
+ ucred->uc_fsuid = ucred->uc_o_fsuid =
+ from_kuid(&init_user_ns, current_fsuid());
+ ucred->uc_fsgid = ucred->uc_o_fsgid =
+ from_kgid(&init_user_ns, current_fsgid());
+ ucred->uc_cap = cfs_curproc_cap_pack();
+
+ /* remove fs privilege for non-root user. */
+ if (ucred->uc_fsuid)
+ ucred->uc_cap &= ~CFS_CAP_FS_MASK;
+ ucred->uc_valid = UCRED_NEW;
+}
+
+static void echo_ucred_fini(struct lu_env *env)
+{
+ struct lu_ucred *ucred = lu_ucred(env);
+ ucred->uc_valid = UCRED_INIT;
+}
+
+#define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
+#define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
+static int echo_md_handler(struct echo_device *ed, int command,
+ char *path, int path_len, __u64 id, int count,
+ struct obd_ioctl_data *data)
+{
+ struct echo_thread_info *info;
+ struct lu_device *ld = ed->ed_next;
+ struct lu_env *env;
+ int refcheck;
+ struct lu_object *parent;
+ char *name = NULL;
+ int namelen = data->ioc_plen2;
+ int rc = 0;
+ ENTRY;
+
+ if (ld == NULL) {
+ CERROR("MD echo client is not being initialized properly\n");
+ RETURN(-EINVAL);
+ }
+
+ if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
+ CERROR("Only support MDD layer right now!\n");
+ RETURN(-EINVAL);
+ }
+
+ env = cl_env_get(&refcheck);
+ if (IS_ERR(env))
+ RETURN(PTR_ERR(env));
+
+ rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
+ if (rc != 0)
+ GOTO(out_env, rc);
+
+ /* init big_lmm buffer */
+ info = echo_env_info(env);
+ LASSERT(info->eti_big_lmm == NULL);
+ OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
+ if (info->eti_big_lmm == NULL)
+ GOTO(out_env, rc = -ENOMEM);
+ info->eti_big_lmmsize = MIN_MD_SIZE;
+
+ parent = echo_resolve_path(env, ed, path, path_len);
+ if (IS_ERR(parent)) {
+ CERROR("Can not resolve the path %s: rc = %ld\n", path,
+ PTR_ERR(parent));
+ GOTO(out_free, rc = PTR_ERR(parent));
+ }
+
+ if (namelen > 0) {
+ OBD_ALLOC(name, namelen + 1);
+ if (name == NULL)
+ GOTO(out_put, rc = -ENOMEM);
+ if (copy_from_user(name, data->ioc_pbuf2, namelen))
+ GOTO(out_name, rc = -EFAULT);
+ }
+
+ echo_ucred_init(env);
+
+ switch (command) {
+ case ECHO_MD_CREATE:
+ case ECHO_MD_MKDIR: {
+ struct echo_thread_info *info = echo_env_info(env);
+ __u32 mode = data->ioc_obdo2.o_mode;
+ struct lu_fid *fid = &info->eti_fid;
+ int stripe_count = (int)data->ioc_obdo2.o_misc;
+ int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
+
+ rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
+ if (rc != 0)
+ break;
+
+ /* In the function below, .hs_keycmp resolves to
+ * lu_obj_hop_keycmp() */
+ /* coverity[overrun-buffer-val] */
+ rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
+ id, mode, count, stripe_count,
+ stripe_index);
+ break;
+ }
+ case ECHO_MD_DESTROY:
+ case ECHO_MD_RMDIR: {
+ __u32 mode = data->ioc_obdo2.o_mode;
+
+ rc = echo_destroy_object(env, ed, parent, name, namelen,
+ id, mode, count);
+ break;
+ }
+ case ECHO_MD_LOOKUP:
+ rc = echo_lookup_object(env, ed, parent, id, count);
+ break;
+ case ECHO_MD_GETATTR:
+ rc = echo_getattr_object(env, ed, parent, id, count);
+ break;
+ case ECHO_MD_SETATTR:
+ rc = echo_setattr_object(env, ed, parent, id, count);
+ break;
+ default:
+ CERROR("unknown command %d\n", command);
+ rc = -EINVAL;
+ break;
+ }
+ echo_ucred_fini(env);
+
+out_name:
+ if (name != NULL)
+ OBD_FREE(name, namelen + 1);
+out_put:
+ lu_object_put(env, parent);
+out_free:
+ LASSERT(info->eti_big_lmm);
+ OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
+ info->eti_big_lmm = NULL;
+ info->eti_big_lmmsize = 0;
+out_env:
+ cl_env_put(env, &refcheck);
+ return rc;
+}
+#endif /* HAVE_SERVER_SUPPORT */
+
+static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
+ int on_target, struct obdo *oa, void *ulsm,
+ int ulsm_nob, struct obd_trans_info *oti)