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