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 <linux/version.h>
28 #include <linux/module.h>
30 #include <linux/completion.h>
31 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
32 #include <linux/iobuf.h>
34 #include <asm/div64.h>
35 #include <linux/smp_lock.h>
37 #include <liblustre.h>
40 #include <linux/obd.h>
41 #include <linux/obd_support.h>
42 #include <linux/obd_class.h>
43 #include <linux/obd_echo.h>
44 #include <linux/lustre_ver.h>
45 #include <linux/lustre_debug.h>
46 #include <linux/lprocfs_status.h>
48 static obd_id last_object_id;
52 echo_printk_object (char *msg, struct ec_object *eco)
54 struct lov_stripe_md *lsm = eco->eco_lsm;
57 printk (KERN_INFO "Lustre: %s: object %p: "LPX64", refs %d%s: "LPX64
58 "=%u!%u\n", msg, eco, eco->eco_id, eco->eco_refcount,
59 eco->eco_deleted ? "(deleted) " : "",
60 lsm->lsm_object_id, lsm->lsm_stripe_size,
61 lsm->lsm_stripe_count);
63 for (i = 0; i < lsm->lsm_stripe_count; i++)
64 printk (KERN_INFO "Lustre: @%2u:"LPX64"\n",
65 lsm->lsm_oinfo[i].loi_ost_idx,
66 lsm->lsm_oinfo[i].loi_id);
70 static struct ec_object *
71 echo_find_object_locked (struct obd_device *obd, obd_id id)
73 struct echo_client_obd *ec = &obd->u.echo_client;
74 struct ec_object *eco = NULL;
77 list_for_each (el, &ec->ec_objects) {
78 eco = list_entry (el, struct ec_object, eco_obj_chain);
80 if (eco->eco_id == id)
87 echo_copyout_lsm (struct lov_stripe_md *lsm, void *ulsm, int ulsm_nob)
91 nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
95 if (copy_to_user (ulsm, lsm, nob))
102 echo_copyin_lsm (struct obd_device *obd, struct lov_stripe_md *lsm,
103 void *ulsm, int ulsm_nob)
105 struct echo_client_obd *ec = &obd->u.echo_client;
108 if (ulsm_nob < sizeof (*lsm))
111 if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
114 nob = lsm->lsm_stripe_count * sizeof (lsm->lsm_oinfo[0]);
116 if (ulsm_nob < nob ||
117 lsm->lsm_stripe_count > ec->ec_nstripes ||
118 lsm->lsm_magic != LOV_MAGIC ||
119 (lsm->lsm_stripe_size & (PAGE_SIZE - 1)) != 0 ||
120 ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
123 if (copy_from_user(lsm->lsm_oinfo,
124 ((struct lov_stripe_md *)ulsm)->lsm_oinfo, nob))
130 static struct ec_object *
131 echo_allocate_object (struct obd_device *obd)
133 struct echo_client_obd *ec = &obd->u.echo_client;
134 struct ec_object *eco;
137 OBD_ALLOC(eco, sizeof (*eco));
141 rc = obd_alloc_memmd(ec->ec_exp, &eco->eco_lsm);
143 OBD_FREE(eco, sizeof (*eco));
147 eco->eco_device = obd;
148 eco->eco_deleted = 0;
149 eco->eco_refcount = 0;
150 eco->eco_lsm->lsm_magic = LOV_MAGIC;
151 /* leave stripe count 0 by default */
157 echo_free_object (struct ec_object *eco)
159 struct obd_device *obd = eco->eco_device;
160 struct echo_client_obd *ec = &obd->u.echo_client;
162 LASSERT (eco->eco_refcount == 0);
163 obd_free_memmd(ec->ec_exp, &eco->eco_lsm);
164 OBD_FREE (eco, sizeof (*eco));
167 static int echo_create_object(struct obd_device *obd, int on_target,
168 struct obdo *oa, void *ulsm, int ulsm_nob,
169 struct obd_trans_info *oti)
171 struct echo_client_obd *ec = &obd->u.echo_client;
172 struct ec_object *eco2;
173 struct ec_object *eco;
174 struct lov_stripe_md *lsm;
178 if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
179 (on_target || /* set_stripe */
180 ec->ec_nstripes != 0)) { /* LOV */
181 CERROR ("No valid oid\n");
186 eco = echo_allocate_object (obd);
192 rc = echo_copyin_lsm (obd, lsm, ulsm, ulsm_nob);
196 /* setup object ID here for !on_target and LOV hint */
197 if ((oa->o_valid & OBD_MD_FLID) != 0)
198 eco->eco_id = lsm->lsm_object_id = oa->o_id;
200 if (lsm->lsm_stripe_count == 0)
201 lsm->lsm_stripe_count = ec->ec_nstripes;
203 if (lsm->lsm_stripe_size == 0)
204 lsm->lsm_stripe_size = PAGE_SIZE;
208 /* setup stripes: indices + default ids if required */
209 for (i = 0; i < lsm->lsm_stripe_count; i++) {
210 if (lsm->lsm_oinfo[i].loi_id == 0)
211 lsm->lsm_oinfo[i].loi_id = lsm->lsm_object_id;
213 lsm->lsm_oinfo[i].loi_ost_idx =
214 (idx + i) % ec->ec_nstripes;
217 OBD_ALLOC(eco, sizeof(*eco));
218 eco->eco_device = obd;
223 oa->o_id = ++last_object_id;
226 oa->o_gr = FILTER_GROUP_ECHO;
227 oa->o_valid |= OBD_MD_FLGROUP;
229 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
233 /* See what object ID we were given */
234 eco->eco_id = oa->o_id = lsm->lsm_object_id;
235 oa->o_valid |= OBD_MD_FLID;
237 LASSERT(eco->eco_lsm == NULL || eco->eco_lsm == lsm);
241 spin_lock (&ec->ec_lock);
243 eco2 = echo_find_object_locked (obd, oa->o_id);
244 if (eco2 != NULL) { /* conflict */
245 spin_unlock (&ec->ec_lock);
247 CERROR ("Can't create object id "LPX64": id already exists%s\n",
248 oa->o_id, on_target ? " (undoing create)" : "");
251 obd_destroy(ec->ec_exp, oa, lsm, oti, NULL);
257 list_add (&eco->eco_obj_chain, &ec->ec_objects);
258 spin_unlock (&ec->ec_lock);
260 "created %p: "LPX64"=%u#%u@%u refs %d del %d\n",
262 eco->eco_lsm->lsm_stripe_size,
263 eco->eco_lsm->lsm_stripe_count,
264 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
265 eco->eco_refcount, eco->eco_deleted);
269 echo_free_object (eco);
274 echo_get_object (struct ec_object **ecop, struct obd_device *obd,
277 struct echo_client_obd *ec = &obd->u.echo_client;
278 struct ec_object *eco;
279 struct ec_object *eco2;
282 if ((oa->o_valid & OBD_MD_FLID) == 0 ||
283 oa->o_id == 0) /* disallow use of object id 0 */
285 CERROR ("No valid oid\n");
289 spin_lock (&ec->ec_lock);
290 eco = echo_find_object_locked (obd, oa->o_id);
292 if (eco->eco_deleted) /* being deleted */
293 return (-EAGAIN); /* (see comment in cleanup) */
296 spin_unlock (&ec->ec_lock);
299 "found %p: "LPX64"=%u#%u@%u refs %d del %d\n",
301 eco->eco_lsm->lsm_stripe_size,
302 eco->eco_lsm->lsm_stripe_count,
303 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
304 eco->eco_refcount, eco->eco_deleted);
307 spin_unlock (&ec->ec_lock);
309 if (ec->ec_nstripes != 0) /* striping required */
312 eco = echo_allocate_object (obd);
316 eco->eco_id = eco->eco_lsm->lsm_object_id = oa->o_id;
318 spin_lock (&ec->ec_lock);
320 eco2 = echo_find_object_locked (obd, oa->o_id);
321 if (eco2 == NULL) { /* didn't race */
322 list_add (&eco->eco_obj_chain, &ec->ec_objects);
323 spin_unlock (&ec->ec_lock);
324 eco->eco_refcount = 1;
327 "created %p: "LPX64"=%u#%u@%d refs %d del %d\n",
329 eco->eco_lsm->lsm_stripe_size,
330 eco->eco_lsm->lsm_stripe_count,
331 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
332 eco->eco_refcount, eco->eco_deleted);
336 if (eco2->eco_deleted)
337 rc = -EAGAIN; /* lose race */
339 eco2->eco_refcount++; /* take existing */
342 LASSERT (eco2->eco_id == eco2->eco_lsm->lsm_object_id);
344 "found(2) %p: "LPX64"=%u#%u@%d refs %d del %d\n",
346 eco2->eco_lsm->lsm_stripe_size,
347 eco2->eco_lsm->lsm_stripe_count,
348 eco2->eco_lsm->lsm_oinfo[0].loi_ost_idx,
349 eco2->eco_refcount, eco2->eco_deleted);
352 spin_unlock (&ec->ec_lock);
354 echo_free_object (eco);
359 echo_put_object (struct ec_object *eco)
361 struct obd_device *obd = eco->eco_device;
362 struct echo_client_obd *ec = &obd->u.echo_client;
364 /* Release caller's ref on the object.
365 * delete => mark for deletion when last ref goes
368 spin_lock (&ec->ec_lock);
371 LASSERT (eco->eco_refcount >= 0);
373 CDEBUG(D_INFO, "put %p: "LPX64"=%u#%u@%d refs %d del %d\n",
375 eco->eco_lsm->lsm_stripe_size,
376 eco->eco_lsm->lsm_stripe_count,
377 eco->eco_lsm->lsm_oinfo[0].loi_ost_idx,
378 eco->eco_refcount, eco->eco_deleted);
380 if (eco->eco_refcount != 0 || !eco->eco_deleted) {
381 spin_unlock (&ec->ec_lock);
385 spin_unlock (&ec->ec_lock);
387 /* NB leave obj in the object list. We must prevent anyone from
388 * attempting to enqueue on this object number until we can be
389 * sure there will be no more lock callbacks.
391 obd_cancel_unused(ec->ec_exp, eco->eco_lsm, 0, NULL);
393 /* now we can let it go */
394 spin_lock (&ec->ec_lock);
395 list_del (&eco->eco_obj_chain);
396 spin_unlock (&ec->ec_lock);
398 LASSERT (eco->eco_refcount == 0);
400 echo_free_object (eco);
404 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
406 unsigned long stripe_count;
407 unsigned long stripe_size;
409 unsigned long woffset;
413 if (lsm->lsm_stripe_count <= 1)
417 stripe_size = lsm->lsm_stripe_size;
418 stripe_count = lsm->lsm_stripe_count;
420 /* width = # bytes in all stripes */
421 width = stripe_size * stripe_count;
423 /* woffset = offset within a width; offset = whole number of widths */
424 woffset = do_div (offset, width);
426 stripe_index = woffset / stripe_size;
428 *idp = lsm->lsm_oinfo[stripe_index].loi_id;
429 *offp = offset * stripe_size + woffset % stripe_size;
433 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
434 struct page *page, int rw, obd_id id,
435 obd_off offset, obd_off count)
442 /* no partial pages on the client */
443 LASSERT(count == PAGE_SIZE);
447 for (delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
448 if (rw == OBD_BRW_WRITE) {
449 stripe_off = offset + delta;
451 echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
453 stripe_off = 0xdeadbeef00c0ffeeULL;
454 stripe_id = 0xdeadbeef00c0ffeeULL;
456 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
457 stripe_off, stripe_id);
464 echo_client_page_debug_check(struct lov_stripe_md *lsm,
465 struct page *page, obd_id id,
466 obd_off offset, obd_off count)
475 /* no partial pages on the client */
476 LASSERT(count == PAGE_SIZE);
480 for (rc = delta = 0; delta < PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
481 stripe_off = offset + delta;
483 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
485 rc2 = block_debug_check("test_brw",
486 addr + delta, OBD_ECHO_BLOCK_SIZE,
487 stripe_off, stripe_id);
489 CERROR ("Error in echo object "LPX64"\n", id);
498 static int echo_client_kbrw(struct obd_device *obd, int rw, struct obdo *oa,
499 struct lov_stripe_md *lsm, obd_off offset,
500 obd_size count, struct obd_trans_info *oti)
502 struct echo_client_obd *ec = &obd->u.echo_client;
504 struct brw_page *pga;
505 struct brw_page *pgp;
512 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
513 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
514 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
516 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
518 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
519 LASSERT(lsm != NULL);
520 LASSERT(lsm->lsm_object_id == oa->o_id);
523 (count & (PAGE_SIZE - 1)) != 0)
526 /* XXX think again with misaligned I/O */
527 npages = count >> PAGE_SHIFT;
529 OBD_ALLOC(pga, npages * sizeof(*pga));
533 for (i = 0, pgp = pga, off = offset;
535 i++, pgp++, off += PAGE_SIZE) {
537 LASSERT (pgp->pg == NULL); /* for cleanup */
540 pgp->pg = alloc_pages (gfp_mask, 0);
544 pgp->count = PAGE_SIZE;
549 echo_client_page_debug_setup(lsm, pgp->pg, rw,
550 oa->o_id, off, pgp->count);
553 rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
556 if (rc != 0 || rw != OBD_BRW_READ)
559 for (i = 0, pgp = pga; i < npages; i++, pgp++) {
565 vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
566 pgp->off, pgp->count);
567 if (vrc != 0 && rc == 0)
570 __free_pages(pgp->pg, 0);
572 OBD_FREE(pga, npages * sizeof(*pga));
577 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
578 static int echo_client_ubrw(struct obd_device *obd, int rw,
579 struct obdo *oa, struct lov_stripe_md *lsm,
580 obd_off offset, obd_size count, char *buffer,
581 struct obd_trans_info *oti)
583 struct echo_client_obd *ec = &obd->u.echo_client;
585 struct brw_page *pga;
586 struct brw_page *pgp;
588 struct kiobuf *kiobuf;
592 LASSERT (rw == OBD_BRW_WRITE ||
595 /* NB: for now, only whole pages, page aligned */
598 ((long)buffer & (PAGE_SIZE - 1)) != 0 ||
599 (count & (PAGE_SIZE - 1)) != 0 ||
600 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
603 /* XXX think again with misaligned I/O */
604 npages = count >> PAGE_SHIFT;
606 OBD_ALLOC(pga, npages * sizeof(*pga));
610 rc = alloc_kiovec (1, &kiobuf);
614 rc = map_user_kiobuf ((rw == OBD_BRW_READ) ? READ : WRITE,
615 kiobuf, (unsigned long)buffer, count);
619 LASSERT (kiobuf->offset == 0);
620 LASSERT (kiobuf->nr_pages == npages);
622 for (i = 0, off = offset, pgp = pga;
624 i++, off += PAGE_SIZE, pgp++) {
626 pgp->pg = kiobuf->maplist[i];
627 pgp->count = PAGE_SIZE;
631 rc = obd_brw(rw, ec->ec_exp, oa, lsm, npages, pga, oti);
633 // if (rw == OBD_BRW_READ)
634 // mark_dirty_kiobuf (kiobuf, count);
636 unmap_kiobuf (kiobuf);
638 free_kiovec (1, &kiobuf);
640 OBD_FREE(pga, npages * sizeof(*pga));
644 static int echo_client_ubrw(struct obd_device *obd, int rw,
645 struct obdo *oa, struct lov_stripe_md *lsm,
646 obd_off offset, obd_size count, char *buffer,
647 struct obd_trans_info *oti)
649 #warning "echo_client_ubrw() needs to be ported on 2.6 yet"
656 struct echo_async_state;
658 #define EAP_MAGIC 79277927
659 struct echo_async_page {
661 struct page *eap_page;
664 struct echo_async_state *eap_eas;
665 struct list_head eap_item;
668 #define EAP_FROM_COOKIE(c) \
669 (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC), \
670 (struct echo_async_page *)(c))
672 struct echo_async_state {
674 obd_off eas_next_offset;
675 obd_off eas_end_offset;
678 wait_queue_head_t eas_waitq;
679 struct list_head eas_avail;
681 struct lov_stripe_md *eas_lsm;
684 static int eas_should_wake(struct echo_async_state *eas)
688 spin_lock_irqsave(&eas->eas_lock, flags);
689 if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
691 spin_unlock_irqrestore(&eas->eas_lock, flags);
695 static int ec_ap_make_ready(void *data, int cmd)
697 /* our pages are issued ready */
701 static int ec_ap_refresh_count(void *data, int cmd)
703 /* our pages are issued with a stable count */
707 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
709 struct echo_async_page *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;
722 if (cmd == OBD_BRW_READ &&
723 eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
724 (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
725 (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
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;
762 verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
763 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
764 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
766 gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
769 LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
772 (count & (PAGE_SIZE - 1)) != 0 ||
774 lsm->lsm_object_id != oa->o_id))
777 /* XXX think again with misaligned I/O */
778 npages = batching >> PAGE_SHIFT;
780 memcpy(&eas.eas_oa, oa, sizeof(*oa));
781 eas.eas_next_offset = offset;
782 eas.eas_end_offset = offset + count;
783 spin_lock_init(&eas.eas_lock);
784 init_waitqueue_head(&eas.eas_waitq);
785 eas.eas_in_flight = 0;
788 INIT_LIST_HEAD(&eas.eas_avail);
790 /* prepare the group of pages that we're going to be keeping
792 for (i = 0; i < npages; i++) {
793 struct page *page = alloc_page(GFP_KERNEL);
795 GOTO(out, rc = -ENOMEM);
798 list_add_tail(&PAGE_LIST(page), &pages);
800 OBD_ALLOC(eap, sizeof(*eap));
802 GOTO(out, rc = -ENOMEM);
804 eap->eap_magic = EAP_MAGIC;
805 eap->eap_page = page;
807 page->private = (unsigned long)eap;
808 list_add_tail(&eap->eap_item, &eas.eas_avail);
811 /* first we spin queueing io and being woken by its completion */
812 spin_lock_irqsave(&eas.eas_lock, flags);
816 /* sleep until we have a page to send */
817 spin_unlock_irqrestore(&eas.eas_lock, flags);
818 rc = wait_event_interruptible(eas.eas_waitq,
819 eas_should_wake(&eas));
820 spin_lock_irqsave(&eas.eas_lock, flags);
821 if (rc && !eas.eas_rc)
825 if (list_empty(&eas.eas_avail))
827 eap = list_entry(eas.eas_avail.next, struct echo_async_page,
829 list_del(&eap->eap_item);
830 spin_unlock_irqrestore(&eas.eas_lock, flags);
832 /* unbind the eap from its old page offset */
833 if (eap->eap_cookie != NULL) {
834 obd_teardown_async_page(exp, lsm, NULL,
836 eap->eap_cookie = NULL;
839 eas.eas_next_offset += PAGE_SIZE;
840 eap->eap_off = eas.eas_next_offset;
842 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
843 eap->eap_off, &ec_async_page_ops,
844 eap, &eap->eap_cookie);
846 spin_lock_irqsave(&eas.eas_lock, flags);
851 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
852 (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
853 (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
854 echo_client_page_debug_setup(lsm, eap->eap_page, rw,
856 eap->eap_off, PAGE_SIZE);
858 /* always asserts urgent, which isn't quite right */
859 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
861 ASYNC_READY | ASYNC_URGENT |
863 spin_lock_irqsave(&eas.eas_lock, flags);
864 if (rc && !eas.eas_rc) {
869 if (eas.eas_next_offset == eas.eas_end_offset)
873 /* still hold the eas_lock here.. */
875 /* now we just spin waiting for all the rpcs to complete */
876 while(eas.eas_in_flight) {
877 spin_unlock_irqrestore(&eas.eas_lock, flags);
878 wait_event_interruptible(eas.eas_waitq,
879 eas.eas_in_flight == 0);
880 spin_lock_irqsave(&eas.eas_lock, flags);
882 spin_unlock_irqrestore(&eas.eas_lock, flags);
885 list_for_each_safe(pos, n, &pages) {
886 struct page *page = list_entry(pos, struct page,
889 list_del(&PAGE_LIST(page));
890 if (page->private != 0) {
891 eap = (struct echo_async_page *)page->private;
892 if (eap->eap_cookie != NULL)
893 obd_teardown_async_page(exp, lsm, NULL,
895 OBD_FREE(eap, sizeof(*eap));
903 static int echo_client_prep_commit(struct obd_export *exp, int rw,
904 struct obdo *oa, struct lov_stripe_md *lsm,
905 obd_off offset, obd_size count,
906 obd_size batch, struct obd_trans_info *oti)
908 struct obd_ioobj ioo;
909 struct niobuf_local *lnb;
910 struct niobuf_remote *rnb;
912 obd_size npages, tot_pages;
916 if (count <= 0 || (count & (PAGE_SIZE - 1)) != 0 ||
917 (lsm != NULL && lsm->lsm_object_id != oa->o_id))
920 npages = batch >> PAGE_SHIFT;
921 tot_pages = count >> PAGE_SHIFT;
923 OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
924 OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
926 if (lnb == NULL || rnb == NULL)
927 GOTO(out, ret = -ENOMEM);
929 obdo_to_ioobj(oa, &ioo);
933 for(; tot_pages; tot_pages -= npages) {
934 if (tot_pages < npages)
937 for (i = 0; i < npages; i++, off += PAGE_SIZE) {
939 rnb[i].len = PAGE_SIZE;
942 ioo.ioo_bufcnt = npages;
943 oti->oti_transno = 0;
945 ret = obd_preprw(rw, exp, oa, 1, &ioo, npages, rnb, lnb, oti);
949 for (i = 0; i < npages; i++) {
950 struct page *page = lnb[i].page;
953 if (page == NULL && lnb[i].rc == 0)
956 if (oa->o_id == ECHO_PERSISTENT_OBJID ||
957 (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
958 (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
961 if (rw == OBD_BRW_WRITE)
962 echo_client_page_debug_setup(lsm, page, rw,
967 echo_client_page_debug_check(lsm, page,
973 ret = obd_commitrw(rw, exp, oa, 1, &ioo, npages, lnb, oti, ret);
980 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
982 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
986 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
987 struct obd_ioctl_data *data)
989 struct obd_device *obd = class_exp2obd(exp);
990 struct echo_client_obd *ec = &obd->u.echo_client;
991 struct obd_trans_info dummy_oti = { .oti_thread_id = -1 };
992 struct ec_object *eco;
996 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1000 data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
1001 data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
1002 data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
1004 switch((long)data->ioc_pbuf1) {
1006 if (data->ioc_pbuf2 == NULL) { // NULL user data pointer
1007 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
1008 eco->eco_lsm, data->ioc_offset,
1009 data->ioc_count, &dummy_oti);
1012 rc = echo_client_ubrw(obd, rw, &data->ioc_obdo1,
1013 eco->eco_lsm, data->ioc_offset,
1014 data->ioc_count, data->ioc_pbuf2,
1020 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
1021 eco->eco_lsm, data->ioc_offset,
1022 data->ioc_count, data->ioc_plen1);
1025 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1026 eco->eco_lsm, data->ioc_offset,
1027 data->ioc_count, data->ioc_plen1,
1033 echo_put_object(eco);
1038 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1039 void *data, int flag)
1041 struct ec_object *eco = (struct ec_object *)data;
1042 struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1043 struct lustre_handle lockh;
1044 struct list_head *el;
1048 ldlm_lock2handle (lock, &lockh);
1050 /* #ifdef this out if we're not feeling paranoid */
1051 spin_lock (&ec->ec_lock);
1052 list_for_each (el, &ec->ec_objects) {
1053 found = (eco == list_entry(el, struct ec_object,
1058 spin_unlock (&ec->ec_lock);
1062 case LDLM_CB_BLOCKING:
1063 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1064 eco->eco_id, lockh.cookie);
1065 rc = ldlm_cli_cancel (&lockh);
1067 CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1070 case LDLM_CB_CANCELING:
1071 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1072 eco->eco_id, lockh.cookie);
1083 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1084 int mode, obd_off offset, obd_size nob)
1086 struct obd_device *obd = exp->exp_obd;
1087 struct echo_client_obd *ec = &obd->u.echo_client;
1088 struct lustre_handle *ulh = obdo_handle (oa);
1089 struct ec_object *eco;
1090 struct ec_lock *ecl;
1094 if (!(mode == LCK_PR || mode == LCK_PW))
1097 if ((offset & (PAGE_SIZE - 1)) != 0 ||
1098 (nob & (PAGE_SIZE - 1)) != 0)
1101 rc = echo_get_object (&eco, obd, oa);
1106 OBD_ALLOC (ecl, sizeof (*ecl));
1110 ecl->ecl_mode = mode;
1111 ecl->ecl_object = eco;
1112 ecl->ecl_policy.l_extent.start = offset;
1113 ecl->ecl_policy.l_extent.end =
1114 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1117 rc = obd_enqueue(ec->ec_exp, eco->eco_lsm, LDLM_EXTENT,
1118 &ecl->ecl_policy, mode, &flags, echo_ldlm_callback,
1119 ldlm_completion_ast, NULL, eco, sizeof(struct ost_lvb),
1120 lustre_swab_ost_lvb, &ecl->ecl_lock_handle);
1124 CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1126 /* NB ecl takes object ref from echo_get_object() above */
1127 spin_lock(&ec->ec_lock);
1129 list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1130 ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1132 spin_unlock(&ec->ec_lock);
1134 oa->o_valid |= OBD_MD_FLHANDLE;
1138 OBD_FREE (ecl, sizeof (*ecl));
1140 echo_put_object (eco);
1145 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1147 struct obd_device *obd = exp->exp_obd;
1148 struct echo_client_obd *ec = &obd->u.echo_client;
1149 struct lustre_handle *ulh = obdo_handle (oa);
1150 struct ec_lock *ecl = NULL;
1152 struct list_head *el;
1155 if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1158 spin_lock (&ec->ec_lock);
1160 list_for_each (el, &exp->exp_ec_data.eced_locks) {
1161 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1162 found = (ecl->ecl_cookie == ulh->cookie);
1164 list_del (&ecl->ecl_exp_chain);
1169 spin_unlock (&ec->ec_lock);
1174 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1175 &ecl->ecl_lock_handle);
1177 echo_put_object (ecl->ecl_object);
1178 OBD_FREE (ecl, sizeof (*ecl));
1184 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1185 int len, void *karg, void *uarg)
1187 struct obd_device *obd;
1188 struct echo_client_obd *ec;
1189 struct ec_object *eco;
1190 struct obd_ioctl_data *data = karg;
1191 struct obd_trans_info dummy_oti;
1192 struct oti_req_ack_lock *ack_lock;
1194 int rw = OBD_BRW_READ;
1201 memset(&dummy_oti, 0, sizeof(dummy_oti));
1204 ec = &obd->u.echo_client;
1207 case OBD_IOC_CREATE: /* may create echo object */
1208 if (!capable (CAP_SYS_ADMIN))
1209 GOTO (out, rc = -EPERM);
1211 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1212 data->ioc_pbuf1, data->ioc_plen1,
1216 case OBD_IOC_DESTROY:
1217 if (!capable (CAP_SYS_ADMIN))
1218 GOTO (out, rc = -EPERM);
1220 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1222 oa = &data->ioc_obdo1;
1223 oa->o_gr = FILTER_GROUP_ECHO;
1224 oa->o_valid |= OBD_MD_FLGROUP;
1225 rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1228 eco->eco_deleted = 1;
1229 echo_put_object(eco);
1233 case OBD_IOC_GETATTR:
1234 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1236 rc = obd_getattr(ec->ec_exp, &data->ioc_obdo1,
1238 echo_put_object(eco);
1242 case OBD_IOC_SETATTR:
1243 if (!capable (CAP_SYS_ADMIN))
1244 GOTO (out, rc = -EPERM);
1246 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1248 rc = obd_setattr(ec->ec_exp, &data->ioc_obdo1,
1249 eco->eco_lsm, NULL);
1250 echo_put_object(eco);
1254 case OBD_IOC_BRW_WRITE:
1255 if (!capable (CAP_SYS_ADMIN))
1256 GOTO (out, rc = -EPERM);
1260 case OBD_IOC_BRW_READ:
1261 rc = echo_client_brw_ioctl(rw, exp, data);
1264 case ECHO_IOC_GET_STRIPE:
1265 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1267 rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1269 echo_put_object(eco);
1273 case ECHO_IOC_SET_STRIPE:
1274 if (!capable (CAP_SYS_ADMIN))
1275 GOTO (out, rc = -EPERM);
1277 if (data->ioc_pbuf1 == NULL) { /* unset */
1278 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1280 eco->eco_deleted = 1;
1281 echo_put_object(eco);
1284 rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1286 data->ioc_plen1, &dummy_oti);
1290 case ECHO_IOC_ENQUEUE:
1291 if (!capable (CAP_SYS_ADMIN))
1292 GOTO (out, rc = -EPERM);
1294 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1295 data->ioc_conn1, /* lock mode */
1296 data->ioc_offset, data->ioc_count);/*extent*/
1299 case ECHO_IOC_CANCEL:
1300 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1304 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1305 GOTO (out, rc = -ENOTTY);
1311 /* XXX this should be in a helper also called by target_send_reply */
1312 for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1314 if (!ack_lock->mode)
1316 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1324 static int echo_client_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
1326 struct echo_client_obd *ec = &obddev->u.echo_client;
1327 struct obd_device *tgt;
1328 struct lustre_handle conn = {0, };
1329 struct obd_uuid echo_uuid = { "ECHO_UUID" };
1330 struct obd_connect_data *ocd = NULL;
1334 if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1335 CERROR("requires a TARGET OBD name\n");
1339 tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1340 if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1341 CERROR("device not attached or not set up (%s)\n",
1342 lustre_cfg_string(lcfg, 1));
1346 spin_lock_init (&ec->ec_lock);
1347 INIT_LIST_HEAD (&ec->ec_objects);
1350 OBD_ALLOC(ocd, sizeof(*ocd));
1352 CERROR("Can't alloc ocd connecting to %s\n",
1353 lustre_cfg_string(lcfg, 1));
1357 ocd->ocd_version = LUSTRE_VERSION_CODE;
1359 rc = obd_connect(&conn, tgt, &echo_uuid, ocd);
1361 OBD_FREE(ocd, sizeof(*ocd));
1364 CERROR("fail to connect to device %s\n",
1365 lustre_cfg_string(lcfg, 1));
1368 ec->ec_exp = class_conn2export(&conn);
1373 static int echo_client_cleanup(struct obd_device *obddev)
1375 struct list_head *el;
1376 struct ec_object *eco;
1377 struct echo_client_obd *ec = &obddev->u.echo_client;
1381 if (!list_empty(&obddev->obd_exports)) {
1382 CERROR("still has clients!\n");
1386 /* XXX assuming sole access */
1387 while (!list_empty(&ec->ec_objects)) {
1388 el = ec->ec_objects.next;
1389 eco = list_entry(el, struct ec_object, eco_obj_chain);
1391 LASSERT(eco->eco_refcount == 0);
1392 eco->eco_refcount = 1;
1393 eco->eco_deleted = 1;
1394 echo_put_object(eco);
1397 rc = obd_disconnect(ec->ec_exp);
1399 CERROR("fail to disconnect device: %d\n", rc);
1404 static int echo_client_connect(struct lustre_handle *conn,
1405 struct obd_device *src, struct obd_uuid *cluuid,
1406 struct obd_connect_data *data)
1408 struct obd_export *exp;
1411 rc = class_connect(conn, src, cluuid);
1413 exp = class_conn2export(conn);
1414 INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1415 class_export_put(exp);
1421 static int echo_client_disconnect(struct obd_export *exp)
1423 struct obd_device *obd;
1424 struct echo_client_obd *ec;
1425 struct ec_lock *ecl;
1430 GOTO(out, rc = -EINVAL);
1433 ec = &obd->u.echo_client;
1435 /* no more contention on export's lock list */
1436 while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1437 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1438 struct ec_lock, ecl_exp_chain);
1439 list_del (&ecl->ecl_exp_chain);
1441 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1442 ecl->ecl_mode, &ecl->ecl_lock_handle);
1444 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1445 "(%d)\n", ecl->ecl_object->eco_id, rc);
1447 echo_put_object (ecl->ecl_object);
1448 OBD_FREE (ecl, sizeof (*ecl));
1451 rc = class_disconnect(exp);
1457 static struct obd_ops echo_obd_ops = {
1458 .o_owner = THIS_MODULE,
1459 .o_setup = echo_client_setup,
1460 .o_cleanup = echo_client_cleanup,
1461 .o_iocontrol = echo_client_iocontrol,
1462 .o_connect = echo_client_connect,
1463 .o_disconnect = echo_client_disconnect
1466 int echo_client_init(void)
1468 struct lprocfs_static_vars lvars;
1470 lprocfs_init_vars(echo, &lvars);
1471 return class_register_type(&echo_obd_ops, lvars.module_vars,
1472 OBD_ECHO_CLIENT_DEVICENAME, NULL);
1475 void echo_client_exit(void)
1477 class_unregister_type(OBD_ECHO_CLIENT_DEVICENAME);