Whamcloud - gitweb
b=20407 replay-ost-single: do not skip for HARD mode and mixed_ost_devs
[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                 return (-ENOMEM);
560
561         for (i = 0, pgp = pga, off = offset;
562              i < npages;
563              i++, pgp++, off += CFS_PAGE_SIZE) {
564
565                 LASSERT (pgp->pg == NULL);      /* for cleanup */
566
567                 rc = -ENOMEM;
568                 OBD_PAGE_ALLOC(pgp->pg, gfp_mask);
569                 if (pgp->pg == NULL)
570                         goto out;
571
572                 pgp->count = CFS_PAGE_SIZE;
573                 pgp->off = off;
574                 pgp->flag = brw_flags;
575
576                 if (verify)
577                         echo_client_page_debug_setup(lsm, pgp->pg, rw,
578                                                      oa->o_id, off, pgp->count);
579         }
580
581         oinfo.oi_oa = oa;
582         oinfo.oi_md = lsm;
583
584         /* OST/filter device don't support o_brw_async ops, turn to o_brw ops */
585         if (ec->ec_exp && ec->ec_exp->exp_obd &&
586             OBT(ec->ec_exp->exp_obd) && OBP(ec->ec_exp->exp_obd, brw_async)) {
587                 rc = obd_brw_async(rw, ec->ec_exp, &oinfo, npages, pga, oti,
588                                    set, 0);
589                 if (rc == 0) {
590                         rc = ptlrpc_set_wait(set);
591                         if (rc)
592                                 CERROR("error from callback: rc = %d\n", rc);
593                 }
594         } else {
595                 rc = obd_brw(rw, ec->ec_exp, &oinfo, npages, pga, oti);
596         }
597         if (rc)
598                 CDEBUG_LIMIT(rc == -ENOSPC ? D_INODE : D_ERROR,
599                              "error from obd_brw_async: rc = %d\n", rc);
600         ptlrpc_set_destroy(set);
601  out:
602         if (rc != 0 || rw != OBD_BRW_READ)
603                 verify = 0;
604
605         for (i = 0, pgp = pga; i < npages; i++, pgp++) {
606                 if (pgp->pg == NULL)
607                         continue;
608
609                 if (verify) {
610                         int vrc;
611                         vrc = echo_client_page_debug_check(lsm, pgp->pg, oa->o_id,
612                                                            pgp->off, pgp->count);
613                         if (vrc != 0 && rc == 0)
614                                 rc = vrc;
615                 }
616                 OBD_PAGE_FREE(pgp->pg);
617         }
618         OBD_FREE(pga, npages * sizeof(*pga));
619         return (rc);
620 }
621
622 struct echo_async_state;
623
624 #define EAP_MAGIC 79277927
625 struct echo_async_page {
626         int                     eap_magic;
627         cfs_page_t             *eap_page;
628         void                    *eap_cookie;
629         obd_off                 eap_off;
630         struct echo_async_state *eap_eas;
631         struct list_head        eap_item;
632 };
633
634 #define EAP_FROM_COOKIE(c)                                                      \
635         (LASSERT(((struct echo_async_page *)(c))->eap_magic == EAP_MAGIC),      \
636          (struct echo_async_page *)(c))
637
638 struct echo_async_state {
639         spinlock_t              eas_lock;
640         obd_off                 eas_next_offset;
641         obd_off                 eas_end_offset;
642         int                     eas_in_flight;
643         int                     eas_rc;
644         cfs_waitq_t             eas_waitq;
645         struct list_head        eas_avail;
646         struct obdo             eas_oa;
647         struct lov_stripe_md    *eas_lsm;
648 };
649
650 static int eas_should_wake(struct echo_async_state *eas)
651 {
652         int rc = 0;
653
654         spin_lock(&eas->eas_lock);
655         if (eas->eas_rc == 0 && !list_empty(&eas->eas_avail))
656             rc = 1;
657         spin_unlock(&eas->eas_lock);
658         return rc;
659 };
660
661 static int ec_ap_make_ready(void *data, int cmd)
662 {
663         /* our pages are issued ready */
664         LBUG();
665         return 0;
666 }
667 static int ec_ap_refresh_count(void *data, int cmd)
668 {
669         /* our pages are issued with a stable count */
670         LBUG();
671         return CFS_PAGE_SIZE;
672 }
673 static void ec_ap_fill_obdo(void *data, int cmd, struct obdo *oa)
674 {
675         struct echo_async_page *eap = EAP_FROM_COOKIE(data);
676
677         lustre_set_wire_obdo(oa, &eap->eap_eas->eas_oa);
678 }
679
680 static int ec_ap_completion(void *data, int cmd, struct obdo *oa, int rc)
681 {
682         struct echo_async_page *eap = EAP_FROM_COOKIE(data);
683         struct echo_async_state *eas;
684
685         eas = eap->eap_eas;
686
687         if (cmd == OBD_BRW_READ &&
688             eas->eas_oa.o_id != ECHO_PERSISTENT_OBJID &&
689             (eas->eas_oa.o_valid & OBD_MD_FLFLAGS) != 0 &&
690             (eas->eas_oa.o_flags & OBD_FL_DEBUG_CHECK) != 0)
691                 echo_client_page_debug_check(eas->eas_lsm, eap->eap_page,
692                                              eas->eas_oa.o_id, eap->eap_off,
693                                              CFS_PAGE_SIZE);
694
695         spin_lock(&eas->eas_lock);
696         if (rc && !eas->eas_rc)
697                 eas->eas_rc = rc;
698         eas->eas_in_flight--;
699         list_add(&eap->eap_item, &eas->eas_avail);
700         cfs_waitq_signal(&eas->eas_waitq);
701         spin_unlock(&eas->eas_lock);
702         return 0;
703 }
704
705 static struct obd_async_page_ops ec_async_page_ops = {
706         .ap_make_ready =        ec_ap_make_ready,
707         .ap_refresh_count =     ec_ap_refresh_count,
708         .ap_fill_obdo =         ec_ap_fill_obdo,
709         .ap_completion =        ec_ap_completion,
710 };
711
712 static int echo_client_async_page(struct obd_export *exp, int rw,
713                                    struct obdo *oa, struct lov_stripe_md *lsm,
714                                    obd_off offset, obd_size count,
715                                    obd_size batching)
716 {
717         obd_count npages, i;
718         struct echo_async_page *eap;
719         struct echo_async_state *eas;
720         int rc = 0;
721         struct echo_async_page **aps = NULL;
722         int brw_flags = 0;
723
724         ENTRY;
725 #if 0
726         int                     verify;
727         int                     gfp_mask;
728
729         verify = ((oa->o_id) != ECHO_PERSISTENT_OBJID &&
730                   (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
731                   (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
732
733         gfp_mask = ((oa->o_id & 2) == 0) ? GFP_KERNEL : GFP_HIGHUSER;
734 #endif
735
736         LASSERT(rw == OBD_BRW_WRITE || rw == OBD_BRW_READ);
737
738         if (count <= 0 ||
739             (count & (~CFS_PAGE_MASK)) != 0 ||
740             (lsm != NULL &&
741              lsm->lsm_object_id != oa->o_id))
742                 return (-EINVAL);
743
744         /* XXX think again with misaligned I/O */
745         npages = batching >> CFS_PAGE_SHIFT;
746
747         OBD_ALLOC_PTR(eas);
748         if (NULL == eas)
749                 return(-ENOMEM);
750
751         if (rw == OBD_BRW_WRITE)
752                 brw_flags = OBD_BRW_ASYNC;
753
754         memcpy(&eas->eas_oa, oa, sizeof(*oa));
755         eas->eas_next_offset = offset;
756         eas->eas_end_offset = offset + count;
757         spin_lock_init(&eas->eas_lock);
758         cfs_waitq_init(&eas->eas_waitq);
759         eas->eas_in_flight = 0;
760         eas->eas_rc = 0;
761         eas->eas_lsm = lsm;
762         CFS_INIT_LIST_HEAD(&eas->eas_avail);
763
764         OBD_ALLOC(aps, npages * sizeof aps[0]);
765         if (aps == NULL)
766                 GOTO(free_eas, rc = -ENOMEM);
767
768         /* prepare the group of pages that we're going to be keeping
769          * in flight */
770         for (i = 0; i < npages; i++) {
771                 cfs_page_t *page;
772                 OBD_PAGE_ALLOC(page, CFS_ALLOC_STD);
773                 if (page == NULL)
774                         GOTO(out, rc = -ENOMEM);
775
776                 OBD_ALLOC(eap, sizeof(*eap));
777                 if (eap == NULL) {
778                         OBD_PAGE_FREE(page);
779                         GOTO(out, rc = -ENOMEM);
780                 }
781
782                 eap->eap_magic = EAP_MAGIC;
783                 eap->eap_page = page;
784                 eap->eap_eas = eas;
785                 list_add_tail(&eap->eap_item, &eas->eas_avail);
786                 aps[i] = eap;
787         }
788
789         /* first we spin queueing io and being woken by its completion */
790         spin_lock(&eas->eas_lock);
791         for(;;) {
792                 int rc;
793
794                 /* sleep until we have a page to send */
795                 spin_unlock(&eas->eas_lock);
796                 rc = wait_event_interruptible(eas->eas_waitq,
797                                               eas_should_wake(eas));
798                 spin_lock(&eas->eas_lock);
799                 if (rc && !eas->eas_rc)
800                         eas->eas_rc = rc;
801                 if (eas->eas_rc)
802                         break;
803                 if (list_empty(&eas->eas_avail))
804                         continue;
805                 eap = list_entry(eas->eas_avail.next, struct echo_async_page,
806                                  eap_item);
807                 list_del(&eap->eap_item);
808                 spin_unlock(&eas->eas_lock);
809
810                 /* unbind the eap from its old page offset */
811                 if (eap->eap_cookie != NULL) {
812                         obd_teardown_async_page(exp, lsm, NULL,
813                                                 eap->eap_cookie);
814                         eap->eap_cookie = NULL;
815                 }
816
817                 eas->eas_next_offset += CFS_PAGE_SIZE;
818                 eap->eap_off = eas->eas_next_offset;
819
820                 rc = obd_prep_async_page(exp, lsm, NULL, eap->eap_page,
821                                          eap->eap_off, &ec_async_page_ops,
822                                          eap, &eap->eap_cookie,
823                                          OBD_PAGE_NO_CACHE, NULL);
824                 if (rc) {
825                         spin_lock(&eas->eas_lock);
826                         eas->eas_rc = rc;
827                         break;
828                 }
829
830                 if (oa->o_id != ECHO_PERSISTENT_OBJID &&
831                     (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
832                     (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0)
833                         echo_client_page_debug_setup(lsm, eap->eap_page, rw,
834                                                      oa->o_id,
835                                                      eap->eap_off, CFS_PAGE_SIZE);
836
837                 /* always asserts urgent, which isn't quite right */
838                 rc = obd_queue_async_io(exp, lsm, NULL, eap->eap_cookie,
839                                         rw, 0, CFS_PAGE_SIZE, brw_flags,
840                                         ASYNC_READY | ASYNC_URGENT |
841                                         ASYNC_COUNT_STABLE);
842                 spin_lock(&eas->eas_lock);
843                 if (rc && !eas->eas_rc) {
844                         eas->eas_rc = rc;
845                         break;
846                 }
847                 eas->eas_in_flight++;
848                 if (eas->eas_next_offset == eas->eas_end_offset)
849                         break;
850         }
851
852         /* still hold the eas_lock here.. */
853
854         /* now we just spin waiting for all the rpcs to complete */
855         while(eas->eas_in_flight) {
856                 spin_unlock(&eas->eas_lock);
857                 wait_event_interruptible(eas->eas_waitq,
858                                          eas->eas_in_flight == 0);
859                 spin_lock(&eas->eas_lock);
860         }
861         spin_unlock(&eas->eas_lock);
862
863 out:
864         if (aps != NULL) {
865                 for (i = 0; i < npages; ++ i) {
866                         cfs_page_t *page;
867
868                         eap = aps[i];
869                         page = eap->eap_page;
870                         if (eap->eap_cookie != NULL)
871                                 obd_teardown_async_page(exp, lsm, NULL,
872                                                         eap->eap_cookie);
873                         OBD_FREE(eap, sizeof(*eap));
874                         OBD_PAGE_FREE(page);
875                 }
876                 OBD_FREE(aps, npages * sizeof aps[0]);
877         }
878 free_eas:
879         OBD_FREE_PTR(eas);
880
881         RETURN(rc);
882 }
883
884 static int echo_client_prep_commit(struct obd_export *exp, int rw,
885                                    struct obdo *oa, struct lov_stripe_md *lsm,
886                                    obd_off offset, obd_size count,
887                                    obd_size batch, struct obd_trans_info *oti)
888 {
889         struct obd_ioobj ioo;
890         struct niobuf_local *lnb;
891         struct niobuf_remote *rnb;
892         obd_off off;
893         obd_size npages, tot_pages;
894         int i, ret = 0;
895         ENTRY;
896
897         if (count <= 0 || (count & (~CFS_PAGE_MASK)) != 0 ||
898             (lsm != NULL && lsm->lsm_object_id != oa->o_id))
899                 RETURN(-EINVAL);
900
901         npages = batch >> CFS_PAGE_SHIFT;
902         tot_pages = count >> CFS_PAGE_SHIFT;
903
904         OBD_ALLOC(lnb, npages * sizeof(struct niobuf_local));
905         OBD_ALLOC(rnb, npages * sizeof(struct niobuf_remote));
906
907         if (lnb == NULL || rnb == NULL)
908                 GOTO(out, ret = -ENOMEM);
909
910         obdo_to_ioobj(oa, &ioo);
911
912         off = offset;
913
914         for(; tot_pages; tot_pages -= npages) {
915                 int lpages;
916
917                 if (tot_pages < npages)
918                         npages = tot_pages;
919
920                 for (i = 0; i < npages; i++, off += CFS_PAGE_SIZE) {
921                         rnb[i].offset = off;
922                         rnb[i].len = CFS_PAGE_SIZE;
923                 }
924
925                 ioo.ioo_bufcnt = npages;
926                 oti->oti_transno = 0;
927
928                 lpages = npages;
929                 ret = obd_preprw(rw, exp, oa, 1, &ioo, rnb, &lpages, lnb, oti);
930                 if (ret != 0)
931                         GOTO(out, ret);
932                 LASSERT(lpages == npages);
933
934                 for (i = 0; i < lpages; i++) {
935                         cfs_page_t *page = lnb[i].page;
936
937                         /* read past eof? */
938                         if (page == NULL && lnb[i].rc == 0)
939                                 continue;
940
941                         if (oa->o_id == ECHO_PERSISTENT_OBJID ||
942                             (oa->o_valid & OBD_MD_FLFLAGS) == 0 ||
943                             (oa->o_flags & OBD_FL_DEBUG_CHECK) == 0)
944                                 continue;
945
946                         if (rw == OBD_BRW_WRITE)
947                                 echo_client_page_debug_setup(lsm, page, rw,
948                                                              oa->o_id,
949                                                              rnb[i].offset,
950                                                              rnb[i].len);
951                         else
952                                 echo_client_page_debug_check(lsm, page,
953                                                              oa->o_id,
954                                                              rnb[i].offset,
955                                                              rnb[i].len);
956                 }
957
958                 ret = obd_commitrw(rw, exp, oa, 1,&ioo,rnb,npages,lnb,oti,ret);
959                 if (ret != 0)
960                         GOTO(out, ret);
961         }
962
963 out:
964         if (lnb)
965                 OBD_FREE(lnb, npages * sizeof(struct niobuf_local));
966         if (rnb)
967                 OBD_FREE(rnb, npages * sizeof(struct niobuf_remote));
968         RETURN(ret);
969 }
970
971 int echo_client_brw_ioctl(int rw, struct obd_export *exp,
972                           struct obd_ioctl_data *data)
973 {
974         struct obd_device *obd = class_exp2obd(exp);
975         struct echo_client_obd *ec = &obd->u.echo_client;
976         struct obd_trans_info dummy_oti = { .oti_thread = NULL };
977         struct ec_object *eco;
978         int rc;
979         ENTRY;
980
981         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
982         if (rc)
983                 RETURN(rc);
984
985         data->ioc_obdo1.o_valid &= ~OBD_MD_FLHANDLE;
986         data->ioc_obdo1.o_valid |= OBD_MD_FLGROUP;
987         data->ioc_obdo1.o_gr = FILTER_GROUP_ECHO;
988
989         switch((long)data->ioc_pbuf1) {
990         case 1:
991                 rc = echo_client_kbrw(obd, rw, &data->ioc_obdo1,
992                                       eco->eco_lsm, data->ioc_offset,
993                                       data->ioc_count, &dummy_oti);
994                 break;
995         case 2:
996                 rc = echo_client_async_page(ec->ec_exp, rw, &data->ioc_obdo1,
997                                            eco->eco_lsm, data->ioc_offset,
998                                            data->ioc_count, data->ioc_plen1);
999                 break;
1000         case 3:
1001                 rc = echo_client_prep_commit(ec->ec_exp, rw, &data->ioc_obdo1,
1002                                             eco->eco_lsm, data->ioc_offset,
1003                                             data->ioc_count, data->ioc_plen1,
1004                                             &dummy_oti);
1005                 break;
1006         default:
1007                 rc = -EINVAL;
1008         }
1009         echo_put_object(eco);
1010         RETURN(rc);
1011 }
1012
1013 static int
1014 echo_ldlm_callback (struct ldlm_lock *lock, struct ldlm_lock_desc *new,
1015                     void *data, int flag)
1016 {
1017         struct ec_object       *eco = (struct ec_object *)data;
1018         struct echo_client_obd *ec = &(eco->eco_device->u.echo_client);
1019         struct lustre_handle    lockh;
1020         struct list_head       *el;
1021         int                     found = 0;
1022         int                     rc;
1023
1024         ldlm_lock2handle (lock, &lockh);
1025
1026         /* #ifdef this out if we're not feeling paranoid */
1027         spin_lock (&ec->ec_lock);
1028         list_for_each (el, &ec->ec_objects) {
1029                 found = (eco == list_entry(el, struct ec_object,
1030                                            eco_obj_chain));
1031                 if (found)
1032                         break;
1033         }
1034         spin_unlock (&ec->ec_lock);
1035         LASSERT (found);
1036
1037         switch (flag) {
1038         case LDLM_CB_BLOCKING:
1039                 CDEBUG(D_INFO, "blocking callback on "LPX64", handle "LPX64"\n",
1040                        eco->eco_id, lockh.cookie);
1041                 rc = ldlm_cli_cancel (&lockh);
1042                 if (rc != ELDLM_OK)
1043                         CERROR ("ldlm_cli_cancel failed: %d\n", rc);
1044                 break;
1045
1046         case LDLM_CB_CANCELING:
1047                 CDEBUG(D_INFO, "cancel callback on "LPX64", handle "LPX64"\n",
1048                        eco->eco_id, lockh.cookie);
1049                 break;
1050
1051         default:
1052                 LBUG ();
1053         }
1054
1055         return (0);
1056 }
1057
1058 static int
1059 echo_client_enqueue(struct obd_export *exp, struct obdo *oa,
1060                     int mode, obd_off offset, obd_size nob)
1061 {
1062         struct obd_device      *obd = exp->exp_obd;
1063         struct echo_client_obd *ec = &obd->u.echo_client;
1064         struct lustre_handle   *ulh = &oa->o_handle;
1065         struct ldlm_enqueue_info einfo = { 0 };
1066         struct obd_info oinfo = { { { 0 } } };
1067         struct ec_object       *eco;
1068         struct ec_lock         *ecl;
1069         int                     rc;
1070
1071         if (!(mode == LCK_PR || mode == LCK_PW))
1072                 return -EINVAL;
1073
1074         if ((offset & (~CFS_PAGE_MASK)) != 0 ||
1075             (nob & (~CFS_PAGE_MASK)) != 0)
1076                 return -EINVAL;
1077
1078         rc = echo_get_object (&eco, obd, oa);
1079         if (rc != 0)
1080                 return rc;
1081
1082         rc = -ENOMEM;
1083         OBD_ALLOC (ecl, sizeof (*ecl));
1084         if (ecl == NULL)
1085                 goto failed_0;
1086
1087         ecl->ecl_mode = mode;
1088         ecl->ecl_object = eco;
1089         ecl->ecl_policy.l_extent.start = offset;
1090         ecl->ecl_policy.l_extent.end =
1091                 (nob == 0) ? ((obd_off) -1) : (offset + nob - 1);
1092
1093         einfo.ei_type = LDLM_EXTENT;
1094         einfo.ei_mode = mode;
1095         einfo.ei_cb_bl = echo_ldlm_callback;
1096         einfo.ei_cb_cp = ldlm_completion_ast;
1097         einfo.ei_cb_gl = NULL;
1098         einfo.ei_cbdata = eco;
1099
1100         oinfo.oi_policy = ecl->ecl_policy;
1101         oinfo.oi_lockh = &ecl->ecl_lock_handle;
1102         oinfo.oi_md = eco->eco_lsm;
1103         rc = obd_enqueue(ec->ec_exp, &oinfo, &einfo, NULL);
1104         if (rc != 0)
1105                 goto failed_1;
1106
1107         CDEBUG(D_INFO, "enqueue handle "LPX64"\n", ecl->ecl_lock_handle.cookie);
1108
1109         /* NB ecl takes object ref from echo_get_object() above */
1110         spin_lock(&ec->ec_lock);
1111
1112         list_add(&ecl->ecl_exp_chain, &exp->exp_ec_data.eced_locks);
1113         ulh->cookie = ecl->ecl_cookie = ec->ec_unique++;
1114
1115         spin_unlock(&ec->ec_lock);
1116
1117         oa->o_valid |= OBD_MD_FLHANDLE;
1118         return 0;
1119
1120  failed_1:
1121         OBD_FREE (ecl, sizeof (*ecl));
1122  failed_0:
1123         echo_put_object (eco);
1124         return (rc);
1125 }
1126
1127 static int
1128 echo_client_cancel(struct obd_export *exp, struct obdo *oa)
1129 {
1130         struct obd_device      *obd = exp->exp_obd;
1131         struct echo_client_obd *ec = &obd->u.echo_client;
1132         struct lustre_handle   *ulh = &oa->o_handle;
1133         struct ec_lock         *ecl = NULL;
1134         int                     found = 0;
1135         struct list_head       *el;
1136         int                     rc;
1137
1138         if ((oa->o_valid & OBD_MD_FLHANDLE) == 0)
1139                 return -EINVAL;
1140
1141         spin_lock (&ec->ec_lock);
1142
1143         list_for_each (el, &exp->exp_ec_data.eced_locks) {
1144                 ecl = list_entry (el, struct ec_lock, ecl_exp_chain);
1145                 found = (ecl->ecl_cookie == ulh->cookie);
1146                 if (found) {
1147                         list_del (&ecl->ecl_exp_chain);
1148                         break;
1149                 }
1150         }
1151
1152         spin_unlock (&ec->ec_lock);
1153
1154         if (!found)
1155                 return (-ENOENT);
1156
1157         rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm, ecl->ecl_mode,
1158                         &ecl->ecl_lock_handle, 0, 0);
1159
1160         echo_put_object (ecl->ecl_object);
1161         OBD_FREE (ecl, sizeof (*ecl));
1162
1163         return rc;
1164 }
1165
1166 static int
1167 echo_client_iocontrol(unsigned int cmd, struct obd_export *exp,
1168                       int len, void *karg, void *uarg)
1169 {
1170         struct obd_device      *obd;
1171         struct echo_client_obd *ec;
1172         struct ec_object       *eco;
1173         struct obd_ioctl_data  *data = karg;
1174         struct obd_trans_info   dummy_oti;
1175         struct oti_req_ack_lock *ack_lock;
1176         struct obdo            *oa;
1177         int                     rw = OBD_BRW_READ;
1178         int                     rc = 0;
1179         int                     i;
1180         ENTRY;
1181
1182         unlock_kernel();
1183
1184         memset(&dummy_oti, 0, sizeof(dummy_oti));
1185
1186         obd = exp->exp_obd;
1187         ec = &obd->u.echo_client;
1188
1189         switch (cmd) {
1190         case OBD_IOC_CREATE:                    /* may create echo object */
1191                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1192                         GOTO (out, rc = -EPERM);
1193
1194                 rc = echo_create_object (obd, 1, &data->ioc_obdo1,
1195                                          data->ioc_pbuf1, data->ioc_plen1,
1196                                          &dummy_oti);
1197                 GOTO(out, rc);
1198
1199         case OBD_IOC_DESTROY:
1200                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1201                         GOTO (out, rc = -EPERM);
1202                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1203                 if (rc == 0) {
1204                         oa = &data->ioc_obdo1;
1205                         oa->o_gr = FILTER_GROUP_ECHO;
1206                         oa->o_valid |= OBD_MD_FLGROUP;
1207                         rc = obd_destroy(ec->ec_exp, oa, eco->eco_lsm,
1208                                          &dummy_oti, NULL);
1209                         if (rc == 0)
1210                                 eco->eco_deleted = 1;
1211                         echo_put_object(eco);
1212                 }
1213                 GOTO(out, rc);
1214
1215         case OBD_IOC_GETATTR:
1216                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1217                 if (rc == 0) {
1218                         struct obd_info oinfo = { { { 0 } } };
1219                         oinfo.oi_md = eco->eco_lsm;
1220                         oinfo.oi_oa = &data->ioc_obdo1;
1221                         rc = obd_getattr(ec->ec_exp, &oinfo);
1222                         echo_put_object(eco);
1223                 }
1224                 GOTO(out, rc);
1225
1226         case OBD_IOC_SETATTR:
1227                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1228                         GOTO (out, rc = -EPERM);
1229
1230                 rc = echo_get_object (&eco, obd, &data->ioc_obdo1);
1231                 if (rc == 0) {
1232                         struct obd_info oinfo = { { { 0 } } };
1233                         oinfo.oi_oa = &data->ioc_obdo1;
1234                         oinfo.oi_md = eco->eco_lsm;
1235
1236                         rc = obd_setattr(ec->ec_exp, &oinfo, NULL);
1237                         echo_put_object(eco);
1238                 }
1239                 GOTO(out, rc);
1240
1241         case OBD_IOC_BRW_WRITE:
1242                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1243                         GOTO (out, rc = -EPERM);
1244
1245                 rw = OBD_BRW_WRITE;
1246                 /* fall through */
1247         case OBD_IOC_BRW_READ:
1248                 rc = echo_client_brw_ioctl(rw, exp, data);
1249                 GOTO(out, rc);
1250
1251         case ECHO_IOC_GET_STRIPE:
1252                 rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1253                 if (rc == 0) {
1254                         rc = echo_copyout_lsm(eco->eco_lsm, data->ioc_pbuf1,
1255                                               data->ioc_plen1);
1256                         echo_put_object(eco);
1257                 }
1258                 GOTO(out, rc);
1259
1260         case ECHO_IOC_SET_STRIPE:
1261                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1262                         GOTO (out, rc = -EPERM);
1263
1264                 if (data->ioc_pbuf1 == NULL) {  /* unset */
1265                         rc = echo_get_object(&eco, obd, &data->ioc_obdo1);
1266                         if (rc == 0) {
1267                                 eco->eco_deleted = 1;
1268                                 echo_put_object(eco);
1269                         }
1270                 } else {
1271                         rc = echo_create_object(obd, 0, &data->ioc_obdo1,
1272                                                 data->ioc_pbuf1,
1273                                                 data->ioc_plen1, &dummy_oti);
1274                 }
1275                 GOTO (out, rc);
1276
1277         case ECHO_IOC_ENQUEUE:
1278                 if (!cfs_capable(CFS_CAP_SYS_ADMIN))
1279                         GOTO (out, rc = -EPERM);
1280
1281                 rc = echo_client_enqueue(exp, &data->ioc_obdo1,
1282                                          data->ioc_conn1, /* lock mode */
1283                                    data->ioc_offset, data->ioc_count);/*extent*/
1284                 GOTO (out, rc);
1285
1286         case ECHO_IOC_CANCEL:
1287                 rc = echo_client_cancel(exp, &data->ioc_obdo1);
1288                 GOTO (out, rc);
1289
1290         default:
1291                 CERROR ("echo_ioctl(): unrecognised ioctl %#x\n", cmd);
1292                 GOTO (out, rc = -ENOTTY);
1293         }
1294
1295         EXIT;
1296  out:
1297
1298         /* XXX this should be in a helper also called by target_send_reply */
1299         for (ack_lock = dummy_oti.oti_ack_locks, i = 0; i < 4;
1300              i++, ack_lock++) {
1301                 if (!ack_lock->mode)
1302                         break;
1303                 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1304         }
1305
1306         lock_kernel();
1307
1308         return rc;
1309 }
1310
1311 static int
1312 echo_client_setup(struct obd_device *obddev, obd_count len, void *buf)
1313 {
1314         struct lustre_cfg* lcfg = buf;
1315         struct echo_client_obd *ec = &obddev->u.echo_client;
1316         struct obd_device *tgt;
1317         struct lustre_handle conn = {0, };
1318         struct obd_uuid echo_uuid = { "ECHO_UUID" };
1319         struct obd_connect_data *ocd = NULL;
1320         int rc;
1321         ENTRY;
1322
1323         if (lcfg->lcfg_bufcount < 2 || LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
1324                 CERROR("requires a TARGET OBD name\n");
1325                 RETURN(-EINVAL);
1326         }
1327
1328         tgt = class_name2obd(lustre_cfg_string(lcfg, 1));
1329         if (!tgt || !tgt->obd_attached || !tgt->obd_set_up) {
1330                 CERROR("device not attached or not set up (%s)\n",
1331                        lustre_cfg_string(lcfg, 1));
1332                 RETURN(-EINVAL);
1333         }
1334
1335         spin_lock_init (&ec->ec_lock);
1336         CFS_INIT_LIST_HEAD (&ec->ec_objects);
1337         ec->ec_unique = 0;
1338
1339         ec->ec_exp = lustre_hash_lookup(tgt->obd_uuid_hash, &echo_uuid);
1340         if (ec->ec_exp)
1341                 RETURN(0);
1342
1343         OBD_ALLOC(ocd, sizeof(*ocd));
1344         if (ocd == NULL) {
1345                 CERROR("Can't alloc ocd connecting to %s\n",
1346                        lustre_cfg_string(lcfg, 1));
1347                 return -ENOMEM;
1348         }
1349         ocd->ocd_connect_flags = OBD_CONNECT_VERSION | OBD_CONNECT_REQPORTAL;
1350         ocd->ocd_version = LUSTRE_VERSION_CODE;
1351
1352         if ((strncmp(tgt->obd_type->typ_name, LUSTRE_OSC_NAME,
1353                      strlen(LUSTRE_OSC_NAME)) == 0) ||
1354             (strncmp(tgt->obd_type->typ_name, LUSTRE_LOV_NAME,
1355                      strlen(LUSTRE_LOV_NAME)) == 0)) {
1356                 rc = obd_connect(&conn, tgt, &echo_uuid, ocd, &ec->ec_exp);
1357         } else {
1358                 rc = obd_connect(&conn, tgt, &echo_uuid, ocd, NULL);
1359                 if (rc == 0) {
1360                         ec->ec_exp = class_conn2export(&conn);
1361
1362                         /* Turn off pinger because it connects to tgt obd directly */
1363                         spin_lock(&tgt->obd_dev_lock);
1364                         list_del_init(&ec->ec_exp->exp_obd_chain_timed);
1365                         spin_unlock(&tgt->obd_dev_lock);
1366                 }
1367         }
1368
1369         OBD_FREE(ocd, sizeof(*ocd));
1370
1371         if (rc == -EALREADY && (strncmp(tgt->obd_type->typ_name,LUSTRE_OSC_NAME,
1372                                         strlen(LUSTRE_OSC_NAME)) == 0)) {
1373                 /* OSC obd forbid reconnect already connected import,
1374                  * so we hack creating another export here */
1375                 down_write(&tgt->u.cli.cl_sem);
1376                 rc = class_connect(&conn, tgt, &echo_uuid);
1377                 if (rc == 0) {
1378                         ++tgt->u.cli.cl_conn_count;
1379                         ec->ec_exp = class_conn2export(&conn);
1380                 }
1381                 up_write(&tgt->u.cli.cl_sem);
1382         }
1383
1384         if (rc != 0)
1385                 CERROR("fail to connect to device %s\n",
1386                        lustre_cfg_string(lcfg, 1));
1387
1388         RETURN(rc);
1389 }
1390
1391 static int echo_client_cleanup(struct obd_device *obddev)
1392 {
1393         struct list_head       *el;
1394         struct ec_object       *eco;
1395         struct echo_client_obd *ec = &obddev->u.echo_client;
1396         int rc;
1397         ENTRY;
1398
1399         if (!list_empty(&obddev->obd_exports)) {
1400                 CERROR("still has clients!\n");
1401                 RETURN(-EBUSY);
1402         }
1403
1404         /* XXX assuming sole access */
1405         while (!list_empty(&ec->ec_objects)) {
1406                 el = ec->ec_objects.next;
1407                 eco = list_entry(el, struct ec_object, eco_obj_chain);
1408
1409                 LASSERT(eco->eco_refcount == 0);
1410                 eco->eco_refcount = 1;
1411                 eco->eco_deleted = 1;
1412                 echo_put_object(eco);
1413         }
1414
1415         rc = obd_disconnect(ec->ec_exp);
1416         if (rc != 0)
1417                 CERROR("fail to disconnect device: %d\n", rc);
1418
1419         RETURN(rc);
1420 }
1421
1422 static int echo_client_connect(struct lustre_handle *conn,
1423                                struct obd_device *src, struct obd_uuid *cluuid,
1424                                struct obd_connect_data *data, void *localdata)
1425 {
1426         struct obd_export *exp;
1427         int                rc;
1428
1429         ENTRY;
1430         rc = class_connect(conn, src, cluuid);
1431         if (rc == 0) {
1432                 exp = class_conn2export(conn);
1433                 CFS_INIT_LIST_HEAD(&exp->exp_ec_data.eced_locks);
1434                 class_export_put(exp);
1435         }
1436
1437         RETURN (rc);
1438 }
1439
1440 static int echo_client_disconnect(struct obd_export *exp)
1441 {
1442         struct obd_device      *obd;
1443         struct echo_client_obd *ec;
1444         struct ec_lock         *ecl;
1445         int                     rc;
1446         ENTRY;
1447
1448         if (exp == NULL)
1449                 GOTO(out, rc = -EINVAL);
1450
1451         obd = exp->exp_obd;
1452         ec = &obd->u.echo_client;
1453
1454         /* no more contention on export's lock list */
1455         while (!list_empty (&exp->exp_ec_data.eced_locks)) {
1456                 ecl = list_entry (exp->exp_ec_data.eced_locks.next,
1457                                   struct ec_lock, ecl_exp_chain);
1458                 list_del (&ecl->ecl_exp_chain);
1459
1460                 rc = obd_cancel(ec->ec_exp, ecl->ecl_object->eco_lsm,
1461                                  ecl->ecl_mode, &ecl->ecl_lock_handle, 0, 0);
1462
1463                 CDEBUG (D_INFO, "Cancel lock on object "LPX64" on disconnect "
1464                         "(%d)\n", ecl->ecl_object->eco_id, rc);
1465
1466                 echo_put_object (ecl->ecl_object);
1467                 OBD_FREE (ecl, sizeof (*ecl));
1468         }
1469
1470         rc = class_disconnect(exp);
1471         GOTO(out, rc);
1472  out:
1473         return rc;
1474 }
1475
1476 static struct obd_ops echo_obd_ops = {
1477         .o_owner       = THIS_MODULE,
1478         .o_setup       = echo_client_setup,
1479         .o_cleanup     = echo_client_cleanup,
1480         .o_iocontrol   = echo_client_iocontrol,
1481         .o_connect     = echo_client_connect,
1482         .o_disconnect  = echo_client_disconnect
1483 };
1484
1485 int echo_client_init(void)
1486 {
1487         struct lprocfs_static_vars lvars = { 0 };
1488
1489         lprocfs_echo_init_vars(&lvars);
1490         return class_register_type(&echo_obd_ops, lvars.module_vars,
1491                                    LUSTRE_ECHO_CLIENT_NAME);
1492 }
1493
1494 void echo_client_exit(void)
1495 {
1496         class_unregister_type(LUSTRE_ECHO_CLIENT_NAME);
1497 }