1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2001-2003 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
25 #define DEBUG_SUBSYSTEM S_ECHO
27 #include <libcfs/libcfs.h>
29 #include <liblustre.h>
33 #include <obd_support.h>
34 #include <obd_class.h>
36 #include <lustre_debug.h>
37 #include <lprocfs_status.h>
39 static obd_id last_object_id;
43 echo_printk_object (char *msg, struct ec_object *eco)
45 struct lov_stripe_md *lsm = eco->eco_lsm;
48 printk (KERN_INFO "Lustre: %s: object %p: "LPX64", refs %d%s: "LPX64
49 "=%u!%u\n", msg, eco, eco->eco_id, eco->eco_refcount,
50 eco->eco_deleted ? "(deleted) " : "",
51 lsm->lsm_object_id, lsm->lsm_stripe_size,
52 lsm->lsm_stripe_count);
54 for (i = 0; i < lsm->lsm_stripe_count; i++)
55 printk (KERN_INFO "Lustre: @%2u:"LPX64"\n",
56 lsm->lsm_oinfo[i].loi_ost_idx,
57 lsm->lsm_oinfo[i].loi_id);
61 static struct ec_object *
62 echo_find_object_locked (struct obd_device *obd, obd_id id)
64 struct echo_client_obd *ec = &obd->u.echo_client;
65 struct ec_object *eco = NULL;
68 list_for_each (el, &ec->ec_objects) {
69 eco = list_entry (el, struct ec_object, eco_obj_chain);
71 if (eco->eco_id == id)
78 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
80 struct lov_stripe_md *ulsm = _ulsm;
83 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
87 if (copy_to_user (ulsm, lsm, sizeof(ulsm)))
90 for (i = 0; i < lsm->lsm_stripe_count; i++) {
91 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
92 sizeof(lsm->lsm_oinfo[0])))
99 echo_copyin_lsm (struct obd_device *obd, struct lov_stripe_md *lsm,
100 void *ulsm, int ulsm_nob)
102 struct echo_client_obd *ec = &obd->u.echo_client;
105 if (ulsm_nob < sizeof (*lsm))
108 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
111 if (lsm->lsm_stripe_count > ec->ec_nstripes ||
112 lsm->lsm_magic != LOV_MAGIC ||
113 (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
114 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
118 for (i = 0; i < lsm->lsm_stripe_count; i++) {
119 if (copy_from_user(lsm->lsm_oinfo[i],
120 ((struct lov_stripe_md *)ulsm)->lsm_oinfo[i],
121 sizeof(lsm->lsm_oinfo[0])))
127 static struct ec_object *
128 echo_allocate_object (struct obd_device *obd)
130 struct echo_client_obd *ec = &obd->u.echo_client;
131 struct ec_object *eco;
134 OBD_ALLOC(eco, sizeof (*eco));
138 rc = obd_alloc_memmd(ec->ec_exp, &eco->eco_lsm);
140 OBD_FREE(eco, sizeof (*eco));
144 eco->eco_device = obd;
145 eco->eco_deleted = 0;
146 eco->eco_refcount = 0;
147 eco->eco_lsm->lsm_magic = LOV_MAGIC;
148 /* leave stripe count 0 by default */
154 echo_free_object (struct ec_object *eco)
156 struct obd_device *obd = eco->eco_device;
157 struct echo_client_obd *ec = &obd->u.echo_client;
159 LASSERT (eco->eco_refcount == 0);
161 CERROR("No object %s\n", obd->obd_name);
163 obd_free_memmd(ec->ec_exp, &eco->eco_lsm);
164 OBD_FREE (eco, sizeof (*eco));
167 static int echo_create_object(struct obd_device *obd, int on_target,
168 struct obdo *oa, void *ulsm, int ulsm_nob,
169 struct obd_trans_info *oti)
171 struct echo_client_obd *ec = &obd->u.echo_client;
172 struct ec_object *eco2;
173 struct ec_object *eco;
174 struct lov_stripe_md *lsm;
178 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
179 (on_target || /* set_stripe */
180 ec->ec_nstripes != 0)) { /* LOV */
181 CERROR ("No valid oid\n");
186 eco = echo_allocate_object (obd);
192 rc = echo_copyin_lsm (obd, lsm, ulsm, ulsm_nob);
196 /* setup object ID here for !on_target and LOV hint */
197 if ((oa->o_valid & OBD_MD_FLID) != 0)
198 eco->eco_id = lsm->lsm_object_id = oa->o_id;
200 if (lsm->lsm_stripe_count == 0)
201 lsm->lsm_stripe_count = ec->ec_nstripes;
203 if (lsm->lsm_stripe_size == 0)
204 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
208 /* setup stripes: indices + default ids if required */
209 for (i = 0; i < lsm->lsm_stripe_count; i++) {
210 if (lsm->lsm_oinfo[i]->loi_id == 0)
211 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
213 lsm->lsm_oinfo[i]->loi_ost_idx =
214 (idx + i) % ec->ec_nstripes;
217 OBD_ALLOC(eco, sizeof(*eco));
220 eco->eco_device = obd;
225 oa->o_id = ++last_object_id;
228 oa->o_gr = FILTER_GROUP_ECHO;
229 oa->o_valid |= OBD_MD_FLGROUP;
231 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
235 /* See what object ID we were given */
236 eco->eco_id = oa->o_id = lsm->lsm_object_id;
237 oa->o_valid |= OBD_MD_FLID;
239 LASSERT(eco->eco_lsm == NULL || eco->eco_lsm == lsm);
243 spin_lock (&ec->ec_lock);
245 eco2 = echo_find_object_locked (obd, oa->o_id);
246 if (eco2 != NULL) { /* conflict */
247 spin_unlock (&ec->ec_lock);
249 CERROR ("Can't create object id "LPX64": id already exists%s\n",
250 oa->o_id, on_target ? " (undoing create)" : "");
253 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL);
259 list_add (&eco->eco_obj_chain, &ec->ec_objects);
260 spin_unlock (&ec->ec_lock);
262 "created %p: "LPX64"=%u#%u@%u refs %d del %d\n",
264 eco->eco_lsm->lsm_stripe_size,
265 eco->eco_lsm->lsm_stripe_count,
266 eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
267 eco->eco_refcount, eco->eco_deleted);
271 echo_free_object (eco);
273 CERROR("%s: err %d on create\n", obd->obd_name, rc);
278 echo_get_object (struct ec_object **ecop, struct obd_device *obd,
281 struct echo_client_obd *ec = &obd->u.echo_client;
282 struct ec_object *eco;
283 struct ec_object *eco2;
286 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
287 oa->o_id == 0) /* disallow use of object id 0 */
289 CERROR ("No valid oid\n");
293 spin_lock (&ec->ec_lock);
294 eco = echo_find_object_locked (obd, oa->o_id);
296 if (eco->eco_deleted) { /* being deleted */
297 spin_unlock(&ec->ec_lock);/* (see comment in cleanup) */
302 spin_unlock (&ec->ec_lock);
305 "found %p: "LPX64"=%u#%u@%u refs %d del %d\n",
307 eco->eco_lsm->lsm_stripe_size,
308 eco->eco_lsm->lsm_stripe_count,
309 eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
310 eco->eco_refcount, eco->eco_deleted);
313 spin_unlock (&ec->ec_lock);
315 if (ec->ec_nstripes != 0) /* striping required */
318 eco = echo_allocate_object (obd);
322 eco->eco_id = eco->eco_lsm->lsm_object_id = oa->o_id;
324 spin_lock (&ec->ec_lock);
326 eco2 = echo_find_object_locked (obd, oa->o_id);
327 if (eco2 == NULL) { /* didn't race */
328 list_add (&eco->eco_obj_chain, &ec->ec_objects);
329 spin_unlock (&ec->ec_lock);
330 eco->eco_refcount = 1;
333 "created %p: "LPX64"=%u#%u@%d refs %d del %d\n",
335 eco->eco_lsm->lsm_stripe_size,
336 eco->eco_lsm->lsm_stripe_count,
337 eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
338 eco->eco_refcount, eco->eco_deleted);
342 if (eco2->eco_deleted)
343 rc = -EAGAIN; /* lose race */
345 eco2->eco_refcount++; /* take existing */
348 LASSERT (eco2->eco_id == eco2->eco_lsm->lsm_object_id);
350 "found(2) %p: "LPX64"=%u#%u@%d refs %d del %d\n",
352 eco2->eco_lsm->lsm_stripe_size,
353 eco2->eco_lsm->lsm_stripe_count,
354 eco2->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
355 eco2->eco_refcount, eco2->eco_deleted);
358 spin_unlock (&ec->ec_lock);
360 echo_free_object (eco);
365 echo_put_object (struct ec_object *eco)
367 struct obd_device *obd = eco->eco_device;
368 struct echo_client_obd *ec = &obd->u.echo_client;
370 /* Release caller's ref on the object.
371 * delete => mark for deletion when last ref goes
374 spin_lock (&ec->ec_lock);
377 LASSERT (eco->eco_refcount >= 0);
379 CDEBUG(D_INFO, "put %p: "LPX64"=%u#%u@%d refs %d del %d\n",
381 eco->eco_lsm->lsm_stripe_size,
382 eco->eco_lsm->lsm_stripe_count,
383 eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
384 eco->eco_refcount, eco->eco_deleted);
386 if (eco->eco_refcount != 0 || !eco->eco_deleted) {
387 spin_unlock (&ec->ec_lock);
391 spin_unlock (&ec->ec_lock);
393 /* NB leave obj in the object list. We must prevent anyone from
394 * attempting to enqueue on this object number until we can be
395 * sure there will be no more lock callbacks.
397 obd_cancel_unused(ec->ec_exp, eco->eco_lsm, 0, NULL);
399 /* now we can let it go */
400 spin_lock (&ec->ec_lock);
401 list_del (&eco->eco_obj_chain);
402 spin_unlock (&ec->ec_lock);
404 LASSERT (eco->eco_refcount == 0);
406 echo_free_object (eco);
410 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
412 unsigned long stripe_count;
413 unsigned long stripe_size;
415 unsigned long woffset;
419 if (lsm->lsm_stripe_count <= 1)
423 stripe_size = lsm->lsm_stripe_size;
424 stripe_count = lsm->lsm_stripe_count;
426 /* width = # bytes in all stripes */
427 width = stripe_size * stripe_count;
429 /* woffset = offset within a width; offset = whole number of widths */
430 woffset = do_div (offset, width);
432 stripe_index = woffset / stripe_size;
434 *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
435 *offp = offset * stripe_size + woffset % stripe_size;
439 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
440 cfs_page_t *page, int rw, obd_id id,
441 obd_off offset, obd_off count)
448 /* no partial pages on the client */
449 LASSERT(count == CFS_PAGE_SIZE);
451 addr = cfs_kmap(page);
453 for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
454 if (rw == OBD_BRW_WRITE) {
455 stripe_off = offset + delta;
457 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
459 stripe_off = 0xdeadbeef00c0ffeeULL;
460 stripe_id = 0xdeadbeef00c0ffeeULL;
462 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
463 stripe_off, stripe_id);
469 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
470 cfs_page_t *page, obd_id id,
471 obd_off offset, obd_off count)
480 /* no partial pages on the client */
481 LASSERT(count == CFS_PAGE_SIZE);
483 addr = cfs_kmap(page);
485 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
486 stripe_off = offset + delta;
488 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
490 rc2 = block_debug_check("test_brw",
491 addr + delta, OBD_ECHO_BLOCK_SIZE,
492 stripe_off, stripe_id);
494 CERROR ("Error in echo object "LPX64"\n", id);
503 static int echo_client_kbrw(struct obd_device *obd, int rw, struct obdo *oa,
504 struct lov_stripe_md *lsm, obd_off offset,
505 obd_size count, struct obd_trans_info *oti)
507 struct echo_client_obd *ec = &obd->u.echo_client;
508 struct obd_info oinfo = { { { 0 } } };
510 struct brw_page *pga;
511 struct brw_page *pgp;
518 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
519 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
520 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
522 gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
524 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
525 LASSERT(lsm != NULL);
526 LASSERT(lsm->lsm_object_id == oa->o_id);
529 (count & (~CFS_PAGE_MASK)) != 0)
532 /* XXX think again with misaligned I/O */
533 npages = count >> CFS_PAGE_SHIFT;
535 OBD_ALLOC(pga, npages * sizeof(*pga));
539 for (i = 0, pgp = pga, off = offset;
541 i++, pgp++, off += CFS_PAGE_SIZE) {
543 LASSERT (pgp->pg == NULL); /* for cleanup */
546 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
550 pgp->count = CFS_PAGE_SIZE;
555 echo_client_page_debug_setup(lsm, pgp->pg, rw,
556 oa->o_id, off, pgp->count);
561 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
564 if (rc != 0 || rw != OBD_BRW_READ)
567 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
573 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
574 pgp->off, pgp->count);
575 if (vrc != 0 && rc == 0)
578 OBD_PAGE_FREE(pgp->pg);
580 OBD_FREE(pga, npages * sizeof(*pga));
585 static int echo_client_ubrw(struct obd_device *obd, int rw,
586 struct obdo *oa, struct lov_stripe_md *lsm,
587 obd_off offset, obd_size count, char *buffer,
588 struct obd_trans_info *oti)
590 #warning "echo_client_ubrw() needs to be ported on 2.6 yet"
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 #define EAP_FROM_COOKIE(c) \
609 (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC), \
610 (struct echo_async_page *)(c))
612 struct echo_async_state {
614 obd_off eas_next_offset;
615 obd_off eas_end_offset;
618 cfs_waitq_t eas_waitq;
619 struct list_head eas_avail;
621 struct lov_stripe_md *eas_lsm;
624 static int eas_should_wake(struct echo_async_state *eas)
628 spin_lock(&eas->eas_lock);
629 if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
631 spin_unlock(&eas->eas_lock);
635 static int ec_ap_make_ready(void *data, int cmd)
637 /* our pages are issued ready */
641 static int ec_ap_refresh_count(void *data, int cmd)
643 /* our pages are issued with a stable count */
645 return CFS_PAGE_SIZE;
647 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
649 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
651 memcpy(oa, &eap->eap_eas->eas_oa, sizeof(*oa));
654 static int ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
656 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
657 struct echo_async_state *eas;
661 if (cmd == OBD_BRW_READ &&
662 eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
663 (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
664 (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
665 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
666 eas->eas_oa.o_id, eap->eap_off,
669 spin_lock(&eas->eas_lock);
670 if (rc && !eas->eas_rc)
672 eas->eas_in_flight--;
673 list_add(&eap->eap_item, &eas->eas_avail);
674 cfs_waitq_signal(&eas->eas_waitq);
675 spin_unlock(&eas->eas_lock);
679 static struct obd_async_page_ops ec_async_page_ops = {
680 .ap_make_ready = ec_ap_make_ready,
681 .ap_refresh_count = ec_ap_refresh_count,
682 .ap_fill_obdo = ec_ap_fill_obdo,
683 .ap_completion = ec_ap_completion,
686 static int echo_client_async_page(struct obd_export *exp, int rw,
687 struct obdo *oa, struct lov_stripe_md *lsm,
688 obd_off offset, obd_size count,
692 struct echo_async_page *eap;
693 struct echo_async_state eas;
695 struct echo_async_page **aps = NULL;
702 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
703 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
704 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
706 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
709 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
712 (count & (~CFS_PAGE_MASK)) != 0 ||
714 lsm->lsm_object_id != oa->o_id))
717 /* XXX think again with misaligned I/O */
718 npages = batching >> CFS_PAGE_SHIFT;
720 memcpy(&eas.eas_oa, oa, sizeof(*oa));
721 eas.eas_next_offset = offset;
722 eas.eas_end_offset = offset + count;
723 spin_lock_init(&eas.eas_lock);
724 cfs_waitq_init(&eas.eas_waitq);
725 eas.eas_in_flight = 0;
728 CFS_INIT_LIST_HEAD(&eas.eas_avail);
730 OBD_ALLOC(aps, npages * sizeof aps[0]);
734 /* prepare the group of pages that we're going to be keeping
736 for (i = 0; i < npages; i++) {
738 OBD_PAGE_ALLOC(page, CFS_ALLOC_STD);
740 GOTO(out, rc = -ENOMEM);
742 OBD_ALLOC(eap, sizeof(*eap));
745 GOTO(out, rc = -ENOMEM);
748 eap->eap_magic = EAP_MAGIC;
749 eap->eap_page = page;
751 list_add_tail(&eap->eap_item, &eas.eas_avail);
755 /* first we spin queueing io and being woken by its completion */
756 spin_lock(&eas.eas_lock);
760 /* sleep until we have a page to send */
761 spin_unlock(&eas.eas_lock);
762 rc = wait_event_interruptible(eas.eas_waitq,
763 eas_should_wake(&eas));
764 spin_lock(&eas.eas_lock);
765 if (rc && !eas.eas_rc)
769 if (list_empty(&eas.eas_avail))
771 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
773 list_del(&eap->eap_item);
774 spin_unlock(&eas.eas_lock);
776 /* unbind the eap from its old page offset */
777 if (eap->eap_cookie != NULL) {
778 obd_teardown_async_page(exp, lsm, NULL,
780 eap->eap_cookie = NULL;
783 eas.eas_next_offset += CFS_PAGE_SIZE;
784 eap->eap_off = eas.eas_next_offset;
786 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
787 eap->eap_off, &ec_async_page_ops,
788 eap, &eap->eap_cookie);
790 spin_lock(&eas.eas_lock);
795 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
796 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
797 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
798 echo_client_page_debug_setup(lsm, eap->eap_page, rw,
800 eap->eap_off, CFS_PAGE_SIZE);
802 /* always asserts urgent, which isn't quite right */
803 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
804 rw, 0, CFS_PAGE_SIZE, 0,
805 ASYNC_READY | ASYNC_URGENT |
807 spin_lock(&eas.eas_lock);
808 if (rc && !eas.eas_rc) {
813 if (eas.eas_next_offset == eas.eas_end_offset)
817 /* still hold the eas_lock here.. */
819 /* now we just spin waiting for all the rpcs to complete */
820 while(eas.eas_in_flight) {
821 spin_unlock(&eas.eas_lock);
822 wait_event_interruptible(eas.eas_waitq,
823 eas.eas_in_flight == 0);
824 spin_lock(&eas.eas_lock);
826 spin_unlock(&eas.eas_lock);
830 for (i = 0; i < npages; ++ i) {
834 page = eap->eap_page;
835 if (eap->eap_cookie != NULL)
836 obd_teardown_async_page(exp, lsm, NULL,
838 OBD_FREE(eap, sizeof(*eap));
841 OBD_FREE(aps, npages * sizeof aps[0]);
847 static int echo_client_prep_commit(struct obd_export *exp, int rw,
848 struct obdo *oa, struct lov_stripe_md *lsm,
849 obd_off offset, obd_size count,
850 obd_size batch, struct obd_trans_info *oti)
852 struct obd_ioobj ioo;
853 struct niobuf_local *lnb;
854 struct niobuf_remote *rnb;
856 obd_size npages, tot_pages;
860 if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
861 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
864 npages = batch >> CFS_PAGE_SHIFT;
865 tot_pages = count >> CFS_PAGE_SHIFT;
867 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
868 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
870 if (lnb == NULL || rnb == NULL)
871 GOTO(out, ret = -ENOMEM);
873 obdo_to_ioobj(oa, &ioo);
877 for(; tot_pages; tot_pages -= npages) {
878 if (tot_pages < npages)
881 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
883 rnb[i].len = CFS_PAGE_SIZE;
886 ioo.ioo_bufcnt = npages;
887 oti->oti_transno = 0;
889 ret = obd_preprw(rw, exp, oa, 1, &ioo, npages, rnb, lnb, oti,
894 for (i = 0; i < npages; i++) {
895 cfs_page_t *page = lnb[i].page;
898 if (page == NULL && lnb[i].rc == 0)
901 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
902 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
903 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
906 if (rw == OBD_BRW_WRITE)
907 echo_client_page_debug_setup(lsm, page, rw,
912 echo_client_page_debug_check(lsm, page,
918 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
925 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
927 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
931 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
932 struct obd_ioctl_data *data)
934 struct obd_device *obd = class_exp2obd(exp);
935 struct echo_client_obd *ec = &obd->u.echo_client;
936 struct obd_trans_info dummy_oti = { .oti_thread_id = -1 };
937 struct ec_object *eco;
941 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
945 data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
946 data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
947 data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
949 switch((long)data->ioc_pbuf1) {
951 if (data->ioc_pbuf2 == NULL) { // NULL user data pointer
952 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
953 eco->eco_lsm, data->ioc_offset,
954 data->ioc_count, &dummy_oti);
957 rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
958 eco->eco_lsm, data->ioc_offset,
959 data->ioc_count, data->ioc_pbuf2,
965 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
966 eco->eco_lsm, data->ioc_offset,
967 data->ioc_count, data->ioc_plen1);
970 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
971 eco->eco_lsm, data->ioc_offset,
972 data->ioc_count, data->ioc_plen1,
978 echo_put_object(eco);
983 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
984 void *data, int flag)
986 struct ec_object *eco = (struct ec_object *)data;
987 struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
988 struct lustre_handle lockh;
989 struct list_head *el;
993 ldlm_lock2handle (lock, &lockh);
995 /* #ifdef this out if we're not feeling paranoid */
996 spin_lock (&ec->ec_lock);
997 list_for_each (el, &ec->ec_objects) {
998 found = (eco == list_entry(el, struct ec_object,
1003 spin_unlock (&ec->ec_lock);
1007 case LDLM_CB_BLOCKING:
1008 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1009 eco->eco_id, lockh.cookie);
1010 rc = ldlm_cli_cancel (&lockh);
1012 CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1015 case LDLM_CB_CANCELING:
1016 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1017 eco->eco_id, lockh.cookie);
1028 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1029 int mode, obd_off offset, obd_size nob)
1031 struct obd_device *obd = exp->exp_obd;
1032 struct echo_client_obd *ec = &obd->u.echo_client;
1033 struct lustre_handle *ulh = obdo_handle (oa);
1034 struct ldlm_enqueue_info einfo = { 0 };
1035 struct obd_info oinfo = { { { 0 } } };
1036 struct ec_object *eco;
1037 struct ec_lock *ecl;
1040 if (!(mode == LCK_PR || mode == LCK_PW))
1043 if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1044 (nob & (~CFS_PAGE_MASK)) != 0)
1047 rc = echo_get_object (&eco, obd, oa);
1052 OBD_ALLOC (ecl, sizeof (*ecl));
1056 ecl->ecl_mode = mode;
1057 ecl->ecl_object = eco;
1058 ecl->ecl_policy.l_extent.start = offset;
1059 ecl->ecl_policy.l_extent.end =
1060 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1062 einfo.ei_type = LDLM_EXTENT;
1063 einfo.ei_mode = mode;
1064 einfo.ei_cb_bl = echo_ldlm_callback;
1065 einfo.ei_cb_cp = ldlm_completion_ast;
1066 einfo.ei_cb_gl = NULL;
1067 einfo.ei_cbdata = eco;
1069 oinfo.oi_policy = ecl->ecl_policy;
1070 oinfo.oi_lockh = &ecl->ecl_lock_handle;
1071 oinfo.oi_md = eco->eco_lsm;
1072 rc = obd_enqueue(ec->ec_exp, &oinfo, &einfo, NULL);
1076 CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1078 /* NB ecl takes object ref from echo_get_object() above */
1079 spin_lock(&ec->ec_lock);
1081 list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1082 ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1084 spin_unlock(&ec->ec_lock);
1086 oa->o_valid |= OBD_MD_FLHANDLE;
1090 OBD_FREE (ecl, sizeof (*ecl));
1092 echo_put_object (eco);
1097 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1099 struct obd_device *obd = exp->exp_obd;
1100 struct echo_client_obd *ec = &obd->u.echo_client;
1101 struct lustre_handle *ulh = obdo_handle (oa);
1102 struct ec_lock *ecl = NULL;
1104 struct list_head *el;
1107 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1110 spin_lock (&ec->ec_lock);
1112 list_for_each (el, &exp->exp_ec_data.eced_locks) {
1113 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1114 found = (ecl->ecl_cookie == ulh->cookie);
1116 list_del (&ecl->ecl_exp_chain);
1121 spin_unlock (&ec->ec_lock);
1126 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1127 &ecl->ecl_lock_handle);
1129 echo_put_object (ecl->ecl_object);
1130 OBD_FREE (ecl, sizeof (*ecl));
1136 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1137 int len, void *karg, void *uarg)
1139 struct obd_device *obd;
1140 struct echo_client_obd *ec;
1141 struct ec_object *eco;
1142 struct obd_ioctl_data *data = karg;
1143 struct obd_trans_info dummy_oti;
1144 struct oti_req_ack_lock *ack_lock;
1146 int rw = OBD_BRW_READ;
1153 memset(&dummy_oti, 0, sizeof(dummy_oti));
1156 ec = &obd->u.echo_client;
1159 case OBD_IOC_CREATE: /* may create echo object */
1160 if (!capable (CAP_SYS_ADMIN))
1161 GOTO (out, rc = -EPERM);
1163 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1164 data->ioc_pbuf1, data->ioc_plen1,
1168 case OBD_IOC_DESTROY:
1169 if (!capable (CAP_SYS_ADMIN))
1170 GOTO (out, rc = -EPERM);
1172 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1174 oa = &data->ioc_obdo1;
1175 oa->o_gr = FILTER_GROUP_ECHO;
1176 oa->o_valid |= OBD_MD_FLGROUP;
1177 rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1180 eco->eco_deleted = 1;
1181 echo_put_object(eco);
1185 case OBD_IOC_GETATTR:
1186 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1188 struct obd_info oinfo = { { { 0 } } };
1189 oinfo.oi_md = eco->eco_lsm;
1190 oinfo.oi_oa = &data->ioc_obdo1;
1191 rc = obd_getattr(ec->ec_exp, &oinfo);
1192 echo_put_object(eco);
1196 case OBD_IOC_SETATTR:
1197 if (!capable (CAP_SYS_ADMIN))
1198 GOTO (out, rc = -EPERM);
1200 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1202 struct obd_info oinfo = { { { 0 } } };
1203 oinfo.oi_oa = &data->ioc_obdo1;
1204 oinfo.oi_md = eco->eco_lsm;
1206 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1207 echo_put_object(eco);
1211 case OBD_IOC_BRW_WRITE:
1212 if (!capable (CAP_SYS_ADMIN))
1213 GOTO (out, rc = -EPERM);
1217 case OBD_IOC_BRW_READ:
1218 rc = echo_client_brw_ioctl(rw, exp, data);
1221 case ECHO_IOC_GET_STRIPE:
1222 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1224 rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1226 echo_put_object(eco);
1230 case ECHO_IOC_SET_STRIPE:
1231 if (!capable (CAP_SYS_ADMIN))
1232 GOTO (out, rc = -EPERM);
1234 if (data->ioc_pbuf1 == NULL) { /* unset */
1235 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1237 eco->eco_deleted = 1;
1238 echo_put_object(eco);
1241 rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1243 data->ioc_plen1, &dummy_oti);
1247 case ECHO_IOC_ENQUEUE:
1248 if (!capable (CAP_SYS_ADMIN))
1249 GOTO (out, rc = -EPERM);
1251 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1252 data->ioc_conn1, /* lock mode */
1253 data->ioc_offset, data->ioc_count);/*extent*/
1256 case ECHO_IOC_CANCEL:
1257 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1261 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1262 GOTO (out, rc = -ENOTTY);
1268 /* XXX this should be in a helper also called by target_send_reply */
1269 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1271 if (!ack_lock->mode)
1273 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1281 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1283 struct echo_client_obd *ec = &obddev->u.echo_client;
1284 struct obd_device *tgt;
1285 struct lustre_handle conn = {0, };
1286 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1287 struct obd_connect_data *ocd = NULL;
1291 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1292 CERROR("requires a TARGET OBD name\n");
1296 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1297 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1298 CERROR("device not attached or not set up (%s)\n",
1299 lustre_cfg_string(lcfg, 1));
1303 spin_lock_init (&ec->ec_lock);
1304 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1307 OBD_ALLOC(ocd, sizeof(*ocd));
1309 CERROR("Can't alloc ocd connecting to %s\n",
1310 lustre_cfg_string(lcfg, 1));
1314 ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL;
1315 ocd->ocd_version = LUSTRE_VERSION_CODE;
1316 ocd->ocd_group = FILTER_GROUP_ECHO;
1318 rc = obd_connect(NULL, &conn, tgt, &echo_uuid, ocd);
1320 OBD_FREE(ocd, sizeof(*ocd));
1323 CERROR("fail to connect to device %s\n",
1324 lustre_cfg_string(lcfg, 1));
1327 ec->ec_exp = class_conn2export(&conn);
1332 static int echo_client_cleanup(struct obd_device *obddev)
1334 struct list_head *el;
1335 struct ec_object *eco;
1336 struct echo_client_obd *ec = &obddev->u.echo_client;
1340 if (!list_empty(&obddev->obd_exports)) {
1341 CERROR("still has clients!\n");
1345 /* XXX assuming sole access */
1346 while (!list_empty(&ec->ec_objects)) {
1347 el = ec->ec_objects.next;
1348 eco = list_entry(el, struct ec_object, eco_obj_chain);
1350 LASSERT(eco->eco_refcount == 0);
1351 eco->eco_refcount = 1;
1352 eco->eco_deleted = 1;
1353 echo_put_object(eco);
1356 rc = obd_disconnect(ec->ec_exp);
1358 CERROR("fail to disconnect device: %d\n", rc);
1363 static int echo_client_connect(const struct lu_env *env,
1364 struct lustre_handle *conn,
1365 struct obd_device *src, struct obd_uuid *cluuid,
1366 struct obd_connect_data *data)
1368 struct obd_export *exp;
1372 rc = class_connect(conn, src, cluuid);
1374 exp = class_conn2export(conn);
1375 CFS_INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1376 class_export_put(exp);
1382 static int echo_client_disconnect(struct obd_export *exp)
1384 struct obd_device *obd;
1385 struct echo_client_obd *ec;
1386 struct ec_lock *ecl;
1391 GOTO(out, rc = -EINVAL);
1394 ec = &obd->u.echo_client;
1396 /* no more contention on export's lock list */
1397 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1398 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1399 struct ec_lock, ecl_exp_chain);
1400 list_del (&ecl->ecl_exp_chain);
1402 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1403 ecl->ecl_mode, &ecl->ecl_lock_handle);
1405 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1406 "(%d)\n", ecl->ecl_object->eco_id, rc);
1408 echo_put_object (ecl->ecl_object);
1409 OBD_FREE (ecl, sizeof (*ecl));
1412 rc = class_disconnect(exp);
1418 static struct obd_ops echo_obd_ops = {
1419 .o_owner = THIS_MODULE,
1420 .o_setup = echo_client_setup,
1421 .o_cleanup = echo_client_cleanup,
1422 .o_iocontrol = echo_client_iocontrol,
1423 .o_connect = echo_client_connect,
1424 .o_disconnect = echo_client_disconnect
1427 int echo_client_init(void)
1429 struct lprocfs_static_vars lvars;
1431 lprocfs_init_vars(echo, &lvars);
1432 return class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
1433 LUSTRE_ECHO_CLIENT_NAME, NULL);
1436 void echo_client_exit(void)
1438 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);