1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_ECHO
39 #include <libcfs/libcfs.h>
41 #include <liblustre.h>
45 #include <obd_support.h>
46 #include <obd_class.h>
48 #include <lustre_debug.h>
49 #include <lprocfs_status.h>
51 static obd_id last_object_id;
55 echo_printk_object (char *msg, struct ec_object *eco)
57 struct lov_stripe_md *lsm = eco->eco_lsm;
60 printk (KERN_INFO "Lustre: %s: object %p: "LPX64", refs %d%s: "LPX64
61 "=%u!%u\n", msg, eco, eco->eco_id, eco->eco_refcount,
62 eco->eco_deleted ? "(deleted) " : "",
63 lsm->lsm_object_id, lsm->lsm_stripe_size,
64 lsm->lsm_stripe_count);
66 for (i = 0; i < lsm->lsm_stripe_count; i++)
67 printk (KERN_INFO "Lustre: @%2u:"LPX64"\n",
68 lsm->lsm_oinfo[i].loi_ost_idx,
69 lsm->lsm_oinfo[i].loi_id);
73 static struct ec_object *
74 echo_find_object_locked (struct obd_device *obd, obd_id id)
76 struct echo_client_obd *ec = &obd->u.echo_client;
77 struct ec_object *eco = NULL;
80 list_for_each (el, &ec->ec_objects) {
81 eco = list_entry (el, struct ec_object, eco_obj_chain);
83 if (eco->eco_id == id)
90 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
92 struct lov_stripe_md *ulsm = _ulsm;
95 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
99 if (copy_to_user (ulsm, lsm, sizeof(ulsm)))
102 for (i = 0; i < lsm->lsm_stripe_count; i++) {
103 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
104 sizeof(lsm->lsm_oinfo[0])))
111 echo_copyin_lsm (struct obd_device *obd, struct lov_stripe_md *lsm,
112 void *ulsm, int ulsm_nob)
114 struct echo_client_obd *ec = &obd->u.echo_client;
117 if (ulsm_nob < sizeof (*lsm))
120 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
123 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
124 lsm->lsm_magic != LOV_MAGIC ||
125 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
126 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
129 for (i = 0; i < lsm->lsm_stripe_count; i++) {
130 if (copy_from_user(lsm->lsm_oinfo[i],
131 ((struct lov_stripe_md *)ulsm)->lsm_oinfo[i],
132 sizeof(lsm->lsm_oinfo[0])))
139 static struct ec_object *
140 echo_allocate_object (struct obd_device *obd)
142 struct echo_client_obd *ec = &obd->u.echo_client;
143 struct ec_object *eco;
146 OBD_ALLOC(eco, sizeof (*eco));
150 rc = obd_alloc_memmd(ec->ec_exp, &eco->eco_lsm);
152 OBD_FREE(eco, sizeof (*eco));
156 eco->eco_device = obd;
157 eco->eco_deleted = 0;
158 eco->eco_refcount = 0;
159 eco->eco_lsm->lsm_magic = LOV_MAGIC;
160 /* leave stripe count 0 by default */
166 echo_free_object (struct ec_object *eco)
168 struct obd_device *obd = eco->eco_device;
169 struct echo_client_obd *ec = &obd->u.echo_client;
171 LASSERT (eco->eco_refcount == 0);
173 CERROR("No object %s\n", obd->obd_name);
175 obd_free_memmd(ec->ec_exp, &eco->eco_lsm);
176 OBD_FREE (eco, sizeof (*eco));
179 static int echo_create_object(struct obd_device *obd, int on_target,
180 struct obdo *oa, void *ulsm, int ulsm_nob,
181 struct obd_trans_info *oti)
183 struct echo_client_obd *ec = &obd->u.echo_client;
184 struct ec_object *eco2;
185 struct ec_object *eco;
186 struct lov_stripe_md *lsm;
190 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
191 (on_target || /* set_stripe */
192 ec->ec_nstripes != 0)) { /* LOV */
193 CERROR ("No valid oid\n");
198 eco = echo_allocate_object (obd);
204 rc = echo_copyin_lsm (obd, lsm, ulsm, ulsm_nob);
208 /* setup object ID here for !on_target and LOV hint */
209 if ((oa->o_valid & OBD_MD_FLID) != 0)
210 eco->eco_id = lsm->lsm_object_id = oa->o_id;
212 if (lsm->lsm_stripe_count == 0)
213 lsm->lsm_stripe_count = ec->ec_nstripes;
215 if (lsm->lsm_stripe_size == 0)
216 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
220 /* setup stripes: indices + default ids if required */
221 for (i = 0; i < lsm->lsm_stripe_count; i++) {
222 if (lsm->lsm_oinfo[i]->loi_id == 0)
223 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
225 lsm->lsm_oinfo[i]->loi_ost_idx =
226 (idx + i) % ec->ec_nstripes;
229 OBD_ALLOC(eco, sizeof(*eco));
232 eco->eco_device = obd;
237 oa->o_id = ++last_object_id;
240 oa->o_gr = FILTER_GROUP_ECHO;
241 oa->o_valid |= OBD_MD_FLGROUP;
243 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
247 /* See what object ID we were given */
248 eco->eco_id = oa->o_id = lsm->lsm_object_id;
249 oa->o_valid |= OBD_MD_FLID;
251 LASSERT(eco->eco_lsm == NULL || eco->eco_lsm == lsm);
255 spin_lock (&ec->ec_lock);
257 eco2 = echo_find_object_locked (obd, oa->o_id);
258 if (eco2 != NULL) { /* conflict */
259 spin_unlock (&ec->ec_lock);
261 CERROR ("Can't create object id "LPX64": id already exists%s\n",
262 oa->o_id, on_target ? " (undoing create)" : "");
265 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL);
271 list_add (&eco->eco_obj_chain, &ec->ec_objects);
272 spin_unlock (&ec->ec_lock);
274 "created %p: "LPX64"=%u#%u@%u refs %d del %d\n",
276 eco->eco_lsm->lsm_stripe_size,
277 eco->eco_lsm->lsm_stripe_count,
278 eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
279 eco->eco_refcount, eco->eco_deleted);
283 echo_free_object (eco);
285 CERROR("%s: err %d on create\n", obd->obd_name, rc);
290 echo_get_object (struct ec_object **ecop, struct obd_device *obd,
293 struct echo_client_obd *ec = &obd->u.echo_client;
294 struct ec_object *eco;
295 struct ec_object *eco2;
298 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
299 oa->o_id == 0) /* disallow use of object id 0 */
301 CERROR ("No valid oid\n");
305 spin_lock (&ec->ec_lock);
306 eco = echo_find_object_locked (obd, oa->o_id);
308 if (eco->eco_deleted) { /* being deleted */
309 spin_unlock(&ec->ec_lock);/* (see comment in cleanup) */
314 spin_unlock (&ec->ec_lock);
317 "found %p: "LPX64"=%u#%u@%u refs %d del %d\n",
319 eco->eco_lsm->lsm_stripe_size,
320 eco->eco_lsm->lsm_stripe_count,
321 eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
322 eco->eco_refcount, eco->eco_deleted);
325 spin_unlock (&ec->ec_lock);
327 if (ec->ec_nstripes != 0) /* striping required */
330 eco = echo_allocate_object (obd);
334 eco->eco_id = eco->eco_lsm->lsm_object_id = oa->o_id;
336 spin_lock (&ec->ec_lock);
338 eco2 = echo_find_object_locked (obd, oa->o_id);
339 if (eco2 == NULL) { /* didn't race */
340 list_add (&eco->eco_obj_chain, &ec->ec_objects);
341 spin_unlock (&ec->ec_lock);
342 eco->eco_refcount = 1;
345 "created %p: "LPX64"=%u#%u@%d refs %d del %d\n",
347 eco->eco_lsm->lsm_stripe_size,
348 eco->eco_lsm->lsm_stripe_count,
349 eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
350 eco->eco_refcount, eco->eco_deleted);
354 if (eco2->eco_deleted)
355 rc = -EAGAIN; /* lose race */
357 eco2->eco_refcount++; /* take existing */
360 LASSERT (eco2->eco_id == eco2->eco_lsm->lsm_object_id);
362 "found(2) %p: "LPX64"=%u#%u@%d refs %d del %d\n",
364 eco2->eco_lsm->lsm_stripe_size,
365 eco2->eco_lsm->lsm_stripe_count,
366 eco2->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
367 eco2->eco_refcount, eco2->eco_deleted);
370 spin_unlock (&ec->ec_lock);
372 echo_free_object (eco);
377 echo_put_object (struct ec_object *eco)
379 struct obd_device *obd = eco->eco_device;
380 struct echo_client_obd *ec = &obd->u.echo_client;
382 /* Release caller's ref on the object.
383 * delete => mark for deletion when last ref goes
386 spin_lock (&ec->ec_lock);
389 LASSERT (eco->eco_refcount >= 0);
391 CDEBUG(D_INFO, "put %p: "LPX64"=%u#%u@%d refs %d del %d\n",
393 eco->eco_lsm->lsm_stripe_size,
394 eco->eco_lsm->lsm_stripe_count,
395 eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
396 eco->eco_refcount, eco->eco_deleted);
398 if (eco->eco_refcount != 0 || !eco->eco_deleted) {
399 spin_unlock (&ec->ec_lock);
403 spin_unlock (&ec->ec_lock);
405 /* NB leave obj in the object list. We must prevent anyone from
406 * attempting to enqueue on this object number until we can be
407 * sure there will be no more lock callbacks.
409 obd_cancel_unused(ec->ec_exp, eco->eco_lsm, 0, NULL);
411 /* now we can let it go */
412 spin_lock (&ec->ec_lock);
413 list_del (&eco->eco_obj_chain);
414 spin_unlock (&ec->ec_lock);
416 LASSERT (eco->eco_refcount == 0);
418 echo_free_object (eco);
422 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
424 unsigned long stripe_count;
425 unsigned long stripe_size;
427 unsigned long woffset;
431 if (lsm->lsm_stripe_count <= 1)
435 stripe_size = lsm->lsm_stripe_size;
436 stripe_count = lsm->lsm_stripe_count;
438 /* width = # bytes in all stripes */
439 width = stripe_size * stripe_count;
441 /* woffset = offset within a width; offset = whole number of widths */
442 woffset = do_div (offset, width);
444 stripe_index = woffset / stripe_size;
446 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
447 *offp = offset * stripe_size + woffset % stripe_size;
451 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
452 cfs_page_t *page, int rw, obd_id id,
453 obd_off offset, obd_off count)
460 /* no partial pages on the client */
461 LASSERT(count == CFS_PAGE_SIZE);
463 addr = cfs_kmap(page);
465 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
466 if (rw == OBD_BRW_WRITE) {
467 stripe_off = offset + delta;
469 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
471 stripe_off = 0xdeadbeef00c0ffeeULL;
472 stripe_id = 0xdeadbeef00c0ffeeULL;
474 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
475 stripe_off, stripe_id);
482 echo_client_page_debug_check(struct lov_stripe_md *lsm,
483 cfs_page_t *page, obd_id id,
484 obd_off offset, obd_off count)
493 /* no partial pages on the client */
494 LASSERT(count == CFS_PAGE_SIZE);
496 addr = cfs_kmap(page);
498 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
499 stripe_off = offset + delta;
501 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
503 rc2 = block_debug_check("test_brw",
504 addr + delta, OBD_ECHO_BLOCK_SIZE,
505 stripe_off, stripe_id);
507 CERROR ("Error in echo object "LPX64"\n", id);
516 static int echo_client_kbrw(struct obd_device *obd, int rw, struct obdo *oa,
517 struct lov_stripe_md *lsm, obd_off offset,
518 obd_size count, struct obd_trans_info *oti)
520 struct echo_client_obd *ec = &obd->u.echo_client;
521 struct obd_info oinfo = { { { 0 } } };
523 struct ptlrpc_request_set *set = NULL;
524 struct brw_page *pga;
525 struct brw_page *pgp;
533 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
534 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
535 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
537 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
539 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
540 LASSERT(lsm != NULL);
541 LASSERT(lsm->lsm_object_id == oa->o_id);
544 (count & (~CFS_PAGE_MASK)) != 0)
547 if (rw == OBD_BRW_WRITE)
548 brw_flags = OBD_BRW_ASYNC;
550 set = ptlrpc_prep_set();
554 /* XXX think again with misaligned I/O */
555 npages = count >> CFS_PAGE_SHIFT;
557 OBD_ALLOC(pga, npages * sizeof(*pga));
561 for (i = 0, pgp = pga, off = offset;
563 i++, pgp++, off += CFS_PAGE_SIZE) {
565 LASSERT (pgp->pg == NULL); /* for cleanup */
568 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
572 pgp->count = CFS_PAGE_SIZE;
574 pgp->flag = brw_flags;
577 echo_client_page_debug_setup(lsm, pgp->pg, rw,
578 oa->o_id, off, pgp->count);
584 /* OST/filter device don't support o_brw_async ops, turn to o_brw ops */
585 if (ec->ec_exp && ec->ec_exp->exp_obd &&
586 OBT(ec->ec_exp->exp_obd) && OBP(ec->ec_exp->exp_obd, brw_async)) {
587 rc = obd_brw_async(rw, ec->ec_exp, &oinfo, npages, pga, oti,
590 rc = ptlrpc_set_wait(set);
592 CERROR("error from callback: rc = %d\n", rc);
595 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
598 CDEBUG_LIMIT(rc == -ENOSPC ? D_INODE : D_ERROR,
599 "error from obd_brw_async: rc = %d\n", rc);
600 ptlrpc_set_destroy(set);
602 if (rc != 0 || rw != OBD_BRW_READ)
605 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
611 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
612 pgp->off, pgp->count);
613 if (vrc != 0 && rc == 0)
616 OBD_PAGE_FREE(pgp->pg);
618 OBD_FREE(pga, npages * sizeof(*pga));
622 struct echo_async_state;
624 #define EAP_MAGIC 79277927
625 struct echo_async_page {
627 cfs_page_t *eap_page;
630 struct echo_async_state *eap_eas;
631 struct list_head eap_item;
634 #define EAP_FROM_COOKIE(c) \
635 (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC), \
636 (struct echo_async_page *)(c))
638 struct echo_async_state {
640 obd_off eas_next_offset;
641 obd_off eas_end_offset;
644 cfs_waitq_t eas_waitq;
645 struct list_head eas_avail;
647 struct lov_stripe_md *eas_lsm;
650 static int eas_should_wake(struct echo_async_state *eas)
654 spin_lock(&eas->eas_lock);
655 if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
657 spin_unlock(&eas->eas_lock);
661 static int ec_ap_make_ready(void *data, int cmd)
663 /* our pages are issued ready */
667 static int ec_ap_refresh_count(void *data, int cmd)
669 /* our pages are issued with a stable count */
671 return CFS_PAGE_SIZE;
673 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
675 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
677 lustre_set_wire_obdo(oa, &eap->eap_eas->eas_oa);
680 static int ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
682 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
683 struct echo_async_state *eas;
687 if (cmd == OBD_BRW_READ &&
688 eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
689 (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
690 (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
691 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
692 eas->eas_oa.o_id, eap->eap_off,
695 spin_lock(&eas->eas_lock);
696 if (rc && !eas->eas_rc)
698 eas->eas_in_flight--;
699 list_add(&eap->eap_item, &eas->eas_avail);
700 cfs_waitq_signal(&eas->eas_waitq);
701 spin_unlock(&eas->eas_lock);
705 static struct obd_async_page_ops ec_async_page_ops = {
706 .ap_make_ready = ec_ap_make_ready,
707 .ap_refresh_count = ec_ap_refresh_count,
708 .ap_fill_obdo = ec_ap_fill_obdo,
709 .ap_completion = ec_ap_completion,
712 static int echo_client_async_page(struct obd_export *exp, int rw,
713 struct obdo *oa, struct lov_stripe_md *lsm,
714 obd_off offset, obd_size count,
718 struct echo_async_page *eap;
719 struct echo_async_state *eas;
721 struct echo_async_page **aps = NULL;
729 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
730 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
731 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
733 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
736 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
739 (count & (~CFS_PAGE_MASK)) != 0 ||
741 lsm->lsm_object_id != oa->o_id))
744 /* XXX think again with misaligned I/O */
745 npages = batching >> CFS_PAGE_SHIFT;
751 if (rw == OBD_BRW_WRITE)
752 brw_flags = OBD_BRW_ASYNC;
754 memcpy(&eas->eas_oa, oa, sizeof(*oa));
755 eas->eas_next_offset = offset;
756 eas->eas_end_offset = offset + count;
757 spin_lock_init(&eas->eas_lock);
758 cfs_waitq_init(&eas->eas_waitq);
759 eas->eas_in_flight = 0;
762 CFS_INIT_LIST_HEAD(&eas->eas_avail);
764 OBD_ALLOC(aps, npages * sizeof aps[0]);
766 GOTO(free_eas, rc = -ENOMEM);
768 /* prepare the group of pages that we're going to be keeping
770 for (i = 0; i < npages; i++) {
772 OBD_PAGE_ALLOC(page, CFS_ALLOC_STD);
774 GOTO(out, rc = -ENOMEM);
776 OBD_ALLOC(eap, sizeof(*eap));
779 GOTO(out, rc = -ENOMEM);
782 eap->eap_magic = EAP_MAGIC;
783 eap->eap_page = page;
785 list_add_tail(&eap->eap_item, &eas->eas_avail);
789 /* first we spin queueing io and being woken by its completion */
790 spin_lock(&eas->eas_lock);
794 /* sleep until we have a page to send */
795 spin_unlock(&eas->eas_lock);
796 rc = wait_event_interruptible(eas->eas_waitq,
797 eas_should_wake(eas));
798 spin_lock(&eas->eas_lock);
799 if (rc && !eas->eas_rc)
803 if (list_empty(&eas->eas_avail))
805 eap = list_entry(eas->eas_avail.next, struct echo_async_page,
807 list_del(&eap->eap_item);
808 spin_unlock(&eas->eas_lock);
810 /* unbind the eap from its old page offset */
811 if (eap->eap_cookie != NULL) {
812 obd_teardown_async_page(exp, lsm, NULL,
814 eap->eap_cookie = NULL;
817 eas->eas_next_offset += CFS_PAGE_SIZE;
818 eap->eap_off = eas->eas_next_offset;
820 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
821 eap->eap_off, &ec_async_page_ops,
822 eap, &eap->eap_cookie,
823 OBD_PAGE_NO_CACHE, NULL);
825 spin_lock(&eas->eas_lock);
830 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
831 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
832 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
833 echo_client_page_debug_setup(lsm, eap->eap_page, rw,
835 eap->eap_off, CFS_PAGE_SIZE);
837 /* always asserts urgent, which isn't quite right */
838 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
839 rw, 0, CFS_PAGE_SIZE, brw_flags,
840 ASYNC_READY | ASYNC_URGENT |
842 spin_lock(&eas->eas_lock);
843 if (rc && !eas->eas_rc) {
847 eas->eas_in_flight++;
848 if (eas->eas_next_offset == eas->eas_end_offset)
852 /* still hold the eas_lock here.. */
854 /* now we just spin waiting for all the rpcs to complete */
855 while(eas->eas_in_flight) {
856 spin_unlock(&eas->eas_lock);
857 wait_event_interruptible(eas->eas_waitq,
858 eas->eas_in_flight == 0);
859 spin_lock(&eas->eas_lock);
861 spin_unlock(&eas->eas_lock);
865 for (i = 0; i < npages; ++ i) {
869 page = eap->eap_page;
870 if (eap->eap_cookie != NULL)
871 obd_teardown_async_page(exp, lsm, NULL,
873 OBD_FREE(eap, sizeof(*eap));
876 OBD_FREE(aps, npages * sizeof aps[0]);
884 static int echo_client_prep_commit(struct obd_export *exp, int rw,
885 struct obdo *oa, struct lov_stripe_md *lsm,
886 obd_off offset, obd_size count,
887 obd_size batch, struct obd_trans_info *oti)
889 struct obd_ioobj ioo;
890 struct niobuf_local *lnb;
891 struct niobuf_remote *rnb;
893 obd_size npages, tot_pages;
897 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
898 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
901 npages = batch >> CFS_PAGE_SHIFT;
902 tot_pages = count >> CFS_PAGE_SHIFT;
904 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
905 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
907 if (lnb == NULL || rnb == NULL)
908 GOTO(out, ret = -ENOMEM);
910 obdo_to_ioobj(oa, &ioo);
914 for(; tot_pages; tot_pages -= npages) {
917 if (tot_pages < npages)
920 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
922 rnb[i].len = CFS_PAGE_SIZE;
925 ioo.ioo_bufcnt = npages;
926 oti->oti_transno = 0;
929 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti);
932 LASSERT(lpages == npages);
934 for (i = 0; i < lpages; i++) {
935 cfs_page_t *page = lnb[i].page;
938 if (page == NULL && lnb[i].rc == 0)
941 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
942 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
943 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
946 if (rw == OBD_BRW_WRITE)
947 echo_client_page_debug_setup(lsm, page, rw,
952 echo_client_page_debug_check(lsm, page,
958 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
965 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
967 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
971 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
972 struct obd_ioctl_data *data)
974 struct obd_device *obd = class_exp2obd(exp);
975 struct echo_client_obd *ec = &obd->u.echo_client;
976 struct obd_trans_info dummy_oti = { .oti_thread = NULL };
977 struct ec_object *eco;
981 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
985 data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
986 data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
987 data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
989 switch((long)data->ioc_pbuf1) {
991 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
992 eco->eco_lsm, data->ioc_offset,
993 data->ioc_count, &dummy_oti);
996 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
997 eco->eco_lsm, data->ioc_offset,
998 data->ioc_count, data->ioc_plen1);
1001 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1002 eco->eco_lsm, data->ioc_offset,
1003 data->ioc_count, data->ioc_plen1,
1009 echo_put_object(eco);
1014 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1015 void *data, int flag)
1017 struct ec_object *eco = (struct ec_object *)data;
1018 struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1019 struct lustre_handle lockh;
1020 struct list_head *el;
1024 ldlm_lock2handle (lock, &lockh);
1026 /* #ifdef this out if we're not feeling paranoid */
1027 spin_lock (&ec->ec_lock);
1028 list_for_each (el, &ec->ec_objects) {
1029 found = (eco == list_entry(el, struct ec_object,
1034 spin_unlock (&ec->ec_lock);
1038 case LDLM_CB_BLOCKING:
1039 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1040 eco->eco_id, lockh.cookie);
1041 rc = ldlm_cli_cancel (&lockh);
1043 CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1046 case LDLM_CB_CANCELING:
1047 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1048 eco->eco_id, lockh.cookie);
1059 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1060 int mode, obd_off offset, obd_size nob)
1062 struct obd_device *obd = exp->exp_obd;
1063 struct echo_client_obd *ec = &obd->u.echo_client;
1064 struct lustre_handle *ulh = &oa->o_handle;
1065 struct ldlm_enqueue_info einfo = { 0 };
1066 struct obd_info oinfo = { { { 0 } } };
1067 struct ec_object *eco;
1068 struct ec_lock *ecl;
1071 if (!(mode == LCK_PR || mode == LCK_PW))
1074 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1075 (nob & (~CFS_PAGE_MASK)) != 0)
1078 rc = echo_get_object (&eco, obd, oa);
1083 OBD_ALLOC (ecl, sizeof (*ecl));
1087 ecl->ecl_mode = mode;
1088 ecl->ecl_object = eco;
1089 ecl->ecl_policy.l_extent.start = offset;
1090 ecl->ecl_policy.l_extent.end =
1091 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1093 einfo.ei_type = LDLM_EXTENT;
1094 einfo.ei_mode = mode;
1095 einfo.ei_cb_bl = echo_ldlm_callback;
1096 einfo.ei_cb_cp = ldlm_completion_ast;
1097 einfo.ei_cb_gl = NULL;
1098 einfo.ei_cbdata = eco;
1100 oinfo.oi_policy = ecl->ecl_policy;
1101 oinfo.oi_lockh = &ecl->ecl_lock_handle;
1102 oinfo.oi_md = eco->eco_lsm;
1103 rc = obd_enqueue(ec->ec_exp, &oinfo, &einfo, NULL);
1107 CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1109 /* NB ecl takes object ref from echo_get_object() above */
1110 spin_lock(&ec->ec_lock);
1112 list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1113 ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1115 spin_unlock(&ec->ec_lock);
1117 oa->o_valid |= OBD_MD_FLHANDLE;
1121 OBD_FREE (ecl, sizeof (*ecl));
1123 echo_put_object (eco);
1128 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1130 struct obd_device *obd = exp->exp_obd;
1131 struct echo_client_obd *ec = &obd->u.echo_client;
1132 struct lustre_handle *ulh = &oa->o_handle;
1133 struct ec_lock *ecl = NULL;
1135 struct list_head *el;
1138 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1141 spin_lock (&ec->ec_lock);
1143 list_for_each (el, &exp->exp_ec_data.eced_locks) {
1144 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1145 found = (ecl->ecl_cookie == ulh->cookie);
1147 list_del (&ecl->ecl_exp_chain);
1152 spin_unlock (&ec->ec_lock);
1157 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1158 &ecl->ecl_lock_handle, 0, 0);
1160 echo_put_object (ecl->ecl_object);
1161 OBD_FREE (ecl, sizeof (*ecl));
1167 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1168 int len, void *karg, void *uarg)
1170 struct obd_device *obd;
1171 struct echo_client_obd *ec;
1172 struct ec_object *eco;
1173 struct obd_ioctl_data *data = karg;
1174 struct obd_trans_info dummy_oti;
1175 struct oti_req_ack_lock *ack_lock;
1177 int rw = OBD_BRW_READ;
1184 memset(&dummy_oti, 0, sizeof(dummy_oti));
1187 ec = &obd->u.echo_client;
1190 case OBD_IOC_CREATE: /* may create echo object */
1191 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1192 GOTO (out, rc = -EPERM);
1194 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1195 data->ioc_pbuf1, data->ioc_plen1,
1199 case OBD_IOC_DESTROY:
1200 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1201 GOTO (out, rc = -EPERM);
1202 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1204 oa = &data->ioc_obdo1;
1205 oa->o_gr = FILTER_GROUP_ECHO;
1206 oa->o_valid |= OBD_MD_FLGROUP;
1207 rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1210 eco->eco_deleted = 1;
1211 echo_put_object(eco);
1215 case OBD_IOC_GETATTR:
1216 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1218 struct obd_info oinfo = { { { 0 } } };
1219 oinfo.oi_md = eco->eco_lsm;
1220 oinfo.oi_oa = &data->ioc_obdo1;
1221 rc = obd_getattr(ec->ec_exp, &oinfo);
1222 echo_put_object(eco);
1226 case OBD_IOC_SETATTR:
1227 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1228 GOTO (out, rc = -EPERM);
1230 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1232 struct obd_info oinfo = { { { 0 } } };
1233 oinfo.oi_oa = &data->ioc_obdo1;
1234 oinfo.oi_md = eco->eco_lsm;
1236 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1237 echo_put_object(eco);
1241 case OBD_IOC_BRW_WRITE:
1242 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1243 GOTO (out, rc = -EPERM);
1247 case OBD_IOC_BRW_READ:
1248 rc = echo_client_brw_ioctl(rw, exp, data);
1251 case ECHO_IOC_GET_STRIPE:
1252 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1254 rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1256 echo_put_object(eco);
1260 case ECHO_IOC_SET_STRIPE:
1261 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1262 GOTO (out, rc = -EPERM);
1264 if (data->ioc_pbuf1 == NULL) { /* unset */
1265 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1267 eco->eco_deleted = 1;
1268 echo_put_object(eco);
1271 rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1273 data->ioc_plen1, &dummy_oti);
1277 case ECHO_IOC_ENQUEUE:
1278 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1279 GOTO (out, rc = -EPERM);
1281 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1282 data->ioc_conn1, /* lock mode */
1283 data->ioc_offset, data->ioc_count);/*extent*/
1286 case ECHO_IOC_CANCEL:
1287 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1291 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1292 GOTO (out, rc = -ENOTTY);
1298 /* XXX this should be in a helper also called by target_send_reply */
1299 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1301 if (!ack_lock->mode)
1303 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1312 echo_client_setup(struct obd_device *obddev, obd_count len, void *buf)
1314 struct lustre_cfg* lcfg = buf;
1315 struct echo_client_obd *ec = &obddev->u.echo_client;
1316 struct obd_device *tgt;
1317 struct lustre_handle conn = {0, };
1318 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1319 struct obd_connect_data *ocd = NULL;
1323 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1324 CERROR("requires a TARGET OBD name\n");
1328 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1329 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1330 CERROR("device not attached or not set up (%s)\n",
1331 lustre_cfg_string(lcfg, 1));
1335 spin_lock_init (&ec->ec_lock);
1336 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1339 ec->ec_exp = lustre_hash_lookup(tgt->obd_uuid_hash, &echo_uuid);
1343 OBD_ALLOC(ocd, sizeof(*ocd));
1345 CERROR("Can't alloc ocd connecting to %s\n",
1346 lustre_cfg_string(lcfg, 1));
1349 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL;
1350 ocd->ocd_version = LUSTRE_VERSION_CODE;
1352 if ((strncmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME,
1353 strlen(LUSTRE_OSC_NAME)) == 0) ||
1354 (strncmp(tgt->obd_type->typ_name, LUSTRE_LOV_NAME,
1355 strlen(LUSTRE_LOV_NAME)) == 0)) {
1356 rc = obd_connect(&conn, tgt, &echo_uuid, ocd, &ec->ec_exp);
1358 rc = obd_connect(&conn, tgt, &echo_uuid, ocd, NULL);
1360 ec->ec_exp = class_conn2export(&conn);
1362 /* Turn off pinger because it connects to tgt obd directly */
1363 spin_lock(&tgt->obd_dev_lock);
1364 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1365 spin_unlock(&tgt->obd_dev_lock);
1369 OBD_FREE(ocd, sizeof(*ocd));
1371 if (rc == -EALREADY && (strncmp(tgt->obd_type->typ_name,LUSTRE_OSC_NAME,
1372 strlen(LUSTRE_OSC_NAME)) == 0)) {
1373 /* OSC obd forbid reconnect already connected import,
1374 * so we hack creating another export here */
1375 down_write(&tgt->u.cli.cl_sem);
1376 rc = class_connect(&conn, tgt, &echo_uuid);
1378 ++tgt->u.cli.cl_conn_count;
1379 ec->ec_exp = class_conn2export(&conn);
1381 up_write(&tgt->u.cli.cl_sem);
1385 CERROR("fail to connect to device %s\n",
1386 lustre_cfg_string(lcfg, 1));
1391 static int echo_client_cleanup(struct obd_device *obddev)
1393 struct list_head *el;
1394 struct ec_object *eco;
1395 struct echo_client_obd *ec = &obddev->u.echo_client;
1399 if (!list_empty(&obddev->obd_exports)) {
1400 CERROR("still has clients!\n");
1404 /* XXX assuming sole access */
1405 while (!list_empty(&ec->ec_objects)) {
1406 el = ec->ec_objects.next;
1407 eco = list_entry(el, struct ec_object, eco_obj_chain);
1409 LASSERT(eco->eco_refcount == 0);
1410 eco->eco_refcount = 1;
1411 eco->eco_deleted = 1;
1412 echo_put_object(eco);
1415 rc = obd_disconnect(ec->ec_exp);
1417 CERROR("fail to disconnect device: %d\n", rc);
1422 static int echo_client_connect(struct lustre_handle *conn,
1423 struct obd_device *src, struct obd_uuid *cluuid,
1424 struct obd_connect_data *data, void *localdata)
1426 struct obd_export *exp;
1430 rc = class_connect(conn, src, cluuid);
1432 exp = class_conn2export(conn);
1433 CFS_INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1434 class_export_put(exp);
1440 static int echo_client_disconnect(struct obd_export *exp)
1442 struct obd_device *obd;
1443 struct echo_client_obd *ec;
1444 struct ec_lock *ecl;
1449 GOTO(out, rc = -EINVAL);
1452 ec = &obd->u.echo_client;
1454 /* no more contention on export's lock list */
1455 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1456 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1457 struct ec_lock, ecl_exp_chain);
1458 list_del (&ecl->ecl_exp_chain);
1460 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1461 ecl->ecl_mode, &ecl->ecl_lock_handle, 0, 0);
1463 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1464 "(%d)\n", ecl->ecl_object->eco_id, rc);
1466 echo_put_object (ecl->ecl_object);
1467 OBD_FREE (ecl, sizeof (*ecl));
1470 rc = class_disconnect(exp);
1476 static struct obd_ops echo_obd_ops = {
1477 .o_owner = THIS_MODULE,
1478 .o_setup = echo_client_setup,
1479 .o_cleanup = echo_client_cleanup,
1480 .o_iocontrol = echo_client_iocontrol,
1481 .o_connect = echo_client_connect,
1482 .o_disconnect = echo_client_disconnect
1485 int echo_client_init(void)
1487 struct lprocfs_static_vars lvars = { 0 };
1489 lprocfs_echo_init_vars(&lvars);
1490 return class_register_type(&echo_obd_ops, lvars.module_vars,
1491 LUSTRE_ECHO_CLIENT_NAME);
1494 void echo_client_exit(void)
1496 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);