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