Whamcloud - gitweb
LU-3259 clio: cl_lock simplification
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_ECHO
38 #include <libcfs/libcfs.h>
39
40 #include <obd.h>
41 #include <obd_support.h>
42 #include <obd_class.h>
43 #include <lustre_debug.h>
44 #include <lprocfs_status.h>
45 #include <cl_object.h>
46 #include <lustre_fid.h>
47 #include <lustre_acl.h>
48 #include <lustre_ioctl.h>
49 #include <lustre_net.h>
50 #ifdef HAVE_SERVER_SUPPORT
51 # include <md_object.h>
52 #endif /* HAVE_SERVER_SUPPORT */
53
54 #include "echo_internal.h"
55
56 /** \defgroup echo_client Echo Client
57  * @{
58  */
59
60 struct echo_device {
61         struct cl_device        ed_cl;
62         struct echo_client_obd *ed_ec;
63
64         struct cl_site          ed_site_myself;
65         struct cl_site         *ed_site;
66         struct lu_device       *ed_next;
67         int                     ed_next_islov;
68         int                     ed_next_ismd;
69         struct lu_client_seq   *ed_cl_seq;
70 };
71
72 struct echo_object {
73         struct cl_object        eo_cl;
74         struct cl_object_header eo_hdr;
75
76         struct echo_device     *eo_dev;
77         struct list_head        eo_obj_chain;
78         struct lov_stripe_md   *eo_lsm;
79         atomic_t                eo_npages;
80         int                     eo_deleted;
81 };
82
83 struct echo_object_conf {
84         struct cl_object_conf  eoc_cl;
85         struct lov_stripe_md **eoc_md;
86 };
87
88 struct echo_page {
89         struct cl_page_slice    ep_cl;
90         struct mutex            ep_lock;
91 };
92
93 struct echo_lock {
94         struct cl_lock_slice    el_cl;
95         struct list_head        el_chain;
96         struct echo_object     *el_object;
97         __u64                   el_cookie;
98         atomic_t                el_refcount;
99 };
100
101 static int echo_client_setup(const struct lu_env *env,
102                              struct obd_device *obddev,
103                              struct lustre_cfg *lcfg);
104 static int echo_client_cleanup(struct obd_device *obddev);
105
106
107 /** \defgroup echo_helpers Helper functions
108  * @{
109  */
110 static inline struct echo_device *cl2echo_dev(const struct cl_device *dev)
111 {
112         return container_of0(dev, struct echo_device, ed_cl);
113 }
114
115 static inline struct cl_device *echo_dev2cl(struct echo_device *d)
116 {
117         return &d->ed_cl;
118 }
119
120 static inline struct echo_device *obd2echo_dev(const struct obd_device *obd)
121 {
122         return cl2echo_dev(lu2cl_dev(obd->obd_lu_dev));
123 }
124
125 static inline struct cl_object *echo_obj2cl(struct echo_object *eco)
126 {
127         return &eco->eo_cl;
128 }
129
130 static inline struct echo_object *cl2echo_obj(const struct cl_object *o)
131 {
132         return container_of(o, struct echo_object, eo_cl);
133 }
134
135 static inline struct echo_page *cl2echo_page(const struct cl_page_slice *s)
136 {
137         return container_of(s, struct echo_page, ep_cl);
138 }
139
140 static inline struct echo_lock *cl2echo_lock(const struct cl_lock_slice *s)
141 {
142         return container_of(s, struct echo_lock, el_cl);
143 }
144
145 static inline struct cl_lock *echo_lock2cl(const struct echo_lock *ecl)
146 {
147         return ecl->el_cl.cls_lock;
148 }
149
150 static struct lu_context_key echo_thread_key;
151 static inline struct echo_thread_info *echo_env_info(const struct lu_env *env)
152 {
153         struct echo_thread_info *info;
154         info = lu_context_key_get(&env->le_ctx, &echo_thread_key);
155         LASSERT(info != NULL);
156         return info;
157 }
158
159 static inline
160 struct echo_object_conf *cl2echo_conf(const struct cl_object_conf *c)
161 {
162         return container_of(c, struct echo_object_conf, eoc_cl);
163 }
164
165 /** @} echo_helpers */
166
167 static struct echo_object *cl_echo_object_find(struct echo_device *d,
168                                                struct lov_stripe_md **lsm);
169 static int cl_echo_object_put(struct echo_object *eco);
170 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
171                               struct page **pages, int npages, int async);
172
173 struct echo_thread_info {
174         struct echo_object_conf eti_conf;
175         struct lustre_md        eti_md;
176
177         struct cl_2queue        eti_queue;
178         struct cl_io            eti_io;
179         struct cl_lock          eti_lock;
180         struct lu_fid           eti_fid;
181         struct lu_fid           eti_fid2;
182 #ifdef HAVE_SERVER_SUPPORT
183         struct md_op_spec       eti_spec;
184         struct lov_mds_md_v3    eti_lmm;
185         struct lov_user_md_v3   eti_lum;
186         struct md_attr          eti_ma;
187         struct lu_name          eti_lname;
188         /* per-thread values, can be re-used */
189         void                    *eti_big_lmm;
190         int                     eti_big_lmmsize;
191         char                    eti_name[20];
192         struct lu_buf           eti_buf;
193         char                    eti_xattr_buf[LUSTRE_POSIX_ACL_MAX_SIZE];
194 #endif
195 };
196
197 /* No session used right now */
198 struct echo_session_info {
199         unsigned long dummy;
200 };
201
202 static struct kmem_cache *echo_lock_kmem;
203 static struct kmem_cache *echo_object_kmem;
204 static struct kmem_cache *echo_thread_kmem;
205 static struct kmem_cache *echo_session_kmem;
206 /* static struct kmem_cache *echo_req_kmem; */
207
208 static struct lu_kmem_descr echo_caches[] = {
209         {
210                 .ckd_cache = &echo_lock_kmem,
211                 .ckd_name  = "echo_lock_kmem",
212                 .ckd_size  = sizeof (struct echo_lock)
213         },
214         {
215                 .ckd_cache = &echo_object_kmem,
216                 .ckd_name  = "echo_object_kmem",
217                 .ckd_size  = sizeof (struct echo_object)
218         },
219         {
220                 .ckd_cache = &echo_thread_kmem,
221                 .ckd_name  = "echo_thread_kmem",
222                 .ckd_size  = sizeof (struct echo_thread_info)
223         },
224         {
225                 .ckd_cache = &echo_session_kmem,
226                 .ckd_name  = "echo_session_kmem",
227                 .ckd_size  = sizeof (struct echo_session_info)
228         },
229         {
230                 .ckd_cache = NULL
231         }
232 };
233
234 /** \defgroup echo_page Page operations
235  *
236  * Echo page operations.
237  *
238  * @{
239  */
240 static int echo_page_own(const struct lu_env *env,
241                          const struct cl_page_slice *slice,
242                          struct cl_io *io, int nonblock)
243 {
244         struct echo_page *ep = cl2echo_page(slice);
245
246         if (!nonblock)
247                 mutex_lock(&ep->ep_lock);
248         else if (!mutex_trylock(&ep->ep_lock))
249                 return -EAGAIN;
250         return 0;
251 }
252
253 static void echo_page_disown(const struct lu_env *env,
254                              const struct cl_page_slice *slice,
255                              struct cl_io *io)
256 {
257         struct echo_page *ep = cl2echo_page(slice);
258
259         LASSERT(mutex_is_locked(&ep->ep_lock));
260         mutex_unlock(&ep->ep_lock);
261 }
262
263 static void echo_page_discard(const struct lu_env *env,
264                               const struct cl_page_slice *slice,
265                               struct cl_io *unused)
266 {
267         cl_page_delete(env, slice->cpl_page);
268 }
269
270 static int echo_page_is_vmlocked(const struct lu_env *env,
271                                  const struct cl_page_slice *slice)
272 {
273         if (mutex_is_locked(&cl2echo_page(slice)->ep_lock))
274                 return -EBUSY;
275         return -ENODATA;
276 }
277
278 static void echo_page_completion(const struct lu_env *env,
279                                  const struct cl_page_slice *slice,
280                                  int ioret)
281 {
282         LASSERT(slice->cpl_page->cp_sync_io != NULL);
283 }
284
285 static void echo_page_fini(const struct lu_env *env,
286                            struct cl_page_slice *slice)
287 {
288         struct echo_object *eco = cl2echo_obj(slice->cpl_obj);
289         ENTRY;
290
291         atomic_dec(&eco->eo_npages);
292         page_cache_release(slice->cpl_page->cp_vmpage);
293         EXIT;
294 }
295
296 static int echo_page_prep(const struct lu_env *env,
297                           const struct cl_page_slice *slice,
298                           struct cl_io *unused)
299 {
300         return 0;
301 }
302
303 static int echo_page_print(const struct lu_env *env,
304                            const struct cl_page_slice *slice,
305                            void *cookie, lu_printer_t printer)
306 {
307         struct echo_page *ep = cl2echo_page(slice);
308
309         (*printer)(env, cookie, LUSTRE_ECHO_CLIENT_NAME"-page@%p %d vm@%p\n",
310                    ep, mutex_is_locked(&ep->ep_lock),
311                    slice->cpl_page->cp_vmpage);
312         return 0;
313 }
314
315 static const struct cl_page_operations echo_page_ops = {
316         .cpo_own           = echo_page_own,
317         .cpo_disown        = echo_page_disown,
318         .cpo_discard       = echo_page_discard,
319         .cpo_fini          = echo_page_fini,
320         .cpo_print         = echo_page_print,
321         .cpo_is_vmlocked   = echo_page_is_vmlocked,
322         .io = {
323                 [CRT_READ] = {
324                         .cpo_prep        = echo_page_prep,
325                         .cpo_completion  = echo_page_completion,
326                 },
327                 [CRT_WRITE] = {
328                         .cpo_prep        = echo_page_prep,
329                         .cpo_completion  = echo_page_completion,
330                 }
331         }
332 };
333 /** @} echo_page */
334
335 /** \defgroup echo_lock Locking
336  *
337  * echo lock operations
338  *
339  * @{
340  */
341 static void echo_lock_fini(const struct lu_env *env,
342                            struct cl_lock_slice *slice)
343 {
344         struct echo_lock *ecl = cl2echo_lock(slice);
345
346         LASSERT(list_empty(&ecl->el_chain));
347         OBD_SLAB_FREE_PTR(ecl, echo_lock_kmem);
348 }
349
350 static struct cl_lock_operations echo_lock_ops = {
351         .clo_fini      = echo_lock_fini,
352 };
353
354 /** @} echo_lock */
355
356 /** \defgroup echo_cl_ops cl_object operations
357  *
358  * operations for cl_object
359  *
360  * @{
361  */
362 static int echo_page_init(const struct lu_env *env, struct cl_object *obj,
363                           struct cl_page *page, pgoff_t index)
364 {
365         struct echo_page *ep = cl_object_page_slice(obj, page);
366         struct echo_object *eco = cl2echo_obj(obj);
367         ENTRY;
368
369         page_cache_get(page->cp_vmpage);
370         mutex_init(&ep->ep_lock);
371         cl_page_slice_add(page, &ep->ep_cl, obj, index, &echo_page_ops);
372         atomic_inc(&eco->eo_npages);
373         RETURN(0);
374 }
375
376 static int echo_io_init(const struct lu_env *env, struct cl_object *obj,
377                         struct cl_io *io)
378 {
379         return 0;
380 }
381
382 static int echo_lock_init(const struct lu_env *env,
383                           struct cl_object *obj, struct cl_lock *lock,
384                           const struct cl_io *unused)
385 {
386         struct echo_lock *el;
387         ENTRY;
388
389         OBD_SLAB_ALLOC_PTR_GFP(el, echo_lock_kmem, GFP_NOFS);
390         if (el != NULL) {
391                 cl_lock_slice_add(lock, &el->el_cl, obj, &echo_lock_ops);
392                 el->el_object = cl2echo_obj(obj);
393                 INIT_LIST_HEAD(&el->el_chain);
394                 atomic_set(&el->el_refcount, 0);
395         }
396         RETURN(el == NULL ? -ENOMEM : 0);
397 }
398
399 static int echo_conf_set(const struct lu_env *env, struct cl_object *obj,
400                          const struct cl_object_conf *conf)
401 {
402         return 0;
403 }
404
405 static const struct cl_object_operations echo_cl_obj_ops = {
406         .coo_page_init = echo_page_init,
407         .coo_lock_init = echo_lock_init,
408         .coo_io_init   = echo_io_init,
409         .coo_conf_set  = echo_conf_set
410 };
411 /** @} echo_cl_ops */
412
413 /** \defgroup echo_lu_ops lu_object operations
414  *
415  * operations for echo lu object.
416  *
417  * @{
418  */
419 static int echo_object_init(const struct lu_env *env, struct lu_object *obj,
420                             const struct lu_object_conf *conf)
421 {
422         struct echo_device *ed         = cl2echo_dev(lu2cl_dev(obj->lo_dev));
423         struct echo_client_obd *ec     = ed->ed_ec;
424         struct echo_object *eco        = cl2echo_obj(lu2cl(obj));
425         ENTRY;
426
427         if (ed->ed_next) {
428                 struct lu_object  *below;
429                 struct lu_device  *under;
430
431                 under = ed->ed_next;
432                 below = under->ld_ops->ldo_object_alloc(env, obj->lo_header,
433                                                         under);
434                 if (below == NULL)
435                         RETURN(-ENOMEM);
436                 lu_object_add(obj, below);
437         }
438
439         if (!ed->ed_next_ismd) {
440                 const struct cl_object_conf *cconf = lu2cl_conf(conf);
441                 struct echo_object_conf *econf = cl2echo_conf(cconf);
442
443                 LASSERT(econf->eoc_md);
444                 eco->eo_lsm = *econf->eoc_md;
445                 /* clear the lsm pointer so that it won't get freed. */
446                 *econf->eoc_md = NULL;
447         } else {
448                 eco->eo_lsm = NULL;
449         }
450
451         eco->eo_dev = ed;
452         atomic_set(&eco->eo_npages, 0);
453         cl_object_page_init(lu2cl(obj), sizeof(struct echo_page));
454
455         spin_lock(&ec->ec_lock);
456         list_add_tail(&eco->eo_obj_chain, &ec->ec_objects);
457         spin_unlock(&ec->ec_lock);
458
459         RETURN(0);
460 }
461
462 /* taken from osc_unpackmd() */
463 static int echo_alloc_memmd(struct echo_device *ed,
464                             struct lov_stripe_md **lsmp)
465 {
466         int lsm_size;
467
468         ENTRY;
469
470         /* If export is lov/osc then use their obd method */
471         if (ed->ed_next != NULL)
472                 return obd_alloc_memmd(ed->ed_ec->ec_exp, lsmp);
473         /* OFD has no unpackmd method, do everything here */
474         lsm_size = lov_stripe_md_size(1);
475
476         LASSERT(*lsmp == NULL);
477         OBD_ALLOC(*lsmp, lsm_size);
478         if (*lsmp == NULL)
479                 RETURN(-ENOMEM);
480
481         OBD_ALLOC((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
482         if ((*lsmp)->lsm_oinfo[0] == NULL) {
483                 OBD_FREE(*lsmp, lsm_size);
484                 RETURN(-ENOMEM);
485         }
486
487         loi_init((*lsmp)->lsm_oinfo[0]);
488         (*lsmp)->lsm_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
489         ostid_set_seq_echo(&(*lsmp)->lsm_oi);
490
491         RETURN(lsm_size);
492 }
493
494 static int echo_free_memmd(struct echo_device *ed, struct lov_stripe_md **lsmp)
495 {
496         int lsm_size;
497
498         ENTRY;
499
500         /* If export is lov/osc then use their obd method */
501         if (ed->ed_next != NULL)
502                 return obd_free_memmd(ed->ed_ec->ec_exp, lsmp);
503         /* OFD has no unpackmd method, do everything here */
504         lsm_size = lov_stripe_md_size(1);
505
506         LASSERT(*lsmp != NULL);
507         OBD_FREE((*lsmp)->lsm_oinfo[0], sizeof(struct lov_oinfo));
508         OBD_FREE(*lsmp, lsm_size);
509         *lsmp = NULL;
510         RETURN(0);
511 }
512
513 static void echo_object_free(const struct lu_env *env, struct lu_object *obj)
514 {
515         struct echo_object *eco    = cl2echo_obj(lu2cl(obj));
516         struct echo_client_obd *ec = eco->eo_dev->ed_ec;
517         ENTRY;
518
519         LASSERT(atomic_read(&eco->eo_npages) == 0);
520
521         spin_lock(&ec->ec_lock);
522         list_del_init(&eco->eo_obj_chain);
523         spin_unlock(&ec->ec_lock);
524
525         lu_object_fini(obj);
526         lu_object_header_fini(obj->lo_header);
527
528         if (eco->eo_lsm)
529                 echo_free_memmd(eco->eo_dev, &eco->eo_lsm);
530         OBD_SLAB_FREE_PTR(eco, echo_object_kmem);
531         EXIT;
532 }
533
534 static int echo_object_print(const struct lu_env *env, void *cookie,
535                             lu_printer_t p, const struct lu_object *o)
536 {
537         struct echo_object *obj = cl2echo_obj(lu2cl(o));
538
539         return (*p)(env, cookie, "echoclient-object@%p", obj);
540 }
541
542 static const struct lu_object_operations echo_lu_obj_ops = {
543         .loo_object_init      = echo_object_init,
544         .loo_object_delete    = NULL,
545         .loo_object_release   = NULL,
546         .loo_object_free      = echo_object_free,
547         .loo_object_print     = echo_object_print,
548         .loo_object_invariant = NULL
549 };
550 /** @} echo_lu_ops */
551
552 /** \defgroup echo_lu_dev_ops  lu_device operations
553  *
554  * Operations for echo lu device.
555  *
556  * @{
557  */
558 static struct lu_object *echo_object_alloc(const struct lu_env *env,
559                                            const struct lu_object_header *hdr,
560                                            struct lu_device *dev)
561 {
562         struct echo_object *eco;
563         struct lu_object *obj = NULL;
564         ENTRY;
565
566         /* we're the top dev. */
567         LASSERT(hdr == NULL);
568         OBD_SLAB_ALLOC_PTR_GFP(eco, echo_object_kmem, GFP_NOFS);
569         if (eco != NULL) {
570                 struct cl_object_header *hdr = &eco->eo_hdr;
571
572                 obj = &echo_obj2cl(eco)->co_lu;
573                 cl_object_header_init(hdr);
574                 hdr->coh_page_bufsize = cfs_size_round(sizeof(struct cl_page));
575
576                 lu_object_init(obj, &hdr->coh_lu, dev);
577                 lu_object_add_top(&hdr->coh_lu, obj);
578
579                 eco->eo_cl.co_ops = &echo_cl_obj_ops;
580                 obj->lo_ops       = &echo_lu_obj_ops;
581         }
582         RETURN(obj);
583 }
584
585 static struct lu_device_operations echo_device_lu_ops = {
586         .ldo_object_alloc   = echo_object_alloc,
587 };
588
589 /** @} echo_lu_dev_ops */
590
591 static struct cl_device_operations echo_device_cl_ops = {
592 };
593
594 /** \defgroup echo_init Setup and teardown
595  *
596  * Init and fini functions for echo client.
597  *
598  * @{
599  */
600 static int echo_site_init(const struct lu_env *env, struct echo_device *ed)
601 {
602         struct cl_site *site = &ed->ed_site_myself;
603         int rc;
604
605         /* initialize site */
606         rc = cl_site_init(site, &ed->ed_cl);
607         if (rc) {
608                 CERROR("Cannot initilize site for echo client(%d)\n", rc);
609                 return rc;
610         }
611
612         rc = lu_site_init_finish(&site->cs_lu);
613         if (rc)
614                 return rc;
615
616         ed->ed_site = site;
617         return 0;
618 }
619
620 static void echo_site_fini(const struct lu_env *env, struct echo_device *ed)
621 {
622         if (ed->ed_site) {
623                 if (!ed->ed_next_ismd)
624                         cl_site_fini(ed->ed_site);
625                 ed->ed_site = NULL;
626         }
627 }
628
629 static void *echo_thread_key_init(const struct lu_context *ctx,
630                                   struct lu_context_key *key)
631 {
632         struct echo_thread_info *info;
633
634         OBD_SLAB_ALLOC_PTR_GFP(info, echo_thread_kmem, GFP_NOFS);
635         if (info == NULL)
636                 info = ERR_PTR(-ENOMEM);
637         return info;
638 }
639
640 static void echo_thread_key_fini(const struct lu_context *ctx,
641                          struct lu_context_key *key, void *data)
642 {
643         struct echo_thread_info *info = data;
644         OBD_SLAB_FREE_PTR(info, echo_thread_kmem);
645 }
646
647 static void echo_thread_key_exit(const struct lu_context *ctx,
648                          struct lu_context_key *key, void *data)
649 {
650 }
651
652 static struct lu_context_key echo_thread_key = {
653         .lct_tags = LCT_CL_THREAD,
654         .lct_init = echo_thread_key_init,
655         .lct_fini = echo_thread_key_fini,
656         .lct_exit = echo_thread_key_exit
657 };
658
659 static void *echo_session_key_init(const struct lu_context *ctx,
660                                   struct lu_context_key *key)
661 {
662         struct echo_session_info *session;
663
664         OBD_SLAB_ALLOC_PTR_GFP(session, echo_session_kmem, GFP_NOFS);
665         if (session == NULL)
666                 session = ERR_PTR(-ENOMEM);
667         return session;
668 }
669
670 static void echo_session_key_fini(const struct lu_context *ctx,
671                                  struct lu_context_key *key, void *data)
672 {
673         struct echo_session_info *session = data;
674         OBD_SLAB_FREE_PTR(session, echo_session_kmem);
675 }
676
677 static void echo_session_key_exit(const struct lu_context *ctx,
678                                  struct lu_context_key *key, void *data)
679 {
680 }
681
682 static struct lu_context_key echo_session_key = {
683         .lct_tags = LCT_SESSION,
684         .lct_init = echo_session_key_init,
685         .lct_fini = echo_session_key_fini,
686         .lct_exit = echo_session_key_exit
687 };
688
689 LU_TYPE_INIT_FINI(echo, &echo_thread_key, &echo_session_key);
690
691 #ifdef HAVE_SERVER_SUPPORT
692 # define ECHO_SEQ_WIDTH 0xffffffff
693 static int echo_fid_init(struct echo_device *ed, char *obd_name,
694                          struct seq_server_site *ss)
695 {
696         char *prefix;
697         int rc;
698         ENTRY;
699
700         OBD_ALLOC_PTR(ed->ed_cl_seq);
701         if (ed->ed_cl_seq == NULL)
702                 RETURN(-ENOMEM);
703
704         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
705         if (prefix == NULL)
706                 GOTO(out_free_seq, rc = -ENOMEM);
707
708         snprintf(prefix, MAX_OBD_NAME + 5, "srv-%s", obd_name);
709
710         /* Init client side sequence-manager */
711         rc = seq_client_init(ed->ed_cl_seq, NULL,
712                              LUSTRE_SEQ_METADATA,
713                              prefix, ss->ss_server_seq);
714         ed->ed_cl_seq->lcs_width = ECHO_SEQ_WIDTH;
715         OBD_FREE(prefix, MAX_OBD_NAME + 5);
716         if (rc)
717                 GOTO(out_free_seq, rc);
718
719         RETURN(0);
720
721 out_free_seq:
722         OBD_FREE_PTR(ed->ed_cl_seq);
723         ed->ed_cl_seq = NULL;
724         RETURN(rc);
725 }
726
727 static int echo_fid_fini(struct obd_device *obddev)
728 {
729         struct echo_device *ed = obd2echo_dev(obddev);
730         ENTRY;
731
732         if (ed->ed_cl_seq != NULL) {
733                 seq_client_fini(ed->ed_cl_seq);
734                 OBD_FREE_PTR(ed->ed_cl_seq);
735                 ed->ed_cl_seq = NULL;
736         }
737
738         RETURN(0);
739 }
740 #endif /* HAVE_SERVER_SUPPORT */
741
742 static struct lu_device *echo_device_alloc(const struct lu_env *env,
743                                            struct lu_device_type *t,
744                                            struct lustre_cfg *cfg)
745 {
746         struct lu_device   *next;
747         struct echo_device *ed;
748         struct cl_device   *cd;
749         struct obd_device  *obd = NULL; /* to keep compiler happy */
750         struct obd_device  *tgt;
751         const char *tgt_type_name;
752         int rc;
753         int cleanup = 0;
754         ENTRY;
755
756         OBD_ALLOC_PTR(ed);
757         if (ed == NULL)
758                 GOTO(out, rc = -ENOMEM);
759
760         cleanup = 1;
761         cd = &ed->ed_cl;
762         rc = cl_device_init(cd, t);
763         if (rc)
764                 GOTO(out, rc);
765
766         cd->cd_lu_dev.ld_ops = &echo_device_lu_ops;
767         cd->cd_ops = &echo_device_cl_ops;
768
769         cleanup = 2;
770         obd = class_name2obd(lustre_cfg_string(cfg, 0));
771         LASSERT(obd != NULL);
772         LASSERT(env != NULL);
773
774         tgt = class_name2obd(lustre_cfg_string(cfg, 1));
775         if (tgt == NULL) {
776                 CERROR("Can not find tgt device %s\n",
777                         lustre_cfg_string(cfg, 1));
778                 GOTO(out, rc = -ENODEV);
779         }
780
781         next = tgt->obd_lu_dev;
782         if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
783                 ed->ed_next_ismd = 1;
784         } else {
785                 ed->ed_next_ismd = 0;
786                 rc = echo_site_init(env, ed);
787                 if (rc)
788                         GOTO(out, rc);
789         }
790         cleanup = 3;
791
792         rc = echo_client_setup(env, obd, cfg);
793         if (rc)
794                 GOTO(out, rc);
795
796         ed->ed_ec = &obd->u.echo_client;
797         cleanup = 4;
798
799         if (ed->ed_next_ismd) {
800 #ifdef HAVE_SERVER_SUPPORT
801                 /* Suppose to connect to some Metadata layer */
802                 struct lu_site *ls;
803                 struct lu_device *ld;
804                 int    found = 0;
805
806                 if (next == NULL) {
807                         CERROR("%s is not lu device type!\n",
808                                lustre_cfg_string(cfg, 1));
809                         GOTO(out, rc = -EINVAL);
810                 }
811
812                 tgt_type_name = lustre_cfg_string(cfg, 2);
813                 if (!tgt_type_name) {
814                         CERROR("%s no type name for echo %s setup\n",
815                                 lustre_cfg_string(cfg, 1),
816                                 tgt->obd_type->typ_name);
817                         GOTO(out, rc = -EINVAL);
818                 }
819
820                 ls = next->ld_site;
821
822                 spin_lock(&ls->ls_ld_lock);
823                 list_for_each_entry(ld, &ls->ls_ld_linkage, ld_linkage) {
824                         if (strcmp(ld->ld_type->ldt_name, tgt_type_name) == 0) {
825                                 found = 1;
826                                 break;
827                         }
828                 }
829                 spin_unlock(&ls->ls_ld_lock);
830
831                 if (found == 0) {
832                         CERROR("%s is not lu device type!\n",
833                                lustre_cfg_string(cfg, 1));
834                         GOTO(out, rc = -EINVAL);
835                 }
836
837                 next = ld;
838                 /* For MD echo client, it will use the site in MDS stack */
839                 ed->ed_site_myself.cs_lu = *ls;
840                 ed->ed_site = &ed->ed_site_myself;
841                 ed->ed_cl.cd_lu_dev.ld_site = &ed->ed_site_myself.cs_lu;
842                 rc = echo_fid_init(ed, obd->obd_name, lu_site2seq(ls));
843                 if (rc) {
844                         CERROR("echo fid init error %d\n", rc);
845                         GOTO(out, rc);
846                 }
847 #else /* !HAVE_SERVER_SUPPORT */
848                 CERROR("Local operations are NOT supported on client side. "
849                        "Only remote operations are supported. Metadata client "
850                        "must be run on server side.\n");
851                 GOTO(out, rc = -EOPNOTSUPP);
852 #endif
853         } else {
854                  /* if echo client is to be stacked upon ost device, the next is
855                   * NULL since ost is not a clio device so far */
856                 if (next != NULL && !lu_device_is_cl(next))
857                         next = NULL;
858
859                 tgt_type_name = tgt->obd_type->typ_name;
860                 if (next != NULL) {
861                         LASSERT(next != NULL);
862                         if (next->ld_site != NULL)
863                                 GOTO(out, rc = -EBUSY);
864
865                         next->ld_site = &ed->ed_site->cs_lu;
866                         rc = next->ld_type->ldt_ops->ldto_device_init(env, next,
867                                                      next->ld_type->ldt_name,
868                                                      NULL);
869                         if (rc)
870                                 GOTO(out, rc);
871
872                         /* Tricky case, I have to determine the obd type since
873                          * CLIO uses the different parameters to initialize
874                          * objects for lov & osc. */
875                         if (strcmp(tgt_type_name, LUSTRE_LOV_NAME) == 0)
876                                 ed->ed_next_islov = 1;
877                         else
878                                 LASSERT(strcmp(tgt_type_name,
879                                                LUSTRE_OSC_NAME) == 0);
880                 } else
881                         LASSERT(strcmp(tgt_type_name, LUSTRE_OST_NAME) == 0);
882         }
883
884         ed->ed_next = next;
885         RETURN(&cd->cd_lu_dev);
886 out:
887         switch(cleanup) {
888         case 4: {
889                 int rc2;
890                 rc2 = echo_client_cleanup(obd);
891                 if (rc2)
892                         CERROR("Cleanup obd device %s error(%d)\n",
893                                obd->obd_name, rc2);
894         }
895
896         case 3:
897                 echo_site_fini(env, ed);
898         case 2:
899                 cl_device_fini(&ed->ed_cl);
900         case 1:
901                 OBD_FREE_PTR(ed);
902         case 0:
903         default:
904                 break;
905         }
906         return(ERR_PTR(rc));
907 }
908
909 static int echo_device_init(const struct lu_env *env, struct lu_device *d,
910                           const char *name, struct lu_device *next)
911 {
912         LBUG();
913         return 0;
914 }
915
916 static struct lu_device *echo_device_fini(const struct lu_env *env,
917                                           struct lu_device *d)
918 {
919         struct echo_device *ed = cl2echo_dev(lu2cl_dev(d));
920         struct lu_device *next = ed->ed_next;
921
922         while (next && !ed->ed_next_ismd)
923                 next = next->ld_type->ldt_ops->ldto_device_fini(env, next);
924         return NULL;
925 }
926
927 static void echo_lock_release(const struct lu_env *env,
928                               struct echo_lock *ecl,
929                               int still_used)
930 {
931         struct cl_lock *clk = echo_lock2cl(ecl);
932
933         cl_lock_release(env, clk);
934 }
935
936 static struct lu_device *echo_device_free(const struct lu_env *env,
937                                           struct lu_device *d)
938 {
939         struct echo_device     *ed   = cl2echo_dev(lu2cl_dev(d));
940         struct echo_client_obd *ec   = ed->ed_ec;
941         struct echo_object     *eco;
942         struct lu_device       *next = ed->ed_next;
943
944         CDEBUG(D_INFO, "echo device:%p is going to be freed, next = %p\n",
945                ed, next);
946
947         lu_site_purge(env, &ed->ed_site->cs_lu, -1);
948
949         /* check if there are objects still alive.
950          * It shouldn't have any object because lu_site_purge would cleanup
951          * all of cached objects. Anyway, probably the echo device is being
952          * parallelly accessed.
953          */
954         spin_lock(&ec->ec_lock);
955         list_for_each_entry(eco, &ec->ec_objects, eo_obj_chain)
956                 eco->eo_deleted = 1;
957         spin_unlock(&ec->ec_lock);
958
959         /* purge again */
960         lu_site_purge(env, &ed->ed_site->cs_lu, -1);
961
962         CDEBUG(D_INFO,
963                "Waiting for the reference of echo object to be dropped\n");
964
965         /* Wait for the last reference to be dropped. */
966         spin_lock(&ec->ec_lock);
967         while (!list_empty(&ec->ec_objects)) {
968                 spin_unlock(&ec->ec_lock);
969                 CERROR("echo_client still has objects at cleanup time, "
970                        "wait for 1 second\n");
971                 schedule_timeout_and_set_state(TASK_UNINTERRUPTIBLE,
972                                                    cfs_time_seconds(1));
973                 lu_site_purge(env, &ed->ed_site->cs_lu, -1);
974                 spin_lock(&ec->ec_lock);
975         }
976         spin_unlock(&ec->ec_lock);
977
978         LASSERT(list_empty(&ec->ec_locks));
979
980         CDEBUG(D_INFO, "No object exists, exiting...\n");
981
982         echo_client_cleanup(d->ld_obd);
983 #ifdef HAVE_SERVER_SUPPORT
984         echo_fid_fini(d->ld_obd);
985 #endif
986         while (next && !ed->ed_next_ismd)
987                 next = next->ld_type->ldt_ops->ldto_device_free(env, next);
988
989         LASSERT(ed->ed_site == lu2cl_site(d->ld_site));
990         echo_site_fini(env, ed);
991         cl_device_fini(&ed->ed_cl);
992         OBD_FREE_PTR(ed);
993
994         return NULL;
995 }
996
997 static const struct lu_device_type_operations echo_device_type_ops = {
998         .ldto_init = echo_type_init,
999         .ldto_fini = echo_type_fini,
1000
1001         .ldto_start = echo_type_start,
1002         .ldto_stop  = echo_type_stop,
1003
1004         .ldto_device_alloc = echo_device_alloc,
1005         .ldto_device_free  = echo_device_free,
1006         .ldto_device_init  = echo_device_init,
1007         .ldto_device_fini  = echo_device_fini
1008 };
1009
1010 static struct lu_device_type echo_device_type = {
1011         .ldt_tags     = LU_DEVICE_CL,
1012         .ldt_name     = LUSTRE_ECHO_CLIENT_NAME,
1013         .ldt_ops      = &echo_device_type_ops,
1014         .ldt_ctx_tags = LCT_CL_THREAD | LCT_MD_THREAD | LCT_DT_THREAD,
1015 };
1016 /** @} echo_init */
1017
1018 /** \defgroup echo_exports Exported operations
1019  *
1020  * exporting functions to echo client
1021  *
1022  * @{
1023  */
1024
1025 /* Interfaces to echo client obd device */
1026 static struct echo_object *cl_echo_object_find(struct echo_device *d,
1027                                                struct lov_stripe_md **lsmp)
1028 {
1029         struct lu_env *env;
1030         struct echo_thread_info *info;
1031         struct echo_object_conf *conf;
1032         struct lov_stripe_md    *lsm;
1033         struct echo_object *eco;
1034         struct cl_object   *obj;
1035         struct lu_fid *fid;
1036         int refcheck;
1037         int rc;
1038         ENTRY;
1039
1040         LASSERT(lsmp);
1041         lsm = *lsmp;
1042         LASSERT(lsm);
1043         LASSERTF(ostid_id(&lsm->lsm_oi) != 0, DOSTID"\n", POSTID(&lsm->lsm_oi));
1044         LASSERTF(ostid_seq(&lsm->lsm_oi) == FID_SEQ_ECHO, DOSTID"\n",
1045                  POSTID(&lsm->lsm_oi));
1046
1047         /* Never return an object if the obd is to be freed. */
1048         if (echo_dev2cl(d)->cd_lu_dev.ld_obd->obd_stopping)
1049                 RETURN(ERR_PTR(-ENODEV));
1050
1051         env = cl_env_get(&refcheck);
1052         if (IS_ERR(env))
1053                 RETURN((void *)env);
1054
1055         info = echo_env_info(env);
1056         conf = &info->eti_conf;
1057         if (d->ed_next) {
1058                 if (!d->ed_next_islov) {
1059                         struct lov_oinfo *oinfo = lsm->lsm_oinfo[0];
1060                         LASSERT(oinfo != NULL);
1061                         oinfo->loi_oi = lsm->lsm_oi;
1062                         conf->eoc_cl.u.coc_oinfo = oinfo;
1063                 } else {
1064                         struct lustre_md *md;
1065                         md = &info->eti_md;
1066                         memset(md, 0, sizeof *md);
1067                         md->lsm = lsm;
1068                         conf->eoc_cl.u.coc_md = md;
1069                 }
1070         }
1071         conf->eoc_md = lsmp;
1072
1073         fid  = &info->eti_fid;
1074         rc = ostid_to_fid(fid, &lsm->lsm_oi, 0);
1075         if (rc != 0)
1076                 GOTO(out, eco = ERR_PTR(rc));
1077
1078         /* In the function below, .hs_keycmp resolves to
1079          * lu_obj_hop_keycmp() */
1080         /* coverity[overrun-buffer-val] */
1081         obj = cl_object_find(env, echo_dev2cl(d), fid, &conf->eoc_cl);
1082         if (IS_ERR(obj))
1083                 GOTO(out, eco = (void*)obj);
1084
1085         eco = cl2echo_obj(obj);
1086         if (eco->eo_deleted) {
1087                 cl_object_put(env, obj);
1088                 eco = ERR_PTR(-EAGAIN);
1089         }
1090
1091 out:
1092         cl_env_put(env, &refcheck);
1093         RETURN(eco);
1094 }
1095
1096 static int cl_echo_object_put(struct echo_object *eco)
1097 {
1098         struct lu_env *env;
1099         struct cl_object *obj = echo_obj2cl(eco);
1100         int refcheck;
1101         ENTRY;
1102
1103         env = cl_env_get(&refcheck);
1104         if (IS_ERR(env))
1105                 RETURN(PTR_ERR(env));
1106
1107         /* an external function to kill an object? */
1108         if (eco->eo_deleted) {
1109                 struct lu_object_header *loh = obj->co_lu.lo_header;
1110                 LASSERT(&eco->eo_hdr == luh2coh(loh));
1111                 set_bit(LU_OBJECT_HEARD_BANSHEE, &loh->loh_flags);
1112         }
1113
1114         cl_object_put(env, obj);
1115         cl_env_put(env, &refcheck);
1116         RETURN(0);
1117 }
1118
1119 static int cl_echo_enqueue0(struct lu_env *env, struct echo_object *eco,
1120                             obd_off start, obd_off end, int mode,
1121                             __u64 *cookie , __u32 enqflags)
1122 {
1123         struct cl_io *io;
1124         struct cl_lock *lck;
1125         struct cl_object *obj;
1126         struct cl_lock_descr *descr;
1127         struct echo_thread_info *info;
1128         int rc = -ENOMEM;
1129         ENTRY;
1130
1131         info = echo_env_info(env);
1132         io = &info->eti_io;
1133         lck = &info->eti_lock;
1134         obj = echo_obj2cl(eco);
1135
1136         memset(lck, 0, sizeof(*lck));
1137         descr = &lck->cll_descr;
1138         descr->cld_obj   = obj;
1139         descr->cld_start = cl_index(obj, start);
1140         descr->cld_end   = cl_index(obj, end);
1141         descr->cld_mode  = mode == LCK_PW ? CLM_WRITE : CLM_READ;
1142         descr->cld_enq_flags = enqflags;
1143         io->ci_obj = obj;
1144
1145         rc = cl_lock_request(env, io, lck);
1146         if (rc == 0) {
1147                 struct echo_client_obd *ec = eco->eo_dev->ed_ec;
1148                 struct echo_lock *el;
1149
1150                 el = cl2echo_lock(cl_lock_at(lck, &echo_device_type));
1151                 spin_lock(&ec->ec_lock);
1152                 if (list_empty(&el->el_chain)) {
1153                         list_add(&el->el_chain, &ec->ec_locks);
1154                         el->el_cookie = ++ec->ec_unique;
1155                 }
1156                 atomic_inc(&el->el_refcount);
1157                 *cookie = el->el_cookie;
1158                 spin_unlock(&ec->ec_lock);
1159         }
1160         RETURN(rc);
1161 }
1162
1163 static int cl_echo_cancel0(struct lu_env *env, struct echo_device *ed,
1164                            __u64 cookie)
1165 {
1166         struct echo_client_obd *ec = ed->ed_ec;
1167         struct echo_lock       *ecl = NULL;
1168         struct list_head        *el;
1169         int found = 0, still_used = 0;
1170         ENTRY;
1171
1172         LASSERT(ec != NULL);
1173         spin_lock(&ec->ec_lock);
1174         list_for_each(el, &ec->ec_locks) {
1175                 ecl = list_entry(el, struct echo_lock, el_chain);
1176                 CDEBUG(D_INFO, "ecl: %p, cookie: "LPX64"\n", ecl, ecl->el_cookie);
1177                 found = (ecl->el_cookie == cookie);
1178                 if (found) {
1179                         if (atomic_dec_and_test(&ecl->el_refcount))
1180                                 list_del_init(&ecl->el_chain);
1181                         else
1182                                 still_used = 1;
1183                         break;
1184                 }
1185         }
1186         spin_unlock(&ec->ec_lock);
1187
1188         if (!found)
1189                 RETURN(-ENOENT);
1190
1191         echo_lock_release(env, ecl, still_used);
1192         RETURN(0);
1193 }
1194
1195 static void echo_commit_callback(const struct lu_env *env, struct cl_io *io,
1196                                 struct cl_page *page)
1197 {
1198         struct echo_thread_info *info;
1199         struct cl_2queue        *queue;
1200
1201         info = echo_env_info(env);
1202         LASSERT(io == &info->eti_io);
1203
1204         queue = &info->eti_queue;
1205         cl_page_list_add(&queue->c2_qout, page);
1206 }
1207
1208 static int cl_echo_object_brw(struct echo_object *eco, int rw, obd_off offset,
1209                               struct page **pages, int npages, int async)
1210 {
1211         struct lu_env           *env;
1212         struct echo_thread_info *info;
1213         struct cl_object        *obj = echo_obj2cl(eco);
1214         struct echo_device      *ed  = eco->eo_dev;
1215         struct cl_2queue        *queue;
1216         struct cl_io            *io;
1217         struct cl_page          *clp;
1218         struct lustre_handle    lh = { 0 };
1219         int page_size = cl_page_size(obj);
1220         int refcheck;
1221         int rc;
1222         int i;
1223         ENTRY;
1224
1225         LASSERT((offset & ~CFS_PAGE_MASK) == 0);
1226         LASSERT(ed->ed_next != NULL);
1227         env = cl_env_get(&refcheck);
1228         if (IS_ERR(env))
1229                 RETURN(PTR_ERR(env));
1230
1231         info    = echo_env_info(env);
1232         io      = &info->eti_io;
1233         queue   = &info->eti_queue;
1234
1235         cl_2queue_init(queue);
1236
1237         io->ci_ignore_layout = 1;
1238         rc = cl_io_init(env, io, CIT_MISC, obj);
1239         if (rc < 0)
1240                 GOTO(out, rc);
1241         LASSERT(rc == 0);
1242
1243
1244         rc = cl_echo_enqueue0(env, eco, offset,
1245                               offset + npages * PAGE_CACHE_SIZE - 1,
1246                               rw == READ ? LCK_PR : LCK_PW, &lh.cookie,
1247                               CEF_NEVER);
1248         if (rc < 0)
1249                 GOTO(error_lock, rc);
1250
1251         for (i = 0; i < npages; i++) {
1252                 LASSERT(pages[i]);
1253                 clp = cl_page_find(env, obj, cl_index(obj, offset),
1254                                    pages[i], CPT_TRANSIENT);
1255                 if (IS_ERR(clp)) {
1256                         rc = PTR_ERR(clp);
1257                         break;
1258                 }
1259                 LASSERT(clp->cp_type == CPT_TRANSIENT);
1260
1261                 rc = cl_page_own(env, io, clp);
1262                 if (rc) {
1263                         LASSERT(clp->cp_state == CPS_FREEING);
1264                         cl_page_put(env, clp);
1265                         break;
1266                 }
1267
1268                 cl_2queue_add(queue, clp);
1269
1270                 /* drop the reference count for cl_page_find, so that the page
1271                  * will be freed in cl_2queue_fini. */
1272                 cl_page_put(env, clp);
1273                 cl_page_clip(env, clp, 0, page_size);
1274
1275                 offset += page_size;
1276         }
1277
1278         if (rc == 0) {
1279                 enum cl_req_type typ = rw == READ ? CRT_READ : CRT_WRITE;
1280
1281                 async = async && (typ == CRT_WRITE);
1282                 if (async)
1283                         rc = cl_io_commit_async(env, io, &queue->c2_qin,
1284                                                 0, PAGE_SIZE,
1285                                                 echo_commit_callback);
1286                 else
1287                         rc = cl_io_submit_sync(env, io, typ, queue, 0);
1288                 CDEBUG(D_INFO, "echo_client %s write returns %d\n",
1289                        async ? "async" : "sync", rc);
1290         }
1291
1292         cl_echo_cancel0(env, ed, lh.cookie);
1293         EXIT;
1294 error_lock:
1295         cl_2queue_discard(env, io, queue);
1296         cl_2queue_disown(env, io, queue);
1297         cl_2queue_fini(env, queue);
1298         cl_io_fini(env, io);
1299 out:
1300         cl_env_put(env, &refcheck);
1301         return rc;
1302 }
1303 /** @} echo_exports */
1304
1305
1306 static obd_id last_object_id;
1307
1308 static int
1309 echo_copyin_lsm (struct echo_device *ed, struct lov_stripe_md *lsm,
1310                  void __user *ulsm, int ulsm_nob)
1311 {
1312         struct echo_client_obd *ec = ed->ed_ec;
1313         int                     i;
1314
1315         if (ulsm_nob < sizeof (*lsm))
1316                 return (-EINVAL);
1317
1318         if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
1319                 return (-EFAULT);
1320
1321         if (lsm->lsm_stripe_count > ec->ec_nstripes ||
1322             lsm->lsm_magic != LOV_MAGIC ||
1323             (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
1324             ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
1325                 return (-EINVAL);
1326
1327
1328         for (i = 0; i < lsm->lsm_stripe_count; i++) {
1329                 if (copy_from_user(lsm->lsm_oinfo[i],
1330                                        ((struct lov_stripe_md *)ulsm)-> \
1331                                        lsm_oinfo[i],
1332                                        sizeof(lsm->lsm_oinfo[0])))
1333                         return (-EFAULT);
1334         }
1335         return (0);
1336 }
1337
1338 #ifdef HAVE_SERVER_SUPPORT
1339 static inline void echo_md_build_name(struct lu_name *lname, char *name,
1340                                       __u64 id)
1341 {
1342         sprintf(name, LPU64, id);
1343         lname->ln_name = name;
1344         lname->ln_namelen = strlen(name);
1345 }
1346
1347 /* similar to mdt_attr_get_complex */
1348 static int echo_big_lmm_get(const struct lu_env *env, struct md_object *o,
1349                             struct md_attr *ma)
1350 {
1351         struct echo_thread_info *info = echo_env_info(env);
1352         int                      rc;
1353
1354         ENTRY;
1355
1356         LASSERT(ma->ma_lmm_size > 0);
1357
1358         rc = mo_xattr_get(env, o, &LU_BUF_NULL, XATTR_NAME_LOV);
1359         if (rc < 0)
1360                 RETURN(rc);
1361
1362         /* big_lmm may need to be grown */
1363         if (info->eti_big_lmmsize < rc) {
1364                 int size = size_roundup_power2(rc);
1365
1366                 if (info->eti_big_lmmsize > 0) {
1367                         /* free old buffer */
1368                         LASSERT(info->eti_big_lmm);
1369                         OBD_FREE_LARGE(info->eti_big_lmm,
1370                                        info->eti_big_lmmsize);
1371                         info->eti_big_lmm = NULL;
1372                         info->eti_big_lmmsize = 0;
1373                 }
1374
1375                 OBD_ALLOC_LARGE(info->eti_big_lmm, size);
1376                 if (info->eti_big_lmm == NULL)
1377                         RETURN(-ENOMEM);
1378                 info->eti_big_lmmsize = size;
1379         }
1380         LASSERT(info->eti_big_lmmsize >= rc);
1381
1382         info->eti_buf.lb_buf = info->eti_big_lmm;
1383         info->eti_buf.lb_len = info->eti_big_lmmsize;
1384         rc = mo_xattr_get(env, o, &info->eti_buf, XATTR_NAME_LOV);
1385         if (rc < 0)
1386                 RETURN(rc);
1387
1388         ma->ma_valid |= MA_LOV;
1389         ma->ma_lmm = info->eti_big_lmm;
1390         ma->ma_lmm_size = rc;
1391
1392         RETURN(0);
1393 }
1394
1395 static int echo_attr_get_complex(const struct lu_env *env,
1396                                  struct md_object *next,
1397                                  struct md_attr *ma)
1398 {
1399         struct echo_thread_info *info = echo_env_info(env);
1400         struct lu_buf           *buf = &info->eti_buf;
1401         umode_t          mode = lu_object_attr(&next->mo_lu);
1402         int                      need = ma->ma_need;
1403         int                      rc = 0, rc2;
1404
1405         ENTRY;
1406
1407         ma->ma_valid = 0;
1408
1409         if (need & MA_INODE) {
1410                 ma->ma_need = MA_INODE;
1411                 rc = mo_attr_get(env, next, ma);
1412                 if (rc)
1413                         GOTO(out, rc);
1414                 ma->ma_valid |= MA_INODE;
1415         }
1416
1417         if (need & MA_LOV) {
1418                 if (S_ISREG(mode) || S_ISDIR(mode)) {
1419                         LASSERT(ma->ma_lmm_size > 0);
1420                         buf->lb_buf = ma->ma_lmm;
1421                         buf->lb_len = ma->ma_lmm_size;
1422                         rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_LOV);
1423                         if (rc2 > 0) {
1424                                 ma->ma_lmm_size = rc2;
1425                                 ma->ma_valid |= MA_LOV;
1426                         } else if (rc2 == -ENODATA) {
1427                                 /* no LOV EA */
1428                                 ma->ma_lmm_size = 0;
1429                         } else if (rc2 == -ERANGE) {
1430                                 rc2 = echo_big_lmm_get(env, next, ma);
1431                                 if (rc2 < 0)
1432                                         GOTO(out, rc = rc2);
1433                         } else {
1434                                 GOTO(out, rc = rc2);
1435                         }
1436                 }
1437         }
1438
1439 #ifdef CONFIG_FS_POSIX_ACL
1440         if (need & MA_ACL_DEF && S_ISDIR(mode)) {
1441                 buf->lb_buf = ma->ma_acl;
1442                 buf->lb_len = ma->ma_acl_size;
1443                 rc2 = mo_xattr_get(env, next, buf, XATTR_NAME_ACL_DEFAULT);
1444                 if (rc2 > 0) {
1445                         ma->ma_acl_size = rc2;
1446                         ma->ma_valid |= MA_ACL_DEF;
1447                 } else if (rc2 == -ENODATA) {
1448                         /* no ACLs */
1449                         ma->ma_acl_size = 0;
1450                 } else {
1451                         GOTO(out, rc = rc2);
1452                 }
1453         }
1454 #endif
1455 out:
1456         ma->ma_need = need;
1457         CDEBUG(D_INODE, "after getattr rc = %d, ma_valid = "LPX64" ma_lmm=%p\n",
1458                rc, ma->ma_valid, ma->ma_lmm);
1459         RETURN(rc);
1460 }
1461
1462 static int
1463 echo_md_create_internal(const struct lu_env *env, struct echo_device *ed,
1464                         struct md_object *parent, struct lu_fid *fid,
1465                         struct lu_name *lname, struct md_op_spec *spec,
1466                         struct md_attr *ma)
1467 {
1468         struct lu_object        *ec_child, *child;
1469         struct lu_device        *ld = ed->ed_next;
1470         struct echo_thread_info *info = echo_env_info(env);
1471         struct lu_fid           *fid2 = &info->eti_fid2;
1472         struct lu_object_conf    conf = { .loc_flags = LOC_F_NEW };
1473         int                      rc;
1474
1475         ENTRY;
1476
1477         rc = mdo_lookup(env, parent, lname, fid2, spec);
1478         if (rc == 0)
1479                 return -EEXIST;
1480         else if (rc != -ENOENT)
1481                 return rc;
1482
1483         ec_child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev,
1484                                      fid, &conf);
1485         if (IS_ERR(ec_child)) {
1486                 CERROR("Can not find the child "DFID": rc = %ld\n", PFID(fid),
1487                         PTR_ERR(ec_child));
1488                 RETURN(PTR_ERR(ec_child));
1489         }
1490
1491         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1492         if (child == NULL) {
1493                 CERROR("Can not locate the child "DFID"\n", PFID(fid));
1494                 GOTO(out_put, rc = -EINVAL);
1495         }
1496
1497         CDEBUG(D_RPCTRACE, "Start creating object "DFID" %s %p\n",
1498                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1499
1500         /*
1501          * Do not perform lookup sanity check. We know that name does not exist.
1502          */
1503         spec->sp_cr_lookup = 0;
1504         rc = mdo_create(env, parent, lname, lu2md(child), spec, ma);
1505         if (rc) {
1506                 CERROR("Can not create child "DFID": rc = %d\n", PFID(fid), rc);
1507                 GOTO(out_put, rc);
1508         }
1509         CDEBUG(D_RPCTRACE, "End creating object "DFID" %s %p rc  = %d\n",
1510                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent, rc);
1511         EXIT;
1512 out_put:
1513         lu_object_put(env, ec_child);
1514         return rc;
1515 }
1516
1517 static int echo_set_lmm_size(const struct lu_env *env, struct lu_device *ld,
1518                              struct md_attr *ma)
1519 {
1520         struct echo_thread_info *info = echo_env_info(env);
1521
1522         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
1523                 ma->ma_lmm = (void *)&info->eti_lmm;
1524                 ma->ma_lmm_size = sizeof(info->eti_lmm);
1525         } else {
1526                 LASSERT(info->eti_big_lmmsize);
1527                 ma->ma_lmm = info->eti_big_lmm;
1528                 ma->ma_lmm_size = info->eti_big_lmmsize;
1529         }
1530
1531         return 0;
1532 }
1533
1534 static int echo_create_md_object(const struct lu_env *env,
1535                                  struct echo_device *ed,
1536                                  struct lu_object *ec_parent,
1537                                  struct lu_fid *fid,
1538                                  char *name, int namelen,
1539                                  __u64 id, __u32 mode, int count,
1540                                  int stripe_count, int stripe_offset)
1541 {
1542         struct lu_object        *parent;
1543         struct echo_thread_info *info = echo_env_info(env);
1544         struct lu_name          *lname = &info->eti_lname;
1545         struct md_op_spec       *spec = &info->eti_spec;
1546         struct md_attr          *ma = &info->eti_ma;
1547         struct lu_device        *ld = ed->ed_next;
1548         int                      rc = 0;
1549         int                      i;
1550
1551         ENTRY;
1552
1553         if (ec_parent == NULL)
1554                 return -1;
1555         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1556         if (parent == NULL)
1557                 RETURN(-ENXIO);
1558
1559         memset(ma, 0, sizeof(*ma));
1560         memset(spec, 0, sizeof(*spec));
1561         if (stripe_count != 0) {
1562                 spec->sp_cr_flags |= FMODE_WRITE;
1563                 echo_set_lmm_size(env, ld, ma);
1564                 if (stripe_count != -1) {
1565                         struct lov_user_md_v3 *lum = &info->eti_lum;
1566
1567                         lum->lmm_magic = LOV_USER_MAGIC_V3;
1568                         lum->lmm_stripe_count = stripe_count;
1569                         lum->lmm_stripe_offset = stripe_offset;
1570                         lum->lmm_pattern = 0;
1571                         spec->u.sp_ea.eadata = lum;
1572                         spec->u.sp_ea.eadatalen = sizeof(*lum);
1573                         spec->sp_cr_flags |= MDS_OPEN_HAS_EA;
1574                 }
1575         }
1576
1577         ma->ma_attr.la_mode = mode;
1578         ma->ma_attr.la_valid = LA_CTIME | LA_MODE;
1579         ma->ma_attr.la_ctime = cfs_time_current_64();
1580
1581         if (name != NULL) {
1582                 lname->ln_name = name;
1583                 lname->ln_namelen = namelen;
1584                 /* If name is specified, only create one object by name */
1585                 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1586                                              spec, ma);
1587                 RETURN(rc);
1588         }
1589
1590         /* Create multiple object sequenced by id */
1591         for (i = 0; i < count; i++) {
1592                 char *tmp_name = info->eti_name;
1593
1594                 echo_md_build_name(lname, tmp_name, id);
1595
1596                 rc = echo_md_create_internal(env, ed, lu2md(parent), fid, lname,
1597                                              spec, ma);
1598                 if (rc) {
1599                         CERROR("Can not create child %s: rc = %d\n", tmp_name,
1600                                 rc);
1601                         break;
1602                 }
1603                 id++;
1604                 fid->f_oid++;
1605         }
1606
1607         RETURN(rc);
1608 }
1609
1610 static struct lu_object *echo_md_lookup(const struct lu_env *env,
1611                                         struct echo_device *ed,
1612                                         struct md_object *parent,
1613                                         struct lu_name *lname)
1614 {
1615         struct echo_thread_info *info = echo_env_info(env);
1616         struct lu_fid           *fid = &info->eti_fid;
1617         struct lu_object        *child;
1618         int    rc;
1619         ENTRY;
1620
1621         CDEBUG(D_INFO, "lookup %s in parent "DFID" %p\n", lname->ln_name,
1622                PFID(fid), parent);
1623         rc = mdo_lookup(env, parent, lname, fid, NULL);
1624         if (rc) {
1625                 CERROR("lookup %s: rc = %d\n", lname->ln_name, rc);
1626                 RETURN(ERR_PTR(rc));
1627         }
1628
1629         /* In the function below, .hs_keycmp resolves to
1630          * lu_obj_hop_keycmp() */
1631         /* coverity[overrun-buffer-val] */
1632         child = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1633
1634         RETURN(child);
1635 }
1636
1637 static int echo_setattr_object(const struct lu_env *env,
1638                                struct echo_device *ed,
1639                                struct lu_object *ec_parent,
1640                                __u64 id, int count)
1641 {
1642         struct lu_object        *parent;
1643         struct echo_thread_info *info = echo_env_info(env);
1644         struct lu_name          *lname = &info->eti_lname;
1645         char                    *name = info->eti_name;
1646         struct lu_device        *ld = ed->ed_next;
1647         struct lu_buf           *buf = &info->eti_buf;
1648         int                      rc = 0;
1649         int                      i;
1650
1651         ENTRY;
1652
1653         if (ec_parent == NULL)
1654                 return -1;
1655         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1656         if (parent == NULL)
1657                 RETURN(-ENXIO);
1658
1659         for (i = 0; i < count; i++) {
1660                 struct lu_object *ec_child, *child;
1661
1662                 echo_md_build_name(lname, name, id);
1663
1664                 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1665                 if (IS_ERR(ec_child)) {
1666                         CERROR("Can't find child %s: rc = %ld\n",
1667                                 lname->ln_name, PTR_ERR(ec_child));
1668                         RETURN(PTR_ERR(ec_child));
1669                 }
1670
1671                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1672                 if (child == NULL) {
1673                         CERROR("Can not locate the child %s\n", lname->ln_name);
1674                         lu_object_put(env, ec_child);
1675                         rc = -EINVAL;
1676                         break;
1677                 }
1678
1679                 CDEBUG(D_RPCTRACE, "Start setattr object "DFID"\n",
1680                        PFID(lu_object_fid(child)));
1681
1682                 buf->lb_buf = info->eti_xattr_buf;
1683                 buf->lb_len = sizeof(info->eti_xattr_buf);
1684
1685                 sprintf(name, "%s.test1", XATTR_USER_PREFIX);
1686                 rc = mo_xattr_set(env, lu2md(child), buf, name,
1687                                   LU_XATTR_CREATE);
1688                 if (rc < 0) {
1689                         CERROR("Can not setattr child "DFID": rc = %d\n",
1690                                 PFID(lu_object_fid(child)), rc);
1691                         lu_object_put(env, ec_child);
1692                         break;
1693                 }
1694                 CDEBUG(D_RPCTRACE, "End setattr object "DFID"\n",
1695                        PFID(lu_object_fid(child)));
1696                 id++;
1697                 lu_object_put(env, ec_child);
1698         }
1699         RETURN(rc);
1700 }
1701
1702 static int echo_getattr_object(const struct lu_env *env,
1703                                struct echo_device *ed,
1704                                struct lu_object *ec_parent,
1705                                __u64 id, int count)
1706 {
1707         struct lu_object        *parent;
1708         struct echo_thread_info *info = echo_env_info(env);
1709         struct lu_name          *lname = &info->eti_lname;
1710         char                    *name = info->eti_name;
1711         struct md_attr          *ma = &info->eti_ma;
1712         struct lu_device        *ld = ed->ed_next;
1713         int                      rc = 0;
1714         int                      i;
1715
1716         ENTRY;
1717
1718         if (ec_parent == NULL)
1719                 return -1;
1720         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1721         if (parent == NULL)
1722                 RETURN(-ENXIO);
1723
1724         memset(ma, 0, sizeof(*ma));
1725         ma->ma_need |= MA_INODE | MA_LOV | MA_PFID | MA_HSM | MA_ACL_DEF;
1726         ma->ma_acl = info->eti_xattr_buf;
1727         ma->ma_acl_size = sizeof(info->eti_xattr_buf);
1728
1729         for (i = 0; i < count; i++) {
1730                 struct lu_object *ec_child, *child;
1731
1732                 ma->ma_valid = 0;
1733                 echo_md_build_name(lname, name, id);
1734                 echo_set_lmm_size(env, ld, ma);
1735
1736                 ec_child = echo_md_lookup(env, ed, lu2md(parent), lname);
1737                 if (IS_ERR(ec_child)) {
1738                         CERROR("Can't find child %s: rc = %ld\n",
1739                                lname->ln_name, PTR_ERR(ec_child));
1740                         RETURN(PTR_ERR(ec_child));
1741                 }
1742
1743                 child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1744                 if (child == NULL) {
1745                         CERROR("Can not locate the child %s\n", lname->ln_name);
1746                         lu_object_put(env, ec_child);
1747                         RETURN(-EINVAL);
1748                 }
1749
1750                 CDEBUG(D_RPCTRACE, "Start getattr object "DFID"\n",
1751                        PFID(lu_object_fid(child)));
1752                 rc = echo_attr_get_complex(env, lu2md(child), ma);
1753                 if (rc) {
1754                         CERROR("Can not getattr child "DFID": rc = %d\n",
1755                                 PFID(lu_object_fid(child)), rc);
1756                         lu_object_put(env, ec_child);
1757                         break;
1758                 }
1759                 CDEBUG(D_RPCTRACE, "End getattr object "DFID"\n",
1760                        PFID(lu_object_fid(child)));
1761                 id++;
1762                 lu_object_put(env, ec_child);
1763         }
1764
1765         RETURN(rc);
1766 }
1767
1768 static int echo_lookup_object(const struct lu_env *env,
1769                               struct echo_device *ed,
1770                               struct lu_object *ec_parent,
1771                               __u64 id, int count)
1772 {
1773         struct lu_object        *parent;
1774         struct echo_thread_info *info = echo_env_info(env);
1775         struct lu_name          *lname = &info->eti_lname;
1776         char                    *name = info->eti_name;
1777         struct lu_fid           *fid = &info->eti_fid;
1778         struct lu_device        *ld = ed->ed_next;
1779         int                      rc = 0;
1780         int                      i;
1781
1782         if (ec_parent == NULL)
1783                 return -1;
1784         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1785         if (parent == NULL)
1786                 return -ENXIO;
1787
1788         /*prepare the requests*/
1789         for (i = 0; i < count; i++) {
1790                 echo_md_build_name(lname, name, id);
1791
1792                 CDEBUG(D_RPCTRACE, "Start lookup object "DFID" %s %p\n",
1793                        PFID(lu_object_fid(parent)), lname->ln_name, parent);
1794
1795                 rc = mdo_lookup(env, lu2md(parent), lname, fid, NULL);
1796                 if (rc) {
1797                         CERROR("Can not lookup child %s: rc = %d\n", name, rc);
1798                         break;
1799                 }
1800                 CDEBUG(D_RPCTRACE, "End lookup object "DFID" %s %p\n",
1801                        PFID(lu_object_fid(parent)), lname->ln_name, parent);
1802
1803                 id++;
1804         }
1805         return rc;
1806 }
1807
1808 static int echo_md_destroy_internal(const struct lu_env *env,
1809                                     struct echo_device *ed,
1810                                     struct md_object *parent,
1811                                     struct lu_name *lname,
1812                                     struct md_attr *ma)
1813 {
1814         struct lu_device   *ld = ed->ed_next;
1815         struct lu_object   *ec_child;
1816         struct lu_object   *child;
1817         int                 rc;
1818
1819         ENTRY;
1820
1821         ec_child = echo_md_lookup(env, ed, parent, lname);
1822         if (IS_ERR(ec_child)) {
1823                 CERROR("Can't find child %s: rc = %ld\n", lname->ln_name,
1824                         PTR_ERR(ec_child));
1825                 RETURN(PTR_ERR(ec_child));
1826         }
1827
1828         child = lu_object_locate(ec_child->lo_header, ld->ld_type);
1829         if (child == NULL) {
1830                 CERROR("Can not locate the child %s\n", lname->ln_name);
1831                 GOTO(out_put, rc = -EINVAL);
1832         }
1833
1834         if (lu_object_remote(child)) {
1835                 CERROR("Can not destroy remote object %s: rc = %d\n",
1836                        lname->ln_name, -EPERM);
1837                 GOTO(out_put, rc = -EPERM);
1838         }
1839         CDEBUG(D_RPCTRACE, "Start destroy object "DFID" %s %p\n",
1840                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1841
1842         rc = mdo_unlink(env, parent, lu2md(child), lname, ma, 0);
1843         if (rc) {
1844                 CERROR("Can not unlink child %s: rc = %d\n",
1845                         lname->ln_name, rc);
1846                 GOTO(out_put, rc);
1847         }
1848         CDEBUG(D_RPCTRACE, "End destroy object "DFID" %s %p\n",
1849                PFID(lu_object_fid(&parent->mo_lu)), lname->ln_name, parent);
1850 out_put:
1851         lu_object_put(env, ec_child);
1852         return rc;
1853 }
1854
1855 static int echo_destroy_object(const struct lu_env *env,
1856                                struct echo_device *ed,
1857                                struct lu_object *ec_parent,
1858                                char *name, int namelen,
1859                                __u64 id, __u32 mode,
1860                                int count)
1861 {
1862         struct echo_thread_info *info = echo_env_info(env);
1863         struct lu_name          *lname = &info->eti_lname;
1864         struct md_attr          *ma = &info->eti_ma;
1865         struct lu_device        *ld = ed->ed_next;
1866         struct lu_object        *parent;
1867         int                      rc = 0;
1868         int                      i;
1869         ENTRY;
1870
1871         parent = lu_object_locate(ec_parent->lo_header, ld->ld_type);
1872         if (parent == NULL)
1873                 RETURN(-EINVAL);
1874
1875         memset(ma, 0, sizeof(*ma));
1876         ma->ma_attr.la_mode = mode;
1877         ma->ma_attr.la_valid = LA_CTIME;
1878         ma->ma_attr.la_ctime = cfs_time_current_64();
1879         ma->ma_need = MA_INODE;
1880         ma->ma_valid = 0;
1881
1882         if (name != NULL) {
1883                 lname->ln_name = name;
1884                 lname->ln_namelen = namelen;
1885                 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1886                                               ma);
1887                 RETURN(rc);
1888         }
1889
1890         /*prepare the requests*/
1891         for (i = 0; i < count; i++) {
1892                 char *tmp_name = info->eti_name;
1893
1894                 ma->ma_valid = 0;
1895                 echo_md_build_name(lname, tmp_name, id);
1896
1897                 rc = echo_md_destroy_internal(env, ed, lu2md(parent), lname,
1898                                               ma);
1899                 if (rc) {
1900                         CERROR("Can not unlink child %s: rc = %d\n", name, rc);
1901                         break;
1902                 }
1903                 id++;
1904         }
1905
1906         RETURN(rc);
1907 }
1908
1909 static struct lu_object *echo_resolve_path(const struct lu_env *env,
1910                                            struct echo_device *ed, char *path,
1911                                            int path_len)
1912 {
1913         struct lu_device        *ld = ed->ed_next;
1914         struct md_device        *md = lu2md_dev(ld);
1915         struct echo_thread_info *info = echo_env_info(env);
1916         struct lu_fid           *fid = &info->eti_fid;
1917         struct lu_name          *lname = &info->eti_lname;
1918         struct lu_object        *parent = NULL;
1919         struct lu_object        *child = NULL;
1920         int rc = 0;
1921         ENTRY;
1922
1923         /*Only support MDD layer right now*/
1924         rc = md->md_ops->mdo_root_get(env, md, fid);
1925         if (rc) {
1926                 CERROR("get root error: rc = %d\n", rc);
1927                 RETURN(ERR_PTR(rc));
1928         }
1929
1930         /* In the function below, .hs_keycmp resolves to
1931          * lu_obj_hop_keycmp() */
1932         /* coverity[overrun-buffer-val] */
1933         parent = lu_object_find_at(env, &ed->ed_cl.cd_lu_dev, fid, NULL);
1934         if (IS_ERR(parent)) {
1935                 CERROR("Can not find the parent "DFID": rc = %ld\n",
1936                         PFID(fid), PTR_ERR(parent));
1937                 RETURN(parent);
1938         }
1939
1940         while (1) {
1941                 struct lu_object *ld_parent;
1942                 char *e;
1943
1944                 e = strsep(&path, "/");
1945                 if (e == NULL)
1946                         break;
1947
1948                 if (e[0] == 0) {
1949                         if (!path || path[0] == '\0')
1950                                 break;
1951                         continue;
1952                 }
1953
1954                 lname->ln_name = e;
1955                 lname->ln_namelen = strlen(e);
1956
1957                 ld_parent = lu_object_locate(parent->lo_header, ld->ld_type);
1958                 if (ld_parent == NULL) {
1959                         lu_object_put(env, parent);
1960                         rc = -EINVAL;
1961                         break;
1962                 }
1963
1964                 child = echo_md_lookup(env, ed, lu2md(ld_parent), lname);
1965                 lu_object_put(env, parent);
1966                 if (IS_ERR(child)) {
1967                         rc = (int)PTR_ERR(child);
1968                         CERROR("lookup %s under parent "DFID": rc = %d\n",
1969                                 lname->ln_name, PFID(lu_object_fid(ld_parent)),
1970                                 rc);
1971                         break;
1972                 }
1973                 parent = child;
1974         }
1975         if (rc)
1976                 RETURN(ERR_PTR(rc));
1977
1978         RETURN(parent);
1979 }
1980
1981 static void echo_ucred_init(struct lu_env *env)
1982 {
1983         struct lu_ucred *ucred = lu_ucred(env);
1984
1985         ucred->uc_valid = UCRED_INVALID;
1986
1987         ucred->uc_suppgids[0] = -1;
1988         ucred->uc_suppgids[1] = -1;
1989
1990         ucred->uc_uid = ucred->uc_o_uid  =
1991                                 from_kuid(&init_user_ns, current_uid());
1992         ucred->uc_gid = ucred->uc_o_gid  =
1993                                 from_kgid(&init_user_ns, current_gid());
1994         ucred->uc_fsuid = ucred->uc_o_fsuid =
1995                                 from_kuid(&init_user_ns, current_fsuid());
1996         ucred->uc_fsgid = ucred->uc_o_fsgid =
1997                                 from_kgid(&init_user_ns, current_fsgid());
1998         ucred->uc_cap = cfs_curproc_cap_pack();
1999
2000         /* remove fs privilege for non-root user. */
2001         if (ucred->uc_fsuid)
2002                 ucred->uc_cap &= ~CFS_CAP_FS_MASK;
2003         ucred->uc_valid = UCRED_NEW;
2004 }
2005
2006 static void echo_ucred_fini(struct lu_env *env)
2007 {
2008         struct lu_ucred *ucred = lu_ucred(env);
2009         ucred->uc_valid = UCRED_INIT;
2010 }
2011
2012 #define ECHO_MD_CTX_TAG (LCT_REMEMBER | LCT_MD_THREAD)
2013 #define ECHO_MD_SES_TAG (LCT_REMEMBER | LCT_SESSION | LCT_SERVER_SESSION)
2014 static int echo_md_handler(struct echo_device *ed, int command,
2015                            char *path, int path_len, __u64 id, int count,
2016                            struct obd_ioctl_data *data)
2017 {
2018         struct echo_thread_info *info;
2019         struct lu_device      *ld = ed->ed_next;
2020         struct lu_env         *env;
2021         int                    refcheck;
2022         struct lu_object      *parent;
2023         char                  *name = NULL;
2024         int                    namelen = data->ioc_plen2;
2025         int                    rc = 0;
2026         ENTRY;
2027
2028         if (ld == NULL) {
2029                 CERROR("MD echo client is not being initialized properly\n");
2030                 RETURN(-EINVAL);
2031         }
2032
2033         if (strcmp(ld->ld_type->ldt_name, LUSTRE_MDD_NAME)) {
2034                 CERROR("Only support MDD layer right now!\n");
2035                 RETURN(-EINVAL);
2036         }
2037
2038         env = cl_env_get(&refcheck);
2039         if (IS_ERR(env))
2040                 RETURN(PTR_ERR(env));
2041
2042         rc = lu_env_refill_by_tags(env, ECHO_MD_CTX_TAG, ECHO_MD_SES_TAG);
2043         if (rc != 0)
2044                 GOTO(out_env, rc);
2045
2046         /* init big_lmm buffer */
2047         info = echo_env_info(env);
2048         LASSERT(info->eti_big_lmm == NULL);
2049         OBD_ALLOC_LARGE(info->eti_big_lmm, MIN_MD_SIZE);
2050         if (info->eti_big_lmm == NULL)
2051                 GOTO(out_env, rc = -ENOMEM);
2052         info->eti_big_lmmsize = MIN_MD_SIZE;
2053
2054         parent = echo_resolve_path(env, ed, path, path_len);
2055         if (IS_ERR(parent)) {
2056                 CERROR("Can not resolve the path %s: rc = %ld\n", path,
2057                         PTR_ERR(parent));
2058                 GOTO(out_free, rc = PTR_ERR(parent));
2059         }
2060
2061         if (namelen > 0) {
2062                 OBD_ALLOC(name, namelen + 1);
2063                 if (name == NULL)
2064                         GOTO(out_put, rc = -ENOMEM);
2065                 if (copy_from_user(name, data->ioc_pbuf2, namelen))
2066                         GOTO(out_name, rc = -EFAULT);
2067         }
2068
2069         echo_ucred_init(env);
2070
2071         switch (command) {
2072         case ECHO_MD_CREATE:
2073         case ECHO_MD_MKDIR: {
2074                 struct echo_thread_info *info = echo_env_info(env);
2075                 __u32 mode = data->ioc_obdo2.o_mode;
2076                 struct lu_fid *fid = &info->eti_fid;
2077                 int stripe_count = (int)data->ioc_obdo2.o_misc;
2078                 int stripe_index = (int)data->ioc_obdo2.o_stripe_idx;
2079
2080                 rc = ostid_to_fid(fid, &data->ioc_obdo1.o_oi, 0);
2081                 if (rc != 0)
2082                         break;
2083
2084                 /* In the function below, .hs_keycmp resolves to
2085                  * lu_obj_hop_keycmp() */
2086                 /* coverity[overrun-buffer-val] */
2087                 rc = echo_create_md_object(env, ed, parent, fid, name, namelen,
2088                                            id, mode, count, stripe_count,
2089                                            stripe_index);
2090                 break;
2091         }
2092         case ECHO_MD_DESTROY:
2093         case ECHO_MD_RMDIR: {
2094                 __u32 mode = data->ioc_obdo2.o_mode;
2095
2096                 rc = echo_destroy_object(env, ed, parent, name, namelen,
2097                                          id, mode, count);
2098                 break;
2099         }
2100         case ECHO_MD_LOOKUP:
2101                 rc = echo_lookup_object(env, ed, parent, id, count);
2102                 break;
2103         case ECHO_MD_GETATTR:
2104                 rc = echo_getattr_object(env, ed, parent, id, count);
2105                 break;
2106         case ECHO_MD_SETATTR:
2107                 rc = echo_setattr_object(env, ed, parent, id, count);
2108                 break;
2109         default:
2110                 CERROR("unknown command %d\n", command);
2111                 rc = -EINVAL;
2112                 break;
2113         }
2114         echo_ucred_fini(env);
2115
2116 out_name:
2117         if (name != NULL)
2118                 OBD_FREE(name, namelen + 1);
2119 out_put:
2120         lu_object_put(env, parent);
2121 out_free:
2122         LASSERT(info->eti_big_lmm);
2123         OBD_FREE_LARGE(info->eti_big_lmm, info->eti_big_lmmsize);
2124         info->eti_big_lmm = NULL;
2125         info->eti_big_lmmsize = 0;
2126 out_env:
2127         cl_env_put(env, &refcheck);
2128         return rc;
2129 }
2130 #endif /* HAVE_SERVER_SUPPORT */
2131
2132 static int echo_create_object(const struct lu_env *env, struct echo_device *ed,
2133                               int on_target, struct obdo *oa, void __user *ulsm,
2134                               int ulsm_nob, struct obd_trans_info *oti)
2135 {
2136         struct echo_object     *eco;
2137         struct echo_client_obd *ec = ed->ed_ec;
2138         struct lov_stripe_md   *lsm = NULL;
2139         int                     rc;
2140         int                     created = 0;
2141         ENTRY;
2142
2143         if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
2144             (on_target ||                       /* set_stripe */
2145              ec->ec_nstripes != 0)) {           /* LOV */
2146                 CERROR ("No valid oid\n");
2147                 RETURN(-EINVAL);
2148         }
2149
2150         rc = echo_alloc_memmd(ed, &lsm);
2151         if (rc < 0) {
2152                 CERROR("Cannot allocate md: rc = %d\n", rc);
2153                 GOTO(failed, rc);
2154         }
2155
2156         if (ulsm != NULL) {
2157                 int i, idx;
2158
2159                 rc = echo_copyin_lsm (ed, lsm, ulsm, ulsm_nob);
2160                 if (rc != 0)
2161                         GOTO(failed, rc);
2162
2163                 if (lsm->lsm_stripe_count == 0)
2164                         lsm->lsm_stripe_count = ec->ec_nstripes;
2165
2166                 if (lsm->lsm_stripe_size == 0)
2167                         lsm->lsm_stripe_size = PAGE_CACHE_SIZE;
2168
2169                 idx = cfs_rand();
2170
2171                 /* setup stripes: indices + default ids if required */
2172                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
2173                         if (ostid_id(&lsm->lsm_oinfo[i]->loi_oi) == 0)
2174                                 lsm->lsm_oinfo[i]->loi_oi = lsm->lsm_oi;
2175
2176                         lsm->lsm_oinfo[i]->loi_ost_idx =
2177                                 (idx + i) % ec->ec_nstripes;
2178                 }
2179         }
2180
2181         /* setup object ID here for !on_target and LOV hint */
2182         if (oa->o_valid & OBD_MD_FLID) {
2183                 LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2184                 lsm->lsm_oi = oa->o_oi;
2185         }
2186
2187         if (ostid_id(&lsm->lsm_oi) == 0)
2188                 ostid_set_id(&lsm->lsm_oi, ++last_object_id);
2189
2190         rc = 0;
2191         if (on_target) {
2192                 /* Only echo objects are allowed to be created */
2193                 LASSERT((oa->o_valid & OBD_MD_FLGROUP) &&
2194                         (ostid_seq(&oa->o_oi) == FID_SEQ_ECHO));
2195                 rc = obd_create(env, ec->ec_exp, oa, &lsm, oti);
2196                 if (rc != 0) {
2197                         CERROR("Cannot create objects: rc = %d\n", rc);
2198                         GOTO(failed, rc);
2199                 }
2200                 created = 1;
2201         }
2202
2203         /* See what object ID we were given */
2204         oa->o_oi = lsm->lsm_oi;
2205         oa->o_valid |= OBD_MD_FLID;
2206
2207         eco = cl_echo_object_find(ed, &lsm);
2208         if (IS_ERR(eco))
2209                 GOTO(failed, rc = PTR_ERR(eco));
2210         cl_echo_object_put(eco);
2211
2212         CDEBUG(D_INFO, "oa oid "DOSTID"\n", POSTID(&oa->o_oi));
2213         EXIT;
2214
2215  failed:
2216         if (created && rc)
2217                 obd_destroy(env, ec->ec_exp, oa, lsm, oti, NULL, NULL);
2218         if (lsm)
2219                 echo_free_memmd(ed, &lsm);
2220         if (rc)
2221                 CERROR("create object failed with: rc = %d\n", rc);
2222         return (rc);
2223 }
2224
2225 static int echo_get_object(struct echo_object **ecop, struct echo_device *ed,
2226                            struct obdo *oa)
2227 {
2228         struct lov_stripe_md   *lsm = NULL;
2229         struct echo_object     *eco;
2230         int                     rc;
2231         ENTRY;
2232
2233         if ((oa->o_valid & OBD_MD_FLID) == 0 || ostid_id(&oa->o_oi) == 0) {
2234                 /* disallow use of object id 0 */
2235                 CERROR ("No valid oid\n");
2236                 RETURN(-EINVAL);
2237         }
2238
2239         rc = echo_alloc_memmd(ed, &lsm);
2240         if (rc < 0)
2241                 RETURN(rc);
2242
2243         lsm->lsm_oi = oa->o_oi;
2244         if (!(oa->o_valid & OBD_MD_FLGROUP))
2245                 ostid_set_seq_echo(&lsm->lsm_oi);
2246
2247         rc = 0;
2248         eco = cl_echo_object_find(ed, &lsm);
2249         if (!IS_ERR(eco))
2250                 *ecop = eco;
2251         else
2252                 rc = PTR_ERR(eco);
2253         if (lsm)
2254                 echo_free_memmd(ed, &lsm);
2255         RETURN(rc);
2256 }
2257
2258 static void echo_put_object(struct echo_object *eco)
2259 {
2260         int rc;
2261
2262         rc = cl_echo_object_put(eco);
2263         if (rc)
2264                 CERROR("%s: echo client drop an object failed: rc = %d\n",
2265                        eco->eo_dev->ed_ec->ec_exp->exp_obd->obd_name, rc);
2266 }
2267
2268 static void
2269 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
2270 {
2271         unsigned long stripe_count;
2272         unsigned long stripe_size;
2273         unsigned long width;
2274         unsigned long woffset;
2275         int           stripe_index;
2276         obd_off       offset;
2277
2278         if (lsm->lsm_stripe_count <= 1)
2279                 return;
2280
2281         offset       = *offp;
2282         stripe_size  = lsm->lsm_stripe_size;
2283         stripe_count = lsm->lsm_stripe_count;
2284
2285         /* width = # bytes in all stripes */
2286         width = stripe_size * stripe_count;
2287
2288         /* woffset = offset within a width; offset = whole number of widths */
2289         woffset = do_div (offset, width);
2290
2291         stripe_index = woffset / stripe_size;
2292
2293         *idp = ostid_id(&lsm->lsm_oinfo[stripe_index]->loi_oi);
2294         *offp = offset * stripe_size + woffset % stripe_size;
2295 }
2296
2297 static void
2298 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
2299                              struct page *page, int rw, obd_id id,
2300                              obd_off offset, obd_off count)
2301 {
2302         char    *addr;
2303         obd_off  stripe_off;
2304         obd_id   stripe_id;
2305         int      delta;
2306
2307         /* no partial pages on the client */
2308         LASSERT(count == PAGE_CACHE_SIZE);
2309
2310         addr = kmap(page);
2311
2312         for (delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2313                 if (rw == OBD_BRW_WRITE) {
2314                         stripe_off = offset + delta;
2315                         stripe_id = id;
2316                         echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
2317                 } else {
2318                         stripe_off = 0xdeadbeef00c0ffeeULL;
2319                         stripe_id = 0xdeadbeef00c0ffeeULL;
2320                 }
2321                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
2322                                   stripe_off, stripe_id);
2323         }
2324
2325         kunmap(page);
2326 }
2327
2328 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
2329                                         struct page *page, obd_id id,
2330                                         obd_off offset, obd_off count)
2331 {
2332         obd_off stripe_off;
2333         obd_id  stripe_id;
2334         char   *addr;
2335         int     delta;
2336         int     rc;
2337         int     rc2;
2338
2339         /* no partial pages on the client */
2340         LASSERT(count == PAGE_CACHE_SIZE);
2341
2342         addr = kmap(page);
2343
2344         for (rc = delta = 0; delta < PAGE_CACHE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
2345                 stripe_off = offset + delta;
2346                 stripe_id = id;
2347                 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
2348
2349                 rc2 = block_debug_check("test_brw",
2350                                         addr + delta, OBD_ECHO_BLOCK_SIZE,
2351                                         stripe_off, stripe_id);
2352                 if (rc2 != 0) {
2353                         CERROR ("Error in echo object "LPX64"\n", id);
2354                         rc = rc2;
2355                 }
2356         }
2357
2358         kunmap(page);
2359         return rc;
2360 }
2361
2362 static int echo_client_kbrw(struct echo_device *ed, int rw, struct obdo *oa,
2363                             struct echo_object *eco, obd_off offset,
2364                             obd_size count, int async,
2365                             struct obd_trans_info *oti)
2366 {
2367         struct lov_stripe_md   *lsm = eco->eo_lsm;
2368         obd_count               npages;
2369         struct brw_page        *pga;
2370         struct brw_page        *pgp;
2371         struct page            **pages;
2372         obd_off                 off;
2373         int                     i;
2374         int                     rc;
2375         int                     verify;
2376         gfp_t                   gfp_mask;
2377         int                     brw_flags = 0;
2378         ENTRY;
2379
2380         verify = (ostid_id(&oa->o_oi) != ECHO_PERSISTENT_OBJID &&
2381                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
2382                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
2383
2384         gfp_mask = ((ostid_id(&oa->o_oi) & 2) == 0) ? GFP_IOFS : GFP_HIGHUSER;
2385
2386         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
2387         LASSERT(lsm != NULL);
2388         LASSERT(ostid_id(&lsm->lsm_oi) == ostid_id(&oa->o_oi));
2389
2390         if (count <= 0 ||
2391             (count & (~CFS_PAGE_MASK)) != 0)
2392                 RETURN(-EINVAL);
2393
2394         /* XXX think again with misaligned I/O */
2395         npages = count >> PAGE_CACHE_SHIFT;
2396
2397         if (rw == OBD_BRW_WRITE)
2398                 brw_flags = OBD_BRW_ASYNC;
2399
2400         OBD_ALLOC(pga, npages * sizeof(*pga));
2401         if (pga == NULL)
2402                 RETURN(-ENOMEM);
2403
2404         OBD_ALLOC(pages, npages * sizeof(*pages));
2405         if (pages == NULL) {
2406                 OBD_FREE(pga, npages * sizeof(*pga));
2407                 RETURN(-ENOMEM);
2408         }
2409
2410         for (i = 0, pgp = pga, off = offset;
2411              i < npages;
2412              i++, pgp++, off += PAGE_CACHE_SIZE) {
2413
2414                 LASSERT (pgp->pg == NULL);      /* for cleanup */
2415
2416                 rc = -ENOMEM;
2417                 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
2418                 if (pgp->pg == NULL)
2419                         goto out;
2420
2421                 pages[i] = pgp->pg;
2422                 pgp->count = PAGE_CACHE_SIZE;
2423                 pgp->off = off;
2424                 pgp->flag = brw_flags;
2425
2426                 if (verify)
2427                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
2428                                                      ostid_id(&oa->o_oi), off,
2429                                                      pgp->count);
2430         }
2431
2432         /* brw mode can only be used at client */
2433         LASSERT(ed->ed_next != NULL);
2434         rc = cl_echo_object_brw(eco, rw, offset, pages, npages, async);
2435
2436  out:
2437         if (rc != 0 || rw != OBD_BRW_READ)
2438                 verify = 0;
2439
2440         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
2441                 if (pgp->pg == NULL)
2442                         continue;
2443
2444                 if (verify) {
2445                         int vrc;
2446                         vrc = echo_client_page_debug_check(lsm, pgp->pg,
2447                                                            ostid_id(&oa->o_oi),
2448                                                            pgp->off, pgp->count);
2449                         if (vrc != 0 && rc == 0)
2450                                 rc = vrc;
2451                 }
2452                 OBD_PAGE_FREE(pgp->pg);
2453         }
2454         OBD_FREE(pga, npages * sizeof(*pga));
2455         OBD_FREE(pages, npages * sizeof(*pages));
2456         RETURN(rc);
2457 }
2458
2459 static int echo_client_prep_commit(const struct lu_env *env,
2460                                    struct obd_export *exp, int rw,
2461                                    struct obdo *oa, struct echo_object *eco,
2462                                    obd_off offset, obd_size count,
2463                                    obd_size batch, struct obd_trans_info *oti,
2464                                    int async)
2465 {
2466         struct lov_stripe_md *lsm = eco->eo_lsm;
2467         struct obd_ioobj ioo;
2468         struct niobuf_local *lnb;
2469         struct niobuf_remote *rnb;
2470         obd_off off;
2471         obd_size npages, tot_pages;
2472         int i, ret = 0, brw_flags = 0;
2473
2474         ENTRY;
2475
2476         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
2477             (lsm != NULL && ostid_id(&lsm->lsm_oi) != ostid_id(&oa->o_oi)))
2478                 RETURN(-EINVAL);
2479
2480         npages = batch >> PAGE_CACHE_SHIFT;
2481         tot_pages = count >> PAGE_CACHE_SHIFT;
2482
2483         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
2484         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
2485
2486         if (lnb == NULL || rnb == NULL)
2487                 GOTO(out, ret = -ENOMEM);
2488
2489         if (rw == OBD_BRW_WRITE && async)
2490                 brw_flags |= OBD_BRW_ASYNC;
2491
2492         obdo_to_ioobj(oa, &ioo);
2493
2494         off = offset;
2495
2496         for(; tot_pages; tot_pages -= npages) {
2497                 int lpages;
2498
2499                 if (tot_pages < npages)
2500                         npages = tot_pages;
2501
2502                 for (i = 0; i < npages; i++, off += PAGE_CACHE_SIZE) {
2503                         rnb[i].rnb_offset = off;
2504                         rnb[i].rnb_len = PAGE_CACHE_SIZE;
2505                         rnb[i].rnb_flags = brw_flags;
2506                 }
2507
2508                 ioo.ioo_bufcnt = npages;
2509
2510                 lpages = npages;
2511                 ret = obd_preprw(env, rw, exp, oa, 1, &ioo, rnb, &lpages,
2512                                  lnb, oti, NULL);
2513                 if (ret != 0)
2514                         GOTO(out, ret);
2515                 LASSERT(lpages == npages);
2516
2517                 for (i = 0; i < lpages; i++) {
2518                         struct page *page = lnb[i].lnb_page;
2519
2520                         /* read past eof? */
2521                         if (page == NULL && lnb[i].lnb_rc == 0)
2522                                 continue;
2523
2524                         if (async)
2525                                 lnb[i].lnb_flags |= OBD_BRW_ASYNC;
2526
2527                         if (ostid_id(&oa->o_oi) == ECHO_PERSISTENT_OBJID ||
2528                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
2529                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
2530                                 continue;
2531
2532                         if (rw == OBD_BRW_WRITE)
2533                                 echo_client_page_debug_setup(lsm, page, rw,
2534                                                             ostid_id(&oa->o_oi),
2535                                                              rnb[i].rnb_offset,
2536                                                              rnb[i].rnb_len);
2537                         else
2538                                 echo_client_page_debug_check(lsm, page,
2539                                                             ostid_id(&oa->o_oi),
2540                                                              rnb[i].rnb_offset,
2541                                                              rnb[i].rnb_len);
2542                 }
2543
2544                 ret = obd_commitrw(env, rw, exp, oa, 1, &ioo,
2545                                    rnb, npages, lnb, oti, ret);
2546                 if (ret != 0)
2547                         GOTO(out, ret);
2548
2549                 /* Reset oti otherwise it would confuse ldiskfs. */
2550                 memset(oti, 0, sizeof(*oti));
2551
2552                 /* Reuse env context. */
2553                 lu_context_exit((struct lu_context *)&env->le_ctx);
2554                 lu_context_enter((struct lu_context *)&env->le_ctx);
2555         }
2556
2557 out:
2558         if (lnb)
2559                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
2560         if (rnb)
2561                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
2562         RETURN(ret);
2563 }
2564
2565 static int echo_client_brw_ioctl(const struct lu_env *env, int rw,
2566                                  struct obd_export *exp,
2567                                  struct obd_ioctl_data *data,
2568                                  struct obd_trans_info *dummy_oti)
2569 {
2570         struct obd_device *obd = class_exp2obd(exp);
2571         struct echo_device *ed = obd2echo_dev(obd);
2572         struct echo_client_obd *ec = ed->ed_ec;
2573         struct obdo *oa = &data->ioc_obdo1;
2574         struct echo_object *eco;
2575         int rc;
2576         int async = 0;
2577         long test_mode;
2578         ENTRY;
2579
2580         LASSERT(oa->o_valid & OBD_MD_FLGROUP);
2581
2582         rc = echo_get_object(&eco, ed, oa);
2583         if (rc)
2584                 RETURN(rc);
2585
2586         oa->o_valid &= ~OBD_MD_FLHANDLE;
2587
2588         /* OFD/obdfilter works only via prep/commit */
2589         test_mode = (long)data->ioc_pbuf1;
2590         if (ed->ed_next == NULL && test_mode != 3) {
2591                 test_mode = 3;
2592                 data->ioc_plen1 = data->ioc_count;
2593         }
2594
2595         if (test_mode == 3)
2596                 async = 1;
2597
2598         /* Truncate batch size to maximum */
2599         if (data->ioc_plen1 > PTLRPC_MAX_BRW_SIZE)
2600                 data->ioc_plen1 = PTLRPC_MAX_BRW_SIZE;
2601
2602         switch (test_mode) {
2603         case 1:
2604                 /* fall through */
2605         case 2:
2606                 rc = echo_client_kbrw(ed, rw, oa,
2607                                       eco, data->ioc_offset,
2608                                       data->ioc_count, async, dummy_oti);
2609                 break;
2610         case 3:
2611                 rc = echo_client_prep_commit(env, ec->ec_exp, rw, oa,
2612                                              eco, data->ioc_offset,
2613                                              data->ioc_count, data->ioc_plen1,
2614                                              dummy_oti, async);
2615                 break;
2616         default:
2617                 rc = -EINVAL;
2618         }
2619         echo_put_object(eco);
2620         RETURN(rc);
2621 }
2622
2623 static int
2624 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2625                       void *karg, void *uarg)
2626 {
2627 #ifdef HAVE_SERVER_SUPPORT
2628         struct tgt_session_info *tsi;
2629 #endif
2630         struct obd_device      *obd = exp->exp_obd;
2631         struct echo_device     *ed = obd2echo_dev(obd);
2632         struct echo_client_obd *ec = ed->ed_ec;
2633         struct echo_object     *eco;
2634         struct obd_ioctl_data  *data = karg;
2635         struct obd_trans_info   dummy_oti;
2636         struct lu_env          *env;
2637         struct oti_req_ack_lock *ack_lock;
2638         struct obdo            *oa;
2639         struct lu_fid           fid;
2640         int                     rw = OBD_BRW_READ;
2641         int                     rc = 0;
2642         int                     i;
2643 #ifdef HAVE_SERVER_SUPPORT
2644         struct lu_context        echo_session;
2645 #endif
2646         ENTRY;
2647
2648         memset(&dummy_oti, 0, sizeof(dummy_oti));
2649
2650         oa = &data->ioc_obdo1;
2651         if (!(oa->o_valid & OBD_MD_FLGROUP)) {
2652                 oa->o_valid |= OBD_MD_FLGROUP;
2653                 ostid_set_seq_echo(&oa->o_oi);
2654         }
2655
2656         /* This FID is unpacked just for validation at this point */
2657         rc = ostid_to_fid(&fid, &oa->o_oi, 0);
2658         if (rc < 0)
2659                 RETURN(rc);
2660
2661         OBD_ALLOC_PTR(env);
2662         if (env == NULL)
2663                 RETURN(-ENOMEM);
2664
2665         rc = lu_env_init(env, LCT_DT_THREAD);
2666         if (rc)
2667                 GOTO(out_alloc, rc = -ENOMEM);
2668
2669 #ifdef HAVE_SERVER_SUPPORT
2670         env->le_ses = &echo_session;
2671         rc = lu_context_init(env->le_ses, LCT_SERVER_SESSION | LCT_NOREF);
2672         if (unlikely(rc < 0))
2673                 GOTO(out_env, rc);
2674         lu_context_enter(env->le_ses);
2675
2676         tsi = tgt_ses_info(env);
2677         tsi->tsi_exp = ec->ec_exp;
2678         tsi->tsi_jobid = NULL;
2679 #endif
2680         switch (cmd) {
2681         case OBD_IOC_CREATE:                    /* may create echo object */
2682                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2683                         GOTO (out, rc = -EPERM);
2684
2685                 rc = echo_create_object(env, ed, 1, oa, data->ioc_pbuf1,
2686                                         data->ioc_plen1, &dummy_oti);
2687                 GOTO(out, rc);
2688
2689 #ifdef HAVE_SERVER_SUPPORT
2690         case OBD_IOC_ECHO_MD: {
2691                 int count;
2692                 int cmd;
2693                 char *dir = NULL;
2694                 int dirlen;
2695                 __u64 id;
2696
2697                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2698                         GOTO(out, rc = -EPERM);
2699
2700                 count = data->ioc_count;
2701                 cmd = data->ioc_command;
2702
2703                 id = data->ioc_obdo2.o_oi.oi.oi_id;
2704                 dirlen = data->ioc_plen1;
2705                 OBD_ALLOC(dir, dirlen + 1);
2706                 if (dir == NULL)
2707                         GOTO(out, rc = -ENOMEM);
2708
2709                 if (copy_from_user(dir, data->ioc_pbuf1, dirlen)) {
2710                         OBD_FREE(dir, data->ioc_plen1 + 1);
2711                         GOTO(out, rc = -EFAULT);
2712                 }
2713
2714                 rc = echo_md_handler(ed, cmd, dir, dirlen, id, count, data);
2715                 OBD_FREE(dir, dirlen + 1);
2716                 GOTO(out, rc);
2717         }
2718         case OBD_IOC_ECHO_ALLOC_SEQ: {
2719                 struct lu_env   *cl_env;
2720                 int              refcheck;
2721                 __u64            seq;
2722                 int              max_count;
2723
2724                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2725                         GOTO(out, rc = -EPERM);
2726
2727                 cl_env = cl_env_get(&refcheck);
2728                 if (IS_ERR(cl_env))
2729                         GOTO(out, rc = PTR_ERR(cl_env));
2730
2731                 rc = lu_env_refill_by_tags(cl_env, ECHO_MD_CTX_TAG,
2732                                             ECHO_MD_SES_TAG);
2733                 if (rc != 0) {
2734                         cl_env_put(cl_env, &refcheck);
2735                         GOTO(out, rc);
2736                 }
2737
2738                 rc = seq_client_get_seq(cl_env, ed->ed_cl_seq, &seq);
2739                 cl_env_put(cl_env, &refcheck);
2740                 if (rc < 0) {
2741                         CERROR("%s: Can not alloc seq: rc = %d\n",
2742                                obd->obd_name, rc);
2743                         GOTO(out, rc);
2744                 }
2745
2746                 if (copy_to_user(data->ioc_pbuf1, &seq, data->ioc_plen1))
2747                         return -EFAULT;
2748
2749                 max_count = LUSTRE_METADATA_SEQ_MAX_WIDTH;
2750                 if (copy_to_user(data->ioc_pbuf2, &max_count,
2751                                      data->ioc_plen2))
2752                         return -EFAULT;
2753                 GOTO(out, rc);
2754         }
2755 #endif /* HAVE_SERVER_SUPPORT */
2756         case OBD_IOC_DESTROY:
2757                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2758                         GOTO (out, rc = -EPERM);
2759
2760                 rc = echo_get_object(&eco, ed, oa);
2761                 if (rc == 0) {
2762                         rc = obd_destroy(env, ec->ec_exp, oa, eco->eo_lsm,
2763                                          &dummy_oti, NULL, NULL);
2764                         if (rc == 0)
2765                                 eco->eo_deleted = 1;
2766                         echo_put_object(eco);
2767                 }
2768                 GOTO(out, rc);
2769
2770         case OBD_IOC_GETATTR:
2771                 rc = echo_get_object(&eco, ed, oa);
2772                 if (rc == 0) {
2773                         struct obd_info oinfo = { { { 0 } } };
2774                         oinfo.oi_md = eco->eo_lsm;
2775                         oinfo.oi_oa = oa;
2776                         rc = obd_getattr(env, ec->ec_exp, &oinfo);
2777                         echo_put_object(eco);
2778                 }
2779                 GOTO(out, rc);
2780
2781         case OBD_IOC_SETATTR:
2782                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2783                         GOTO (out, rc = -EPERM);
2784
2785                 rc = echo_get_object(&eco, ed, oa);
2786                 if (rc == 0) {
2787                         struct obd_info oinfo = { { { 0 } } };
2788                         oinfo.oi_oa = oa;
2789                         oinfo.oi_md = eco->eo_lsm;
2790
2791                         rc = obd_setattr(env, ec->ec_exp, &oinfo, NULL);
2792                         echo_put_object(eco);
2793                 }
2794                 GOTO(out, rc);
2795
2796         case OBD_IOC_BRW_WRITE:
2797                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
2798                         GOTO (out, rc = -EPERM);
2799
2800                 rw = OBD_BRW_WRITE;
2801                 /* fall through */
2802         case OBD_IOC_BRW_READ:
2803                 rc = echo_client_brw_ioctl(env, rw, exp, data, &dummy_oti);
2804                 GOTO(out, rc);
2805
2806         default:
2807                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
2808                 GOTO (out, rc = -ENOTTY);
2809         }
2810
2811         EXIT;
2812 out:
2813 #ifdef HAVE_SERVER_SUPPORT
2814         lu_context_exit(env->le_ses);
2815         lu_context_fini(env->le_ses);
2816 out_env:
2817 #endif
2818         lu_env_fini(env);
2819 out_alloc:
2820         OBD_FREE_PTR(env);
2821
2822         /* XXX this should be in a helper also called by target_send_reply */
2823         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
2824              i++, ack_lock++) {
2825                 if (!ack_lock->mode)
2826                         break;
2827                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
2828         }
2829
2830         return rc;
2831 }
2832
2833 static int echo_client_setup(const struct lu_env *env,
2834                              struct obd_device *obddev, struct lustre_cfg *lcfg)
2835 {
2836         struct echo_client_obd *ec = &obddev->u.echo_client;
2837         struct obd_device *tgt;
2838         struct obd_uuid echo_uuid = { "ECHO_UUID" };
2839         struct obd_connect_data *ocd = NULL;
2840         int rc;
2841         ENTRY;
2842
2843         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
2844                 CERROR("requires a TARGET OBD name\n");
2845                 RETURN(-EINVAL);
2846         }
2847
2848         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
2849         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
2850                 CERROR("device not attached or not set up (%s)\n",
2851                        lustre_cfg_string(lcfg, 1));
2852                 RETURN(-EINVAL);
2853         }
2854
2855         spin_lock_init(&ec->ec_lock);
2856         INIT_LIST_HEAD(&ec->ec_objects);
2857         INIT_LIST_HEAD(&ec->ec_locks);
2858         ec->ec_unique = 0;
2859         ec->ec_nstripes = 0;
2860
2861         if (!strcmp(tgt->obd_type->typ_name, LUSTRE_MDT_NAME)) {
2862 #ifdef HAVE_SERVER_SUPPORT
2863                 lu_context_tags_update(ECHO_MD_CTX_TAG);
2864                 lu_session_tags_update(ECHO_MD_SES_TAG);
2865 #else
2866                 CERROR("Local operations are NOT supported on client side. "
2867                        "Only remote operations are supported. Metadata client "
2868                        "must be run on server side.\n");
2869 #endif
2870                 RETURN(0);
2871         }
2872
2873         OBD_ALLOC(ocd, sizeof(*ocd));
2874         if (ocd == NULL) {
2875                 CERROR("Can't alloc ocd connecting to %s\n",
2876                        lustre_cfg_string(lcfg, 1));
2877                 return -ENOMEM;
2878         }
2879
2880         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL |
2881                                  OBD_CONNECT_BRW_SIZE |
2882                                  OBD_CONNECT_GRANT | OBD_CONNECT_FULL20 |
2883                                  OBD_CONNECT_64BITHASH | OBD_CONNECT_LVB_TYPE |
2884                                  OBD_CONNECT_FID;
2885         ocd->ocd_brw_size = DT_MAX_BRW_SIZE;
2886         ocd->ocd_version = LUSTRE_VERSION_CODE;
2887         ocd->ocd_group = FID_SEQ_ECHO;
2888
2889         rc = obd_connect(env, &ec->ec_exp, tgt, &echo_uuid, ocd, NULL);
2890         if (rc == 0) {
2891                 /* Turn off pinger because it connects to tgt obd directly. */
2892                 spin_lock(&tgt->obd_dev_lock);
2893                 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
2894                 spin_unlock(&tgt->obd_dev_lock);
2895         }
2896
2897         OBD_FREE(ocd, sizeof(*ocd));
2898
2899         if (rc != 0) {
2900                 CERROR("fail to connect to device %s\n",
2901                        lustre_cfg_string(lcfg, 1));
2902                 return (rc);
2903         }
2904
2905         RETURN(rc);
2906 }
2907
2908 static int echo_client_cleanup(struct obd_device *obddev)
2909 {
2910         struct echo_device *ed = obd2echo_dev(obddev);
2911         struct echo_client_obd *ec = &obddev->u.echo_client;
2912         int rc;
2913         ENTRY;
2914
2915         /*Do nothing for Metadata echo client*/
2916         if (ed == NULL )
2917                 RETURN(0);
2918
2919         if (ed->ed_next_ismd) {
2920 #ifdef HAVE_SERVER_SUPPORT
2921                 lu_context_tags_clear(ECHO_MD_CTX_TAG);
2922                 lu_session_tags_clear(ECHO_MD_SES_TAG);
2923 #else
2924                 CERROR("This is client-side only module, does not support "
2925                         "metadata echo client.\n");
2926 #endif
2927                 RETURN(0);
2928         }
2929
2930         if (!list_empty(&obddev->obd_exports)) {
2931                 CERROR("still has clients!\n");
2932                 RETURN(-EBUSY);
2933         }
2934
2935         LASSERT(atomic_read(&ec->ec_exp->exp_refcount) > 0);
2936         rc = obd_disconnect(ec->ec_exp);
2937         if (rc != 0)
2938                 CERROR("fail to disconnect device: %d\n", rc);
2939
2940         RETURN(rc);
2941 }
2942
2943 static int echo_client_connect(const struct lu_env *env,
2944                                struct obd_export **exp,
2945                                struct obd_device *src, struct obd_uuid *cluuid,
2946                                struct obd_connect_data *data, void *localdata)
2947 {
2948         int                rc;
2949         struct lustre_handle conn = { 0 };
2950
2951         ENTRY;
2952         rc = class_connect(&conn, src, cluuid);
2953         if (rc == 0) {
2954                 *exp = class_conn2export(&conn);
2955         }
2956
2957         RETURN (rc);
2958 }
2959
2960 static int echo_client_disconnect(struct obd_export *exp)
2961 {
2962         int                     rc;
2963         ENTRY;
2964
2965         if (exp == NULL)
2966                 GOTO(out, rc = -EINVAL);
2967
2968         rc = class_disconnect(exp);
2969         GOTO(out, rc);
2970  out:
2971         return rc;
2972 }
2973
2974 static struct obd_ops echo_client_obd_ops = {
2975         .o_owner       = THIS_MODULE,
2976         .o_iocontrol   = echo_client_iocontrol,
2977         .o_connect     = echo_client_connect,
2978         .o_disconnect  = echo_client_disconnect
2979 };
2980
2981 int echo_client_init(void)
2982 {
2983         int rc;
2984
2985         rc = lu_kmem_init(echo_caches);
2986         if (rc == 0) {
2987                 rc = class_register_type(&echo_client_obd_ops, NULL, true, NULL,
2988                                          LUSTRE_ECHO_CLIENT_NAME,
2989                                          &echo_device_type);
2990                 if (rc)
2991                         lu_kmem_fini(echo_caches);
2992         }
2993         return rc;
2994 }
2995
2996 void echo_client_exit(void)
2997 {
2998         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
2999         lu_kmem_fini(echo_caches);
3000 }
3001
3002 static int __init obdecho_init(void)
3003 {
3004         int rc;
3005
3006         ENTRY;
3007         LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
3008
3009         LASSERT(PAGE_CACHE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
3010
3011 # ifdef HAVE_SERVER_SUPPORT
3012         rc = echo_persistent_pages_init();
3013         if (rc != 0)
3014                 goto failed_0;
3015
3016         rc = class_register_type(&echo_obd_ops, NULL, true, NULL,
3017                                  LUSTRE_ECHO_NAME, NULL);
3018         if (rc != 0)
3019                 goto failed_1;
3020 # endif
3021
3022         rc = echo_client_init();
3023
3024 # ifdef HAVE_SERVER_SUPPORT
3025         if (rc == 0)
3026                 RETURN(0);
3027
3028         class_unregister_type(LUSTRE_ECHO_NAME);
3029 failed_1:
3030         echo_persistent_pages_fini();
3031 failed_0:
3032 # endif
3033         RETURN(rc);
3034 }
3035
3036 static void /*__exit*/ obdecho_exit(void)
3037 {
3038         echo_client_exit();
3039
3040 # ifdef HAVE_SERVER_SUPPORT
3041         class_unregister_type(LUSTRE_ECHO_NAME);
3042         echo_persistent_pages_fini();
3043 # endif
3044 }
3045
3046 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
3047 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
3048 MODULE_LICENSE("GPL");
3049
3050 cfs_module(obdecho, LUSTRE_VERSION_STRING, obdecho_init, obdecho_exit);
3051
3052 /** @} echo_client */