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));
563 for (i = 0, pgp = pga, off = offset;
565 i++, pgp++, off += CFS_PAGE_SIZE) {
567 LASSERT (pgp->pg == NULL); /* for cleanup */
570 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
574 pgp->count = CFS_PAGE_SIZE;
576 pgp->flag = brw_flags;
579 echo_client_page_debug_setup(lsm, pgp->pg, rw,
580 oa->o_id, off, pgp->count);
586 /* OST/filter device don't support o_brw_async ops, turn to o_brw ops */
587 if (ec->ec_exp && ec->ec_exp->exp_obd &&
588 OBT(ec->ec_exp->exp_obd) && OBP(ec->ec_exp->exp_obd, brw_async)) {
589 rc = obd_brw_async(rw, ec->ec_exp, &oinfo, npages, pga, oti,
592 rc = ptlrpc_set_wait(set);
594 CERROR("error from callback: rc = %d\n", rc);
597 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
600 CDEBUG_LIMIT(rc == -ENOSPC ? D_INODE : D_ERROR,
601 "error from obd_brw_async: rc = %d\n", rc);
603 if (rc != 0 || rw != OBD_BRW_READ)
606 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
612 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
613 pgp->off, pgp->count);
614 if (vrc != 0 && rc == 0)
617 OBD_PAGE_FREE(pgp->pg);
619 OBD_FREE(pga, npages * sizeof(*pga));
621 ptlrpc_set_destroy(set);
625 struct echo_async_state;
627 #define EAP_MAGIC 79277927
628 struct echo_async_page {
630 cfs_page_t *eap_page;
633 struct echo_async_state *eap_eas;
634 struct list_head eap_item;
637 #define EAP_FROM_COOKIE(c) \
638 (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC), \
639 (struct echo_async_page *)(c))
641 struct echo_async_state {
643 obd_off eas_next_offset;
644 obd_off eas_end_offset;
647 cfs_waitq_t eas_waitq;
648 struct list_head eas_avail;
650 struct lov_stripe_md *eas_lsm;
653 static int eas_should_wake(struct echo_async_state *eas)
657 spin_lock(&eas->eas_lock);
658 if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
660 spin_unlock(&eas->eas_lock);
664 static int ec_ap_make_ready(void *data, int cmd)
666 /* our pages are issued ready */
670 static int ec_ap_refresh_count(void *data, int cmd)
672 /* our pages are issued with a stable count */
674 return CFS_PAGE_SIZE;
676 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
678 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
680 lustre_set_wire_obdo(oa, &eap->eap_eas->eas_oa);
683 static int ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
685 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
686 struct echo_async_state *eas;
690 if (cmd == OBD_BRW_READ &&
691 eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
692 (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
693 (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
694 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
695 eas->eas_oa.o_id, eap->eap_off,
698 spin_lock(&eas->eas_lock);
699 if (rc && !eas->eas_rc)
701 eas->eas_in_flight--;
702 list_add(&eap->eap_item, &eas->eas_avail);
703 cfs_waitq_signal(&eas->eas_waitq);
704 spin_unlock(&eas->eas_lock);
708 static struct obd_async_page_ops ec_async_page_ops = {
709 .ap_make_ready = ec_ap_make_ready,
710 .ap_refresh_count = ec_ap_refresh_count,
711 .ap_fill_obdo = ec_ap_fill_obdo,
712 .ap_completion = ec_ap_completion,
715 static int echo_client_async_page(struct obd_export *exp, int rw,
716 struct obdo *oa, struct lov_stripe_md *lsm,
717 obd_off offset, obd_size count,
721 struct echo_async_page *eap;
722 struct echo_async_state *eas;
724 struct echo_async_page **aps = NULL;
732 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
733 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
734 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
736 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
739 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
742 (count & (~CFS_PAGE_MASK)) != 0 ||
744 lsm->lsm_object_id != oa->o_id))
747 /* XXX think again with misaligned I/O */
748 npages = batching >> CFS_PAGE_SHIFT;
754 if (rw == OBD_BRW_WRITE)
755 brw_flags = OBD_BRW_ASYNC;
757 memcpy(&eas->eas_oa, oa, sizeof(*oa));
758 eas->eas_next_offset = offset;
759 eas->eas_end_offset = offset + count;
760 spin_lock_init(&eas->eas_lock);
761 cfs_waitq_init(&eas->eas_waitq);
762 eas->eas_in_flight = 0;
765 CFS_INIT_LIST_HEAD(&eas->eas_avail);
767 OBD_ALLOC(aps, npages * sizeof aps[0]);
769 GOTO(free_eas, rc = -ENOMEM);
771 /* prepare the group of pages that we're going to be keeping
773 for (i = 0; i < npages; i++) {
775 OBD_PAGE_ALLOC(page, CFS_ALLOC_STD);
777 GOTO(out, rc = -ENOMEM);
779 OBD_ALLOC(eap, sizeof(*eap));
782 GOTO(out, rc = -ENOMEM);
785 eap->eap_magic = EAP_MAGIC;
786 eap->eap_page = page;
788 list_add_tail(&eap->eap_item, &eas->eas_avail);
792 /* first we spin queueing io and being woken by its completion */
793 spin_lock(&eas->eas_lock);
797 /* sleep until we have a page to send */
798 spin_unlock(&eas->eas_lock);
799 rc = wait_event_interruptible(eas->eas_waitq,
800 eas_should_wake(eas));
801 spin_lock(&eas->eas_lock);
802 if (rc && !eas->eas_rc)
806 if (list_empty(&eas->eas_avail))
808 eap = list_entry(eas->eas_avail.next, struct echo_async_page,
810 list_del(&eap->eap_item);
811 spin_unlock(&eas->eas_lock);
813 /* unbind the eap from its old page offset */
814 if (eap->eap_cookie != NULL) {
815 obd_teardown_async_page(exp, lsm, NULL,
817 eap->eap_cookie = NULL;
820 eas->eas_next_offset += CFS_PAGE_SIZE;
821 eap->eap_off = eas->eas_next_offset;
823 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
824 eap->eap_off, &ec_async_page_ops,
825 eap, &eap->eap_cookie,
826 OBD_PAGE_NO_CACHE, NULL);
828 spin_lock(&eas->eas_lock);
833 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
834 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
835 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
836 echo_client_page_debug_setup(lsm, eap->eap_page, rw,
838 eap->eap_off, CFS_PAGE_SIZE);
840 /* always asserts urgent, which isn't quite right */
841 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
842 rw, 0, CFS_PAGE_SIZE, brw_flags,
843 ASYNC_READY | ASYNC_URGENT |
845 spin_lock(&eas->eas_lock);
846 if (rc && !eas->eas_rc) {
850 eas->eas_in_flight++;
851 if (eas->eas_next_offset == eas->eas_end_offset)
855 /* still hold the eas_lock here.. */
857 /* now we just spin waiting for all the rpcs to complete */
858 while(eas->eas_in_flight) {
859 spin_unlock(&eas->eas_lock);
860 wait_event_interruptible(eas->eas_waitq,
861 eas->eas_in_flight == 0);
862 spin_lock(&eas->eas_lock);
864 spin_unlock(&eas->eas_lock);
868 for (i = 0; i < npages; ++ i) {
873 page = eap->eap_page;
874 if (eap->eap_cookie != NULL)
875 obd_teardown_async_page(exp, lsm, NULL,
877 OBD_FREE(eap, sizeof(*eap));
881 OBD_FREE(aps, npages * sizeof aps[0]);
889 static int echo_client_prep_commit(struct obd_export *exp, int rw,
890 struct obdo *oa, struct lov_stripe_md *lsm,
891 obd_off offset, obd_size count,
892 obd_size batch, struct obd_trans_info *oti)
894 struct obd_ioobj ioo;
895 struct niobuf_local *lnb;
896 struct niobuf_remote *rnb;
898 obd_size npages, tot_pages;
902 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
903 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
906 npages = batch >> CFS_PAGE_SHIFT;
907 tot_pages = count >> CFS_PAGE_SHIFT;
909 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
910 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
912 if (lnb == NULL || rnb == NULL)
913 GOTO(out, ret = -ENOMEM);
915 obdo_to_ioobj(oa, &ioo);
919 for(; tot_pages; tot_pages -= npages) {
922 if (tot_pages < npages)
925 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
927 rnb[i].len = CFS_PAGE_SIZE;
930 ioo.ioo_bufcnt = npages;
931 oti->oti_transno = 0;
934 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti);
937 LASSERT(lpages == npages);
939 for (i = 0; i < lpages; i++) {
940 cfs_page_t *page = lnb[i].page;
943 if (page == NULL && lnb[i].rc == 0)
946 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
947 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
948 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
951 if (rw == OBD_BRW_WRITE)
952 echo_client_page_debug_setup(lsm, page, rw,
957 echo_client_page_debug_check(lsm, page,
963 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
970 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
972 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
976 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
977 struct obd_ioctl_data *data)
979 struct obd_device *obd = class_exp2obd(exp);
980 struct echo_client_obd *ec = &obd->u.echo_client;
981 struct obd_trans_info dummy_oti = { .oti_thread = NULL };
982 struct ec_object *eco;
986 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
990 data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
991 data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
992 data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
994 switch((long)data->ioc_pbuf1) {
996 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
997 eco->eco_lsm, data->ioc_offset,
998 data->ioc_count, &dummy_oti);
1001 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
1002 eco->eco_lsm, data->ioc_offset,
1003 data->ioc_count, data->ioc_plen1);
1006 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1007 eco->eco_lsm, data->ioc_offset,
1008 data->ioc_count, data->ioc_plen1,
1014 echo_put_object(eco);
1019 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1020 void *data, int flag)
1022 struct ec_object *eco = (struct ec_object *)data;
1023 struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1024 struct lustre_handle lockh;
1025 struct list_head *el;
1029 ldlm_lock2handle (lock, &lockh);
1031 /* #ifdef this out if we're not feeling paranoid */
1032 spin_lock (&ec->ec_lock);
1033 list_for_each (el, &ec->ec_objects) {
1034 found = (eco == list_entry(el, struct ec_object,
1039 spin_unlock (&ec->ec_lock);
1043 case LDLM_CB_BLOCKING:
1044 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1045 eco->eco_id, lockh.cookie);
1046 rc = ldlm_cli_cancel (&lockh);
1048 CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1051 case LDLM_CB_CANCELING:
1052 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1053 eco->eco_id, lockh.cookie);
1064 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1065 int mode, obd_off offset, obd_size nob)
1067 struct obd_device *obd = exp->exp_obd;
1068 struct echo_client_obd *ec = &obd->u.echo_client;
1069 struct lustre_handle *ulh = &oa->o_handle;
1070 struct ldlm_enqueue_info einfo = { 0 };
1071 struct obd_info oinfo = { { { 0 } } };
1072 struct ec_object *eco;
1073 struct ec_lock *ecl;
1076 if (!(mode == LCK_PR || mode == LCK_PW))
1079 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1080 (nob & (~CFS_PAGE_MASK)) != 0)
1083 rc = echo_get_object (&eco, obd, oa);
1088 OBD_ALLOC (ecl, sizeof (*ecl));
1092 ecl->ecl_mode = mode;
1093 ecl->ecl_object = eco;
1094 ecl->ecl_policy.l_extent.start = offset;
1095 ecl->ecl_policy.l_extent.end =
1096 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1098 einfo.ei_type = LDLM_EXTENT;
1099 einfo.ei_mode = mode;
1100 einfo.ei_cb_bl = echo_ldlm_callback;
1101 einfo.ei_cb_cp = ldlm_completion_ast;
1102 einfo.ei_cb_gl = NULL;
1103 einfo.ei_cbdata = eco;
1105 oinfo.oi_policy = ecl->ecl_policy;
1106 oinfo.oi_lockh = &ecl->ecl_lock_handle;
1107 oinfo.oi_md = eco->eco_lsm;
1108 rc = obd_enqueue(ec->ec_exp, &oinfo, &einfo, NULL);
1112 CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1114 /* NB ecl takes object ref from echo_get_object() above */
1115 spin_lock(&ec->ec_lock);
1117 list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1118 ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1120 spin_unlock(&ec->ec_lock);
1122 oa->o_valid |= OBD_MD_FLHANDLE;
1126 OBD_FREE (ecl, sizeof (*ecl));
1128 echo_put_object (eco);
1133 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1135 struct obd_device *obd = exp->exp_obd;
1136 struct echo_client_obd *ec = &obd->u.echo_client;
1137 struct lustre_handle *ulh = &oa->o_handle;
1138 struct ec_lock *ecl = NULL;
1140 struct list_head *el;
1143 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1146 spin_lock (&ec->ec_lock);
1148 list_for_each (el, &exp->exp_ec_data.eced_locks) {
1149 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1150 found = (ecl->ecl_cookie == ulh->cookie);
1152 list_del (&ecl->ecl_exp_chain);
1157 spin_unlock (&ec->ec_lock);
1162 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1163 &ecl->ecl_lock_handle, 0, 0);
1165 echo_put_object (ecl->ecl_object);
1166 OBD_FREE (ecl, sizeof (*ecl));
1172 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1173 int len, void *karg, void *uarg)
1175 struct obd_device *obd;
1176 struct echo_client_obd *ec;
1177 struct ec_object *eco;
1178 struct obd_ioctl_data *data = karg;
1179 struct obd_trans_info dummy_oti;
1180 struct oti_req_ack_lock *ack_lock;
1182 int rw = OBD_BRW_READ;
1187 #ifndef HAVE_UNLOCKED_IOCTL
1190 memset(&dummy_oti, 0, sizeof(dummy_oti));
1193 ec = &obd->u.echo_client;
1196 case OBD_IOC_CREATE: /* may create echo object */
1197 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1198 GOTO (out, rc = -EPERM);
1200 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1201 data->ioc_pbuf1, data->ioc_plen1,
1205 case OBD_IOC_DESTROY:
1206 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1207 GOTO (out, rc = -EPERM);
1208 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1210 oa = &data->ioc_obdo1;
1211 oa->o_gr = FILTER_GROUP_ECHO;
1212 oa->o_valid |= OBD_MD_FLGROUP;
1213 rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1216 eco->eco_deleted = 1;
1217 echo_put_object(eco);
1221 case OBD_IOC_GETATTR:
1222 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1224 struct obd_info oinfo = { { { 0 } } };
1225 oinfo.oi_md = eco->eco_lsm;
1226 oinfo.oi_oa = &data->ioc_obdo1;
1227 rc = obd_getattr(ec->ec_exp, &oinfo);
1228 echo_put_object(eco);
1232 case OBD_IOC_SETATTR:
1233 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1234 GOTO (out, rc = -EPERM);
1236 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1238 struct obd_info oinfo = { { { 0 } } };
1239 oinfo.oi_oa = &data->ioc_obdo1;
1240 oinfo.oi_md = eco->eco_lsm;
1242 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1243 echo_put_object(eco);
1247 case OBD_IOC_BRW_WRITE:
1248 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1249 GOTO (out, rc = -EPERM);
1253 case OBD_IOC_BRW_READ:
1254 rc = echo_client_brw_ioctl(rw, exp, data);
1257 case ECHO_IOC_GET_STRIPE:
1258 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1260 rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1262 echo_put_object(eco);
1266 case ECHO_IOC_SET_STRIPE:
1267 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1268 GOTO (out, rc = -EPERM);
1270 if (data->ioc_pbuf1 == NULL) { /* unset */
1271 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1273 eco->eco_deleted = 1;
1274 echo_put_object(eco);
1277 rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1279 data->ioc_plen1, &dummy_oti);
1283 case ECHO_IOC_ENQUEUE:
1284 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1285 GOTO (out, rc = -EPERM);
1287 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1288 data->ioc_conn1, /* lock mode */
1289 data->ioc_offset, data->ioc_count);/*extent*/
1292 case ECHO_IOC_CANCEL:
1293 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1297 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1298 GOTO (out, rc = -ENOTTY);
1304 /* XXX this should be in a helper also called by target_send_reply */
1305 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1307 if (!ack_lock->mode)
1309 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1312 #ifndef HAVE_UNLOCKED_IOCTL
1319 echo_client_setup(struct obd_device *obddev, obd_count len, void *buf)
1321 struct lustre_cfg* lcfg = buf;
1322 struct echo_client_obd *ec = &obddev->u.echo_client;
1323 struct obd_device *tgt;
1324 struct lustre_handle conn = {0, };
1325 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1326 struct obd_connect_data *ocd = NULL;
1330 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1331 CERROR("requires a TARGET OBD name\n");
1335 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1336 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1337 CERROR("device not attached or not set up (%s)\n",
1338 lustre_cfg_string(lcfg, 1));
1342 spin_lock_init (&ec->ec_lock);
1343 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1346 ec->ec_exp = lustre_hash_lookup(tgt->obd_uuid_hash, &echo_uuid);
1350 OBD_ALLOC(ocd, sizeof(*ocd));
1352 CERROR("Can't alloc ocd connecting to %s\n",
1353 lustre_cfg_string(lcfg, 1));
1356 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL;
1357 ocd->ocd_version = LUSTRE_VERSION_CODE;
1359 if ((strncmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME,
1360 strlen(LUSTRE_OSC_NAME)) == 0) ||
1361 (strncmp(tgt->obd_type->typ_name, LUSTRE_LOV_NAME,
1362 strlen(LUSTRE_LOV_NAME)) == 0)) {
1363 rc = obd_connect(&conn, tgt, &echo_uuid, ocd, &ec->ec_exp);
1365 rc = obd_connect(&conn, tgt, &echo_uuid, ocd, NULL);
1367 ec->ec_exp = class_conn2export(&conn);
1369 /* Turn off pinger because it connects to tgt obd directly */
1370 spin_lock(&tgt->obd_dev_lock);
1371 list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1372 spin_unlock(&tgt->obd_dev_lock);
1376 OBD_FREE(ocd, sizeof(*ocd));
1378 if (rc == -EALREADY && (strncmp(tgt->obd_type->typ_name,LUSTRE_OSC_NAME,
1379 strlen(LUSTRE_OSC_NAME)) == 0)) {
1380 /* OSC obd forbid reconnect already connected import,
1381 * so we hack creating another export here */
1382 down_write(&tgt->u.cli.cl_sem);
1383 rc = class_connect(&conn, tgt, &echo_uuid);
1385 ++tgt->u.cli.cl_conn_count;
1386 ec->ec_exp = class_conn2export(&conn);
1388 up_write(&tgt->u.cli.cl_sem);
1392 CERROR("fail to connect to device %s\n",
1393 lustre_cfg_string(lcfg, 1));
1398 static int echo_client_cleanup(struct obd_device *obddev)
1400 struct list_head *el;
1401 struct ec_object *eco;
1402 struct echo_client_obd *ec = &obddev->u.echo_client;
1406 if (!list_empty(&obddev->obd_exports)) {
1407 CERROR("still has clients!\n");
1411 /* XXX assuming sole access */
1412 while (!list_empty(&ec->ec_objects)) {
1413 el = ec->ec_objects.next;
1414 eco = list_entry(el, struct ec_object, eco_obj_chain);
1416 if (eco->eco_refcount > 0)
1418 eco->eco_refcount = 1;
1419 eco->eco_deleted = 1;
1420 echo_put_object(eco);
1423 rc = obd_disconnect(ec->ec_exp);
1425 CERROR("fail to disconnect device: %d\n", rc);
1430 static int echo_client_connect(struct lustre_handle *conn,
1431 struct obd_device *src, struct obd_uuid *cluuid,
1432 struct obd_connect_data *data, void *localdata)
1434 struct obd_export *exp;
1438 rc = class_connect(conn, src, cluuid);
1440 exp = class_conn2export(conn);
1441 CFS_INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1442 class_export_put(exp);
1448 static int echo_client_disconnect(struct obd_export *exp)
1450 struct obd_device *obd;
1451 struct echo_client_obd *ec;
1452 struct ec_lock *ecl;
1457 GOTO(out, rc = -EINVAL);
1460 ec = &obd->u.echo_client;
1462 /* no more contention on export's lock list */
1463 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1464 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1465 struct ec_lock, ecl_exp_chain);
1466 list_del (&ecl->ecl_exp_chain);
1468 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1469 ecl->ecl_mode, &ecl->ecl_lock_handle, 0, 0);
1471 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1472 "(%d)\n", ecl->ecl_object->eco_id, rc);
1474 echo_put_object (ecl->ecl_object);
1475 OBD_FREE (ecl, sizeof (*ecl));
1478 rc = class_disconnect(exp);
1484 static struct obd_ops echo_obd_ops = {
1485 .o_owner = THIS_MODULE,
1486 .o_setup = echo_client_setup,
1487 .o_cleanup = echo_client_cleanup,
1488 .o_iocontrol = echo_client_iocontrol,
1489 .o_connect = echo_client_connect,
1490 .o_disconnect = echo_client_disconnect
1493 int echo_client_init(void)
1495 struct lprocfs_static_vars lvars = { 0 };
1497 lprocfs_echo_init_vars(&lvars);
1498 return class_register_type(&echo_obd_ops, lvars.module_vars,
1499 LUSTRE_ECHO_CLIENT_NAME);
1502 void echo_client_exit(void)
1504 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);