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>
32 #include <linux/smp_lock.h>
34 #include <liblustre.h>
37 #include <linux/obd.h>
38 #include <linux/obd_support.h>
39 #include <linux/obd_class.h>
40 #include <linux/obd_echo.h>
41 #include <linux/lustre_debug.h>
42 #include <linux/lprocfs_status.h>
44 static obd_id last_object_id;
48 echo_printk_object (char *msg, struct ec_object *eco)
50 struct lov_stripe_md *lsm = eco->eco_lsm;
53 printk (KERN_INFO "Lustre: %s: object %p: "LPX64", refs %d%s: "LPX64
54 "=%u!%u\n", msg, eco, eco->eco_id, eco->eco_refcount,
55 eco->eco_deleted ? "(deleted) " : "",
56 lsm->lsm_object_id, lsm->lsm_stripe_size,
57 lsm->lsm_stripe_count);
59 for (i = 0; i < lsm->lsm_stripe_count; i++)
60 printk (KERN_INFO "Lustre: @%2u:"LPX64"\n",
61 lsm->lsm_oinfo[i].loi_ost_idx,
62 lsm->lsm_oinfo[i].loi_id);
66 static struct ec_object *
67 echo_find_object_locked (struct obd_device *obd, obd_id id)
69 struct echo_client_obd *ec = &obd->u.echo_client;
70 struct ec_object *eco = NULL;
73 list_for_each (el, &ec->ec_objects) {
74 eco = list_entry (el, struct ec_object, eco_obj_chain);
76 if (eco->eco_id == id)
83 echo_copyout_lsm (struct lov_stripe_md *lsm, void *ulsm, int ulsm_nob)
87 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
91 if (copy_to_user (ulsm, lsm, nob))
98 echo_copyin_lsm (struct obd_device *obd, struct lov_stripe_md *lsm,
99 void *ulsm, int ulsm_nob)
101 struct echo_client_obd *ec = &obd->u.echo_client;
104 if (ulsm_nob < sizeof (*lsm))
107 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
110 nob = lsm->lsm_stripe_count * sizeof (lsm->lsm_oinfo[0]);
112 if (ulsm_nob < nob ||
113 lsm->lsm_stripe_count > ec->ec_nstripes ||
114 lsm->lsm_magic != LOV_MAGIC ||
115 (lsm->lsm_stripe_size & (PAGE_SIZE - 1)) != 0 ||
116 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
119 if (copy_from_user(lsm->lsm_oinfo,
120 ((struct lov_stripe_md *)ulsm)->lsm_oinfo, nob))
126 static struct ec_object *
127 echo_allocate_object (struct obd_device *obd)
129 struct echo_client_obd *ec = &obd->u.echo_client;
130 struct ec_object *eco;
133 OBD_ALLOC(eco, sizeof (*eco));
137 rc = obd_alloc_memmd(ec->ec_exp, &eco->eco_lsm);
139 OBD_FREE(eco, sizeof (*eco));
143 eco->eco_device = obd;
144 eco->eco_deleted = 0;
145 eco->eco_refcount = 0;
146 eco->eco_lsm->lsm_magic = LOV_MAGIC;
147 /* leave stripe count 0 by default */
153 echo_free_object (struct ec_object *eco)
155 struct obd_device *obd = eco->eco_device;
156 struct echo_client_obd *ec = &obd->u.echo_client;
158 LASSERT (eco->eco_refcount == 0);
159 obd_free_memmd(ec->ec_exp, &eco->eco_lsm);
160 OBD_FREE (eco, sizeof (*eco));
163 static int echo_create_object(struct obd_device *obd, int on_target,
164 struct obdo *oa, void *ulsm, int ulsm_nob,
165 struct obd_trans_info *oti)
167 struct echo_client_obd *ec = &obd->u.echo_client;
168 struct ec_object *eco2;
169 struct ec_object *eco;
170 struct lov_stripe_md *lsm;
174 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
175 (on_target || /* set_stripe */
176 ec->ec_nstripes != 0)) { /* LOV */
177 CERROR ("No valid oid\n");
182 eco = echo_allocate_object (obd);
188 rc = echo_copyin_lsm (obd, lsm, ulsm, ulsm_nob);
192 /* setup object ID here for !on_target and LOV hint */
193 if ((oa->o_valid & OBD_MD_FLID) != 0)
194 eco->eco_id = lsm->lsm_object_id = oa->o_id;
196 if (lsm->lsm_stripe_count == 0)
197 lsm->lsm_stripe_count = ec->ec_nstripes;
199 if (lsm->lsm_stripe_size == 0)
200 lsm->lsm_stripe_size = PAGE_SIZE;
202 idx = ll_insecure_random_int();
204 /* setup stripes: indices + default ids if required */
205 for (i = 0; i < lsm->lsm_stripe_count; i++) {
206 if (lsm->lsm_oinfo[i].loi_id == 0)
207 lsm->lsm_oinfo[i].loi_id = lsm->lsm_object_id;
209 lsm->lsm_oinfo[i].loi_ost_idx =
210 (idx + i) % ec->ec_nstripes;
213 OBD_ALLOC(eco, sizeof(*eco));
214 eco->eco_device = obd;
219 oa->o_id = ++last_object_id;
222 /* XXX get some filter group constants */
224 oa->o_valid |= OBD_MD_FLGROUP;
225 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
229 /* See what object ID we were given */
230 eco->eco_id = oa->o_id = lsm->lsm_object_id;
231 oa->o_valid |= OBD_MD_FLID;
233 LASSERT(eco->eco_lsm == NULL || eco->eco_lsm == lsm);
237 spin_lock (&ec->ec_lock);
239 eco2 = echo_find_object_locked (obd, oa->o_id);
240 if (eco2 != NULL) { /* conflict */
241 spin_unlock (&ec->ec_lock);
243 CERROR ("Can't create object id "LPX64": id already exists%s\n",
244 oa->o_id, on_target ? " (undoing create)" : "");
247 obd_destroy(ec->ec_exp, oa, lsm, oti);
253 list_add (&eco->eco_obj_chain, &ec->ec_objects);
254 spin_unlock (&ec->ec_lock);
256 "created %p: "LPX64"=%u#%u@%u refs %d del %d\n",
258 eco->eco_lsm->lsm_stripe_size,
259 eco->eco_lsm->lsm_stripe_count,
260 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
261 eco->eco_refcount, eco->eco_deleted);
265 echo_free_object (eco);
270 echo_get_object (struct ec_object **ecop, struct obd_device *obd,
273 struct echo_client_obd *ec = &obd->u.echo_client;
274 struct ec_object *eco;
275 struct ec_object *eco2;
278 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
279 oa->o_id == 0) /* disallow use of object id 0 */
281 CERROR ("No valid oid\n");
285 spin_lock (&ec->ec_lock);
286 eco = echo_find_object_locked (obd, oa->o_id);
288 if (eco->eco_deleted) /* being deleted */
289 return (-EAGAIN); /* (see comment in cleanup) */
292 spin_unlock (&ec->ec_lock);
295 "found %p: "LPX64"=%u#%u@%u refs %d del %d\n",
297 eco->eco_lsm->lsm_stripe_size,
298 eco->eco_lsm->lsm_stripe_count,
299 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
300 eco->eco_refcount, eco->eco_deleted);
303 spin_unlock (&ec->ec_lock);
305 if (ec->ec_nstripes != 0) /* striping required */
308 eco = echo_allocate_object (obd);
312 eco->eco_id = eco->eco_lsm->lsm_object_id = oa->o_id;
314 spin_lock (&ec->ec_lock);
316 eco2 = echo_find_object_locked (obd, oa->o_id);
317 if (eco2 == NULL) { /* didn't race */
318 list_add (&eco->eco_obj_chain, &ec->ec_objects);
319 spin_unlock (&ec->ec_lock);
320 eco->eco_refcount = 1;
323 "created %p: "LPX64"=%u#%u@%d refs %d del %d\n",
325 eco->eco_lsm->lsm_stripe_size,
326 eco->eco_lsm->lsm_stripe_count,
327 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
328 eco->eco_refcount, eco->eco_deleted);
332 if (eco2->eco_deleted)
333 rc = -EAGAIN; /* lose race */
335 eco2->eco_refcount++; /* take existing */
338 LASSERT (eco2->eco_id == eco2->eco_lsm->lsm_object_id);
340 "found(2) %p: "LPX64"=%u#%u@%d refs %d del %d\n",
342 eco2->eco_lsm->lsm_stripe_size,
343 eco2->eco_lsm->lsm_stripe_count,
344 eco2->eco_lsm->lsm_oinfo[0].loi_ost_idx,
345 eco2->eco_refcount, eco2->eco_deleted);
348 spin_unlock (&ec->ec_lock);
350 echo_free_object (eco);
355 echo_put_object (struct ec_object *eco)
357 struct obd_device *obd = eco->eco_device;
358 struct echo_client_obd *ec = &obd->u.echo_client;
360 /* Release caller's ref on the object.
361 * delete => mark for deletion when last ref goes
364 spin_lock (&ec->ec_lock);
367 LASSERT (eco->eco_refcount >= 0);
369 CDEBUG(D_INFO, "put %p: "LPX64"=%u#%u@%d refs %d del %d\n",
371 eco->eco_lsm->lsm_stripe_size,
372 eco->eco_lsm->lsm_stripe_count,
373 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
374 eco->eco_refcount, eco->eco_deleted);
376 if (eco->eco_refcount != 0 || !eco->eco_deleted) {
377 spin_unlock (&ec->ec_lock);
381 spin_unlock (&ec->ec_lock);
383 /* NB leave obj in the object list. We must prevent anyone from
384 * attempting to enqueue on this object number until we can be
385 * sure there will be no more lock callbacks.
387 obd_cancel_unused(ec->ec_exp, eco->eco_lsm, 0, NULL);
389 /* now we can let it go */
390 spin_lock (&ec->ec_lock);
391 list_del (&eco->eco_obj_chain);
392 spin_unlock (&ec->ec_lock);
394 LASSERT (eco->eco_refcount == 0);
396 echo_free_object (eco);
400 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
402 unsigned long stripe_count;
403 unsigned long stripe_size;
405 unsigned long woffset;
409 if (lsm->lsm_stripe_count <= 1)
413 stripe_size = lsm->lsm_stripe_size;
414 stripe_count = lsm->lsm_stripe_count;
416 /* width = # bytes in all stripes */
417 width = stripe_size * stripe_count;
419 /* woffset = offset within a width; offset = whole number of widths */
420 woffset = do_div (offset, width);
422 stripe_index = woffset / stripe_size;
424 *idp = lsm->lsm_oinfo[stripe_index].loi_id;
425 *offp = offset * stripe_size + woffset % stripe_size;
429 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
430 struct page *page, int rw, obd_id id,
431 obd_off offset, obd_off count)
438 /* no partial pages on the client */
439 LASSERT(count == PAGE_SIZE);
443 for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
444 if (rw == OBD_BRW_WRITE) {
445 stripe_off = offset + delta;
447 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
449 stripe_off = 0xdeadbeef00c0ffeeULL;
450 stripe_id = 0xdeadbeef00c0ffeeULL;
452 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
453 stripe_off, stripe_id);
460 echo_client_page_debug_check(struct lov_stripe_md *lsm,
461 struct page *page, obd_id id,
462 obd_off offset, obd_off count)
471 /* no partial pages on the client */
472 LASSERT(count == PAGE_SIZE);
476 for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
477 stripe_off = offset + delta;
479 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
481 rc2 = block_debug_check("test_brw",
482 addr + delta, OBD_ECHO_BLOCK_SIZE,
483 stripe_off, stripe_id);
492 static int echo_client_kbrw(struct obd_device *obd, int rw, struct obdo *oa,
493 struct lov_stripe_md *lsm, obd_off offset,
494 obd_size count, struct obd_trans_info *oti)
496 struct echo_client_obd *ec = &obd->u.echo_client;
498 struct brw_page *pga;
499 struct brw_page *pgp;
506 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
507 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
508 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
510 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
512 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
515 (count & (PAGE_SIZE - 1)) != 0 ||
517 lsm->lsm_object_id != oa->o_id))
520 /* XXX think again with misaligned I/O */
521 npages = count >> PAGE_SHIFT;
523 OBD_ALLOC(pga, npages * sizeof(*pga));
527 for (i = 0, pgp = pga, off = offset;
529 i++, pgp++, off += PAGE_SIZE) {
531 LASSERT (pgp->pg == NULL); /* for cleanup */
534 pgp->pg = alloc_pages (gfp_mask, 0);
538 pgp->count = PAGE_SIZE;
543 echo_client_page_debug_setup(lsm, pgp->pg, rw,
544 oa->o_id, off, pgp->count);
547 rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
550 if (rc != 0 || rw != OBD_BRW_READ)
553 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
559 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
560 pgp->off, pgp->count);
561 if (vrc != 0 && rc == 0)
564 __free_pages(pgp->pg, 0);
566 OBD_FREE(pga, npages * sizeof(*pga));
571 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
572 static int echo_client_ubrw(struct obd_device *obd, int rw,
573 struct obdo *oa, struct lov_stripe_md *lsm,
574 obd_off offset, obd_size count, char *buffer,
575 struct obd_trans_info *oti)
577 struct echo_client_obd *ec = &obd->u.echo_client;
579 struct brw_page *pga;
580 struct brw_page *pgp;
582 struct kiobuf *kiobuf;
586 LASSERT (rw == OBD_BRW_WRITE ||
589 /* NB: for now, only whole pages, page aligned */
592 ((long)buffer & (PAGE_SIZE - 1)) != 0 ||
593 (count & (PAGE_SIZE - 1)) != 0 ||
594 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
597 /* XXX think again with misaligned I/O */
598 npages = count >> PAGE_SHIFT;
600 OBD_ALLOC(pga, npages * sizeof(*pga));
604 rc = alloc_kiovec (1, &kiobuf);
608 rc = map_user_kiobuf ((rw == OBD_BRW_READ) ? READ : WRITE,
609 kiobuf, (unsigned long)buffer, count);
613 LASSERT (kiobuf->offset == 0);
614 LASSERT (kiobuf->nr_pages == npages);
616 for (i = 0, off = offset, pgp = pga;
618 i++, off += PAGE_SIZE, pgp++) {
620 pgp->pg = kiobuf->maplist[i];
621 pgp->count = PAGE_SIZE;
625 rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
627 // if (rw == OBD_BRW_READ)
628 // mark_dirty_kiobuf (kiobuf, count);
630 unmap_kiobuf (kiobuf);
632 free_kiovec (1, &kiobuf);
634 OBD_FREE(pga, npages * sizeof(*pga));
638 static int echo_client_ubrw(struct obd_device *obd, int rw,
639 struct obdo *oa, struct lov_stripe_md *lsm,
640 obd_off offset, obd_size count, char *buffer,
641 struct obd_trans_info *oti)
643 #warning "echo_client_ubrw() needs to be ported on 2.6 yet"
650 struct echo_async_state;
652 #define EAP_MAGIC 79277927
653 struct echo_async_page {
655 struct page *eap_page;
658 struct echo_async_state *eap_eas;
659 struct list_head eap_item;
662 #define EAP_FROM_COOKIE(c) \
663 (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC), \
664 (struct echo_async_page *)(c))
666 struct echo_async_state {
668 obd_off eas_next_offset;
669 obd_off eas_end_offset;
672 wait_queue_head_t eas_waitq;
673 struct list_head eas_avail;
675 struct lov_stripe_md *eas_lsm;
678 static int eas_should_wake(struct echo_async_state *eas)
682 spin_lock_irqsave(&eas->eas_lock, flags);
683 if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
685 spin_unlock_irqrestore(&eas->eas_lock, flags);
689 static int ec_ap_make_ready(void *data, int cmd)
691 /* our pages are issued ready */
695 static int ec_ap_refresh_count(void *data, int cmd)
697 /* our pages are issued with a stable count */
701 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
703 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
705 memcpy(oa, &eap->eap_eas->eas_oa, sizeof(*oa));
708 static void ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
710 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
711 struct echo_async_state *eas;
716 if (cmd == OBD_BRW_READ &&
717 eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
718 (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
719 (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
720 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
721 eas->eas_oa.o_id, eap->eap_off,
724 spin_lock_irqsave(&eas->eas_lock, flags);
725 if (rc && !eas->eas_rc)
727 eas->eas_in_flight--;
728 list_add(&eap->eap_item, &eas->eas_avail);
729 wake_up(&eas->eas_waitq);
730 spin_unlock_irqrestore(&eas->eas_lock, flags);
733 static struct obd_async_page_ops ec_async_page_ops = {
734 .ap_make_ready = ec_ap_make_ready,
735 .ap_refresh_count = ec_ap_refresh_count,
736 .ap_fill_obdo = ec_ap_fill_obdo,
737 .ap_completion = ec_ap_completion,
740 static int echo_client_async_page(struct obd_export *exp, int rw,
741 struct obdo *oa, struct lov_stripe_md *lsm,
742 obd_off offset, obd_size count,
746 struct echo_async_page *eap;
747 struct echo_async_state eas;
748 struct list_head *pos, *n;
756 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
757 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
758 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
760 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
763 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
766 (count & (PAGE_SIZE - 1)) != 0 ||
768 lsm->lsm_object_id != oa->o_id))
771 /* XXX think again with misaligned I/O */
772 npages = batching >> PAGE_SHIFT;
774 memcpy(&eas.eas_oa, oa, sizeof(*oa));
775 eas.eas_next_offset = offset;
776 eas.eas_end_offset = offset + count;
777 spin_lock_init(&eas.eas_lock);
778 init_waitqueue_head(&eas.eas_waitq);
779 eas.eas_in_flight = 0;
782 INIT_LIST_HEAD(&eas.eas_avail);
784 /* prepare the group of pages that we're going to be keeping
786 for (i = 0; i < npages; i++) {
787 struct page *page = alloc_page(GFP_KERNEL);
789 GOTO(out, rc = -ENOMEM);
792 list_add_tail(&PAGE_LIST(page), &pages);
794 OBD_ALLOC(eap, sizeof(*eap));
796 GOTO(out, rc = -ENOMEM);
798 eap->eap_magic = EAP_MAGIC;
799 eap->eap_page = page;
801 page->private = (unsigned long)eap;
802 list_add_tail(&eap->eap_item, &eas.eas_avail);
805 /* first we spin queueing io and being woken by its completion */
806 spin_lock_irqsave(&eas.eas_lock, flags);
810 /* sleep until we have a page to send */
811 spin_unlock_irqrestore(&eas.eas_lock, flags);
812 rc = wait_event_interruptible(eas.eas_waitq,
813 eas_should_wake(&eas));
814 spin_lock_irqsave(&eas.eas_lock, flags);
815 if (rc && !eas.eas_rc)
819 if (list_empty(&eas.eas_avail))
821 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
823 list_del(&eap->eap_item);
824 spin_unlock_irqrestore(&eas.eas_lock, flags);
826 /* unbind the eap from its old page offset */
827 if (eap->eap_cookie != NULL) {
828 obd_teardown_async_page(exp, lsm, NULL,
830 eap->eap_cookie = NULL;
833 eas.eas_next_offset += PAGE_SIZE;
834 eap->eap_off = eas.eas_next_offset;
836 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
837 eap->eap_off, &ec_async_page_ops,
838 eap, &eap->eap_cookie);
840 spin_lock_irqsave(&eas.eas_lock, flags);
845 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
846 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
847 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
848 echo_client_page_debug_setup(lsm, eap->eap_page, rw,
850 eap->eap_off, PAGE_SIZE);
852 /* always asserts urgent, which isn't quite right */
853 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
855 ASYNC_READY | ASYNC_URGENT |
857 spin_lock_irqsave(&eas.eas_lock, flags);
858 if (rc && !eas.eas_rc) {
863 if (eas.eas_next_offset == eas.eas_end_offset)
867 /* still hold the eas_lock here.. */
869 /* now we just spin waiting for all the rpcs to complete */
870 while(eas.eas_in_flight) {
871 spin_unlock_irqrestore(&eas.eas_lock, flags);
872 wait_event_interruptible(eas.eas_waitq,
873 eas.eas_in_flight == 0);
874 spin_lock_irqsave(&eas.eas_lock, flags);
876 spin_unlock_irqrestore(&eas.eas_lock, flags);
879 list_for_each_safe(pos, n, &pages) {
880 struct page *page = list_entry(pos, struct page,
883 list_del(&PAGE_LIST(page));
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 ||
952 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
953 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
956 if (rw == OBD_BRW_WRITE)
957 echo_client_page_debug_setup(lsm, page, rw,
962 echo_client_page_debug_check(lsm, page,
968 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
975 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
977 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
981 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
982 struct obd_ioctl_data *data)
984 struct obd_device *obd = class_exp2obd(exp);
985 struct echo_client_obd *ec = &obd->u.echo_client;
986 struct obd_trans_info dummy_oti;
987 struct ec_object *eco;
991 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
995 memset(&dummy_oti, 0, sizeof(dummy_oti));
997 data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
998 data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
999 data->ioc_obdo1.o_gr = 2;
1001 switch((long)data->ioc_pbuf1) {
1003 if (data->ioc_pbuf2 == NULL) { // NULL user data pointer
1004 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
1005 eco->eco_lsm, data->ioc_offset,
1006 data->ioc_count, &dummy_oti);
1009 rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
1010 eco->eco_lsm, data->ioc_offset,
1011 data->ioc_count, data->ioc_pbuf2,
1017 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
1018 eco->eco_lsm, data->ioc_offset,
1019 data->ioc_count, data->ioc_plen1);
1022 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1023 eco->eco_lsm, data->ioc_offset,
1024 data->ioc_count, data->ioc_plen1,
1030 echo_put_object(eco);
1035 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1036 void *data, int flag)
1038 struct ec_object *eco = (struct ec_object *)data;
1039 struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1040 struct lustre_handle lockh;
1041 struct list_head *el;
1045 ldlm_lock2handle (lock, &lockh);
1047 /* #ifdef this out if we're not feeling paranoid */
1048 spin_lock (&ec->ec_lock);
1049 list_for_each (el, &ec->ec_objects) {
1050 found = (eco == list_entry(el, struct ec_object,
1055 spin_unlock (&ec->ec_lock);
1059 case LDLM_CB_BLOCKING:
1060 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1061 eco->eco_id, lockh.cookie);
1062 rc = ldlm_cli_cancel (&lockh);
1064 CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1067 case LDLM_CB_CANCELING:
1068 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1069 eco->eco_id, lockh.cookie);
1080 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1081 int mode, obd_off offset, obd_size nob)
1083 struct obd_device *obd = exp->exp_obd;
1084 struct echo_client_obd *ec = &obd->u.echo_client;
1085 struct lustre_handle *ulh = obdo_handle (oa);
1086 struct ec_object *eco;
1087 struct ec_lock *ecl;
1091 if (!(mode == LCK_PR || mode == LCK_PW))
1094 if ((offset & (PAGE_SIZE - 1)) != 0 ||
1095 (nob & (PAGE_SIZE - 1)) != 0)
1098 rc = echo_get_object (&eco, obd, oa);
1103 OBD_ALLOC (ecl, sizeof (*ecl));
1107 ecl->ecl_mode = mode;
1108 ecl->ecl_object = eco;
1109 ecl->ecl_policy.l_extent.start = offset;
1110 ecl->ecl_policy.l_extent.end =
1111 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1114 rc = obd_enqueue(ec->ec_exp, eco->eco_lsm, LDLM_EXTENT,
1115 &ecl->ecl_policy, mode, &flags, echo_ldlm_callback,
1116 ldlm_completion_ast, NULL, eco, sizeof(struct ost_lvb),
1117 lustre_swab_ost_lvb, &ecl->ecl_lock_handle);
1121 CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1123 /* NB ecl takes object ref from echo_get_object() above */
1124 spin_lock(&ec->ec_lock);
1126 list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1127 ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1129 spin_unlock(&ec->ec_lock);
1131 oa->o_valid |= OBD_MD_FLHANDLE;
1135 OBD_FREE (ecl, sizeof (*ecl));
1137 echo_put_object (eco);
1142 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1144 struct obd_device *obd = exp->exp_obd;
1145 struct echo_client_obd *ec = &obd->u.echo_client;
1146 struct lustre_handle *ulh = obdo_handle (oa);
1147 struct ec_lock *ecl = NULL;
1149 struct list_head *el;
1152 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1155 spin_lock (&ec->ec_lock);
1157 list_for_each (el, &exp->exp_ec_data.eced_locks) {
1158 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1159 found = (ecl->ecl_cookie == ulh->cookie);
1161 list_del (&ecl->ecl_exp_chain);
1166 spin_unlock (&ec->ec_lock);
1171 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1172 &ecl->ecl_lock_handle);
1174 echo_put_object (ecl->ecl_object);
1175 OBD_FREE (ecl, sizeof (*ecl));
1181 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1182 int len, void *karg, void *uarg)
1184 struct obd_device *obd;
1185 struct echo_client_obd *ec;
1186 struct ec_object *eco;
1187 struct obd_ioctl_data *data = karg;
1188 struct obd_trans_info dummy_oti;
1189 struct oti_req_ack_lock *ack_lock;
1191 int rw = OBD_BRW_READ;
1198 memset(&dummy_oti, 0, sizeof(dummy_oti));
1201 ec = &obd->u.echo_client;
1204 case OBD_IOC_CREATE: /* may create echo object */
1205 if (!capable (CAP_SYS_ADMIN))
1206 GOTO (out, rc = -EPERM);
1208 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1209 data->ioc_pbuf1, data->ioc_plen1,
1213 case OBD_IOC_DESTROY:
1214 if (!capable (CAP_SYS_ADMIN))
1215 GOTO (out, rc = -EPERM);
1217 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1219 oa = &data->ioc_obdo1;
1221 oa->o_valid |= OBD_MD_FLGROUP;
1222 rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1225 eco->eco_deleted = 1;
1226 echo_put_object(eco);
1230 case OBD_IOC_GETATTR:
1231 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1233 rc = obd_getattr(ec->ec_exp, &data->ioc_obdo1,
1235 echo_put_object(eco);
1239 case OBD_IOC_SETATTR:
1240 if (!capable (CAP_SYS_ADMIN))
1241 GOTO (out, rc = -EPERM);
1243 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1245 rc = obd_setattr(ec->ec_exp, &data->ioc_obdo1,
1246 eco->eco_lsm, NULL);
1247 echo_put_object(eco);
1251 case OBD_IOC_BRW_WRITE:
1252 if (!capable (CAP_SYS_ADMIN))
1253 GOTO (out, rc = -EPERM);
1257 case OBD_IOC_BRW_READ:
1258 rc = echo_client_brw_ioctl(rw, exp, data);
1261 case ECHO_IOC_GET_STRIPE:
1262 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1264 rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1266 echo_put_object(eco);
1270 case ECHO_IOC_SET_STRIPE:
1271 if (!capable (CAP_SYS_ADMIN))
1272 GOTO (out, rc = -EPERM);
1274 if (data->ioc_pbuf1 == NULL) { /* unset */
1275 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1277 eco->eco_deleted = 1;
1278 echo_put_object(eco);
1281 rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1283 data->ioc_plen1, &dummy_oti);
1287 case ECHO_IOC_ENQUEUE:
1288 if (!capable (CAP_SYS_ADMIN))
1289 GOTO (out, rc = -EPERM);
1291 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1292 data->ioc_conn1, /* lock mode */
1293 data->ioc_offset, data->ioc_count);/*extent*/
1296 case ECHO_IOC_CANCEL:
1297 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1301 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1302 GOTO (out, rc = -ENOTTY);
1308 /* XXX this should be in a helper also called by target_send_reply */
1309 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1311 if (!ack_lock->mode)
1313 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1322 echo_client_setup(struct obd_device *obddev, obd_count len, void *buf)
1324 struct lustre_cfg* lcfg = buf;
1325 struct echo_client_obd *ec = &obddev->u.echo_client;
1326 struct obd_device *tgt;
1327 struct lustre_handle conn = {0, };
1328 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1332 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1333 CERROR("requires a TARGET OBD name\n");
1337 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1338 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1339 CERROR("device not attached or not set up (%s)\n",
1340 lustre_cfg_string(lcfg, 1));
1344 spin_lock_init (&ec->ec_lock);
1345 INIT_LIST_HEAD (&ec->ec_objects);
1348 rc = obd_connect(&conn, tgt, &echo_uuid);
1350 CERROR("fail to connect to device %s\n",
1351 lustre_cfg_string(lcfg, 1));
1354 ec->ec_exp = class_conn2export(&conn);
1359 static int echo_client_cleanup(struct obd_device *obddev)
1361 struct list_head *el;
1362 struct ec_object *eco;
1363 struct echo_client_obd *ec = &obddev->u.echo_client;
1367 if (!list_empty(&obddev->obd_exports)) {
1368 CERROR("still has clients!\n");
1372 /* XXX assuming sole access */
1373 while (!list_empty(&ec->ec_objects)) {
1374 el = ec->ec_objects.next;
1375 eco = list_entry(el, struct ec_object, eco_obj_chain);
1377 LASSERT(eco->eco_refcount == 0);
1378 eco->eco_refcount = 1;
1379 eco->eco_deleted = 1;
1380 echo_put_object(eco);
1383 rc = obd_disconnect(ec->ec_exp);
1385 CERROR("fail to disconnect device: %d\n", rc);
1390 static int echo_client_connect(struct lustre_handle *conn,
1391 struct obd_device *src, struct obd_uuid *cluuid)
1393 struct obd_export *exp;
1396 rc = class_connect(conn, src, cluuid);
1398 exp = class_conn2export(conn);
1399 INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1400 class_export_put(exp);
1406 static int echo_client_disconnect(struct obd_export *exp)
1408 struct obd_device *obd;
1409 struct echo_client_obd *ec;
1410 struct ec_lock *ecl;
1415 GOTO(out, rc = -EINVAL);
1418 ec = &obd->u.echo_client;
1420 /* no more contention on export's lock list */
1421 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1422 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1423 struct ec_lock, ecl_exp_chain);
1424 list_del (&ecl->ecl_exp_chain);
1426 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1427 ecl->ecl_mode, &ecl->ecl_lock_handle);
1429 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1430 "(%d)\n", ecl->ecl_object->eco_id, rc);
1432 echo_put_object (ecl->ecl_object);
1433 OBD_FREE (ecl, sizeof (*ecl));
1436 rc = class_disconnect(exp);
1442 static struct obd_ops echo_obd_ops = {
1443 .o_owner = THIS_MODULE,
1444 .o_setup = echo_client_setup,
1445 .o_cleanup = echo_client_cleanup,
1446 .o_iocontrol = echo_client_iocontrol,
1447 .o_connect = echo_client_connect,
1448 .o_disconnect = echo_client_disconnect
1451 int echo_client_init(void)
1453 struct lprocfs_static_vars lvars;
1455 lprocfs_init_vars(echo, &lvars);
1456 return class_register_type(&echo_obd_ops, lvars.module_vars,
1457 OBD_ECHO_CLIENT_DEVICENAME);
1460 void echo_client_exit(void)
1462 class_unregister_type(OBD_ECHO_CLIENT_DEVICENAME);