Whamcloud - gitweb
LU-16524 sec: add fscrypt_admin rbac role
[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 = cfs_size_round(sizeof(struct cl_page));
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         /* coverity[overrun-buffer-val] */
951         obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
952         if (IS_ERR(obj))
953                 GOTO(out, eco = (void *)obj);
954
955         eco = cl2echo_obj(obj);
956         if (eco->eo_deleted) {
957                 cl_object_put(env, obj);
958                 eco = ERR_PTR(-EAGAIN);
959         }
960
961 out:
962         if (oinfo)
963                 OBD_FREE_PTR(oinfo);
964
965         cl_env_put(env, &refcheck);
966         RETURN(eco);
967 }
968
969 static int cl_echo_object_put(struct echo_object *eco)
970 {
971         struct lu_env *env;
972         struct cl_object *obj = echo_obj2cl(eco);
973         __u16  refcheck;
974
975         ENTRY;
976         env = cl_env_get(&refcheck);
977         if (IS_ERR(env))
978                 RETURN(PTR_ERR(env));
979
980         /* an external function to kill an object? */
981         if (eco->eo_deleted) {
982                 struct lu_object_header *loh = obj->co_lu.lo_header;
983
984                 LASSERT(&eco->eo_hdr == luh2coh(loh));
985                 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
986         }
987
988         cl_object_put(env, obj);
989         cl_env_put(env, &refcheck);
990         RETURN(0);
991 }
992
993 /** @} echo_exports */
994
995 static u64 last_object_id;
996
997 #ifdef HAVE_SERVER_SUPPORT
998 static void echo_md_build_name(struct lu_name *lname, char *name,
999                                       __u64 id)
1000 {
1001         snprintf(name, ETI_NAME_LEN, "%llu", id);
1002         lname->ln_name = name;
1003         lname->ln_namelen = strlen(name);
1004 }
1005
1006 /* similar to mdt_attr_get_complex */
1007 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1008                             struct md_attr *ma)
1009 {
1010         struct echo_thread_info *info = echo_env_info(env);
1011         int rc;
1012
1013         ENTRY;
1014
1015         LASSERT(ma->ma_lmm_size > 0);
1016
1017         LASSERT(ma->ma_need & (MA_LOV | MA_LMV));
1018         if (ma->ma_need & MA_LOV)
1019                 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1020         else
1021                 rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LMV);
1022
1023         if (rc < 0)
1024                 RETURN(rc);
1025
1026         /* big_lmm may need to be grown */
1027         if (info->eti_big_lmmsize < rc) {
1028                 int size = size_roundup_power2(rc);
1029
1030                 if (info->eti_big_lmmsize > 0) {
1031                         /* free old buffer */
1032                         LASSERT(info->eti_big_lmm);
1033                         OBD_FREE_LARGE(info->eti_big_lmm,
1034                                        info->eti_big_lmmsize);
1035                         info->eti_big_lmm = NULL;
1036                         info->eti_big_lmmsize = 0;
1037                 }
1038
1039                 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1040                 if (!info->eti_big_lmm)
1041                         RETURN(-ENOMEM);
1042                 info->eti_big_lmmsize = size;
1043         }
1044         LASSERT(info->eti_big_lmmsize >= rc);
1045
1046         info->eti_buf.lb_buf = info->eti_big_lmm;
1047         info->eti_buf.lb_len = info->eti_big_lmmsize;
1048         if (ma->ma_need & MA_LOV)
1049                 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1050         else
1051                 rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LMV);
1052         if (rc < 0)
1053                 RETURN(rc);
1054
1055         if (ma->ma_need & MA_LOV)
1056                 ma->ma_valid |= MA_LOV;
1057         else
1058                 ma->ma_valid |= MA_LMV;
1059
1060         ma->ma_lmm = info->eti_big_lmm;
1061         ma->ma_lmm_size = rc;
1062
1063         RETURN(0);
1064 }
1065
1066 static int echo_attr_get_complex(const struct lu_env *env,
1067                                  struct md_object *next,
1068                                  struct md_attr *ma)
1069 {
1070         struct echo_thread_info *info = echo_env_info(env);
1071         struct lu_buf           *buf = &info->eti_buf;
1072         umode_t                  mode = lu_object_attr(&next->mo_lu);
1073         int                      rc = 0, rc2;
1074
1075         ENTRY;
1076
1077         ma->ma_valid = 0;
1078
1079         if (ma->ma_need & MA_INODE) {
1080                 rc = mo_attr_get(env, next, ma);
1081                 if (rc)
1082                         GOTO(out, rc);
1083                 ma->ma_valid |= MA_INODE;
1084         }
1085
1086         if ((ma->ma_need & MA_LOV) && (S_ISREG(mode) || S_ISDIR(mode))) {
1087                 LASSERT(ma->ma_lmm_size > 0);
1088                 buf->lb_buf = ma->ma_lmm;
1089                 buf->lb_len = ma->ma_lmm_size;
1090                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1091                 if (rc2 > 0) {
1092                         ma->ma_lmm_size = rc2;
1093                         ma->ma_valid |= MA_LOV;
1094                 } else if (rc2 == -ENODATA) {
1095                         /* no LOV EA */
1096                         ma->ma_lmm_size = 0;
1097                 } else if (rc2 == -ERANGE) {
1098                         rc2 = echo_big_lmm_get(env, next, ma);
1099                         if (rc2 < 0)
1100                                 GOTO(out, rc = rc2);
1101                 } else {
1102                         GOTO(out, rc = rc2);
1103                 }
1104         }
1105
1106         if ((ma->ma_need & MA_LMV) && S_ISDIR(mode)) {
1107                 LASSERT(ma->ma_lmm_size > 0);
1108                 buf->lb_buf = ma->ma_lmm;
1109                 buf->lb_len = ma->ma_lmm_size;
1110                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LMV);
1111                 if (rc2 > 0) {
1112                         ma->ma_lmm_size = rc2;
1113                         ma->ma_valid |= MA_LMV;
1114                 } else if (rc2 == -ENODATA) {
1115                         /* no LMV EA */
1116                         ma->ma_lmm_size = 0;
1117                 } else if (rc2 == -ERANGE) {
1118                         rc2 = echo_big_lmm_get(env, next, ma);
1119                         if (rc2 < 0)
1120                                 GOTO(out, rc = rc2);
1121                 } else {
1122                         GOTO(out, rc = rc2);
1123                 }
1124         }
1125
1126 #ifdef CONFIG_LUSTRE_FS_POSIX_ACL
1127         if ((ma->ma_need & MA_ACL_DEF) && S_ISDIR(mode)) {
1128                 buf->lb_buf = ma->ma_acl;
1129                 buf->lb_len = ma->ma_acl_size;
1130                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1131                 if (rc2 > 0) {
1132                         ma->ma_acl_size = rc2;
1133                         ma->ma_valid |= MA_ACL_DEF;
1134                 } else if (rc2 == -ENODATA) {
1135                         /* no ACLs */
1136                         ma->ma_acl_size = 0;
1137                 } else {
1138                         GOTO(out, rc = rc2);
1139                 }
1140         }
1141 #endif
1142 out:
1143         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = %#llx ma_lmm=%p\n",
1144                rc, ma->ma_valid, ma->ma_lmm);
1145         RETURN(rc);
1146 }
1147
1148 static int
1149 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1150                         struct md_object *parent, struct lu_fid *fid,
1151                         struct lu_name *lname, struct md_op_spec *spec,
1152                         struct md_attr *ma)
1153 {
1154         struct lu_object        *ec_child, *child;
1155         struct lu_device        *ld = ed->ed_next;
1156         struct echo_thread_info *info = echo_env_info(env);
1157         struct lu_fid           *fid2 = &info->eti_fid2;
1158         struct lu_object_conf    conf = { .loc_flags = LOC_F_NEW };
1159         int                      rc;
1160
1161         ENTRY;
1162
1163         rc = mdo_lookup(env, parent, lname, fid2, spec);
1164         if (rc == 0)
1165                 return -EEXIST;
1166         else if (rc != -ENOENT)
1167                 return rc;
1168
1169         ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1170                                      fid, &conf);
1171         if (IS_ERR(ec_child)) {
1172                 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1173                         PTR_ERR(ec_child));
1174                 RETURN(PTR_ERR(ec_child));
1175         }
1176
1177         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1178         if (!child) {
1179                 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1180                 GOTO(out_put, rc = -EINVAL);
1181         }
1182
1183         CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1184                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1185
1186         /*
1187          * Do not perform lookup sanity check. We know that name does not exist.
1188          */
1189         spec->sp_cr_lookup = 0;
1190         rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1191         if (rc) {
1192                 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1193                 GOTO(out_put, rc);
1194         }
1195         CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc  = %d\n",
1196                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1197         EXIT;
1198 out_put:
1199         lu_object_put(env, ec_child);
1200         return rc;
1201 }
1202
1203 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1204                              struct md_attr *ma)
1205 {
1206         struct echo_thread_info *info = echo_env_info(env);
1207
1208         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1209                 ma->ma_lmm = (void *)&info->eti_lmm;
1210                 ma->ma_lmm_size = sizeof(info->eti_lmm);
1211         } else {
1212                 LASSERT(info->eti_big_lmmsize);
1213                 ma->ma_lmm = info->eti_big_lmm;
1214                 ma->ma_lmm_size = info->eti_big_lmmsize;
1215         }
1216
1217         return 0;
1218 }
1219
1220 static int
1221 echo_md_dir_stripe_choose(const struct lu_env *env, struct echo_device *ed,
1222                           struct lu_object *obj, const char *name,
1223                           unsigned int namelen, __u64 id,
1224                           struct lu_object **new_parent)
1225 {
1226         struct echo_thread_info *info = echo_env_info(env);
1227         struct md_attr          *ma = &info->eti_ma;
1228         struct lmv_mds_md_v1    *lmv;
1229         struct lu_device        *ld = ed->ed_next;
1230         unsigned int            idx;
1231         struct lu_name          tmp_ln_name;
1232         struct lu_fid           stripe_fid;
1233         struct lu_object        *stripe_obj;
1234         int                     rc;
1235
1236         LASSERT(obj != NULL);
1237         LASSERT(S_ISDIR(obj->lo_header->loh_attr));
1238
1239         memset(ma, 0, sizeof(*ma));
1240         echo_set_lmm_size(env, ld, ma);
1241         ma->ma_need = MA_LMV;
1242         rc = echo_attr_get_complex(env, lu2md(obj), ma);
1243         if (rc) {
1244                 CERROR("Can not getattr child "DFID": rc = %d\n",
1245                         PFID(lu_object_fid(obj)), rc);
1246                 return rc;
1247         }
1248
1249         if (!(ma->ma_valid & MA_LMV)) {
1250                 *new_parent = obj;
1251                 return 0;
1252         }
1253
1254         lmv = (struct lmv_mds_md_v1 *)ma->ma_lmm;
1255         if (!lmv_is_sane(lmv)) {
1256                 rc = -EINVAL;
1257                 CERROR("Invalid mds md magic %x "DFID": rc = %d\n",
1258                        le32_to_cpu(lmv->lmv_magic), PFID(lu_object_fid(obj)),
1259                        rc);
1260                 return rc;
1261         }
1262
1263         if (name) {
1264                 tmp_ln_name.ln_name = name;
1265                 tmp_ln_name.ln_namelen = namelen;
1266         } else {
1267                 LASSERT(id != -1);
1268                 echo_md_build_name(&tmp_ln_name, info->eti_name, id);
1269         }
1270
1271         idx = lmv_name_to_stripe_index(lmv, tmp_ln_name.ln_name,
1272                                        tmp_ln_name.ln_namelen);
1273
1274         LASSERT(idx < le32_to_cpu(lmv->lmv_stripe_count));
1275         fid_le_to_cpu(&stripe_fid, &lmv->lmv_stripe_fids[idx]);
1276
1277         stripe_obj = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, &stripe_fid,
1278                                        NULL);
1279         if (IS_ERR(stripe_obj)) {
1280                 rc = PTR_ERR(stripe_obj);
1281                 CERROR("Can not find the parent "DFID": rc = %d\n",
1282                        PFID(&stripe_fid), rc);
1283                 return rc;
1284         }
1285
1286         *new_parent = lu_object_locate(stripe_obj->lo_header, ld->ld_type);
1287         if (!*new_parent) {
1288                 lu_object_put(env, stripe_obj);
1289                 RETURN(-ENXIO);
1290         }
1291
1292         return rc;
1293 }
1294
1295 static int echo_create_md_object(const struct lu_env *env,
1296                                  struct echo_device *ed,
1297                                  struct lu_object *ec_parent,
1298                                  struct lu_fid *fid,
1299                                  char *name, int namelen,
1300                                   __u64 id, __u32 mode, int count,
1301                                  int stripe_count, int stripe_offset)
1302 {
1303         struct lu_object *parent;
1304         struct lu_object *new_parent;
1305         struct echo_thread_info *info = echo_env_info(env);
1306         struct lu_name *lname = &info->eti_lname;
1307         struct md_op_spec *spec = &info->eti_spec;
1308         struct md_attr *ma = &info->eti_ma;
1309         struct lu_device *ld = ed->ed_next;
1310         int rc = 0;
1311         int i;
1312
1313         ENTRY;
1314
1315         if (!ec_parent)
1316                 return -1;
1317         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1318         if (!parent)
1319                 RETURN(-ENXIO);
1320
1321         rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
1322                                        id, &new_parent);
1323         if (rc != 0)
1324                 RETURN(rc);
1325
1326         LASSERT(new_parent != NULL);
1327         memset(ma, 0, sizeof(*ma));
1328         memset(spec, 0, sizeof(*spec));
1329         echo_set_lmm_size(env, ld, ma);
1330         if (stripe_count != 0) {
1331                 spec->sp_cr_flags |= MDS_FMODE_WRITE;
1332                 if (stripe_count != -1) {
1333                         if (S_ISDIR(mode)) {
1334                                 struct lmv_user_md *lmu;
1335
1336                                 lmu = (struct lmv_user_md *)&info->eti_lum;
1337                                 lmu->lum_magic = LMV_USER_MAGIC;
1338                                 lmu->lum_stripe_offset = stripe_offset;
1339                                 lmu->lum_stripe_count = stripe_count;
1340                                 lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
1341                                 spec->u.sp_ea.eadata = lmu;
1342                                 spec->u.sp_ea.eadatalen = sizeof(*lmu);
1343                         } else {
1344                                 struct lov_user_md_v3 *lum = &info->eti_lum;
1345
1346                                 lum->lmm_magic = LOV_USER_MAGIC_V3;
1347                                 lum->lmm_stripe_count = stripe_count;
1348                                 lum->lmm_stripe_offset = stripe_offset;
1349                                 lum->lmm_pattern = LOV_PATTERN_NONE;
1350                                 spec->u.sp_ea.eadata = lum;
1351                                 spec->u.sp_ea.eadatalen = sizeof(*lum);
1352                         }
1353                         spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1354                 }
1355         }
1356
1357         ma->ma_attr.la_mode = mode;
1358         ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1359         ma->ma_attr.la_ctime = ktime_get_real_seconds();
1360
1361         if (name) {
1362                 lname->ln_name = name;
1363                 lname->ln_namelen = namelen;
1364                 /* If name is specified, only create one object by name */
1365                 rc = echo_md_create_internal(env, ed, lu2md(new_parent), fid,
1366                                              lname, spec, ma);
1367                 GOTO(out_put, rc);
1368         }
1369
1370         /* Create multiple object sequenced by id */
1371         for (i = 0; i < count; i++) {
1372                 char *tmp_name = info->eti_name;
1373
1374                 echo_md_build_name(lname, tmp_name, id);
1375
1376                 rc = echo_md_create_internal(env, ed, lu2md(new_parent),
1377                                              fid, lname, spec, ma);
1378                 if (rc) {
1379                         CERROR("Can not create child %s: rc = %d\n", tmp_name,
1380                                 rc);
1381                         break;
1382                 }
1383                 id++;
1384                 fid->f_oid++;
1385         }
1386
1387 out_put:
1388         if (new_parent != parent)
1389                 lu_object_put(env, new_parent);
1390
1391         RETURN(rc);
1392 }
1393
1394 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1395                                         struct echo_device *ed,
1396                                         struct md_object *parent,
1397                                         struct lu_name *lname)
1398 {
1399         struct echo_thread_info *info = echo_env_info(env);
1400         struct lu_fid *fid = &info->eti_fid;
1401         struct lu_object *child;
1402         int rc;
1403
1404         ENTRY;
1405         CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1406                PFID(fid), parent);
1407
1408         rc = mdo_lookup(env, parent, lname, fid, NULL);
1409         if (rc) {
1410                 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1411                 RETURN(ERR_PTR(rc));
1412         }
1413
1414         /*
1415          * In the function below, .hs_keycmp resolves to
1416          * lu_obj_hop_keycmp()
1417          */
1418         /* coverity[overrun-buffer-val] */
1419         child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1420
1421         RETURN(child);
1422 }
1423
1424 static int echo_setattr_object(const struct lu_env *env,
1425                                struct echo_device *ed,
1426                                struct lu_object *ec_parent,
1427                                __u64 id, int count)
1428 {
1429         struct lu_object *parent;
1430         struct lu_object *new_parent;
1431         struct echo_thread_info *info = echo_env_info(env);
1432         struct lu_name *lname = &info->eti_lname;
1433         char *name = info->eti_name;
1434         struct lu_device *ld = ed->ed_next;
1435         struct lu_buf *buf = &info->eti_buf;
1436         int rc = 0;
1437         int i;
1438
1439         ENTRY;
1440
1441         if (!ec_parent)
1442                 return -1;
1443         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1444         if (!parent)
1445                 RETURN(-ENXIO);
1446
1447         rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1448                                        &new_parent);
1449         if (rc != 0)
1450                 RETURN(rc);
1451
1452         for (i = 0; i < count; i++) {
1453                 struct lu_object *ec_child, *child;
1454
1455                 echo_md_build_name(lname, name, id);
1456
1457                 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1458                 if (IS_ERR(ec_child)) {
1459                         rc = PTR_ERR(ec_child);
1460                         CERROR("Can't find child %s: rc = %d\n",
1461                                 lname->ln_name, rc);
1462                         break;
1463                 }
1464
1465                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1466                 if (!child) {
1467                         CERROR("Can not locate the child %s\n", lname->ln_name);
1468                         lu_object_put(env, ec_child);
1469                         rc = -EINVAL;
1470                         break;
1471                 }
1472
1473                 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1474                        PFID(lu_object_fid(child)));
1475
1476                 buf->lb_buf = info->eti_xattr_buf;
1477                 buf->lb_len = sizeof(info->eti_xattr_buf);
1478
1479                 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1480                 rc = mo_xattr_set(env, lu2md(child), buf, name,
1481                                   LU_XATTR_CREATE);
1482                 if (rc < 0) {
1483                         CERROR("Can not setattr child "DFID": rc = %d\n",
1484                                 PFID(lu_object_fid(child)), rc);
1485                         lu_object_put(env, ec_child);
1486                         break;
1487                 }
1488                 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1489                        PFID(lu_object_fid(child)));
1490                 id++;
1491                 lu_object_put(env, ec_child);
1492         }
1493
1494         if (new_parent != parent)
1495                 lu_object_put(env, new_parent);
1496
1497         RETURN(rc);
1498 }
1499
1500 static int echo_getattr_object(const struct lu_env *env,
1501                                struct echo_device *ed,
1502                                struct lu_object *ec_parent,
1503                                __u64 id, int count)
1504 {
1505         struct lu_object *parent;
1506         struct lu_object *new_parent;
1507         struct echo_thread_info *info = echo_env_info(env);
1508         struct lu_name *lname = &info->eti_lname;
1509         char *name = info->eti_name;
1510         struct md_attr *ma = &info->eti_ma;
1511         struct lu_device *ld = ed->ed_next;
1512         int rc = 0;
1513         int i;
1514
1515         ENTRY;
1516
1517         if (!ec_parent)
1518                 return -1;
1519         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1520         if (!parent)
1521                 RETURN(-ENXIO);
1522
1523         rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1524                                        &new_parent);
1525         if (rc != 0)
1526                 RETURN(rc);
1527
1528         memset(ma, 0, sizeof(*ma));
1529         ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1530         ma->ma_acl = info->eti_xattr_buf;
1531         ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1532
1533         for (i = 0; i < count; i++) {
1534                 struct lu_object *ec_child, *child;
1535
1536                 ma->ma_valid = 0;
1537                 echo_md_build_name(lname, name, id);
1538                 echo_set_lmm_size(env, ld, ma);
1539
1540                 ec_child = echo_md_lookup(env, ed, lu2md(new_parent), lname);
1541                 if (IS_ERR(ec_child)) {
1542                         CERROR("Can't find child %s: rc = %ld\n",
1543                                lname->ln_name, PTR_ERR(ec_child));
1544                         RETURN(PTR_ERR(ec_child));
1545                 }
1546
1547                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1548                 if (!child) {
1549                         CERROR("Can not locate the child %s\n", lname->ln_name);
1550                         lu_object_put(env, ec_child);
1551                         RETURN(-EINVAL);
1552                 }
1553
1554                 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1555                        PFID(lu_object_fid(child)));
1556                 rc = echo_attr_get_complex(env, lu2md(child), ma);
1557                 if (rc) {
1558                         CERROR("Can not getattr child "DFID": rc = %d\n",
1559                                 PFID(lu_object_fid(child)), rc);
1560                         lu_object_put(env, ec_child);
1561                         break;
1562                 }
1563                 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1564                        PFID(lu_object_fid(child)));
1565                 id++;
1566                 lu_object_put(env, ec_child);
1567         }
1568
1569         if (new_parent != parent)
1570                 lu_object_put(env, new_parent);
1571
1572         RETURN(rc);
1573 }
1574
1575 static int echo_lookup_object(const struct lu_env *env,
1576                               struct echo_device *ed,
1577                               struct lu_object *ec_parent,
1578                               __u64 id, int count)
1579 {
1580         struct lu_object *parent;
1581         struct lu_object *new_parent;
1582         struct echo_thread_info *info = echo_env_info(env);
1583         struct lu_name *lname = &info->eti_lname;
1584         char *name = info->eti_name;
1585         struct lu_fid *fid = &info->eti_fid;
1586         struct lu_device *ld = ed->ed_next;
1587         int rc = 0;
1588         int i;
1589
1590         if (!ec_parent)
1591                 return -1;
1592         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1593         if (!parent)
1594                 return -ENXIO;
1595
1596         rc = echo_md_dir_stripe_choose(env, ed, parent, NULL, 0, id,
1597                                        &new_parent);
1598         if (rc != 0)
1599                 RETURN(rc);
1600
1601         /*prepare the requests*/
1602         for (i = 0; i < count; i++) {
1603                 echo_md_build_name(lname, name, id);
1604
1605                 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1606                        PFID(lu_object_fid(new_parent)), lname->ln_name,
1607                        new_parent);
1608
1609                 rc = mdo_lookup(env, lu2md(new_parent), lname, fid, NULL);
1610                 if (rc) {
1611                         CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1612                         break;
1613                 }
1614
1615                 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1616                        PFID(lu_object_fid(new_parent)), lname->ln_name,
1617                        new_parent);
1618
1619                 id++;
1620         }
1621
1622         if (new_parent != parent)
1623                 lu_object_put(env, new_parent);
1624
1625         return rc;
1626 }
1627
1628 static int echo_md_destroy_internal(const struct lu_env *env,
1629                                     struct echo_device *ed,
1630                                     struct md_object *parent,
1631                                     struct lu_name *lname,
1632                                     struct md_attr *ma)
1633 {
1634         struct lu_device   *ld = ed->ed_next;
1635         struct lu_object   *ec_child;
1636         struct lu_object   *child;
1637         int                 rc;
1638
1639         ENTRY;
1640
1641         ec_child = echo_md_lookup(env, ed, parent, lname);
1642         if (IS_ERR(ec_child)) {
1643                 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1644                         PTR_ERR(ec_child));
1645                 RETURN(PTR_ERR(ec_child));
1646         }
1647
1648         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1649         if (!child) {
1650                 CERROR("Can not locate the child %s\n", lname->ln_name);
1651                 GOTO(out_put, rc = -EINVAL);
1652         }
1653
1654         if (lu_object_remote(child)) {
1655                 CERROR("Can not destroy remote object %s: rc = %d\n",
1656                        lname->ln_name, -EPERM);
1657                 GOTO(out_put, rc = -EPERM);
1658         }
1659         CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1660                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1661
1662         rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1663         if (rc) {
1664                 CERROR("Can not unlink child %s: rc = %d\n",
1665                         lname->ln_name, rc);
1666                 GOTO(out_put, rc);
1667         }
1668         CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1669                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1670 out_put:
1671         lu_object_put(env, ec_child);
1672         return rc;
1673 }
1674
1675 static int echo_destroy_object(const struct lu_env *env,
1676                                struct echo_device *ed,
1677                                struct lu_object *ec_parent,
1678                                char *name, int namelen,
1679                                __u64 id, __u32 mode,
1680                                int count)
1681 {
1682         struct echo_thread_info *info = echo_env_info(env);
1683         struct lu_name          *lname = &info->eti_lname;
1684         struct md_attr          *ma = &info->eti_ma;
1685         struct lu_device        *ld = ed->ed_next;
1686         struct lu_object        *parent;
1687         struct lu_object        *new_parent;
1688         int                      rc = 0;
1689         int                      i;
1690
1691         ENTRY;
1692         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1693         if (!parent)
1694                 RETURN(-EINVAL);
1695
1696         rc = echo_md_dir_stripe_choose(env, ed, parent, name, namelen,
1697                                        id, &new_parent);
1698         if (rc != 0)
1699                 RETURN(rc);
1700
1701         memset(ma, 0, sizeof(*ma));
1702         ma->ma_attr.la_mode = mode;
1703         ma->ma_attr.la_valid = LA_CTIME;
1704         ma->ma_attr.la_ctime = ktime_get_real_seconds();
1705         ma->ma_need = MA_INODE;
1706         ma->ma_valid = 0;
1707
1708         if (name) {
1709                 lname->ln_name = name;
1710                 lname->ln_namelen = namelen;
1711                 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
1712                                               ma);
1713                 GOTO(out_put, rc);
1714         }
1715
1716         /*prepare the requests*/
1717         for (i = 0; i < count; i++) {
1718                 char *tmp_name = info->eti_name;
1719
1720                 ma->ma_valid = 0;
1721                 echo_md_build_name(lname, tmp_name, id);
1722
1723                 rc = echo_md_destroy_internal(env, ed, lu2md(new_parent), lname,
1724                                               ma);
1725                 if (rc) {
1726                         CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1727                         break;
1728                 }
1729                 id++;
1730         }
1731
1732 out_put:
1733         if (new_parent != parent)
1734                 lu_object_put(env, new_parent);
1735
1736         RETURN(rc);
1737 }
1738
1739 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1740                                            struct echo_device *ed, char *path,
1741                                            int path_len)
1742 {
1743         struct lu_device        *ld = ed->ed_next;
1744         struct echo_thread_info *info = echo_env_info(env);
1745         struct lu_fid           *fid = &info->eti_fid;
1746         struct lu_name          *lname = &info->eti_lname;
1747         struct lu_object        *parent = NULL;
1748         struct lu_object        *child = NULL;
1749         int                      rc = 0;
1750
1751         ENTRY;
1752         *fid = ed->ed_root_fid;
1753
1754         /*
1755          * In the function below, .hs_keycmp resolves to
1756          * lu_obj_hop_keycmp()
1757          */
1758         /* coverity[overrun-buffer-val] */
1759         parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1760         if (IS_ERR(parent)) {
1761                 CERROR("Can not find the parent "DFID": rc = %ld\n",
1762                         PFID(fid), PTR_ERR(parent));
1763                 RETURN(parent);
1764         }
1765
1766         while (1) {
1767                 struct lu_object *ld_parent;
1768                 char *e;
1769
1770                 e = strsep(&path, "/");
1771                 if (!e)
1772                         break;
1773
1774                 if (e[0] == 0) {
1775                         if (!path || path[0] == '\0')
1776                                 break;
1777                         continue;
1778                 }
1779
1780                 lname->ln_name = e;
1781                 lname->ln_namelen = strlen(e);
1782
1783                 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1784                 if (!ld_parent) {
1785                         lu_object_put(env, parent);
1786                         rc = -EINVAL;
1787                         break;
1788                 }
1789
1790                 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1791                 lu_object_put(env, parent);
1792                 if (IS_ERR(child)) {
1793                         rc = (int)PTR_ERR(child);
1794                         CERROR("lookup %s under parent "DFID": rc = %d\n",
1795                                 lname->ln_name, PFID(lu_object_fid(ld_parent)),
1796                                 rc);
1797                         break;
1798                 }
1799                 parent = child;
1800         }
1801         if (rc)
1802                 RETURN(ERR_PTR(rc));
1803
1804         RETURN(parent);
1805 }
1806
1807 static void echo_ucred_init(struct lu_env *env)
1808 {
1809         struct lu_ucred *ucred = lu_ucred(env);
1810         kernel_cap_t kcap = current_cap();
1811
1812         ucred->uc_valid = UCRED_INVALID;
1813
1814         ucred->uc_suppgids[0] = -1;
1815         ucred->uc_suppgids[1] = -1;
1816
1817         ucred->uc_uid = ucred->uc_o_uid  =
1818                                 from_kuid(&init_user_ns, current_uid());
1819         ucred->uc_gid = ucred->uc_o_gid  =
1820                                 from_kgid(&init_user_ns, current_gid());
1821         ucred->uc_fsuid = ucred->uc_o_fsuid =
1822                                 from_kuid(&init_user_ns, current_fsuid());
1823         ucred->uc_fsgid = ucred->uc_o_fsgid =
1824                                 from_kgid(&init_user_ns, current_fsgid());
1825         ucred->uc_cap = current_cap();
1826
1827         /* remove fs privilege for non-root user. */
1828         if (ucred->uc_fsuid) {
1829                 kcap = cap_drop_nfsd_set(kcap);
1830                 kcap = cap_drop_fs_set(kcap);
1831         }
1832         ucred->uc_cap = kcap;
1833         ucred->uc_valid = UCRED_NEW;
1834         /* do not let rbac interfere with obdecho */
1835         ucred->uc_rbac_file_perms = 1;
1836         ucred->uc_rbac_dne_ops = 1;
1837         ucred->uc_rbac_quota_ops = 1;
1838         ucred->uc_rbac_byfid_ops = 1;
1839         ucred->uc_rbac_chlg_ops = 1;
1840         ucred->uc_rbac_fscrypt_admin = 1;
1841 }
1842
1843 static void echo_ucred_fini(struct lu_env *env)
1844 {
1845         struct lu_ucred *ucred = lu_ucred(env);
1846
1847         ucred->uc_valid = UCRED_INIT;
1848 }
1849
1850 static int echo_md_handler(struct echo_device *ed, int command,
1851                            char *path, int path_len, __u64 id, int count,
1852                            struct obd_ioctl_data *data)
1853 {
1854         struct echo_thread_info *info;
1855         struct lu_device *ld = ed->ed_next;
1856         struct lu_env *env;
1857         __u16 refcheck;
1858         struct lu_object *parent;
1859         char *name = NULL;
1860         int namelen = data->ioc_plen2;
1861         int rc = 0;
1862
1863         ENTRY;
1864         if (!ld) {
1865                 CERROR("MD echo client is not being initialized properly\n");
1866                 RETURN(-EINVAL);
1867         }
1868
1869         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1870                 CERROR("Only support MDD layer right now!\n");
1871                 RETURN(-EINVAL);
1872         }
1873
1874         env = cl_env_get(&refcheck);
1875         if (IS_ERR(env))
1876                 RETURN(PTR_ERR(env));
1877
1878         rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_SES_TAG);
1879         if (rc != 0)
1880                 GOTO(out_env, rc);
1881
1882         /* init big_lmm buffer */
1883         info = echo_env_info(env);
1884         LASSERT(info->eti_big_lmm == NULL);
1885         OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
1886         if (!info->eti_big_lmm)
1887                 GOTO(out_env, rc = -ENOMEM);
1888         info->eti_big_lmmsize = MIN_MD_SIZE;
1889
1890         parent = echo_resolve_path(env, ed, path, path_len);
1891         if (IS_ERR(parent)) {
1892                 CERROR("Can not resolve the path %s: rc = %ld\n", path,
1893                         PTR_ERR(parent));
1894                 GOTO(out_free, rc = PTR_ERR(parent));
1895         }
1896
1897         if (namelen > 0) {
1898                 OBD_ALLOC(name, namelen + 1);
1899                 if (!name)
1900                         GOTO(out_put, rc = -ENOMEM);
1901                 if (copy_from_user(name, data->ioc_pbuf2, namelen))
1902                         GOTO(out_name, rc = -EFAULT);
1903         }
1904
1905         echo_ucred_init(env);
1906
1907         switch (command) {
1908         case ECHO_MD_CREATE:
1909         case ECHO_MD_MKDIR: {
1910                 struct echo_thread_info *info = echo_env_info(env);
1911                 __u32 mode = data->ioc_obdo2.o_mode;
1912                 struct lu_fid *fid = &info->eti_fid;
1913                 int stripe_count = (int)data->ioc_obdo2.o_misc;
1914                 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
1915
1916                 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
1917                 if (rc != 0)
1918                         break;
1919
1920                 /*
1921                  * In the function below, .hs_keycmp resolves to
1922                  * lu_obj_hop_keycmp()
1923                  */
1924                 /* coverity[overrun-buffer-val] */
1925                 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
1926                                            id, mode, count, stripe_count,
1927                                            stripe_index);
1928                 break;
1929         }
1930         case ECHO_MD_DESTROY:
1931         case ECHO_MD_RMDIR: {
1932                 __u32 mode = data->ioc_obdo2.o_mode;
1933
1934                 rc = echo_destroy_object(env, ed, parent, name, namelen,
1935                                          id, mode, count);
1936                 break;
1937         }
1938         case ECHO_MD_LOOKUP:
1939                 rc = echo_lookup_object(env, ed, parent, id, count);
1940                 break;
1941         case ECHO_MD_GETATTR:
1942                 rc = echo_getattr_object(env, ed, parent, id, count);
1943                 break;
1944         case ECHO_MD_SETATTR:
1945                 rc = echo_setattr_object(env, ed, parent, id, count);
1946                 break;
1947         default:
1948                 CERROR("unknown command %d\n", command);
1949                 rc = -EINVAL;
1950                 break;
1951         }
1952         echo_ucred_fini(env);
1953
1954 out_name:
1955         if (name)
1956                 OBD_FREE(name, namelen + 1);
1957 out_put:
1958         lu_object_put(env, parent);
1959 out_free:
1960         LASSERT(info->eti_big_lmm);
1961         OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
1962         info->eti_big_lmm = NULL;
1963         info->eti_big_lmmsize = 0;
1964 out_env:
1965         cl_env_put(env, &refcheck);
1966         return rc;
1967 }
1968 #endif /* HAVE_SERVER_SUPPORT */
1969
1970 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
1971                               struct obdo *oa)
1972 {
1973         struct echo_object      *eco;
1974         struct echo_client_obd  *ec = ed->ed_ec;
1975         int created = 0;
1976         int rc;
1977
1978         ENTRY;
1979         if (!(oa->o_valid & OBD_MD_FLID) ||
1980             !(oa->o_valid & OBD_MD_FLGROUP) ||
1981             !fid_seq_is_echo(ostid_seq(&oa->o_oi))) {
1982                 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
1983                 RETURN(-EINVAL);
1984         }
1985
1986         if (ostid_id(&oa->o_oi) == 0) {
1987                 rc = ostid_set_id(&oa->o_oi, ++last_object_id);
1988                 if (rc)
1989                         GOTO(failed, rc);
1990         }
1991
1992         rc = obd_create(env, ec->ec_exp, oa);
1993         if (rc != 0) {
1994                 CERROR("Cannot create objects: rc = %d\n", rc);
1995                 GOTO(failed, rc);
1996         }
1997
1998         created = 1;
1999
2000         oa->o_valid |= OBD_MD_FLID;
2001
2002         eco = cl_echo_object_find(ed, &oa->o_oi);
2003         if (IS_ERR(eco))
2004                 GOTO(failed, rc = PTR_ERR(eco));
2005         cl_echo_object_put(eco);
2006
2007         CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2008         EXIT;
2009
2010 failed:
2011         if (created && rc != 0)
2012                 obd_destroy(env, ec->ec_exp, oa);
2013
2014         if (rc != 0)
2015                 CERROR("create object failed with: rc = %d\n", rc);
2016
2017         return rc;
2018 }
2019
2020 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2021                            struct obdo *oa)
2022 {
2023         struct echo_object *eco;
2024         int rc;
2025
2026         ENTRY;
2027         if (!(oa->o_valid & OBD_MD_FLID) ||
2028             !(oa->o_valid & OBD_MD_FLGROUP) ||
2029             ostid_id(&oa->o_oi) == 0) {
2030                 CERROR("invalid oid "DOSTID"\n", POSTID(&oa->o_oi));
2031                 RETURN(-EINVAL);
2032         }
2033
2034         rc = 0;
2035         eco = cl_echo_object_find(ed, &oa->o_oi);
2036         if (!IS_ERR(eco))
2037                 *ecop = eco;
2038         else
2039                 rc = PTR_ERR(eco);
2040
2041         RETURN(rc);
2042 }
2043
2044 static void echo_put_object(struct echo_object *eco)
2045 {
2046         int rc;
2047
2048         rc = cl_echo_object_put(eco);
2049         if (rc)
2050                 CERROR("%s: echo client drop an object failed: rc = %d\n",
2051                        eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2052 }
2053
2054 static void echo_client_page_debug_setup(struct page *page, int rw, u64 id,
2055                                          u64 offset, u64 count)
2056 {
2057         char *addr;
2058         u64 stripe_off;
2059         u64 stripe_id;
2060         int delta;
2061
2062         /* no partial pages on the client */
2063         LASSERT(count == PAGE_SIZE);
2064
2065         addr = kmap(page);
2066
2067         for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2068                 if (rw == OBD_BRW_WRITE) {
2069                         stripe_off = offset + delta;
2070                         stripe_id = id;
2071                 } else {
2072                         stripe_off = 0xdeadbeef00c0ffeeULL;
2073                         stripe_id = 0xdeadbeef00c0ffeeULL;
2074                 }
2075                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2076                                   stripe_off, stripe_id);
2077         }
2078
2079         kunmap(page);
2080 }
2081
2082 static int
2083 echo_client_page_debug_check(struct page *page, u64 id, u64 offset, u64 count)
2084 {
2085         u64 stripe_off;
2086         u64 stripe_id;
2087         char *addr;
2088         int delta;
2089         int rc;
2090         int rc2;
2091
2092         /* no partial pages on the client */
2093         LASSERT(count == PAGE_SIZE);
2094
2095         addr = kmap(page);
2096
2097         for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2098                 stripe_off = offset + delta;
2099                 stripe_id = id;
2100
2101                 rc2 = block_debug_check("test_brw",
2102                                         addr + delta, OBD_ECHO_BLOCK_SIZE,
2103                                         stripe_off, stripe_id);
2104                 if (rc2 != 0) {
2105                         CERROR("Error in echo object %#llx\n", id);
2106                         rc = rc2;
2107                 }
2108         }
2109
2110         kunmap(page);
2111         return rc;
2112 }
2113
2114 static int echo_client_prep_commit(const struct lu_env *env,
2115                                    struct obd_export *exp, int rw,
2116                                    struct obdo *oa, struct echo_object *eco,
2117                                    u64 offset, u64 count,
2118                                    u64 batch, int async)
2119 {
2120         struct obd_ioobj ioo;
2121         struct niobuf_local *lnb;
2122         struct niobuf_remote rnb;
2123         u64 off;
2124         u64 npages, tot_pages, apc;
2125         int i, ret = 0, brw_flags = 0;
2126
2127         ENTRY;
2128         if (count <= 0 || (count & ~PAGE_MASK) != 0)
2129                 RETURN(-EINVAL);
2130
2131         apc = npages = batch >> PAGE_SHIFT;
2132         tot_pages = count >> PAGE_SHIFT;
2133
2134         OBD_ALLOC_PTR_ARRAY_LARGE(lnb, apc);
2135         if (!lnb)
2136                 RETURN(-ENOMEM);
2137
2138         if (rw == OBD_BRW_WRITE && async)
2139                 brw_flags |= OBD_BRW_ASYNC;
2140
2141         obdo_to_ioobj(oa, &ioo);
2142
2143         off = offset;
2144
2145         for (; tot_pages > 0; tot_pages -= npages) {
2146                 int lpages;
2147
2148                 if (tot_pages < npages)
2149                         npages = tot_pages;
2150
2151                 rnb.rnb_offset = off;
2152                 rnb.rnb_len = npages * PAGE_SIZE;
2153                 rnb.rnb_flags = brw_flags;
2154                 ioo.ioo_bufcnt = 1;
2155                 off += npages * PAGE_SIZE;
2156
2157                 lpages = npages;
2158                 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, &rnb, &lpages, lnb);
2159                 if (ret != 0)
2160                         GOTO(out, ret);
2161
2162                 for (i = 0; i < lpages; i++) {
2163                         struct page *page = lnb[i].lnb_page;
2164
2165                         /* read past eof? */
2166                         if (!page && lnb[i].lnb_rc == 0)
2167                                 continue;
2168
2169                         if (async)
2170                                 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2171
2172                         if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2173                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2174                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2175                                 continue;
2176
2177                         if (rw == OBD_BRW_WRITE)
2178                                 echo_client_page_debug_setup(page, rw,
2179                                                         ostid_id(&oa->o_oi),
2180                                                         lnb[i].lnb_file_offset,
2181                                                         lnb[i].lnb_len);
2182                         else
2183                                 echo_client_page_debug_check(page,
2184                                                         ostid_id(&oa->o_oi),
2185                                                         lnb[i].lnb_file_offset,
2186                                                         lnb[i].lnb_len);
2187                 }
2188
2189                 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo, &rnb, npages, lnb,
2190                                    ret, rnb.rnb_len, ktime_set(0, 0));
2191                 if (ret != 0)
2192                         break;
2193
2194                 /* Reuse env context. */
2195                 lu_context_exit((struct lu_context *)&env->le_ctx);
2196                 lu_context_enter((struct lu_context *)&env->le_ctx);
2197         }
2198
2199 out:
2200         OBD_FREE_PTR_ARRAY_LARGE(lnb, apc);
2201
2202         RETURN(ret);
2203 }
2204
2205 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2206                                  struct obd_export *exp,
2207                                  struct obd_ioctl_data *data)
2208 {
2209         struct obd_device *obd = class_exp2obd(exp);
2210         struct echo_device *ed = obd2echo_dev(obd);
2211         struct echo_client_obd *ec = ed->ed_ec;
2212         struct obdo *oa = &data->ioc_obdo1;
2213         struct echo_object *eco;
2214         int rc;
2215         int async = 0;
2216         long test_mode;
2217
2218         ENTRY;
2219         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2220
2221         rc = echo_get_object(&eco, ed, oa);
2222         if (rc)
2223                 RETURN(rc);
2224
2225         oa->o_valid &= ~OBD_MD_FLHANDLE;
2226
2227         /* OFD/obdfilter works only via prep/commit */
2228         test_mode = (long)data->ioc_pbuf1;
2229         if (!ed->ed_next && test_mode != 3) {
2230                 test_mode = 3;
2231                 data->ioc_plen1 = data->ioc_count;
2232         }
2233
2234         if (test_mode == 3)
2235                 async = 1;
2236
2237         /* Truncate batch size to maximum */
2238         if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2239                 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2240
2241         switch (test_mode) {
2242         case 3:
2243                 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa, eco,
2244                                              data->ioc_offset, data->ioc_count,
2245                                              data->ioc_plen1, async);
2246                 break;
2247         default:
2248                 rc = -EINVAL;
2249         }
2250
2251         echo_put_object(eco);
2252
2253         RETURN(rc);
2254 }
2255
2256 static int
2257 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2258                       void *karg, void __user *uarg)
2259 {
2260 #ifdef HAVE_SERVER_SUPPORT
2261         struct tgt_session_info *tsi;
2262 #endif
2263         struct obd_device      *obd = exp->exp_obd;
2264         struct echo_device     *ed = obd2echo_dev(obd);
2265         struct echo_client_obd *ec = ed->ed_ec;
2266         struct echo_object     *eco;
2267         struct obd_ioctl_data  *data = karg;
2268         struct lu_env          *env;
2269         unsigned long           env_tags = 0;
2270         __u16                   refcheck;
2271         struct obdo            *oa;
2272         struct lu_fid           fid;
2273         int                     rw = OBD_BRW_READ;
2274         int                     rc = 0;
2275
2276         ENTRY;
2277         oa = &data->ioc_obdo1;
2278         if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2279                 oa->o_valid |= OBD_MD_FLGROUP;
2280                 ostid_set_seq_echo(&oa->o_oi);
2281         }
2282
2283         /* This FID is unpacked just for validation at this point */
2284         rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2285         if (rc < 0)
2286                 RETURN(rc);
2287
2288         env = cl_env_get(&refcheck);
2289         if (IS_ERR(env))
2290                 RETURN(PTR_ERR(env));
2291
2292         lu_env_add(env);
2293
2294 #ifdef HAVE_SERVER_SUPPORT
2295         if (cmd == OBD_IOC_ECHO_MD || cmd == OBD_IOC_ECHO_ALLOC_SEQ)
2296                 env_tags = ECHO_MD_CTX_TAG;
2297         else
2298 #endif
2299                 env_tags = ECHO_DT_CTX_TAG;
2300
2301         rc = lu_env_refill_by_tags(env, env_tags, ECHO_SES_TAG);
2302         if (rc != 0)
2303                 GOTO(out, rc);
2304
2305 #ifdef HAVE_SERVER_SUPPORT
2306         tsi = tgt_ses_info(env);
2307         /* treat as local operation */
2308         tsi->tsi_exp = NULL;
2309         tsi->tsi_jobid = NULL;
2310 #endif
2311
2312         switch (cmd) {
2313         case OBD_IOC_CREATE:                    /* may create echo object */
2314                 if (!capable(CAP_SYS_ADMIN))
2315                         GOTO(out, rc = -EPERM);
2316
2317                 rc = echo_create_object(env, ed, oa);
2318                 GOTO(out, rc);
2319
2320 #ifdef HAVE_SERVER_SUPPORT
2321         case OBD_IOC_ECHO_MD: {
2322                 int count;
2323                 int cmd;
2324                 char *dir = NULL;
2325                 int dirlen;
2326                 __u64 id;
2327
2328                 if (!capable(CAP_SYS_ADMIN))
2329                         GOTO(out, rc = -EPERM);
2330
2331                 count = data->ioc_count;
2332                 cmd = data->ioc_command;
2333
2334                 id = data->ioc_obdo2.o_oi.oi.oi_id;
2335                 dirlen = data->ioc_plen1;
2336                 OBD_ALLOC(dir, dirlen + 1);
2337                 if (!dir)
2338                         GOTO(out, rc = -ENOMEM);
2339
2340                 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2341                         OBD_FREE(dir, data->ioc_plen1 + 1);
2342                         GOTO(out, rc = -EFAULT);
2343                 }
2344
2345                 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2346                 OBD_FREE(dir, dirlen + 1);
2347                 GOTO(out, rc);
2348         }
2349         case OBD_IOC_ECHO_ALLOC_SEQ: {
2350                 __u64            seq;
2351                 int              max_count;
2352
2353                 if (!capable(CAP_SYS_ADMIN))
2354                         GOTO(out, rc = -EPERM);
2355
2356                 rc = seq_client_get_seq(env, ed->ed_cl_seq, &seq);
2357                 if (rc < 0) {
2358                         CERROR("%s: Can not alloc seq: rc = %d\n",
2359                                obd->obd_name, rc);
2360                         GOTO(out, rc);
2361                 }
2362
2363                 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2364                         return -EFAULT;
2365
2366                 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2367                 if (copy_to_user(data->ioc_pbuf2, &max_count,
2368                                      data->ioc_plen2))
2369                         return -EFAULT;
2370                 GOTO(out, rc);
2371         }
2372 #endif /* HAVE_SERVER_SUPPORT */
2373         case OBD_IOC_DESTROY:
2374                 if (!capable(CAP_SYS_ADMIN))
2375                         GOTO(out, rc = -EPERM);
2376
2377                 rc = echo_get_object(&eco, ed, oa);
2378                 if (rc == 0) {
2379                         rc = obd_destroy(env, ec->ec_exp, oa);
2380                         if (rc == 0)
2381                                 eco->eo_deleted = 1;
2382                         echo_put_object(eco);
2383                 }
2384                 GOTO(out, rc);
2385
2386         case OBD_IOC_GETATTR:
2387                 rc = echo_get_object(&eco, ed, oa);
2388                 if (rc == 0) {
2389                         rc = obd_getattr(env, ec->ec_exp, oa);
2390                         echo_put_object(eco);
2391                 }
2392                 GOTO(out, rc);
2393
2394         case OBD_IOC_SETATTR:
2395                 if (!capable(CAP_SYS_ADMIN))
2396                         GOTO(out, rc = -EPERM);
2397
2398                 rc = echo_get_object(&eco, ed, oa);
2399                 if (rc == 0) {
2400                         rc = obd_setattr(env, ec->ec_exp, oa);
2401                         echo_put_object(eco);
2402                 }
2403                 GOTO(out, rc);
2404
2405         case OBD_IOC_BRW_WRITE:
2406                 if (!capable(CAP_SYS_ADMIN))
2407                         GOTO(out, rc = -EPERM);
2408
2409                 rw = OBD_BRW_WRITE;
2410                 fallthrough;
2411         case OBD_IOC_BRW_READ:
2412                 rc = echo_client_brw_ioctl(env, rw, exp, data);
2413                 GOTO(out, rc);
2414
2415         default:
2416                 CERROR("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2417                 GOTO(out, rc = -ENOTTY);
2418         }
2419
2420         EXIT;
2421 out:
2422         lu_env_remove(env);
2423         cl_env_put(env, &refcheck);
2424
2425         return rc;
2426 }
2427
2428 static int echo_client_setup(const struct lu_env *env,
2429                              struct obd_device *obd, struct lustre_cfg *lcfg)
2430 {
2431         struct echo_client_obd *ec = &obd->u.echo_client;
2432         struct obd_device *tgt;
2433         struct obd_uuid echo_uuid = { "ECHO_UUID" };
2434         struct obd_connect_data *ocd = NULL;
2435         int rc;
2436
2437         ENTRY;
2438         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2439                 CERROR("requires a TARGET OBD name\n");
2440                 RETURN(-EINVAL);
2441         }
2442
2443         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2444         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2445                 CERROR("device not attached or not set up (%s)\n",
2446                        lustre_cfg_string(lcfg, 1));
2447                 RETURN(-EINVAL);
2448         }
2449
2450         spin_lock_init(&ec->ec_lock);
2451         INIT_LIST_HEAD(&ec->ec_objects);
2452         INIT_LIST_HEAD(&ec->ec_locks);
2453         ec->ec_unique = 0;
2454
2455         lu_context_tags_update(ECHO_DT_CTX_TAG);
2456         lu_session_tags_update(ECHO_SES_TAG);
2457
2458         if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2459 #ifdef HAVE_SERVER_SUPPORT
2460                 lu_context_tags_update(ECHO_MD_CTX_TAG);
2461 #else
2462                 CERROR(
2463                        "Local operations are NOT supported on client side. Only remote operations are supported. Metadata client must be run on server side.\n");
2464 #endif
2465                 RETURN(0);
2466         }
2467
2468         OBD_ALLOC(ocd, sizeof(*ocd));
2469         if (!ocd) {
2470                 CERROR("Can't alloc ocd connecting to %s\n",
2471                        lustre_cfg_string(lcfg, 1));
2472                 return -ENOMEM;
2473         }
2474
2475         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2476                                  OBD_CONNECT_BRW_SIZE |
2477                                  OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2478                                  OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2479                                  OBD_CONNECT_FID | OBD_CONNECT_FLAGS2;
2480         ocd->ocd_connect_flags2 = OBD_CONNECT2_REP_MBITS;
2481
2482         ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2483         ocd->ocd_version = LUSTRE_VERSION_CODE;
2484         ocd->ocd_group = FID_SEQ_ECHO;
2485
2486         rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2487         if (rc == 0) {
2488                 /* Turn off pinger because it connects to tgt obd directly. */
2489                 spin_lock(&tgt->obd_dev_lock);
2490                 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2491                 spin_unlock(&tgt->obd_dev_lock);
2492         }
2493
2494         OBD_FREE(ocd, sizeof(*ocd));
2495
2496         if (rc != 0) {
2497                 CERROR("fail to connect to device %s\n",
2498                        lustre_cfg_string(lcfg, 1));
2499                 return rc;
2500         }
2501
2502         RETURN(rc);
2503 }
2504
2505 static int echo_client_cleanup(struct obd_device *obd)
2506 {
2507         struct echo_device *ed = obd2echo_dev(obd);
2508         struct echo_client_obd *ec = &obd->u.echo_client;
2509         int rc;
2510
2511         ENTRY;
2512         /*Do nothing for Metadata echo client*/
2513         if (!ed)
2514                 RETURN(0);
2515
2516         lu_session_tags_clear(ECHO_SES_TAG & ~LCT_SESSION);
2517         lu_context_tags_clear(ECHO_DT_CTX_TAG);
2518         if (ed->ed_next_ismd) {
2519 #ifdef HAVE_SERVER_SUPPORT
2520                 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2521 #else
2522                 CERROR(
2523                        "This is client-side only module, does not support metadata echo client.\n");
2524 #endif
2525                 RETURN(0);
2526         }
2527
2528         if (!list_empty(&obd->obd_exports)) {
2529                 CERROR("still has clients!\n");
2530                 RETURN(-EBUSY);
2531         }
2532
2533         LASSERT(refcount_read(&ec->ec_exp->exp_handle.h_ref) > 0);
2534         rc = obd_disconnect(ec->ec_exp);
2535         if (rc != 0)
2536                 CERROR("fail to disconnect device: %d\n", rc);
2537
2538         RETURN(rc);
2539 }
2540
2541 static int echo_client_connect(const struct lu_env *env,
2542                                struct obd_export **exp,
2543                                struct obd_device *src, struct obd_uuid *cluuid,
2544                                struct obd_connect_data *data, void *localdata)
2545 {
2546         int rc;
2547         struct lustre_handle conn = { 0 };
2548
2549         ENTRY;
2550         rc = class_connect(&conn, src, cluuid);
2551         if (rc == 0)
2552                 *exp = class_conn2export(&conn);
2553
2554         RETURN(rc);
2555 }
2556
2557 static int echo_client_disconnect(struct obd_export *exp)
2558 {
2559         int rc;
2560
2561         ENTRY;
2562         if (!exp)
2563                 GOTO(out, rc = -EINVAL);
2564
2565         rc = class_disconnect(exp);
2566         GOTO(out, rc);
2567 out:
2568         return rc;
2569 }
2570
2571 static const struct obd_ops echo_client_obd_ops = {
2572         .o_owner       = THIS_MODULE,
2573         .o_iocontrol   = echo_client_iocontrol,
2574         .o_connect     = echo_client_connect,
2575         .o_disconnect  = echo_client_disconnect
2576 };
2577
2578 static int __init obdecho_init(void)
2579 {
2580         int rc;
2581
2582         ENTRY;
2583         LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
2584
2585         LASSERT(PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
2586
2587 # ifdef HAVE_SERVER_SUPPORT
2588         rc = echo_persistent_pages_init();
2589         if (rc != 0)
2590                 goto failed_0;
2591
2592         rc = class_register_type(&echo_obd_ops, NULL, true,
2593                                  LUSTRE_ECHO_NAME, &echo_srv_type);
2594         if (rc != 0)
2595                 goto failed_1;
2596 # endif
2597
2598         rc = lu_kmem_init(echo_caches);
2599         if (rc == 0) {
2600                 rc = class_register_type(&echo_client_obd_ops, NULL, false,
2601                                          LUSTRE_ECHO_CLIENT_NAME,
2602                                          &echo_device_type);
2603                 if (rc)
2604                         lu_kmem_fini(echo_caches);
2605         }
2606
2607 # ifdef HAVE_SERVER_SUPPORT
2608         if (rc == 0)
2609                 RETURN(0);
2610
2611         class_unregister_type(LUSTRE_ECHO_NAME);
2612 failed_1:
2613         echo_persistent_pages_fini();
2614 failed_0:
2615 # endif
2616         RETURN(rc);
2617 }
2618
2619 static void __exit obdecho_exit(void)
2620 {
2621         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2622         lu_kmem_fini(echo_caches);
2623
2624 #ifdef HAVE_SERVER_SUPPORT
2625         class_unregister_type(LUSTRE_ECHO_NAME);
2626         echo_persistent_pages_fini();
2627 #endif
2628 }
2629
2630 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2631 MODULE_DESCRIPTION("Lustre Echo Client test driver");
2632 MODULE_VERSION(LUSTRE_VERSION_STRING);
2633 MODULE_LICENSE("GPL");
2634
2635 module_init(obdecho_init);
2636 module_exit(obdecho_exit);
2637
2638 /** @} echo_client */