Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / obdecho / echo.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  2008 Sun Microsystems, Inc. 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  * lustre/obdecho/echo.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  */
41
42 #ifndef EXPORT_SYMTAB
43 # define EXPORT_SYMTAB
44 #endif
45
46 #define DEBUG_SUBSYSTEM S_ECHO
47
48 #include <obd_support.h>
49 #include <obd_class.h>
50 #include <lustre_debug.h>
51 #include <lustre_dlm.h>
52 #include <lprocfs_status.h>
53
54 #include "echo_internal.h"
55
56 #define ECHO_INIT_OBJID      0x1000000000000000ULL
57 #define ECHO_HANDLE_MAGIC    0xabcd0123fedc9876ULL
58
59 #define ECHO_PERSISTENT_PAGES (ECHO_PERSISTENT_SIZE >> CFS_PAGE_SHIFT)
60 static cfs_page_t *echo_persistent_pages[ECHO_PERSISTENT_PAGES];
61
62 enum {
63         LPROC_ECHO_READ_BYTES = 1,
64         LPROC_ECHO_WRITE_BYTES = 2,
65         LPROC_ECHO_LAST = LPROC_ECHO_WRITE_BYTES +1
66 };
67
68 static int echo_connect(const struct lu_env *env,
69                         struct obd_export **exp, struct obd_device *obd,
70                         struct obd_uuid *cluuid, struct obd_connect_data *data,
71                         void *localdata)
72 {
73         struct lustre_handle conn = { 0 };
74         int rc;
75
76         data->ocd_connect_flags &= ECHO_CONNECT_SUPPORTED;
77         rc = class_connect(&conn, obd, cluuid);
78         if (rc) {
79                 CERROR("can't connect %d\n", rc);
80                 return rc;
81         }
82         *exp = class_conn2export(&conn);
83
84         return 0;
85 }
86
87 static int echo_disconnect(struct obd_export *exp)
88 {
89         LASSERT (exp != NULL);
90
91         ldlm_cancel_locks_for_export(exp);
92
93         /* complete all outstanding replies */
94         spin_lock(&exp->exp_lock);
95         while (!list_empty(&exp->exp_outstanding_replies)) {
96                 struct ptlrpc_reply_state *rs =
97                         list_entry(exp->exp_outstanding_replies.next,
98                                    struct ptlrpc_reply_state, rs_exp_list);
99                 struct ptlrpc_service *svc = rs->rs_service;
100
101                 spin_lock(&svc->srv_lock);
102                 list_del_init(&rs->rs_exp_list);
103                 spin_lock(&rs->rs_lock);
104                 ptlrpc_schedule_difficult_reply(rs);
105                 spin_unlock(&rs->rs_lock);
106                 spin_unlock(&svc->srv_lock);
107         }
108         spin_unlock(&exp->exp_lock);
109
110         return class_disconnect(exp);
111 }
112
113 static int echo_init_export(struct obd_export *exp)
114 {
115         return ldlm_init_export(exp);
116 }
117
118 static int echo_destroy_export(struct obd_export *exp)
119 {
120         ENTRY;
121
122         target_destroy_export(exp);
123         ldlm_destroy_export(exp);
124
125         RETURN(0);
126 }
127
128  static __u64 echo_next_id(struct obd_device *obddev)
129 {
130         obd_id id;
131
132         spin_lock(&obddev->u.echo.eo_lock);
133         id = ++obddev->u.echo.eo_lastino;
134         spin_unlock(&obddev->u.echo.eo_lock);
135
136         return id;
137 }
138
139 int echo_create(struct obd_export *exp, struct obdo *oa,
140                 struct lov_stripe_md **ea, struct obd_trans_info *oti)
141 {
142         struct obd_device *obd = class_exp2obd(exp);
143
144         if (!obd) {
145                 CERROR("invalid client cookie "LPX64"\n",
146                        exp->exp_handle.h_cookie);
147                 return -EINVAL;
148         }
149
150         if (!(oa->o_mode && S_IFMT)) {
151                 CERROR("echo obd: no type!\n");
152                 return -ENOENT;
153         }
154
155         if (!(oa->o_valid & OBD_MD_FLTYPE)) {
156                 CERROR("invalid o_valid "LPX64"\n", oa->o_valid);
157                 return -EINVAL;
158         }
159
160         oa->o_id = echo_next_id(obd);
161         oa->o_valid = OBD_MD_FLID;
162
163         return 0;
164 }
165
166 int echo_destroy(struct obd_export *exp, struct obdo *oa,
167                  struct lov_stripe_md *ea, struct obd_trans_info *oti,
168                  struct obd_export *md_exp, void *capa)
169 {
170         struct obd_device *obd = class_exp2obd(exp);
171
172         ENTRY;
173         if (!obd) {
174                 CERROR("invalid client cookie "LPX64"\n",
175                        exp->exp_handle.h_cookie);
176                 RETURN(-EINVAL);
177         }
178
179         if (!(oa->o_valid & OBD_MD_FLID)) {
180                 CERROR("obdo missing FLID valid flag: "LPX64"\n", oa->o_valid);
181                 RETURN(-EINVAL);
182         }
183
184         if (oa->o_id > obd->u.echo.eo_lastino || oa->o_id < ECHO_INIT_OBJID) {
185                 CERROR("bad destroy objid: "LPX64"\n", oa->o_id);
186                 RETURN(-EINVAL);
187         }
188
189         RETURN(0);
190 }
191
192 static int echo_getattr(struct obd_export *exp, struct obd_info *oinfo)
193 {
194         struct obd_device *obd = class_exp2obd(exp);
195         obd_id id = oinfo->oi_oa->o_id;
196
197         ENTRY;
198         if (!obd) {
199                 CERROR("invalid client cookie "LPX64"\n",
200                        exp->exp_handle.h_cookie);
201                 RETURN(-EINVAL);
202         }
203
204         if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
205                 CERROR("obdo missing FLID valid flag: "LPX64"\n",
206                        oinfo->oi_oa->o_valid);
207                 RETURN(-EINVAL);
208         }
209
210         obdo_cpy_md(oinfo->oi_oa, &obd->u.echo.eo_oa, oinfo->oi_oa->o_valid);
211         oinfo->oi_oa->o_id = id;
212
213         RETURN(0);
214 }
215
216 static int echo_setattr(struct obd_export *exp, struct obd_info *oinfo,
217                         struct obd_trans_info *oti)
218 {
219         struct obd_device *obd = class_exp2obd(exp);
220
221         ENTRY;
222         if (!obd) {
223                 CERROR("invalid client cookie "LPX64"\n",
224                        exp->exp_handle.h_cookie);
225                 RETURN(-EINVAL);
226         }
227
228         if (!(oinfo->oi_oa->o_valid & OBD_MD_FLID)) {
229                 CERROR("obdo missing FLID valid flag: "LPX64"\n",
230                        oinfo->oi_oa->o_valid);
231                 RETURN(-EINVAL);
232         }
233
234         memcpy(&obd->u.echo.eo_oa, oinfo->oi_oa, sizeof(*oinfo->oi_oa));
235
236         if (oinfo->oi_oa->o_id & 4) {
237                 /* Save lock to force ACKed reply */
238                 ldlm_lock_addref (&obd->u.echo.eo_nl_lock, LCK_NL);
239                 oti->oti_ack_locks[0].mode = LCK_NL;
240                 oti->oti_ack_locks[0].lock = obd->u.echo.eo_nl_lock;
241         }
242
243         RETURN(0);
244 }
245
246 static void
247 echo_page_debug_setup(cfs_page_t *page, int rw, obd_id id,
248                       __u64 offset, int len)
249 {
250         int   page_offset = offset & ~CFS_PAGE_MASK;
251         char *addr        = ((char *)cfs_kmap(page)) + page_offset;
252
253         if (len % OBD_ECHO_BLOCK_SIZE != 0)
254                 CERROR("Unexpected block size %d\n", len);
255
256         while (len > 0) {
257                 if (rw & OBD_BRW_READ)
258                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
259                                           offset, id);
260                 else
261                         block_debug_setup(addr, OBD_ECHO_BLOCK_SIZE,
262                                           0xecc0ecc0ecc0ecc0ULL,
263                                           0xecc0ecc0ecc0ecc0ULL);
264
265                 addr   += OBD_ECHO_BLOCK_SIZE;
266                 offset += OBD_ECHO_BLOCK_SIZE;
267                 len    -= OBD_ECHO_BLOCK_SIZE;
268         }
269
270         cfs_kunmap(page);
271 }
272
273 static int
274 echo_page_debug_check(cfs_page_t *page, obd_id id,
275                       __u64 offset, int len)
276 {
277         int   page_offset = offset & ~CFS_PAGE_MASK;
278         char *addr        = ((char *)cfs_kmap(page)) + page_offset;
279         int   rc          = 0;
280         int   rc2;
281
282         if (len % OBD_ECHO_BLOCK_SIZE != 0)
283                 CERROR("Unexpected block size %d\n", len);
284
285         while (len > 0) {
286                 rc2 = block_debug_check("echo", addr, OBD_ECHO_BLOCK_SIZE,
287                                         offset, id);
288
289                 if (rc2 != 0 && rc == 0)
290                         rc = rc2;
291
292                 addr   += OBD_ECHO_BLOCK_SIZE;
293                 offset += OBD_ECHO_BLOCK_SIZE;
294                 len    -= OBD_ECHO_BLOCK_SIZE;
295         }
296
297         cfs_kunmap(page);
298
299         return (rc);
300 }
301
302 /* This allows us to verify that desc_private is passed unmolested */
303 #define DESC_PRIV 0x10293847
304
305 static int echo_map_nb_to_lb(struct obdo *oa, struct obd_ioobj *obj,
306                              struct niobuf_remote *nb, int *pages,
307                              struct niobuf_local *lb, int cmd, int *left)
308 {
309         int gfp_mask = (obj->ioo_id & 1) ? CFS_ALLOC_HIGHUSER : CFS_ALLOC_STD;
310         int ispersistent = obj->ioo_id == ECHO_PERSISTENT_OBJID;
311         int debug_setup = (!ispersistent &&
312                            (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
313                            (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
314         struct niobuf_local *res = lb;
315         obd_off offset = nb->offset;
316         int len = nb->len;
317
318         while (len > 0) {
319                 int plen = CFS_PAGE_SIZE - (offset & (CFS_PAGE_SIZE-1));
320                 if (len < plen)
321                         plen = len;
322
323                 /* check for local buf overflow */
324                 if (*left == 0)
325                         return -EINVAL;
326
327                 res->offset = offset;
328                 res->len = plen;
329                 LASSERT((res->offset & ~CFS_PAGE_MASK) + res->len <= CFS_PAGE_SIZE);
330
331
332                 if (ispersistent &&
333                     (res->offset >> CFS_PAGE_SHIFT) < ECHO_PERSISTENT_PAGES) {
334                         res->page = echo_persistent_pages[res->offset >>
335                                 CFS_PAGE_SHIFT];
336                         /* Take extra ref so __free_pages() can be called OK */
337                         cfs_get_page (res->page);
338                 } else {
339                         OBD_PAGE_ALLOC(res->page, gfp_mask);
340                         if (res->page == NULL) {
341                                 CERROR("can't get page for id " LPU64"\n",
342                                        obj->ioo_id);
343                                 return -ENOMEM;
344                         }
345                 }
346
347                 CDEBUG(D_PAGE, "$$$$ get page %p @ "LPU64" for %d\n",
348                        res->page, res->offset, res->len);
349
350                 if (cmd & OBD_BRW_READ)
351                         res->rc = res->len;
352
353                 if (debug_setup)
354                         echo_page_debug_setup(res->page, cmd, obj->ioo_id,
355                                               res->offset, res->len);
356
357                 offset += plen;
358                 len -= plen;
359                 res++;
360
361                 (*left)--;
362                 (*pages)++;
363         }
364
365         return 0;
366 }
367
368 static int echo_finalize_lb(struct obdo *oa, struct obd_ioobj *obj,
369                             struct niobuf_remote *rb, int *pgs,
370                             struct niobuf_local *lb, int verify)
371 {
372         struct niobuf_local *res = lb;
373         obd_off start  = rb->offset >> CFS_PAGE_SHIFT;
374         obd_off end    = (rb->offset + rb->len + CFS_PAGE_SIZE - 1) >> CFS_PAGE_SHIFT;
375         int     count  = (int)(end - start);
376         int     rc     = 0;
377         int     i;
378
379         for (i = 0; i < count; i++, (*pgs) ++, res++) {
380                 cfs_page_t *page = res->page;
381                 void       *addr;
382
383                 if (page == NULL) {
384                         CERROR("null page objid "LPU64":%p, buf %d/%d\n",
385                                obj->ioo_id, page, i, obj->ioo_bufcnt);
386                         return -EFAULT;
387                 }
388
389                 addr = cfs_kmap(page);
390
391                 CDEBUG(D_PAGE, "$$$$ use page %p, addr %p@"LPU64"\n",
392                        res->page, addr, res->offset);
393
394                 if (verify) {
395                         int vrc = echo_page_debug_check(page, obj->ioo_id,
396                                                         res->offset, res->len);
397                         /* check all the pages always */
398                         if (vrc != 0 && rc == 0)
399                                 rc = vrc;
400                 }
401
402                 cfs_kunmap(page);
403                 /* NB see comment above regarding persistent pages */
404                 OBD_PAGE_FREE(page);
405         }
406
407         return rc;
408 }
409
410 int echo_preprw(int cmd, struct obd_export *export, struct obdo *oa,
411                 int objcount, struct obd_ioobj *obj, struct niobuf_remote *nb,
412                 int *pages, struct niobuf_local *res,
413                 struct obd_trans_info *oti, struct lustre_capa *unused)
414 {
415         struct obd_device *obd;
416         int tot_bytes = 0;
417         int rc = 0;
418         int i, left;
419         ENTRY;
420
421         obd = export->exp_obd;
422         if (obd == NULL)
423                 RETURN(-EINVAL);
424
425         /* Temp fix to stop falling foul of osc_announce_cached() */
426         oa->o_valid &= ~(OBD_MD_FLBLOCKS | OBD_MD_FLGRANT);
427
428         memset(res, 0, sizeof(*res) * *pages);
429
430         CDEBUG(D_PAGE, "%s %d obdos with %d IOs\n",
431                cmd == OBD_BRW_READ ? "reading" : "writing", objcount, *pages);
432
433         if (oti)
434                 oti->oti_handle = (void *)DESC_PRIV;
435
436         left = *pages;
437         *pages = 0;
438
439         for (i = 0; i < objcount; i++, obj++) {
440                 int j;
441
442                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, nb++) {
443
444                         rc = echo_map_nb_to_lb(oa, obj, nb, pages,
445                                                res + *pages, cmd, &left);
446                         if (rc)
447                                 GOTO(preprw_cleanup, rc);
448
449                         tot_bytes += nb->len;
450                 }
451         }
452
453         atomic_add(*pages, &obd->u.echo.eo_prep);
454
455         if (cmd & OBD_BRW_READ)
456                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_READ_BYTES,
457                                     tot_bytes);
458         else
459                 lprocfs_counter_add(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
460                                     tot_bytes);
461
462         CDEBUG(D_PAGE, "%d pages allocated after prep\n",
463                atomic_read(&obd->u.echo.eo_prep));
464
465         RETURN(0);
466
467 preprw_cleanup:
468         /* It is possible that we would rather handle errors by  allow
469          * any already-set-up pages to complete, rather than tearing them
470          * all down again.  I believe that this is what the in-kernel
471          * prep/commit operations do.
472          */
473         CERROR("cleaning up %u pages (%d obdos)\n", *pages, objcount);
474         for (i = 0; i < *pages; i++) {
475                 cfs_kunmap(res[i].page);
476                 /* NB if this is a persistent page, __free_pages will just
477                  * lose the extra ref gained above */
478                 OBD_PAGE_FREE(res[i].page);
479                 res[i].page = NULL;
480                 atomic_dec(&obd->u.echo.eo_prep);
481         }
482
483         return rc;
484 }
485
486 int echo_commitrw(int cmd, struct obd_export *export, struct obdo *oa,
487                   int objcount, struct obd_ioobj *obj,
488                   struct niobuf_remote *rb, int niocount,
489                   struct niobuf_local *res, struct obd_trans_info *oti, int rc)
490 {
491         struct obd_device *obd;
492         int pgs = 0;
493         int i;
494         ENTRY;
495
496         obd = export->exp_obd;
497         if (obd == NULL)
498                 RETURN(-EINVAL);
499
500         if (rc)
501                 GOTO(commitrw_cleanup, rc);
502
503         if ((cmd & OBD_BRW_RWMASK) == OBD_BRW_READ) {
504                 CDEBUG(D_PAGE, "reading %d obdos with %d IOs\n",
505                        objcount, niocount);
506         } else {
507                 CDEBUG(D_PAGE, "writing %d obdos with %d IOs\n",
508                        objcount, niocount);
509         }
510
511         if (niocount && res == NULL) {
512                 CERROR("NULL res niobuf with niocount %d\n", niocount);
513                 RETURN(-EINVAL);
514         }
515
516         LASSERT(oti == NULL || oti->oti_handle == (void *)DESC_PRIV);
517
518         for (i = 0; i < objcount; i++, obj++) {
519                 int verify = (rc == 0 &&
520                               obj->ioo_id != ECHO_PERSISTENT_OBJID &&
521                               (oa->o_valid & OBD_MD_FLFLAGS) != 0 &&
522                               (oa->o_flags & OBD_FL_DEBUG_CHECK) != 0);
523                 int j;
524
525                 for (j = 0 ; j < obj->ioo_bufcnt ; j++, rb++) {
526                         int vrc = echo_finalize_lb(oa, obj, rb, &pgs, &res[pgs], verify);
527
528                         if (vrc == 0)
529                                 continue;
530
531                         if (vrc == -EFAULT)
532                                 GOTO(commitrw_cleanup, rc = vrc);
533
534                         if (rc == 0)
535                                 rc = vrc;
536                 }
537
538         }
539
540         atomic_sub(pgs, &obd->u.echo.eo_prep);
541
542         CDEBUG(D_PAGE, "%d pages remain after commit\n",
543                atomic_read(&obd->u.echo.eo_prep));
544         RETURN(rc);
545
546 commitrw_cleanup:
547         atomic_sub(pgs, &obd->u.echo.eo_prep);
548
549         CERROR("cleaning up %d pages (%d obdos)\n",
550                niocount - pgs - 1, objcount);
551
552         while (pgs ++ < niocount) {
553                 cfs_page_t *page = res[pgs].page;
554
555                 if (page == NULL)
556                         continue;
557
558                 /* NB see comment above regarding persistent pages */
559                 OBD_PAGE_FREE(page);
560                 atomic_dec(&obd->u.echo.eo_prep);
561         }
562         return rc;
563 }
564
565 static int echo_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
566 {
567         struct lprocfs_static_vars lvars;
568         int                        rc;
569         int                        lock_flags = 0;
570         struct ldlm_res_id         res_id = {.name = {1}};
571         char                       ns_name[48];
572         ENTRY;
573
574         spin_lock_init(&obd->u.echo.eo_lock);
575         obd->u.echo.eo_lastino = ECHO_INIT_OBJID;
576
577         sprintf(ns_name, "echotgt-%s", obd->obd_uuid.uuid);
578         obd->obd_namespace = ldlm_namespace_new(obd, ns_name,
579                                                 LDLM_NAMESPACE_SERVER,
580                                                 LDLM_NAMESPACE_MODEST);
581         if (obd->obd_namespace == NULL) {
582                 LBUG();
583                 RETURN(-ENOMEM);
584         }
585
586         rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id, LDLM_PLAIN,
587                                     NULL, LCK_NL, &lock_flags, NULL,
588                                     ldlm_completion_ast, NULL, NULL,
589                                     0, NULL, NULL, &obd->u.echo.eo_nl_lock);
590         LASSERT (rc == ELDLM_OK);
591
592         lprocfs_echo_init_vars(&lvars);
593         if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0 &&
594             lprocfs_alloc_obd_stats(obd, LPROC_ECHO_LAST) == 0) {
595                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_READ_BYTES,
596                                      LPROCFS_CNTR_AVGMINMAX,
597                                      "read_bytes", "bytes");
598                 lprocfs_counter_init(obd->obd_stats, LPROC_ECHO_WRITE_BYTES,
599                                      LPROCFS_CNTR_AVGMINMAX,
600                                      "write_bytes", "bytes");
601         }
602
603         ptlrpc_init_client (LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
604                             "echo_ldlm_cb_client", &obd->obd_ldlm_client);
605         RETURN(0);
606 }
607
608 static int echo_cleanup(struct obd_device *obd)
609 {
610         int leaked;
611         ENTRY;
612
613         lprocfs_obd_cleanup(obd);
614         lprocfs_free_obd_stats(obd);
615
616         ldlm_lock_decref (&obd->u.echo.eo_nl_lock, LCK_NL);
617
618         /* XXX Bug 3413; wait for a bit to ensure the BL callback has
619          * happened before calling ldlm_namespace_free() */
620         cfs_schedule_timeout (CFS_TASK_UNINT, cfs_time_seconds(1));
621
622         ldlm_namespace_free(obd->obd_namespace, NULL, obd->obd_force);
623         obd->obd_namespace = NULL;
624
625         leaked = atomic_read(&obd->u.echo.eo_prep);
626         if (leaked != 0)
627                 CERROR("%d prep/commitrw pages leaked\n", leaked);
628
629         RETURN(0);
630 }
631
632 static struct obd_ops echo_obd_ops = {
633         .o_owner           = THIS_MODULE,
634         .o_connect         = echo_connect,
635         .o_disconnect      = echo_disconnect,
636         .o_init_export     = echo_init_export,
637         .o_destroy_export  = echo_destroy_export,
638         .o_create          = echo_create,
639         .o_destroy         = echo_destroy,
640         .o_getattr         = echo_getattr,
641         .o_setattr         = echo_setattr,
642         .o_preprw          = echo_preprw,
643         .o_commitrw        = echo_commitrw,
644         .o_setup           = echo_setup,
645         .o_cleanup         = echo_cleanup
646 };
647
648 extern int echo_client_init(void);
649 extern void echo_client_exit(void);
650
651 static void
652 echo_persistent_pages_fini (void)
653 {
654         int     i;
655
656         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++)
657                 if (echo_persistent_pages[i] != NULL) {
658                         OBD_PAGE_FREE(echo_persistent_pages[i]);
659                         echo_persistent_pages[i] = NULL;
660                 }
661 }
662
663 static int
664 echo_persistent_pages_init (void)
665 {
666         cfs_page_t *pg;
667         int          i;
668
669         for (i = 0; i < ECHO_PERSISTENT_PAGES; i++) {
670                 int gfp_mask = (i < ECHO_PERSISTENT_PAGES/2) ?
671                         CFS_ALLOC_STD : CFS_ALLOC_HIGHUSER;
672
673                 OBD_PAGE_ALLOC(pg, gfp_mask);
674                 if (pg == NULL) {
675                         echo_persistent_pages_fini ();
676                         return (-ENOMEM);
677                 }
678
679                 memset (cfs_kmap (pg), 0, CFS_PAGE_SIZE);
680                 cfs_kunmap (pg);
681
682                 echo_persistent_pages[i] = pg;
683         }
684
685         return (0);
686 }
687
688 static int __init obdecho_init(void)
689 {
690         struct lprocfs_static_vars lvars;
691         int rc;
692
693         ENTRY;
694         LCONSOLE_INFO("Echo OBD driver; http://www.lustre.org/\n");
695
696         LASSERT(CFS_PAGE_SIZE % OBD_ECHO_BLOCK_SIZE == 0);
697
698         lprocfs_echo_init_vars(&lvars);
699
700         rc = echo_persistent_pages_init ();
701         if (rc != 0)
702                 goto failed_0;
703
704         rc = class_register_type(&echo_obd_ops, NULL, lvars.module_vars,
705                                  LUSTRE_ECHO_NAME, NULL);
706         if (rc != 0)
707                 goto failed_1;
708
709         rc = echo_client_init();
710         if (rc == 0)
711                 RETURN (0);
712
713         class_unregister_type(LUSTRE_ECHO_NAME);
714  failed_1:
715         echo_persistent_pages_fini ();
716  failed_0:
717         RETURN(rc);
718 }
719
720 static void /*__exit*/ obdecho_exit(void)
721 {
722         echo_client_exit();
723         class_unregister_type(LUSTRE_ECHO_NAME);
724         echo_persistent_pages_fini ();
725 }
726
727 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
728 MODULE_DESCRIPTION("Lustre Testing Echo OBD driver");
729 MODULE_LICENSE("GPL");
730
731 cfs_module(obdecho, "1.0.0", obdecho_init, obdecho_exit);