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 2008 Sun Microsystems, Inc. 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 CDEBUG(D_INFO, "%s: object %p: "LPX64", refs %d%s: "LPX64"=%u!%u\n",
61 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 CDEBUG(D_INFO, "@%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))
130 for (i = 0; i < lsm->lsm_stripe_count; i++) {
131 if (copy_from_user(lsm->lsm_oinfo[i],
132 ((struct lov_stripe_md *)ulsm)->lsm_oinfo[i],
133 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);
481 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
482 cfs_page_t *page, obd_id id,
483 obd_off offset, obd_off count)
492 /* no partial pages on the client */
493 LASSERT(count == CFS_PAGE_SIZE);
495 addr = cfs_kmap(page);
497 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
498 stripe_off = offset + delta;
500 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
502 rc2 = block_debug_check("test_brw",
503 addr + delta, OBD_ECHO_BLOCK_SIZE,
504 stripe_off, stripe_id);
506 CERROR ("Error in echo object "LPX64"\n", id);
515 static int echo_client_kbrw(struct obd_device *obd, int rw, struct obdo *oa,
516 struct lov_stripe_md *lsm, obd_off offset,
517 obd_size count, struct obd_trans_info *oti)
519 struct echo_client_obd *ec = &obd->u.echo_client;
520 struct obd_info oinfo = { { { 0 } } };
522 struct brw_page *pga;
523 struct brw_page *pgp;
530 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
531 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
532 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
534 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
536 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
537 LASSERT(lsm != NULL);
538 LASSERT(lsm->lsm_object_id == oa->o_id);
541 (count & (~CFS_PAGE_MASK)) != 0)
544 /* XXX think again with misaligned I/O */
545 npages = count >> CFS_PAGE_SHIFT;
547 OBD_ALLOC(pga, npages * sizeof(*pga));
551 for (i = 0, pgp = pga, off = offset;
553 i++, pgp++, off += CFS_PAGE_SIZE) {
555 LASSERT (pgp->pg == NULL); /* for cleanup */
558 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
562 pgp->count = CFS_PAGE_SIZE;
567 echo_client_page_debug_setup(lsm, pgp->pg, rw,
568 oa->o_id, off, pgp->count);
573 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
576 if (rc != 0 || rw != OBD_BRW_READ)
579 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
585 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
586 pgp->off, pgp->count);
587 if (vrc != 0 && rc == 0)
590 OBD_PAGE_FREE(pgp->pg);
592 OBD_FREE(pga, npages * sizeof(*pga));
596 struct echo_async_state;
598 #define EAP_MAGIC 79277927
599 struct echo_async_page {
601 cfs_page_t *eap_page;
604 struct echo_async_state *eap_eas;
605 struct list_head eap_item;
608 static inline struct echo_async_page *eap_from_cookie(void *ptr)
610 struct echo_async_page *ap = ptr;
611 LASSERT(ap->eap_magic == EAP_MAGIC);
615 struct echo_async_state {
617 obd_off eas_next_offset;
618 obd_off eas_end_offset;
621 cfs_waitq_t eas_waitq;
622 struct list_head eas_avail;
624 struct lov_stripe_md *eas_lsm;
627 static int eas_should_wake(struct echo_async_state *eas)
631 spin_lock(&eas->eas_lock);
632 if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
634 spin_unlock(&eas->eas_lock);
638 static int ec_ap_make_ready(void *data, int cmd)
640 /* our pages are issued ready */
644 static int ec_ap_refresh_count(void *data, int cmd)
646 /* our pages are issued with a stable count */
648 return CFS_PAGE_SIZE;
650 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
652 struct echo_async_page *eap = eap_from_cookie(data);
654 memcpy(oa, &eap->eap_eas->eas_oa, sizeof(*oa));
657 static int ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
659 struct echo_async_state *eas;
660 struct echo_async_page *eap = eap_from_cookie(data);
664 if (cmd == OBD_BRW_READ &&
665 eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
666 (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
667 (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
668 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
669 eas->eas_oa.o_id, eap->eap_off,
672 spin_lock(&eas->eas_lock);
673 if (rc && !eas->eas_rc)
675 eas->eas_in_flight--;
676 list_add(&eap->eap_item, &eas->eas_avail);
677 cfs_waitq_signal(&eas->eas_waitq);
678 spin_unlock(&eas->eas_lock);
682 static struct obd_async_page_ops ec_async_page_ops = {
683 .ap_make_ready = ec_ap_make_ready,
684 .ap_refresh_count = ec_ap_refresh_count,
685 .ap_fill_obdo = ec_ap_fill_obdo,
686 .ap_completion = ec_ap_completion,
689 static int echo_client_async_page(struct obd_export *exp, int rw,
690 struct obdo *oa, struct lov_stripe_md *lsm,
691 obd_off offset, obd_size count,
695 struct echo_async_page *eap;
696 struct echo_async_state eas;
698 struct echo_async_page **aps = NULL;
705 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
706 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
707 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
709 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
712 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
715 (count & (~CFS_PAGE_MASK)) != 0 ||
717 lsm->lsm_object_id != oa->o_id))
720 /* XXX think again with misaligned I/O */
721 npages = batching >> CFS_PAGE_SHIFT;
723 memcpy(&eas.eas_oa, oa, sizeof(*oa));
724 eas.eas_next_offset = offset;
725 eas.eas_end_offset = offset + count;
726 spin_lock_init(&eas.eas_lock);
727 cfs_waitq_init(&eas.eas_waitq);
728 eas.eas_in_flight = 0;
731 CFS_INIT_LIST_HEAD(&eas.eas_avail);
733 OBD_ALLOC(aps, npages * sizeof aps[0]);
737 /* prepare the group of pages that we're going to be keeping
739 for (i = 0; i < npages; i++) {
741 OBD_PAGE_ALLOC(page, CFS_ALLOC_STD);
743 GOTO(out, rc = -ENOMEM);
745 OBD_ALLOC(eap, sizeof(*eap));
748 GOTO(out, rc = -ENOMEM);
751 eap->eap_magic = EAP_MAGIC;
752 eap->eap_page = page;
754 list_add_tail(&eap->eap_item, &eas.eas_avail);
758 /* first we spin queueing io and being woken by its completion */
759 spin_lock(&eas.eas_lock);
763 /* sleep until we have a page to send */
764 spin_unlock(&eas.eas_lock);
765 rc = wait_event_interruptible(eas.eas_waitq,
766 eas_should_wake(&eas));
767 spin_lock(&eas.eas_lock);
768 if (rc && !eas.eas_rc)
772 if (list_empty(&eas.eas_avail))
774 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
776 list_del(&eap->eap_item);
777 spin_unlock(&eas.eas_lock);
779 /* unbind the eap from its old page offset */
780 if (eap->eap_cookie != NULL) {
781 obd_teardown_async_page(exp, lsm, NULL,
783 eap->eap_cookie = NULL;
786 eas.eas_next_offset += CFS_PAGE_SIZE;
787 eap->eap_off = eas.eas_next_offset;
789 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
790 eap->eap_off, &ec_async_page_ops,
791 eap, &eap->eap_cookie, 1, NULL);
793 spin_lock(&eas.eas_lock);
798 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
799 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
800 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
801 echo_client_page_debug_setup(lsm, eap->eap_page, rw,
803 eap->eap_off, CFS_PAGE_SIZE);
805 /* always asserts urgent, which isn't quite right */
806 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
807 rw, 0, CFS_PAGE_SIZE, 0,
808 ASYNC_READY | ASYNC_URGENT |
810 spin_lock(&eas.eas_lock);
811 if (rc && !eas.eas_rc) {
816 if (eas.eas_next_offset == eas.eas_end_offset)
820 /* still hold the eas_lock here.. */
822 /* now we just spin waiting for all the rpcs to complete */
823 while(eas.eas_in_flight) {
824 spin_unlock(&eas.eas_lock);
825 wait_event_interruptible(eas.eas_waitq,
826 eas.eas_in_flight == 0);
827 spin_lock(&eas.eas_lock);
829 spin_unlock(&eas.eas_lock);
833 for (i = 0; i < npages; ++ i) {
837 page = eap->eap_page;
838 if (eap->eap_cookie != NULL)
839 obd_teardown_async_page(exp, lsm, NULL,
841 OBD_FREE(eap, sizeof(*eap));
844 OBD_FREE(aps, npages * sizeof aps[0]);
850 static int echo_client_prep_commit(struct obd_export *exp, int rw,
851 struct obdo *oa, struct lov_stripe_md *lsm,
852 obd_off offset, obd_size count,
853 obd_size batch, struct obd_trans_info *oti)
855 struct obd_ioobj ioo;
856 struct niobuf_local *lnb;
857 struct niobuf_remote *rnb;
859 obd_size npages, tot_pages;
863 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
864 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
867 npages = batch >> CFS_PAGE_SHIFT;
868 tot_pages = count >> CFS_PAGE_SHIFT;
870 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
871 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
873 if (lnb == NULL || rnb == NULL)
874 GOTO(out, ret = -ENOMEM);
876 obdo_to_ioobj(oa, &ioo);
880 for(; tot_pages; tot_pages -= npages) {
881 if (tot_pages < npages)
884 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
886 rnb[i].len = CFS_PAGE_SIZE;
889 ioo.ioo_bufcnt = npages;
890 oti->oti_transno = 0;
892 ret = obd_preprw(rw, exp, oa, 1, &ioo, npages, rnb, lnb, oti,
897 for (i = 0; i < npages; i++) {
898 cfs_page_t *page = lnb[i].page;
901 if (page == NULL && lnb[i].rc == 0)
904 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
905 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
906 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
909 if (rw == OBD_BRW_WRITE)
910 echo_client_page_debug_setup(lsm, page, rw,
915 echo_client_page_debug_check(lsm, page,
921 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
928 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
930 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
934 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
935 struct obd_ioctl_data *data)
937 struct obd_device *obd = class_exp2obd(exp);
938 struct echo_client_obd *ec = &obd->u.echo_client;
939 struct obd_trans_info dummy_oti = { .oti_thread_id = -1 };
940 struct ec_object *eco;
944 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
948 data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
949 data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
950 data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
952 switch((long)data->ioc_pbuf1) {
954 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
955 eco->eco_lsm, data->ioc_offset,
956 data->ioc_count, &dummy_oti);
959 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
960 eco->eco_lsm, data->ioc_offset,
961 data->ioc_count, data->ioc_plen1);
964 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
965 eco->eco_lsm, data->ioc_offset,
966 data->ioc_count, data->ioc_plen1,
972 echo_put_object(eco);
977 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
978 void *data, int flag)
980 struct ec_object *eco = (struct ec_object *)data;
981 struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
982 struct lustre_handle lockh;
983 struct list_head *el;
987 ldlm_lock2handle (lock, &lockh);
989 /* #ifdef this out if we're not feeling paranoid */
990 spin_lock (&ec->ec_lock);
991 list_for_each (el, &ec->ec_objects) {
992 found = (eco == list_entry(el, struct ec_object,
997 spin_unlock (&ec->ec_lock);
1001 case LDLM_CB_BLOCKING:
1002 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1003 eco->eco_id, lockh.cookie);
1004 rc = ldlm_cli_cancel (&lockh);
1006 CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1009 case LDLM_CB_CANCELING:
1010 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1011 eco->eco_id, lockh.cookie);
1022 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1023 int mode, obd_off offset, obd_size nob)
1025 struct obd_device *obd = exp->exp_obd;
1026 struct echo_client_obd *ec = &obd->u.echo_client;
1027 struct lustre_handle *ulh = &oa->o_handle;
1028 struct ldlm_enqueue_info einfo = { 0 };
1029 struct obd_info oinfo = { { { 0 } } };
1030 struct ec_object *eco;
1031 struct ec_lock *ecl;
1034 if (!(mode == LCK_PR || mode == LCK_PW))
1037 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1038 (nob & (~CFS_PAGE_MASK)) != 0)
1041 rc = echo_get_object (&eco, obd, oa);
1046 OBD_ALLOC (ecl, sizeof (*ecl));
1050 ecl->ecl_mode = mode;
1051 ecl->ecl_object = eco;
1052 ecl->ecl_policy.l_extent.start = offset;
1053 ecl->ecl_policy.l_extent.end =
1054 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1056 einfo.ei_type = LDLM_EXTENT;
1057 einfo.ei_mode = mode;
1058 einfo.ei_cb_bl = echo_ldlm_callback;
1059 einfo.ei_cb_cp = ldlm_completion_ast;
1060 einfo.ei_cb_gl = NULL;
1061 einfo.ei_cbdata = eco;
1063 oinfo.oi_policy = ecl->ecl_policy;
1064 oinfo.oi_lockh = &ecl->ecl_lock_handle;
1065 oinfo.oi_md = eco->eco_lsm;
1066 rc = obd_enqueue(ec->ec_exp, &oinfo, &einfo, NULL);
1070 CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1072 /* NB ecl takes object ref from echo_get_object() above */
1073 spin_lock(&ec->ec_lock);
1075 list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1076 ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1078 spin_unlock(&ec->ec_lock);
1080 oa->o_valid |= OBD_MD_FLHANDLE;
1084 OBD_FREE (ecl, sizeof (*ecl));
1086 echo_put_object (eco);
1091 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1093 struct obd_device *obd = exp->exp_obd;
1094 struct echo_client_obd *ec = &obd->u.echo_client;
1095 struct lustre_handle *ulh = &oa->o_handle;
1096 struct ec_lock *ecl = NULL;
1098 struct list_head *el;
1101 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1104 spin_lock (&ec->ec_lock);
1106 list_for_each (el, &exp->exp_ec_data.eced_locks) {
1107 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1108 found = (ecl->ecl_cookie == ulh->cookie);
1110 list_del (&ecl->ecl_exp_chain);
1115 spin_unlock (&ec->ec_lock);
1120 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1121 &ecl->ecl_lock_handle);
1123 echo_put_object (ecl->ecl_object);
1124 OBD_FREE (ecl, sizeof (*ecl));
1130 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1131 int len, void *karg, void *uarg)
1133 struct obd_device *obd;
1134 struct echo_client_obd *ec;
1135 struct ec_object *eco;
1136 struct obd_ioctl_data *data = karg;
1137 struct obd_trans_info dummy_oti;
1138 struct oti_req_ack_lock *ack_lock;
1140 int rw = OBD_BRW_READ;
1147 memset(&dummy_oti, 0, sizeof(dummy_oti));
1150 ec = &obd->u.echo_client;
1153 case OBD_IOC_CREATE: /* may create echo object */
1154 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1155 GOTO (out, rc = -EPERM);
1157 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1158 data->ioc_pbuf1, data->ioc_plen1,
1162 case OBD_IOC_DESTROY:
1163 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1164 GOTO (out, rc = -EPERM);
1166 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1168 oa = &data->ioc_obdo1;
1169 oa->o_gr = FILTER_GROUP_ECHO;
1170 oa->o_valid |= OBD_MD_FLGROUP;
1171 rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1174 eco->eco_deleted = 1;
1175 echo_put_object(eco);
1179 case OBD_IOC_GETATTR:
1180 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1182 struct obd_info oinfo = { { { 0 } } };
1183 oinfo.oi_md = eco->eco_lsm;
1184 oinfo.oi_oa = &data->ioc_obdo1;
1185 rc = obd_getattr(ec->ec_exp, &oinfo);
1186 echo_put_object(eco);
1190 case OBD_IOC_SETATTR:
1191 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1192 GOTO (out, rc = -EPERM);
1194 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1196 struct obd_info oinfo = { { { 0 } } };
1197 oinfo.oi_oa = &data->ioc_obdo1;
1198 oinfo.oi_md = eco->eco_lsm;
1200 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1201 echo_put_object(eco);
1205 case OBD_IOC_BRW_WRITE:
1206 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1207 GOTO (out, rc = -EPERM);
1211 case OBD_IOC_BRW_READ:
1212 rc = echo_client_brw_ioctl(rw, exp, data);
1215 case ECHO_IOC_GET_STRIPE:
1216 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1218 rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1220 echo_put_object(eco);
1224 case ECHO_IOC_SET_STRIPE:
1225 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1226 GOTO (out, rc = -EPERM);
1228 if (data->ioc_pbuf1 == NULL) { /* unset */
1229 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1231 eco->eco_deleted = 1;
1232 echo_put_object(eco);
1235 rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1237 data->ioc_plen1, &dummy_oti);
1241 case ECHO_IOC_ENQUEUE:
1242 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1243 GOTO (out, rc = -EPERM);
1245 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1246 data->ioc_conn1, /* lock mode */
1247 data->ioc_offset, data->ioc_count);/*extent*/
1250 case ECHO_IOC_CANCEL:
1251 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1255 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1256 GOTO (out, rc = -ENOTTY);
1262 /* XXX this should be in a helper also called by target_send_reply */
1263 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1265 if (!ack_lock->mode)
1267 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1275 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1277 struct echo_client_obd *ec = &obddev->u.echo_client;
1278 struct obd_device *tgt;
1279 struct lustre_handle conn = {0, };
1280 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1281 struct obd_connect_data *ocd = NULL;
1285 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1286 CERROR("requires a TARGET OBD name\n");
1290 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1291 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1292 CERROR("device not attached or not set up (%s)\n",
1293 lustre_cfg_string(lcfg, 1));
1297 spin_lock_init (&ec->ec_lock);
1298 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1301 OBD_ALLOC(ocd, sizeof(*ocd));
1303 CERROR("Can't alloc ocd connecting to %s\n",
1304 lustre_cfg_string(lcfg, 1));
1308 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL;
1309 ocd->ocd_version = LUSTRE_VERSION_CODE;
1310 ocd->ocd_group = FILTER_GROUP_ECHO;
1312 rc = obd_connect(NULL, &conn, tgt, &echo_uuid, ocd, NULL);
1314 OBD_FREE(ocd, sizeof(*ocd));
1317 CERROR("fail to connect to device %s\n",
1318 lustre_cfg_string(lcfg, 1));
1321 ec->ec_exp = class_conn2export(&conn);
1326 static int echo_client_cleanup(struct obd_device *obddev)
1328 struct list_head *el;
1329 struct ec_object *eco;
1330 struct echo_client_obd *ec = &obddev->u.echo_client;
1334 if (!list_empty(&obddev->obd_exports)) {
1335 CERROR("still has clients!\n");
1339 /* XXX assuming sole access */
1340 while (!list_empty(&ec->ec_objects)) {
1341 el = ec->ec_objects.next;
1342 eco = list_entry(el, struct ec_object, eco_obj_chain);
1344 LASSERT(eco->eco_refcount == 0);
1345 eco->eco_refcount = 1;
1346 eco->eco_deleted = 1;
1347 echo_put_object(eco);
1350 rc = obd_disconnect(ec->ec_exp);
1352 CERROR("fail to disconnect device: %d\n", rc);
1357 static int echo_client_connect(const struct lu_env *env,
1358 struct lustre_handle *conn,
1359 struct obd_device *src, struct obd_uuid *cluuid,
1360 struct obd_connect_data *data, void *localdata)
1362 struct obd_export *exp;
1366 rc = class_connect(conn, src, cluuid);
1368 exp = class_conn2export(conn);
1369 CFS_INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1370 class_export_put(exp);
1376 static int echo_client_disconnect(struct obd_export *exp)
1378 struct obd_device *obd;
1379 struct echo_client_obd *ec;
1380 struct ec_lock *ecl;
1385 GOTO(out, rc = -EINVAL);
1388 ec = &obd->u.echo_client;
1390 /* no more contention on export's lock list */
1391 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1392 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1393 struct ec_lock, ecl_exp_chain);
1394 list_del (&ecl->ecl_exp_chain);
1396 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1397 ecl->ecl_mode, &ecl->ecl_lock_handle);
1399 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1400 "(%d)\n", ecl->ecl_object->eco_id, rc);
1402 echo_put_object (ecl->ecl_object);
1403 OBD_FREE (ecl, sizeof (*ecl));
1406 rc = class_disconnect(exp);
1412 static struct obd_ops echo_obd_ops = {
1413 .o_owner = THIS_MODULE,
1414 .o_setup = echo_client_setup,
1415 .o_cleanup = echo_client_cleanup,
1416 .o_iocontrol = echo_client_iocontrol,
1417 .o_connect = echo_client_connect,
1418 .o_disconnect = echo_client_disconnect
1421 int echo_client_init(void)
1423 struct lprocfs_static_vars lvars = { 0 };
1425 lprocfs_echo_init_vars(&lvars);
1426 return class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
1427 LUSTRE_ECHO_CLIENT_NAME, NULL);
1430 void echo_client_exit(void)
1432 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);