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 oa->o_gr = FILTER_GROUP_ECHO;
222 oa->o_valid |= OBD_MD_FLGROUP;
223 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
227 /* See what object ID we were given */
228 eco->eco_id = oa->o_id = lsm->lsm_object_id;
229 oa->o_valid |= OBD_MD_FLID;
231 LASSERT(eco->eco_lsm == NULL || eco->eco_lsm == lsm);
235 spin_lock (&ec->ec_lock);
237 eco2 = echo_find_object_locked (obd, oa->o_id);
238 if (eco2 != NULL) { /* conflict */
239 spin_unlock (&ec->ec_lock);
241 CERROR ("Can't create object id "LPX64": id already exists%s\n",
242 oa->o_id, on_target ? " (undoing create)" : "");
245 obd_destroy(ec->ec_exp, oa, lsm, oti);
251 list_add (&eco->eco_obj_chain, &ec->ec_objects);
252 spin_unlock (&ec->ec_lock);
254 "created %p: "LPX64"=%u#%u@%u refs %d del %d\n",
256 eco->eco_lsm->lsm_stripe_size,
257 eco->eco_lsm->lsm_stripe_count,
258 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
259 eco->eco_refcount, eco->eco_deleted);
263 echo_free_object (eco);
268 echo_get_object (struct ec_object **ecop, struct obd_device *obd,
271 struct echo_client_obd *ec = &obd->u.echo_client;
272 struct ec_object *eco;
273 struct ec_object *eco2;
276 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
277 oa->o_id == 0) /* disallow use of object id 0 */
279 CERROR ("No valid oid\n");
283 spin_lock (&ec->ec_lock);
284 eco = echo_find_object_locked (obd, oa->o_id);
286 if (eco->eco_deleted) /* being deleted */
287 return (-EAGAIN); /* (see comment in cleanup) */
290 spin_unlock (&ec->ec_lock);
293 "found %p: "LPX64"=%u#%u@%u refs %d del %d\n",
295 eco->eco_lsm->lsm_stripe_size,
296 eco->eco_lsm->lsm_stripe_count,
297 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
298 eco->eco_refcount, eco->eco_deleted);
301 spin_unlock (&ec->ec_lock);
303 if (ec->ec_nstripes != 0) /* striping required */
306 eco = echo_allocate_object (obd);
310 eco->eco_id = eco->eco_lsm->lsm_object_id = oa->o_id;
312 spin_lock (&ec->ec_lock);
314 eco2 = echo_find_object_locked (obd, oa->o_id);
315 if (eco2 == NULL) { /* didn't race */
316 list_add (&eco->eco_obj_chain, &ec->ec_objects);
317 spin_unlock (&ec->ec_lock);
318 eco->eco_refcount = 1;
321 "created %p: "LPX64"=%u#%u@%d refs %d del %d\n",
323 eco->eco_lsm->lsm_stripe_size,
324 eco->eco_lsm->lsm_stripe_count,
325 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
326 eco->eco_refcount, eco->eco_deleted);
330 if (eco2->eco_deleted)
331 rc = -EAGAIN; /* lose race */
333 eco2->eco_refcount++; /* take existing */
336 LASSERT (eco2->eco_id == eco2->eco_lsm->lsm_object_id);
338 "found(2) %p: "LPX64"=%u#%u@%d refs %d del %d\n",
340 eco2->eco_lsm->lsm_stripe_size,
341 eco2->eco_lsm->lsm_stripe_count,
342 eco2->eco_lsm->lsm_oinfo[0].loi_ost_idx,
343 eco2->eco_refcount, eco2->eco_deleted);
346 spin_unlock (&ec->ec_lock);
348 echo_free_object (eco);
353 echo_put_object (struct ec_object *eco)
355 struct obd_device *obd = eco->eco_device;
356 struct echo_client_obd *ec = &obd->u.echo_client;
358 /* Release caller's ref on the object.
359 * delete => mark for deletion when last ref goes
362 spin_lock (&ec->ec_lock);
365 LASSERT (eco->eco_refcount >= 0);
367 CDEBUG(D_INFO, "put %p: "LPX64"=%u#%u@%d refs %d del %d\n",
369 eco->eco_lsm->lsm_stripe_size,
370 eco->eco_lsm->lsm_stripe_count,
371 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
372 eco->eco_refcount, eco->eco_deleted);
374 if (eco->eco_refcount != 0 || !eco->eco_deleted) {
375 spin_unlock (&ec->ec_lock);
379 spin_unlock (&ec->ec_lock);
381 /* NB leave obj in the object list. We must prevent anyone from
382 * attempting to enqueue on this object number until we can be
383 * sure there will be no more lock callbacks.
385 obd_cancel_unused(ec->ec_exp, eco->eco_lsm, 0, NULL);
387 /* now we can let it go */
388 spin_lock (&ec->ec_lock);
389 list_del (&eco->eco_obj_chain);
390 spin_unlock (&ec->ec_lock);
392 LASSERT (eco->eco_refcount == 0);
394 echo_free_object (eco);
398 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
400 unsigned long stripe_count;
401 unsigned long stripe_size;
403 unsigned long woffset;
407 if (lsm->lsm_stripe_count <= 1)
411 stripe_size = lsm->lsm_stripe_size;
412 stripe_count = lsm->lsm_stripe_count;
414 /* width = # bytes in all stripes */
415 width = stripe_size * stripe_count;
417 /* woffset = offset within a width; offset = whole number of widths */
418 woffset = do_div (offset, width);
420 stripe_index = woffset / stripe_size;
422 *idp = lsm->lsm_oinfo[stripe_index].loi_id;
423 *offp = offset * stripe_size + woffset % stripe_size;
427 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
428 struct page *page, int rw, obd_id id,
429 obd_off offset, obd_off count)
436 /* no partial pages on the client */
437 LASSERT(count == PAGE_SIZE);
441 for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
442 if (rw == OBD_BRW_WRITE) {
443 stripe_off = offset + delta;
445 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
447 stripe_off = 0xdeadbeef00c0ffeeULL;
448 stripe_id = 0xdeadbeef00c0ffeeULL;
450 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
451 stripe_off, stripe_id);
458 echo_client_page_debug_check(struct lov_stripe_md *lsm,
459 struct page *page, obd_id id,
460 obd_off offset, obd_off count)
469 /* no partial pages on the client */
470 LASSERT(count == PAGE_SIZE);
474 for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
475 stripe_off = offset + delta;
477 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
479 rc2 = block_debug_check("test_brw",
480 addr + delta, OBD_ECHO_BLOCK_SIZE,
481 stripe_off, stripe_id);
490 static int echo_client_kbrw(struct obd_device *obd, int rw, struct obdo *oa,
491 struct lov_stripe_md *lsm, obd_off offset,
492 obd_size count, struct obd_trans_info *oti)
494 struct echo_client_obd *ec = &obd->u.echo_client;
496 struct brw_page *pga;
497 struct brw_page *pgp;
504 /* oa_id == ECHO_PERSISTENT_OBJID => speed test (no verification).
505 * oa & 1 => use HIGHMEM */
507 verify = (oa->o_id) != ECHO_PERSISTENT_OBJID;
508 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
510 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
513 (count & (PAGE_SIZE - 1)) != 0 ||
515 lsm->lsm_object_id != oa->o_id))
518 /* XXX think again with misaligned I/O */
519 npages = count >> PAGE_SHIFT;
521 OBD_ALLOC(pga, npages * sizeof(*pga));
525 for (i = 0, pgp = pga, off = offset;
527 i++, pgp++, off += PAGE_SIZE) {
529 LASSERT (pgp->pg == NULL); /* for cleanup */
532 pgp->pg = alloc_pages (gfp_mask, 0);
536 pgp->count = PAGE_SIZE;
541 echo_client_page_debug_setup(lsm, pgp->pg, rw,
542 oa->o_id, off, pgp->count);
545 rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
548 if (rc != 0 || rw != OBD_BRW_READ)
551 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
557 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
558 pgp->off, pgp->count);
559 if (vrc != 0 && rc == 0)
562 __free_pages(pgp->pg, 0);
564 OBD_FREE(pga, npages * sizeof(*pga));
569 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
570 static int echo_client_ubrw(struct obd_device *obd, int rw,
571 struct obdo *oa, struct lov_stripe_md *lsm,
572 obd_off offset, obd_size count, char *buffer,
573 struct obd_trans_info *oti)
575 struct echo_client_obd *ec = &obd->u.echo_client;
577 struct brw_page *pga;
578 struct brw_page *pgp;
580 struct kiobuf *kiobuf;
584 LASSERT (rw == OBD_BRW_WRITE ||
587 /* NB: for now, only whole pages, page aligned */
590 ((long)buffer & (PAGE_SIZE - 1)) != 0 ||
591 (count & (PAGE_SIZE - 1)) != 0 ||
592 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
595 /* XXX think again with misaligned I/O */
596 npages = count >> PAGE_SHIFT;
598 OBD_ALLOC(pga, npages * sizeof(*pga));
602 rc = alloc_kiovec (1, &kiobuf);
606 rc = map_user_kiobuf ((rw == OBD_BRW_READ) ? READ : WRITE,
607 kiobuf, (unsigned long)buffer, count);
611 LASSERT (kiobuf->offset == 0);
612 LASSERT (kiobuf->nr_pages == npages);
614 for (i = 0, off = offset, pgp = pga;
616 i++, off += PAGE_SIZE, pgp++) {
618 pgp->pg = kiobuf->maplist[i];
619 pgp->count = PAGE_SIZE;
623 rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
625 // if (rw == OBD_BRW_READ)
626 // mark_dirty_kiobuf (kiobuf, count);
628 unmap_kiobuf (kiobuf);
630 free_kiovec (1, &kiobuf);
632 OBD_FREE(pga, npages * sizeof(*pga));
636 static int echo_client_ubrw(struct obd_device *obd, int rw,
637 struct obdo *oa, struct lov_stripe_md *lsm,
638 obd_off offset, obd_size count, char *buffer,
639 struct obd_trans_info *oti)
641 #warning "echo_client_ubrw() needs to be ported on 2.6 yet"
648 struct echo_async_state;
650 #define EAP_MAGIC 79277927
651 struct echo_async_page {
653 struct page *eap_page;
656 struct echo_async_state *eap_eas;
657 struct list_head eap_item;
660 struct echo_async_state {
662 obd_off eas_next_offset;
663 obd_off eas_end_offset;
666 wait_queue_head_t eas_waitq;
667 struct list_head eas_avail;
669 struct lov_stripe_md *eas_lsm;
672 static int eas_should_wake(struct echo_async_state *eas)
676 spin_lock_irqsave(&eas->eas_lock, flags);
677 if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
679 spin_unlock_irqrestore(&eas->eas_lock, flags);
683 struct echo_async_page *eap_from_cookie(void *cookie)
685 struct echo_async_page *eap = cookie;
686 if (eap->eap_magic != EAP_MAGIC)
687 return ERR_PTR(-EINVAL);
691 static int ec_ap_make_ready(void *data, int cmd)
693 /* our pages are issued ready */
697 static int ec_ap_refresh_count(void *data, int cmd)
699 /* our pages are issued with a stable count */
703 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
705 struct echo_async_page *eap;
706 eap = eap_from_cookie(data);
710 memcpy(oa, &eap->eap_eas->eas_oa, sizeof(*oa));
712 static void ec_ap_completion(void *data, int cmd, int rc)
714 struct echo_async_page *eap = eap_from_cookie(data);
715 struct echo_async_state *eas;
722 if (cmd == OBD_BRW_READ &&
723 eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID)
724 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
725 eas->eas_oa.o_id, eap->eap_off,
728 spin_lock_irqsave(&eas->eas_lock, flags);
729 if (rc && !eas->eas_rc)
731 eas->eas_in_flight--;
732 list_add(&eap->eap_item, &eas->eas_avail);
733 wake_up(&eas->eas_waitq);
734 spin_unlock_irqrestore(&eas->eas_lock, flags);
737 static struct obd_async_page_ops ec_async_page_ops = {
738 .ap_make_ready = ec_ap_make_ready,
739 .ap_refresh_count = ec_ap_refresh_count,
740 .ap_fill_obdo = ec_ap_fill_obdo,
741 .ap_completion = ec_ap_completion,
744 static int echo_client_async_page(struct obd_export *exp, int rw,
745 struct obdo *oa, struct lov_stripe_md *lsm,
746 obd_off offset, obd_size count,
750 struct echo_async_page *eap;
751 struct echo_async_state eas;
752 struct list_head *pos, *n;
759 /* oa_id == 0 => speed test (no verification) else...
760 * oa & 1 => use HIGHMEM
762 verify = (oa->o_id != 0);
763 gfp_mask = ((oa->o_id & 1) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
766 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
769 (count & (PAGE_SIZE - 1)) != 0 ||
771 lsm->lsm_object_id != oa->o_id))
774 /* XXX think again with misaligned I/O */
775 npages = batching >> PAGE_SHIFT;
777 memcpy(&eas.eas_oa, oa, sizeof(*oa));
778 eas.eas_next_offset = offset;
779 eas.eas_end_offset = offset + count;
780 spin_lock_init(&eas.eas_lock);
781 init_waitqueue_head(&eas.eas_waitq);
782 eas.eas_in_flight = 0;
785 INIT_LIST_HEAD(&eas.eas_avail);
787 /* prepare the group of pages that we're going to be keeping
789 for (i = 0; i < npages; i++) {
790 struct page *page = alloc_page(GFP_KERNEL);
792 GOTO(out, rc = -ENOMEM);
795 list_add_tail(&page->list, &pages);
797 OBD_ALLOC(eap, sizeof(*eap));
799 GOTO(out, rc = -ENOMEM);
801 eap->eap_magic = EAP_MAGIC;
802 eap->eap_page = page;
804 page->private = (unsigned long)eap;
805 list_add_tail(&eap->eap_item, &eas.eas_avail);
808 /* first we spin queueing io and being woken by its completion */
809 spin_lock_irqsave(&eas.eas_lock, flags);
813 /* sleep until we have a page to send */
814 spin_unlock_irqrestore(&eas.eas_lock, flags);
815 rc = wait_event_interruptible(eas.eas_waitq,
816 eas_should_wake(&eas));
817 spin_lock_irqsave(&eas.eas_lock, flags);
818 if (rc && !eas.eas_rc)
822 if (list_empty(&eas.eas_avail))
824 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
826 list_del(&eap->eap_item);
827 spin_unlock_irqrestore(&eas.eas_lock, flags);
829 /* unbind the eap from its old page offset */
830 if (eap->eap_cookie != NULL) {
831 obd_teardown_async_page(exp, lsm, NULL,
833 eap->eap_cookie = NULL;
836 eas.eas_next_offset += PAGE_SIZE;
837 eap->eap_off = eas.eas_next_offset;
839 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
840 eap->eap_off, &ec_async_page_ops,
841 eap, &eap->eap_cookie);
843 spin_lock_irqsave(&eas.eas_lock, flags);
848 if (oa->o_id != ECHO_PERSISTENT_OBJID)
849 echo_client_page_debug_setup(lsm, eap->eap_page, rw,
851 eap->eap_off, PAGE_SIZE);
853 /* always asserts urgent, which isn't quite right */
854 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
856 ASYNC_READY | ASYNC_URGENT |
858 spin_lock_irqsave(&eas.eas_lock, flags);
859 if (rc && !eas.eas_rc) {
864 if (eas.eas_next_offset == eas.eas_end_offset)
868 /* still hold the eas_lock here.. */
870 /* now we just spin waiting for all the rpcs to complete */
871 while(eas.eas_in_flight) {
872 spin_unlock_irqrestore(&eas.eas_lock, flags);
873 wait_event_interruptible(eas.eas_waitq,
874 eas.eas_in_flight == 0);
875 spin_lock_irqsave(&eas.eas_lock, flags);
877 spin_unlock_irqrestore(&eas.eas_lock, flags);
880 list_for_each_safe(pos, n, &pages) {
881 struct page *page = list_entry(pos, struct page, list);
883 list_del(&page->list);
884 if (page->private != 0) {
885 eap = (struct echo_async_page *)page->private;
886 if (eap->eap_cookie != NULL)
887 obd_teardown_async_page(exp, lsm, NULL,
889 OBD_FREE(eap, sizeof(*eap));
897 static int echo_client_prep_commit(struct obd_export *exp, int rw,
898 struct obdo *oa, struct lov_stripe_md *lsm,
899 obd_off offset, obd_size count,
900 obd_size batch, struct obd_trans_info *oti)
902 struct obd_ioobj ioo;
903 struct niobuf_local *lnb;
904 struct niobuf_remote *rnb;
906 obd_size npages, tot_pages;
910 if (count <= 0 || (count & (PAGE_SIZE - 1)) != 0 ||
911 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
914 npages = batch >> PAGE_SHIFT;
915 tot_pages = count >> PAGE_SHIFT;
917 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
918 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
920 if (lnb == NULL || rnb == NULL)
921 GOTO(out, ret = -ENOMEM);
923 obdo_to_ioobj(oa, &ioo);
927 for(; tot_pages; tot_pages -= npages) {
928 if (tot_pages < npages)
931 for (i = 0; i < npages; i++, off += PAGE_SIZE) {
933 rnb[i].len = PAGE_SIZE;
936 /* XXX this can't be the best.. */
937 memset(oti, 0, sizeof(*oti));
938 ioo.ioo_bufcnt = npages;
940 ret = obd_preprw(rw, exp, oa, 1, &ioo, npages, rnb, lnb, oti);
944 for (i = 0; i < npages; i++) {
945 struct page *page = lnb[i].page;
948 if (page == NULL && lnb[i].rc == 0)
951 if (oa->o_id == ECHO_PERSISTENT_OBJID)
954 if (rw == OBD_BRW_WRITE)
955 echo_client_page_debug_setup(lsm, page, rw,
960 echo_client_page_debug_check(lsm, page,
966 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
973 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
975 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
979 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
980 struct obd_ioctl_data *data)
982 struct obd_device *obd = class_exp2obd(exp);
983 struct echo_client_obd *ec = &obd->u.echo_client;
984 struct obd_trans_info dummy_oti;
985 struct ec_object *eco;
989 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
993 memset(&dummy_oti, 0, sizeof(dummy_oti));
995 data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
996 data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
997 data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
999 switch((long)data->ioc_pbuf1) {
1001 if (data->ioc_pbuf2 == NULL) { // NULL user data pointer
1002 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
1003 eco->eco_lsm, data->ioc_offset,
1004 data->ioc_count, &dummy_oti);
1007 rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
1008 eco->eco_lsm, data->ioc_offset,
1009 data->ioc_count, data->ioc_pbuf2,
1015 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
1016 eco->eco_lsm, data->ioc_offset,
1017 data->ioc_count, data->ioc_plen1);
1020 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1021 eco->eco_lsm, data->ioc_offset,
1022 data->ioc_count, data->ioc_plen1,
1028 echo_put_object(eco);
1033 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1034 void *data, int flag)
1036 struct ec_object *eco = (struct ec_object *)data;
1037 struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1038 struct lustre_handle lockh;
1039 struct list_head *el;
1043 ldlm_lock2handle (lock, &lockh);
1045 /* #ifdef this out if we're not feeling paranoid */
1046 spin_lock (&ec->ec_lock);
1047 list_for_each (el, &ec->ec_objects) {
1048 found = (eco == list_entry(el, struct ec_object,
1053 spin_unlock (&ec->ec_lock);
1057 case LDLM_CB_BLOCKING:
1058 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1059 eco->eco_id, lockh.cookie);
1060 rc = ldlm_cli_cancel (&lockh);
1062 CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1065 case LDLM_CB_CANCELING:
1066 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1067 eco->eco_id, lockh.cookie);
1078 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1079 int mode, obd_off offset, obd_size nob)
1081 struct obd_device *obd = exp->exp_obd;
1082 struct echo_client_obd *ec = &obd->u.echo_client;
1083 struct lustre_handle *ulh = obdo_handle (oa);
1084 struct ec_object *eco;
1085 struct ec_lock *ecl;
1089 if (!(mode == LCK_PR || mode == LCK_PW))
1092 if ((offset & (PAGE_SIZE - 1)) != 0 ||
1093 (nob & (PAGE_SIZE - 1)) != 0)
1096 rc = echo_get_object (&eco, obd, oa);
1101 OBD_ALLOC (ecl, sizeof (*ecl));
1105 ecl->ecl_mode = mode;
1106 ecl->ecl_object = eco;
1107 ecl->ecl_policy.l_extent.start = offset;
1108 ecl->ecl_policy.l_extent.end =
1109 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1112 rc = obd_enqueue(ec->ec_exp, eco->eco_lsm, LDLM_EXTENT,
1113 &ecl->ecl_policy, mode, &flags, echo_ldlm_callback,
1114 ldlm_completion_ast, NULL, eco, sizeof(struct ost_lvb),
1115 lustre_swab_ost_lvb, &ecl->ecl_lock_handle);
1119 CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1121 /* NB ecl takes object ref from echo_get_object() above */
1122 spin_lock(&ec->ec_lock);
1124 list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1125 ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1127 spin_unlock(&ec->ec_lock);
1129 oa->o_valid |= OBD_MD_FLHANDLE;
1133 OBD_FREE (ecl, sizeof (*ecl));
1135 echo_put_object (eco);
1140 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1142 struct obd_device *obd = exp->exp_obd;
1143 struct echo_client_obd *ec = &obd->u.echo_client;
1144 struct lustre_handle *ulh = obdo_handle (oa);
1145 struct ec_lock *ecl = NULL;
1147 struct list_head *el;
1150 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1153 spin_lock (&ec->ec_lock);
1155 list_for_each (el, &exp->exp_ec_data.eced_locks) {
1156 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1157 found = (ecl->ecl_cookie == ulh->cookie);
1159 list_del (&ecl->ecl_exp_chain);
1164 spin_unlock (&ec->ec_lock);
1169 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1170 &ecl->ecl_lock_handle);
1172 echo_put_object (ecl->ecl_object);
1173 OBD_FREE (ecl, sizeof (*ecl));
1179 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1180 int len, void *karg, void *uarg)
1182 struct obd_device *obd;
1183 struct echo_client_obd *ec;
1184 struct ec_object *eco;
1185 struct obd_ioctl_data *data = karg;
1186 struct obd_trans_info dummy_oti;
1187 struct oti_req_ack_lock *ack_lock;
1189 int rw = OBD_BRW_READ;
1194 memset(&dummy_oti, 0, sizeof(dummy_oti));
1197 ec = &obd->u.echo_client;
1200 case OBD_IOC_CREATE: /* may create echo object */
1201 if (!capable (CAP_SYS_ADMIN))
1202 GOTO (out, rc = -EPERM);
1204 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1205 data->ioc_pbuf1, data->ioc_plen1,
1209 case OBD_IOC_DESTROY:
1210 if (!capable (CAP_SYS_ADMIN))
1211 GOTO (out, rc = -EPERM);
1213 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1215 oa = &data->ioc_obdo1;
1216 oa->o_gr = FILTER_GROUP_ECHO;
1217 oa->o_valid |= OBD_MD_FLGROUP;
1218 rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1221 eco->eco_deleted = 1;
1222 echo_put_object(eco);
1226 case OBD_IOC_GETATTR:
1227 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1229 rc = obd_getattr(ec->ec_exp, &data->ioc_obdo1,
1231 echo_put_object(eco);
1235 case OBD_IOC_SETATTR:
1236 if (!capable (CAP_SYS_ADMIN))
1237 GOTO (out, rc = -EPERM);
1239 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1241 rc = obd_setattr(ec->ec_exp, &data->ioc_obdo1,
1242 eco->eco_lsm, NULL);
1243 echo_put_object(eco);
1247 case OBD_IOC_BRW_WRITE:
1248 if (!capable (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 (!capable (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 (!capable (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);
1316 echo_client_setup(struct obd_device *obddev, obd_count len, void *buf)
1318 struct lustre_cfg* lcfg = buf;
1319 struct echo_client_obd *ec = &obddev->u.echo_client;
1320 struct obd_device *tgt;
1321 struct lustre_handle conn = {0, };
1322 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1326 if (lcfg->lcfg_inllen1 < 1) {
1327 CERROR("requires a TARGET OBD name\n");
1331 tgt = class_name2obd(lcfg->lcfg_inlbuf1);
1332 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1333 CERROR("device not attached or not set up (%s)\n",
1334 lcfg->lcfg_inlbuf1);
1338 spin_lock_init (&ec->ec_lock);
1339 INIT_LIST_HEAD (&ec->ec_objects);
1342 rc = obd_connect(&conn, tgt, &echo_uuid);
1344 CERROR("fail to connect to device %s\n", lcfg->lcfg_inlbuf1);
1347 ec->ec_exp = class_conn2export(&conn);
1352 static int echo_client_cleanup(struct obd_device *obddev, int flags)
1354 struct list_head *el;
1355 struct ec_object *eco;
1356 struct echo_client_obd *ec = &obddev->u.echo_client;
1360 if (!list_empty(&obddev->obd_exports)) {
1361 CERROR("still has clients!\n");
1365 /* XXX assuming sole access */
1366 while (!list_empty(&ec->ec_objects)) {
1367 el = ec->ec_objects.next;
1368 eco = list_entry(el, struct ec_object, eco_obj_chain);
1370 LASSERT(eco->eco_refcount == 0);
1371 eco->eco_refcount = 1;
1372 eco->eco_deleted = 1;
1373 echo_put_object(eco);
1376 rc = obd_disconnect(ec->ec_exp, 0);
1378 CERROR("fail to disconnect device: %d\n", rc);
1383 static int echo_client_connect(struct lustre_handle *conn,
1384 struct obd_device *src, struct obd_uuid *cluuid)
1386 struct obd_export *exp;
1389 rc = class_connect(conn, src, cluuid);
1391 exp = class_conn2export(conn);
1392 INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1393 class_export_put(exp);
1399 static int echo_client_disconnect(struct obd_export *exp, int flags)
1401 struct obd_device *obd;
1402 struct echo_client_obd *ec;
1403 struct ec_lock *ecl;
1408 GOTO(out, rc = -EINVAL);
1411 ec = &obd->u.echo_client;
1413 /* no more contention on export's lock list */
1414 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1415 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1416 struct ec_lock, ecl_exp_chain);
1417 list_del (&ecl->ecl_exp_chain);
1419 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1420 ecl->ecl_mode, &ecl->ecl_lock_handle);
1422 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1423 "(%d)\n", ecl->ecl_object->eco_id, rc);
1425 echo_put_object (ecl->ecl_object);
1426 OBD_FREE (ecl, sizeof (*ecl));
1429 rc = class_disconnect(exp, 0);
1435 static struct obd_ops echo_obd_ops = {
1436 o_owner: THIS_MODULE,
1437 o_setup: echo_client_setup,
1438 o_cleanup: echo_client_cleanup,
1439 o_iocontrol: echo_client_iocontrol,
1440 o_connect: echo_client_connect,
1441 o_disconnect: echo_client_disconnect
1444 int echo_client_init(void)
1446 struct lprocfs_static_vars lvars;
1448 lprocfs_init_vars(echo, &lvars);
1449 return class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
1450 OBD_ECHO_CLIENT_DEVICENAME);
1453 void echo_client_exit(void)
1455 class_unregister_type(OBD_ECHO_CLIENT_DEVICENAME);