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_ver.h>
37 #include <lustre_debug.h>
38 #include <lprocfs_status.h>
40 static obd_id last_object_id;
44 echo_printk_object (char *msg, struct ec_object *eco)
46 struct lov_stripe_md *lsm = eco->eco_lsm;
49 printk (KERN_INFO "Lustre: %s: object %p: "LPX64", refs %d%s: "LPX64
50 "=%u!%u\n", msg, eco, eco->eco_id, eco->eco_refcount,
51 eco->eco_deleted ? "(deleted) " : "",
52 lsm->lsm_object_id, lsm->lsm_stripe_size,
53 lsm->lsm_stripe_count);
55 for (i = 0; i < lsm->lsm_stripe_count; i++)
56 printk (KERN_INFO "Lustre: @%2u:"LPX64"\n",
57 lsm->lsm_oinfo[i].loi_ost_idx,
58 lsm->lsm_oinfo[i].loi_id);
62 static struct ec_object *
63 echo_find_object_locked (struct obd_device *obd, obd_id id)
65 struct echo_client_obd *ec = &obd->u.echo_client;
66 struct ec_object *eco = NULL;
69 list_for_each (el, &ec->ec_objects) {
70 eco = list_entry (el, struct ec_object, eco_obj_chain);
72 if (eco->eco_id == id)
79 echo_copyout_lsm (struct lov_stripe_md *lsm, void *ulsm, int ulsm_nob)
83 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
87 if (copy_to_user (ulsm, lsm, nob))
94 echo_copyin_lsm (struct obd_device *obd, struct lov_stripe_md *lsm,
95 void *ulsm, int ulsm_nob)
97 struct echo_client_obd *ec = &obd->u.echo_client;
100 if (ulsm_nob < sizeof (*lsm))
103 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
106 nob = lsm->lsm_stripe_count * sizeof (lsm->lsm_oinfo[0]);
108 if (ulsm_nob < nob ||
109 lsm->lsm_stripe_count > ec->ec_nstripes ||
110 lsm->lsm_magic != LOV_MAGIC ||
111 (lsm->lsm_stripe_size & (CFS_PAGE_SIZE - 1)) != 0 ||
112 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
115 if (copy_from_user(lsm->lsm_oinfo,
116 ((struct lov_stripe_md *)ulsm)->lsm_oinfo, nob))
122 static struct ec_object *
123 echo_allocate_object (struct obd_device *obd)
125 struct echo_client_obd *ec = &obd->u.echo_client;
126 struct ec_object *eco;
129 OBD_ALLOC(eco, sizeof (*eco));
133 rc = obd_alloc_memmd(ec->ec_exp, &eco->eco_lsm);
135 OBD_FREE(eco, sizeof (*eco));
139 eco->eco_device = obd;
140 eco->eco_deleted = 0;
141 eco->eco_refcount = 0;
142 eco->eco_lsm->lsm_magic = LOV_MAGIC;
143 /* leave stripe count 0 by default */
149 echo_free_object (struct ec_object *eco)
151 struct obd_device *obd = eco->eco_device;
152 struct echo_client_obd *ec = &obd->u.echo_client;
154 LASSERT (eco->eco_refcount == 0);
155 obd_free_memmd(ec->ec_exp, &eco->eco_lsm);
156 OBD_FREE (eco, sizeof (*eco));
159 static int echo_create_object(struct obd_device *obd, int on_target,
160 struct obdo *oa, void *ulsm, int ulsm_nob,
161 struct obd_trans_info *oti)
163 struct echo_client_obd *ec = &obd->u.echo_client;
164 struct ec_object *eco2;
165 struct ec_object *eco;
166 struct lov_stripe_md *lsm;
170 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
171 (on_target || /* set_stripe */
172 ec->ec_nstripes != 0)) { /* LOV */
173 CERROR ("No valid oid\n");
178 eco = echo_allocate_object (obd);
184 rc = echo_copyin_lsm (obd, lsm, ulsm, ulsm_nob);
188 /* setup object ID here for !on_target and LOV hint */
189 if ((oa->o_valid & OBD_MD_FLID) != 0)
190 eco->eco_id = lsm->lsm_object_id = oa->o_id;
192 if (lsm->lsm_stripe_count == 0)
193 lsm->lsm_stripe_count = ec->ec_nstripes;
195 if (lsm->lsm_stripe_size == 0)
196 lsm->lsm_stripe_size = CFS_PAGE_SIZE;
200 /* setup stripes: indices + default ids if required */
201 for (i = 0; i < lsm->lsm_stripe_count; i++) {
202 if (lsm->lsm_oinfo[i].loi_id == 0)
203 lsm->lsm_oinfo[i].loi_id = lsm->lsm_object_id;
205 lsm->lsm_oinfo[i].loi_ost_idx =
206 (idx + i) % ec->ec_nstripes;
209 OBD_ALLOC(eco, sizeof(*eco));
210 eco->eco_device = obd;
215 oa->o_id = ++last_object_id;
218 oa->o_gr = FILTER_GROUP_ECHO;
219 oa->o_valid |= OBD_MD_FLGROUP;
221 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
225 /* See what object ID we were given */
226 eco->eco_id = oa->o_id = lsm->lsm_object_id;
227 oa->o_valid |= OBD_MD_FLID;
229 LASSERT(eco->eco_lsm == NULL || eco->eco_lsm == lsm);
233 spin_lock (&ec->ec_lock);
235 eco2 = echo_find_object_locked (obd, oa->o_id);
236 if (eco2 != NULL) { /* conflict */
237 spin_unlock (&ec->ec_lock);
239 CERROR ("Can't create object id "LPX64": id already exists%s\n",
240 oa->o_id, on_target ? " (undoing create)" : "");
243 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL);
249 list_add (&eco->eco_obj_chain, &ec->ec_objects);
250 spin_unlock (&ec->ec_lock);
252 "created %p: "LPX64"=%u#%u@%u refs %d del %d\n",
254 eco->eco_lsm->lsm_stripe_size,
255 eco->eco_lsm->lsm_stripe_count,
256 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
257 eco->eco_refcount, eco->eco_deleted);
261 echo_free_object (eco);
266 echo_get_object (struct ec_object **ecop, struct obd_device *obd,
269 struct echo_client_obd *ec = &obd->u.echo_client;
270 struct ec_object *eco;
271 struct ec_object *eco2;
274 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
275 oa->o_id == 0) /* disallow use of object id 0 */
277 CERROR ("No valid oid\n");
281 spin_lock (&ec->ec_lock);
282 eco = echo_find_object_locked (obd, oa->o_id);
284 if (eco->eco_deleted) { /* being deleted */
285 spin_unlock(&ec->ec_lock);/* (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 cfs_page_t *page, int rw, obd_id id,
429 obd_off offset, obd_off count)
436 /* no partial pages on the client */
437 LASSERT(count == CFS_PAGE_SIZE);
439 addr = cfs_kmap(page);
441 for (delta = 0; delta < CFS_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);
457 static int echo_client_page_debug_check(struct lov_stripe_md *lsm,
458 cfs_page_t *page, obd_id id,
459 obd_off offset, obd_off count)
468 /* no partial pages on the client */
469 LASSERT(count == CFS_PAGE_SIZE);
471 addr = cfs_kmap(page);
473 for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
474 stripe_off = offset + delta;
476 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
478 rc2 = block_debug_check("test_brw",
479 addr + delta, OBD_ECHO_BLOCK_SIZE,
480 stripe_off, stripe_id);
482 CERROR ("Error in echo object "LPX64"\n", 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;
496 struct obd_info oinfo = { { { 0 } } };
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) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
512 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
513 LASSERT(lsm != NULL);
514 LASSERT(lsm->lsm_object_id == oa->o_id);
517 (count & (CFS_PAGE_SIZE - 1)) != 0)
520 /* XXX think again with misaligned I/O */
521 npages = count >> CFS_PAGE_SHIFT;
523 OBD_ALLOC(pga, npages * sizeof(*pga));
527 for (i = 0, pgp = pga, off = offset;
529 i++, pgp++, off += CFS_PAGE_SIZE) {
531 LASSERT (pgp->pg == NULL); /* for cleanup */
534 pgp->pg = cfs_alloc_page (gfp_mask);
538 pgp->count = CFS_PAGE_SIZE;
543 echo_client_page_debug_setup(lsm, pgp->pg, rw,
544 oa->o_id, off, pgp->count);
549 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
552 if (rc != 0 || rw != OBD_BRW_READ)
555 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
561 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
562 pgp->off, pgp->count);
563 if (vrc != 0 && rc == 0)
566 cfs_free_page(pgp->pg);
568 OBD_FREE(pga, npages * sizeof(*pga));
573 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
574 static int echo_client_ubrw(struct obd_device *obd, int rw,
575 struct obdo *oa, struct lov_stripe_md *lsm,
576 obd_off offset, obd_size count, char *buffer,
577 struct obd_trans_info *oti)
579 struct echo_client_obd *ec = &obd->u.echo_client;
580 struct obd_info oinfo = { { { 0 } } };
582 struct brw_page *pga;
583 struct brw_page *pgp;
585 struct kiobuf *kiobuf;
589 LASSERT (rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
591 /* NB: for now, only whole pages, page aligned */
594 ((long)buffer & (CFS_PAGE_SIZE - 1)) != 0 ||
595 (count & (CFS_PAGE_SIZE - 1)) != 0 ||
596 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
599 /* XXX think again with misaligned I/O */
600 npages = count >> CFS_PAGE_SHIFT;
602 OBD_ALLOC(pga, npages * sizeof(*pga));
606 rc = alloc_kiovec (1, &kiobuf);
610 rc = map_user_kiobuf ((rw == OBD_BRW_READ) ? READ : WRITE,
611 kiobuf, (unsigned long)buffer, count);
615 LASSERT (kiobuf->offset == 0);
616 LASSERT (kiobuf->nr_pages == npages);
618 for (i = 0, off = offset, pgp = pga;
620 i++, off += CFS_PAGE_SIZE, pgp++) {
622 pgp->pg = kiobuf->maplist[i];
623 pgp->count = CFS_PAGE_SIZE;
629 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
631 // if (rw == OBD_BRW_READ)
632 // mark_dirty_kiobuf (kiobuf, count);
634 unmap_kiobuf (kiobuf);
636 free_kiovec (1, &kiobuf);
638 OBD_FREE(pga, npages * sizeof(*pga));
642 static int echo_client_ubrw(struct obd_device *obd, int rw,
643 struct obdo *oa, struct lov_stripe_md *lsm,
644 obd_off offset, obd_size count, char *buffer,
645 struct obd_trans_info *oti)
647 #warning "echo_client_ubrw() needs to be ported on 2.6 yet"
654 struct echo_async_state;
656 #define EAP_MAGIC 79277927
657 struct echo_async_page {
659 cfs_page_t *eap_page;
662 struct echo_async_state *eap_eas;
663 struct list_head eap_item;
666 #define EAP_FROM_COOKIE(c) \
667 (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC), \
668 (struct echo_async_page *)(c))
670 struct echo_async_state {
672 obd_off eas_next_offset;
673 obd_off eas_end_offset;
676 cfs_waitq_t eas_waitq;
677 struct list_head eas_avail;
679 struct lov_stripe_md *eas_lsm;
682 static int eas_should_wake(struct echo_async_state *eas)
686 spin_lock(&eas->eas_lock);
687 if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
689 spin_unlock(&eas->eas_lock);
693 static int ec_ap_make_ready(void *data, int cmd)
695 /* our pages are issued ready */
699 static int ec_ap_refresh_count(void *data, int cmd)
701 /* our pages are issued with a stable count */
703 return CFS_PAGE_SIZE;
705 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
707 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
709 memcpy(oa, &eap->eap_eas->eas_oa, sizeof(*oa));
712 static int ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
714 struct echo_async_page *eap = EAP_FROM_COOKIE(data);
715 struct echo_async_state *eas;
719 if (cmd == OBD_BRW_READ &&
720 eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
721 (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
722 (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
723 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
724 eas->eas_oa.o_id, eap->eap_off,
727 spin_lock(&eas->eas_lock);
728 if (rc && !eas->eas_rc)
730 eas->eas_in_flight--;
731 list_add(&eap->eap_item, &eas->eas_avail);
732 cfs_waitq_signal(&eas->eas_waitq);
733 spin_unlock(&eas->eas_lock);
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;
753 struct echo_async_page **aps = NULL;
760 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
761 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
762 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
764 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
767 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
770 (count & (CFS_PAGE_SIZE - 1)) != 0 ||
772 lsm->lsm_object_id != oa->o_id))
775 /* XXX think again with misaligned I/O */
776 npages = batching >> CFS_PAGE_SHIFT;
778 memcpy(&eas.eas_oa, oa, sizeof(*oa));
779 eas.eas_next_offset = offset;
780 eas.eas_end_offset = offset + count;
781 spin_lock_init(&eas.eas_lock);
782 cfs_waitq_init(&eas.eas_waitq);
783 eas.eas_in_flight = 0;
786 CFS_INIT_LIST_HEAD(&eas.eas_avail);
788 OBD_ALLOC(aps, npages * sizeof aps[0]);
792 /* prepare the group of pages that we're going to be keeping
794 for (i = 0; i < npages; i++) {
795 cfs_page_t *page = cfs_alloc_page(CFS_ALLOC_STD);
797 GOTO(out, rc = -ENOMEM);
799 OBD_ALLOC(eap, sizeof(*eap));
802 GOTO(out, rc = -ENOMEM);
805 eap->eap_magic = EAP_MAGIC;
806 eap->eap_page = page;
808 list_add_tail(&eap->eap_item, &eas.eas_avail);
812 /* first we spin queueing io and being woken by its completion */
813 spin_lock(&eas.eas_lock);
817 /* sleep until we have a page to send */
818 spin_unlock(&eas.eas_lock);
819 rc = wait_event_interruptible(eas.eas_waitq,
820 eas_should_wake(&eas));
821 spin_lock(&eas.eas_lock);
822 if (rc && !eas.eas_rc)
826 if (list_empty(&eas.eas_avail))
828 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
830 list_del(&eap->eap_item);
831 spin_unlock(&eas.eas_lock);
833 /* unbind the eap from its old page offset */
834 if (eap->eap_cookie != NULL) {
835 obd_teardown_async_page(exp, lsm, NULL,
837 eap->eap_cookie = NULL;
840 eas.eas_next_offset += CFS_PAGE_SIZE;
841 eap->eap_off = eas.eas_next_offset;
843 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
844 eap->eap_off, &ec_async_page_ops,
845 eap, &eap->eap_cookie);
847 spin_lock(&eas.eas_lock);
852 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
853 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
854 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
855 echo_client_page_debug_setup(lsm, eap->eap_page, rw,
857 eap->eap_off, CFS_PAGE_SIZE);
859 /* always asserts urgent, which isn't quite right */
860 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
861 rw, 0, CFS_PAGE_SIZE, 0,
862 ASYNC_READY | ASYNC_URGENT |
864 spin_lock(&eas.eas_lock);
865 if (rc && !eas.eas_rc) {
870 if (eas.eas_next_offset == eas.eas_end_offset)
874 /* still hold the eas_lock here.. */
876 /* now we just spin waiting for all the rpcs to complete */
877 while(eas.eas_in_flight) {
878 spin_unlock(&eas.eas_lock);
879 wait_event_interruptible(eas.eas_waitq,
880 eas.eas_in_flight == 0);
881 spin_lock(&eas.eas_lock);
883 spin_unlock(&eas.eas_lock);
887 for (i = 0; i < npages; ++ i) {
891 page = eap->eap_page;
892 if (eap->eap_cookie != NULL)
893 obd_teardown_async_page(exp, lsm, NULL,
895 OBD_FREE(eap, sizeof(*eap));
898 OBD_FREE(aps, npages * sizeof aps[0]);
904 static int echo_client_prep_commit(struct obd_export *exp, int rw,
905 struct obdo *oa, struct lov_stripe_md *lsm,
906 obd_off offset, obd_size count,
907 obd_size batch, struct obd_trans_info *oti)
909 struct obd_ioobj ioo;
910 struct niobuf_local *lnb;
911 struct niobuf_remote *rnb;
913 obd_size npages, tot_pages;
917 if (count <= 0 || (count & (CFS_PAGE_SIZE - 1)) != 0 ||
918 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
921 npages = batch >> CFS_PAGE_SHIFT;
922 tot_pages = count >> CFS_PAGE_SHIFT;
924 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
925 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
927 if (lnb == NULL || rnb == NULL)
928 GOTO(out, ret = -ENOMEM);
930 obdo_to_ioobj(oa, &ioo);
934 for(; tot_pages; tot_pages -= npages) {
935 if (tot_pages < npages)
938 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
940 rnb[i].len = CFS_PAGE_SIZE;
943 ioo.ioo_bufcnt = npages;
944 oti->oti_transno = 0;
946 ret = obd_preprw(rw, exp, oa, 1, &ioo, npages, rnb, lnb, oti);
950 for (i = 0; i < npages; i++) {
951 cfs_page_t *page = lnb[i].page;
954 if (page == NULL && lnb[i].rc == 0)
957 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
958 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
959 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
962 if (rw == OBD_BRW_WRITE)
963 echo_client_page_debug_setup(lsm, page, rw,
968 echo_client_page_debug_check(lsm, page,
974 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
981 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
983 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
987 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
988 struct obd_ioctl_data *data)
990 struct obd_device *obd = class_exp2obd(exp);
991 struct echo_client_obd *ec = &obd->u.echo_client;
992 struct obd_trans_info dummy_oti = { .oti_thread_id = -1 };
993 struct ec_object *eco;
997 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1001 data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
1002 data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
1003 data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
1005 switch((long)data->ioc_pbuf1) {
1007 if (data->ioc_pbuf2 == NULL) { // NULL user data pointer
1008 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
1009 eco->eco_lsm, data->ioc_offset,
1010 data->ioc_count, &dummy_oti);
1013 rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
1014 eco->eco_lsm, data->ioc_offset,
1015 data->ioc_count, data->ioc_pbuf2,
1021 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
1022 eco->eco_lsm, data->ioc_offset,
1023 data->ioc_count, data->ioc_plen1);
1026 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1027 eco->eco_lsm, data->ioc_offset,
1028 data->ioc_count, data->ioc_plen1,
1034 echo_put_object(eco);
1039 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1040 void *data, int flag)
1042 struct ec_object *eco = (struct ec_object *)data;
1043 struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1044 struct lustre_handle lockh;
1045 struct list_head *el;
1049 ldlm_lock2handle (lock, &lockh);
1051 /* #ifdef this out if we're not feeling paranoid */
1052 spin_lock (&ec->ec_lock);
1053 list_for_each (el, &ec->ec_objects) {
1054 found = (eco == list_entry(el, struct ec_object,
1059 spin_unlock (&ec->ec_lock);
1063 case LDLM_CB_BLOCKING:
1064 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1065 eco->eco_id, lockh.cookie);
1066 rc = ldlm_cli_cancel (&lockh);
1068 CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1071 case LDLM_CB_CANCELING:
1072 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1073 eco->eco_id, lockh.cookie);
1084 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1085 int mode, obd_off offset, obd_size nob)
1087 struct obd_device *obd = exp->exp_obd;
1088 struct echo_client_obd *ec = &obd->u.echo_client;
1089 struct lustre_handle *ulh = obdo_handle (oa);
1090 struct obd_enqueue_info einfo = { 0 };
1091 struct obd_info oinfo = { { { 0 } } };
1092 struct ec_object *eco;
1093 struct ec_lock *ecl;
1096 if (!(mode == LCK_PR || mode == LCK_PW))
1099 if ((offset & (CFS_PAGE_SIZE - 1)) != 0 ||
1100 (nob & (CFS_PAGE_SIZE - 1)) != 0)
1103 rc = echo_get_object (&eco, obd, oa);
1108 OBD_ALLOC (ecl, sizeof (*ecl));
1112 ecl->ecl_mode = mode;
1113 ecl->ecl_object = eco;
1114 ecl->ecl_policy.l_extent.start = offset;
1115 ecl->ecl_policy.l_extent.end =
1116 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1118 einfo.ei_type = LDLM_EXTENT;
1119 einfo.ei_mode = mode;
1120 einfo.ei_cb_bl = echo_ldlm_callback;
1121 einfo.ei_cb_cp = ldlm_completion_ast;
1122 einfo.ei_cb_gl = NULL;
1123 einfo.ei_cbdata = eco;
1125 oinfo.oi_policy = ecl->ecl_policy;
1126 oinfo.oi_lockh = &ecl->ecl_lock_handle;
1127 oinfo.oi_md = eco->eco_lsm;
1128 rc = obd_enqueue(ec->ec_exp, &oinfo, &einfo);
1132 CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1134 /* NB ecl takes object ref from echo_get_object() above */
1135 spin_lock(&ec->ec_lock);
1137 list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1138 ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1140 spin_unlock(&ec->ec_lock);
1142 oa->o_valid |= OBD_MD_FLHANDLE;
1146 OBD_FREE (ecl, sizeof (*ecl));
1148 echo_put_object (eco);
1153 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1155 struct obd_device *obd = exp->exp_obd;
1156 struct echo_client_obd *ec = &obd->u.echo_client;
1157 struct lustre_handle *ulh = obdo_handle (oa);
1158 struct ec_lock *ecl = NULL;
1160 struct list_head *el;
1163 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1166 spin_lock (&ec->ec_lock);
1168 list_for_each (el, &exp->exp_ec_data.eced_locks) {
1169 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1170 found = (ecl->ecl_cookie == ulh->cookie);
1172 list_del (&ecl->ecl_exp_chain);
1177 spin_unlock (&ec->ec_lock);
1182 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1183 &ecl->ecl_lock_handle);
1185 echo_put_object (ecl->ecl_object);
1186 OBD_FREE (ecl, sizeof (*ecl));
1192 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1193 int len, void *karg, void *uarg)
1195 struct obd_device *obd;
1196 struct echo_client_obd *ec;
1197 struct ec_object *eco;
1198 struct obd_ioctl_data *data = karg;
1199 struct obd_trans_info dummy_oti;
1200 struct oti_req_ack_lock *ack_lock;
1202 int rw = OBD_BRW_READ;
1209 memset(&dummy_oti, 0, sizeof(dummy_oti));
1212 ec = &obd->u.echo_client;
1215 case OBD_IOC_CREATE: /* may create echo object */
1216 if (!capable (CAP_SYS_ADMIN))
1217 GOTO (out, rc = -EPERM);
1219 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1220 data->ioc_pbuf1, data->ioc_plen1,
1224 case OBD_IOC_DESTROY:
1225 if (!capable (CAP_SYS_ADMIN))
1226 GOTO (out, rc = -EPERM);
1228 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1230 oa = &data->ioc_obdo1;
1231 oa->o_gr = FILTER_GROUP_ECHO;
1232 oa->o_valid |= OBD_MD_FLGROUP;
1233 rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1236 eco->eco_deleted = 1;
1237 echo_put_object(eco);
1241 case OBD_IOC_GETATTR:
1242 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1244 struct obd_info oinfo = { { { 0 } } };
1245 oinfo.oi_md = eco->eco_lsm;
1246 oinfo.oi_oa = &data->ioc_obdo1;
1247 rc = obd_getattr(ec->ec_exp, &oinfo);
1248 echo_put_object(eco);
1252 case OBD_IOC_SETATTR:
1253 if (!capable (CAP_SYS_ADMIN))
1254 GOTO (out, rc = -EPERM);
1256 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1258 struct obd_info oinfo = { { { 0 } } };
1259 oinfo.oi_oa = &data->ioc_obdo1;
1260 oinfo.oi_md = eco->eco_lsm;
1262 rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1263 echo_put_object(eco);
1267 case OBD_IOC_BRW_WRITE:
1268 if (!capable (CAP_SYS_ADMIN))
1269 GOTO (out, rc = -EPERM);
1273 case OBD_IOC_BRW_READ:
1274 rc = echo_client_brw_ioctl(rw, exp, data);
1277 case ECHO_IOC_GET_STRIPE:
1278 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1280 rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1282 echo_put_object(eco);
1286 case ECHO_IOC_SET_STRIPE:
1287 if (!capable (CAP_SYS_ADMIN))
1288 GOTO (out, rc = -EPERM);
1290 if (data->ioc_pbuf1 == NULL) { /* unset */
1291 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1293 eco->eco_deleted = 1;
1294 echo_put_object(eco);
1297 rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1299 data->ioc_plen1, &dummy_oti);
1303 case ECHO_IOC_ENQUEUE:
1304 if (!capable (CAP_SYS_ADMIN))
1305 GOTO (out, rc = -EPERM);
1307 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1308 data->ioc_conn1, /* lock mode */
1309 data->ioc_offset, data->ioc_count);/*extent*/
1312 case ECHO_IOC_CANCEL:
1313 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1317 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1318 GOTO (out, rc = -ENOTTY);
1324 /* XXX this should be in a helper also called by target_send_reply */
1325 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1327 if (!ack_lock->mode)
1329 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1337 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1339 struct echo_client_obd *ec = &obddev->u.echo_client;
1340 struct obd_device *tgt;
1341 struct lustre_handle conn = {0, };
1342 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1343 struct obd_connect_data *ocd = NULL;
1347 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1348 CERROR("requires a TARGET OBD name\n");
1352 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1353 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1354 CERROR("device not attached or not set up (%s)\n",
1355 lustre_cfg_string(lcfg, 1));
1359 spin_lock_init (&ec->ec_lock);
1360 CFS_INIT_LIST_HEAD (&ec->ec_objects);
1363 OBD_ALLOC(ocd, sizeof(*ocd));
1365 CERROR("Can't alloc ocd connecting to %s\n",
1366 lustre_cfg_string(lcfg, 1));
1370 ocd->ocd_connect_flags = OBD_CONNECT_VERSION;
1371 ocd->ocd_version = LUSTRE_VERSION_CODE;
1373 rc = obd_connect(NULL, &conn, tgt, &echo_uuid, ocd);
1375 OBD_FREE(ocd, sizeof(*ocd));
1378 CERROR("fail to connect to device %s\n",
1379 lustre_cfg_string(lcfg, 1));
1382 ec->ec_exp = class_conn2export(&conn);
1387 static int echo_client_cleanup(struct obd_device *obddev)
1389 struct list_head *el;
1390 struct ec_object *eco;
1391 struct echo_client_obd *ec = &obddev->u.echo_client;
1395 if (!list_empty(&obddev->obd_exports)) {
1396 CERROR("still has clients!\n");
1400 /* XXX assuming sole access */
1401 while (!list_empty(&ec->ec_objects)) {
1402 el = ec->ec_objects.next;
1403 eco = list_entry(el, struct ec_object, eco_obj_chain);
1405 LASSERT(eco->eco_refcount == 0);
1406 eco->eco_refcount = 1;
1407 eco->eco_deleted = 1;
1408 echo_put_object(eco);
1411 rc = obd_disconnect(ec->ec_exp);
1413 CERROR("fail to disconnect device: %d\n", rc);
1418 static int echo_client_connect(const struct lu_context *ctx,
1419 struct lustre_handle *conn,
1420 struct obd_device *src, struct obd_uuid *cluuid,
1421 struct obd_connect_data *data)
1423 struct obd_export *exp;
1427 rc = class_connect(conn, src, cluuid);
1429 exp = class_conn2export(conn);
1430 CFS_INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1431 class_export_put(exp);
1437 static int echo_client_disconnect(struct obd_export *exp)
1439 struct obd_device *obd;
1440 struct echo_client_obd *ec;
1441 struct ec_lock *ecl;
1446 GOTO(out, rc = -EINVAL);
1449 ec = &obd->u.echo_client;
1451 /* no more contention on export's lock list */
1452 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1453 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1454 struct ec_lock, ecl_exp_chain);
1455 list_del (&ecl->ecl_exp_chain);
1457 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1458 ecl->ecl_mode, &ecl->ecl_lock_handle);
1460 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1461 "(%d)\n", ecl->ecl_object->eco_id, rc);
1463 echo_put_object (ecl->ecl_object);
1464 OBD_FREE (ecl, sizeof (*ecl));
1467 rc = class_disconnect(exp);
1473 static struct obd_ops echo_obd_ops = {
1474 .o_owner = THIS_MODULE,
1475 .o_setup = echo_client_setup,
1476 .o_cleanup = echo_client_cleanup,
1477 .o_iocontrol = echo_client_iocontrol,
1478 .o_connect = echo_client_connect,
1479 .o_disconnect = echo_client_disconnect
1482 int echo_client_init(void)
1484 struct lprocfs_static_vars lvars;
1486 lprocfs_init_vars(echo, &lvars);
1487 return class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
1488 LUSTRE_ECHO_CLIENT_NAME, NULL);
1491 void echo_client_exit(void)
1493 class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);