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 Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define DEBUG_SUBSYSTEM S_ECHO
24 #include <linux/version.h>
25 #include <linux/module.h>
27 #include <linux/completion.h>
28 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
29 #include <linux/iobuf.h>
31 #include <asm/div64.h>
33 #include <liblustre.h>
36 #include <linux/obd.h>
37 #include <linux/obd_support.h>
38 #include <linux/obd_class.h>
39 #include <linux/obd_echo.h>
40 #include <linux/lustre_debug.h>
41 #include <linux/lprocfs_status.h>
43 static obd_id last_object_id;
47 echo_printk_object (char *msg, struct ec_object *eco)
49 struct lov_stripe_md *lsm = eco->eco_lsm;
52 printk (KERN_INFO "Lustre: %s: object %p: "LPX64", refs %d%s: "LPX64
53 "=%u!%u\n", msg, eco, eco->eco_id, eco->eco_refcount,
54 eco->eco_deleted ? "(deleted) " : "",
55 lsm->lsm_object_id, lsm->lsm_stripe_size,
56 lsm->lsm_stripe_count);
58 for (i = 0; i < lsm->lsm_stripe_count; i++)
59 printk (KERN_INFO "Lustre: @%2u:"LPX64"\n",
60 lsm->lsm_oinfo[i].loi_ost_idx,
61 lsm->lsm_oinfo[i].loi_id);
65 static struct ec_object *
66 echo_find_object_locked (struct obd_device *obd, obd_id id)
68 struct echo_client_obd *ec = &obd->u.echo_client;
69 struct ec_object *eco = NULL;
72 list_for_each (el, &ec->ec_objects) {
73 eco = list_entry (el, struct ec_object, eco_obj_chain);
75 if (eco->eco_id == id)
82 echo_copyout_lsm (struct lov_stripe_md *lsm, void *ulsm, int ulsm_nob)
86 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
90 if (copy_to_user (ulsm, lsm, nob))
97 echo_copyin_lsm (struct obd_device *obd, struct lov_stripe_md *lsm,
98 void *ulsm, int ulsm_nob)
100 struct echo_client_obd *ec = &obd->u.echo_client;
103 if (ulsm_nob < sizeof (*lsm))
106 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
109 nob = lsm->lsm_stripe_count * sizeof (lsm->lsm_oinfo[0]);
111 if (ulsm_nob < nob ||
112 lsm->lsm_stripe_count > ec->ec_nstripes ||
113 lsm->lsm_magic != LOV_MAGIC ||
114 (lsm->lsm_stripe_size & (PAGE_SIZE - 1)) != 0 ||
115 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
118 if (copy_from_user(lsm->lsm_oinfo,
119 ((struct lov_stripe_md *)ulsm)->lsm_oinfo, nob))
125 static struct ec_object *
126 echo_allocate_object (struct obd_device *obd)
128 struct echo_client_obd *ec = &obd->u.echo_client;
129 struct ec_object *eco;
132 OBD_ALLOC(eco, sizeof (*eco));
136 rc = obd_alloc_memmd(ec->ec_exp, &eco->eco_lsm);
138 OBD_FREE(eco, sizeof (*eco));
142 eco->eco_device = obd;
143 eco->eco_deleted = 0;
144 eco->eco_refcount = 0;
145 eco->eco_lsm->lsm_magic = LOV_MAGIC;
146 /* leave stripe count 0 by default */
152 echo_free_object (struct ec_object *eco)
154 struct obd_device *obd = eco->eco_device;
155 struct echo_client_obd *ec = &obd->u.echo_client;
157 LASSERT (eco->eco_refcount == 0);
158 obd_free_memmd(ec->ec_exp, &eco->eco_lsm);
159 OBD_FREE (eco, sizeof (*eco));
162 static int echo_create_object(struct obd_device *obd, int on_target,
163 struct obdo *oa, void *ulsm, int ulsm_nob,
164 struct obd_trans_info *oti)
166 struct echo_client_obd *ec = &obd->u.echo_client;
167 struct ec_object *eco2;
168 struct ec_object *eco;
169 struct lov_stripe_md *lsm;
173 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
174 (on_target || /* set_stripe */
175 ec->ec_nstripes != 0)) { /* LOV */
176 CERROR ("No valid oid\n");
181 eco = echo_allocate_object (obd);
187 rc = echo_copyin_lsm (obd, lsm, ulsm, ulsm_nob);
191 /* setup object ID here for !on_target and LOV hint */
192 if ((oa->o_valid & OBD_MD_FLID) != 0)
193 eco->eco_id = lsm->lsm_object_id = oa->o_id;
195 if (lsm->lsm_stripe_count == 0)
196 lsm->lsm_stripe_count = ec->ec_nstripes;
198 if (lsm->lsm_stripe_size == 0)
199 lsm->lsm_stripe_size = PAGE_SIZE;
201 idx = ll_insecure_random_int();
203 /* setup stripes: indices + default ids if required */
204 for (i = 0; i < lsm->lsm_stripe_count; i++) {
205 if (lsm->lsm_oinfo[i].loi_id == 0)
206 lsm->lsm_oinfo[i].loi_id = lsm->lsm_object_id;
208 lsm->lsm_oinfo[i].loi_ost_idx =
209 (idx + i) % ec->ec_nstripes;
212 OBD_ALLOC(eco, sizeof(*eco));
213 eco->eco_device = obd;
218 oa->o_id = ++last_object_id;
221 /* XXX get some filter group constants */
223 oa->o_valid |= OBD_MD_FLGROUP;
224 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
228 /* See what object ID we were given */
229 eco->eco_id = oa->o_id = lsm->lsm_object_id;
230 oa->o_valid |= OBD_MD_FLID;
232 LASSERT(eco->eco_lsm == NULL || eco->eco_lsm == lsm);
236 spin_lock (&ec->ec_lock);
238 eco2 = echo_find_object_locked (obd, oa->o_id);
239 if (eco2 != NULL) { /* conflict */
240 spin_unlock (&ec->ec_lock);
242 CERROR ("Can't create object id "LPX64": id already exists%s\n",
243 oa->o_id, on_target ? " (undoing create)" : "");
246 obd_destroy(ec->ec_exp, oa, lsm, oti);
252 list_add (&eco->eco_obj_chain, &ec->ec_objects);
253 spin_unlock (&ec->ec_lock);
255 "created %p: "LPX64"=%u#%u@%u refs %d del %d\n",
257 eco->eco_lsm->lsm_stripe_size,
258 eco->eco_lsm->lsm_stripe_count,
259 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
260 eco->eco_refcount, eco->eco_deleted);
264 echo_free_object (eco);
269 echo_get_object (struct ec_object **ecop, struct obd_device *obd,
272 struct echo_client_obd *ec = &obd->u.echo_client;
273 struct ec_object *eco;
274 struct ec_object *eco2;
277 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
278 oa->o_id == 0) /* disallow use of object id 0 */
280 CERROR ("No valid oid\n");
284 spin_lock (&ec->ec_lock);
285 eco = echo_find_object_locked (obd, oa->o_id);
287 if (eco->eco_deleted) /* being deleted */
288 return (-EAGAIN); /* (see comment in cleanup) */
291 spin_unlock (&ec->ec_lock);
294 "found %p: "LPX64"=%u#%u@%u refs %d del %d\n",
296 eco->eco_lsm->lsm_stripe_size,
297 eco->eco_lsm->lsm_stripe_count,
298 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
299 eco->eco_refcount, eco->eco_deleted);
302 spin_unlock (&ec->ec_lock);
304 if (ec->ec_nstripes != 0) /* striping required */
307 eco = echo_allocate_object (obd);
311 eco->eco_id = eco->eco_lsm->lsm_object_id = oa->o_id;
313 spin_lock (&ec->ec_lock);
315 eco2 = echo_find_object_locked (obd, oa->o_id);
316 if (eco2 == NULL) { /* didn't race */
317 list_add (&eco->eco_obj_chain, &ec->ec_objects);
318 spin_unlock (&ec->ec_lock);
319 eco->eco_refcount = 1;
322 "created %p: "LPX64"=%u#%u@%d refs %d del %d\n",
324 eco->eco_lsm->lsm_stripe_size,
325 eco->eco_lsm->lsm_stripe_count,
326 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
327 eco->eco_refcount, eco->eco_deleted);
331 if (eco2->eco_deleted)
332 rc = -EAGAIN; /* lose race */
334 eco2->eco_refcount++; /* take existing */
337 LASSERT (eco2->eco_id == eco2->eco_lsm->lsm_object_id);
339 "found(2) %p: "LPX64"=%u#%u@%d refs %d del %d\n",
341 eco2->eco_lsm->lsm_stripe_size,
342 eco2->eco_lsm->lsm_stripe_count,
343 eco2->eco_lsm->lsm_oinfo[0].loi_ost_idx,
344 eco2->eco_refcount, eco2->eco_deleted);
347 spin_unlock (&ec->ec_lock);
349 echo_free_object (eco);
354 echo_put_object (struct ec_object *eco)
356 struct obd_device *obd = eco->eco_device;
357 struct echo_client_obd *ec = &obd->u.echo_client;
359 /* Release caller's ref on the object.
360 * delete => mark for deletion when last ref goes
363 spin_lock (&ec->ec_lock);
366 LASSERT (eco->eco_refcount >= 0);
368 CDEBUG(D_INFO, "put %p: "LPX64"=%u#%u@%d refs %d del %d\n",
370 eco->eco_lsm->lsm_stripe_size,
371 eco->eco_lsm->lsm_stripe_count,
372 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
373 eco->eco_refcount, eco->eco_deleted);
375 if (eco->eco_refcount != 0 || !eco->eco_deleted) {
376 spin_unlock (&ec->ec_lock);
380 spin_unlock (&ec->ec_lock);
382 /* NB leave obj in the object list. We must prevent anyone from
383 * attempting to enqueue on this object number until we can be
384 * sure there will be no more lock callbacks.
386 obd_cancel_unused(ec->ec_exp, eco->eco_lsm, 0, NULL);
388 /* now we can let it go */
389 spin_lock (&ec->ec_lock);
390 list_del (&eco->eco_obj_chain);
391 spin_unlock (&ec->ec_lock);
393 LASSERT (eco->eco_refcount == 0);
395 echo_free_object (eco);
399 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
401 unsigned long stripe_count;
402 unsigned long stripe_size;
404 unsigned long woffset;
408 if (lsm->lsm_stripe_count <= 1)
412 stripe_size = lsm->lsm_stripe_size;
413 stripe_count = lsm->lsm_stripe_count;
415 /* width = # bytes in all stripes */
416 width = stripe_size * stripe_count;
418 /* woffset = offset within a width; offset = whole number of widths */
419 woffset = do_div (offset, width);
421 stripe_index = woffset / stripe_size;
423 *idp = lsm->lsm_oinfo[stripe_index].loi_id;
424 *offp = offset * stripe_size + woffset % stripe_size;
428 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
429 struct page *page, int rw, obd_id id,
430 obd_off offset, obd_off count)
437 /* no partial pages on the client */
438 LASSERT(count == PAGE_SIZE);
442 for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
443 if (rw == OBD_BRW_WRITE) {
444 stripe_off = offset + delta;
446 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
448 stripe_off = 0xdeadbeef00c0ffeeULL;
449 stripe_id = 0xdeadbeef00c0ffeeULL;
451 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
452 stripe_off, stripe_id);
459 echo_client_page_debug_check(struct lov_stripe_md *lsm,
460 struct page *page, obd_id id,
461 obd_off offset, obd_off count)
470 /* no partial pages on the client */
471 LASSERT(count == PAGE_SIZE);
475 for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
476 stripe_off = offset + delta;
478 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
480 rc2 = block_debug_check("test_brw",
481 addr + delta, OBD_ECHO_BLOCK_SIZE,
482 stripe_off, stripe_id);
491 static int echo_client_kbrw(struct obd_device *obd, int rw, struct obdo *oa,
492 struct lov_stripe_md *lsm, obd_off offset,
493 obd_size count, struct obd_trans_info *oti)
495 struct echo_client_obd *ec = &obd->u.echo_client;
497 struct brw_page *pga;
498 struct brw_page *pgp;
505 /* oa_id == ECHO_PERSISTENT_OBJID => speed test (no verification).
506 * oa & 1 => use HIGHMEM */
508 verify = (oa->o_id) != ECHO_PERSISTENT_OBJID;
509 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
511 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
514 (count & (PAGE_SIZE - 1)) != 0 ||
516 lsm->lsm_object_id != oa->o_id))
519 /* XXX think again with misaligned I/O */
520 npages = count >> PAGE_SHIFT;
522 OBD_ALLOC(pga, npages * sizeof(*pga));
526 for (i = 0, pgp = pga, off = offset;
528 i++, pgp++, off += PAGE_SIZE) {
530 LASSERT (pgp->pg == NULL); /* for cleanup */
533 pgp->pg = alloc_pages (gfp_mask, 0);
537 pgp->count = PAGE_SIZE;
542 echo_client_page_debug_setup(lsm, pgp->pg, rw,
543 oa->o_id, off, pgp->count);
546 rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
549 if (rc != 0 || rw != OBD_BRW_READ)
552 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
558 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
559 pgp->off, pgp->count);
560 if (vrc != 0 && rc == 0)
563 __free_pages(pgp->pg, 0);
565 OBD_FREE(pga, npages * sizeof(*pga));
570 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
571 static int echo_client_ubrw(struct obd_device *obd, int rw,
572 struct obdo *oa, struct lov_stripe_md *lsm,
573 obd_off offset, obd_size count, char *buffer,
574 struct obd_trans_info *oti)
576 struct echo_client_obd *ec = &obd->u.echo_client;
578 struct brw_page *pga;
579 struct brw_page *pgp;
581 struct kiobuf *kiobuf;
585 LASSERT (rw == OBD_BRW_WRITE ||
588 /* NB: for now, only whole pages, page aligned */
591 ((long)buffer & (PAGE_SIZE - 1)) != 0 ||
592 (count & (PAGE_SIZE - 1)) != 0 ||
593 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
596 /* XXX think again with misaligned I/O */
597 npages = count >> PAGE_SHIFT;
599 OBD_ALLOC(pga, npages * sizeof(*pga));
603 rc = alloc_kiovec (1, &kiobuf);
607 rc = map_user_kiobuf ((rw == OBD_BRW_READ) ? READ : WRITE,
608 kiobuf, (unsigned long)buffer, count);
612 LASSERT (kiobuf->offset == 0);
613 LASSERT (kiobuf->nr_pages == npages);
615 for (i = 0, off = offset, pgp = pga;
617 i++, off += PAGE_SIZE, pgp++) {
619 pgp->pg = kiobuf->maplist[i];
620 pgp->count = PAGE_SIZE;
624 rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
626 // if (rw == OBD_BRW_READ)
627 // mark_dirty_kiobuf (kiobuf, count);
629 unmap_kiobuf (kiobuf);
631 free_kiovec (1, &kiobuf);
633 OBD_FREE(pga, npages * sizeof(*pga));
637 static int echo_client_ubrw(struct obd_device *obd, int rw,
638 struct obdo *oa, struct lov_stripe_md *lsm,
639 obd_off offset, obd_size count, char *buffer,
640 struct obd_trans_info *oti)
642 #warning "echo_client_ubrw() needs to be ported on 2.6 yet"
649 struct echo_async_state;
651 #define EAP_MAGIC 79277927
652 struct echo_async_page {
654 struct page *eap_page;
657 struct echo_async_state *eap_eas;
658 struct list_head eap_item;
661 struct echo_async_state {
663 obd_off eas_next_offset;
664 obd_off eas_end_offset;
667 wait_queue_head_t eas_waitq;
668 struct list_head eas_avail;
670 struct lov_stripe_md *eas_lsm;
673 static int eas_should_wake(struct echo_async_state *eas)
677 spin_lock_irqsave(&eas->eas_lock, flags);
678 if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
680 spin_unlock_irqrestore(&eas->eas_lock, flags);
684 struct echo_async_page *eap_from_cookie(void *cookie)
686 struct echo_async_page *eap = cookie;
687 if (eap->eap_magic != EAP_MAGIC)
688 return ERR_PTR(-EINVAL);
692 static int ec_ap_make_ready(void *data, int cmd)
694 /* our pages are issued ready */
698 static int ec_ap_refresh_count(void *data, int cmd)
700 /* our pages are issued with a stable count */
704 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
706 struct echo_async_page *eap;
707 eap = eap_from_cookie(data);
711 memcpy(oa, &eap->eap_eas->eas_oa, sizeof(*oa));
714 static void ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
716 struct echo_async_page *eap = eap_from_cookie(data);
717 struct echo_async_state *eas;
724 if (cmd == OBD_BRW_READ &&
725 eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID)
726 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
727 eas->eas_oa.o_id, eap->eap_off,
730 spin_lock_irqsave(&eas->eas_lock, flags);
731 if (rc && !eas->eas_rc)
733 eas->eas_in_flight--;
734 list_add(&eap->eap_item, &eas->eas_avail);
735 wake_up(&eas->eas_waitq);
736 spin_unlock_irqrestore(&eas->eas_lock, flags);
739 static struct obd_async_page_ops ec_async_page_ops = {
740 .ap_make_ready = ec_ap_make_ready,
741 .ap_refresh_count = ec_ap_refresh_count,
742 .ap_fill_obdo = ec_ap_fill_obdo,
743 .ap_completion = ec_ap_completion,
746 static int echo_client_async_page(struct obd_export *exp, int rw,
747 struct obdo *oa, struct lov_stripe_md *lsm,
748 obd_off offset, obd_size count,
752 struct echo_async_page *eap;
753 struct echo_async_state eas;
754 struct list_head *pos, *n;
761 /* oa_id == 0 => speed test (no verification) else...
762 * oa & 1 => use HIGHMEM
764 verify = (oa->o_id != 0);
765 gfp_mask = ((oa->o_id & 1) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
768 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
771 (count & (PAGE_SIZE - 1)) != 0 ||
773 lsm->lsm_object_id != oa->o_id))
776 /* XXX think again with misaligned I/O */
777 npages = batching >> PAGE_SHIFT;
779 memcpy(&eas.eas_oa, oa, sizeof(*oa));
780 eas.eas_next_offset = offset;
781 eas.eas_end_offset = offset + count;
782 spin_lock_init(&eas.eas_lock);
783 init_waitqueue_head(&eas.eas_waitq);
784 eas.eas_in_flight = 0;
787 INIT_LIST_HEAD(&eas.eas_avail);
789 /* prepare the group of pages that we're going to be keeping
791 for (i = 0; i < npages; i++) {
792 struct page *page = alloc_page(GFP_KERNEL);
794 GOTO(out, rc = -ENOMEM);
797 list_add_tail(&PAGE_LIST(page), &pages);
799 OBD_ALLOC(eap, sizeof(*eap));
801 GOTO(out, rc = -ENOMEM);
803 eap->eap_magic = EAP_MAGIC;
804 eap->eap_page = page;
806 page->private = (unsigned long)eap;
807 list_add_tail(&eap->eap_item, &eas.eas_avail);
810 /* first we spin queueing io and being woken by its completion */
811 spin_lock_irqsave(&eas.eas_lock, flags);
815 /* sleep until we have a page to send */
816 spin_unlock_irqrestore(&eas.eas_lock, flags);
817 rc = wait_event_interruptible(eas.eas_waitq,
818 eas_should_wake(&eas));
819 spin_lock_irqsave(&eas.eas_lock, flags);
820 if (rc && !eas.eas_rc)
824 if (list_empty(&eas.eas_avail))
826 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
828 list_del(&eap->eap_item);
829 spin_unlock_irqrestore(&eas.eas_lock, flags);
831 /* unbind the eap from its old page offset */
832 if (eap->eap_cookie != NULL) {
833 obd_teardown_async_page(exp, lsm, NULL,
835 eap->eap_cookie = NULL;
838 eas.eas_next_offset += PAGE_SIZE;
839 eap->eap_off = eas.eas_next_offset;
841 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
842 eap->eap_off, &ec_async_page_ops,
843 eap, &eap->eap_cookie);
845 spin_lock_irqsave(&eas.eas_lock, flags);
850 if (oa->o_id != ECHO_PERSISTENT_OBJID)
851 echo_client_page_debug_setup(lsm, eap->eap_page, rw,
853 eap->eap_off, PAGE_SIZE);
855 /* always asserts urgent, which isn't quite right */
856 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
858 ASYNC_READY | ASYNC_URGENT |
860 spin_lock_irqsave(&eas.eas_lock, flags);
861 if (rc && !eas.eas_rc) {
866 if (eas.eas_next_offset == eas.eas_end_offset)
870 /* still hold the eas_lock here.. */
872 /* now we just spin waiting for all the rpcs to complete */
873 while(eas.eas_in_flight) {
874 spin_unlock_irqrestore(&eas.eas_lock, flags);
875 wait_event_interruptible(eas.eas_waitq,
876 eas.eas_in_flight == 0);
877 spin_lock_irqsave(&eas.eas_lock, flags);
879 spin_unlock_irqrestore(&eas.eas_lock, flags);
882 list_for_each_safe(pos, n, &pages) {
883 struct page *page = list_entry(pos, struct page,
886 list_del(&PAGE_LIST(page));
887 if (page->private != 0) {
888 eap = (struct echo_async_page *)page->private;
889 if (eap->eap_cookie != NULL)
890 obd_teardown_async_page(exp, lsm, NULL,
892 OBD_FREE(eap, sizeof(*eap));
900 static int echo_client_prep_commit(struct obd_export *exp, int rw,
901 struct obdo *oa, struct lov_stripe_md *lsm,
902 obd_off offset, obd_size count,
903 obd_size batch, struct obd_trans_info *oti)
905 struct obd_ioobj ioo;
906 struct niobuf_local *lnb;
907 struct niobuf_remote *rnb;
909 obd_size npages, tot_pages;
913 if (count <= 0 || (count & (PAGE_SIZE - 1)) != 0 ||
914 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
917 npages = batch >> PAGE_SHIFT;
918 tot_pages = count >> PAGE_SHIFT;
920 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
921 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
923 if (lnb == NULL || rnb == NULL)
924 GOTO(out, ret = -ENOMEM);
926 obdo_to_ioobj(oa, &ioo);
930 for(; tot_pages; tot_pages -= npages) {
931 if (tot_pages < npages)
934 for (i = 0; i < npages; i++, off += PAGE_SIZE) {
936 rnb[i].len = PAGE_SIZE;
939 /* XXX this can't be the best.. */
940 memset(oti, 0, sizeof(*oti));
941 ioo.ioo_bufcnt = npages;
943 ret = obd_preprw(rw, exp, oa, 1, &ioo, npages, rnb, lnb, oti);
947 for (i = 0; i < npages; i++) {
948 struct page *page = lnb[i].page;
951 if (page == NULL && lnb[i].rc == 0)
954 if (oa->o_id == ECHO_PERSISTENT_OBJID)
957 if (rw == OBD_BRW_WRITE)
958 echo_client_page_debug_setup(lsm, page, rw,
963 echo_client_page_debug_check(lsm, page,
969 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
976 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
978 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
982 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
983 struct obd_ioctl_data *data)
985 struct obd_device *obd = class_exp2obd(exp);
986 struct echo_client_obd *ec = &obd->u.echo_client;
987 struct obd_trans_info dummy_oti;
988 struct ec_object *eco;
992 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
996 memset(&dummy_oti, 0, sizeof(dummy_oti));
998 data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
999 data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
1000 data->ioc_obdo1.o_gr = 2;
1002 switch((long)data->ioc_pbuf1) {
1004 if (data->ioc_pbuf2 == NULL) { // NULL user data pointer
1005 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
1006 eco->eco_lsm, data->ioc_offset,
1007 data->ioc_count, &dummy_oti);
1010 rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
1011 eco->eco_lsm, data->ioc_offset,
1012 data->ioc_count, data->ioc_pbuf2,
1018 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
1019 eco->eco_lsm, data->ioc_offset,
1020 data->ioc_count, data->ioc_plen1);
1023 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1024 eco->eco_lsm, data->ioc_offset,
1025 data->ioc_count, data->ioc_plen1,
1031 echo_put_object(eco);
1036 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1037 void *data, int flag)
1039 struct ec_object *eco = (struct ec_object *)data;
1040 struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1041 struct lustre_handle lockh;
1042 struct list_head *el;
1046 ldlm_lock2handle (lock, &lockh);
1048 /* #ifdef this out if we're not feeling paranoid */
1049 spin_lock (&ec->ec_lock);
1050 list_for_each (el, &ec->ec_objects) {
1051 found = (eco == list_entry(el, struct ec_object,
1056 spin_unlock (&ec->ec_lock);
1060 case LDLM_CB_BLOCKING:
1061 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1062 eco->eco_id, lockh.cookie);
1063 rc = ldlm_cli_cancel (&lockh);
1065 CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1068 case LDLM_CB_CANCELING:
1069 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1070 eco->eco_id, lockh.cookie);
1081 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1082 int mode, obd_off offset, obd_size nob)
1084 struct obd_device *obd = exp->exp_obd;
1085 struct echo_client_obd *ec = &obd->u.echo_client;
1086 struct lustre_handle *ulh = obdo_handle (oa);
1087 struct ec_object *eco;
1088 struct ec_lock *ecl;
1092 if (!(mode == LCK_PR || mode == LCK_PW))
1095 if ((offset & (PAGE_SIZE - 1)) != 0 ||
1096 (nob & (PAGE_SIZE - 1)) != 0)
1099 rc = echo_get_object (&eco, obd, oa);
1104 OBD_ALLOC (ecl, sizeof (*ecl));
1108 ecl->ecl_mode = mode;
1109 ecl->ecl_object = eco;
1110 ecl->ecl_policy.l_extent.start = offset;
1111 ecl->ecl_policy.l_extent.end =
1112 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1115 rc = obd_enqueue(ec->ec_exp, eco->eco_lsm, LDLM_EXTENT,
1116 &ecl->ecl_policy, mode, &flags, echo_ldlm_callback,
1117 ldlm_completion_ast, NULL, eco, sizeof(struct ost_lvb),
1118 lustre_swab_ost_lvb, &ecl->ecl_lock_handle);
1122 CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1124 /* NB ecl takes object ref from echo_get_object() above */
1125 spin_lock(&ec->ec_lock);
1127 list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1128 ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1130 spin_unlock(&ec->ec_lock);
1132 oa->o_valid |= OBD_MD_FLHANDLE;
1136 OBD_FREE (ecl, sizeof (*ecl));
1138 echo_put_object (eco);
1143 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1145 struct obd_device *obd = exp->exp_obd;
1146 struct echo_client_obd *ec = &obd->u.echo_client;
1147 struct lustre_handle *ulh = obdo_handle (oa);
1148 struct ec_lock *ecl = NULL;
1150 struct list_head *el;
1153 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1156 spin_lock (&ec->ec_lock);
1158 list_for_each (el, &exp->exp_ec_data.eced_locks) {
1159 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1160 found = (ecl->ecl_cookie == ulh->cookie);
1162 list_del (&ecl->ecl_exp_chain);
1167 spin_unlock (&ec->ec_lock);
1172 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1173 &ecl->ecl_lock_handle);
1175 echo_put_object (ecl->ecl_object);
1176 OBD_FREE (ecl, sizeof (*ecl));
1182 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1183 int len, void *karg, void *uarg)
1185 struct obd_device *obd;
1186 struct echo_client_obd *ec;
1187 struct ec_object *eco;
1188 struct obd_ioctl_data *data = karg;
1189 struct obd_trans_info dummy_oti;
1190 struct oti_req_ack_lock *ack_lock;
1192 int rw = OBD_BRW_READ;
1197 memset(&dummy_oti, 0, sizeof(dummy_oti));
1200 ec = &obd->u.echo_client;
1203 case OBD_IOC_CREATE: /* may create echo object */
1204 if (!capable (CAP_SYS_ADMIN))
1205 GOTO (out, rc = -EPERM);
1207 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1208 data->ioc_pbuf1, data->ioc_plen1,
1212 case OBD_IOC_DESTROY:
1213 if (!capable (CAP_SYS_ADMIN))
1214 GOTO (out, rc = -EPERM);
1216 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1218 oa = &data->ioc_obdo1;
1220 oa->o_valid |= OBD_MD_FLGROUP;
1221 rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1224 eco->eco_deleted = 1;
1225 echo_put_object(eco);
1229 case OBD_IOC_GETATTR:
1230 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1232 rc = obd_getattr(ec->ec_exp, &data->ioc_obdo1,
1234 echo_put_object(eco);
1238 case OBD_IOC_SETATTR:
1239 if (!capable (CAP_SYS_ADMIN))
1240 GOTO (out, rc = -EPERM);
1242 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1244 rc = obd_setattr(ec->ec_exp, &data->ioc_obdo1,
1245 eco->eco_lsm, NULL);
1246 echo_put_object(eco);
1250 case OBD_IOC_BRW_WRITE:
1251 if (!capable (CAP_SYS_ADMIN))
1252 GOTO (out, rc = -EPERM);
1256 case OBD_IOC_BRW_READ:
1257 rc = echo_client_brw_ioctl(rw, exp, data);
1260 case ECHO_IOC_GET_STRIPE:
1261 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1263 rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1265 echo_put_object(eco);
1269 case ECHO_IOC_SET_STRIPE:
1270 if (!capable (CAP_SYS_ADMIN))
1271 GOTO (out, rc = -EPERM);
1273 if (data->ioc_pbuf1 == NULL) { /* unset */
1274 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1276 eco->eco_deleted = 1;
1277 echo_put_object(eco);
1280 rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1282 data->ioc_plen1, &dummy_oti);
1286 case ECHO_IOC_ENQUEUE:
1287 if (!capable (CAP_SYS_ADMIN))
1288 GOTO (out, rc = -EPERM);
1290 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1291 data->ioc_conn1, /* lock mode */
1292 data->ioc_offset, data->ioc_count);/*extent*/
1295 case ECHO_IOC_CANCEL:
1296 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1300 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1301 GOTO (out, rc = -ENOTTY);
1307 /* XXX this should be in a helper also called by target_send_reply */
1308 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1310 if (!ack_lock->mode)
1312 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
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" };
1329 if (lcfg->lcfg_inllen1 < 1) {
1330 CERROR("requires a TARGET OBD name\n");
1334 tgt = class_name2obd(lcfg->lcfg_inlbuf1);
1335 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1336 CERROR("device not attached or not set up (%s)\n",
1337 lcfg->lcfg_inlbuf1);
1341 spin_lock_init (&ec->ec_lock);
1342 INIT_LIST_HEAD (&ec->ec_objects);
1345 rc = obd_connect(&conn, tgt, &echo_uuid);
1347 CERROR("fail to connect to device %s\n", lcfg->lcfg_inlbuf1);
1350 ec->ec_exp = class_conn2export(&conn);
1355 static int echo_client_cleanup(struct obd_device *obddev, int flags)
1357 struct list_head *el;
1358 struct ec_object *eco;
1359 struct echo_client_obd *ec = &obddev->u.echo_client;
1363 if (!list_empty(&obddev->obd_exports)) {
1364 CERROR("still has clients!\n");
1368 /* XXX assuming sole access */
1369 while (!list_empty(&ec->ec_objects)) {
1370 el = ec->ec_objects.next;
1371 eco = list_entry(el, struct ec_object, eco_obj_chain);
1373 LASSERT(eco->eco_refcount == 0);
1374 eco->eco_refcount = 1;
1375 eco->eco_deleted = 1;
1376 echo_put_object(eco);
1379 rc = obd_disconnect(ec->ec_exp, 0);
1381 CERROR("fail to disconnect device: %d\n", rc);
1386 static int echo_client_connect(struct lustre_handle *conn,
1387 struct obd_device *src, struct obd_uuid *cluuid)
1389 struct obd_export *exp;
1392 rc = class_connect(conn, src, cluuid);
1394 exp = class_conn2export(conn);
1395 INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1396 class_export_put(exp);
1402 static int echo_client_disconnect(struct obd_export *exp, int flags)
1404 struct obd_device *obd;
1405 struct echo_client_obd *ec;
1406 struct ec_lock *ecl;
1411 GOTO(out, rc = -EINVAL);
1414 ec = &obd->u.echo_client;
1416 /* no more contention on export's lock list */
1417 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1418 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1419 struct ec_lock, ecl_exp_chain);
1420 list_del (&ecl->ecl_exp_chain);
1422 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1423 ecl->ecl_mode, &ecl->ecl_lock_handle);
1425 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1426 "(%d)\n", ecl->ecl_object->eco_id, rc);
1428 echo_put_object (ecl->ecl_object);
1429 OBD_FREE (ecl, sizeof (*ecl));
1432 rc = class_disconnect(exp, 0);
1438 static struct obd_ops echo_obd_ops = {
1439 .o_owner = THIS_MODULE,
1440 .o_setup = echo_client_setup,
1441 .o_cleanup = echo_client_cleanup,
1442 .o_iocontrol = echo_client_iocontrol,
1443 .o_connect = echo_client_connect,
1444 .o_disconnect = echo_client_disconnect
1447 int echo_client_init(void)
1449 struct lprocfs_static_vars lvars;
1451 lprocfs_init_vars(echo, &lvars);
1452 return class_register_type(&echo_obd_ops, lvars.module_vars,
1453 OBD_ECHO_CLIENT_DEVICENAME);
1456 void echo_client_exit(void)
1458 class_unregister_type(OBD_ECHO_CLIENT_DEVICENAME);