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