Whamcloud - gitweb
LU-313 tests: re-enable lfsck test to run by default
[fs/lustre-release.git] / lustre / obdecho / echo_client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_ECHO
38 #ifdef __KERNEL__
39 #include <libcfs/libcfs.h>
40 #else
41 #include <liblustre.h>
42 #endif
43
44 #include <obd.h>
45 #include <obd_support.h>
46 #include <obd_class.h>
47 #include <obd_echo.h>
48 #include <lustre_debug.h>
49 #include <lprocfs_status.h>
50
51 static obd_id last_object_id;
52
53 #if 0
54 static void
55 echo_printk_object (char *msg, struct ec_object *eco)
56 {
57         struct lov_stripe_md *lsm = eco->eco_lsm;
58         int                   i;
59
60         printk (KERN_INFO "Lustre: %s: object %p: "LPX64", refs %d%s: "LPX64
61                 "=%u!%u\n", msg, eco, eco->eco_id, eco->eco_refcount,
62                 eco->eco_deleted ? "(deleted) " : "",
63                 lsm->lsm_object_id, lsm->lsm_stripe_size,
64                 lsm->lsm_stripe_count);
65
66         for (i = 0; i < lsm->lsm_stripe_count; i++)
67                 printk (KERN_INFO "Lustre:   @%2u:"LPX64"\n",
68                         lsm->lsm_oinfo[i].loi_ost_idx,
69                         lsm->lsm_oinfo[i].loi_id);
70 }
71 #endif
72
73 static struct ec_object *
74 echo_find_object_locked (struct obd_device *obd, obd_id id)
75 {
76         struct echo_client_obd *ec = &obd->u.echo_client;
77         struct ec_object       *eco = NULL;
78         struct list_head       *el;
79
80         list_for_each (el, &ec->ec_objects) {
81                 eco = list_entry (el, struct ec_object, eco_obj_chain);
82
83                 if (eco->eco_id == id)
84                         return (eco);
85         }
86         return (NULL);
87 }
88
89 static int
90 echo_copyout_lsm (struct lov_stripe_md *lsm, void *_ulsm, int ulsm_nob)
91 {
92         struct lov_stripe_md *ulsm = _ulsm;
93         int nob, i;
94
95         nob = offsetof (struct lov_stripe_md, lsm_oinfo[lsm->lsm_stripe_count]);
96         if (nob > ulsm_nob)
97                 return (-EINVAL);
98
99         if (copy_to_user (ulsm, lsm, sizeof(ulsm)))
100                 return (-EFAULT);
101
102         for (i = 0; i < lsm->lsm_stripe_count; i++) {
103                 if (copy_to_user (ulsm->lsm_oinfo[i], lsm->lsm_oinfo[i],
104                                   sizeof(lsm->lsm_oinfo[0])))
105                         return (-EFAULT);
106         }
107         return (0);
108 }
109
110 static int
111 echo_copyin_lsm (struct obd_device *obd, struct lov_stripe_md *lsm,
112                  void *ulsm, int ulsm_nob)
113 {
114         struct echo_client_obd *ec = &obd->u.echo_client;
115         int                     i;
116
117         if (ulsm_nob < sizeof (*lsm))
118                 return (-EINVAL);
119
120         if (copy_from_user (lsm, ulsm, sizeof (*lsm)))
121                 return (-EFAULT);
122
123         if (lsm->lsm_stripe_count > ec->ec_nstripes ||
124             lsm->lsm_magic != LOV_MAGIC ||
125             (lsm->lsm_stripe_size & (~CFS_PAGE_MASK)) != 0 ||
126             ((__u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count > ~0UL))
127                 return (-EINVAL);
128
129         for (i = 0; i < lsm->lsm_stripe_count; i++) {
130                 if (copy_from_user(lsm->lsm_oinfo[i],
131                                    ((struct lov_stripe_md *)ulsm)->lsm_oinfo[i],
132                                    sizeof(lsm->lsm_oinfo[0])))
133                         return (-EFAULT);
134         }
135
136         return (0);
137 }
138
139 static struct ec_object *
140 echo_allocate_object (struct obd_device *obd)
141 {
142         struct echo_client_obd *ec = &obd->u.echo_client;
143         struct ec_object       *eco;
144         int rc;
145
146         OBD_ALLOC(eco, sizeof (*eco));
147         if (eco == NULL)
148                 return NULL;
149
150         rc = obd_alloc_memmd(ec->ec_exp, &eco->eco_lsm);
151         if (rc < 0) {
152                 OBD_FREE(eco, sizeof (*eco));
153                 return NULL;
154         }
155
156         eco->eco_device = obd;
157         eco->eco_deleted = 0;
158         eco->eco_refcount = 0;
159         eco->eco_lsm->lsm_magic = LOV_MAGIC;
160         /* leave stripe count 0 by default */
161
162         return (eco);
163 }
164
165 static void
166 echo_free_object (struct ec_object *eco)
167 {
168         struct obd_device      *obd = eco->eco_device;
169         struct echo_client_obd *ec = &obd->u.echo_client;
170
171         LASSERT (eco->eco_refcount == 0);
172         if (!eco->eco_lsm)
173                 CERROR("No object %s\n", obd->obd_name);
174         else
175                 obd_free_memmd(ec->ec_exp, &eco->eco_lsm);
176         OBD_FREE (eco, sizeof (*eco));
177 }
178
179 static int echo_create_object(struct obd_device *obd, int on_target,
180                               struct obdo *oa, void *ulsm, int ulsm_nob,
181                               struct obd_trans_info *oti)
182 {
183         struct echo_client_obd *ec = &obd->u.echo_client;
184         struct ec_object       *eco2;
185         struct ec_object       *eco;
186         struct lov_stripe_md   *lsm;
187         int                     rc;
188         int                     i, idx;
189
190         if ((oa->o_valid & OBD_MD_FLID) == 0 && /* no obj id */
191             (on_target ||                       /* set_stripe */
192              ec->ec_nstripes != 0)) {           /* LOV */
193                 CERROR ("No valid oid\n");
194                 return (-EINVAL);
195         }
196
197         if (ulsm != NULL) {
198                 eco = echo_allocate_object (obd);
199                 if (eco == NULL)
200                         return (-ENOMEM);
201
202                 lsm = eco->eco_lsm;
203
204                 rc = echo_copyin_lsm (obd, lsm, ulsm, ulsm_nob);
205                 if (rc != 0)
206                         goto failed;
207
208                 /* setup object ID here for !on_target and LOV hint */
209                 if ((oa->o_valid & OBD_MD_FLID) != 0)
210                         eco->eco_id = lsm->lsm_object_id = oa->o_id;
211
212                 if (lsm->lsm_stripe_count == 0)
213                         lsm->lsm_stripe_count = ec->ec_nstripes;
214
215                 if (lsm->lsm_stripe_size == 0)
216                         lsm->lsm_stripe_size = CFS_PAGE_SIZE;
217
218                 idx = ll_rand();
219
220                 /* setup stripes: indices + default ids if required */
221                 for (i = 0; i < lsm->lsm_stripe_count; i++) {
222                         if (lsm->lsm_oinfo[i]->loi_id == 0)
223                                 lsm->lsm_oinfo[i]->loi_id = lsm->lsm_object_id;
224
225                         lsm->lsm_oinfo[i]->loi_ost_idx =
226                                 (idx + i) % ec->ec_nstripes;
227                 }
228         } else {
229                 OBD_ALLOC(eco, sizeof(*eco));
230                 if (!eco)
231                         return (-ENOMEM);
232                 eco->eco_device = obd;
233                 lsm = NULL;
234         }
235
236         if (oa->o_id == 0)
237                 oa->o_id = ++last_object_id;
238
239         if (on_target) {
240                 oa->o_gr = FILTER_GROUP_ECHO;
241                 oa->o_valid |= OBD_MD_FLGROUP;
242
243                 rc = obd_create(ec->ec_exp, oa, &lsm, oti);
244                 if (rc != 0)
245                         goto failed;
246
247                 /* See what object ID we were given */
248                 eco->eco_id = oa->o_id = lsm->lsm_object_id;
249                 oa->o_valid |= OBD_MD_FLID;
250
251                 LASSERT(eco->eco_lsm == NULL || eco->eco_lsm == lsm);
252                 eco->eco_lsm = lsm;
253         }
254
255         spin_lock (&ec->ec_lock);
256
257         eco2 = echo_find_object_locked (obd, oa->o_id);
258         if (eco2 != NULL) {                     /* conflict */
259                 spin_unlock (&ec->ec_lock);
260
261                 CERROR ("Can't create object id "LPX64": id already exists%s\n",
262                         oa->o_id, on_target ? " (undoing create)" : "");
263
264                 if (on_target)
265                         obd_destroy(ec->ec_exp, oa, lsm, oti, NULL);
266
267                 rc = -EEXIST;
268                 goto failed;
269         }
270
271         list_add (&eco->eco_obj_chain, &ec->ec_objects);
272         spin_unlock (&ec->ec_lock);
273         CDEBUG (D_INFO,
274                 "created %p: "LPX64"=%u#%u@%u refs %d del %d\n",
275                 eco, eco->eco_id,
276                 eco->eco_lsm->lsm_stripe_size,
277                 eco->eco_lsm->lsm_stripe_count,
278                 eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
279                 eco->eco_refcount, eco->eco_deleted);
280         return (0);
281
282  failed:
283         echo_free_object (eco);
284         if (rc)
285                 CERROR("%s: err %d on create\n", obd->obd_name, rc);
286         return (rc);
287 }
288
289 static int
290 echo_get_object (struct ec_object **ecop, struct obd_device *obd,
291                  struct obdo *oa)
292 {
293         struct echo_client_obd *ec = &obd->u.echo_client;
294         struct ec_object       *eco;
295         struct ec_object       *eco2;
296         int                     rc;
297
298         if ((oa->o_valid & OBD_MD_FLID) == 0 ||
299             oa->o_id == 0)                      /* disallow use of object id 0 */
300         {
301                 CERROR ("No valid oid\n");
302                 return (-EINVAL);
303         }
304
305         spin_lock (&ec->ec_lock);
306         eco = echo_find_object_locked (obd, oa->o_id);
307         if (eco != NULL) {
308                 if (eco->eco_deleted) {           /* being deleted */
309                         spin_unlock(&ec->ec_lock);/* (see comment in cleanup) */
310                         return (-EAGAIN);
311                 }
312
313                 eco->eco_refcount++;
314                 spin_unlock (&ec->ec_lock);
315                 *ecop = eco;
316                 CDEBUG (D_INFO,
317                         "found %p: "LPX64"=%u#%u@%u refs %d del %d\n",
318                         eco, eco->eco_id,
319                         eco->eco_lsm->lsm_stripe_size,
320                         eco->eco_lsm->lsm_stripe_count,
321                         eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
322                         eco->eco_refcount, eco->eco_deleted);
323                 return (0);
324         }
325         spin_unlock (&ec->ec_lock);
326
327         if (ec->ec_nstripes != 0)               /* striping required */
328                 return (-ENOENT);
329
330         eco = echo_allocate_object (obd);
331         if (eco == NULL)
332                 return (-ENOMEM);
333
334         eco->eco_id = eco->eco_lsm->lsm_object_id = oa->o_id;
335
336         spin_lock (&ec->ec_lock);
337
338         eco2 = echo_find_object_locked (obd, oa->o_id);
339         if (eco2 == NULL) {                     /* didn't race */
340                 list_add (&eco->eco_obj_chain, &ec->ec_objects);
341                 spin_unlock (&ec->ec_lock);
342                 eco->eco_refcount = 1;
343                 *ecop = eco;
344                 CDEBUG (D_INFO,
345                         "created %p: "LPX64"=%u#%u@%d refs %d del %d\n",
346                         eco, eco->eco_id,
347                         eco->eco_lsm->lsm_stripe_size,
348                         eco->eco_lsm->lsm_stripe_count,
349                         eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
350                         eco->eco_refcount, eco->eco_deleted);
351                 return (0);
352         }
353
354         if (eco2->eco_deleted)
355                 rc = -EAGAIN;                   /* lose race */
356         else {
357                 eco2->eco_refcount++;           /* take existing */
358                 *ecop = eco2;
359                 rc = 0;
360                 LASSERT (eco2->eco_id == eco2->eco_lsm->lsm_object_id);
361                 CDEBUG (D_INFO,
362                         "found(2) %p: "LPX64"=%u#%u@%d refs %d del %d\n",
363                         eco2, eco2->eco_id,
364                         eco2->eco_lsm->lsm_stripe_size,
365                         eco2->eco_lsm->lsm_stripe_count,
366                         eco2->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
367                         eco2->eco_refcount, eco2->eco_deleted);
368         }
369
370         spin_unlock (&ec->ec_lock);
371
372         echo_free_object (eco);
373         return (rc);
374 }
375
376 static void
377 echo_put_object (struct ec_object *eco)
378 {
379         struct obd_device      *obd = eco->eco_device;
380         struct echo_client_obd *ec = &obd->u.echo_client;
381
382         /* Release caller's ref on the object.
383          * delete => mark for deletion when last ref goes
384          */
385
386         spin_lock (&ec->ec_lock);
387
388         eco->eco_refcount--;
389         LASSERT (eco->eco_refcount >= 0);
390
391         CDEBUG(D_INFO, "put %p: "LPX64"=%u#%u@%d refs %d del %d\n",
392                eco, eco->eco_id,
393                eco->eco_lsm->lsm_stripe_size,
394                eco->eco_lsm->lsm_stripe_count,
395                eco->eco_lsm->lsm_oinfo[0]->loi_ost_idx,
396                eco->eco_refcount, eco->eco_deleted);
397
398         if (eco->eco_refcount != 0 || !eco->eco_deleted) {
399                 spin_unlock (&ec->ec_lock);
400                 return;
401         }
402
403         spin_unlock (&ec->ec_lock);
404
405         /* NB leave obj in the object list.  We must prevent anyone from
406          * attempting to enqueue on this object number until we can be
407          * sure there will be no more lock callbacks.
408          */
409         obd_cancel_unused(ec->ec_exp, eco->eco_lsm, 0, NULL);
410
411         /* now we can let it go */
412         spin_lock (&ec->ec_lock);
413         list_del (&eco->eco_obj_chain);
414         spin_unlock (&ec->ec_lock);
415
416         LASSERT (eco->eco_refcount == 0);
417
418         echo_free_object (eco);
419 }
420
421 static void
422 echo_get_stripe_off_id (struct lov_stripe_md *lsm, obd_off *offp, obd_id *idp)
423 {
424         unsigned long stripe_count;
425         unsigned long stripe_size;
426         unsigned long width;
427         unsigned long woffset;
428         int           stripe_index;
429         obd_off       offset;
430
431         if (lsm->lsm_stripe_count <= 1)
432                 return;
433
434         offset       = *offp;
435         stripe_size  = lsm->lsm_stripe_size;
436         stripe_count = lsm->lsm_stripe_count;
437
438         /* width = # bytes in all stripes */
439         width = stripe_size * stripe_count;
440
441         /* woffset = offset within a width; offset = whole number of widths */
442         woffset = do_div (offset, width);
443
444         stripe_index = woffset / stripe_size;
445
446         *idp = lsm->lsm_oinfo[stripe_index]->loi_id;
447         *offp = offset * stripe_size + woffset % stripe_size;
448 }
449
450 static void
451 echo_client_page_debug_setup(struct lov_stripe_md *lsm,
452                              cfs_page_t *page, int rw, obd_id id,
453                              obd_off offset, obd_off count)
454 {
455         char    *addr;
456         obd_off  stripe_off;
457         obd_id   stripe_id;
458         int      delta;
459
460         /* no partial pages on the client */
461         LASSERT(count == CFS_PAGE_SIZE);
462
463         addr = cfs_kmap(page);
464
465         for (delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
466                 if (rw == OBD_BRW_WRITE) {
467                         stripe_off = offset + delta;
468                         stripe_id = id;
469                         echo_get_stripe_off_id(lsm, &stripe_off, &stripe_id);
470                 } else {
471                         stripe_off = 0xdeadbeef00c0ffeeULL;
472                         stripe_id = 0xdeadbeef00c0ffeeULL;
473                 }
474                 block_debug_setup(addr + delta, OBD_ECHO_BLOCK_SIZE,
475                                   stripe_off, stripe_id);
476         }
477
478         cfs_kunmap(page);
479 }
480
481 static int
482 echo_client_page_debug_check(struct lov_stripe_md *lsm,
483                              cfs_page_t *page, obd_id id,
484                              obd_off offset, obd_off count)
485 {
486         obd_off stripe_off;
487         obd_id  stripe_id;
488         char   *addr;
489         int     delta;
490         int     rc;
491         int     rc2;
492
493         /* no partial pages on the client */
494         LASSERT(count == CFS_PAGE_SIZE);
495
496         addr = cfs_kmap(page);
497
498         for (rc = delta = 0; delta < CFS_PAGE_SIZE; delta += OBD_ECHO_BLOCK_SIZE) {
499                 stripe_off = offset + delta;
500                 stripe_id = id;
501                 echo_get_stripe_off_id (lsm, &stripe_off, &stripe_id);
502
503                 rc2 = block_debug_check("test_brw",
504                                         addr + delta, OBD_ECHO_BLOCK_SIZE,
505                                         stripe_off, stripe_id);
506                 if (rc2 != 0) {
507                         CERROR ("Error in echo object "LPX64"\n", id);
508                         rc = rc2;
509                 }
510         }
511
512         cfs_kunmap(page);
513         return rc;
514 }
515
516 static int echo_client_kbrw(struct obd_device *obd, int rw, struct obdo *oa,
517                             struct lov_stripe_md *lsm, obd_off offset,
518                             obd_size count, struct obd_trans_info *oti)
519 {
520         struct echo_client_obd *ec = &obd->u.echo_client;
521         struct obd_info         oinfo = { { { 0 } } };
522         obd_count               npages;
523         struct ptlrpc_request_set *set = NULL;
524         struct brw_page        *pga;
525         struct brw_page        *pgp;
526         obd_off                 off;
527         int                     i;
528         int                     rc;
529         int                     verify;
530         int                     gfp_mask;
531         int                     brw_flags = 0;
532
533         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
534                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
535                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
536
537         gfp_mask = ((oa->o_id & 2) == 0) ? CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
538
539         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
540         LASSERT(lsm != NULL);
541         LASSERT(lsm->lsm_object_id == oa->o_id);
542
543         if (count <= 0 ||
544             (count & (~CFS_PAGE_MASK)) != 0)
545                 return (-EINVAL);
546
547         if (rw == OBD_BRW_WRITE)
548                 brw_flags = OBD_BRW_ASYNC;
549
550         set =  ptlrpc_prep_set();
551         if (set == NULL)
552                 RETURN(-ENOMEM);
553
554         /* XXX think again with misaligned I/O */
555         npages = count >> CFS_PAGE_SHIFT;
556
557         OBD_ALLOC(pga, npages * sizeof(*pga));
558         if (pga == NULL) {
559                 rc = -ENOMEM;
560                 goto out_set;
561         }
562
563         for (i = 0, pgp = pga, off = offset;
564              i < npages;
565              i++, pgp++, off += CFS_PAGE_SIZE) {
566
567                 LASSERT (pgp->pg == NULL);      /* for cleanup */
568
569                 rc = -ENOMEM;
570                 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
571                 if (pgp->pg == NULL)
572                         goto out;
573
574                 pgp->count = CFS_PAGE_SIZE;
575                 pgp->off = off;
576                 pgp->flag = brw_flags;
577
578                 if (verify)
579                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
580                                                      oa->o_id, off, pgp->count);
581         }
582
583         oinfo.oi_oa = oa;
584         oinfo.oi_md = lsm;
585
586         /* OST/filter device don't support o_brw_async ops, turn to o_brw ops */
587         if (ec->ec_exp && ec->ec_exp->exp_obd &&
588             OBT(ec->ec_exp->exp_obd) && OBP(ec->ec_exp->exp_obd, brw_async)) {
589                 rc = obd_brw_async(rw, ec->ec_exp, &oinfo, npages, pga, oti,
590                                    set, 0);
591                 if (rc == 0) {
592                         rc = ptlrpc_set_wait(set);
593                         if (rc)
594                                 CERROR("error from callback: rc = %d\n", rc);
595                 }
596         } else {
597                 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
598         }
599         if (rc)
600                 CDEBUG_LIMIT(rc == -ENOSPC ? D_INODE : D_ERROR,
601                              "error from obd_brw_async: rc = %d\n", rc);
602  out:
603         if (rc != 0 || rw != OBD_BRW_READ)
604                 verify = 0;
605
606         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
607                 if (pgp->pg == NULL)
608                         continue;
609
610                 if (verify) {
611                         int vrc;
612                         vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
613                                                            pgp->off, pgp->count);
614                         if (vrc != 0 && rc == 0)
615                                 rc = vrc;
616                 }
617                 OBD_PAGE_FREE(pgp->pg);
618         }
619         OBD_FREE(pga, npages * sizeof(*pga));
620  out_set:
621         ptlrpc_set_destroy(set);
622         return (rc);
623 }
624
625 struct echo_async_state;
626
627 #define EAP_MAGIC 79277927
628 struct echo_async_page {
629         int                     eap_magic;
630         cfs_page_t             *eap_page;
631         void                    *eap_cookie;
632         obd_off                 eap_off;
633         struct echo_async_state *eap_eas;
634         struct list_head        eap_item;
635 };
636
637 #define EAP_FROM_COOKIE(c)                                                      \
638         (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC),      \
639          (struct echo_async_page *)(c))
640
641 struct echo_async_state {
642         spinlock_t              eas_lock;
643         obd_off                 eas_next_offset;
644         obd_off                 eas_end_offset;
645         int                     eas_in_flight;
646         int                     eas_rc;
647         cfs_waitq_t             eas_waitq;
648         struct list_head        eas_avail;
649         struct obdo             eas_oa;
650         struct lov_stripe_md    *eas_lsm;
651 };
652
653 static int eas_should_wake(struct echo_async_state *eas)
654 {
655         int rc = 0;
656
657         spin_lock(&eas->eas_lock);
658         if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
659             rc = 1;
660         spin_unlock(&eas->eas_lock);
661         return rc;
662 };
663
664 static int ec_ap_make_ready(void *data, int cmd)
665 {
666         /* our pages are issued ready */
667         LBUG();
668         return 0;
669 }
670 static int ec_ap_refresh_count(void *data, int cmd)
671 {
672         /* our pages are issued with a stable count */
673         LBUG();
674         return CFS_PAGE_SIZE;
675 }
676 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
677 {
678         struct echo_async_page *eap = EAP_FROM_COOKIE(data);
679
680         lustre_set_wire_obdo(oa, &eap->eap_eas->eas_oa);
681 }
682
683 static int ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
684 {
685         struct echo_async_page *eap = EAP_FROM_COOKIE(data);
686         struct echo_async_state *eas;
687
688         eas = eap->eap_eas;
689
690         if (cmd == OBD_BRW_READ &&
691             eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
692             (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
693             (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
694                 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
695                                              eas->eas_oa.o_id, eap->eap_off,
696                                              CFS_PAGE_SIZE);
697
698         spin_lock(&eas->eas_lock);
699         if (rc && !eas->eas_rc)
700                 eas->eas_rc = rc;
701         eas->eas_in_flight--;
702         list_add(&eap->eap_item, &eas->eas_avail);
703         cfs_waitq_signal(&eas->eas_waitq);
704         spin_unlock(&eas->eas_lock);
705         return 0;
706 }
707
708 static struct obd_async_page_ops ec_async_page_ops = {
709         .ap_make_ready =        ec_ap_make_ready,
710         .ap_refresh_count =     ec_ap_refresh_count,
711         .ap_fill_obdo =         ec_ap_fill_obdo,
712         .ap_completion =        ec_ap_completion,
713 };
714
715 static int echo_client_async_page(struct obd_export *exp, int rw,
716                                    struct obdo *oa, struct lov_stripe_md *lsm,
717                                    obd_off offset, obd_size count,
718                                    obd_size batching)
719 {
720         obd_count npages, i;
721         struct echo_async_page *eap;
722         struct echo_async_state *eas;
723         int rc = 0;
724         struct echo_async_page **aps = NULL;
725         int brw_flags = 0;
726
727         ENTRY;
728 #if 0
729         int                     verify;
730         int                     gfp_mask;
731
732         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
733                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
734                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
735
736         gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
737 #endif
738
739         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
740
741         if (count <= 0 ||
742             (count & (~CFS_PAGE_MASK)) != 0 ||
743             (lsm != NULL &&
744              lsm->lsm_object_id != oa->o_id))
745                 return (-EINVAL);
746
747         /* XXX think again with misaligned I/O */
748         npages = batching >> CFS_PAGE_SHIFT;
749
750         OBD_ALLOC_PTR(eas);
751         if (NULL == eas)
752                 return(-ENOMEM);
753
754         if (rw == OBD_BRW_WRITE)
755                 brw_flags = OBD_BRW_ASYNC;
756
757         memcpy(&eas->eas_oa, oa, sizeof(*oa));
758         eas->eas_next_offset = offset;
759         eas->eas_end_offset = offset + count;
760         spin_lock_init(&eas->eas_lock);
761         cfs_waitq_init(&eas->eas_waitq);
762         eas->eas_in_flight = 0;
763         eas->eas_rc = 0;
764         eas->eas_lsm = lsm;
765         CFS_INIT_LIST_HEAD(&eas->eas_avail);
766
767         OBD_ALLOC(aps, npages * sizeof aps[0]);
768         if (aps == NULL)
769                 GOTO(free_eas, rc = -ENOMEM);
770
771         /* prepare the group of pages that we're going to be keeping
772          * in flight */
773         for (i = 0; i < npages; i++) {
774                 cfs_page_t *page;
775                 OBD_PAGE_ALLOC(page, CFS_ALLOC_STD);
776                 if (page == NULL)
777                         GOTO(out, rc = -ENOMEM);
778
779                 OBD_ALLOC(eap, sizeof(*eap));
780                 if (eap == NULL) {
781                         OBD_PAGE_FREE(page);
782                         GOTO(out, rc = -ENOMEM);
783                 }
784
785                 eap->eap_magic = EAP_MAGIC;
786                 eap->eap_page = page;
787                 eap->eap_eas = eas;
788                 list_add_tail(&eap->eap_item, &eas->eas_avail);
789                 aps[i] = eap;
790         }
791
792         /* first we spin queueing io and being woken by its completion */
793         spin_lock(&eas->eas_lock);
794         for(;;) {
795                 int rc;
796
797                 /* sleep until we have a page to send */
798                 spin_unlock(&eas->eas_lock);
799                 rc = wait_event_interruptible(eas->eas_waitq,
800                                               eas_should_wake(eas));
801                 spin_lock(&eas->eas_lock);
802                 if (rc && !eas->eas_rc)
803                         eas->eas_rc = rc;
804                 if (eas->eas_rc)
805                         break;
806                 if (list_empty(&eas->eas_avail))
807                         continue;
808                 eap = list_entry(eas->eas_avail.next, struct echo_async_page,
809                                  eap_item);
810                 list_del(&eap->eap_item);
811                 spin_unlock(&eas->eas_lock);
812
813                 /* unbind the eap from its old page offset */
814                 if (eap->eap_cookie != NULL) {
815                         obd_teardown_async_page(exp, lsm, NULL,
816                                                 eap->eap_cookie);
817                         eap->eap_cookie = NULL;
818                 }
819
820                 eas->eas_next_offset += CFS_PAGE_SIZE;
821                 eap->eap_off = eas->eas_next_offset;
822
823                 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
824                                          eap->eap_off, &ec_async_page_ops,
825                                          eap, &eap->eap_cookie,
826                                          OBD_PAGE_NO_CACHE, NULL);
827                 if (rc) {
828                         spin_lock(&eas->eas_lock);
829                         eas->eas_rc = rc;
830                         break;
831                 }
832
833                 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
834                     (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
835                     (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
836                         echo_client_page_debug_setup(lsm, eap->eap_page, rw,
837                                                      oa->o_id,
838                                                      eap->eap_off, CFS_PAGE_SIZE);
839
840                 /* always asserts urgent, which isn't quite right */
841                 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
842                                         rw, 0, CFS_PAGE_SIZE, brw_flags,
843                                         ASYNC_READY | ASYNC_URGENT |
844                                         ASYNC_COUNT_STABLE);
845                 spin_lock(&eas->eas_lock);
846                 if (rc && !eas->eas_rc) {
847                         eas->eas_rc = rc;
848                         break;
849                 }
850                 eas->eas_in_flight++;
851                 if (eas->eas_next_offset == eas->eas_end_offset)
852                         break;
853         }
854
855         /* still hold the eas_lock here.. */
856
857         /* now we just spin waiting for all the rpcs to complete */
858         while(eas->eas_in_flight) {
859                 spin_unlock(&eas->eas_lock);
860                 wait_event_interruptible(eas->eas_waitq,
861                                          eas->eas_in_flight == 0);
862                 spin_lock(&eas->eas_lock);
863         }
864         spin_unlock(&eas->eas_lock);
865
866 out:
867         if (aps != NULL) {
868                 for (i = 0; i < npages; ++ i) {
869                         eap = aps[i];
870                         if (eap != NULL) {
871                                 cfs_page_t *page;
872
873                                 page = eap->eap_page;
874                                 if (eap->eap_cookie != NULL)
875                                         obd_teardown_async_page(exp, lsm, NULL,
876                                                                 eap->eap_cookie);
877                                 OBD_FREE(eap, sizeof(*eap));
878                                 OBD_PAGE_FREE(page);
879                         }
880                 }
881                 OBD_FREE(aps, npages * sizeof aps[0]);
882         }
883 free_eas:
884         OBD_FREE_PTR(eas);
885
886         RETURN(rc);
887 }
888
889 static int echo_client_prep_commit(struct obd_export *exp, int rw,
890                                    struct obdo *oa, struct lov_stripe_md *lsm,
891                                    obd_off offset, obd_size count,
892                                    obd_size batch, struct obd_trans_info *oti)
893 {
894         struct obd_ioobj ioo;
895         struct niobuf_local *lnb;
896         struct niobuf_remote *rnb;
897         obd_off off;
898         obd_size npages, tot_pages;
899         int i, ret = 0;
900         ENTRY;
901
902         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
903             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
904                 RETURN(-EINVAL);
905
906         npages = batch >> CFS_PAGE_SHIFT;
907         tot_pages = count >> CFS_PAGE_SHIFT;
908
909         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
910         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
911
912         if (lnb == NULL || rnb == NULL)
913                 GOTO(out, ret = -ENOMEM);
914
915         obdo_to_ioobj(oa, &ioo);
916
917         off = offset;
918
919         for(; tot_pages; tot_pages -= npages) {
920                 int lpages;
921
922                 if (tot_pages < npages)
923                         npages = tot_pages;
924
925                 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
926                         rnb[i].offset = off;
927                         rnb[i].len = CFS_PAGE_SIZE;
928                 }
929
930                 ioo.ioo_bufcnt = npages;
931                 oti->oti_transno = 0;
932
933                 lpages = npages;
934                 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti);
935                 if (ret != 0)
936                         GOTO(out, ret);
937                 LASSERT(lpages == npages);
938
939                 for (i = 0; i < lpages; i++) {
940                         cfs_page_t *page = lnb[i].page;
941
942                         /* read past eof? */
943                         if (page == NULL && lnb[i].rc == 0)
944                                 continue;
945
946                         if (oa->o_id == ECHO_PERSISTENT_OBJID ||
947                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
948                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
949                                 continue;
950
951                         if (rw == OBD_BRW_WRITE)
952                                 echo_client_page_debug_setup(lsm, page, rw,
953                                                              oa->o_id,
954                                                              rnb[i].offset,
955                                                              rnb[i].len);
956                         else
957                                 echo_client_page_debug_check(lsm, page,
958                                                              oa->o_id,
959                                                              rnb[i].offset,
960                                                              rnb[i].len);
961                 }
962
963                 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
964                 if (ret != 0)
965                         GOTO(out, ret);
966         }
967
968 out:
969         if (lnb)
970                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
971         if (rnb)
972                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
973         RETURN(ret);
974 }
975
976 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
977                           struct obd_ioctl_data *data)
978 {
979         struct obd_device *obd = class_exp2obd(exp);
980         struct echo_client_obd *ec = &obd->u.echo_client;
981         struct obd_trans_info dummy_oti = { .oti_thread = NULL };
982         struct ec_object *eco;
983         int rc;
984         ENTRY;
985
986         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
987         if (rc)
988                 RETURN(rc);
989
990         data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
991         data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
992         data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
993
994         switch((long)data->ioc_pbuf1) {
995         case 1:
996                 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
997                                       eco->eco_lsm, data->ioc_offset,
998                                       data->ioc_count, &dummy_oti);
999                 break;
1000         case 2:
1001                 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
1002                                            eco->eco_lsm, data->ioc_offset,
1003                                            data->ioc_count, data->ioc_plen1);
1004                 break;
1005         case 3:
1006                 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1007                                             eco->eco_lsm, data->ioc_offset,
1008                                             data->ioc_count, data->ioc_plen1,
1009                                             &dummy_oti);
1010                 break;
1011         default:
1012                 rc = -EINVAL;
1013         }
1014         echo_put_object(eco);
1015         RETURN(rc);
1016 }
1017
1018 static int
1019 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1020                     void *data, int flag)
1021 {
1022         struct ec_object       *eco = (struct ec_object *)data;
1023         struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1024         struct lustre_handle    lockh;
1025         struct list_head       *el;
1026         int                     found = 0;
1027         int                     rc;
1028
1029         ldlm_lock2handle (lock, &lockh);
1030
1031         /* #ifdef this out if we're not feeling paranoid */
1032         spin_lock (&ec->ec_lock);
1033         list_for_each (el, &ec->ec_objects) {
1034                 found = (eco == list_entry(el, struct ec_object,
1035                                            eco_obj_chain));
1036                 if (found)
1037                         break;
1038         }
1039         spin_unlock (&ec->ec_lock);
1040         LASSERT (found);
1041
1042         switch (flag) {
1043         case LDLM_CB_BLOCKING:
1044                 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1045                        eco->eco_id, lockh.cookie);
1046                 rc = ldlm_cli_cancel (&lockh);
1047                 if (rc != ELDLM_OK)
1048                         CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1049                 break;
1050
1051         case LDLM_CB_CANCELING:
1052                 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1053                        eco->eco_id, lockh.cookie);
1054                 break;
1055
1056         default:
1057                 LBUG ();
1058         }
1059
1060         return (0);
1061 }
1062
1063 static int
1064 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1065                     int mode, obd_off offset, obd_size nob)
1066 {
1067         struct obd_device      *obd = exp->exp_obd;
1068         struct echo_client_obd *ec = &obd->u.echo_client;
1069         struct lustre_handle   *ulh = &oa->o_handle;
1070         struct ldlm_enqueue_info einfo = { 0 };
1071         struct obd_info oinfo = { { { 0 } } };
1072         struct ec_object       *eco;
1073         struct ec_lock         *ecl;
1074         int                     rc;
1075
1076         if (!(mode == LCK_PR || mode == LCK_PW))
1077                 return -EINVAL;
1078
1079         if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1080             (nob & (~CFS_PAGE_MASK)) != 0)
1081                 return -EINVAL;
1082
1083         rc = echo_get_object (&eco, obd, oa);
1084         if (rc != 0)
1085                 return rc;
1086
1087         rc = -ENOMEM;
1088         OBD_ALLOC (ecl, sizeof (*ecl));
1089         if (ecl == NULL)
1090                 goto failed_0;
1091
1092         ecl->ecl_mode = mode;
1093         ecl->ecl_object = eco;
1094         ecl->ecl_policy.l_extent.start = offset;
1095         ecl->ecl_policy.l_extent.end =
1096                 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1097
1098         einfo.ei_type = LDLM_EXTENT;
1099         einfo.ei_mode = mode;
1100         einfo.ei_cb_bl = echo_ldlm_callback;
1101         einfo.ei_cb_cp = ldlm_completion_ast;
1102         einfo.ei_cb_gl = NULL;
1103         einfo.ei_cbdata = eco;
1104
1105         oinfo.oi_policy = ecl->ecl_policy;
1106         oinfo.oi_lockh = &ecl->ecl_lock_handle;
1107         oinfo.oi_md = eco->eco_lsm;
1108         rc = obd_enqueue(ec->ec_exp, &oinfo, &einfo, NULL);
1109         if (rc != 0)
1110                 goto failed_1;
1111
1112         CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1113
1114         /* NB ecl takes object ref from echo_get_object() above */
1115         spin_lock(&ec->ec_lock);
1116
1117         list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1118         ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1119
1120         spin_unlock(&ec->ec_lock);
1121
1122         oa->o_valid |= OBD_MD_FLHANDLE;
1123         return 0;
1124
1125  failed_1:
1126         OBD_FREE (ecl, sizeof (*ecl));
1127  failed_0:
1128         echo_put_object (eco);
1129         return (rc);
1130 }
1131
1132 static int
1133 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1134 {
1135         struct obd_device      *obd = exp->exp_obd;
1136         struct echo_client_obd *ec = &obd->u.echo_client;
1137         struct lustre_handle   *ulh = &oa->o_handle;
1138         struct ec_lock         *ecl = NULL;
1139         int                     found = 0;
1140         struct list_head       *el;
1141         int                     rc;
1142
1143         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1144                 return -EINVAL;
1145
1146         spin_lock (&ec->ec_lock);
1147
1148         list_for_each (el, &exp->exp_ec_data.eced_locks) {
1149                 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1150                 found = (ecl->ecl_cookie == ulh->cookie);
1151                 if (found) {
1152                         list_del (&ecl->ecl_exp_chain);
1153                         break;
1154                 }
1155         }
1156
1157         spin_unlock (&ec->ec_lock);
1158
1159         if (!found)
1160                 return (-ENOENT);
1161
1162         rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1163                         &ecl->ecl_lock_handle, 0, 0);
1164
1165         echo_put_object (ecl->ecl_object);
1166         OBD_FREE (ecl, sizeof (*ecl));
1167
1168         return rc;
1169 }
1170
1171 static int
1172 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1173                       int len, void *karg, void *uarg)
1174 {
1175         struct obd_device      *obd;
1176         struct echo_client_obd *ec;
1177         struct ec_object       *eco;
1178         struct obd_ioctl_data  *data = karg;
1179         struct obd_trans_info   dummy_oti;
1180         struct oti_req_ack_lock *ack_lock;
1181         struct obdo            *oa;
1182         int                     rw = OBD_BRW_READ;
1183         int                     rc = 0;
1184         int                     i;
1185         ENTRY;
1186
1187 #ifndef HAVE_UNLOCKED_IOCTL
1188         unlock_kernel();
1189 #endif
1190         memset(&dummy_oti, 0, sizeof(dummy_oti));
1191
1192         obd = exp->exp_obd;
1193         ec = &obd->u.echo_client;
1194
1195         switch (cmd) {
1196         case OBD_IOC_CREATE:                    /* may create echo object */
1197                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1198                         GOTO (out, rc = -EPERM);
1199
1200                 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1201                                          data->ioc_pbuf1, data->ioc_plen1,
1202                                          &dummy_oti);
1203                 GOTO(out, rc);
1204
1205         case OBD_IOC_DESTROY:
1206                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1207                         GOTO (out, rc = -EPERM);
1208                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1209                 if (rc == 0) {
1210                         oa = &data->ioc_obdo1;
1211                         oa->o_gr = FILTER_GROUP_ECHO;
1212                         oa->o_valid |= OBD_MD_FLGROUP;
1213                         rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1214                                          &dummy_oti, NULL);
1215                         if (rc == 0)
1216                                 eco->eco_deleted = 1;
1217                         echo_put_object(eco);
1218                 }
1219                 GOTO(out, rc);
1220
1221         case OBD_IOC_GETATTR:
1222                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1223                 if (rc == 0) {
1224                         struct obd_info oinfo = { { { 0 } } };
1225                         oinfo.oi_md = eco->eco_lsm;
1226                         oinfo.oi_oa = &data->ioc_obdo1;
1227                         rc = obd_getattr(ec->ec_exp, &oinfo);
1228                         echo_put_object(eco);
1229                 }
1230                 GOTO(out, rc);
1231
1232         case OBD_IOC_SETATTR:
1233                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1234                         GOTO (out, rc = -EPERM);
1235
1236                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1237                 if (rc == 0) {
1238                         struct obd_info oinfo = { { { 0 } } };
1239                         oinfo.oi_oa = &data->ioc_obdo1;
1240                         oinfo.oi_md = eco->eco_lsm;
1241
1242                         rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1243                         echo_put_object(eco);
1244                 }
1245                 GOTO(out, rc);
1246
1247         case OBD_IOC_BRW_WRITE:
1248                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1249                         GOTO (out, rc = -EPERM);
1250
1251                 rw = OBD_BRW_WRITE;
1252                 /* fall through */
1253         case OBD_IOC_BRW_READ:
1254                 rc = echo_client_brw_ioctl(rw, exp, data);
1255                 GOTO(out, rc);
1256
1257         case ECHO_IOC_GET_STRIPE:
1258                 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1259                 if (rc == 0) {
1260                         rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1261                                               data->ioc_plen1);
1262                         echo_put_object(eco);
1263                 }
1264                 GOTO(out, rc);
1265
1266         case ECHO_IOC_SET_STRIPE:
1267                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1268                         GOTO (out, rc = -EPERM);
1269
1270                 if (data->ioc_pbuf1 == NULL) {  /* unset */
1271                         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1272                         if (rc == 0) {
1273                                 eco->eco_deleted = 1;
1274                                 echo_put_object(eco);
1275                         }
1276                 } else {
1277                         rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1278                                                 data->ioc_pbuf1,
1279                                                 data->ioc_plen1, &dummy_oti);
1280                 }
1281                 GOTO (out, rc);
1282
1283         case ECHO_IOC_ENQUEUE:
1284                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1285                         GOTO (out, rc = -EPERM);
1286
1287                 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1288                                          data->ioc_conn1, /* lock mode */
1289                                    data->ioc_offset, data->ioc_count);/*extent*/
1290                 GOTO (out, rc);
1291
1292         case ECHO_IOC_CANCEL:
1293                 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1294                 GOTO (out, rc);
1295
1296         default:
1297                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1298                 GOTO (out, rc = -ENOTTY);
1299         }
1300
1301         EXIT;
1302  out:
1303
1304         /* XXX this should be in a helper also called by target_send_reply */
1305         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1306              i++, ack_lock++) {
1307                 if (!ack_lock->mode)
1308                         break;
1309                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1310         }
1311
1312 #ifndef HAVE_UNLOCKED_IOCTL
1313         lock_kernel();
1314 #endif
1315         return rc;
1316 }
1317
1318 static int
1319 echo_client_setup(struct obd_device *obddev, obd_count len, void *buf)
1320 {
1321         struct lustre_cfg* lcfg = buf;
1322         struct echo_client_obd *ec = &obddev->u.echo_client;
1323         struct obd_device *tgt;
1324         struct lustre_handle conn = {0, };
1325         struct obd_uuid echo_uuid = { "ECHO_UUID" };
1326         struct obd_connect_data *ocd = NULL;
1327         int rc;
1328         ENTRY;
1329
1330         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1331                 CERROR("requires a TARGET OBD name\n");
1332                 RETURN(-EINVAL);
1333         }
1334
1335         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1336         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1337                 CERROR("device not attached or not set up (%s)\n",
1338                        lustre_cfg_string(lcfg, 1));
1339                 RETURN(-EINVAL);
1340         }
1341
1342         spin_lock_init (&ec->ec_lock);
1343         CFS_INIT_LIST_HEAD (&ec->ec_objects);
1344         ec->ec_unique = 0;
1345
1346         ec->ec_exp = lustre_hash_lookup(tgt->obd_uuid_hash, &echo_uuid);
1347         if (ec->ec_exp)
1348                 RETURN(0);
1349
1350         OBD_ALLOC(ocd, sizeof(*ocd));
1351         if (ocd == NULL) {
1352                 CERROR("Can't alloc ocd connecting to %s\n",
1353                        lustre_cfg_string(lcfg, 1));
1354                 return -ENOMEM;
1355         }
1356         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL;
1357         ocd->ocd_version = LUSTRE_VERSION_CODE;
1358
1359         if ((strncmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME,
1360                      strlen(LUSTRE_OSC_NAME)) == 0) ||
1361             (strncmp(tgt->obd_type->typ_name, LUSTRE_LOV_NAME,
1362                      strlen(LUSTRE_LOV_NAME)) == 0)) {
1363                 rc = obd_connect(&conn, tgt, &echo_uuid, ocd, &ec->ec_exp);
1364         } else {
1365                 rc = obd_connect(&conn, tgt, &echo_uuid, ocd, NULL);
1366                 if (rc == 0) {
1367                         ec->ec_exp = class_conn2export(&conn);
1368
1369                         /* Turn off pinger because it connects to tgt obd directly */
1370                         spin_lock(&tgt->obd_dev_lock);
1371                         list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1372                         spin_unlock(&tgt->obd_dev_lock);
1373                 }
1374         }
1375
1376         OBD_FREE(ocd, sizeof(*ocd));
1377
1378         if (rc == -EALREADY && (strncmp(tgt->obd_type->typ_name,LUSTRE_OSC_NAME,
1379                                         strlen(LUSTRE_OSC_NAME)) == 0)) {
1380                 /* OSC obd forbid reconnect already connected import,
1381                  * so we hack creating another export here */
1382                 down_write(&tgt->u.cli.cl_sem);
1383                 rc = class_connect(&conn, tgt, &echo_uuid);
1384                 if (rc == 0) {
1385                         ++tgt->u.cli.cl_conn_count;
1386                         ec->ec_exp = class_conn2export(&conn);
1387                 }
1388                 up_write(&tgt->u.cli.cl_sem);
1389         }
1390
1391         if (rc != 0)
1392                 CERROR("fail to connect to device %s\n",
1393                        lustre_cfg_string(lcfg, 1));
1394
1395         RETURN(rc);
1396 }
1397
1398 static int echo_client_cleanup(struct obd_device *obddev)
1399 {
1400         struct list_head       *el;
1401         struct ec_object       *eco;
1402         struct echo_client_obd *ec = &obddev->u.echo_client;
1403         int rc;
1404         ENTRY;
1405
1406         if (!list_empty(&obddev->obd_exports)) {
1407                 CERROR("still has clients!\n");
1408                 RETURN(-EBUSY);
1409         }
1410
1411         /* XXX assuming sole access */
1412         while (!list_empty(&ec->ec_objects)) {
1413                 el = ec->ec_objects.next;
1414                 eco = list_entry(el, struct ec_object, eco_obj_chain);
1415
1416                 if (eco->eco_refcount > 0)
1417                         RETURN(-EBUSY);
1418                 eco->eco_refcount = 1;
1419                 eco->eco_deleted = 1;
1420                 echo_put_object(eco);
1421         }
1422
1423         rc = obd_disconnect(ec->ec_exp);
1424         if (rc != 0)
1425                 CERROR("fail to disconnect device: %d\n", rc);
1426
1427         RETURN(rc);
1428 }
1429
1430 static int echo_client_connect(struct lustre_handle *conn,
1431                                struct obd_device *src, struct obd_uuid *cluuid,
1432                                struct obd_connect_data *data, void *localdata)
1433 {
1434         struct obd_export *exp;
1435         int                rc;
1436
1437         ENTRY;
1438         rc = class_connect(conn, src, cluuid);
1439         if (rc == 0) {
1440                 exp = class_conn2export(conn);
1441                 CFS_INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1442                 class_export_put(exp);
1443         }
1444
1445         RETURN (rc);
1446 }
1447
1448 static int echo_client_disconnect(struct obd_export *exp)
1449 {
1450         struct obd_device      *obd;
1451         struct echo_client_obd *ec;
1452         struct ec_lock         *ecl;
1453         int                     rc;
1454         ENTRY;
1455
1456         if (exp == NULL)
1457                 GOTO(out, rc = -EINVAL);
1458
1459         obd = exp->exp_obd;
1460         ec = &obd->u.echo_client;
1461
1462         /* no more contention on export's lock list */
1463         while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1464                 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1465                                   struct ec_lock, ecl_exp_chain);
1466                 list_del (&ecl->ecl_exp_chain);
1467
1468                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1469                                  ecl->ecl_mode, &ecl->ecl_lock_handle, 0, 0);
1470
1471                 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1472                         "(%d)\n", ecl->ecl_object->eco_id, rc);
1473
1474                 echo_put_object (ecl->ecl_object);
1475                 OBD_FREE (ecl, sizeof (*ecl));
1476         }
1477
1478         rc = class_disconnect(exp);
1479         GOTO(out, rc);
1480  out:
1481         return rc;
1482 }
1483
1484 static struct obd_ops echo_obd_ops = {
1485         .o_owner       = THIS_MODULE,
1486         .o_setup       = echo_client_setup,
1487         .o_cleanup     = echo_client_cleanup,
1488         .o_iocontrol   = echo_client_iocontrol,
1489         .o_connect     = echo_client_connect,
1490         .o_disconnect  = echo_client_disconnect
1491 };
1492
1493 int echo_client_init(void)
1494 {
1495         struct lprocfs_static_vars lvars = { 0 };
1496
1497         lprocfs_echo_init_vars(&lvars);
1498         return class_register_type(&echo_obd_ops, lvars.module_vars,
1499                                    LUSTRE_ECHO_CLIENT_NAME);
1500 }
1501
1502 void echo_client_exit(void)
1503 {
1504         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
1505 }