Whamcloud - gitweb
1cf1a75399054b1ddd3b1c0ed3155c0e2dde6042
[fs/lustre-release.git] / lustre / obdecho / echo_client.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 #define DEBUG_SUBSYSTEM S_ECHO
33
34 #include <linux/user_namespace.h>
35 #include <linux/uidgid.h>
36
37 #include <libcfs/libcfs.h>
38 #include <obd.h>
39 #include <obd_support.h>
40 #include <obd_class.h>
41 #include <lprocfs_status.h>
42 #include <cl_object.h>
43 #include <lustre_fid.h>
44 #include <lustre_lmv.h>
45 #include <lustre_acl.h>
46 #include <uapi/linux/lustre/lustre_ioctl.h>
47 #include <lustre_net.h>
48 #ifdef HAVE_SERVER_SUPPORT
49 # include <md_object.h>
50
51 #define ETI_NAME_LEN    20
52
53 #endif /* HAVE_SERVER_SUPPORT */
54
55 #include "echo_internal.h"
56
57 /** \defgroup echo_client Echo Client
58  * @{
59  */
60
61 /* echo thread key have a CL_THREAD flag, which set cl_env function directly */
62 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
63 #define ECHO_DT_CTX_TAG (LCT_REMEMBER | LCT_DT_THREAD)
64 #define ECHO_SES_TAG    (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
65
66 struct echo_device {
67         struct cl_device          ed_cl;
68         struct echo_client_obd   *ed_ec;
69
70         struct cl_site            ed_site_myself;
71         struct lu_site           *ed_site;
72         struct lu_device         *ed_next;
73         int                       ed_next_ismd;
74         struct lu_client_seq     *ed_cl_seq;
75 #ifdef HAVE_SERVER_SUPPORT
76         struct local_oid_storage *ed_los;
77         struct lu_fid             ed_root_fid;
78 #endif /* HAVE_SERVER_SUPPORT */
79 };
80
81 struct echo_object {
82         struct cl_object        eo_cl;
83         struct cl_object_header eo_hdr;
84         struct echo_device     *eo_dev;
85         struct list_head        eo_obj_chain;
86         struct lov_oinfo       *eo_oinfo;
87         int                     eo_deleted;
88 };
89
90 struct echo_object_conf {
91         struct cl_object_conf   eoc_cl;
92         struct lov_oinfo      **eoc_oinfo;
93 };
94
95 #ifdef HAVE_SERVER_SUPPORT
96 static const char echo_md_root_dir_name[] = "ROOT_ECHO";
97
98 /**
99  * In order to use the values of members in struct mdd_device,
100  * we define an alias structure here.
101  */
102 struct echo_md_device {
103         struct md_device                 emd_md_dev;
104         struct obd_export               *emd_child_exp;
105         struct dt_device                *emd_child;
106         struct dt_device                *emd_bottom;
107         struct lu_fid                    emd_root_fid;
108         struct lu_fid                    emd_local_root_fid;
109 };
110 #endif /* HAVE_SERVER_SUPPORT */
111
112 static int echo_client_setup(const struct lu_env *env,
113                              struct obd_device *obd,
114                              struct lustre_cfg *lcfg);
115 static int echo_client_cleanup(struct obd_device *obd);
116
117 /** \defgroup echo_helpers Helper functions
118  * @{
119  */
120 static struct echo_device *cl2echo_dev(const struct cl_device *dev)
121 {
122         return container_of_safe(dev, struct echo_device, ed_cl);
123 }
124
125 static struct cl_device *echo_dev2cl(struct echo_device *d)
126 {
127         return &d->ed_cl;
128 }
129
130 static struct echo_device *obd2echo_dev(const struct obd_device *obd)
131 {
132         return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
133 }
134
135 static struct cl_object *echo_obj2cl(struct echo_object *eco)
136 {
137         return &eco->eo_cl;
138 }
139
140 static struct echo_object *cl2echo_obj(const struct cl_object *o)
141 {
142         return container_of(o, struct echo_object, eo_cl);
143 }
144
145 static struct lu_context_key echo_thread_key;
146
147 static struct echo_thread_info *echo_env_info(const struct lu_env *env)
148 {
149         struct echo_thread_info *info;
150
151         info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
152         LASSERT(info != NULL);
153         return info;
154 }
155
156 static struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
157 {
158         return container_of(c, struct echo_object_conf, eoc_cl);
159 }
160
161 #ifdef HAVE_SERVER_SUPPORT
162 static struct echo_md_device *lu2emd_dev(struct lu_device *d)
163 {
164         return container_of_safe(d, struct echo_md_device,
165                                  emd_md_dev.md_lu_dev);
166 }
167
168 static struct lu_device *emd2lu_dev(struct echo_md_device *d)
169 {
170         return &d->emd_md_dev.md_lu_dev;
171 }
172
173 static struct seq_server_site *echo_md_seq_site(struct echo_md_device *d)
174 {
175         return emd2lu_dev(d)->ld_site->ld_seq_site;
176 }
177
178 static struct obd_device *emd2obd_dev(struct echo_md_device *d)
179 {
180         return d->emd_md_dev.md_lu_dev.ld_obd;
181 }
182 #endif /* HAVE_SERVER_SUPPORT */
183
184 /** @} echo_helpers */
185
186 static int cl_echo_object_put(struct echo_object *eco);
187
188 struct echo_thread_info {
189         struct echo_object_conf eti_conf;
190         struct lustre_md        eti_md;
191         struct lu_fid           eti_fid;
192         struct lu_fid           eti_fid2;
193 #ifdef HAVE_SERVER_SUPPORT
194         struct md_op_spec       eti_spec;
195         struct lov_mds_md_v3    eti_lmm;
196         struct lov_user_md_v3   eti_lum;
197         struct md_attr          eti_ma;
198         struct lu_name          eti_lname;
199         /* per-thread values, can be re-used */
200         void                    *eti_big_lmm; /* may be vmalloc'd */
201         int                     eti_big_lmmsize;
202         char                    eti_name[ETI_NAME_LEN];
203         struct lu_buf           eti_buf;
204         /* If we want to test large ACL, then need to enlarge the buffer. */
205         char                    eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE_OLD];
206 #endif
207 };
208
209 /* No session used right now */
210 struct echo_session_info {
211         unsigned long dummy;
212 };
213
214 static struct kmem_cache *echo_object_kmem;
215 static struct kmem_cache *echo_thread_kmem;
216 static struct kmem_cache *echo_session_kmem;
217
218 static struct lu_kmem_descr echo_caches[] = {
219         {
220                 .ckd_cache = &echo_object_kmem,
221                 .ckd_name  = "echo_object_kmem",
222                 .ckd_size  = sizeof(struct echo_object)
223         },
224         {
225                 .ckd_cache = &echo_thread_kmem,
226                 .ckd_name  = "echo_thread_kmem",
227                 .ckd_size  = sizeof(struct echo_thread_info)
228         },
229         {
230                 .ckd_cache = &echo_session_kmem,
231                 .ckd_name  = "echo_session_kmem",
232                 .ckd_size  = sizeof(struct echo_session_info)
233         },
234         {
235                 .ckd_cache = NULL
236         }
237 };
238
239 /** \defgroup echo_lu_ops lu_object operations
240  *
241  * operations for echo lu object.
242  *
243  * @{
244  */
245 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
246                             const struct lu_object_conf *conf)
247 {
248         struct echo_device *ed         = cl2echo_dev(lu2cl_dev(obj->lo_dev));
249         struct echo_client_obd *ec     = ed->ed_ec;
250         struct echo_object *eco        = cl2echo_obj(lu2cl(obj));
251
252         ENTRY;
253         if (ed->ed_next) {
254                 struct lu_object  *below;
255                 struct lu_device  *under;
256
257                 under = ed->ed_next;
258                 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
259                                                         under);
260                 if (!below)
261                         RETURN(-ENOMEM);
262                 lu_object_add(obj, below);
263         }
264
265         if (!ed->ed_next_ismd) {
266                 const struct cl_object_conf *cconf = lu2cl_conf(conf);
267                 struct echo_object_conf *econf = cl2echo_conf(cconf);
268
269                 LASSERT(econf->eoc_oinfo != NULL);
270
271                 /*
272                  * Transfer the oinfo pointer to eco that it won't be
273                  * freed.
274                  */
275                 eco->eo_oinfo = *econf->eoc_oinfo;
276                 *econf->eoc_oinfo = NULL;
277         } else {
278                 eco->eo_oinfo = NULL;
279         }
280
281         eco->eo_dev = ed;
282         cl_object_page_init(lu2cl(obj), 0);
283
284         spin_lock(&ec->ec_lock);
285         list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
286         spin_unlock(&ec->ec_lock);
287
288         RETURN(0);
289 }
290
291 static void echo_object_delete(const struct lu_env *env, struct lu_object *obj)
292 {
293         struct echo_object *eco    = cl2echo_obj(lu2cl(obj));
294         struct echo_client_obd *ec;
295
296         ENTRY;
297
298         /* object delete called unconditolally - layer init or not */
299         if (eco->eo_dev == NULL)
300                 return;
301
302         ec = eco->eo_dev->ed_ec;
303
304         spin_lock(&ec->ec_lock);
305         list_del_init(&eco->eo_obj_chain);
306         spin_unlock(&ec->ec_lock);
307
308         if (eco->eo_oinfo)
309                 OBD_FREE_PTR(eco->eo_oinfo);
310 }
311
312 static void echo_object_free_rcu(struct rcu_head *head)
313 {
314         struct echo_object *eco = container_of(head, struct echo_object,
315                                                eo_hdr.coh_lu.loh_rcu);
316
317         kmem_cache_free(echo_object_kmem, eco);
318 }
319
320 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
321 {
322         struct echo_object *eco    = cl2echo_obj(lu2cl(obj));
323
324         ENTRY;
325
326         lu_object_fini(obj);
327         lu_object_header_fini(obj->lo_header);
328
329         OBD_FREE_PRE(eco, sizeof(*eco), "slab-freed");
330         call_rcu(&eco->eo_hdr.coh_lu.loh_rcu, echo_object_free_rcu);
331         EXIT;
332 }
333
334 static int echo_object_print(const struct lu_env *env, void *cookie,
335                              lu_printer_t p, const struct lu_object *o)
336 {
337         struct echo_object *obj = cl2echo_obj(lu2cl(o));
338
339         return (*p)(env, cookie, "echoclient-object@%p", obj);
340 }
341
342 static const struct lu_object_operations echo_lu_obj_ops = {
343         .loo_object_init      = echo_object_init,
344         .loo_object_delete    = echo_object_delete,
345         .loo_object_release   = NULL,
346         .loo_object_free      = echo_object_free,
347         .loo_object_print     = echo_object_print,
348         .loo_object_invariant = NULL
349 };
350 /** @} echo_lu_ops */
351
352 /** \defgroup echo_lu_dev_ops  lu_device operations
353  *
354  * Operations for echo lu device.
355  *
356  * @{
357  */
358 static struct lu_object *echo_object_alloc(const struct lu_env *env,
359                                            const struct lu_object_header *hdr,
360                                            struct lu_device *dev)
361 {
362         struct echo_object *eco;
363         struct lu_object *obj = NULL;
364
365         ENTRY;
366         /* we're the top dev. */
367         LASSERT(hdr == NULL);
368         OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
369         if (eco) {
370                 struct cl_object_header *hdr = &eco->eo_hdr;
371
372                 obj = &echo_obj2cl(eco)->co_lu;
373                 cl_object_header_init(hdr);
374                 hdr->coh_page_bufsize = round_up(sizeof(struct cl_page), 8);
375
376                 lu_object_init(obj, &hdr->coh_lu, dev);
377                 lu_object_add_top(&hdr->coh_lu, obj);
378                 obj->lo_ops       = &echo_lu_obj_ops;
379         }
380         RETURN(obj);
381 }
382
383 static const struct lu_device_operations echo_device_lu_ops = {
384         .ldo_object_alloc   = echo_object_alloc,
385 };
386
387 /** @} echo_lu_dev_ops */
388
389 /** \defgroup echo_init Setup and teardown
390  *
391  * Init and fini functions for echo client.
392  *
393  * @{
394  */
395 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
396 {
397         struct cl_site *site = &ed->ed_site_myself;
398         int rc;
399
400         /* initialize site */
401         rc = cl_site_init(site, &ed->ed_cl);
402         if (rc) {
403                 CERROR("Cannot initialize site for echo client(%d)\n", rc);
404                 return rc;
405         }
406
407         rc = lu_site_init_finish(&site->cs_lu);
408         if (rc) {
409                 cl_site_fini(site);
410                 return rc;
411         }
412
413         ed->ed_site = &site->cs_lu;
414         return 0;
415 }
416
417 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
418 {
419         if (ed->ed_site) {
420                 if (!ed->ed_next_ismd)
421                         lu_site_fini(ed->ed_site);
422                 ed->ed_site = NULL;
423         }
424 }
425
426 static void *echo_thread_key_init(const struct lu_context *ctx,
427                                   struct lu_context_key *key)
428 {
429         struct echo_thread_info *info;
430
431         OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
432         if (!info)
433                 info = ERR_PTR(-ENOMEM);
434         return info;
435 }
436
437 static void echo_thread_key_fini(const struct lu_context *ctx,
438                                  struct lu_context_key *key, void *data)
439 {
440         struct echo_thread_info *info = data;
441
442         OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
443 }
444
445 static struct lu_context_key echo_thread_key = {
446         .lct_tags = LCT_CL_THREAD,
447         .lct_init = echo_thread_key_init,
448         .lct_fini = echo_thread_key_fini,
449 };
450
451 static void *echo_session_key_init(const struct lu_context *ctx,
452                                   struct lu_context_key *key)
453 {
454         struct echo_session_info *session;
455
456         OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
457         if (!session)
458                 session = ERR_PTR(-ENOMEM);
459         return session;
460 }
461
462 static void echo_session_key_fini(const struct lu_context *ctx,
463                                   struct lu_context_key *key, void *data)
464 {
465         struct echo_session_info *session = data;
466
467         OBD_SLAB_FREE_PTR(session, echo_session_kmem);
468 }
469
470 static struct lu_context_key echo_session_key = {
471         .lct_tags = LCT_SESSION,
472         .lct_init = echo_session_key_init,
473         .lct_fini = echo_session_key_fini,
474 };
475
476 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
477
478 #ifdef HAVE_SERVER_SUPPORT
479 # define ECHO_SEQ_WIDTH 0xffffffff
480 static int echo_fid_init(struct echo_device *ed, char *obd_name,
481                          struct seq_server_site *ss)
482 {
483         char *prefix;
484         int rc;
485
486         ENTRY;
487         OBD_ALLOC_PTR(ed->ed_cl_seq);
488         if (!ed->ed_cl_seq)
489                 RETURN(-ENOMEM);
490
491         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
492         if (!prefix)
493                 GOTO(out_free_seq, rc = -ENOMEM);
494
495         snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
496
497         /* Init client side sequence-manager */
498         seq_client_init(ed->ed_cl_seq, NULL,
499                         LUSTRE_SEQ_METADATA,
500                         prefix, ss->ss_server_seq);
501         ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
502         OBD_FREE(prefix, MAX_OBD_NAME + 5);
503
504         RETURN(0);
505
506 out_free_seq:
507         OBD_FREE_PTR(ed->ed_cl_seq);
508         ed->ed_cl_seq = NULL;
509         RETURN(rc);
510 }
511
512 static int echo_fid_fini(struct obd_device *obd)
513 {
514         struct echo_device *ed = obd2echo_dev(obd);
515
516         ENTRY;
517         if (ed->ed_cl_seq) {
518                 seq_client_fini(ed->ed_cl_seq);
519                 OBD_FREE_PTR(ed->ed_cl_seq);
520                 ed->ed_cl_seq = NULL;
521         }
522
523         RETURN(0);
524 }
525
526 static void echo_ed_los_fini(const struct lu_env *env, struct echo_device *ed)
527 {
528         ENTRY;
529         if (ed != NULL && ed->ed_next_ismd && ed->ed_los != NULL) {
530                 local_oid_storage_fini(env, ed->ed_los);
531                 ed->ed_los = NULL;
532         }
533 }
534
535 static int
536 echo_md_local_file_create(const struct lu_env *env, struct echo_md_device *emd,
537                           struct local_oid_storage *los,
538                           const struct lu_fid *pfid, const char *name,
539                           __u32 mode, struct lu_fid *fid)
540 {
541         struct dt_object        *parent = NULL;
542         struct dt_object        *dto = NULL;
543         int                      rc = 0;
544
545         ENTRY;
546         LASSERT(!fid_is_zero(pfid));
547         parent = dt_locate(env, emd->emd_bottom, pfid);
548         if (unlikely(IS_ERR(parent)))
549                 RETURN(PTR_ERR(parent));
550
551         /* create local file with @fid */
552         dto = local_file_find_or_create_with_fid(env, emd->emd_bottom, fid,
553                                                  parent, name, mode);
554         if (IS_ERR(dto))
555                 GOTO(out_put, rc = PTR_ERR(dto));
556
557         *fid = *lu_object_fid(&dto->do_lu);
558         /*
559          * since stack is not fully set up the local_storage uses own stack
560          * and we should drop its object from cache
561          */
562         dt_object_put_nocache(env, dto);
563
564         EXIT;
565 out_put:
566         dt_object_put(env, parent);
567         RETURN(rc);
568 }
569
570 static int
571 echo_md_root_get(const struct lu_env *env, struct echo_md_device *emd,
572                  struct echo_device *ed)
573 {
574         struct lu_fid fid;
575         int rc = 0;
576
577         ENTRY;
578         /* Setup local dirs */
579         fid.f_seq = FID_SEQ_LOCAL_NAME;
580         fid.f_oid = 1;
581         fid.f_ver = 0;
582         rc = local_oid_storage_init(env, emd->emd_bottom, &fid, &ed->ed_los);
583         if (rc != 0)
584                 RETURN(rc);
585
586         lu_echo_root_fid(&fid);
587         if (echo_md_seq_site(emd)->ss_node_id == 0) {
588                 rc = echo_md_local_file_create(env, emd, ed->ed_los,
589                                                &emd->emd_local_root_fid,
590                                                echo_md_root_dir_name, S_IFDIR |
591                                                S_IRUGO | S_IWUSR | S_IXUGO,
592                                                &fid);
593                 if (rc != 0) {
594                         CERROR("%s: create md echo root fid failed: rc = %d\n",
595                                emd2obd_dev(emd)->obd_name, rc);
596                         GOTO(out_los, rc);
597                 }
598         }
599         ed->ed_root_fid = fid;
600
601         RETURN(0);
602 out_los:
603         echo_ed_los_fini(env, ed);
604
605         RETURN(rc);
606 }
607 #endif /* HAVE_SERVER_SUPPORT */
608
609 static struct lu_device *echo_device_alloc(const struct lu_env *env,
610                                            struct lu_device_type *t,
611                                            struct lustre_cfg *cfg)
612 {
613         struct lu_device   *next;
614         struct echo_device *ed;
615         struct cl_device   *cd;
616         struct obd_device  *obd = NULL; /* to keep compiler happy */
617         struct obd_device  *tgt;
618         const char *tgt_type_name;
619         int rc;
620         int cleanup = 0;
621
622         ENTRY;
623         OBD_ALLOC_PTR(ed);
624         if (!ed)
625                 GOTO(out, rc = -ENOMEM);
626
627         cleanup = 1;
628         cd = &ed->ed_cl;
629         rc = cl_device_init(cd, t);
630         if (rc)
631                 GOTO(out, rc);
632
633         cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
634
635         cleanup = 2;
636         obd = class_name2obd(lustre_cfg_string(cfg, 0));
637         LASSERT(obd != NULL);
638         LASSERT(env != NULL);
639
640         tgt = class_name2obd(lustre_cfg_string(cfg, 1));
641         if (!tgt) {
642                 CERROR("Can not find tgt device %s\n",
643                         lustre_cfg_string(cfg, 1));
644                 GOTO(out, rc = -ENODEV);
645         }
646
647         next = tgt->obd_lu_dev;
648
649         if (strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME) == 0) {
650                 ed->ed_next_ismd = 1;
651         } else if (strcmp(tgt->obd_type->typ_name, LUSTRE_OST_NAME) == 0 ||
652                    strcmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME) == 0) {
653                 ed->ed_next_ismd = 0;
654                 rc = echo_site_init(env, ed);
655                 if (rc)
656                         GOTO(out, rc);
657         } else {
658                 GOTO(out, rc = -EINVAL);
659         }
660
661         cleanup = 3;
662
663         rc = echo_client_setup(env, obd, cfg);
664         if (rc)
665                 GOTO(out, rc);
666
667         ed->ed_ec = &obd->u.echo_client;
668         cleanup = 4;
669
670         if (ed->ed_next_ismd) {
671 #ifdef HAVE_SERVER_SUPPORT
672                 /* Suppose to connect to some Metadata layer */
673                 struct lu_site          *ls = NULL;
674                 struct lu_device        *ld = NULL;
675                 struct md_device        *md = NULL;
676                 struct echo_md_device   *emd = NULL;
677                 int                      found = 0;
678
679                 if (!next) {
680                         CERROR("%s is not lu device type!\n",
681                                lustre_cfg_string(cfg, 1));
682                         GOTO(out, rc = -EINVAL);
683                 }
684
685                 tgt_type_name = lustre_cfg_string(cfg, 2);
686                 if (!tgt_type_name) {
687                         CERROR("%s no type name for echo %s setup\n",
688                                 lustre_cfg_string(cfg, 1),
689                                 tgt->obd_type->typ_name);
690                         GOTO(out, rc = -EINVAL);
691                 }
692
693                 ls = next->ld_site;
694
695                 spin_lock(&ls->ls_ld_lock);
696                 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
697                         if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
698                                 found = 1;
699                                 break;
700                         }
701                 }
702                 spin_unlock(&ls->ls_ld_lock);
703
704                 if (found == 0) {
705                         CERROR("%s is not lu device type!\n",
706                                lustre_cfg_string(cfg, 1));
707                         GOTO(out, rc = -EINVAL);
708                 }
709
710                 next = ld;
711                 /* For MD echo client, it will use the site in MDS stack */
712                 ed->ed_site = ls;
713                 ed->ed_cl.cd_lu_dev.ld_site = ls;
714                 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
715                 if (rc) {
716                         CERROR("echo fid init error %d\n", rc);
717                         GOTO(out, rc);
718                 }
719
720                 md = lu2md_dev(next);
721                 emd = lu2emd_dev(&md->md_lu_dev);
722                 rc = echo_md_root_get(env, emd, ed);
723                 if (rc != 0) {
724                         CERROR("%s: get root error: rc = %d\n",
725                                 emd2obd_dev(emd)->obd_name, rc);
726                         GOTO(out, rc);
727                 }
728 #else /* !HAVE_SERVER_SUPPORT */
729                 CERROR(
730                        "Local operations are NOT supported on client side. Only remote operations are supported. Metadata client must be run on server side.\n");
731                 GOTO(out, rc = -EOPNOTSUPP);
732 #endif /* HAVE_SERVER_SUPPORT */
733         } else {
734                 /*
735                  * if echo client is to be stacked upon ost device, the next is
736                  * NULL since ost is not a clio device so far
737                  */
738                 if (next != NULL && !lu_device_is_cl(next))
739                         next = NULL;
740
741                 tgt_type_name = tgt->obd_type->typ_name;
742                 if (next) {
743                         LASSERT(next != NULL);
744                         if (next->ld_site)
745                                 GOTO(out, rc = -EBUSY);
746
747                         next->ld_site = ed->ed_site;
748                         rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
749                                                         next->ld_type->ldt_name,
750                                                         NULL);
751                         if (rc)
752                                 GOTO(out, rc);
753                 } else {
754                         LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
755                 }
756         }
757
758         ed->ed_next = next;
759         RETURN(&cd->cd_lu_dev);
760 out:
761         switch (cleanup) {
762         case 4: {
763                 int rc2;
764
765                 rc2 = echo_client_cleanup(obd);
766                 if (rc2)
767                         CERROR("Cleanup obd device %s error(%d)\n",
768                                obd->obd_name, rc2);
769         }
770         fallthrough;
771
772         case 3:
773                 echo_site_fini(env, ed);
774                 fallthrough;
775         case 2:
776                 cl_device_fini(&ed->ed_cl);
777                 fallthrough;
778         case 1:
779                 OBD_FREE_PTR(ed);
780                 fallthrough;
781         case 0:
782         default:
783                 break;
784         }
785         return ERR_PTR(rc);
786 }
787
788 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
789                             const char *name, struct lu_device *next)
790 {
791         LBUG();
792         return 0;
793 }
794
795 static struct lu_device *echo_device_fini(const struct lu_env *env,
796                                           struct lu_device *d)
797 {
798         struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
799         struct lu_device *next = ed->ed_next;
800
801         while (next && !ed->ed_next_ismd)
802                 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
803         return NULL;
804 }
805
806 static struct lu_device *echo_device_free(const struct lu_env *env,
807                                           struct lu_device *d)
808 {
809         struct echo_device     *ed   = cl2echo_dev(lu2cl_dev(d));
810         struct echo_client_obd *ec   = ed->ed_ec;
811         struct echo_object     *eco;
812         struct lu_device       *next = ed->ed_next;
813
814         CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
815                ed, next);
816
817         lu_site_purge(env, ed->ed_site, -1);
818
819         /*
820          * check if there are objects still alive.
821          * It shouldn't have any object because lu_site_purge would cleanup
822          * all of cached objects. Anyway, probably the echo device is being
823          * parallelly accessed.
824          */
825         spin_lock(&ec->ec_lock);
826         list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
827                 eco->eo_deleted = 1;
828         spin_unlock(&ec->ec_lock);
829
830         /* purge again */
831         lu_site_purge(env, ed->ed_site, -1);
832
833         CDEBUG(D_INFO,
834                "Waiting for the reference of echo object to be dropped\n");
835
836         /* Wait for the last reference to be dropped. */
837         spin_lock(&ec->ec_lock);
838         while (!list_empty(&ec->ec_objects)) {
839                 spin_unlock(&ec->ec_lock);
840                 CERROR(
841                        "echo_client still has objects at cleanup time, wait for 1 second\n");
842                 schedule_timeout_uninterruptible(cfs_time_seconds(1));
843                 lu_site_purge(env, ed->ed_site, -1);
844                 spin_lock(&ec->ec_lock);
845         }
846         spin_unlock(&ec->ec_lock);
847
848         LASSERT(list_empty(&ec->ec_locks));
849
850         CDEBUG(D_INFO, "No object exists, exiting...\n");
851
852         echo_client_cleanup(d->ld_obd);
853 #ifdef HAVE_SERVER_SUPPORT
854         echo_fid_fini(d->ld_obd);
855         echo_ed_los_fini(env, ed);
856 #endif
857         while (next && !ed->ed_next_ismd)
858                 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
859
860         LASSERT(ed->ed_site == d->ld_site);
861         echo_site_fini(env, ed);
862         cl_device_fini(&ed->ed_cl);
863         OBD_FREE_PTR(ed);
864
865         cl_env_cache_purge(~0);
866
867         return NULL;
868 }
869
870 static const struct lu_device_type_operations echo_device_type_ops = {
871         .ldto_init = echo_type_init,
872         .ldto_fini = echo_type_fini,
873
874         .ldto_start = echo_type_start,
875         .ldto_stop  = echo_type_stop,
876
877         .ldto_device_alloc = echo_device_alloc,
878         .ldto_device_free  = echo_device_free,
879         .ldto_device_init  = echo_device_init,
880         .ldto_device_fini  = echo_device_fini
881 };
882
883 static struct lu_device_type echo_device_type = {
884         .ldt_tags     = LU_DEVICE_CL,
885         .ldt_name     = LUSTRE_ECHO_CLIENT_NAME,
886         .ldt_ops      = &echo_device_type_ops,
887         .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
888 };
889 /** @} echo_init */
890
891 /** \defgroup echo_exports Exported operations
892  *
893  * exporting functions to echo client
894  *
895  * @{
896  */
897
898 /* Interfaces to echo client obd device */
899 static struct echo_object *
900 cl_echo_object_find(struct echo_device *d, const struct ost_id *oi)
901 {
902         struct lu_env *env;
903         struct echo_thread_info *info;
904         struct echo_object_conf *conf;
905         struct echo_object *eco;
906         struct cl_object *obj;
907         struct lov_oinfo *oinfo = NULL;
908         struct lu_fid *fid;
909         __u16  refcheck;
910         int rc;
911
912         ENTRY;
913         LASSERTF(ostid_id(oi) != 0, DOSTID"\n", POSTID(oi));
914         LASSERTF(ostid_seq(oi) == FID_SEQ_ECHO, DOSTID"\n", POSTID(oi));
915
916         /* Never return an object if the obd is to be freed. */
917         if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
918                 RETURN(ERR_PTR(-ENODEV));
919
920         env = cl_env_get(&refcheck);
921         if (IS_ERR(env))
922                 RETURN((void *)env);
923
924         info = echo_env_info(env);
925         conf = &info->eti_conf;
926         if (d->ed_next) {
927                 OBD_ALLOC_PTR(oinfo);
928                 if (!oinfo)
929                         GOTO(out, eco = ERR_PTR(-ENOMEM));
930
931                 oinfo->loi_oi = *oi;
932                 conf->eoc_cl.u.coc_oinfo = oinfo;
933         }
934
935         /*
936          * If echo_object_init() is successful then ownership of oinfo
937          * is transferred to the object.
938          */
939         conf->eoc_oinfo = &oinfo;
940
941         fid = &info->eti_fid;
942         rc = ostid_to_fid(fid, oi, 0);
943         if (rc != 0)
944                 GOTO(out, eco = ERR_PTR(rc));
945
946         /*
947          * In the function below, .hs_keycmp resolves to
948          * lu_obj_hop_keycmp()
949          */
950         obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
951         if (IS_ERR(obj))
952                 GOTO(out, eco = (void *)obj);
953
954         eco = cl2echo_obj(obj);
955         if (eco->eo_deleted) {
956                 cl_object_put(env, obj);
957                 eco = ERR_PTR(-EAGAIN);
958         }
959
960 out:
961         if (oinfo)
962                 OBD_FREE_PTR(oinfo);
963
964         cl_env_put(env, &refcheck);
965         RETURN(eco);
966 }
967
968 static int cl_echo_object_put(struct echo_object *eco)
969 {
970         struct lu_env *env;
971         struct cl_object *obj = echo_obj2cl(eco);
972         __u16  refcheck;
973
974         ENTRY;
975         env = cl_env_get(&refcheck);
976         if (IS_ERR(env))
977                 RETURN(PTR_ERR(env));
978
979         /* an external function to kill an object? */
980         if (eco->eo_deleted) {
981                 struct lu_object_header *loh = obj->co_lu.lo_header;
982
983                 LASSERT(&eco->eo_hdr == luh2coh(loh));
984                 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
985         }
986
987         cl_object_put(env, obj);
988         cl_env_put(env, &refcheck);
989         RETURN(0);
990 }
991
992 /** @} echo_exports */
993
994 static u64 last_object_id;
995
996 #ifdef HAVE_SERVER_SUPPORT
997 static void echo_md_build_name(struct lu_name *lname, char *name,
998                                       __u64 id)
999 {
1000         snprintf(name, ETI_NAME_LEN, "%llu", id);
1001         lname->ln_name = name;
1002         lname->ln_namelen = strlen(name);
1003 }
1004
1005 /* similar to mdt_attr_get_complex */
1006 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1007                             struct md_attr *ma)
1008 {
1009         struct echo_thread_info *info = echo_env_info(env);
1010         int rc;
1011
1012         ENTRY;
1013
1014         LASSERT(ma->ma_lmm_size > 0);
1015
1016         LASSERT(ma->ma_need & (MA_LOV | MA_LMV));
1017         if (ma->ma_need & MA_LOV)
1018                 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1019         else
1020                 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LMV);
1021
1022         if (rc < 0)
1023                 RETURN(rc);
1024
1025         /* big_lmm may need to be grown */
1026         if (info->eti_big_lmmsize < rc) {
1027                 int size = size_roundup_power2(rc);
1028
1029                 if (info->eti_big_lmmsize > 0) {
1030                         /* free old buffer */
1031                         LASSERT(info->eti_big_lmm);
1032                         OBD_FREE_LARGE(info->eti_big_lmm,
1033                                        info->eti_big_lmmsize);
1034                         info->eti_big_lmm = NULL;
1035                         info->eti_big_lmmsize = 0;
1036                 }
1037
1038                 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1039                 if (!info->eti_big_lmm)
1040                         RETURN(-ENOMEM);
1041                 info->eti_big_lmmsize = size;
1042         }
1043         LASSERT(info->eti_big_lmmsize >= rc);
1044
1045         info->eti_buf.lb_buf = info->eti_big_lmm;
1046         info->eti_buf.lb_len = info->eti_big_lmmsize;
1047         if (ma->ma_need & MA_LOV)
1048                 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1049         else
1050                 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LMV);
1051         if (rc < 0)
1052                 RETURN(rc);
1053
1054         if (ma->ma_need & MA_LOV)
1055                 ma->ma_valid |= MA_LOV;
1056         else
1057                 ma->ma_valid |= MA_LMV;
1058
1059         ma->ma_lmm = info->eti_big_lmm;
1060         ma->ma_lmm_size = rc;
1061
1062         RETURN(0);
1063 }
1064
1065 static int echo_attr_get_complex(const struct lu_env *env,
1066                                  struct md_object *next,
1067                                  struct md_attr *ma)
1068 {
1069         struct echo_thread_info *info = echo_env_info(env);
1070         struct lu_buf           *buf = &info->eti_buf;
1071         umode_t                  mode = lu_object_attr(&next->mo_lu);
1072         int                      rc = 0, rc2;
1073
1074         ENTRY;
1075
1076         ma->ma_valid = 0;
1077
1078         if (ma->ma_need & MA_INODE) {
1079                 rc = mo_attr_get(env, next, ma);
1080                 if (rc)
1081                         GOTO(out, rc);
1082                 ma->ma_valid |= MA_INODE;
1083         }
1084
1085         if ((ma->ma_need & MA_LOV) && (S_ISREG(mode) || S_ISDIR(mode))) {
1086                 LASSERT(ma->ma_lmm_size > 0);
1087                 buf->lb_buf = ma->ma_lmm;
1088                 buf->lb_len = ma->ma_lmm_size;
1089                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1090                 if (rc2 > 0) {
1091                         ma->ma_lmm_size = rc2;
1092                         ma->ma_valid |= MA_LOV;
1093                 } else if (rc2 == -ENODATA) {
1094                         /* no LOV EA */
1095                         ma->ma_lmm_size = 0;
1096                 } else if (rc2 == -ERANGE) {
1097                         rc2 = echo_big_lmm_get(env, next, ma);
1098                         if (rc2 < 0)
1099                                 GOTO(out, rc = rc2);
1100                 } else {
1101                         GOTO(out, rc = rc2);
1102                 }
1103         }
1104
1105         if ((ma->ma_need & MA_LMV) && S_ISDIR(mode)) {
1106                 LASSERT(ma->ma_lmm_size > 0);
1107                 buf->lb_buf = ma->ma_lmm;
1108                 buf->lb_len = ma->ma_lmm_size;
1109                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
1110                 if (rc2 > 0) {
1111                         ma->ma_lmm_size = rc2;
1112                         ma->ma_valid |= MA_LMV;
1113                 } else if (rc2 == -ENODATA) {
1114                         /* no LMV EA */
1115                         ma->ma_lmm_size = 0;
1116                 } else if (rc2 == -ERANGE) {
1117                         rc2 = echo_big_lmm_get(env, next, ma);
1118                         if (rc2 < 0)
1119                                 GOTO(out, rc = rc2);
1120                 } else {
1121                         GOTO(out, rc = rc2);
1122                 }
1123         }
1124
1125 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1126         if ((ma->ma_need & MA_ACL_DEF) && S_ISDIR(mode)) {
1127                 buf->lb_buf = ma->ma_acl;
1128                 buf->lb_len = ma->ma_acl_size;
1129                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1130                 if (rc2 > 0) {
1131                         ma->ma_acl_size = rc2;
1132                         ma->ma_valid |= MA_ACL_DEF;
1133                 } else if (rc2 == -ENODATA) {
1134                         /* no ACLs */
1135                         ma->ma_acl_size = 0;
1136                 } else {
1137                         GOTO(out, rc = rc2);
1138                 }
1139         }
1140 #endif
1141 out:
1142         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1143                rc, ma->ma_valid, ma->ma_lmm);
1144         RETURN(rc);
1145 }
1146
1147 static int
1148 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1149                         struct md_object *parent, struct lu_fid *fid,
1150                         struct lu_name *lname, struct md_op_spec *spec,
1151                         struct md_attr *ma)
1152 {
1153         struct lu_object        *ec_child, *child;
1154         struct lu_device        *ld = ed->ed_next;
1155         struct echo_thread_info *info = echo_env_info(env);
1156         struct lu_fid           *fid2 = &info->eti_fid2;
1157         struct lu_object_conf    conf = { .loc_flags = LOC_F_NEW };
1158         int                      rc;
1159
1160         ENTRY;
1161
1162         rc = mdo_lookup(env, parent, lname, fid2, spec);
1163         if (rc == 0)
1164                 return -EEXIST;
1165         else if (rc != -ENOENT)
1166                 return rc;
1167
1168         ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1169                                      fid, &conf);
1170         if (IS_ERR(ec_child)) {
1171                 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1172                         PTR_ERR(ec_child));
1173                 RETURN(PTR_ERR(ec_child));
1174         }
1175
1176         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1177         if (!child) {
1178                 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1179                 GOTO(out_put, rc = -EINVAL);
1180         }
1181
1182         CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1183                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1184
1185         /*
1186          * Do not perform lookup sanity check. We know that name does not exist.
1187          */
1188         spec->sp_cr_lookup = 0;
1189         rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1190         if (rc) {
1191                 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1192                 GOTO(out_put, rc);
1193         }
1194         CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc  = %d\n",
1195                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1196         EXIT;
1197 out_put:
1198         lu_object_put(env, ec_child);
1199         return rc;
1200 }
1201
1202 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1203                              struct md_attr *ma)
1204 {
1205         struct echo_thread_info *info = echo_env_info(env);
1206
1207         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1208                 ma->ma_lmm = (void *)&info->eti_lmm;
1209                 ma->ma_lmm_size = sizeof(info->eti_lmm);
1210         } else {
1211                 LASSERT(info->eti_big_lmmsize);
1212                 ma->ma_lmm = info->eti_big_lmm;
1213                 ma->ma_lmm_size = info->eti_big_lmmsize;
1214         }
1215
1216         return 0;
1217 }
1218
1219 static int
1220 echo_md_dir_stripe_choose(const struct lu_env *env, struct echo_device *ed,
1221                           struct lu_object *obj, const char *name,
1222                           unsigned int namelen, __u64 id,
1223                           struct lu_object **new_parent)
1224 {
1225         struct echo_thread_info *info = echo_env_info(env);
1226         struct md_attr          *ma = &info->eti_ma;
1227         struct lmv_mds_md_v1    *lmv;
1228         struct lu_device        *ld = ed->ed_next;
1229         unsigned int            idx;
1230         struct lu_name          tmp_ln_name;
1231         struct lu_fid           stripe_fid;
1232         struct lu_object        *stripe_obj;
1233         int                     rc;
1234
1235         LASSERT(obj != NULL);
1236         LASSERT(S_ISDIR(obj->lo_header->loh_attr));
1237
1238         memset(ma, 0, sizeof(*ma));
1239         echo_set_lmm_size(env, ld, ma);
1240         ma->ma_need = MA_LMV;
1241         rc = echo_attr_get_complex(env, lu2md(obj), ma);
1242         if (rc) {
1243                 CERROR("Can not getattr child "DFID": rc = %d\n",
1244                         PFID(lu_object_fid(obj)), rc);
1245                 return rc;
1246         }
1247
1248         if (!(ma->ma_valid & MA_LMV)) {
1249                 *new_parent = obj;
1250                 return 0;
1251         }
1252
1253         lmv = (struct lmv_mds_md_v1 *)ma->ma_lmm;
1254         if (!lmv_is_sane(lmv)) {
1255                 rc = -EINVAL;
1256                 CERROR("Invalid mds md magic %x "DFID": rc = %d\n",
1257                        le32_to_cpu(lmv->lmv_magic), PFID(lu_object_fid(obj)),
1258                        rc);
1259                 return rc;
1260         }
1261
1262         if (name) {
1263                 tmp_ln_name.ln_name = name;
1264                 tmp_ln_name.ln_namelen = namelen;
1265         } else {
1266                 LASSERT(id != -1);
1267                 echo_md_build_name(&tmp_ln_name, info->eti_name, id);
1268         }
1269
1270         idx = lmv_name_to_stripe_index(lmv, tmp_ln_name.ln_name,
1271                                        tmp_ln_name.ln_namelen);
1272
1273         LASSERT(idx < le32_to_cpu(lmv->lmv_stripe_count));
1274         fid_le_to_cpu(&stripe_fid, &lmv->lmv_stripe_fids[idx]);
1275
1276         stripe_obj = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, &stripe_fid,
1277                                        NULL);
1278         if (IS_ERR(stripe_obj)) {
1279                 rc = PTR_ERR(stripe_obj);
1280                 CERROR("Can not find the parent "DFID": rc = %d\n",
1281                        PFID(&stripe_fid), rc);
1282                 return rc;
1283         }
1284
1285         *new_parent = lu_object_locate(stripe_obj->lo_header, ld->ld_type);
1286         if (!*new_parent) {
1287                 lu_object_put(env, stripe_obj);
1288                 RETURN(-ENXIO);
1289         }
1290
1291         return rc;
1292 }
1293
1294 static int echo_create_md_object(const struct lu_env *env,
1295                                  struct echo_device *ed,
1296                                  struct lu_object *ec_parent,
1297                                  struct lu_fid *fid,
1298                                  char *name, int namelen,
1299                                   __u64 id, __u32 mode, int count,
1300                                  int stripe_count, int stripe_offset)
1301 {
1302         struct lu_object *parent;
1303         struct lu_object *new_parent;
1304         struct echo_thread_info *info = echo_env_info(env);
1305         struct lu_name *lname = &info->eti_lname;
1306         struct md_op_spec *spec = &info->eti_spec;
1307         struct md_attr *ma = &info->eti_ma;
1308         struct lu_device *ld = ed->ed_next;
1309         int rc = 0;
1310         int i;
1311
1312         ENTRY;
1313
1314         if (!ec_parent)
1315                 return -1;
1316         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1317         if (!parent)
1318                 RETURN(-ENXIO);
1319
1320         rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
1321                                        id, &new_parent);
1322         if (rc != 0)
1323                 RETURN(rc);
1324
1325         LASSERT(new_parent != NULL);
1326         memset(ma, 0, sizeof(*ma));
1327         memset(spec, 0, sizeof(*spec));
1328         echo_set_lmm_size(env, ld, ma);
1329         if (stripe_count != 0) {
1330                 spec->sp_cr_flags |= MDS_FMODE_WRITE;
1331                 if (stripe_count != -1) {
1332                         if (S_ISDIR(mode)) {
1333                                 struct lmv_user_md *lmu;
1334
1335                                 lmu = (struct lmv_user_md *)&info->eti_lum;
1336                                 lmu->lum_magic = LMV_USER_MAGIC;
1337                                 lmu->lum_stripe_offset = stripe_offset;
1338                                 lmu->lum_stripe_count = stripe_count;
1339                                 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
1340                                 spec->u.sp_ea.eadata = lmu;
1341                                 spec->u.sp_ea.eadatalen = sizeof(*lmu);
1342                         } else {
1343                                 struct lov_user_md_v3 *lum = &info->eti_lum;
1344
1345                                 lum->lmm_magic = LOV_USER_MAGIC_V3;
1346                                 lum->lmm_stripe_count = stripe_count;
1347                                 lum->lmm_stripe_offset = stripe_offset;
1348                                 lum->lmm_pattern = LOV_PATTERN_NONE;
1349                                 spec->u.sp_ea.eadata = lum;
1350                                 spec->u.sp_ea.eadatalen = sizeof(*lum);
1351                         }
1352                         spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1353                 }
1354         }
1355
1356         ma->ma_attr.la_mode = mode;
1357         ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1358         ma->ma_attr.la_ctime = ktime_get_real_seconds();
1359
1360         if (name) {
1361                 lname->ln_name = name;
1362                 lname->ln_namelen = namelen;
1363                 /* If name is specified, only create one object by name */
1364                 rc = echo_md_create_internal(env, ed, lu2md(new_parent), fid,
1365                                              lname, spec, ma);
1366                 GOTO(out_put, rc);
1367         }
1368
1369         /* Create multiple object sequenced by id */
1370         for (i = 0; i < count; i++) {
1371                 char *tmp_name = info->eti_name;
1372
1373                 echo_md_build_name(lname, tmp_name, id);
1374
1375                 rc = echo_md_create_internal(env, ed, lu2md(new_parent),
1376                                              fid, lname, spec, ma);
1377                 if (rc) {
1378                         CERROR("Can not create child %s: rc = %d\n", tmp_name,
1379                                 rc);
1380                         break;
1381                 }
1382                 id++;
1383                 fid->f_oid++;
1384         }
1385
1386 out_put:
1387         if (new_parent != parent)
1388                 lu_object_put(env, new_parent);
1389
1390         RETURN(rc);
1391 }
1392
1393 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1394                                         struct echo_device *ed,
1395                                         struct md_object *parent,
1396                                         struct lu_name *lname)
1397 {
1398         struct echo_thread_info *info = echo_env_info(env);
1399         struct lu_fid *fid = &info->eti_fid;
1400         struct lu_object *child;
1401         int rc;
1402
1403         ENTRY;
1404         CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1405                PFID(fid), parent);
1406
1407         rc = mdo_lookup(env, parent, lname, fid, NULL);
1408         if (rc) {
1409                 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1410                 RETURN(ERR_PTR(rc));
1411         }
1412
1413         /*
1414          * In the function below, .hs_keycmp resolves to
1415          * lu_obj_hop_keycmp()
1416          */
1417         child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1418
1419         RETURN(child);
1420 }
1421
1422 static int echo_setattr_object(const struct lu_env *env,
1423                                struct echo_device *ed,
1424                                struct lu_object *ec_parent,
1425                                __u64 id, int count)
1426 {
1427         struct lu_object *parent;
1428         struct lu_object *new_parent;
1429         struct echo_thread_info *info = echo_env_info(env);
1430         struct lu_name *lname = &info->eti_lname;
1431         char *name = info->eti_name;
1432         struct lu_device *ld = ed->ed_next;
1433         struct lu_buf *buf = &info->eti_buf;
1434         int rc = 0;
1435         int i;
1436
1437         ENTRY;
1438
1439         if (!ec_parent)
1440                 return -1;
1441         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1442         if (!parent)
1443                 RETURN(-ENXIO);
1444
1445         rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1446                                        &new_parent);
1447         if (rc != 0)
1448                 RETURN(rc);
1449
1450         for (i = 0; i < count; i++) {
1451                 struct lu_object *ec_child, *child;
1452
1453                 echo_md_build_name(lname, name, id);
1454
1455                 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1456                 if (IS_ERR(ec_child)) {
1457                         rc = PTR_ERR(ec_child);
1458                         CERROR("Can't find child %s: rc = %d\n",
1459                                 lname->ln_name, rc);
1460                         break;
1461                 }
1462
1463                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1464                 if (!child) {
1465                         CERROR("Can not locate the child %s\n", lname->ln_name);
1466                         lu_object_put(env, ec_child);
1467                         rc = -EINVAL;
1468                         break;
1469                 }
1470
1471                 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1472                        PFID(lu_object_fid(child)));
1473
1474                 buf->lb_buf = info->eti_xattr_buf;
1475                 buf->lb_len = sizeof(info->eti_xattr_buf);
1476
1477                 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1478                 rc = mo_xattr_set(env, lu2md(child), buf, name,
1479                                   LU_XATTR_CREATE);
1480                 if (rc < 0) {
1481                         CERROR("Can not setattr child "DFID": rc = %d\n",
1482                                 PFID(lu_object_fid(child)), rc);
1483                         lu_object_put(env, ec_child);
1484                         break;
1485                 }
1486                 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1487                        PFID(lu_object_fid(child)));
1488                 id++;
1489                 lu_object_put(env, ec_child);
1490         }
1491
1492         if (new_parent != parent)
1493                 lu_object_put(env, new_parent);
1494
1495         RETURN(rc);
1496 }
1497
1498 static int echo_getattr_object(const struct lu_env *env,
1499                                struct echo_device *ed,
1500                                struct lu_object *ec_parent,
1501                                __u64 id, int count)
1502 {
1503         struct lu_object *parent;
1504         struct lu_object *new_parent;
1505         struct echo_thread_info *info = echo_env_info(env);
1506         struct lu_name *lname = &info->eti_lname;
1507         char *name = info->eti_name;
1508         struct md_attr *ma = &info->eti_ma;
1509         struct lu_device *ld = ed->ed_next;
1510         int rc = 0;
1511         int i;
1512
1513         ENTRY;
1514
1515         if (!ec_parent)
1516                 return -1;
1517         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1518         if (!parent)
1519                 RETURN(-ENXIO);
1520
1521         rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1522                                        &new_parent);
1523         if (rc != 0)
1524                 RETURN(rc);
1525
1526         memset(ma, 0, sizeof(*ma));
1527         ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1528         ma->ma_acl = info->eti_xattr_buf;
1529         ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1530
1531         for (i = 0; i < count; i++) {
1532                 struct lu_object *ec_child, *child;
1533
1534                 ma->ma_valid = 0;
1535                 echo_md_build_name(lname, name, id);
1536                 echo_set_lmm_size(env, ld, ma);
1537
1538                 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1539                 if (IS_ERR(ec_child)) {
1540                         CERROR("Can't find child %s: rc = %ld\n",
1541                                lname->ln_name, PTR_ERR(ec_child));
1542                         RETURN(PTR_ERR(ec_child));
1543                 }
1544
1545                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1546                 if (!child) {
1547                         CERROR("Can not locate the child %s\n", lname->ln_name);
1548                         lu_object_put(env, ec_child);
1549                         RETURN(-EINVAL);
1550                 }
1551
1552                 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1553                        PFID(lu_object_fid(child)));
1554                 rc = echo_attr_get_complex(env, lu2md(child), ma);
1555                 if (rc) {
1556                         CERROR("Can not getattr child "DFID": rc = %d\n",
1557                                 PFID(lu_object_fid(child)), rc);
1558                         lu_object_put(env, ec_child);
1559                         break;
1560                 }
1561                 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1562                        PFID(lu_object_fid(child)));
1563                 id++;
1564                 lu_object_put(env, ec_child);
1565         }
1566
1567         if (new_parent != parent)
1568                 lu_object_put(env, new_parent);
1569
1570         RETURN(rc);
1571 }
1572
1573 static int echo_lookup_object(const struct lu_env *env,
1574                               struct echo_device *ed,
1575                               struct lu_object *ec_parent,
1576                               __u64 id, int count)
1577 {
1578         struct lu_object *parent;
1579         struct lu_object *new_parent;
1580         struct echo_thread_info *info = echo_env_info(env);
1581         struct lu_name *lname = &info->eti_lname;
1582         char *name = info->eti_name;
1583         struct lu_fid *fid = &info->eti_fid;
1584         struct lu_device *ld = ed->ed_next;
1585         int rc = 0;
1586         int i;
1587
1588         if (!ec_parent)
1589                 return -1;
1590         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1591         if (!parent)
1592                 return -ENXIO;
1593
1594         rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1595                                        &new_parent);
1596         if (rc != 0)
1597                 RETURN(rc);
1598
1599         /*prepare the requests*/
1600         for (i = 0; i < count; i++) {
1601                 echo_md_build_name(lname, name, id);
1602
1603                 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1604                        PFID(lu_object_fid(new_parent)), lname->ln_name,
1605                        new_parent);
1606
1607                 rc = mdo_lookup(env, lu2md(new_parent), lname, fid, NULL);
1608                 if (rc) {
1609                         CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1610                         break;
1611                 }
1612
1613                 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1614                        PFID(lu_object_fid(new_parent)), lname->ln_name,
1615                        new_parent);
1616
1617                 id++;
1618         }
1619
1620         if (new_parent != parent)
1621                 lu_object_put(env, new_parent);
1622
1623         return rc;
1624 }
1625
1626 static int echo_md_destroy_internal(const struct lu_env *env,
1627                                     struct echo_device *ed,
1628                                     struct md_object *parent,
1629                                     struct lu_name *lname,
1630                                     struct md_attr *ma)
1631 {
1632         struct lu_device   *ld = ed->ed_next;
1633         struct lu_object   *ec_child;
1634         struct lu_object   *child;
1635         int                 rc;
1636
1637         ENTRY;
1638
1639         ec_child = echo_md_lookup(env, ed, parent, lname);
1640         if (IS_ERR(ec_child)) {
1641                 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1642                         PTR_ERR(ec_child));
1643                 RETURN(PTR_ERR(ec_child));
1644         }
1645
1646         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1647         if (!child) {
1648                 CERROR("Can not locate the child %s\n", lname->ln_name);
1649                 GOTO(out_put, rc = -EINVAL);
1650         }
1651
1652         if (lu_object_remote(child)) {
1653                 CERROR("Can not destroy remote object %s: rc = %d\n",
1654                        lname->ln_name, -EPERM);
1655                 GOTO(out_put, rc = -EPERM);
1656         }
1657         CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1658                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1659
1660         rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1661         if (rc) {
1662                 CERROR("Can not unlink child %s: rc = %d\n",
1663                         lname->ln_name, rc);
1664                 GOTO(out_put, rc);
1665         }
1666         CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1667                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1668 out_put:
1669         lu_object_put(env, ec_child);
1670         return rc;
1671 }
1672
1673 static int echo_destroy_object(const struct lu_env *env,
1674                                struct echo_device *ed,
1675                                struct lu_object *ec_parent,
1676                                char *name, int namelen,
1677                                __u64 id, __u32 mode,
1678                                int count)
1679 {
1680         struct echo_thread_info *info = echo_env_info(env);
1681         struct lu_name          *lname = &info->eti_lname;
1682         struct md_attr          *ma = &info->eti_ma;
1683         struct lu_device        *ld = ed->ed_next;
1684         struct lu_object        *parent;
1685         struct lu_object        *new_parent;
1686         int                      rc = 0;
1687         int                      i;
1688
1689         ENTRY;
1690         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1691         if (!parent)
1692                 RETURN(-EINVAL);
1693
1694         rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
1695                                        id, &new_parent);
1696         if (rc != 0)
1697                 RETURN(rc);
1698
1699         memset(ma, 0, sizeof(*ma));
1700         ma->ma_attr.la_mode = mode;
1701         ma->ma_attr.la_valid = LA_CTIME;
1702         ma->ma_attr.la_ctime = ktime_get_real_seconds();
1703         ma->ma_need = MA_INODE;
1704         ma->ma_valid = 0;
1705
1706         if (name) {
1707                 lname->ln_name = name;
1708                 lname->ln_namelen = namelen;
1709                 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
1710                                               ma);
1711                 GOTO(out_put, rc);
1712         }
1713
1714         /*prepare the requests*/
1715         for (i = 0; i < count; i++) {
1716                 char *tmp_name = info->eti_name;
1717
1718                 ma->ma_valid = 0;
1719                 echo_md_build_name(lname, tmp_name, id);
1720
1721                 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
1722                                               ma);
1723                 if (rc) {
1724                         CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1725                         break;
1726                 }
1727                 id++;
1728         }
1729
1730 out_put:
1731         if (new_parent != parent)
1732                 lu_object_put(env, new_parent);
1733
1734         RETURN(rc);
1735 }
1736
1737 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1738                                            struct echo_device *ed, char *path,
1739                                            int path_len)
1740 {
1741         struct lu_device        *ld = ed->ed_next;
1742         struct echo_thread_info *info = echo_env_info(env);
1743         struct lu_fid           *fid = &info->eti_fid;
1744         struct lu_name          *lname = &info->eti_lname;
1745         struct lu_object        *parent = NULL;
1746         struct lu_object        *child = NULL;
1747         int                      rc = 0;
1748
1749         ENTRY;
1750         *fid = ed->ed_root_fid;
1751
1752         /*
1753          * In the function below, .hs_keycmp resolves to
1754          * lu_obj_hop_keycmp()
1755          */
1756         parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1757         if (IS_ERR(parent)) {
1758                 CERROR("Can not find the parent "DFID": rc = %ld\n",
1759                         PFID(fid), PTR_ERR(parent));
1760                 RETURN(parent);
1761         }
1762
1763         while (1) {
1764                 struct lu_object *ld_parent;
1765                 char *e;
1766
1767                 e = strsep(&path, "/");
1768                 if (!e)
1769                         break;
1770
1771                 if (e[0] == 0) {
1772                         if (!path || path[0] == '\0')
1773                                 break;
1774                         continue;
1775                 }
1776
1777                 lname->ln_name = e;
1778                 lname->ln_namelen = strlen(e);
1779
1780                 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1781                 if (!ld_parent) {
1782                         lu_object_put(env, parent);
1783                         rc = -EINVAL;
1784                         break;
1785                 }
1786
1787                 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1788                 lu_object_put(env, parent);
1789                 if (IS_ERR(child)) {
1790                         rc = (int)PTR_ERR(child);
1791                         CERROR("lookup %s under parent "DFID": rc = %d\n",
1792                                 lname->ln_name, PFID(lu_object_fid(ld_parent)),
1793                                 rc);
1794                         break;
1795                 }
1796                 parent = child;
1797         }
1798         if (rc)
1799                 RETURN(ERR_PTR(rc));
1800
1801         RETURN(parent);
1802 }
1803
1804 static void echo_ucred_init(struct lu_env *env)
1805 {
1806         struct lu_ucred *ucred = lu_ucred(env);
1807         kernel_cap_t kcap = current_cap();
1808
1809         ucred->uc_valid = UCRED_INVALID;
1810
1811         ucred->uc_suppgids[0] = -1;
1812         ucred->uc_suppgids[1] = -1;
1813
1814         ucred->uc_uid = ucred->uc_o_uid  =
1815                                 from_kuid(&init_user_ns, current_uid());
1816         ucred->uc_gid = ucred->uc_o_gid  =
1817                                 from_kgid(&init_user_ns, current_gid());
1818         ucred->uc_fsuid = ucred->uc_o_fsuid =
1819                                 from_kuid(&init_user_ns, current_fsuid());
1820         ucred->uc_fsgid = ucred->uc_o_fsgid =
1821                                 from_kgid(&init_user_ns, current_fsgid());
1822         ucred->uc_cap = current_cap();
1823
1824         /* remove fs privilege for non-root user. */
1825         if (ucred->uc_fsuid) {
1826                 kcap = cap_drop_nfsd_set(kcap);
1827                 kcap = cap_drop_fs_set(kcap);
1828         }
1829         ucred->uc_cap = kcap;
1830         ucred->uc_valid = UCRED_NEW;
1831         /* do not let rbac interfere with obdecho */
1832         ucred->uc_rbac_file_perms = 1;
1833         ucred->uc_rbac_dne_ops = 1;
1834         ucred->uc_rbac_quota_ops = 1;
1835         ucred->uc_rbac_byfid_ops = 1;
1836         ucred->uc_rbac_chlg_ops = 1;
1837         ucred->uc_rbac_fscrypt_admin = 1;
1838 }
1839
1840 static void echo_ucred_fini(struct lu_env *env)
1841 {
1842         struct lu_ucred *ucred = lu_ucred(env);
1843
1844         ucred->uc_valid = UCRED_INIT;
1845 }
1846
1847 static int echo_md_handler(struct echo_device *ed, int command,
1848                            char *path, int path_len, __u64 id, int count,
1849                            struct obd_ioctl_data *data)
1850 {
1851         struct echo_thread_info *info;
1852         struct lu_device *ld = ed->ed_next;
1853         struct lu_env *env;
1854         __u16 refcheck;
1855         struct lu_object *parent;
1856         char *name = NULL;
1857         int namelen = data->ioc_plen2;
1858         int rc = 0;
1859
1860         ENTRY;
1861         if (!ld) {
1862                 CERROR("MD echo client is not being initialized properly\n");
1863                 RETURN(-EINVAL);
1864         }
1865
1866         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1867                 CERROR("Only support MDD layer right now!\n");
1868                 RETURN(-EINVAL);
1869         }
1870
1871         env = cl_env_get(&refcheck);
1872         if (IS_ERR(env))
1873                 RETURN(PTR_ERR(env));
1874
1875         rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_SES_TAG);
1876         if (rc != 0)
1877                 GOTO(out_env, rc);
1878
1879         /* init big_lmm buffer */
1880         info = echo_env_info(env);
1881         LASSERT(info->eti_big_lmm == NULL);
1882         OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
1883         if (!info->eti_big_lmm)
1884                 GOTO(out_env, rc = -ENOMEM);
1885         info->eti_big_lmmsize = MIN_MD_SIZE;
1886
1887         parent = echo_resolve_path(env, ed, path, path_len);
1888         if (IS_ERR(parent)) {
1889                 CERROR("Can not resolve the path %s: rc = %ld\n", path,
1890                         PTR_ERR(parent));
1891                 GOTO(out_free, rc = PTR_ERR(parent));
1892         }
1893
1894         if (namelen > 0) {
1895                 OBD_ALLOC(name, namelen + 1);
1896                 if (!name)
1897                         GOTO(out_put, rc = -ENOMEM);
1898                 if (copy_from_user(name, data->ioc_pbuf2, namelen))
1899                         GOTO(out_name, rc = -EFAULT);
1900         }
1901
1902         echo_ucred_init(env);
1903
1904         switch (command) {
1905         case ECHO_MD_CREATE:
1906         case ECHO_MD_MKDIR: {
1907                 struct echo_thread_info *info = echo_env_info(env);
1908                 __u32 mode = data->ioc_obdo2.o_mode;
1909                 struct lu_fid *fid = &info->eti_fid;
1910                 int stripe_count = (int)data->ioc_obdo2.o_misc;
1911                 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
1912
1913                 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
1914                 if (rc != 0)
1915                         break;
1916
1917                 /*
1918                  * In the function below, .hs_keycmp resolves to
1919                  * lu_obj_hop_keycmp()
1920                  */
1921                 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
1922                                            id, mode, count, stripe_count,
1923                                            stripe_index);
1924                 break;
1925         }
1926         case ECHO_MD_DESTROY:
1927         case ECHO_MD_RMDIR: {
1928                 __u32 mode = data->ioc_obdo2.o_mode;
1929
1930                 rc = echo_destroy_object(env, ed, parent, name, namelen,
1931                                          id, mode, count);
1932                 break;
1933         }
1934         case ECHO_MD_LOOKUP:
1935                 rc = echo_lookup_object(env, ed, parent, id, count);
1936                 break;
1937         case ECHO_MD_GETATTR:
1938                 rc = echo_getattr_object(env, ed, parent, id, count);
1939                 break;
1940         case ECHO_MD_SETATTR:
1941                 rc = echo_setattr_object(env, ed, parent, id, count);
1942                 break;
1943         default:
1944                 CERROR("unknown command %d\n", command);
1945                 rc = -EINVAL;
1946                 break;
1947         }
1948         echo_ucred_fini(env);
1949
1950 out_name:
1951         if (name)
1952                 OBD_FREE(name, namelen + 1);
1953 out_put:
1954         lu_object_put(env, parent);
1955 out_free:
1956         LASSERT(info->eti_big_lmm);
1957         OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
1958         info->eti_big_lmm = NULL;
1959         info->eti_big_lmmsize = 0;
1960 out_env:
1961         cl_env_put(env, &refcheck);
1962         return rc;
1963 }
1964 #endif /* HAVE_SERVER_SUPPORT */
1965
1966 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
1967                               struct obdo *oa)
1968 {
1969         struct echo_object      *eco;
1970         struct echo_client_obd  *ec = ed->ed_ec;
1971         int created = 0;
1972         int rc;
1973
1974         ENTRY;
1975         if (!(oa->o_valid & OBD_MD_FLID) ||
1976             !(oa->o_valid & OBD_MD_FLGROUP) ||
1977             !fid_seq_is_echo(ostid_seq(&oa->o_oi))) {
1978                 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
1979                 RETURN(-EINVAL);
1980         }
1981
1982         if (ostid_id(&oa->o_oi) == 0) {
1983                 rc = ostid_set_id(&oa->o_oi, ++last_object_id);
1984                 if (rc)
1985                         GOTO(failed, rc);
1986         }
1987
1988         rc = obd_create(env, ec->ec_exp, oa);
1989         if (rc != 0) {
1990                 CERROR("Cannot create objects: rc = %d\n", rc);
1991                 GOTO(failed, rc);
1992         }
1993
1994         created = 1;
1995
1996         oa->o_valid |= OBD_MD_FLID;
1997
1998         eco = cl_echo_object_find(ed, &oa->o_oi);
1999         if (IS_ERR(eco))
2000                 GOTO(failed, rc = PTR_ERR(eco));
2001         cl_echo_object_put(eco);
2002
2003         CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2004         EXIT;
2005
2006 failed:
2007         if (created && rc != 0)
2008                 obd_destroy(env, ec->ec_exp, oa);
2009
2010         if (rc != 0)
2011                 CERROR("create object failed with: rc = %d\n", rc);
2012
2013         return rc;
2014 }
2015
2016 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2017                            struct obdo *oa)
2018 {
2019         struct echo_object *eco;
2020         int rc;
2021
2022         ENTRY;
2023         if (!(oa->o_valid & OBD_MD_FLID) ||
2024             !(oa->o_valid & OBD_MD_FLGROUP) ||
2025             ostid_id(&oa->o_oi) == 0) {
2026                 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2027                 RETURN(-EINVAL);
2028         }
2029
2030         rc = 0;
2031         eco = cl_echo_object_find(ed, &oa->o_oi);
2032         if (!IS_ERR(eco))
2033                 *ecop = eco;
2034         else
2035                 rc = PTR_ERR(eco);
2036
2037         RETURN(rc);
2038 }
2039
2040 static void echo_put_object(struct echo_object *eco)
2041 {
2042         int rc;
2043
2044         rc = cl_echo_object_put(eco);
2045         if (rc)
2046                 CERROR("%s: echo client drop an object failed: rc = %d\n",
2047                        eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2048 }
2049
2050 static void echo_client_page_debug_setup(struct page *page, int rw, u64 id,
2051                                          u64 offset, u64 count)
2052 {
2053         char *addr;
2054         u64 stripe_off;
2055         u64 stripe_id;
2056         int delta;
2057
2058         /* no partial pages on the client */
2059         LASSERT(count == PAGE_SIZE);
2060
2061         addr = kmap(page);
2062
2063         for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2064                 if (rw == OBD_BRW_WRITE) {
2065                         stripe_off = offset + delta;
2066                         stripe_id = id;
2067                 } else {
2068                         stripe_off = 0xdeadbeef00c0ffeeULL;
2069                         stripe_id = 0xdeadbeef00c0ffeeULL;
2070                 }
2071                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2072                                   stripe_off, stripe_id);
2073         }
2074
2075         kunmap(page);
2076 }
2077
2078 static int
2079 echo_client_page_debug_check(struct page *page, u64 id, u64 offset, u64 count)
2080 {
2081         u64 stripe_off;
2082         u64 stripe_id;
2083         char *addr;
2084         int delta;
2085         int rc;
2086         int rc2;
2087
2088         /* no partial pages on the client */
2089         LASSERT(count == PAGE_SIZE);
2090
2091         addr = kmap(page);
2092
2093         for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2094                 stripe_off = offset + delta;
2095                 stripe_id = id;
2096
2097                 rc2 = block_debug_check("test_brw",
2098                                         addr + delta, OBD_ECHO_BLOCK_SIZE,
2099                                         stripe_off, stripe_id);
2100                 if (rc2 != 0) {
2101                         CERROR("Error in echo object %#llx\n", id);
2102                         rc = rc2;
2103                 }
2104         }
2105
2106         kunmap(page);
2107         return rc;
2108 }
2109
2110 static int echo_client_prep_commit(const struct lu_env *env,
2111                                    struct obd_export *exp, int rw,
2112                                    struct obdo *oa, struct echo_object *eco,
2113                                    u64 offset, u64 count,
2114                                    u64 batch, int async)
2115 {
2116         struct obd_ioobj ioo;
2117         struct niobuf_local *lnb;
2118         struct niobuf_remote rnb;
2119         u64 off;
2120         u64 npages, tot_pages, apc;
2121         int i, ret = 0, brw_flags = 0;
2122
2123         ENTRY;
2124         if (count <= 0 || (count & ~PAGE_MASK) != 0)
2125                 RETURN(-EINVAL);
2126
2127         apc = npages = batch >> PAGE_SHIFT;
2128         tot_pages = count >> PAGE_SHIFT;
2129
2130         OBD_ALLOC_PTR_ARRAY_LARGE(lnb, apc);
2131         if (!lnb)
2132                 RETURN(-ENOMEM);
2133
2134         if (rw == OBD_BRW_WRITE && async)
2135                 brw_flags |= OBD_BRW_ASYNC;
2136
2137         obdo_to_ioobj(oa, &ioo);
2138
2139         off = offset;
2140
2141         for (; tot_pages > 0; tot_pages -= npages) {
2142                 int lpages;
2143
2144                 if (tot_pages < npages)
2145                         npages = tot_pages;
2146
2147                 rnb.rnb_offset = off;
2148                 rnb.rnb_len = npages * PAGE_SIZE;
2149                 rnb.rnb_flags = brw_flags;
2150                 ioo.ioo_bufcnt = 1;
2151                 off += npages * PAGE_SIZE;
2152
2153                 lpages = npages;
2154                 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, &rnb, &lpages, lnb);
2155                 if (ret != 0)
2156                         GOTO(out, ret);
2157
2158                 for (i = 0; i < lpages; i++) {
2159                         struct page *page = lnb[i].lnb_page;
2160
2161                         /* read past eof? */
2162                         if (!page && lnb[i].lnb_rc == 0)
2163                                 continue;
2164
2165                         if (async)
2166                                 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2167
2168                         if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2169                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2170                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2171                                 continue;
2172
2173                         if (rw == OBD_BRW_WRITE)
2174                                 echo_client_page_debug_setup(page, rw,
2175                                                         ostid_id(&oa->o_oi),
2176                                                         lnb[i].lnb_file_offset,
2177                                                         lnb[i].lnb_len);
2178                         else
2179                                 echo_client_page_debug_check(page,
2180                                                         ostid_id(&oa->o_oi),
2181                                                         lnb[i].lnb_file_offset,
2182                                                         lnb[i].lnb_len);
2183                 }
2184
2185                 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo, &rnb, npages, lnb,
2186                                    ret, rnb.rnb_len, ktime_set(0, 0));
2187                 if (ret != 0)
2188                         break;
2189
2190                 /* Reuse env context. */
2191                 lu_context_exit((struct lu_context *)&env->le_ctx);
2192                 lu_context_enter((struct lu_context *)&env->le_ctx);
2193         }
2194
2195 out:
2196         OBD_FREE_PTR_ARRAY_LARGE(lnb, apc);
2197
2198         RETURN(ret);
2199 }
2200
2201 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2202                                  struct obd_export *exp,
2203                                  struct obd_ioctl_data *data)
2204 {
2205         struct obd_device *obd = class_exp2obd(exp);
2206         struct echo_device *ed = obd2echo_dev(obd);
2207         struct echo_client_obd *ec = ed->ed_ec;
2208         struct obdo *oa = &data->ioc_obdo1;
2209         struct echo_object *eco;
2210         int rc;
2211         int async = 0;
2212         long test_mode;
2213
2214         ENTRY;
2215         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2216
2217         rc = echo_get_object(&eco, ed, oa);
2218         if (rc)
2219                 RETURN(rc);
2220
2221         oa->o_valid &= ~OBD_MD_FLHANDLE;
2222
2223         /* OFD/obdfilter works only via prep/commit */
2224         test_mode = (long)data->ioc_pbuf1;
2225         if (!ed->ed_next && test_mode != 3) {
2226                 test_mode = 3;
2227                 data->ioc_plen1 = data->ioc_count;
2228         }
2229
2230         if (test_mode == 3)
2231                 async = 1;
2232
2233         /* Truncate batch size to maximum */
2234         if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2235                 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2236
2237         switch (test_mode) {
2238         case 3:
2239                 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa, eco,
2240                                              data->ioc_offset, data->ioc_count,
2241                                              data->ioc_plen1, async);
2242                 break;
2243         default:
2244                 rc = -EINVAL;
2245         }
2246
2247         echo_put_object(eco);
2248
2249         RETURN(rc);
2250 }
2251
2252 static int
2253 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2254                       void *karg, void __user *uarg)
2255 {
2256 #ifdef HAVE_SERVER_SUPPORT
2257         struct tgt_session_info *tsi;
2258 #endif
2259         struct obd_device *obd = exp->exp_obd;
2260         struct echo_device *ed = obd2echo_dev(obd);
2261         struct echo_client_obd *ec = ed->ed_ec;
2262         struct echo_object *eco;
2263         struct obd_ioctl_data *data;
2264         struct lu_env *env;
2265         unsigned long env_tags = 0;
2266         __u16 refcheck;
2267         struct obdo *oa;
2268         struct lu_fid fid;
2269         int rw = OBD_BRW_READ;
2270         int rc = -EINVAL;
2271
2272         ENTRY;
2273         CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
2274                exp->exp_obd->obd_name, cmd, len, karg, uarg);
2275         if (unlikely(karg == NULL))
2276                 RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "karg=NULL", rc));
2277         data = karg;
2278
2279         oa = &data->ioc_obdo1;
2280         if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2281                 oa->o_valid |= OBD_MD_FLGROUP;
2282                 ostid_set_seq_echo(&oa->o_oi);
2283         }
2284
2285         /* This FID is unpacked just for validation at this point */
2286         rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2287         if (rc < 0)
2288                 RETURN(rc);
2289
2290         env = cl_env_get(&refcheck);
2291         if (IS_ERR(env))
2292                 RETURN(PTR_ERR(env));
2293
2294         lu_env_add(env);
2295
2296 #ifdef HAVE_SERVER_SUPPORT
2297         if (cmd == OBD_IOC_ECHO_MD || cmd == OBD_IOC_ECHO_ALLOC_SEQ)
2298                 env_tags = ECHO_MD_CTX_TAG;
2299         else
2300 #endif
2301                 env_tags = ECHO_DT_CTX_TAG;
2302
2303         rc = lu_env_refill_by_tags(env, env_tags, ECHO_SES_TAG);
2304         if (rc != 0)
2305                 GOTO(out, rc);
2306
2307 #ifdef HAVE_SERVER_SUPPORT
2308         tsi = tgt_ses_info(env);
2309         /* treat as local operation */
2310         tsi->tsi_exp = NULL;
2311         tsi->tsi_jobid = NULL;
2312 #endif
2313
2314         switch (cmd) {
2315         case OBD_IOC_CREATE:                    /* may create echo object */
2316                 if (!capable(CAP_SYS_ADMIN))
2317                         GOTO(out, rc = -EPERM);
2318
2319                 rc = echo_create_object(env, ed, oa);
2320                 GOTO(out, rc);
2321
2322 #ifdef HAVE_SERVER_SUPPORT
2323         case OBD_IOC_ECHO_MD: {
2324                 int count;
2325                 int cmd;
2326                 char *dir = NULL;
2327                 int dirlen;
2328                 __u64 id;
2329
2330                 if (!capable(CAP_SYS_ADMIN))
2331                         GOTO(out, rc = -EPERM);
2332
2333                 count = data->ioc_count;
2334                 cmd = data->ioc_command;
2335
2336                 id = data->ioc_obdo2.o_oi.oi.oi_id;
2337                 dirlen = data->ioc_plen1;
2338                 OBD_ALLOC(dir, dirlen + 1);
2339                 if (!dir)
2340                         GOTO(out, rc = -ENOMEM);
2341
2342                 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2343                         OBD_FREE(dir, data->ioc_plen1 + 1);
2344                         GOTO(out, rc = -EFAULT);
2345                 }
2346
2347                 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2348                 OBD_FREE(dir, dirlen + 1);
2349                 GOTO(out, rc);
2350         }
2351         case OBD_IOC_ECHO_ALLOC_SEQ: {
2352                 __u64            seq;
2353                 int              max_count;
2354
2355                 if (!capable(CAP_SYS_ADMIN))
2356                         GOTO(out, rc = -EPERM);
2357
2358                 rc = seq_client_get_seq(env, ed->ed_cl_seq, &seq);
2359                 if (rc < 0) {
2360                         CERROR("%s: Can not alloc seq: rc = %d\n",
2361                                obd->obd_name, rc);
2362                         GOTO(out, rc);
2363                 }
2364
2365                 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2366                         return -EFAULT;
2367
2368                 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2369                 if (copy_to_user(data->ioc_pbuf2, &max_count,
2370                                      data->ioc_plen2))
2371                         return -EFAULT;
2372                 GOTO(out, rc);
2373         }
2374 #endif /* HAVE_SERVER_SUPPORT */
2375         case OBD_IOC_DESTROY:
2376                 if (!capable(CAP_SYS_ADMIN))
2377                         GOTO(out, rc = -EPERM);
2378
2379                 rc = echo_get_object(&eco, ed, oa);
2380                 if (rc == 0) {
2381                         rc = obd_destroy(env, ec->ec_exp, oa);
2382                         if (rc == 0)
2383                                 eco->eo_deleted = 1;
2384                         echo_put_object(eco);
2385                 }
2386                 GOTO(out, rc);
2387
2388         case OBD_IOC_GETATTR:
2389                 rc = echo_get_object(&eco, ed, oa);
2390                 if (rc == 0) {
2391                         rc = obd_getattr(env, ec->ec_exp, oa);
2392                         echo_put_object(eco);
2393                 }
2394                 GOTO(out, rc);
2395
2396         case OBD_IOC_SETATTR:
2397                 if (!capable(CAP_SYS_ADMIN))
2398                         GOTO(out, rc = -EPERM);
2399
2400                 rc = echo_get_object(&eco, ed, oa);
2401                 if (rc == 0) {
2402                         rc = obd_setattr(env, ec->ec_exp, oa);
2403                         echo_put_object(eco);
2404                 }
2405                 GOTO(out, rc);
2406
2407         case OBD_IOC_BRW_WRITE:
2408                 if (!capable(CAP_SYS_ADMIN))
2409                         GOTO(out, rc = -EPERM);
2410
2411                 rw = OBD_BRW_WRITE;
2412                 fallthrough;
2413         case OBD_IOC_BRW_READ:
2414                 rc = echo_client_brw_ioctl(env, rw, exp, data);
2415                 GOTO(out, rc);
2416         default:
2417                 rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
2418                 break;
2419         }
2420
2421         EXIT;
2422 out:
2423         lu_env_remove(env);
2424         cl_env_put(env, &refcheck);
2425
2426         return rc;
2427 }
2428
2429 static int echo_client_setup(const struct lu_env *env,
2430                              struct obd_device *obd, struct lustre_cfg *lcfg)
2431 {
2432         struct echo_client_obd *ec = &obd->u.echo_client;
2433         struct obd_device *tgt;
2434         struct obd_uuid echo_uuid = { "ECHO_UUID" };
2435         struct obd_connect_data *ocd = NULL;
2436         int rc;
2437
2438         ENTRY;
2439         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2440                 CERROR("requires a TARGET OBD name\n");
2441                 RETURN(-EINVAL);
2442         }
2443
2444         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2445         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2446                 CERROR("device not attached or not set up (%s)\n",
2447                        lustre_cfg_string(lcfg, 1));
2448                 RETURN(-EINVAL);
2449         }
2450
2451         spin_lock_init(&ec->ec_lock);
2452         INIT_LIST_HEAD(&ec->ec_objects);
2453         INIT_LIST_HEAD(&ec->ec_locks);
2454         ec->ec_unique = 0;
2455
2456         lu_context_tags_update(ECHO_DT_CTX_TAG);
2457         lu_session_tags_update(ECHO_SES_TAG);
2458
2459         if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2460 #ifdef HAVE_SERVER_SUPPORT
2461                 lu_context_tags_update(ECHO_MD_CTX_TAG);
2462 #else
2463                 CERROR(
2464                        "Local operations are NOT supported on client side. Only remote operations are supported. Metadata client must be run on server side.\n");
2465 #endif
2466                 RETURN(0);
2467         }
2468
2469         OBD_ALLOC(ocd, sizeof(*ocd));
2470         if (!ocd) {
2471                 CERROR("Can't alloc ocd connecting to %s\n",
2472                        lustre_cfg_string(lcfg, 1));
2473                 return -ENOMEM;
2474         }
2475
2476         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2477                                  OBD_CONNECT_BRW_SIZE |
2478                                  OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2479                                  OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2480                                  OBD_CONNECT_FID | OBD_CONNECT_FLAGS2;
2481         ocd->ocd_connect_flags2 = OBD_CONNECT2_REP_MBITS;
2482
2483         ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2484         ocd->ocd_version = LUSTRE_VERSION_CODE;
2485         ocd->ocd_group = FID_SEQ_ECHO;
2486
2487         rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2488         if (rc == 0) {
2489                 /* Turn off pinger because it connects to tgt obd directly. */
2490                 spin_lock(&tgt->obd_dev_lock);
2491                 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2492                 spin_unlock(&tgt->obd_dev_lock);
2493         }
2494
2495         OBD_FREE(ocd, sizeof(*ocd));
2496
2497         if (rc != 0) {
2498                 CERROR("fail to connect to device %s\n",
2499                        lustre_cfg_string(lcfg, 1));
2500                 return rc;
2501         }
2502
2503         RETURN(rc);
2504 }
2505
2506 static int echo_client_cleanup(struct obd_device *obd)
2507 {
2508         struct echo_device *ed = obd2echo_dev(obd);
2509         struct echo_client_obd *ec = &obd->u.echo_client;
2510         int rc;
2511
2512         ENTRY;
2513         /*Do nothing for Metadata echo client*/
2514         if (!ed)
2515                 RETURN(0);
2516
2517         lu_session_tags_clear(ECHO_SES_TAG & ~LCT_SESSION);
2518         lu_context_tags_clear(ECHO_DT_CTX_TAG);
2519         if (ed->ed_next_ismd) {
2520 #ifdef HAVE_SERVER_SUPPORT
2521                 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2522 #else
2523                 CERROR(
2524                        "This is client-side only module, does not support metadata echo client.\n");
2525 #endif
2526                 RETURN(0);
2527         }
2528
2529         if (!list_empty(&obd->obd_exports)) {
2530                 CERROR("still has clients!\n");
2531                 RETURN(-EBUSY);
2532         }
2533
2534         LASSERT(refcount_read(&ec->ec_exp->exp_handle.h_ref) > 0);
2535         rc = obd_disconnect(ec->ec_exp);
2536         if (rc != 0)
2537                 CERROR("fail to disconnect device: %d\n", rc);
2538
2539         RETURN(rc);
2540 }
2541
2542 static int echo_client_connect(const struct lu_env *env,
2543                                struct obd_export **exp,
2544                                struct obd_device *src, struct obd_uuid *cluuid,
2545                                struct obd_connect_data *data, void *localdata)
2546 {
2547         int rc;
2548         struct lustre_handle conn = { 0 };
2549
2550         ENTRY;
2551         rc = class_connect(&conn, src, cluuid);
2552         if (rc == 0)
2553                 *exp = class_conn2export(&conn);
2554
2555         RETURN(rc);
2556 }
2557
2558 static int echo_client_disconnect(struct obd_export *exp)
2559 {
2560         int rc;
2561
2562         ENTRY;
2563         if (!exp)
2564                 GOTO(out, rc = -EINVAL);
2565
2566         rc = class_disconnect(exp);
2567         GOTO(out, rc);
2568 out:
2569         return rc;
2570 }
2571
2572 static const struct obd_ops echo_client_obd_ops = {
2573         .o_owner       = THIS_MODULE,
2574         .o_iocontrol   = echo_client_iocontrol,
2575         .o_connect     = echo_client_connect,
2576         .o_disconnect  = echo_client_disconnect
2577 };
2578
2579 static int __init obdecho_init(void)
2580 {
2581         int rc;
2582
2583         ENTRY;
2584         LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
2585
2586         LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
2587
2588         rc = libcfs_setup();
2589         if (rc)
2590                 return rc;
2591
2592 # ifdef HAVE_SERVER_SUPPORT
2593         rc = echo_persistent_pages_init();
2594         if (rc != 0)
2595                 goto failed_0;
2596
2597         rc = class_register_type(&echo_obd_ops, NULL, true,
2598                                  LUSTRE_ECHO_NAME, &echo_srv_type);
2599         if (rc != 0)
2600                 goto failed_1;
2601 # endif
2602
2603         rc = lu_kmem_init(echo_caches);
2604         if (rc == 0) {
2605                 rc = class_register_type(&echo_client_obd_ops, NULL, false,
2606                                          LUSTRE_ECHO_CLIENT_NAME,
2607                                          &echo_device_type);
2608                 if (rc)
2609                         lu_kmem_fini(echo_caches);
2610         }
2611
2612 # ifdef HAVE_SERVER_SUPPORT
2613         if (rc == 0)
2614                 RETURN(0);
2615
2616         class_unregister_type(LUSTRE_ECHO_NAME);
2617 failed_1:
2618         echo_persistent_pages_fini();
2619 failed_0:
2620 # endif
2621         RETURN(rc);
2622 }
2623
2624 static void __exit obdecho_exit(void)
2625 {
2626         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2627         lu_kmem_fini(echo_caches);
2628
2629 #ifdef HAVE_SERVER_SUPPORT
2630         class_unregister_type(LUSTRE_ECHO_NAME);
2631         echo_persistent_pages_fini();
2632 #endif
2633 }
2634
2635 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2636 MODULE_DESCRIPTION("Lustre Echo Client test driver");
2637 MODULE_VERSION(LUSTRE_VERSION_STRING);
2638 MODULE_LICENSE("GPL");
2639
2640 module_init(obdecho_init);
2641 module_exit(obdecho_exit);
2642
2643 /** @} echo_client */